Revision 880f5390
Added by Andreas Müller almost 3 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerUtilE4.java | ||
---|---|---|
8 | 8 |
*/ |
9 | 9 |
package eu.etaxonomy.taxeditor.view; |
10 | 10 |
|
11 |
import java.util.ArrayList; |
|
11 | 12 |
import java.util.HashMap; |
13 |
import java.util.HashSet; |
|
14 |
import java.util.List; |
|
12 | 15 |
import java.util.Map; |
16 |
import java.util.Set; |
|
13 | 17 |
|
14 | 18 |
import org.eclipse.core.commands.Command; |
15 | 19 |
import org.eclipse.core.runtime.IConfigurationElement; |
... | ... | |
17 | 21 |
import org.eclipse.core.runtime.Platform; |
18 | 22 |
import org.eclipse.e4.core.commands.ECommandService; |
19 | 23 |
import org.eclipse.e4.core.commands.EHandlerService; |
24 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
20 | 25 |
import org.eclipse.jface.viewers.TreeNode; |
21 | 26 |
|
27 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
|
28 |
import eu.etaxonomy.cdm.model.common.ICdmBase; |
|
22 | 29 |
import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto; |
23 | 30 |
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache; |
31 |
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer; |
|
24 | 32 |
import eu.etaxonomy.taxeditor.model.MessagingUtils; |
25 | 33 |
|
26 | 34 |
/** |
... | ... | |
39 | 47 |
* @return a key-value map of available commands and their string |
40 | 48 |
* representation |
41 | 49 |
*/ |
42 |
public static Map<Command, String> getAvailableViewers(Object input, ECommandService commandService,
|
|
50 |
public static Map<Command, String> getAvailableViewers(List<UuidAndTitleCache<? extends ICdmBase>> input, ECommandService commandService,
|
|
43 | 51 |
EHandlerService handlerService){ |
52 |
|
|
44 | 53 |
Map<Command, String> commandViewerNameMap = new HashMap<>(); |
45 | 54 |
|
46 |
Class<?> inputClass = null;
|
|
55 |
Set<Class<?>> inputClasses = new HashSet<>();
|
|
47 | 56 |
if(input!=null){ |
48 |
if (input instanceof ReferencingObjectDto){ |
|
49 |
ReferencingObjectDto dto = (ReferencingObjectDto)input; |
|
50 |
inputClass = dto.getOpenInTarget() == null ? dto.getType() : dto.getOpenInTarget().getType(); |
|
51 |
} |
|
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(); |
|
56 |
} |
|
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(); |
|
61 |
} |
|
62 |
if (inputClass == null){ |
|
63 |
inputClass = input.getClass(); |
|
64 |
} |
|
65 |
} |
|
57 |
input.stream() |
|
58 |
.filter(uuid->uuid.getType()!= null) |
|
59 |
.forEach(uuid->inputClasses.add(uuid.getType())); |
|
66 | 60 |
|
67 |
if(inputClass!=null){ |
|
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(inputClass)){ |
|
81 |
Command command = commandService.getCommand(commandId); |
|
82 |
commandViewerNameMap.put(command, viewerName); |
|
61 |
inputClasses.forEach(ic->{ |
|
62 |
IExtensionRegistry reg = Platform.getExtensionRegistry(); |
|
63 |
IConfigurationElement[] extensions = reg |
|
64 |
.getConfigurationElementsFor("eu.etaxonomy.taxeditor.store.cdmViewer"); //$NON-NLS-1$ |
|
65 |
for (IConfigurationElement configElement : extensions) { |
|
66 |
try { |
|
67 |
if(configElement.getName().equals("viewCommandMapping")){ //$NON-NLS-1$ |
|
68 |
String commandId = configElement.getAttribute("commandId"); //$NON-NLS-1$ |
|
69 |
String viewerName = configElement.getAttribute("viewerName"); //$NON-NLS-1$ |
|
70 |
Class<?> selectionClass = Class.forName(configElement.getAttribute("selection")); //$NON-NLS-1$ |
|
71 |
if(selectionClass!=null |
|
72 |
&& commandId!=null |
|
73 |
&& viewerName!=null |
|
74 |
&& selectionClass.isAssignableFrom(ic)){ |
|
75 |
Command command = commandService.getCommand(commandId); |
|
76 |
commandViewerNameMap.put(command, viewerName); |
|
77 |
} |
|
83 | 78 |
} |
79 |
} catch (ClassNotFoundException e) { |
|
80 |
MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$ |
|
84 | 81 |
} |
85 |
} catch (ClassNotFoundException e) { |
|
86 |
MessagingUtils.error(CdmViewerChooser.class, "Could not initalize selection class element of cdmViewer extension", e); //$NON-NLS-1$ |
|
87 | 82 |
} |
88 |
} |
|
83 |
});
|
|
89 | 84 |
} |
85 |
|
|
90 | 86 |
return commandViewerNameMap; |
91 | 87 |
} |
92 | 88 |
|
93 |
} |
|
89 |
|
|
90 |
public static List<UuidAndTitleCache<? extends ICdmBase>> transformSelectionToUuidAndTitleCacheList(Object selectedObject) { |
|
91 |
|
|
92 |
//make it all a list |
|
93 |
List<Object> multipleSelection = new ArrayList<>(); |
|
94 |
if(selectedObject instanceof IStructuredSelection){ |
|
95 |
IStructuredSelection selection = ((IStructuredSelection) selectedObject); |
|
96 |
((List<?>)selection.toList()).forEach(o->multipleSelection.add(o)); |
|
97 |
}else if(selectedObject.getClass().isArray()){ |
|
98 |
for (Object o : ((Object[])selectedObject)){ |
|
99 |
multipleSelection.add(o); |
|
100 |
} |
|
101 |
}else if(selectedObject instanceof List){ |
|
102 |
multipleSelection.addAll((List<?>)selectedObject); |
|
103 |
}else{ |
|
104 |
multipleSelection.add(selectedObject); |
|
105 |
} |
|
106 |
|
|
107 |
//filter all as uuidIdAndTitleCache |
|
108 |
List<UuidAndTitleCache<? extends ICdmBase>> uuidTypes = new ArrayList<>(); |
|
109 |
|
|
110 |
for (Object o : multipleSelection){ |
|
111 |
if (o instanceof FeatureNodeContainer){ |
|
112 |
continue ; |
|
113 |
} |
|
114 |
if(o instanceof TreeNode){ |
|
115 |
o = ((TreeNode)o).getValue(); |
|
116 |
} |
|
117 |
o = CdmBase.deproxy(o); |
|
118 |
|
|
119 |
UuidAndTitleCache<? extends ICdmBase> uuidAndTitleCache; |
|
120 |
if (o instanceof CdmBase){ |
|
121 |
CdmBase cdmBase = (CdmBase)o; |
|
122 |
uuidAndTitleCache = new DtoWithEntity<>(cdmBase); |
|
123 |
}else if (o instanceof ReferencingObjectDto){ |
|
124 |
ReferencingObjectDto dto = (ReferencingObjectDto)o; |
|
125 |
uuidAndTitleCache = dto.getOpenInTarget() != null ? dto.getOpenInTarget() : dto; |
|
126 |
}else if (o instanceof UuidAndTitleCache){ |
|
127 |
uuidAndTitleCache = (UuidAndTitleCache<CdmBase>)o; |
|
128 |
}else{ |
|
129 |
continue; |
|
130 |
} |
|
131 |
uuidTypes.add(uuidAndTitleCache); |
|
132 |
} |
|
133 |
return uuidTypes; |
|
134 |
} |
|
135 |
} |
Also available in: Unified diff
ref #9541 refactor Open..Handlers to all use UuidAndTitleCache as input and some more changes and bugfixes