Project

General

Profile

« Previous | Next » 

Revision b93817a3

Added by Patrick Plitzner about 9 years ago

  • refactored cdmViewer extension point to be more generic

View differences:

eu.etaxonomy.taxeditor.editor/plugin.xml
1617 1617
          point="eu.etaxonomy.taxeditor.store.cdmViewer">
1618 1618
       <cdmViewer
1619 1619
             class="eu.etaxonomy.taxeditor.editor.EditorCdmViewer">
1620
          <input
1621
                class="eu.etaxonomy.cdm.model.taxon.TaxonBase">
1622
             <part
1623
                   class="eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor"
1624
                   name="Taxon Editor">
1625
             </part>
1626
          </input>
1627 1620
       </cdmViewer>
1628 1621
    </extension>
1629 1622
</plugin>
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/EditorCdmViewer.java
9 9
*/
10 10
package eu.etaxonomy.taxeditor.editor;
11 11

  
12
import java.util.HashMap;
13
import java.util.Map;
14

  
12 15
import org.apache.log4j.Logger;
13 16
import org.eclipse.ui.PartInitException;
14 17

  
......
31 34
     * @see eu.etaxonomy.taxeditor.view.ICdmViewer#show(java.lang.Object)
32 35
     */
33 36
    @Override
34
    public void show(Object input, String viewerClass) {
37
    public void show(Object input, Class<?> viewerClass) {
35 38
        try {
36
            if(viewerClass.equals(MultiPageTaxonEditor.class.toString())){
39
            if(viewerClass.equals(MultiPageTaxonEditor.class)){
37 40
                if(input instanceof TaxonBase){
38 41
                    TaxonEditorInput editorInput = TaxonEditorInput.NewInstanceFromTaxonBase(((TaxonBase<?>) input).getUuid());
39 42
                    EditorUtil.open(editorInput);
40 43
                }
41 44
            }
42
            else if(viewerClass.equals(DerivateView.class.toString())){
45
            else if(viewerClass.equals(DerivateView.class)){
43 46
                if(input instanceof SpecimenOrObservationBase){
44 47
                }
45 48
            }
......
50 53
        }
51 54
    }
52 55

  
56
    /* (non-Javadoc)
57
     * @see eu.etaxonomy.taxeditor.view.ICdmViewer#getViewerClasses(java.lang.Object)
58
     */
59
    @Override
60
    public Map<Class<?>, String> getViewerClasses(Object input) {
61
        Map<Class<?>, String> viewerNameMap = new HashMap<Class<?>, String>();
62
        if(input instanceof TaxonBase){
63
            viewerNameMap.put(MultiPageTaxonEditor.class, "Taxon Editor");
64
        }
65
        return viewerNameMap;
66
    }
67

  
53 68
}
eu.etaxonomy.taxeditor.store/schema/eu.etaxonomy.taxeditor.store.cdmViewer.exsd
15 15
         <appInfo>
16 16
            <meta.element />
17 17
         </appInfo>
18
         <documentation>
19
            Extensions must provide and ICdmViewer which maps input elements to viewers which are able to display information for them.
20
         </documentation>
18 21
      </annotation>
19 22
      <complexType>
20
         <sequence minOccurs="1" maxOccurs="unbounded">
21
            <element ref="cdmViewer"/>
23
         <sequence>
24
            <element ref="cdmViewer" minOccurs="1" maxOccurs="unbounded"/>
22 25
         </sequence>
23 26
         <attribute name="point" type="string" use="required">
24 27
            <annotation>
......
55 58
      </annotation>
56 59
      <complexType>
57 60
         <sequence>
58
            <element ref="input"/>
59 61
         </sequence>
60 62
         <attribute name="class" type="string" use="required">
61 63
            <annotation>
......
70 72
      </complexType>
71 73
   </element>
72 74

  
73
   <element name="input">
74
      <annotation>
75
         <documentation>
76
            The input element which can be shown by the cdmViewer.
77
         </documentation>
78
      </annotation>
79
      <complexType>
80
         <sequence>
81
            <element ref="part"/>
82
         </sequence>
83
         <attribute name="class" type="string" use="required">
84
            <annotation>
85
               <documentation>
86
                  The class of the input element.
87
               </documentation>
88
               <appInfo>
89
                  <meta.attribute kind="java"/>
90
               </appInfo>
91
            </annotation>
92
         </attribute>
93
      </complexType>
94
   </element>
95

  
96
   <element name="part">
97
      <annotation>
98
         <documentation>
99
            The part which can be opened for the given input.
100
         </documentation>
101
      </annotation>
102
      <complexType>
103
         <attribute name="class" type="string" use="required">
104
            <annotation>
105
               <documentation>
106
                  The class of the part.
107
               </documentation>
108
               <appInfo>
109
                  <meta.attribute kind="java"/>
110
               </appInfo>
111
            </annotation>
112
         </attribute>
113
         <attribute name="name" type="string">
114
            <annotation>
115
               <documentation>
116
                  The name of the part.
117
               </documentation>
118
            </annotation>
119
         </attribute>
120
      </complexType>
121
   </element>
122

  
123 75
   <annotation>
124 76
      <appInfo>
125 77
         <meta.section type="since"/>
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerChooser.java
9 9
 */
10 10
package eu.etaxonomy.taxeditor.view;
11 11

  
12
import java.util.ArrayList;
13
import java.util.Collection;
12
import java.util.HashMap;
13
import java.util.Map;
14
import java.util.Map.Entry;
14 15

  
15 16
import org.eclipse.core.runtime.CoreException;
16 17
import org.eclipse.core.runtime.IConfigurationElement;
17 18
import org.eclipse.core.runtime.IExtensionRegistry;
18
import org.eclipse.core.runtime.InvalidRegistryObjectException;
19
import org.eclipse.core.runtime.IStatus;
19 20
import org.eclipse.core.runtime.Platform;
21
import org.eclipse.core.runtime.Status;
20 22
import org.eclipse.jface.dialogs.PopupDialog;
21 23
import org.eclipse.jface.viewers.ArrayContentProvider;
22 24
import org.eclipse.jface.viewers.ILabelProvider;
......
34 36
import org.eclipse.swt.widgets.Table;
35 37

  
36 38
import eu.etaxonomy.taxeditor.model.MessagingUtils;
39
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
37 40

  
38 41
/**
39 42
 * This class provides the possibility to choose from a list of possible viewers
......
46 49
public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
47 50

  
48 51
    private Object input;
49
    private Collection<IConfigurationElement> partConfigurationElements;
52
    private Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap;
50 53

  
51 54
    public CdmViewerChooser(Shell parentShell) {
52 55
        this(parentShell, SWT.RESIZE | SWT.ON_TOP, true, false, false, false, false, "Open in ...",
......
66 69
     */
67 70
    public void chooseViewer(Object input){
68 71
        this.input = input;
69
        partConfigurationElements = new ArrayList<>();
72
        this.nameViewerMap = new HashMap<Entry<Class<?>, String>, ICdmViewer>();
70 73

  
71 74
        IExtensionRegistry reg = Platform.getExtensionRegistry();
72 75
        IConfigurationElement[] extensions = reg
73 76
                .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
74
        for (IConfigurationElement e : extensions) {
75
            for (IConfigurationElement inputConfigurationElement : e.getChildren("input")) {
76
                String inputClass = inputConfigurationElement.getAttribute("class");
77
                if(inputClass.equals(input.getClass().toString())){
78
                    System.out.println("input class: " + inputClass);
79
                    for (IConfigurationElement partConfigurationElement : inputConfigurationElement.getChildren("part")) {
80
                        partConfigurationElements.add(partConfigurationElement);
77
        for (IConfigurationElement configElement : extensions) {
78
            try {
79
                Object object = configElement.createExecutableExtension("class");
80
                if(object instanceof ICdmViewer){
81
                    ICdmViewer cdmViewer = (ICdmViewer)object;
82
                    Map<Class<?>, String> viewerClasses = cdmViewer.getViewerClasses(input);
83
                    for (Entry<Class<?>, String> entry : viewerClasses.entrySet()) {
84
                        nameViewerMap.put(entry, cdmViewer);
81 85
                    }
82 86
                }
87
                else{
88
                    MessagingUtils.error(CdmViewerChooser.class, new Status(IStatus.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Could not load cdmViewer extension"));
89
                }
90
            } catch (CoreException e) {
91
                MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e);
92
            }
93
        }
94
        //if only one editor is available then open it
95
        if(nameViewerMap.size()==1){
96
            Entry<Class<?>, String> next = nameViewerMap.keySet().iterator().next();
97
            ICdmViewer cdmViewer = nameViewerMap.get(next);
98
            cdmViewer.show(input, next.getKey());
99
        }
100
        else{
101
            if(nameViewerMap.isEmpty()){
102
                this.setInfoText("No viewers registered for this input");
83 103
            }
104
            this.open();
84 105
        }
85
        this.open();
86 106
    }
87 107

  
88 108
    /* (non-Javadoc)
......
94 114
        viewer.setContentProvider(new ArrayContentProvider());
95 115
        viewer.setLabelProvider(this);
96 116
        viewer.addSelectionChangedListener(this);
97
        viewer.setInput(partConfigurationElements);
117
        viewer.setInput(nameViewerMap.keySet());
98 118
        return parent;
99 119
    }
100 120

  
......
106 126
        ISelection selection = event.getSelection();
107 127
        if(selection instanceof IStructuredSelection){
108 128
            Object firstElement = ((IStructuredSelection) selection).getFirstElement();
109
            if(firstElement instanceof IConfigurationElement){
110
                IConfigurationElement configElement = (IConfigurationElement)firstElement;
111
                String viewerClass = configElement.getAttribute("class");
112
                try {
113
                    //get the grand parent (this is the cdmViewer)
114
                    Object o = ((IConfigurationElement)((IConfigurationElement)configElement.getParent()).getParent()).createExecutableExtension("class");
115
                    if(o instanceof ICdmViewer){
116
                        ((ICdmViewer) o).show(input, viewerClass);
117
                    this.close();
118
                    }
119
                } catch (InvalidRegistryObjectException e) {
120
                    MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e);
121
                } catch (CoreException e) {
122
                    MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e);
123
                }
129
            if(nameViewerMap.containsKey(firstElement)){
130
                Entry<Class<?>, String> entry = (Entry<Class<?>, String>)firstElement;
131
                ICdmViewer cdmViewer = nameViewerMap.get(entry);
132
                cdmViewer.show(input, entry.getKey());
133
                this.close();
124 134
            }
125 135
        }
126 136
    }
......
131 141
    @Override
132 142
    public String getText(Object element) {
133 143
        String text = null;
134
        if(element instanceof IConfigurationElement){
135
            IConfigurationElement configElement = (IConfigurationElement)element;
136
            text = configElement.getAttribute("name");
137
            if(text==null){
138
                text = configElement.getAttribute("class");
139
            }
144
        if(nameViewerMap.containsKey(element)){
145
            Entry<Class<?>, String> entry = (Entry<Class<?>, String>) element;
146
            text = entry.getValue();
140 147
        }
141 148
        return text;
142 149
    }
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/ICdmViewer.java
9 9
*/
10 10
package eu.etaxonomy.taxeditor.view;
11 11

  
12
import java.util.Map;
13

  
12 14

  
13 15
/**
14 16
 * Implementors of this interface provide a mapping of input elements to views
......
21 23
 */
22 24
public interface ICdmViewer {
23 25

  
26
    /**
27
     * For the given input a map specifying the available viewers classes as
28
     * keys and their string representation as values is returned
29
     *
30
     * @param input
31
     *            the input for which the viewer classes should be returned
32
     * @return a map holding the viewer classes as keys and their string
33
     *         representations as values
34
     */
35
    public Map<Class<?>, String> getViewerClasses(Object input);
36

  
24 37
    /**
25 38
     * Opens the viewer defined by the given viewerClass for the given input.
26 39
     * @param input the input for which a viewer should be opened
27 40
     * @param viewerClass the qualified class name of the viewer
28 41
     */
29
    public void show(Object input, String viewerClass);
42
    public void show(Object input, Class<?> viewerClass);
30 43

  
31 44
}

Also available in: Unified diff