Project

General

Profile

Download (3.68 KB) Statistics
| Branch: | Tag: | Revision:
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

    
25
/**
26
 * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
27
 * @author pplitzner
28
 * @date Jul 7, 2015
29
 */
30
public class CdmViewerUtilE4 {
31

    
32
    /**
33
     * Returns a map of available commands to open the given input. Keys are the
34
     * 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, ECommandService commandService,
42
            EHandlerService handlerService){
43
        Map<Command, String> commandViewerNameMap = new HashMap<>();
44

    
45
        Class<?> inputClass = null;
46
        if(input!=null){
47
            //for generic UuidAndTitleCache objects try to load the object
48
            if (input instanceof UuidAndTitleCache){
49
                UuidAndTitleCache<?> uuidAndTitleCache = (UuidAndTitleCache<?>)input;
50
                inputClass = uuidAndTitleCache.getType();
51
            }
52
            //for tree nodes get the value resp. the object of the node
53
            else if (input instanceof TreeNode){
54
                TreeNode treeNode = (TreeNode)input;
55
                inputClass = treeNode.getValue() == null? null: treeNode.getValue().getClass();
56
            }
57
            if (inputClass == null){
58
                inputClass = input.getClass();
59
            }
60
        }
61

    
62
        if(inputClass!=null){
63
            IExtensionRegistry reg = Platform.getExtensionRegistry();
64
            IConfigurationElement[] extensions = reg
65
                    .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
66
            for (IConfigurationElement configElement : extensions) {
67
                try {
68
                    if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
69
                        String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
70
                        String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
71
                        Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
72
                        if(selectionClass!=null
73
                                && commandId!=null
74
                                && viewerName!=null
75
                                && selectionClass.isAssignableFrom(inputClass)){
76
                            Command command = commandService.getCommand(commandId);
77
                            commandViewerNameMap.put(command, viewerName);
78
                        }
79
                    }
80
                } catch (ClassNotFoundException e) {
81
                    MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
82
                }
83
            }
84
        }
85
        return commandViewerNameMap;
86
    }
87

    
88
}
(4-4/4)