Generalize cdmViewer extension point
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerUtil.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.view;
11
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IExtensionRegistry;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.core.runtime.Status;
22
23 import eu.etaxonomy.taxeditor.model.MessagingUtils;
24 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
25
26 /**
27 * @author pplitzner
28 * @date Jul 7, 2015
29 *
30 */
31 public class CdmViewerUtil {
32
33 public static Map<Entry<Class<?>, String>, ICdmViewer> getNameViewerMap(Object input){
34 Map<Entry<Class<?>, String>, ICdmViewer> nameViewerMap = new HashMap<Entry<Class<?>, String>, ICdmViewer>();
35
36 IExtensionRegistry reg = Platform.getExtensionRegistry();
37 IConfigurationElement[] extensions = reg
38 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
39 for (IConfigurationElement configElement : extensions) {
40 try {
41 Object object = configElement.createExecutableExtension("class");
42 if(object instanceof ICdmViewer){
43 ICdmViewer cdmViewer = (ICdmViewer)object;
44 Map<Class<?>, String> viewerClasses = cdmViewer.getViewerClasses(input);
45 for (Entry<Class<?>, String> entry : viewerClasses.entrySet()) {
46 nameViewerMap.put(entry, cdmViewer);
47 }
48 }
49 else{
50 MessagingUtils.error(CdmViewerChooser.class, new Status(IStatus.ERROR, TaxeditorStorePlugin.PLUGIN_ID, "Could not load cdmViewer extension"));
51 }
52 } catch (CoreException e) {
53 MessagingUtils.error(CdmViewerChooser.class, "Could not load cdmViewer extension", e);
54 }
55 }
56 return nameViewerMap;
57 }
58
59 public static Map<String, String> getAvailableViewers(Object input){
60 Map<String, String> commandViewerNameMap = new HashMap<String, String>();
61
62 IExtensionRegistry reg = Platform.getExtensionRegistry();
63 IConfigurationElement[] extensions = reg
64 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer");
65 for (IConfigurationElement configElement : extensions) {
66 if(configElement.getName().equals("viewCommandMapping")){
67 String commandId = configElement.getAttribute("commandId");
68 String viewerName = configElement.getAttribute("viewerName");
69 try {
70 Object selectionObject = configElement.createExecutableExtension("selection");
71 System.out.println(selectionObject);
72 Class<? extends Object> selectionClass = selectionObject.getClass();
73 if(input.getClass().isAssignableFrom(selectionClass)){
74 commandViewerNameMap.put(commandId, viewerName);
75 }
76 } catch (CoreException e) {
77 MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e);
78 }
79 }
80 }
81 return commandViewerNameMap;
82 }
83
84 }