Project

General

Profile

Download (9.07 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.ArrayList;
12
import java.util.HashMap;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17

    
18
import org.eclipse.core.commands.Command;
19
import org.eclipse.core.runtime.IConfigurationElement;
20
import org.eclipse.core.runtime.IExtensionRegistry;
21
import org.eclipse.core.runtime.Platform;
22
import org.eclipse.e4.core.commands.ECommandService;
23
import org.eclipse.e4.core.commands.EHandlerService;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25
import org.eclipse.jface.viewers.TreeNode;
26
import org.eclipse.ui.PlatformUI;
27
import org.eclipse.ui.commands.ICommandService;
28

    
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30
import eu.etaxonomy.cdm.model.common.ICdmBase;
31
import eu.etaxonomy.cdm.model.name.NomenclaturalSource;
32
import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto;
33
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
34
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
35
import eu.etaxonomy.taxeditor.model.MessagingUtils;
36
import eu.etaxonomy.taxeditor.store.CdmStore;
37

    
38
/**
39
 * Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
40
 * @author pplitzner
41
 * @date Jul 7, 2015
42
 */
43
public class CdmViewerUtilE4 {
44

    
45
    /**
46
     * Returns a map of available commands to open the given input. Keys are the
47
     * command IDs and values are their string representations.
48
     *
49
     * @param input
50
     *            the object which should be handled by the available commands
51
     * @return a key-value map of available commands and their string
52
     *         representation
53
     */
54
    public static Map<Command, String> getAvailableViewers(List<UuidAndTitleCache<? extends ICdmBase>> input, ECommandService commandService,
55
            EHandlerService handlerService){
56

    
57
        Map<Command, String> commandViewerNameMap = new HashMap<>();
58

    
59
        Set<Class<?>> inputClasses = new HashSet<>();
60
        if(input!=null){
61
            input.stream()
62
                .filter(uuid->uuid.getType()!= null)
63
                .forEach(uuid->inputClasses.add(uuid.getType()));
64

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

    
90
        return commandViewerNameMap;
91
    }
92

    
93
    /**
94
     * Returns a map of available commands to open the given input.
95
     * Keys are the command IDs and values are their string representations.
96
     *
97
     * @param input
98
     *            the object which should be handled by the available commands
99
     * @return a key-value map of available commands and their string
100
     *         representation
101
     */
102
    public static  Map<Command, String> getAvailableViewers(Object input){
103
                Map<Command, String> commandViewerNameMap = new HashMap<>();
104

    
105
        if(input!=null){
106
            //for generic UuidAndTitleCache objects try to load the object
107
            if (input instanceof UuidAndTitleCache){
108
                UuidAndTitleCache<? extends CdmBase> uuidAndTitleCache = (UuidAndTitleCache<? extends CdmBase>)input;
109
                input = CdmStore.getCommonService().find(uuidAndTitleCache.getType(), uuidAndTitleCache.getUuid());
110
            }
111
            //for tree nodes get the value resp. the object of the node
112
            else if (input instanceof TreeNode){
113
                TreeNode treeNode = (TreeNode)input;
114
                input = treeNode.getValue();
115
            }
116
            if(input==null){
117
                return commandViewerNameMap;
118
            }
119
            if (input instanceof NomenclaturalSource){
120
                input = ((NomenclaturalSource)input).getSourcedName();
121
            }
122

    
123
            IExtensionRegistry reg = Platform.getExtensionRegistry();
124
            IConfigurationElement[] extensions = reg
125
                    .getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
126
            for (IConfigurationElement configElement : extensions) {
127
                if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
128
                    try {
129
                        String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
130
                        String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
131
                        Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
132
                        if(selectionClass.isAssignableFrom(input.getClass())){
133
                            ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
134
                            Command command = commandService.getCommand(commandId);
135
                            if(command.isEnabled()){
136
                                commandViewerNameMap.put(command, viewerName);
137
                            }
138
                        }
139
                    } catch (ClassNotFoundException e) {
140
                        MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
141
                    }
142
                }
143
            }
144
        }
145
        return commandViewerNameMap;
146
    }
147

    
148

    
149
    /**
150
     * Trys to create a List of {@link UuidAndTitleCache} from the selection.<BR>
151
     * Selections which are not recognized are not included in the list.<BR>
152
     * As a minimum each returned element contains the type and the uuid.
153
     * If available also the label and the CdmBase entity is added (later by using the subclass {@link DtoWithEntity}.
154
     * Label creation is not forced (TODO maybe in future we should have a parameter forceLabelCreation)
155
     * @param
156
     * @return list of {@link UuidAndTitleCache}
157
     */
158
    public static List<UuidAndTitleCache<? extends ICdmBase>> transformSelectionToUuidAndTitleCacheList(Object selection) {
159

    
160
        //make it all a list
161
        List<Object> multipleSelection = new ArrayList<>();
162
        if(selection instanceof IStructuredSelection){
163
            IStructuredSelection structuredSelection = ((IStructuredSelection) selection);
164
            ((List<?>)structuredSelection.toList()).forEach(o->multipleSelection.add(o));
165
        }else if(selection.getClass().isArray()){
166
            for (Object o : ((Object[])selection)){
167
                multipleSelection.add(o);
168
            }
169
        }else if(selection instanceof List){
170
            multipleSelection.addAll((List<?>)selection);
171
        }else{
172
            multipleSelection.add(selection);
173
        }
174

    
175
        //filter all as uuidIdAndTitleCache
176
        List<UuidAndTitleCache<? extends ICdmBase>> uuidTypes = new ArrayList<>();
177

    
178
        for (Object o : multipleSelection){
179
            if (o instanceof FeatureNodeContainer){
180
                continue ;
181
            }
182
            if(o instanceof TreeNode){
183
                o = ((TreeNode)o).getValue();
184
            }
185
            o = CdmBase.deproxy(o);
186

    
187
            UuidAndTitleCache<? extends ICdmBase> uuidAndTitleCache;
188
            if (o instanceof CdmBase){
189
                CdmBase cdmBase = (CdmBase)o;
190
                uuidAndTitleCache = new DtoWithEntity<>(cdmBase);
191
            }else if (o instanceof ReferencingObjectDto){
192
                ReferencingObjectDto dto = (ReferencingObjectDto)o;
193
                uuidAndTitleCache = dto.getOpenInTarget() != null ? dto.getOpenInTarget() : dto;
194
            }else if (o instanceof UuidAndTitleCache){
195
                uuidAndTitleCache = (UuidAndTitleCache<CdmBase>)o;
196
            }else{
197
                continue;
198
            }
199
            uuidTypes.add(uuidAndTitleCache);
200
        }
201
        return uuidTypes;
202
    }
203
}
(3-3/4)