Generalize cdmViewer extension point
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 20 Jul 2015 16:31:02 +0000 (18:31 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 21 Jul 2015 09:08:08 +0000 (11:08 +0200)
Refactored parameters to be based on commands.

eu.etaxonomy.taxeditor.store/schema/eu.etaxonomy.taxeditor.store.cdmViewer.exsd
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerContextMenu.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerUtil.java

index 42791ec3b89d6c6967cb86041814a7d482ff7619..b233e53f9d3ec4f27bf630e383492765ef7fbdbe 100644 (file)
@@ -21,7 +21,8 @@
       </annotation>
       <complexType>
          <sequence>
       </annotation>
       <complexType>
          <sequence>
-            <element ref="cdmViewer" minOccurs="1" maxOccurs="unbounded"/>
+            <element ref="cdmViewer" minOccurs="0" maxOccurs="unbounded"/>
+            <element ref="viewCommandMapping" minOccurs="0" maxOccurs="unbounded"/>
          </sequence>
          <attribute name="point" type="string" use="required">
             <annotation>
          </sequence>
          <attribute name="point" type="string" use="required">
             <annotation>
       </complexType>
    </element>
 
       </complexType>
    </element>
 
+   <element name="viewCommandMapping">
+      <annotation>
+         <documentation>
+            Maps a viewing class, like editors or views, to a command which opens it.
+         </documentation>
+      </annotation>
+      <complexType>
+         <attribute name="commandId" type="string" use="required">
+            <annotation>
+               <documentation>
+                  The command which opens the viewer to show the selected object
+               </documentation>
+               <appInfo>
+                  <meta.attribute kind="identifier" basedOn="org.eclipse.ui.commands/command/@id"/>
+               </appInfo>
+            </annotation>
+         </attribute>
+         <attribute name="viewerName" type="string">
+            <annotation>
+               <documentation>
+                  The name of the viewer which shows the selected element
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="selection" type="string" use="required">
+            <annotation>
+               <documentation>
+                  The class of the selected element
+               </documentation>
+               <appInfo>
+                  <meta.attribute kind="java"/>
+               </appInfo>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
    <annotation>
       <appInfo>
          <meta.section type="since"/>
    <annotation>
       <appInfo>
          <meta.section type="since"/>
index 712bec9d8a40c91ee3024b635122163b36d2e5a8..f2eef9fe3e63113dab460be3d7cf8bb122b58d87 100644 (file)
@@ -3,6 +3,8 @@ package eu.etaxonomy.taxeditor.view;
 import java.util.Map;
 import java.util.Map.Entry;
 
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.jface.action.ContributionItem;
 import org.eclipse.jface.action.IContributionItem;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.action.ContributionItem;
 import org.eclipse.jface.action.IContributionItem;
 import org.eclipse.jface.viewers.ISelection;
@@ -15,6 +17,10 @@ import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.actions.CompoundContributionItem;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.actions.CompoundContributionItem;
+import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.handlers.IHandlerService;
+
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 /**
  * Generic context menu for opening elements in the taxeditor.
 
 /**
  * Generic context menu for opening elements in the taxeditor.
@@ -28,20 +34,40 @@ public class CdmViewerContextMenu extends CompoundContributionItem {
                 new ContributionItem() {
                     @Override
                     public void fill(Menu menu, int index) {
                 new ContributionItem() {
                     @Override
                     public void fill(Menu menu, int index) {
+                        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+                        final ISelection selection = window.getActivePage().getSelection();
+
                         MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
                         addItem.setText("Open in...");
                         Menu addMenu = new Menu(menu);
                         addItem.setMenu(addMenu);
                         MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
                         addItem.setText("Open in...");
                         Menu addMenu = new Menu(menu);
                         addItem.setMenu(addMenu);
-                        final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-                        final ISelection selection = window.getActivePage().getSelection();
                         if(selection instanceof IStructuredSelection){
                             Object firstElement = ((IStructuredSelection) selection).getFirstElement();
                         if(selection instanceof IStructuredSelection){
                             Object firstElement = ((IStructuredSelection) selection).getFirstElement();
-                            Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap = CdmViewerUtil.getNameViewerMap(firstElement);
-                            for(Entry<Entry<Class<?>, String>, ICdmViewer> entry:nameViewerMap.entrySet()){
-                                Entry<Class<?>, String> viewerClass = entry.getKey();
+                            Map<String, String> availableViewers = CdmViewerUtil.getAvailableViewers(firstElement);
+                            for(Entry<String, String> entry:availableViewers.entrySet()){
+                                final String commandId = entry.getKey();
+                                String viewerName = entry.getValue();
                                 MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
                                 MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
-                                menuItem.setText(viewerClass.getValue());
-                                menuItem.addSelectionListener(new OpenInViewerListener(entry.getValue(), firstElement, viewerClass.getKey()));
+                                menuItem.setText(viewerName);
+                                menuItem.addSelectionListener(new SelectionAdapter() {
+
+                                    @Override
+                                    public void widgetSelected(SelectionEvent e) {
+                                        ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
+
+                                        Command command = commandService.getCommand(commandId);
+                                        if(command.isEnabled()) {
+                                            IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
+                                            try {
+                                                handlerService.executeCommand(commandId, null);
+                                            } catch (NotDefinedException nde) {
+                                                throw new RuntimeException("Could not find open command: " + commandId);
+                                            } catch (Exception exception) {
+                                                MessagingUtils.error(getClass(), "An exception occured while trying execute "+commandId, exception);
+                                            }
+                                        }
+                                    }
+                                }) ;
                             }
                         }
                     }
                             }
                         }
                     }
index 6f5c6f67ab73666cdc2aec92a0f81f8c881aedca..8fc2e73e35c41d041efdfb9188fee6101ab32a89 100644 (file)
@@ -32,7 +32,7 @@ public class CdmViewerUtil {
 
     public static  Map<Entry<Class<?>, String>, ICdmViewer> getNameViewerMap(Object input){
         Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap = new HashMap<Entry<Class<?>, String>, ICdmViewer>();
 
     public static  Map<Entry<Class<?>, String>, ICdmViewer> getNameViewerMap(Object input){
         Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap = new HashMap<Entry<Class<?>, String>, ICdmViewer>();
-    
+
         IExtensionRegistry reg = Platform.getExtensionRegistry();
         IConfigurationElement[] extensions = reg
                 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
         IExtensionRegistry reg = Platform.getExtensionRegistry();
         IConfigurationElement[] extensions = reg
                 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
@@ -56,4 +56,29 @@ public class CdmViewerUtil {
         return nameViewerMap;
     }
 
         return nameViewerMap;
     }
 
+    public static  Map<String, String> getAvailableViewers(Object input){
+        Map<String, String> commandViewerNameMap = new HashMap<String, String>();
+
+        IExtensionRegistry reg = Platform.getExtensionRegistry();
+        IConfigurationElement[] extensions = reg
+                .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
+        for (IConfigurationElement configElement : extensions) {
+            if(configElement.getName().equals("viewCommandMapping")){
+                String commandId = configElement.getAttribute("commandId");
+                String viewerName = configElement.getAttribute("viewerName");
+                try {
+                    Object selectionObject = configElement.createExecutableExtension("selection");
+                    System.out.println(selectionObject);
+                    Class<? extends Object> selectionClass = selectionObject.getClass();
+                    if(input.getClass().isAssignableFrom(selectionClass)){
+                        commandViewerNameMap.put(commandId, viewerName);
+                    }
+                } catch (CoreException e) {
+                    MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e);
+                }
+            }
+        }
+        return commandViewerNameMap;
+    }
+
 }
 }