Generalize extension point for opening object in taxeditor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerChooser.java
index 0129f7f787eb5265b27035c41fa977486e093f50..939f753b698d3874cd784a9a863c1d43c886c09b 100644 (file)
@@ -9,14 +9,13 @@
  */
 package eu.etaxonomy.taxeditor.view;
 
-import java.util.ArrayList;
-import java.util.Collection;
-
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.IExtensionRegistry;
-import org.eclipse.core.runtime.InvalidRegistryObjectException;
-import org.eclipse.core.runtime.Platform;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.ParameterizedCommand;
+import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.jface.dialogs.PopupDialog;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.ILabelProvider;
@@ -32,12 +31,16 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Table;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.commands.ICommandService;
+import org.eclipse.ui.handlers.IHandlerService;
 
+import eu.etaxonomy.cdm.model.common.ICdmBase;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 /**
- * This class provides the possibility to choose from a list of possible viewers
- * which can be opened for a given input in a popup dialog.
+ * This class opens a popup dialog and provides the possibility to choose from a
+ * list of possible viewers which can be opened for a given input.
  *
  * @author pplitzner
  * @date Feb 23, 2015
@@ -45,8 +48,8 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
  */
 public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
 
+    private Map<String, String> nameViewerMap;
     private Object input;
-    private Collection<IConfigurationElement> partConfigurationElements;
 
     public CdmViewerChooser(Shell parentShell) {
         this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
@@ -66,120 +69,98 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
      */
     public void chooseViewer(Object input){
         this.input = input;
-        partConfigurationElements = new ArrayList<>();
-
-        IExtensionRegistry reg = Platform.getExtensionRegistry();
-        IConfigurationElement[] extensions = reg
-                .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
-        for (IConfigurationElement e : extensions) {
-            for (IConfigurationElement inputConfigurationElement : e.getChildren("input")) {
-                String inputClass = inputConfigurationElement.getAttribute("class");
-                if(inputClass.equals(input.getClass().toString())){
-                    System.out.println("input class: " + inputClass);
-                    for (IConfigurationElement partConfigurationElement : inputConfigurationElement.getChildren("part")) {
-                        partConfigurationElements.add(partConfigurationElement);
-                    }
+        this.nameViewerMap = CdmViewerUtil.getAvailableViewers(input);
+
+        //if only one editor is available then open it
+        if(nameViewerMap.size()==1){
+            String commandId = nameViewerMap.keySet().iterator().next();
+            executeCommand(commandId, input);
+        }
+        else{
+            if(nameViewerMap.isEmpty()){
+                this.setInfoText("No viewers registered for this input");
+            }
+            this.open();
+        }
+    }
+
+    private void executeCommand(String commandId, Object input) {
+        ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
+        //get the command from plugin.xml
+        Command command = commandService.getCommand(commandId);
+
+        //set uuid parameter
+        if(input instanceof ICdmBase){
+            Map<String, UUID> params = new HashMap<String, UUID>();
+            params.put(commandId+".uuid", ((ICdmBase) input).getUuid());
+
+            //build the parameterized command
+            ParameterizedCommand pc = ParameterizedCommand.generateCommand(command, params);
+
+            if(command.isEnabled()) {
+                IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
+                try {
+                    handlerService.executeCommand(pc, 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);
                 }
             }
         }
-        this.open();
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
-     */
     @Override
     protected Control createDialogArea(Composite parent) {
         TableViewer viewer = new TableViewer(new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION));
         viewer.setContentProvider(new ArrayContentProvider());
         viewer.setLabelProvider(this);
         viewer.addSelectionChangedListener(this);
-        viewer.setInput(partConfigurationElements);
+        viewer.setInput(nameViewerMap.keySet());
         return parent;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
-     */
     @Override
     public void selectionChanged(SelectionChangedEvent event) {
         ISelection selection = event.getSelection();
         if(selection instanceof IStructuredSelection){
             Object firstElement = ((IStructuredSelection) selection).getFirstElement();
-            if(firstElement instanceof IConfigurationElement){
-                IConfigurationElement configElement = (IConfigurationElement)firstElement;
-                String viewerClass = configElement.getAttribute("class");
-                try {
-                    //get the grand parent (this is the cdmViewer)
-                    Object o = ((IConfigurationElement)((IConfigurationElement)configElement.getParent()).getParent()).createExecutableExtension("class");
-                    if(o instanceof ICdmViewer){
-                        ((ICdmViewer) o).show(input, viewerClass);
-                    this.close();
-                    }
-                } catch (InvalidRegistryObjectException e) {
-                    MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e);
-                } catch (CoreException e) {
-                    MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e);
-                }
+            if(firstElement instanceof String && nameViewerMap.containsKey(firstElement)){
+                executeCommand((String) firstElement, this.input);
+                this.close();
             }
         }
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
-     */
     @Override
     public String getText(Object element) {
-        String text = null;
-        if(element instanceof IConfigurationElement){
-            IConfigurationElement configElement = (IConfigurationElement)element;
-            text = configElement.getAttribute("name");
-            if(text==null){
-                text = configElement.getAttribute("class");
-            }
-        }
-        return text;
+        return nameViewerMap.get(element);
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
-     */
     @Override
     public void addListener(ILabelProviderListener listener) {
         // TODO Auto-generated method stub
 
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
-     */
     @Override
     public void dispose() {
         // TODO Auto-generated method stub
 
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
-     */
     @Override
     public boolean isLabelProperty(Object element, String property) {
         // TODO Auto-generated method stub
         return false;
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
-     */
     @Override
     public void removeListener(ILabelProviderListener listener) {
         // TODO Auto-generated method stub
 
     }
 
-    /* (non-Javadoc)
-     * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
-     */
     @Override
     public Image getImage(Object element) {
         // TODO Auto-generated method stub