- extended DerivateSearchView to allow multiple selection
authorPatric Plitzner <p.plitzner@bgbm.org>
Tue, 18 Nov 2014 13:47:29 +0000 (13:47 +0000)
committerPatric Plitzner <p.plitzner@bgbm.org>
Tue, 18 Nov 2014 13:47:29 +0000 (13:47 +0000)
  - added context menu to open multiple elements in DerivateView
 - extended DerivateView to show multiple derivate hierarchies in one view

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/handler/OpenDerivateViewHandler.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/DerivateContentProvider.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/DerivateView.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/DerivateViewEditorInput.java
eu.etaxonomy.taxeditor.store/plugin.xml
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/derivateSearch/DerivateSearchComposite.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/derivateSearch/DerivateSearchView.java

index d6f0df7e6295919e1ff516db44d7c115dfe021f8..f06996ef75511a31a2a26274e2bca1d27a9e1759 100644 (file)
@@ -1,5 +1,10 @@
 package eu.etaxonomy.taxeditor.editor.handler;
 
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.UUID;
+
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
@@ -22,18 +27,22 @@ public class OpenDerivateViewHandler extends AbstractHandler {
     public Object execute(ExecutionEvent event) throws ExecutionException {
         ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
         if(currentSelection instanceof IStructuredSelection){
-            Object selectedElement = ((IStructuredSelection) currentSelection).getFirstElement();
-            if(selectedElement instanceof SpecimenOrObservationBase<?>){
-
-                DerivateViewEditorInput input = new DerivateViewEditorInput(((SpecimenOrObservationBase<?>) selectedElement).getUuid());
-                try {
-                    EditorUtil.open(input);
-                } catch (PartInitException e) {
-                    MessagingUtils.error(OpenDerivateViewHandler.class, "Could not open DerivateView", e);
-                } catch (NullPointerException npe){
-                    MessagingUtils.messageDialog("Failed to open Editor", OpenDerivateViewHandler.class, "Could not open DerivateView. The derivate hierarchy is corrupted!", npe);
+            Iterator<?> selectionIterator = ((IStructuredSelection) currentSelection).iterator();
+            Set<UUID> selectedElementUUIDs = new HashSet<UUID>();
+            while(selectionIterator.hasNext()){
+                Object object = selectionIterator.next();
+                if(object instanceof SpecimenOrObservationBase<?>){
+                    selectedElementUUIDs.add(((SpecimenOrObservationBase<?>) object).getUuid());
                 }
             }
+            DerivateViewEditorInput input = new DerivateViewEditorInput(selectedElementUUIDs);
+            try {
+                EditorUtil.open(input);
+            } catch (PartInitException e) {
+                MessagingUtils.error(OpenDerivateViewHandler.class, "Could not open DerivateView", e);
+            } catch (NullPointerException npe){
+                MessagingUtils.messageDialog("Failed to open Editor", OpenDerivateViewHandler.class, "Could not open DerivateView. The derivate hierarchy is corrupted!", npe);
+            }
         }
         return null;
     }
index 1e1da7b5bde030cb6e32e859edfdf8541fd86672..023d217fe6d72aa596ad31e8fbe230a877775a1b 100644 (file)
@@ -1,10 +1,10 @@
 package eu.etaxonomy.taxeditor.editor.view.derivate;
 
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.UUID;
 
 import org.eclipse.jface.viewers.ITreeContentProvider;
 import org.eclipse.jface.viewers.TreeNode;
@@ -34,17 +34,17 @@ public class DerivateContentProvider implements ITreeContentProvider {
 
        @Override
        public Object[] getElements(Object inputElement) {
-           SpecimenOrObservationBase<?> rootElement = null;
+           List<TreeNode> rootElements = new ArrayList<TreeNode>();
            if(inputElement instanceof DerivateViewEditorInput){
-               rootElement = CdmStore.getService(IOccurrenceService.class).load(((DerivateViewEditorInput) inputElement).getRootElementUUID());
-           }
-
-           if(rootElement!=null){
-               //show topmost element in the derivate hierarchy if no field unit was found
-               TreeNode node = new TreeNode(rootElement);
-            return Collections.singleton(node).toArray();
+               Set<UUID> rootUUIDs = ((DerivateViewEditorInput) inputElement).getRootUUIDs();
+               for (UUID uuid : rootUUIDs) {
+                   SpecimenOrObservationBase<?> rootElement = CdmStore.getService(IOccurrenceService.class).load(uuid);
+                if(rootElement!=null){
+                    rootElements.add(new TreeNode(rootElement));
+                }
+            }
            }
-           return null;
+           return rootElements.toArray();
        }
 
        @Override
index 963cd911fb11615a7d962ff2abe1953a7e7fb145..13778a6dcdf26386576c55b152dbc34a93e1fbf6 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Collection;
 import java.util.HashSet;
 import java.util.Map.Entry;
 import java.util.Set;
+import java.util.UUID;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.jface.action.MenuManager;
@@ -104,11 +105,13 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
         labelProvider.setMultiLinkSingleReads(multiLinkSingleReads);
         IEditorInput editorInput = getEditorInput();
         viewer.setInput(editorInput);
+        //set selection to selected derivate if only one was selected
         if(editorInput instanceof DerivateViewEditorInput){
-            SpecimenOrObservationBase<?> specimen = CdmStore.getService(IOccurrenceService.class).load(((DerivateViewEditorInput) editorInput).getDerivateUUID());
-            SpecimenOrObservationBase<?> rootElement = CdmStore.getService(IOccurrenceService.class).load(((DerivateViewEditorInput) editorInput).getRootElementUUID());
-            setPartName(labelProvider.getDerivateText(rootElement));
-            viewer.setSelection(new StructuredSelection(new TreeNode(specimen)));
+            Set<UUID> derivateUUIDs = ((DerivateViewEditorInput) editorInput).getDerivateUUIDs();
+            if(derivateUUIDs.size()==1){
+                SpecimenOrObservationBase<?> specimen = CdmStore.getService(IOccurrenceService.class).load(derivateUUIDs.iterator().next());
+                viewer.setSelection(new StructuredSelection(new TreeNode(specimen)));
+            }
         }
 
         //add drag'n'drop support
@@ -151,6 +154,10 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
      */
     @Override
     public String getTitleToolTip() {
+        if(getEditorInput() instanceof DerivateViewEditorInput){
+            DerivateViewEditorInput derivateViewEditorInput = (DerivateViewEditorInput) getEditorInput();
+            return derivateViewEditorInput.getName();
+        }
         return "Derivate View";
     }
 
@@ -162,7 +169,9 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
         setSite(site);
         setInput(input);
         if(input instanceof DerivateViewEditorInput){
-            conversation = ((DerivateViewEditorInput) input).getConversationHolder();
+            DerivateViewEditorInput derivateViewEditorInput = (DerivateViewEditorInput) input;
+            conversation = derivateViewEditorInput.getConversationHolder();
+            setPartName(derivateViewEditorInput.getName());
         }
     }
 
@@ -304,15 +313,6 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
     public boolean postOperation(CdmBase objectAffectedByOperation) {
         refreshTree();
         if(objectAffectedByOperation!=null){
-            DerivateViewEditorInput derivateViewEditorInput = (DerivateViewEditorInput) getEditorInput();
-            if(objectAffectedByOperation.getUuid().equals(derivateViewEditorInput.getRootElementUUID())){
-                // TODO close derivate view if root element is deleted
-//                this.getViewer().setEditorSite().getPage().close();
-                return true;
-            }
-            else if(objectAffectedByOperation.getUuid().equals(derivateViewEditorInput.getDerivateUUID())){
-                derivateViewEditorInput.setRootAsDerivate();
-            }
             changed(objectAffectedByOperation);
             return true;
         }
index c323cb3f0621f4e74b88a409a45499e2a0cab642..141299b613d91e3c4f057506679a60d8e92cb48a 100644 (file)
@@ -9,6 +9,8 @@
 */
 package eu.etaxonomy.taxeditor.editor.view.derivate;
 
+import java.util.HashSet;
+import java.util.Set;
 import java.util.UUID;
 
 import org.eclipse.jface.resource.ImageDescriptor;
@@ -23,6 +25,7 @@ import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
 
 /**
  * Editor input for the {@link DerivateView} which holds the currently selected derivate for which
@@ -35,38 +38,43 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
 public class DerivateViewEditorInput implements IEditorInput{
 
     /**
-     * The selected derivate {@link UUID}
+     * The selected derivate {@link UUID}s
      */
-    private UUID derivateUUID;
+    private Set<UUID> derivateUUIDs;
     /**
-     * The {@link UUID} of the root of the hierarchy (may be the same object as the derivate)
+     * List of the {@link UUID}s of the root elements of the hierarchy (may be the same objects as the derivates)
      */
-    private UUID rootUUID;
+    private Set<UUID> rootUUIDs;
 
     private final ConversationHolder conversationHolder;
 
     /**
-     * Creates an editor input for the {@link DerivateView} with the currently selected derivate and the
-     * corresponding {@link FieldUnit} (both may be the same object).
-     * @param derivateUuid the {@link UUID} of the derivate for which the derivate hierarchy should be shown
-     * @param rootUUID the root of the hierarchy
+     * Creates an editor input for the {@link DerivateView} with the currently selected derivates and the
+     * corresponding {@link FieldUnit}s (both may be the same object).
+     * @param derivateUuids the {@link UUID}s of the derivates for which the derivate hierarchy should be shown
+     * @param rootUUIDs the root of the hierarchy
      */
-    public DerivateViewEditorInput(UUID derivateUuid) {
+    public DerivateViewEditorInput(Set<UUID> derivateUuids) {
         super();
         this.conversationHolder = CdmStore.createConversation();
-        this.derivateUUID = derivateUuid;
-        SpecimenOrObservationBase<?> derivate = CdmStore.getService(IOccurrenceService.class).load(derivateUuid);
-        if(derivate instanceof FieldUnit){
-            this.rootUUID = derivateUUID;
-        }
-        else if(derivate instanceof DerivedUnit){
-            SpecimenOrObservationBase<?> topMostDerivate = EditorUtil.getTopMostDerivate(derivate);
-            rootUUID = topMostDerivate==null?null:topMostDerivate.getUuid();
-            if(rootUUID==null){
-                rootUUID = derivateUUID;
+        this.derivateUUIDs = derivateUuids;
+        this.rootUUIDs = new HashSet<UUID>();
+        for (UUID uuid : derivateUuids) {
+            SpecimenOrObservationBase<?> derivate = CdmStore.getService(IOccurrenceService.class).load(uuid);
+            if(derivate instanceof FieldUnit){
+                rootUUIDs.add(uuid);
+            }
+            else if(derivate instanceof DerivedUnit){
+                SpecimenOrObservationBase<?> topMostDerivate = EditorUtil.getTopMostDerivate(derivate);
+                if(topMostDerivate!=null){
+                    rootUUIDs.add(topMostDerivate.getUuid());
+                }
             }
         }
-        if(rootUUID==null){
+        if(rootUUIDs.isEmpty()){
+            rootUUIDs = derivateUUIDs;
+        }
+        if(rootUUIDs.isEmpty()){
             MessagingUtils.messageDialog("Failed initializing editor", DerivateViewEditorInput.class, "No root element found!");
         }
     }
@@ -102,7 +110,7 @@ public class DerivateViewEditorInput implements IEditorInput{
      */
     @Override
     public String getName() {
-        return derivateUUID.toString();
+        return getEditorName();
     }
 
     /* (non-Javadoc)
@@ -118,19 +126,38 @@ public class DerivateViewEditorInput implements IEditorInput{
      */
     @Override
     public String getToolTipText() {
-        return derivateUUID.toString();
+        return getEditorName();
+    }
+
+    private String getEditorName() {
+        String name = null;
+        //FIXME consider giving label provider a static method to get the derivate text. Pass the conversation as a method parameter.
+        DerivateLabelProvider labelProvider = new DerivateLabelProvider();
+        labelProvider.setConversation(conversationHolder);
+        for(UUID uuid:rootUUIDs){
+            SpecimenOrObservationBase specimen = CdmStore.getService(IOccurrenceService.class).load(uuid);
+            if(specimen!=null){
+                if(name==null){
+                    name = labelProvider.getDerivateText(specimen);
+                }
+                else{
+                    name += ", "+labelProvider.getDerivateText(specimen);
+                }
+            }
+        }
+        return name;
     }
 
-    public UUID getDerivateUUID() {
-        return derivateUUID;
+    public Set<UUID> getRootUUIDs() {
+        return rootUUIDs;
     }
 
-    public UUID getRootElementUUID() {
-        return rootUUID;
+    public Set<UUID> getDerivateUUIDs() {
+        return derivateUUIDs;
     }
 
     public void setRootAsDerivate(){
-        this.derivateUUID = this.rootUUID;
+        this.derivateUUIDs = this.rootUUIDs;
     }
 
     public ConversationHolder getConversationHolder() {
@@ -144,7 +171,7 @@ public class DerivateViewEditorInput implements IEditorInput{
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + ((rootUUID == null) ? 0 : rootUUID.hashCode());
+        result = prime * result + ((rootUUIDs == null) ? 0 : rootUUIDs.hashCode());
         return result;
     }
 
@@ -163,14 +190,15 @@ public class DerivateViewEditorInput implements IEditorInput{
             return false;
         }
         DerivateViewEditorInput other = (DerivateViewEditorInput) obj;
-        if (rootUUID == null) {
-            if (other.rootUUID != null) {
+        if (rootUUIDs == null) {
+            if (other.rootUUIDs != null) {
                 return false;
             }
-        } else if (!rootUUID.equals(other.rootUUID)) {
+        } else if (!rootUUIDs.equals(other.rootUUIDs)) {
             return false;
         }
         return true;
     }
 
+
 }
index 58b9538aeb7ba543ae85a84732bc993395c09394..44d489e5a759533a2f63881b7fa8d69902a4d258 100644 (file)
             </visibleWhen>
          </command>
       </menuContribution>
+      <menuContribution
+            allPopups="false"
+            locationURI="popup:eu.etaxonomy.taxeditor.view.derivateSearch.DerivateSearchView">
+         <command
+               commandId="eu.etaxonomy.taxeditor.editor.handler.openDerivateView"
+               label="Open in Derivate Editor"
+               style="push">
+         </command>
+      </menuContribution>
    </extension>
    <extension
          point="org.eclipse.ui.handlers">
          </activeWhen>
       </handler>
    </extension>
-   <extension
-         point="org.eclipse.ui.commands">
-      <command
-            defaultHandler="eu.etaxonomy.taxeditor.view.datasource.handler.ChangeConnectionHandler"
-            id="eu.etaxonomy.taxeditor.datasource.connect"
-            name="Connect">
-      </command>
-   </extension>
    <extension
          name="Popup Menu Commands"
          point="org.eclipse.ui.commands">
             id="eu.etaxonomy.taxeditor.store.open.OpenDistributionEditorWizardHandler"
             name="Open Distribution Editor Wizard">
       </command>
+      <command
+            defaultHandler="eu.etaxonomy.taxeditor.view.datasource.handler.ChangeConnectionHandler"
+            id="eu.etaxonomy.taxeditor.datasource.connect"
+            name="Connect">
+      </command>
    </extension>
    <extension
          point="org.eclipse.ui.importWizards">
index 60bc2a1c4472182457dcba856b82f9086a149134..8ef16a6422f2345a218b9e70f4f8a4c9698707dc 100644 (file)
@@ -70,7 +70,7 @@ public class DerivateSearchComposite extends Composite {
         new Label(this, SWT.NONE);
         new Label(this, SWT.NONE);
 
-        resultViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
+        resultViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
         table = resultViewer.getTable();
         table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 6, 4));
 
index 6da90e1e38064d4a2a8e00ae462ef5d7e7c357f0..9f950fa3fbffe21c7aedfb871b02b86ab3cd00b1 100644 (file)
 package eu.etaxonomy.taxeditor.view.derivateSearch;
 
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.action.MenuManager;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Menu;
 import org.eclipse.ui.IMemento;
 import org.eclipse.ui.part.ViewPart;
 
@@ -44,6 +47,13 @@ public class DerivateSearchView extends ViewPart implements IContextListener {
         derivateSearchCompositeController = new DerivateSearchCompositeController(parent, this);
         getSite().setSelectionProvider(derivateSearchCompositeController.getResultViewer());
         derivateSearchCompositeController.setEnabled(CdmStore.isActive());
+
+        //create context menu
+        MenuManager menuManager = new MenuManager();
+        getSite().registerContextMenu(menuManager, derivateSearchCompositeController.getResultViewer());
+        Control control = derivateSearchCompositeController.getResultViewer().getControl();
+        Menu menu = menuManager.createContextMenu(control);
+        control.setMenu(menu);
     }
 
     /* (non-Javadoc)