Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerUtil.java
1 /**
2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.view;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.eclipse.core.commands.Command;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.IExtensionRegistry;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.commands.ICommandService;
20
21 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
22 import eu.etaxonomy.taxeditor.model.MessagingUtils;
23 import eu.etaxonomy.taxeditor.store.CdmStore;
24
25 /**
26 * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
27 * @author pplitzner
28 * @date Jul 7, 2015
29 *
30 */
31 public class CdmViewerUtil {
32
33 /**
34 * Returns a map of available commands to open the given input.
35 * Keys are the command IDs and values are their string representations.
36 *
37 * @param input
38 * the object which should be handled by the available commands
39 * @return a key-value map of available commands and their string
40 * representation
41 */
42 public static Map<Command, String> getAvailableViewers(Object input){
43 Map<Command, String> commandViewerNameMap = new HashMap<Command, String>();
44
45 if(input!=null){
46 //for generic UuidAndTitleCache objects try to load the object
47 if (input instanceof UuidAndTitleCache){
48 UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache)input;
49 input = CdmStore.getCommonService().find(uuidAndTitleCache.getType(), uuidAndTitleCache.getUuid());
50 }
51
52
53 IExtensionRegistry reg = Platform.getExtensionRegistry();
54 IConfigurationElement[] extensions = reg
55 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
56 for (IConfigurationElement configElement : extensions) {
57 if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
58 try {
59 String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
60 String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
61 Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
62 if(selectionClass.isAssignableFrom(input.getClass())){
63 ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
64 Command command = commandService.getCommand(commandId);
65 if(command.isEnabled()){
66 commandViewerNameMap.put(command, viewerName);
67 }
68 }
69 } catch (ClassNotFoundException e) {
70 MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
71 }
72 }
73 }
74 }
75 return commandViewerNameMap;
76 }
77
78 }