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.HashMap;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.Map;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.eclipse.core.commands.Command;
|
19
|
import org.eclipse.core.commands.ParameterizedCommand;
|
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.wizard.WizardDialog;
|
24
|
import org.eclipse.swt.widgets.Shell;
|
25
|
import org.eclipse.ui.IEditorReference;
|
26
|
import org.eclipse.ui.IWorkbenchWindow;
|
27
|
import org.eclipse.ui.PartInitException;
|
28
|
import org.eclipse.ui.PlatformUI;
|
29
|
import org.eclipse.ui.commands.ICommandService;
|
30
|
import org.eclipse.ui.handlers.IHandlerService;
|
31
|
|
32
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
33
|
import eu.etaxonomy.cdm.api.service.INameService;
|
34
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
35
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
36
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
37
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
40
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
41
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
42
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
43
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
44
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
45
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
46
|
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
|
47
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
48
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
49
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
50
|
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
|
51
|
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
|
52
|
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TaxonNavigatorE4;
|
53
|
import eu.etaxonomy.taxeditor.newWizard.NewClassificationWizard;
|
54
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
55
|
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
|
56
|
|
57
|
/**
|
58
|
* <p>NavigationUtil class.</p>
|
59
|
*
|
60
|
* @author n.hoffmann
|
61
|
* @created 24.03.2009
|
62
|
* @version 1.0
|
63
|
*/
|
64
|
public class NavigationUtil extends AbstractUtility{
|
65
|
|
66
|
private static final String NOT_IMPLEMENTED_YET = Messages.NavigationUtil_NOT_IMPLEMENTED;
|
67
|
private static final String ERROR_OPENING_THE_EDITOR = Messages.NavigationUtil_OPEN_ERROR;
|
68
|
private static IUndoContext defaultUndoContext;
|
69
|
|
70
|
/**
|
71
|
* <p>openEditor</p>
|
72
|
*
|
73
|
* @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
|
74
|
*/
|
75
|
public static void openEditor(UuidAndTitleCache uuidAndTitleCache){
|
76
|
Class<?> type = uuidAndTitleCache.getType();
|
77
|
ICdmBase cdmBase = null;
|
78
|
if(type.equals(Classification.class)){
|
79
|
cdmBase = CdmStore.getService(IClassificationService.class).load(uuidAndTitleCache.getUuid());
|
80
|
}
|
81
|
else if(type.equals(TaxonNode.class)){
|
82
|
cdmBase = CdmStore.getService(ITaxonNodeService.class).load(uuidAndTitleCache.getUuid());
|
83
|
}
|
84
|
else if(TaxonBase.class.isAssignableFrom(type)){
|
85
|
cdmBase = CdmStore.getService(ITaxonService.class).load(uuidAndTitleCache.getUuid());
|
86
|
}
|
87
|
else if(type.equals(TaxonName.class)){
|
88
|
cdmBase = CdmStore.getService(INameService.class).load(uuidAndTitleCache.getUuid());
|
89
|
}
|
90
|
else{
|
91
|
MessagingUtils.warningDialog(Messages.NavigationUtil_UNKNOWN_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNKNOWN_TYPE_MESSAGE);
|
92
|
}
|
93
|
if(cdmBase!=null){
|
94
|
openEditor(cdmBase, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
|
95
|
}
|
96
|
else{
|
97
|
MessagingUtils.warningDialog(Messages.NavigationUtil_NOT_FOUND, NavigationUtil.class, Messages.NavigationUtil_NOT_FOUND_MESSAGE);
|
98
|
}
|
99
|
}
|
100
|
|
101
|
public static void openEditor(ICdmBase selectedObject, Shell shell){
|
102
|
UUID entityUuid = selectedObject.getUuid();
|
103
|
try {
|
104
|
if(selectedObject instanceof TaxonNode){
|
105
|
TaxonNode taxonNode = (TaxonNode)selectedObject;
|
106
|
Classification classification = taxonNode.getClassification();
|
107
|
if(classification!=null && classification.getRootNode().equals(taxonNode)){
|
108
|
NewClassificationWizard classificationWizard = new NewClassificationWizard();
|
109
|
classificationWizard.init(null, null);
|
110
|
classificationWizard.setEntity(classification);
|
111
|
WizardDialog dialog = new WizardDialog(shell, classificationWizard);
|
112
|
dialog.open();
|
113
|
}
|
114
|
else{
|
115
|
EditorUtil.openTaxonNodeE4(entityUuid);
|
116
|
}
|
117
|
}else if(selectedObject instanceof TaxonBase){
|
118
|
TaxonBase taxonBase = (TaxonBase)selectedObject;
|
119
|
if(taxonBase.isOrphaned()){
|
120
|
openInBulkEditor(taxonBase);
|
121
|
}
|
122
|
else{
|
123
|
EditorUtil.openTaxonBaseE4(entityUuid);
|
124
|
}
|
125
|
}else if(selectedObject instanceof TaxonName){
|
126
|
openInBulkEditor(selectedObject);
|
127
|
}else if(selectedObject instanceof PolytomousKey){
|
128
|
EditorUtil.openPolytomousKey(entityUuid);
|
129
|
}else{
|
130
|
MessagingUtils.warningDialog(Messages.NavigationUtil_UNSUPPORTED_TYPE, NavigationUtil.class, Messages.NavigationUtil_UNSUPPORTED_TYPE_MESSAGE + selectedObject);
|
131
|
}
|
132
|
} catch (PartInitException e) {
|
133
|
MessagingUtils.error(NavigationUtil.class, ERROR_OPENING_THE_EDITOR, e);
|
134
|
} catch (Exception e) {
|
135
|
MessagingUtils.errorDialog(Messages.NavigationUtil_CREATE_FAILED,
|
136
|
NavigationUtil.class,
|
137
|
e.getMessage(), TaxeditorStorePlugin.PLUGIN_ID,
|
138
|
e,
|
139
|
true);
|
140
|
|
141
|
}
|
142
|
}
|
143
|
|
144
|
private static void openInBulkEditor(ICdmBase selectedObject) {
|
145
|
ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
|
146
|
IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
|
147
|
String openInBulkEditorCommand = "eu.etaxonomy.taxeditor.bulkeditor.openBulkEditorForIdentifiableEntity"; //$NON-NLS-1$
|
148
|
Command command = commandService.getCommand(openInBulkEditorCommand);
|
149
|
if(command.isDefined()){
|
150
|
Map<String, UUID> params = new HashMap<String, UUID>();
|
151
|
params.put(openInBulkEditorCommand+".uuid", selectedObject.getUuid()); //$NON-NLS-1$
|
152
|
ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
|
153
|
try {
|
154
|
if(parameterizedCommand!=null){
|
155
|
handlerService.executeCommand(parameterizedCommand, null);
|
156
|
return;
|
157
|
}
|
158
|
else{
|
159
|
handlerService.executeCommand(command.getId(), null);
|
160
|
return;
|
161
|
}
|
162
|
} catch (NotDefinedException nde) {
|
163
|
throw new RuntimeException("Could not find open command: " + command.getId()); //$NON-NLS-1$
|
164
|
} catch (Exception exception) {
|
165
|
MessagingUtils.error(NavigationUtil.class, "An exception occured while trying to execute "+command.getId(), exception); //$NON-NLS-1$
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* <p>openEmpty</p>
|
172
|
*
|
173
|
* @param parentNodeUuid a {@link java.util.UUID} object.
|
174
|
*/
|
175
|
public static void openEmpty(UUID parentNodeUuid) {
|
176
|
try {
|
177
|
EditorUtil.openEmptyE4(parentNodeUuid);
|
178
|
} catch (PartInitException e) {
|
179
|
MessagingUtils.error(NavigationUtil.class, ERROR_OPENING_THE_EDITOR, e);
|
180
|
}
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* <p>getShell</p>
|
185
|
*
|
186
|
* @return a {@link org.eclipse.swt.widgets.Shell} object.
|
187
|
*/
|
188
|
public static Shell getShell() {
|
189
|
return getActiveWindow().getShell();
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* <p>getActiveWindow</p>
|
194
|
*
|
195
|
* @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
|
196
|
*/
|
197
|
public static IWorkbenchWindow getActiveWindow() {
|
198
|
return TaxeditorNavigationPlugin.getDefault().getWorkbench().
|
199
|
getActiveWorkbenchWindow();
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* <p>getWorkbenchUndoContext</p>
|
204
|
*
|
205
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
|
206
|
*/
|
207
|
public static IUndoContext getWorkbenchUndoContext() {
|
208
|
return TaxeditorEditorPlugin.getDefault().getWorkbench().
|
209
|
getOperationSupport().getUndoContext();
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* <p>getUndoContext</p>
|
214
|
*
|
215
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
|
216
|
*/
|
217
|
public static IUndoContext getUndoContext() {
|
218
|
// FIXME this has to be more specific. Every widget has to have its own undo context
|
219
|
// return IOperationHistory.GLOBAL_UNDO_CONTEXT;
|
220
|
|
221
|
// Plug-ins that wish their operations to be undoable from workbench views
|
222
|
// such as the Navigator or Package Explorer should assign the workbench
|
223
|
// undo context to their operations.
|
224
|
if (defaultUndoContext == null) {
|
225
|
defaultUndoContext = new UndoContext();
|
226
|
}
|
227
|
return defaultUndoContext;
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Whether a taxonNode has unsaved changes.
|
232
|
*
|
233
|
* @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
|
234
|
* @return a boolean.
|
235
|
*/
|
236
|
public static boolean isDirty(TaxonNode taxonNode){
|
237
|
|
238
|
for (IEditorReference reference : getActivePage().getEditorReferences()) {
|
239
|
|
240
|
try {
|
241
|
if (reference.getEditorInput() instanceof TaxonEditorInput) {
|
242
|
TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
|
243
|
if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
|
244
|
return true;
|
245
|
}
|
246
|
}
|
247
|
} catch (PartInitException e) {
|
248
|
MessagingUtils.error(NavigationUtil.class, e.getMessage(), e);
|
249
|
throw new RuntimeException(e);
|
250
|
}
|
251
|
|
252
|
}
|
253
|
return false;
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* <p>openSearch</p>
|
258
|
*
|
259
|
* @param selection a {@link java.lang.Object} object.
|
260
|
*/
|
261
|
public static void openSearch(Object selection) {
|
262
|
if(selection instanceof Taxon){
|
263
|
Taxon taxon = (Taxon) selection;
|
264
|
|
265
|
handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
|
266
|
|
267
|
}else if(selection instanceof Synonym){
|
268
|
Synonym synonym = (Synonym) selection;
|
269
|
|
270
|
Set<Taxon> accTaxa = new HashSet<Taxon>();
|
271
|
if (synonym.getAcceptedTaxon() != null){
|
272
|
accTaxa.add(synonym.getAcceptedTaxon());
|
273
|
}
|
274
|
handleOpeningOfMultipleTaxa(accTaxa);
|
275
|
|
276
|
}else{
|
277
|
MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_NAME_MESSAGE);
|
278
|
}
|
279
|
|
280
|
}
|
281
|
|
282
|
private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
|
283
|
if(acceptedTaxa.size() == 1){
|
284
|
openEditor(acceptedTaxa.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
|
285
|
}else if(acceptedTaxa.size() > 1){
|
286
|
// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
|
287
|
MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_MULTI_TREE);
|
288
|
}else if(acceptedTaxa.size() == 0){
|
289
|
// this is an undesired state
|
290
|
MessagingUtils.warningDialog(NOT_IMPLEMENTED_YET, NavigationUtil.class, Messages.NavigationUtil_ORPHAN_TAXON);
|
291
|
}
|
292
|
}
|
293
|
|
294
|
/**
|
295
|
* @param taxonNodes
|
296
|
*/
|
297
|
private static void handleOpeningOfMultipleTaxonNodes(
|
298
|
Set<TaxonNode> taxonNodes) {
|
299
|
|
300
|
if(taxonNodes.size() == 1){
|
301
|
openEditor(taxonNodes.iterator().next(), PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
|
302
|
}else if(taxonNodes.size() > 1){
|
303
|
// FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
|
304
|
MessagingUtils.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. This case is not handled yet by the software.");
|
305
|
}else if(taxonNodes.size() == 0){
|
306
|
// this is an undesired state
|
307
|
MessagingUtils.warningDialog(Messages.NavigationUtil_INCORRECT_STATE, NavigationUtil.class, Messages.NavigationUtil_INCORRECT_STATE_MESSAGE);
|
308
|
}
|
309
|
}
|
310
|
|
311
|
public static TaxonNavigatorE4 getNavigator(boolean restore) {
|
312
|
//FIXME E4 migrate or delete
|
313
|
return null;
|
314
|
// return (TaxonNavigatorE4) getView(TaxonNavigator.ID, restore);
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* <p>getPluginId</p>
|
319
|
*
|
320
|
* @return a {@link java.lang.String} object.
|
321
|
*/
|
322
|
public static String getPluginId(){
|
323
|
return TaxeditorNavigationPlugin.PLUGIN_ID;
|
324
|
}
|
325
|
|
326
|
}
|