ref #7073: keep the details view when clicking on supplemental data
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerUtilE4.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.e4.core.commands.ECommandService;
19 import org.eclipse.e4.core.commands.EHandlerService;
20 import org.eclipse.jface.viewers.TreeNode;
21
22 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
23 import eu.etaxonomy.taxeditor.model.MessagingUtils;
24 import eu.etaxonomy.taxeditor.store.CdmStore;
25
26 /**
27 * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
28 * @author pplitzner
29 * @date Jul 7, 2015
30 *
31 */
32 public class CdmViewerUtilE4 {
33
34 /**
35 * Returns a map of available commands to open the given input. Keys are the
36 * command IDs and values are their string representations.
37 *
38 * @param input
39 * the object which should be handled by the available commands
40 * @return a key-value map of available commands and their string
41 * representation
42 */
43 public static Map<Command, String> getAvailableViewers(Object input, ECommandService commandService,
44 EHandlerService handlerService){
45 Map<Command, String> commandViewerNameMap = new HashMap<>();
46
47 if(input!=null){
48 //for generic UuidAndTitleCache objects try to load the object
49 if (input instanceof UuidAndTitleCache){
50 UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache)input;
51 input = CdmStore.getCommonService().find(uuidAndTitleCache.getType(), uuidAndTitleCache.getUuid());
52 }
53 //for tree nodes get the value resp. the object of the node
54 else if (input instanceof TreeNode){
55 TreeNode treeNode = (TreeNode)input;
56 input = treeNode.getValue();
57 }
58
59 IExtensionRegistry reg = Platform.getExtensionRegistry();
60 IConfigurationElement[] extensions = reg
61 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
62 for (IConfigurationElement configElement : extensions) {
63 try {
64 if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
65 String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
66 String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
67 Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
68 if(selectionClass.isAssignableFrom(input.getClass())){
69 Command command = commandService.getCommand(commandId);
70 commandViewerNameMap.put(command, viewerName);
71 }
72 }
73 } catch (ClassNotFoundException e) {
74 MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
75 }
76 }
77 }
78 return commandViewerNameMap;
79 }
80
81 }