Refactor SingleReadContextMenu #5406
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerChooser.java
index acf9f735295219e41079c26fa7b2832e7f306ceb..09f3784042fbbc3cd5f4476c1f55c12aefc5968f 100644 (file)
@@ -11,36 +11,49 @@ package eu.etaxonomy.taxeditor.view;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.UUID;
 
-import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.IExtensionRegistry;
-import org.eclipse.core.runtime.Platform;
+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.ColumnLabelProvider;
+import org.eclipse.jface.viewers.ILabelProvider;
+import org.eclipse.jface.viewers.ILabelProviderListener;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Image;
 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 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
  *
  */
-public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener{
+public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
 
-    private Map<String, ICdmViewer> viewerNameMap;
+    private Map<Command, String> nameViewerMap;
     private Object input;
 
     public CdmViewerChooser(Shell parentShell) {
-        this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...", "Clicking will open the selected viewer");
+        this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
+                "Clicking will open the selected viewer");
     }
 
     public CdmViewerChooser(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize,
@@ -50,65 +63,108 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
                 titleText, infoText);
     }
 
+    /**
+     * Opens a popup dialog with all possible viewers for the given input.
+     * @param input the input for which the viewers are listed
+     */
     public void chooseViewer(Object input){
-        viewerNameMap = new HashMap<String, ICdmViewer>();
         this.input = input;
-        IExtensionRegistry reg = Platform.getExtensionRegistry();
-        IConfigurationElement[] extensions = reg
-                .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
-        for (IConfigurationElement e : extensions) {
-            Object o;
-//            try {
-//                o = e.createExecutableExtension("class");
-//                if (o instanceof ICdmViewer) {
-//                    ICdmViewer cdmViewer = (ICdmViewer) o;
-//                    Collection<String> viewerNames = cdmViewer.getViewerNames(input);
-//                    if(viewerNames!=null){
-//                        for (String string : viewerNames) {
-//                            viewerNameMap.put(string, cdmViewer);
-//                        }
-//                    }
-//                }
-//            } catch (CoreException e1) {
-//                MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e1);
-//            }
-            for (IConfigurationElement inputConfigurationElement : e.getChildren("input")) {
-                System.out.println("input");
-                for (IConfigurationElement partConfigurationElement : inputConfigurationElement.getChildren("part")) {
-                    System.out.println("part");
+        this.nameViewerMap = CdmViewerUtil.getAvailableViewers(input);
+
+        //if only one editor is available then open it
+        if(nameViewerMap.size()==1){
+            Command command = nameViewerMap.keySet().iterator().next();
+            executeCommand(command.getId(), 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(new ColumnLabelProvider());
+        viewer.setLabelProvider(this);
         viewer.addSelectionChangedListener(this);
-        viewer.setInput(viewerNameMap.keySet());
+        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();
-            ICdmViewer cdmViewer = viewerNameMap.get(firstElement);
-            if(cdmViewer!=null){
-                cdmViewer.show(input, null);
+            if(firstElement instanceof String && nameViewerMap.containsKey(firstElement)){
+                executeCommand((String) firstElement, this.input);
                 this.close();
             }
         }
     }
 
+    @Override
+    public String getText(Object element) {
+        return nameViewerMap.get(element);
+    }
+
+    @Override
+    public void addListener(ILabelProviderListener listener) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public void dispose() {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public boolean isLabelProperty(Object element, String property) {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    @Override
+    public void removeListener(ILabelProviderListener listener) {
+        // TODO Auto-generated method stub
+
+    }
+
+    @Override
+    public Image getImage(Object element) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
 }