1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.taxeditor.navigation;
|
11
|
|
12
|
import java.util.Arrays;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.eclipse.core.commands.ExecutionException;
|
18
|
import org.eclipse.core.commands.NotEnabledException;
|
19
|
import org.eclipse.core.commands.NotHandledException;
|
20
|
import org.eclipse.core.commands.common.NotDefinedException;
|
21
|
import org.eclipse.core.commands.operations.IUndoContext;
|
22
|
import org.eclipse.core.commands.operations.UndoContext;
|
23
|
import org.eclipse.jface.viewers.StructuredSelection;
|
24
|
import org.eclipse.swt.widgets.Display;
|
25
|
import org.eclipse.swt.widgets.Shell;
|
26
|
import org.eclipse.ui.IEditorPart;
|
27
|
import org.eclipse.ui.IEditorReference;
|
28
|
import org.eclipse.ui.IWorkbenchWindow;
|
29
|
import org.eclipse.ui.PartInitException;
|
30
|
import org.eclipse.ui.handlers.IHandlerService;
|
31
|
import org.eclipse.ui.navigator.CommonViewer;
|
32
|
|
33
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
34
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
35
|
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
|
36
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
37
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
41
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
42
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
43
|
import eu.etaxonomy.taxeditor.editor.OpenEditorConfiguration;
|
44
|
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
|
45
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
46
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
47
|
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
|
48
|
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
|
49
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
50
|
|
51
|
/**
|
52
|
* <p>NavigationUtil class.</p>
|
53
|
*
|
54
|
* @author n.hoffmann
|
55
|
* @created 24.03.2009
|
56
|
* @version 1.0
|
57
|
*/
|
58
|
public class NavigationUtil extends AbstractUtility{
|
59
|
private static IUndoContext defaultUndoContext;
|
60
|
|
61
|
/**
|
62
|
* <p>executeEditHandler</p>
|
63
|
*/
|
64
|
public static void executeEditHandler(){
|
65
|
|
66
|
String commandId = "eu.etaxonomy.taxeditor.navigation.command.editSelection";
|
67
|
|
68
|
IHandlerService handlerService = (IHandlerService) NavigationUtil.getService(IHandlerService.class);
|
69
|
try {
|
70
|
handlerService.executeCommand(commandId, null);
|
71
|
} catch (ExecutionException e) {
|
72
|
NavigationUtil.error(NavigationUtil.class, e);
|
73
|
} catch (NotDefinedException e) {
|
74
|
NavigationUtil.error(NavigationUtil.class, e);
|
75
|
} catch (NotEnabledException e) {
|
76
|
NavigationUtil.error(NavigationUtil.class, e);
|
77
|
} catch (NotHandledException e) {
|
78
|
NavigationUtil.error(NavigationUtil.class, e);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* <p>openEditor</p>
|
84
|
*
|
85
|
* @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
|
86
|
*/
|
87
|
public static void openEditor(ICdmBase selectedObject){
|
88
|
UUID entityUuid = selectedObject.getUuid();
|
89
|
try {
|
90
|
if(selectedObject instanceof TaxonNode){
|
91
|
EditorUtil.openTaxonNode(entityUuid);
|
92
|
}else if(selectedObject instanceof TaxonBase){
|
93
|
EditorUtil.openTaxonBase(entityUuid);
|
94
|
}else if(selectedObject instanceof TaxonNameBase){
|
95
|
// TODO open bulk editor
|
96
|
warningDialog("Not implemented yet", NavigationUtil.class, "You tried to open a name. This is not handled by the software yet.");
|
97
|
}else if(selectedObject instanceof PolytomousKey){
|
98
|
EditorUtil.openPolytomousKey(entityUuid);
|
99
|
}else{
|
100
|
warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
|
101
|
}
|
102
|
} catch (PartInitException e) {
|
103
|
NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
|
104
|
} catch (Exception e) {
|
105
|
EditorUtil.warningDialog("Could not create Taxon", NavigationUtil.class, e.getMessage());
|
106
|
}
|
107
|
}
|
108
|
|
109
|
public static void openEditor(Object selectedObject){
|
110
|
if (selectedObject instanceof UuidAndTitleCache){
|
111
|
Class type = ((UuidAndTitleCache) selectedObject).getType();
|
112
|
if(type == Taxon.class || type == Synonym.class){
|
113
|
try {
|
114
|
EditorUtil.openTaxonBase(((UuidAndTitleCache) selectedObject).getUuid());
|
115
|
} catch (PartInitException e) {
|
116
|
NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
|
117
|
}
|
118
|
}
|
119
|
}else if(selectedObject instanceof ICdmBase){
|
120
|
openEditor((ICdmBase) selectedObject);
|
121
|
}else{
|
122
|
NavigationUtil.error(NavigationUtil.class, new IllegalArgumentException("Selected object is not supported: " + selectedObject));
|
123
|
}
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* <p>openEmpty</p>
|
128
|
*
|
129
|
* @param parentNodeUuid a {@link java.util.UUID} object.
|
130
|
*/
|
131
|
public static void openEmpty(UUID parentNodeUuid) {
|
132
|
try {
|
133
|
EditorUtil.openEmpty(parentNodeUuid);
|
134
|
} catch (PartInitException e) {
|
135
|
NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
|
136
|
}
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* <p>getShell</p>
|
141
|
*
|
142
|
* @return a {@link org.eclipse.swt.widgets.Shell} object.
|
143
|
*/
|
144
|
public static Shell getShell() {
|
145
|
return getActiveWindow().getShell();
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* <p>getActiveWindow</p>
|
150
|
*
|
151
|
* @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
|
152
|
*/
|
153
|
public static IWorkbenchWindow getActiveWindow() {
|
154
|
return TaxeditorNavigationPlugin.getDefault().getWorkbench().
|
155
|
getActiveWorkbenchWindow();
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* <p>getWorkbenchUndoContext</p>
|
160
|
*
|
161
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
|
162
|
*/
|
163
|
public static IUndoContext getWorkbenchUndoContext() {
|
164
|
return TaxeditorEditorPlugin.getDefault().getWorkbench().
|
165
|
getOperationSupport().getUndoContext();
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* <p>getUndoContext</p>
|
170
|
*
|
171
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
|
172
|
*/
|
173
|
public static IUndoContext getUndoContext() {
|
174
|
// FIXME this has to be more specific. Every widget has to have its own undo context
|
175
|
// return IOperationHistory.GLOBAL_UNDO_CONTEXT;
|
176
|
|
177
|
// Plug-ins that wish their operations to be undoable from workbench views
|
178
|
// such as the Navigator or Package Explorer should assign the workbench
|
179
|
// undo context to their operations.
|
180
|
if (defaultUndoContext == null) {
|
181
|
defaultUndoContext = new UndoContext();
|
182
|
}
|
183
|
return defaultUndoContext;
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* Whether a taxonNode has unsaved changes.
|
188
|
*
|
189
|
* @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
|
190
|
* @return a boolean.
|
191
|
*/
|
192
|
public static boolean isDirty(TaxonNode taxonNode){
|
193
|
|
194
|
for (IEditorReference reference : getActivePage().getEditorReferences()) {
|
195
|
|
196
|
try {
|
197
|
if (reference.getEditorInput() instanceof TaxonEditorInput) {
|
198
|
TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
|
199
|
if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
|
200
|
return true;
|
201
|
}
|
202
|
}
|
203
|
} catch (PartInitException e) {
|
204
|
NavigationUtil.error(NavigationUtil.class, e.getMessage(), e);
|
205
|
throw new RuntimeException(e);
|
206
|
}
|
207
|
|
208
|
}
|
209
|
return false;
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* <p>selectInNavigator</p>
|
214
|
*
|
215
|
* @param element a {@link java.lang.Object} object.
|
216
|
* @param parentElement a {@link java.lang.Object} object.
|
217
|
*/
|
218
|
public static void selectInNavigator(final Object element, final Object parentElement) {
|
219
|
Display.getDefault().asyncExec(new Runnable(){
|
220
|
|
221
|
public void run() {
|
222
|
TaxonNavigator navigator = showNavigator();
|
223
|
|
224
|
if (navigator != null) {
|
225
|
CommonViewer viewer = navigator.getCommonViewer();
|
226
|
if (viewer != null) {
|
227
|
if (parentElement != null) {
|
228
|
viewer.setExpandedState(parentElement, true);
|
229
|
}
|
230
|
viewer.setSelection(new StructuredSelection((TaxonNode) element));
|
231
|
}
|
232
|
}
|
233
|
}
|
234
|
|
235
|
});
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* <p>openSearch</p>
|
240
|
*
|
241
|
* @param selection a {@link java.lang.Object} object.
|
242
|
*/
|
243
|
public static void openSearch(Object selection) {
|
244
|
if(selection instanceof Taxon){
|
245
|
Taxon taxon = (Taxon) selection;
|
246
|
|
247
|
handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
|
248
|
|
249
|
}else if(selection instanceof Synonym){
|
250
|
Synonym synonym = (Synonym) selection;
|
251
|
|
252
|
handleOpeningOfMultipleTaxa(synonym.getAcceptedTaxa());
|
253
|
|
254
|
}else{
|
255
|
warningDialog("Not implemented yet", NavigationUtil.class, "You chose to open a name that has no connection to a taxon. The Editor does not support editing of such a content type at the moment.");
|
256
|
}
|
257
|
|
258
|
}
|
259
|
|
260
|
private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
|
261
|
if(acceptedTaxa.size() == 1){
|
262
|
openEditor(acceptedTaxa.iterator().next());
|
263
|
}else if(acceptedTaxa.size() > 1){
|
264
|
// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
|
265
|
warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
|
266
|
" This case is not handled yet by the software.");
|
267
|
}else if(acceptedTaxa.size() == 0){
|
268
|
// this is an undesired state
|
269
|
warningDialog("Not implemented yet", NavigationUtil.class, "This taxon is not connected to a classification. Currently editing of such taxa is not supported yet.");
|
270
|
}
|
271
|
}
|
272
|
|
273
|
/**
|
274
|
* @param taxonNodes
|
275
|
*/
|
276
|
private static void handleOpeningOfMultipleTaxonNodes(
|
277
|
Set<TaxonNode> taxonNodes) {
|
278
|
|
279
|
if(taxonNodes.size() == 1){
|
280
|
openEditor(taxonNodes.iterator().next());
|
281
|
}else if(taxonNodes.size() > 1){
|
282
|
// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
|
283
|
warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
|
284
|
" This case is not handled yet by the software.");
|
285
|
}else if(taxonNodes.size() == 0){
|
286
|
// this is an undesired state
|
287
|
warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
|
288
|
}
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* <p>showNavigator</p>
|
293
|
*
|
294
|
* @return the TaxonNavigator instance if present
|
295
|
*/
|
296
|
public static TaxonNavigator showNavigator() {
|
297
|
return (TaxonNavigator) showView(TaxonNavigator.ID);
|
298
|
}
|
299
|
|
300
|
/**
|
301
|
* <p>getNavigator</p>
|
302
|
*
|
303
|
* @param restore a boolean.
|
304
|
* @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
|
305
|
*/
|
306
|
public static TaxonNavigator getNavigator(boolean restore) {
|
307
|
return (TaxonNavigator) getView(TaxonNavigator.ID, restore);
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* <p>getOpenEditors</p>
|
312
|
*
|
313
|
* @return a {@link java.util.Set} object.
|
314
|
*/
|
315
|
public static Set<IEditorPart> getOpenEditors() {
|
316
|
return EditorUtil.getOpenEditors();
|
317
|
}
|
318
|
|
319
|
/**
|
320
|
* <p>getPluginId</p>
|
321
|
*
|
322
|
* @return a {@link java.lang.String} object.
|
323
|
*/
|
324
|
protected static String getPluginId(){
|
325
|
return TaxeditorNavigationPlugin.PLUGIN_ID;
|
326
|
}
|
327
|
|
328
|
public static TaxonNode getTaxonNode(Object element){
|
329
|
if (element instanceof TaxonNode){
|
330
|
List<String> propertyPaths = Arrays.asList(new String[]{
|
331
|
"taxon.sec",
|
332
|
"taxon.name",
|
333
|
"childNodes"
|
334
|
});
|
335
|
TaxonNode taxonNode = CdmStore.getService(ITaxonNodeService.class).load(((ICdmBase) element).getUuid(), propertyPaths);
|
336
|
return taxonNode;
|
337
|
}
|
338
|
return null;
|
339
|
}
|
340
|
}
|