- refactored cdmViewer extension point to be more generic
authorPatric Plitzner <p.plitzner@bgbm.org>
Wed, 25 Feb 2015 06:31:03 +0000 (06:31 +0000)
committerPatric Plitzner <p.plitzner@bgbm.org>
Wed, 25 Feb 2015 06:31:03 +0000 (06:31 +0000)
eu.etaxonomy.taxeditor.editor/plugin.xml
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/EditorCdmViewer.java
eu.etaxonomy.taxeditor.store/schema/eu.etaxonomy.taxeditor.store.cdmViewer.exsd
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerChooser.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/ICdmViewer.java

index b2dc9bfa68955694681b624893deb79574441ea5..53080cc47d69177e2a1509e581dcb603f1e19765 100644 (file)
           point="eu.etaxonomy.taxeditor.store.cdmViewer">
        <cdmViewer
              class="eu.etaxonomy.taxeditor.editor.EditorCdmViewer">
-          <input
-                class="eu.etaxonomy.cdm.model.taxon.TaxonBase">
-             <part
-                   class="eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor"
-                   name="Taxon Editor">
-             </part>
-          </input>
        </cdmViewer>
     </extension>
 </plugin>
index 4a5b4ea8d189e8b47c7712d0163ceae89559b421..877ed28f44530befcf5ce97db7b069ce3947e9c4 100644 (file)
@@ -9,6 +9,9 @@
 */
 package eu.etaxonomy.taxeditor.editor;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.log4j.Logger;
 import org.eclipse.ui.PartInitException;
 
@@ -31,15 +34,15 @@ public class EditorCdmViewer implements ICdmViewer {
      * @see eu.etaxonomy.taxeditor.view.ICdmViewer#show(java.lang.Object)
      */
     @Override
-    public void show(Object input, String viewerClass) {
+    public void show(Object input, Class<?> viewerClass) {
         try {
-            if(viewerClass.equals(MultiPageTaxonEditor.class.toString())){
+            if(viewerClass.equals(MultiPageTaxonEditor.class)){
                 if(input instanceof TaxonBase){
                     TaxonEditorInput editorInput = TaxonEditorInput.NewInstanceFromTaxonBase(((TaxonBase<?>) input).getUuid());
                     EditorUtil.open(editorInput);
                 }
             }
-            else if(viewerClass.equals(DerivateView.class.toString())){
+            else if(viewerClass.equals(DerivateView.class)){
                 if(input instanceof SpecimenOrObservationBase){
                 }
             }
@@ -50,4 +53,16 @@ public class EditorCdmViewer implements ICdmViewer {
         }
     }
 
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.view.ICdmViewer#getViewerClasses(java.lang.Object)
+     */
+    @Override
+    public Map<Class<?>, String> getViewerClasses(Object input) {
+        Map<Class<?>, String> viewerNameMap = new HashMap<Class<?>, String>();
+        if(input instanceof TaxonBase){
+            viewerNameMap.put(MultiPageTaxonEditor.class, "Taxon Editor");
+        }
+        return viewerNameMap;
+    }
+
 }
index b7f2964c8c89a9a037625228d3d4dc8afa10edd0..d885f02880ef9bb394893692ed8dbcbadc55425a 100644 (file)
          <appInfo>
             <meta.element />
          </appInfo>
+         <documentation>
+            Extensions must provide and ICdmViewer which maps input elements to viewers which are able to display information for them.
+         </documentation>
       </annotation>
       <complexType>
-         <sequence minOccurs="1" maxOccurs="unbounded">
-            <element ref="cdmViewer"/>
+         <sequence>
+            <element ref="cdmViewer" minOccurs="1" maxOccurs="unbounded"/>
          </sequence>
          <attribute name="point" type="string" use="required">
             <annotation>
@@ -55,7 +58,6 @@
       </annotation>
       <complexType>
          <sequence>
-            <element ref="input"/>
          </sequence>
          <attribute name="class" type="string" use="required">
             <annotation>
       </complexType>
    </element>
 
-   <element name="input">
-      <annotation>
-         <documentation>
-            The input element which can be shown by the cdmViewer.
-         </documentation>
-      </annotation>
-      <complexType>
-         <sequence>
-            <element ref="part"/>
-         </sequence>
-         <attribute name="class" type="string" use="required">
-            <annotation>
-               <documentation>
-                  The class of the input element.
-               </documentation>
-               <appInfo>
-                  <meta.attribute kind="java"/>
-               </appInfo>
-            </annotation>
-         </attribute>
-      </complexType>
-   </element>
-
-   <element name="part">
-      <annotation>
-         <documentation>
-            The part which can be opened for the given input.
-         </documentation>
-      </annotation>
-      <complexType>
-         <attribute name="class" type="string" use="required">
-            <annotation>
-               <documentation>
-                  The class of the part.
-               </documentation>
-               <appInfo>
-                  <meta.attribute kind="java"/>
-               </appInfo>
-            </annotation>
-         </attribute>
-         <attribute name="name" type="string">
-            <annotation>
-               <documentation>
-                  The name of the part.
-               </documentation>
-            </annotation>
-         </attribute>
-      </complexType>
-   </element>
-
    <annotation>
       <appInfo>
          <meta.section type="since"/>
index 0129f7f787eb5265b27035c41fa977486e093f50..a862b694640103098dda6844f3a2bcb5bb5875d0 100644 (file)
@@ -9,14 +9,16 @@
  */
 package eu.etaxonomy.taxeditor.view;
 
-import java.util.ArrayList;
-import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
 
 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.IStatus;
 import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.dialogs.PopupDialog;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.ILabelProvider;
@@ -34,6 +36,7 @@ import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Table;
 
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
  * This class provides the possibility to choose from a list of possible viewers
@@ -46,7 +49,7 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
 public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
 
     private Object input;
-    private Collection<IConfigurationElement> partConfigurationElements;
+    private Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap;
 
     public CdmViewerChooser(Shell parentShell) {
         this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
@@ -66,23 +69,40 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
      */
     public void chooseViewer(Object input){
         this.input = input;
-        partConfigurationElements = new ArrayList<>();
+        this.nameViewerMap = new HashMap<Entry<Class<?>, String>, ICdmViewer>();
 
         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);
+        for (IConfigurationElement configElement : extensions) {
+            try {
+                Object object = configElement.createExecutableExtension("class");
+                if(object instanceof ICdmViewer){
+                    ICdmViewer cdmViewer = (ICdmViewer)object;
+                    Map<Class<?>, String> viewerClasses = cdmViewer.getViewerClasses(input);
+                    for (Entry<Class<?>, String> entry : viewerClasses.entrySet()) {
+                        nameViewerMap.put(entry, cdmViewer);
                     }
                 }
+                else{
+                    MessagingUtils.error(CdmViewerChooser.class, new Status(IStatus.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Could not load cdmViewer extension"));
+                }
+            } catch (CoreException e) {
+                MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e);
+            }
+        }
+        //if only one editor is available then open it
+        if(nameViewerMap.size()==1){
+            Entry<Class<?>, String> next = nameViewerMap.keySet().iterator().next();
+            ICdmViewer cdmViewer = nameViewerMap.get(next);
+            cdmViewer.show(input, next.getKey());
+        }
+        else{
+            if(nameViewerMap.isEmpty()){
+                this.setInfoText("No viewers registered for this input");
             }
+            this.open();
         }
-        this.open();
     }
 
     /* (non-Javadoc)
@@ -94,7 +114,7 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
         viewer.setContentProvider(new ArrayContentProvider());
         viewer.setLabelProvider(this);
         viewer.addSelectionChangedListener(this);
-        viewer.setInput(partConfigurationElements);
+        viewer.setInput(nameViewerMap.keySet());
         return parent;
     }
 
@@ -106,21 +126,11 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
         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(nameViewerMap.containsKey(firstElement)){
+                Entry<Class<?>, String> entry = (Entry<Class<?>, String>)firstElement;
+                ICdmViewer cdmViewer = nameViewerMap.get(entry);
+                cdmViewer.show(input, entry.getKey());
+                this.close();
             }
         }
     }
@@ -131,12 +141,9 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
     @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");
-            }
+        if(nameViewerMap.containsKey(element)){
+            Entry<Class<?>, String> entry = (Entry<Class<?>, String>) element;
+            text = entry.getValue();
         }
         return text;
     }
index 0ef3362a94fb5af58796b3165e27e0c64dd8d438..0dd9e9b445e7d74df05c893c5c0ffc4d0220505b 100644 (file)
@@ -9,6 +9,8 @@
 */
 package eu.etaxonomy.taxeditor.view;
 
+import java.util.Map;
+
 
 /**
  * Implementors of this interface provide a mapping of input elements to views
@@ -21,11 +23,22 @@ package eu.etaxonomy.taxeditor.view;
  */
 public interface ICdmViewer {
 
+    /**
+     * For the given input a map specifying the available viewers classes as
+     * keys and their string representation as values is returned
+     *
+     * @param input
+     *            the input for which the viewer classes should be returned
+     * @return a map holding the viewer classes as keys and their string
+     *         representations as values
+     */
+    public Map<Class<?>, String> getViewerClasses(Object input);
+
     /**
      * Opens the viewer defined by the given viewerClass for the given input.
      * @param input the input for which a viewer should be opened
      * @param viewerClass the qualified class name of the viewer
      */
-    public void show(Object input, String viewerClass);
+    public void show(Object input, Class<?> viewerClass);
 
 }