Refactor generic "open in..." framework
[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
15 import org.eclipse.core.commands.Command;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtensionRegistry;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.commands.ICommandService;
21
22 import eu.etaxonomy.taxeditor.model.MessagingUtils;
23
24 /**
25 * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
26 * @author pplitzner
27 * @date Jul 7, 2015
28 *
29 */
30 public class CdmViewerUtil {
31
32 /**
33 * Returns a map of available commands to open the given input.
34 * Keys are the command IDs and values are their string representations.
35 *
36 * @param input
37 * the object which should be handled by the available commands
38 * @return a key-value map of available commands and their string
39 * representation
40 */
41 public static Map<Command, String> getAvailableViewers(Object input){
42 Map<Command, String> commandViewerNameMap = new HashMap<Command, String>();
43
44 if(input!=null){
45 IExtensionRegistry reg = Platform.getExtensionRegistry();
46 IConfigurationElement[] extensions = reg
47 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
48 for (IConfigurationElement configElement : extensions) {
49 if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
50 try {
51 String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
52 String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
53 Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
54 if(selectionClass.isAssignableFrom(input.getClass())){
55 ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
56 Command command = commandService.getCommand(commandId);
57 //TODO: maybe pass the command directly instead of just the command id
58 if(command.isEnabled()){
59 commandViewerNameMap.put(command, viewerName);
60 }
61 }
62 } catch (ClassNotFoundException e) {
63 MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
64 }
65 }
66 }
67 }
68 return commandViewerNameMap;
69 }
70
71 }