From 0789a0d721d85d1eaceda33ddc6c6d4bd4d6e57b Mon Sep 17 00:00:00 2001 From: "n.hoffmann" Date: Tue, 27 Oct 2009 17:31:34 +0000 Subject: [PATCH 1/1] fixes #1014 --- .gitattributes | 3 + .../ProtologuePropertyDescriptor.java | 58 ++++ .../ProtologuePropertySource.java | 58 ++++ .../propertysheet/ProtologuesDialog.java | 266 ++++++++++++++++++ .../name/NonViralNamePropertySource.java | 36 +++ 5 files changed, 421 insertions(+) create mode 100644 taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertyDescriptor.java create mode 100644 taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertySource.java create mode 100644 taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuesDialog.java diff --git a/.gitattributes b/.gitattributes index c365190ca..0fb315f13 100644 --- a/.gitattributes +++ b/.gitattributes @@ -870,6 +870,9 @@ taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/MarkersDialo taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/MarkersPropertyDescriptor.java -text taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/MarkersPropertySource.java -text taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/PropertySheetUtil.java -text +taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertyDescriptor.java -text +taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertySource.java -text +taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuesDialog.java -text taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/SelectMarkerTypeDialog.java -text taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/SourceViewerConfig.java -text taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/TimePeriodPropertySource.java -text diff --git a/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertyDescriptor.java b/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertyDescriptor.java new file mode 100644 index 000000000..2569b261f --- /dev/null +++ b/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertyDescriptor.java @@ -0,0 +1,58 @@ +/** + * + */ +package eu.etaxonomy.taxeditor.propertysheet; + +import java.util.Set; + +import org.eclipse.jface.viewers.CellEditor; +import org.eclipse.jface.viewers.DialogCellEditor; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Dialog; +import org.eclipse.ui.views.properties.PropertyDescriptor; + +import eu.etaxonomy.cdm.model.description.TaxonNameDescription; +import eu.etaxonomy.cdm.model.description.TextData; +import eu.etaxonomy.cdm.model.name.TaxonNameBase; + +/** + * @author nho + * + */ +public abstract class ProtologuePropertyDescriptor extends PropertyDescriptor { + private TaxonNameDescription description; + + public ProtologuePropertyDescriptor(Object id, String displayName, TaxonNameDescription description) { + super(id, displayName); + this.description = description; + } + + public CellEditor createPropertyEditor(Composite parent) { + CellEditor editor = new DialogCellEditor(parent) { + + protected Dialog dialog; + + @Override + protected Object openDialogBox( + Control cellEditorWindow) { + dialog = new ProtologuesDialog(cellEditorWindow.getShell(), description); + Object result = ((ProtologuesDialog) dialog).open(); + if (result instanceof Set) { + + Set protologues = (Set) result; + saveProtologue(protologues); + return new ProtologuePropertySource(description); + } + return null; + } + + }; + if (getValidator() != null) { + editor.setValidator(getValidator()); + } + return editor; + } + + abstract protected void saveProtologue(Set set); +} diff --git a/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertySource.java b/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertySource.java new file mode 100644 index 000000000..ba78976ce --- /dev/null +++ b/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuePropertySource.java @@ -0,0 +1,58 @@ +/** + * + */ +package eu.etaxonomy.taxeditor.propertysheet; + +import eu.etaxonomy.cdm.model.description.DescriptionElementBase; +import eu.etaxonomy.cdm.model.description.TaxonNameDescription; +import eu.etaxonomy.cdm.model.media.Media; +import eu.etaxonomy.cdm.model.media.MediaRepresentation; +import eu.etaxonomy.cdm.model.media.MediaRepresentationPart; + +/** + * @author nho + * + */ +public class ProtologuePropertySource extends CollectionPropertySource { + + private TaxonNameDescription description; + + public ProtologuePropertySource(TaxonNameDescription description) { + super(description.getElements()); + + this.description = description; + } + + @Override + protected String getItemDisplayName(Object item) { + // No display names on individual annotations + return ""; + } + + @Override + public Object getPropertyValue(Object id) { + if (id instanceof DescriptionElementBase) { + DescriptionElementBase descriptionElement = (DescriptionElementBase) id; + for(Media media : descriptionElement.getMedia()){ + for(MediaRepresentation mediaRepresentation : media.getRepresentations()){ + for(MediaRepresentationPart mediaRepresentationPart : mediaRepresentation.getParts()){ + return mediaRepresentationPart.getUri(); + } + } + } + } + return null; + } + + @Override + public void setPropertyValue(Object id, Object value) { + // Fields not editable in property sheet view + } + + @Override + public String toString() { + // "Annotations" header has no value + return ""; + } + +} diff --git a/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuesDialog.java b/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuesDialog.java new file mode 100644 index 000000000..605270e34 --- /dev/null +++ b/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/ProtologuesDialog.java @@ -0,0 +1,266 @@ +/** + * + */ +package eu.etaxonomy.taxeditor.propertysheet; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; + +import org.eclipse.jface.action.Action; +import org.eclipse.jface.viewers.ArrayContentProvider; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Dialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableItem; +import org.eclipse.swt.widgets.Text; + +import eu.etaxonomy.cdm.model.description.DescriptionElementBase; +import eu.etaxonomy.cdm.model.description.Feature; +import eu.etaxonomy.cdm.model.description.TaxonNameDescription; +import eu.etaxonomy.cdm.model.description.TextData; +import eu.etaxonomy.cdm.model.media.Media; +import eu.etaxonomy.cdm.model.media.MediaRepresentation; +import eu.etaxonomy.cdm.model.media.MediaRepresentationPart; +import eu.etaxonomy.cdm.model.name.TaxonNameBase; + +/** + * @author nho + * + */ +public class ProtologuesDialog extends Dialog { + private Text text; + private TableViewer tableViewer; + protected Object result; + protected Shell shell; + +// private WritableList list = new WritableList(); + List protologues = new ArrayList(); + private TaxonNameBase name; + protected RemoveDescriptionElementAction removeAction; + private TaxonNameDescription description; + + /** + * Create the dialog + * @param parent + * @param style + */ + public ProtologuesDialog(Shell parent, int style) { + super(parent, style); + } + + /** + * Create the dialog + * @param parent + */ + public ProtologuesDialog(Shell parent) { + this(parent, SWT.NONE); + } + + public ProtologuesDialog(Shell parent, TaxonNameDescription description) { + this(parent, SWT.NONE); + this.description = description; + // FIXME this is a quick and dirty implementation. Will be obsolete with tabbed properties anyways + // we are looking for description elements with the feature protolog + // TODO this might change very soon because Feature.PROTOLOG is currently in + // the set of Taxon features which does not make sense + // as we want to be able to enter multiple protologues, we are done with filling the list now + for(DescriptionElementBase descriptionElement : description.getElements()){ + if(descriptionElement.getFeature().equals(Feature.PROTOLOG())){ + protologues.add((TextData) descriptionElement); + } + } + } + + /** + * Open the dialog + * @return the result + */ + public Object open() { + createContents(); + shell.open(); + shell.layout(); + Display display = getParent().getDisplay(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + return result; + } + + /** + * Create contents of the dialog + */ + protected void createContents() { + + // Create shell for popup dialog + shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); + shell.setLayout(new FillLayout()); + shell.setSize(500, 375); + shell.setText("Protologues"); + + // Create composite for entire shell + final Composite composite = new Composite(shell, SWT.NONE); + final GridLayout gridLayout = new GridLayout(); + gridLayout.numColumns = 3; + composite.setLayout(gridLayout); + + text = new Text(composite, SWT.BORDER); + text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); + + final Button addButton = new Button(composite, SWT.NONE); + addButton.setLayoutData(new GridData()); + addButton.setText("Add"); + addButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseUp(MouseEvent e) { + if (text.getText().equals("")) { + return; + } + TextData descriptionElement = createNewProtologMediaElement(text.getText()); + + protologues.add(descriptionElement); + description.addElement(descriptionElement); + tableViewer.refresh(); + text.setText(""); + } + }); + + final Button removeButton = new Button(composite, SWT.NONE); + removeButton.setLayoutData(new GridData()); + removeButton.setText("Remove"); + removeButton.setEnabled(false); + removeButton.addMouseListener(new MouseAdapter() { + /* (non-Javadoc) + * @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent) + */ + @Override + public void mouseUp(MouseEvent e) { + removeDescriptionElement(); + } + }); + + tableViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI); + Table table = tableViewer.getTable(); + table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1)); + table.setLinesVisible(false); + table.setHeaderVisible(false); + new Label(composite, SWT.NONE); + + tableViewer.setContentProvider(new ArrayContentProvider()); + + tableViewer.setLabelProvider(new LabelProvider() { + /* (non-Javadoc) + * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object) + */ + @Override + public String getText(Object element) { + DescriptionElementBase descriptionElement = (DescriptionElementBase) element; + + for(Media media : descriptionElement.getMedia()){ + for(MediaRepresentation mediaRepresentation : media.getRepresentations()){ + for(MediaRepresentationPart mediaRepresentationPart : mediaRepresentation.getParts()){ + return mediaRepresentationPart.getUri(); + } + } + } + + return null; + } + }); + tableViewer.setInput(protologues); + +// final MenuManager manager = new MenuManager(); +// final Menu menu = manager.createContextMenu(table); +// table.setMenu(menu); +// +// table.addSelectionListener(new SelectionAdapter() { +// public void widgetSelected(SelectionEvent e) { +// +// Object data = e.item.getData(); +// +// if (data instanceof DescriptionElementBase) { +// manager.removeAll(); +// removeAction = new RemoveDescriptionElementAction(); +// manager.add(removeAction); +// } +// +// removeButton.setEnabled(true); +// } +// }); + + final Button cancelButton = new Button(composite, SWT.NONE); + cancelButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false)); + cancelButton.setText("Cancel"); + cancelButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseUp(MouseEvent e) { + shell.dispose(); + } + }); + + final Button okButton = new Button(composite, SWT.NONE); + final GridData gd_okButton = new GridData(); + okButton.setLayoutData(gd_okButton); + okButton.setText("OK"); + okButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseUp(MouseEvent e) { + result = new HashSet(protologues); + shell.dispose(); + } + }); + } + + private TextData createNewProtologMediaElement(String text) { + TextData descriptionElement = TextData.NewInstance(Feature.PROTOLOG()); + + MediaRepresentationPart mediaRepresentationPart = MediaRepresentationPart.NewInstance(text, 0); + + MediaRepresentation representation = MediaRepresentation.NewInstance("application/pdf", "pdf"); + representation.addRepresentationPart(mediaRepresentationPart); + + Media media = Media.NewInstance(); + media.addRepresentation(representation); + + descriptionElement.addMedia(media); + + return descriptionElement; + } + + private void removeDescriptionElement() { + TableItem[] selectedItems = tableViewer.getTable().getSelection(); + for (TableItem item : selectedItems) { + removeDescriptionElement((DescriptionElementBase) item.getData()); + } + tableViewer.refresh(); + } + + private void removeDescriptionElement(DescriptionElementBase descriptionElement) { + protologues.remove(descriptionElement); + description.removeElement(descriptionElement); + } + + public class RemoveDescriptionElementAction extends Action { + + RemoveDescriptionElementAction() { + setText("Remove"); + } + + public void run() { + removeDescriptionElement(); + } + } +} diff --git a/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/name/NonViralNamePropertySource.java b/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/name/NonViralNamePropertySource.java index d477b8e93..b60b3b941 100644 --- a/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/name/NonViralNamePropertySource.java +++ b/taxeditor-editor/src/main/java/eu/etaxonomy/taxeditor/propertysheet/name/NonViralNamePropertySource.java @@ -26,6 +26,7 @@ import org.eclipse.ui.views.properties.TextPropertyDescriptor; import eu.etaxonomy.cdm.common.CdmUtils; import eu.etaxonomy.cdm.model.common.Marker; +import eu.etaxonomy.cdm.model.description.TaxonNameDescription; import eu.etaxonomy.cdm.model.name.NomenclaturalStatus; import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType; import eu.etaxonomy.cdm.model.name.NonViralName; @@ -40,6 +41,8 @@ import eu.etaxonomy.taxeditor.propertysheet.AnnotationPropertySource; import eu.etaxonomy.taxeditor.propertysheet.AnnotationsPropertyDescriptor; import eu.etaxonomy.taxeditor.propertysheet.ICdmBasePropertySource; import eu.etaxonomy.taxeditor.propertysheet.MarkersPropertySource; +import eu.etaxonomy.taxeditor.propertysheet.ProtologuePropertyDescriptor; +import eu.etaxonomy.taxeditor.propertysheet.ProtologuePropertySource; import eu.etaxonomy.taxeditor.propertysheet.reference.NomenclaturalReferencePropertySource; import eu.etaxonomy.taxeditor.propertysheet.reference.ReferencePropertySource; import eu.etaxonomy.taxeditor.propertysheet.reference.ReferenceSearchDescriptor; @@ -67,6 +70,7 @@ public class NonViralNamePropertySource implements ICdmBasePropertySource { public static final String P_ID_NAMERELATIONS = "namerelations"; public static final String P_ID_TYPE = "type"; public static final String P_ID_ANNOTATIONS = "annotations"; + public static final String P_ID_PROTOLOGUES = "protologues"; public static final String P_ID_NOMSTATUS = "nomstatus"; public static final String P_ID_UUID = "uuid"; public static final String P_ID_NUM_OF_BASES = "numberofbases"; @@ -85,6 +89,7 @@ public class NonViralNamePropertySource implements ICdmBasePropertySource { public static final String P_NAMERELATIONS = "Name Relations"; public static final String P_TYPE = "Type"; public static final String P_ANNOTATIONS = "Annotations"; + public static final String P_PROTOLOGUES = "Protologues"; public static final String P_NOMSTATUS = "Nomenclatural Status"; public static final String P_UUID = "UUID"; public static final String P_NUM_OF_BASES = "Number of Bases"; @@ -106,6 +111,7 @@ public class NonViralNamePropertySource implements ICdmBasePropertySource { P_ID_NOMSTATUS, P_ID_NAMERELATIONS, P_ID_ANNOTATIONS, + P_ID_PROTOLOGUES, P_ID_CREATED, P_ID_CREATEDBY }; @@ -244,6 +250,17 @@ public class NonViralNamePropertySource implements ICdmBasePropertySource { ); }; + // Annotations, listed in custom property descriptor + if (id.equals(P_ID_PROTOLOGUES)) { + descriptors.addElement( + new ProtologuePropertyDescriptor(P_ID_PROTOLOGUES, P_PROTOLOGUES, getFirstTaxonNameDescription()) { + protected void saveProtologue(Set set) { + setPropertyValue(P_ID_PROTOLOGUES, set); + } + } + ); + }; + // Nomenclatural status if (id.equals(P_ID_NOMSTATUS)) { if (nomStatusTypes == null) { @@ -383,6 +400,11 @@ public class NonViralNamePropertySource implements ICdmBasePropertySource { return new AnnotationPropertySource(name); } + // Protologues, listed in custom property descriptor + if(id.equals(P_ID_PROTOLOGUES)){ + return new ProtologuePropertySource(getFirstTaxonNameDescription()); + } + // Nomenclatural status if (id.equals(P_ID_NOMSTATUS)) { Set nomStatusSet = this.name.getStatus(); @@ -591,4 +613,18 @@ public class NonViralNamePropertySource implements ICdmBasePropertySource { ReferenceType.Generic, ReferenceType.Thesis }; } + /** + * TODO for the quick and dirty implementation we always assume + * the first TaxonNameDescription element to be the relevant + * @return + */ + private TaxonNameDescription getFirstTaxonNameDescription(){ + // we assume that the first name description holds our information and if there is none we create one + Set descriptions = name.getDescriptions(); + if(descriptions != null && descriptions.iterator().hasNext()){ + return descriptions.iterator().next(); + }else{ + return TaxonNameDescription.NewInstance(name); + } + } } -- 2.34.1