2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.view
;
11 import java
.util
.HashMap
;
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
;
22 import eu
.etaxonomy
.cdm
.persistence
.dto
.ReferencingObjectDto
;
23 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
24 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
27 * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
31 public class CdmViewerUtilE4
{
34 * Returns a map of available commands to open the given input. Keys are the
35 * command IDs and values are their string representations.
38 * the object which should be handled by the available commands
39 * @return a key-value map of available commands and their string
42 public static Map
<Command
, String
> getAvailableViewers(Object input
, ECommandService commandService
,
43 EHandlerService handlerService
){
44 Map
<Command
, String
> commandViewerNameMap
= new HashMap
<>();
46 Class
<?
> inputClass
= null;
48 if (input
instanceof ReferencingObjectDto
){
49 ReferencingObjectDto dto
= (ReferencingObjectDto
)input
;
50 inputClass
= dto
.getOpenInTarget() == null ? dto
.getType() : dto
.getOpenInTarget().getType();
52 //for generic UuidAndTitleCache objects try to load the object
53 else if (input
instanceof UuidAndTitleCache
){
54 UuidAndTitleCache
<?
> uuidAndTitleCache
= (UuidAndTitleCache
<?
>)input
;
55 inputClass
= uuidAndTitleCache
.getType();
57 //for tree nodes get the value resp. the object of the node
58 else if (input
instanceof TreeNode
){
59 TreeNode treeNode
= (TreeNode
)input
;
60 inputClass
= treeNode
.getValue() == null?
null: treeNode
.getValue().getClass();
62 if (inputClass
== null){
63 inputClass
= input
.getClass();
68 IExtensionRegistry reg
= Platform
.getExtensionRegistry();
69 IConfigurationElement
[] extensions
= reg
70 .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
71 for (IConfigurationElement configElement
: extensions
) {
73 if(configElement
.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
74 String commandId
= configElement
.getAttribute("commandId"); //$NON-NLS-1$
75 String viewerName
= configElement
.getAttribute("viewerName"); //$NON-NLS-1$
76 Class
<?
> selectionClass
= Class
.forName(configElement
.getAttribute("selection")); //$NON-NLS-1$
77 if(selectionClass
!=null
80 && selectionClass
.isAssignableFrom(inputClass
)){
81 Command command
= commandService
.getCommand(commandId
);
82 commandViewerNameMap
.put(command
, viewerName
);
85 } catch (ClassNotFoundException e
) {
86 MessagingUtils
.error(CdmViewerChooser
.class, "Could not initalize selection class element of cdmViewer extension", e
); //$NON-NLS-1$
90 return commandViewerNameMap
;