Revision b93817a3
Added by Patrick Plitzner about 9 years ago
- refactored cdmViewer extension point to be more generic
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 |
} |
Also available in: Unified diff