ref #7010 Adapt term editor handlers for multiple selection
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 6 Jun 2018 15:55:06 +0000 (17:55 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 13 Jun 2018 08:05:47 +0000 (10:05 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/e4/DefinedTermEditorE4.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/e4/DefinedTermMenuE4.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/e4/handler/CreateDefinedTermHandlerE4.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/editor/definedterm/e4/handler/DeleteTermBaseHandlerE4.java

index db180cc2a9acc7672c9425b83d6e4736fdbb79c8..41ed8c186fc4dd70dcec31dd4a705e3748424d73 100644 (file)
@@ -44,7 +44,6 @@ import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
 import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
 import eu.etaxonomy.taxeditor.event.EventUtility;
 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.IContextListener;
 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
@@ -105,7 +104,7 @@ IPartContentHasDetails, IPartContentHasSupplementalData, IE4SavablePart, IContex
            layout.type = SWT.VERTICAL;
 
            parent.setLayout(layout);
-           viewer = new TreeViewer(new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION));
+           viewer = new TreeViewer(new Tree(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.MULTI));
            viewer.getControl().setLayoutData(LayoutConstants.FILL());
            viewer.setContentProvider(new TermContentProvider());
            viewer.setLabelProvider(new TermLabelProvider());
@@ -118,7 +117,7 @@ IPartContentHasDetails, IPartContentHasSupplementalData, IE4SavablePart, IContex
         viewer.addDropSupport(dndOperations, transfers, dropListener);
 
            //propagate selection
-           selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
+           selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
            viewer.addSelectionChangedListener(selectionChangedListener);
 
            //create context menu
index 059fbfeb680a4b3a140883e05e53afbf24af8bb3..9ff8e2f68558fb7e0c90e727ab8de6ae23e5be97 100644 (file)
@@ -16,8 +16,6 @@ import java.util.EnumSet;
 import java.util.List;
 import java.util.Set;
 
-import javax.inject.Named;
-
 import org.eclipse.e4.ui.di.AboutToShow;
 import org.eclipse.e4.ui.model.application.commands.MCommand;
 import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
@@ -25,7 +23,6 @@ import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
 import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
 import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
-import org.eclipse.e4.ui.services.IServiceConstants;
 
 import eu.etaxonomy.cdm.model.common.TermType;
 import eu.etaxonomy.taxeditor.l10n.Messages;
@@ -47,7 +44,7 @@ public class DefinedTermMenuE4 {
 
     /** {@inheritDoc} */
     @AboutToShow
-    public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
+    public void aboutToShow(List<MMenuElement> items) {
 
         if(CdmStore.isActive()){
 
index d53359d1f1a3531f6a7a773590ae144143a87131..8d25e4a6bf708667cc2cf7f18e77fcf549b34598 100644 (file)
@@ -13,15 +13,13 @@ import javax.inject.Named;
 import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.e4.core.di.annotations.CanExecute;
 import org.eclipse.e4.core.di.annotations.Execute;
-import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.ui.di.UISynchronize;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.viewers.IStructuredSelection;
 
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
-import eu.etaxonomy.cdm.model.common.Marker;
-import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.common.TermBase;
 import eu.etaxonomy.cdm.model.common.TermVocabulary;
 import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermEditorE4;
@@ -40,7 +38,7 @@ public class CreateDefinedTermHandlerE4 {
 
     @Execute
     public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
-            @Optional@Named(IServiceConstants.ACTIVE_SELECTION) TermBase termBase, MHandledMenuItem menuItem,
+            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem,
             UISynchronize sync) {
 
         DefinedTermEditorE4 termEditor = (DefinedTermEditorE4) activePart.getObject();
@@ -54,7 +52,7 @@ public class CreateDefinedTermHandlerE4 {
         AbstractPostOperation operation =
                 new CreateDefinedTermOperation(label,
                         undoContext,
-                        termBase,
+                        (TermBase) selection.getFirstElement(),
                         termEditor.getDefinedTermEditorInput(),
                         termEditor, addTermAsKindOf);
         AbstractUtility.executeOperation(operation, sync);
@@ -62,30 +60,24 @@ public class CreateDefinedTermHandlerE4 {
     }
 
     @CanExecute
-    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) TermBase termBase){
-        if (termBase == null){
-            return true;
-        }
-
+    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
+            MHandledMenuItem menuItem){
+        boolean canExecute = false;
+        Object firstElement = selection.getFirstElement();
+        canExecute = selection.size()==1
+                &&
+                (firstElement instanceof DefinedTermBase
+                        || firstElement instanceof TermVocabulary);
         TermVocabulary vocabulary = null;
 
-        if(termBase instanceof DefinedTermBase){
-            vocabulary = ((DefinedTermBase) termBase).getVocabulary();
-        }else if(termBase instanceof TermVocabulary){
-            vocabulary = (TermVocabulary) termBase;
-        }
-
-        if(vocabulary == null){
-            return true;
+        if(firstElement instanceof DefinedTermBase){
+            vocabulary = ((DefinedTermBase) firstElement).getVocabulary();
+        }else if(firstElement instanceof TermVocabulary){
+            vocabulary = (TermVocabulary) firstElement;
         }
-
-        for(Marker vocabularyMarker : vocabulary.getMarkers()){
-            if(vocabularyMarker.getMarkerType().equals(MarkerType.MODIFIABLE())){
-                return vocabularyMarker.getValue();
-            }
-        }
-
-        return true;
+        canExecute &= vocabulary!=null;
+        menuItem.setVisible(canExecute);
+        return canExecute;
     }
 
 }
index d31b44668a906f44566da592a541d5ce3c5aa264..73de51e38bc54fde8b8bfd7d450bb1c488761d4b 100644 (file)
@@ -13,16 +13,15 @@ import javax.inject.Named;
 import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.e4.core.di.annotations.CanExecute;
 import org.eclipse.e4.core.di.annotations.Execute;
-import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.ui.di.UISynchronize;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.IStructuredSelection;
 
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
-import eu.etaxonomy.cdm.model.common.Marker;
-import eu.etaxonomy.cdm.model.common.MarkerType;
 import eu.etaxonomy.cdm.model.common.TermBase;
 import eu.etaxonomy.cdm.model.common.TermVocabulary;
 import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermEditorE4;
@@ -41,7 +40,7 @@ public class DeleteTermBaseHandlerE4 {
 
     @Execute
     public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
-            @Optional@Named(IServiceConstants.ACTIVE_SELECTION) TermBase termBase, MMenuItem menuItem,
+            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MMenuItem menuItem,
             UISynchronize sync) {
 
         DefinedTermEditorE4 termEditor = (DefinedTermEditorE4) activePart.getObject();
@@ -60,36 +59,29 @@ public class DeleteTermBaseHandlerE4 {
         AbstractPostOperation operation =
                 new DeleteTermBaseOperation(label,
                         undoContext,
-                        termBase,
+                        (TermBase) selection.getFirstElement(),
                         termEditor.getDefinedTermEditorInput(),
                         termEditor);
         AbstractUtility.executeOperation(operation, sync);
     }
 
     @CanExecute
-    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) TermBase termBase){
-        if (termBase == null){
-            return true;
-        }
-
+    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
+            MHandledMenuItem menuItem){
+        boolean canExecute = false;
+        Object firstElement = selection.getFirstElement();
+        canExecute = selection.size()==1
+                &&
+                (firstElement instanceof DefinedTermBase
+                        || firstElement instanceof TermVocabulary);
         TermVocabulary vocabulary = null;
-
-        if(termBase instanceof DefinedTermBase){
-            vocabulary = ((DefinedTermBase) termBase).getVocabulary();
-        }else if(termBase instanceof TermVocabulary){
-            vocabulary = (TermVocabulary) termBase;
-        }
-
-        if(vocabulary == null){
-            return true;
+        if(firstElement instanceof DefinedTermBase){
+            vocabulary = ((DefinedTermBase) firstElement).getVocabulary();
+        }else if(firstElement instanceof TermVocabulary){
+            vocabulary = (TermVocabulary) firstElement;
         }
-
-        for(Marker vocabularyMarker : vocabulary.getMarkers()){
-            if(vocabularyMarker.getMarkerType().equals(MarkerType.MODIFIABLE())){
-                return vocabularyMarker.getValue();
-            }
-        }
-
-        return true;
+        canExecute &= vocabulary!=null;
+        menuItem.setVisible(canExecute);
+        return canExecute;
     }
 }