ref #6566 Migrate "Open in.." context menu items for specimen editor
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 22 May 2017 08:23:59 +0000 (10:23 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 22 May 2017 08:23:59 +0000 (10:23 +0200)
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/defaultHandler/OpenBulkEditorForIdentifiableEntityE4.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.editor/META-INF/MANIFEST.MF
eu.etaxonomy.taxeditor.editor/fragment.e4xmi
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/defaultHandler/DefaultOpenHandlerBaseE4.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerContextMenuE4.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerUtilE4.java [new file with mode: 0644]

diff --git a/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/defaultHandler/OpenBulkEditorForIdentifiableEntityE4.java b/eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/handler/defaultHandler/OpenBulkEditorForIdentifiableEntityE4.java
new file mode 100644 (file)
index 0000000..f2451a0
--- /dev/null
@@ -0,0 +1,30 @@
+package eu.etaxonomy.taxeditor.bulkeditor.handler.defaultHandler;
+
+import java.util.UUID;
+
+import org.eclipse.ui.PartInitException;
+
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
+import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
+import eu.etaxonomy.taxeditor.handler.defaultHandler.DefaultOpenHandlerBaseE4;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+
+public class OpenBulkEditorForIdentifiableEntityE4 extends DefaultOpenHandlerBaseE4<IdentifiableEntity> {
+
+       @Override
+       protected IdentifiableEntity getEntity(UUID uuid) {
+               return CdmStore.getCommonService().find(IdentifiableEntity.class, uuid);
+       }
+
+       @Override
+       protected void open(IdentifiableEntity entity) {
+               try {
+                       BulkEditorUtil.openEditor(entity);
+               } catch (PartInitException e) {
+            MessagingUtils.error(this.getClass(), "Bulk Editor could not be opened for "+entity, e);
+               }
+       }
+
+
+}
index d7ea64513c02d80da7aa8120d4dd8aea92cfddaf..79f07deb35ec7c7fe8511e219997414d278b8c0f 100644 (file)
@@ -30,7 +30,9 @@ Require-Bundle: org.eclipse.ui,
  eu.etaxonomy.taxeditor.cdmlib,
  org.eclipse.ui.ide,
  org.eclipse.e4.ui.workbench,
- org.eclipse.e4.core.di
+ org.eclipse.e4.core.di,
+ org.eclipse.e4.ui.model.workbench,
+ org.eclipse.e4.ui.di
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Bundle-ActivationPolicy: lazy
 Import-Package: javax.inject;version="1.0.0",
index 0ba92d6dc32da8327b3e7888693b796c09848973..f2b0643529338667eccd9fe042f3ae705e938b95 100644 (file)
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="ASCII"?>
 <fragment:ModelFragments xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:basic="http://www.eclipse.org/ui/2010/UIModel/application/descriptor/basic" xmlns:commands="http://www.eclipse.org/ui/2010/UIModel/application/commands" xmlns:fragment="http://www.eclipse.org/ui/2010/UIModel/fragment" xmlns:menu="http://www.eclipse.org/ui/2010/UIModel/application/ui/menu" xmlns:ui="http://www.eclipse.org/ui/2010/UIModel/application/ui" xmi:id="_OhwnQCTfEeeiN5lBIuqN3g">
   <imports xsi:type="commands:Command" xmi:id="_WPjpoDSnEeek0dKsFNy--Q" elementId="eu.etaxonomy.taxeditor.command.openPart"/>
+  <imports xsi:type="commands:Command" xmi:id="_Qz6EMD62EeekvPZafWEv7w" elementId="eu.etaxonomy.taxeditor.bulkeditor.openBulkEditorForIdentifiableEntity"/>
   <fragments xsi:type="fragment:StringModelFragment" xmi:id="_gSpRsDQDEeeTNOFVGI4q2w" featurename="descriptors" parentElementId="xpath:/">
     <elements xsi:type="basic:PartDescriptor" xmi:id="_gSpRsTQDEeeTNOFVGI4q2w" elementId="eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView" label="%command.label.DERIVATIVE_EDITOR" iconURI="platform:/plugin/eu.etaxonomy.taxeditor.editor/icons/derivate_view-16x16-32.png" tooltip="" allowMultiple="true" category="Sample Category" closeable="true" dirtyable="true" contributionURI="bundleclass://eu.etaxonomy.taxeditor.editor/eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView">
       <tags>View</tags>
@@ -19,6 +20,7 @@
       <handlers xmi:id="_bAGysDumEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.view.derivate.handler.SingleReadHandler.reuseSingleRead" contributionURI="bundleclass://eu.etaxonomy.taxeditor.editor/eu.etaxonomy.taxeditor.editor.view.derivate.handler.SingleReadReuseHandler" command="_HmerkDumEeeOtqC_3qh40A"/>
       <handlers xmi:id="_ed9ZoDumEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.view.derivate.handler.SingleReadHandler.removeSingleRead" contributionURI="bundleclass://eu.etaxonomy.taxeditor.editor/eu.etaxonomy.taxeditor.editor.view.derivate.handler.SingleReadRemoveHandler" command="_K4DxEDumEeeOtqC_3qh40A"/>
       <handlers xmi:id="_CQjyIDvaEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.view.derivate.handler.DeleteDerivateHandler" contributionURI="bundleclass://eu.etaxonomy.taxeditor.editor/eu.etaxonomy.taxeditor.editor.view.derivate.handler.DeleteDerivateHandler" command="_YPp1wDvREeeOtqC_3qh40A"/>
+      <handlers xmi:id="_OJK8QD62EeekvPZafWEv7w" elementId="eu.etaxonomy.taxeditor.bulkeditor.handler.defaultHandler.OpenBulkEditorForIdentifiableEntityE4" contributionURI="bundleclass://eu.etaxonomy.taxeditor.bulkeditor/eu.etaxonomy.taxeditor.bulkeditor.handler.defaultHandler.OpenBulkEditorForIdentifiableEntityE4" command="_Qz6EMD62EeekvPZafWEv7w"/>
       <menus xsi:type="menu:PopupMenu" xmi:id="_CYXZ0DQEEeeTNOFVGI4q2w" elementId="eu.etaxonomy.taxeditor.editor.popupmenu.specimeneditor">
         <children xsi:type="menu:Menu" xmi:id="_cdjzEDmAEeeOos_QXKSXcA" elementId="eu.etaxonomy.taxeditor.editor.menu.add" label="%menu.label.5">
           <visibleWhen xsi:type="ui:CoreExpression" xmi:id="_ASKmMDpCEeecM-jyCkToYw" coreExpressionId="isAddDerivateAllowed"/>
@@ -35,6 +37,8 @@
           <children xsi:type="menu:HandledMenuItem" xmi:id="_Z-LKEDmBEeeOos_QXKSXcA" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.singleread" label="%handledmenuitem.label.7" command="_LTuN8DooEeecLdI14WMq7w"/>
         </children>
         <children xsi:type="menu:MenuSeparator" xmi:id="_O4ujcDuhEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.1"/>
+        <children xsi:type="menu:DynamicMenuContribution" xmi:id="_yz9M4DxrEeeqBMqdgjGMrQ" elementId="eu.etaxonomy.taxeditor.editor.specimeneditor.dynamicmenucontribution.cdmViewer" label="Open in..." contributionURI="bundleclass://eu.etaxonomy.taxeditor.store/eu.etaxonomy.taxeditor.view.CdmViewerContextMenuE4"/>
+        <children xsi:type="menu:MenuSeparator" xmi:id="_sp6BoDy8EeeqBMqdgjGMrQ" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.5"/>
         <children xsi:type="menu:HandledMenuItem" xmi:id="_um1oYDVpEee_b7RlBzTDRw" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.create_field_unit_for_taxon" label="%handledmenuitem.label.8" command="_BjF3ADVqEee_b7RlBzTDRw"/>
         <children xsi:type="menu:HandledMenuItem" xmi:id="_5GMRcDWQEeeNfoZ3nN0K8Q" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.createfieldunit" label="%handledmenuitem.label.9" command="_BjF3ADVqEee_b7RlBzTDRw"/>
         <children xsi:type="menu:MenuSeparator" xmi:id="_QoiUMDuhEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.2"/>
index c93bc1e16378858044892397698da6d9a0f77fa6..6ccb5a00ea2e8460ea39eb660395a5650079b7e0 100644 (file)
@@ -18,6 +18,7 @@ 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.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.TreeNode;
 
@@ -125,8 +126,11 @@ public class DeleteDerivateHandler {
     }
 
     @CanExecute
-    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node){
-        return node !=null;
+    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node,
+            MHandledMenuItem menuItem){
+        boolean canExecute = node !=null;
+        menuItem.setVisible(canExecute);
+        return canExecute;
     }
 
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/defaultHandler/DefaultOpenHandlerBaseE4.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/defaultHandler/DefaultOpenHandlerBaseE4.java
new file mode 100644 (file)
index 0000000..e1882bc
--- /dev/null
@@ -0,0 +1,27 @@
+package eu.etaxonomy.taxeditor.handler.defaultHandler;
+
+import java.util.UUID;
+
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
+
+public abstract class DefaultOpenHandlerBaseE4 <T> {
+
+    @Execute
+    public Object execute(MHandledMenuItem menuItem){
+        String commandId = menuItem.getCommand().getElementId();
+        UUID uuid = (UUID) menuItem.getTransientData().get(commandId+".uuid");
+        //check if uuid parameter is set
+        if(uuid!=null){
+            T entity = getEntity(uuid);
+            open(entity);
+        }
+        return null;
+    }
+
+
+    protected abstract T getEntity(UUID uuid);
+
+    protected abstract void open(T entity);
+
+}
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerContextMenuE4.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerContextMenuE4.java
new file mode 100644 (file)
index 0000000..5882a70
--- /dev/null
@@ -0,0 +1,103 @@
+package eu.etaxonomy.taxeditor.view;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.UUID;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.e4.core.commands.ECommandService;
+import org.eclipse.e4.core.commands.EHandlerService;
+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;
+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 org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.TreeNode;
+
+import eu.etaxonomy.cdm.model.common.ICdmBase;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
+import eu.etaxonomy.taxeditor.l10n.Messages;
+
+/**
+ * Generic context menu for opening elements in the taxeditor.
+ *
+ */
+public class CdmViewerContextMenuE4 {
+
+    @Inject
+    private EHandlerService handlerService;
+
+    @Inject
+    private ECommandService commandService;
+
+    @AboutToShow
+    public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selectedObject) {
+        if(selectedObject instanceof IStructuredSelection){
+            selectedObject = ((IStructuredSelection) selectedObject).getFirstElement();
+        }
+        if(selectedObject instanceof TreeNode){
+            selectedObject = ((TreeNode) selectedObject).getValue();
+        }
+        Map<Command, String> enabledCommands = CdmViewerUtilE4.getAvailableViewers(selectedObject, commandService, handlerService);
+
+        //get UUID
+        UUID uuid = null;
+        //for generic UuidAndTitleCache objects try to load the object
+        if (selectedObject instanceof UuidAndTitleCache){
+            uuid = ((UuidAndTitleCache)selectedObject).getUuid();
+        }
+        if(selectedObject instanceof ICdmBase){
+            uuid = ((ICdmBase) selectedObject).getUuid();
+        }
+
+        //check if only one or multiple viewers/commands are available
+        if(enabledCommands.size()==1){
+            Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
+            final Command command = entry.getKey();
+            String viewerName = entry.getValue();
+            addCommand(uuid, command, String.format(Messages.CdmViewerContextMenu_OPEN, viewerName), items);
+        }
+        else if(enabledCommands.size()>1){
+            MMenu menu = MMenuFactory.INSTANCE.createMenu();
+            menu.setLabel(Messages.CdmViewerContextMenu_OPEN_IN);
+            items.add(menu);
+            for(Entry<Command, String> entry:enabledCommands.entrySet()){
+                String viewerName = entry.getValue();
+                Command command = entry.getKey();
+                menu.getChildren().add(addCommand(uuid, command, viewerName, items));
+            }
+        }
+    }
+
+    public MHandledMenuItem addCommand(UUID uuid, Command command, String commandLabel, List<MMenuElement> items) {
+        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
+        menuItem.setLabel(commandLabel);
+        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
+        mCommand.setElementId(command.getId());
+        mCommand.setCommandName(commandLabel);
+        try {
+            mCommand.setCommandName(command.getName());
+        } catch (NotDefinedException e) {
+            e.printStackTrace();
+        }
+        //set params
+        menuItem.getTransientData().put(command.getId()+".uuid", uuid);
+//        MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
+//        parameter.setElementId(command.getId()+".uuid");
+//        parameter.setValue(uuid.toString());
+//        menuItem.getParameters().add(parameter);
+
+        menuItem.setCommand(mCommand);
+        return menuItem;
+    }
+
+}
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerUtilE4.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerUtilE4.java
new file mode 100644 (file)
index 0000000..7fdbe86
--- /dev/null
@@ -0,0 +1,81 @@
+/**
+* Copyright (C) 2015 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.view;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExtensionRegistry;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.e4.core.commands.ECommandService;
+import org.eclipse.e4.core.commands.EHandlerService;
+import org.eclipse.jface.viewers.TreeNode;
+
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+
+/**
+ * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
+ * @author pplitzner
+ * @date Jul 7, 2015
+ *
+ */
+public class CdmViewerUtilE4 {
+
+    /**
+     * Returns a map of available commands to open the given input. Keys are the
+     * command IDs and values are their string representations.
+     *
+     * @param input
+     *            the object which should be handled by the available commands
+     * @return a key-value map of available commands and their string
+     *         representation
+     */
+    public static Map<Command, String> getAvailableViewers(Object input, ECommandService commandService,
+            EHandlerService handlerService){
+        Map<Command, String> commandViewerNameMap = new HashMap<>();
+
+        if(input!=null){
+            //for generic UuidAndTitleCache objects try to load the object
+            if (input instanceof UuidAndTitleCache){
+                UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache)input;
+                input = CdmStore.getCommonService().find(uuidAndTitleCache.getType(), uuidAndTitleCache.getUuid());
+            }
+            //for tree nodes get the value resp. the object of the node
+            else if (input instanceof TreeNode){
+                TreeNode treeNode = (TreeNode)input;
+                input = treeNode.getValue();
+            }
+
+            IExtensionRegistry reg = Platform.getExtensionRegistry();
+            IConfigurationElement[] extensions = reg
+                    .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
+            for (IConfigurationElement configElement : extensions) {
+                try {
+                    if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
+                        String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
+                        String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
+                        Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
+                        if(selectionClass.isAssignableFrom(input.getClass())){
+                            Command command = commandService.getCommand(commandId);
+                            commandViewerNameMap.put(command, viewerName);
+                        }
+                    }
+                } catch (ClassNotFoundException e) {
+                    MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
+                }
+            }
+        }
+        return commandViewerNameMap;
+    }
+
+}