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.model.term.DefinedTermBase;
|
33
|
import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto;
|
34
|
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
|
35
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
36
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
37
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
38
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
39
|
|
40
|
/**
|
41
|
* Scans eu.etaxonomy.taxeditor.store.cdmViewer extension point.
|
42
|
* @author pplitzner
|
43
|
* @date Jul 7, 2015
|
44
|
*/
|
45
|
public class CdmViewerUtil {
|
46
|
|
47
|
/**
|
48
|
* Returns a map of available commands to open the given input. Keys are the
|
49
|
* command IDs and values are their string representations.
|
50
|
*
|
51
|
* @param input
|
52
|
* the object which should be handled by the available commands
|
53
|
* @return a key-value map of available commands and their string
|
54
|
* representation
|
55
|
*/
|
56
|
public static Map<Command, String> getAvailableViewers(List<UuidAndTitleCache<? extends ICdmBase>> input, ECommandService commandService,
|
57
|
EHandlerService handlerService){
|
58
|
|
59
|
Map<Command, String> commandViewerNameMap = new HashMap<>();
|
60
|
|
61
|
Set<Class<?>> inputClasses = new HashSet<>();
|
62
|
if(input!=null){
|
63
|
input.stream()
|
64
|
.filter(uuid->uuid.getType()!= null)
|
65
|
.forEach(uuid->inputClasses.add(uuid.getType()));
|
66
|
|
67
|
inputClasses.forEach(ic->{
|
68
|
IExtensionRegistry reg = Platform.getExtensionRegistry();
|
69
|
IConfigurationElement[] extensions = reg
|
70
|
.getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
|
71
|
for (IConfigurationElement configElement : extensions) {
|
72
|
try {
|
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
|
78
|
&& commandId!=null
|
79
|
&& viewerName!=null
|
80
|
&& selectionClass.isAssignableFrom(ic)){
|
81
|
Command command = commandService.getCommand(commandId);
|
82
|
commandViewerNameMap.put(command, viewerName);
|
83
|
}
|
84
|
}
|
85
|
} catch (ClassNotFoundException e) {
|
86
|
MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
|
87
|
}
|
88
|
}
|
89
|
});
|
90
|
}
|
91
|
|
92
|
return commandViewerNameMap;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Returns a map of available commands to open the given input.
|
97
|
* Keys are the command IDs and values are their string representations.
|
98
|
*
|
99
|
* @param input
|
100
|
* the object which should be handled by the available commands
|
101
|
* @return a key-value map of available commands and their string
|
102
|
* representation
|
103
|
*/
|
104
|
public static Map<Command, String> getAvailableViewers(Object input){
|
105
|
Map<Command, String> commandViewerNameMap = new HashMap<>();
|
106
|
|
107
|
if(input!=null){
|
108
|
//for generic UuidAndTitleCache objects try to load the object
|
109
|
if (input instanceof UuidAndTitleCache){
|
110
|
UuidAndTitleCache<? extends CdmBase> uuidAndTitleCache = (UuidAndTitleCache<? extends CdmBase>)input;
|
111
|
input = CdmStore.getCommonService().find(uuidAndTitleCache.getType(), uuidAndTitleCache.getUuid());
|
112
|
}
|
113
|
//for tree nodes get the value resp. the object of the node
|
114
|
else if (input instanceof TreeNode){
|
115
|
TreeNode treeNode = (TreeNode)input;
|
116
|
input = treeNode.getValue();
|
117
|
}
|
118
|
if(input==null){
|
119
|
return commandViewerNameMap;
|
120
|
}
|
121
|
if (input instanceof NomenclaturalSource){
|
122
|
input = ((NomenclaturalSource)input).getSourcedName();
|
123
|
}
|
124
|
|
125
|
IExtensionRegistry reg = Platform.getExtensionRegistry();
|
126
|
IConfigurationElement[] extensions = reg
|
127
|
.getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$
|
128
|
for (IConfigurationElement configElement : extensions) {
|
129
|
if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$
|
130
|
try {
|
131
|
String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$
|
132
|
String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$
|
133
|
Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$
|
134
|
if(selectionClass.isAssignableFrom(input.getClass())){
|
135
|
ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
|
136
|
Command command = commandService.getCommand(commandId);
|
137
|
if(command.isEnabled()){
|
138
|
commandViewerNameMap.put(command, viewerName);
|
139
|
}
|
140
|
}
|
141
|
} catch (ClassNotFoundException e) {
|
142
|
MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$
|
143
|
}
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
return commandViewerNameMap;
|
148
|
}
|
149
|
|
150
|
|
151
|
/**
|
152
|
* Trys to create a List of {@link UuidAndTitleCache} from the selection.<BR>
|
153
|
* Selections which are not recognized are not included in the list.<BR>
|
154
|
* As a minimum each returned element contains the type and the uuid.
|
155
|
* If available also the label and the CdmBase entity is added (later by using the subclass {@link DtoWithEntity}.
|
156
|
* Label creation is not forced (TODO maybe in future we should have a parameter forceLabelCreation)
|
157
|
* @param
|
158
|
* @return list of {@link UuidAndTitleCache}
|
159
|
*/
|
160
|
public static List<UuidAndTitleCache<? extends ICdmBase>> transformSelectionToUuidAndTitleCacheList(Object selection) {
|
161
|
|
162
|
//make it all a list
|
163
|
List<Object> multipleSelection = new ArrayList<>();
|
164
|
if(selection instanceof IStructuredSelection){
|
165
|
IStructuredSelection structuredSelection = ((IStructuredSelection) selection);
|
166
|
((List<?>)structuredSelection.toList()).forEach(o->multipleSelection.add(o));
|
167
|
}else if(selection.getClass().isArray()){
|
168
|
for (Object o : ((Object[])selection)){
|
169
|
multipleSelection.add(o);
|
170
|
}
|
171
|
}else if(selection instanceof List){
|
172
|
multipleSelection.addAll((List<?>)selection);
|
173
|
}else{
|
174
|
multipleSelection.add(selection);
|
175
|
}
|
176
|
|
177
|
//filter all as uuidIdAndTitleCache
|
178
|
List<UuidAndTitleCache<? extends ICdmBase>> uuidTypes = new ArrayList<>();
|
179
|
|
180
|
for (Object o : multipleSelection){
|
181
|
if (o instanceof FeatureNodeContainer){
|
182
|
continue ;
|
183
|
}
|
184
|
if(o instanceof TreeNode){
|
185
|
o = ((TreeNode)o).getValue();
|
186
|
}
|
187
|
o = CdmBase.deproxy(o);
|
188
|
|
189
|
UuidAndTitleCache<? extends ICdmBase> uuidAndTitleCache;
|
190
|
if (o instanceof CdmBase){
|
191
|
CdmBase cdmBase = (CdmBase)o;
|
192
|
uuidAndTitleCache = new DtoWithEntity<>(cdmBase);
|
193
|
}else if (o instanceof ReferencingObjectDto){
|
194
|
ReferencingObjectDto dto = (ReferencingObjectDto)o;
|
195
|
uuidAndTitleCache = dto.getOpenInTarget() != null ? dto.getOpenInTarget() : dto;
|
196
|
}else if (o instanceof UuidAndTitleCache){
|
197
|
uuidAndTitleCache = (UuidAndTitleCache<CdmBase>)o;
|
198
|
}else if (o instanceof TermNodeDto){
|
199
|
uuidAndTitleCache = new UuidAndTitleCache<>(DefinedTermBase.class, ((TermNodeDto)o).getTerm().getUuid(), null, ((TermNodeDto)o).getTerm().getTitleCache());
|
200
|
}else{
|
201
|
continue;
|
202
|
}
|
203
|
uuidTypes.add(uuidAndTitleCache);
|
204
|
}
|
205
|
return uuidTypes;
|
206
|
}
|
207
|
}
|