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.editor;
|
11
|
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Set;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.eclipse.core.commands.ExecutionEvent;
|
17
|
import org.eclipse.core.commands.operations.IOperationHistory;
|
18
|
import org.eclipse.core.commands.operations.IUndoContext;
|
19
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
20
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
21
|
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
|
22
|
import org.eclipse.jface.dialogs.MessageDialog;
|
23
|
import org.eclipse.jface.viewers.ISelection;
|
24
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
25
|
import org.eclipse.jface.viewers.TreeNode;
|
26
|
import org.eclipse.swt.widgets.Shell;
|
27
|
import org.eclipse.ui.IEditorInput;
|
28
|
import org.eclipse.ui.IEditorPart;
|
29
|
import org.eclipse.ui.IEditorReference;
|
30
|
import org.eclipse.ui.IWorkbenchPage;
|
31
|
import org.eclipse.ui.PartInitException;
|
32
|
import org.eclipse.ui.handlers.HandlerUtil;
|
33
|
|
34
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
35
|
import eu.etaxonomy.cdm.model.common.ITreeNode;
|
36
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
37
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
38
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
41
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
42
|
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
|
43
|
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
|
44
|
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
|
45
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
46
|
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
|
47
|
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
|
48
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
49
|
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
|
50
|
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
|
51
|
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
|
52
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
53
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
54
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
55
|
|
56
|
/**
|
57
|
* Utility for the editor package
|
58
|
*
|
59
|
* @author n.hoffmann
|
60
|
* @created 20.01.2009
|
61
|
* @version 1.0
|
62
|
*/
|
63
|
public class EditorUtil extends AbstractUtility {
|
64
|
|
65
|
private static boolean isSaving = false;
|
66
|
|
67
|
/**
|
68
|
* Opens a new editor window with the given input
|
69
|
*
|
70
|
* @param input
|
71
|
* @param editorId
|
72
|
* @return
|
73
|
* @return
|
74
|
* @throws PartInitException
|
75
|
*/
|
76
|
private static IEditorPart open(final IEditorInput input,
|
77
|
final String editorId) throws PartInitException {
|
78
|
IEditorPart editor = getActivePage().openEditor(input, editorId);
|
79
|
if(input != null &&
|
80
|
editor.getEditorInput() != null &&
|
81
|
input != editor.getEditorInput() &&
|
82
|
input instanceof CdmEntitySessionInput) {
|
83
|
((CdmEntitySessionInput)input).dispose();
|
84
|
}
|
85
|
return editor;
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Opens a new editor window with the given TaxonEditorInput
|
90
|
*
|
91
|
* @param input
|
92
|
* a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
|
93
|
* object.
|
94
|
* @throws org.eclipse.ui.PartInitException
|
95
|
* if any.
|
96
|
*/
|
97
|
public static void open(TaxonEditorInput input) throws PartInitException {
|
98
|
open(input, MultiPageTaxonEditor.ID);
|
99
|
}
|
100
|
|
101
|
public static void open(PolytomousKeyEditorInput input)
|
102
|
throws PartInitException {
|
103
|
open(input, KeyEditor.ID);
|
104
|
}
|
105
|
|
106
|
public static void open(CdmAuthorityEditorInput input)
|
107
|
throws PartInitException {
|
108
|
open(input, CdmAuthorityEditor.ID);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Opens a new ChecklistView for the given input
|
113
|
* @param input a {@link ChecklistEditorInput} representing the selected checklist
|
114
|
* @throws PartInitException
|
115
|
*/
|
116
|
public static void open(ChecklistEditorInput input)
|
117
|
throws PartInitException {
|
118
|
open(input, ChecklistEditor.ID);
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Opens a new {@link DataImportEditor} for the given input
|
123
|
* @param input a {@link DataImportEditorInput}
|
124
|
* @throws PartInitException
|
125
|
*/
|
126
|
// public static void open(DataImportEditorInput<?> input)
|
127
|
// throws PartInitException {
|
128
|
// if(input instanceof BioCaseEditorInput){
|
129
|
// open(input, SpecimenImportEditor.ID);
|
130
|
// }
|
131
|
// else if(input instanceof GbifImportEditorInput){
|
132
|
// open(input, GbifImportEditor.ID);
|
133
|
// }
|
134
|
// }
|
135
|
|
136
|
/**
|
137
|
* Taxon Editors may be opened by supplying a taxon node uuid. Session gets
|
138
|
* initialised here and is passed to the editor
|
139
|
*
|
140
|
* @param taxonNodeUuid
|
141
|
* a {@link java.util.UUID} object.
|
142
|
* @throws java.lang.Exception
|
143
|
* if any.
|
144
|
*/
|
145
|
public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
|
146
|
TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
|
147
|
open(input);
|
148
|
}
|
149
|
|
150
|
public static void openTaxonNodeE4(UUID taxonNodeUuid) throws Exception {
|
151
|
TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
|
152
|
EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getService(EPartService.class);
|
153
|
MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
|
154
|
partService.showPart(part, PartState.ACTIVATE);
|
155
|
TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
|
156
|
editor.init(input);
|
157
|
}
|
158
|
|
159
|
public static void openTaxonBase(UUID taxonBaseUuid)
|
160
|
throws PartInitException {
|
161
|
TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
|
162
|
if (taxonBase != null && taxonBase.isOrphaned()) {
|
163
|
if(taxonBase.isInstanceOf(Synonym.class)){
|
164
|
MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInput.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
|
165
|
return;
|
166
|
}
|
167
|
else{
|
168
|
MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInput.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
|
169
|
return;
|
170
|
}
|
171
|
}
|
172
|
TaxonEditorInput input = TaxonEditorInput
|
173
|
.NewInstanceFromTaxonBase(taxonBaseUuid);
|
174
|
if(input!=null && input.getTaxonNode()!=null){
|
175
|
open(input);
|
176
|
}
|
177
|
}
|
178
|
|
179
|
public static void openTaxonBaseE4(UUID taxonBaseUuid) throws PartInitException{
|
180
|
TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
|
181
|
|
182
|
if (taxonBase != null && taxonBase.isOrphaned()) {
|
183
|
if(taxonBase.isInstanceOf(Synonym.class)){
|
184
|
MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
|
185
|
return;
|
186
|
}
|
187
|
else{
|
188
|
MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
|
189
|
return;
|
190
|
}
|
191
|
}
|
192
|
TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
|
193
|
if(input!=null && input.getTaxonNode()!=null){
|
194
|
EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getService(EPartService.class);
|
195
|
MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
|
196
|
partService.showPart(part, PartState.ACTIVATE);
|
197
|
TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
|
198
|
editor.init(input);
|
199
|
}
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* <p>
|
204
|
* findEditorByTaxonNodeUuid
|
205
|
* </p>
|
206
|
*
|
207
|
* @param taxonNodeUuid
|
208
|
* a {@link java.util.UUID} object.
|
209
|
* @return a {@link org.eclipse.ui.IEditorPart} object.
|
210
|
* @throws java.lang.Exception
|
211
|
* if any.
|
212
|
*/
|
213
|
public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
|
214
|
throws Exception {
|
215
|
IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
|
216
|
return getActivePage().findEditor(input);
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* An uninitialized taxon is one that hasn't been saved yet. As such, it
|
221
|
* should appear in neither the list of recent names nor in the taxonomic
|
222
|
* tree when opened.
|
223
|
*
|
224
|
* @throws org.eclipse.ui.PartInitException
|
225
|
* if any.
|
226
|
* @param parentNodeUuid
|
227
|
* a {@link java.util.UUID} object.
|
228
|
*/
|
229
|
public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
|
230
|
TaxonEditorInput input = TaxonEditorInput
|
231
|
.NewEmptyInstance(parentNodeUuid);
|
232
|
open(input, MultiPageTaxonEditor.ID);
|
233
|
|
234
|
getActiveMultiPageTaxonEditor().changed(null);
|
235
|
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* <p>
|
240
|
* setSaving
|
241
|
* </p>
|
242
|
*
|
243
|
* @param isSaving
|
244
|
* a boolean.
|
245
|
*/
|
246
|
public static void setSaving(boolean isSaving) {
|
247
|
EditorUtil.isSaving = isSaving;
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* <p>
|
252
|
* isSaving
|
253
|
* </p>
|
254
|
*
|
255
|
* @return a boolean.
|
256
|
*/
|
257
|
public static boolean isSaving() {
|
258
|
return isSaving;
|
259
|
}
|
260
|
|
261
|
/**
|
262
|
* Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
|
263
|
*
|
264
|
* @return a {@link java.util.Set} object.
|
265
|
*/
|
266
|
public static Set<IEditorPart> getOpenEditors() {
|
267
|
Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
|
268
|
|
269
|
if (getActivePage() != null) {
|
270
|
for (IEditorReference reference : getActivePage()
|
271
|
.getEditorReferences()) {
|
272
|
IEditorPart editor = reference.getEditor(false);
|
273
|
if (editor instanceof MultiPageTaxonEditor) {
|
274
|
taxonEditors.add(editor);
|
275
|
}
|
276
|
}
|
277
|
}
|
278
|
|
279
|
return taxonEditors;
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* Returns the currently active taxon editor
|
284
|
*
|
285
|
* @return the taxon editor that has focus
|
286
|
*/
|
287
|
public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
|
288
|
Object editorPart = getActiveE4Editor();
|
289
|
if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
|
290
|
MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
|
291
|
editor.getConversationHolder().bind();
|
292
|
return editor;
|
293
|
}
|
294
|
return null;
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* <p>
|
299
|
* getActiveEditorPage
|
300
|
* </p>
|
301
|
*
|
302
|
* @param page
|
303
|
* a {@link eu.etaxonomy.taxeditor.editor.Page} object.
|
304
|
* @return a {@link org.eclipse.ui.IEditorPart} object.
|
305
|
*/
|
306
|
public static IEditorPart getActiveEditorPage(Page page) {
|
307
|
MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
|
308
|
|
309
|
return editor != null ? editor.getPage(page) : null;
|
310
|
}
|
311
|
|
312
|
/**
|
313
|
* Returns the selection of the currently active taxon editor
|
314
|
*
|
315
|
* @return a {@link org.eclipse.jface.viewers.ISelection} object.
|
316
|
*/
|
317
|
public static ISelection getCurrentSelection() {
|
318
|
if (getActiveMultiPageTaxonEditor() == null) {
|
319
|
return null;
|
320
|
} else {
|
321
|
return getActiveMultiPageTaxonEditor().getSite()
|
322
|
.getSelectionProvider().getSelection();
|
323
|
}
|
324
|
}
|
325
|
|
326
|
/**
|
327
|
* <p>
|
328
|
* getUndoContext
|
329
|
* </p>
|
330
|
*
|
331
|
* @param editor
|
332
|
* a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
|
333
|
* object.
|
334
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext}
|
335
|
* object.
|
336
|
*/
|
337
|
public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
|
338
|
return editor.getUndoContext();
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* <p>
|
343
|
* getUndoContext
|
344
|
* </p>
|
345
|
*
|
346
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext}
|
347
|
* object.
|
348
|
*/
|
349
|
public static IUndoContext getUndoContext() {
|
350
|
return IOperationHistory.GLOBAL_UNDO_CONTEXT;
|
351
|
}
|
352
|
|
353
|
/**
|
354
|
* <p>
|
355
|
* forceUserSave
|
356
|
* </p>
|
357
|
*
|
358
|
* @param editor
|
359
|
* a {@link org.eclipse.ui.IEditorPart} object.
|
360
|
* @param shell
|
361
|
* a {@link org.eclipse.swt.widgets.Shell} object.
|
362
|
* @return a boolean.
|
363
|
*/
|
364
|
public static boolean forceUserSave(IEditorPart editor, Shell shell) {
|
365
|
if (editor.isDirty()) {
|
366
|
|
367
|
boolean doSave = MessageDialog
|
368
|
.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
|
369
|
Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
|
370
|
|
371
|
if (!doSave) {
|
372
|
return false;
|
373
|
}
|
374
|
|
375
|
editor.doSave(AbstractUtility.getMonitor());
|
376
|
}
|
377
|
return true;
|
378
|
}
|
379
|
|
380
|
public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
|
381
|
if (editor.isDirty()) {
|
382
|
|
383
|
boolean doSave = MessageDialog
|
384
|
.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
|
385
|
Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
|
386
|
|
387
|
if (!doSave) {
|
388
|
return false;
|
389
|
}
|
390
|
|
391
|
editor.doSave(AbstractUtility.getMonitor());
|
392
|
}
|
393
|
return true;
|
394
|
}
|
395
|
|
396
|
/**
|
397
|
* <p>
|
398
|
* getSelection
|
399
|
* </p>
|
400
|
*
|
401
|
* @param event
|
402
|
* a {@link org.eclipse.core.commands.ExecutionEvent} object.
|
403
|
* @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
|
404
|
*/
|
405
|
public static IStructuredSelection getSelection(ExecutionEvent event) {
|
406
|
IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
|
407
|
|
408
|
return (IStructuredSelection) activeEditor.getSite()
|
409
|
.getSelectionProvider().getSelection();
|
410
|
}
|
411
|
|
412
|
/**
|
413
|
* <p>
|
414
|
* getPluginId
|
415
|
* </p>
|
416
|
*
|
417
|
* @return a {@link java.lang.String} object.
|
418
|
*/
|
419
|
public static String getPluginId() {
|
420
|
return TaxeditorEditorPlugin.PLUGIN_ID;
|
421
|
}
|
422
|
|
423
|
public static void openPolytomousKey(UUID polytomousKeyUuid)
|
424
|
throws Exception {
|
425
|
PolytomousKeyEditorInput input = PolytomousKeyEditorInput
|
426
|
.NewInstance(polytomousKeyUuid);
|
427
|
open(input);
|
428
|
}
|
429
|
|
430
|
// public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
|
431
|
// throws Exception {
|
432
|
// PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
|
433
|
// open(input);
|
434
|
// }
|
435
|
|
436
|
public static void openCdmAuthorities(UUID groupUuid)
|
437
|
throws Exception {
|
438
|
CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
|
439
|
open(input);
|
440
|
}
|
441
|
|
442
|
/**
|
443
|
* Iterates recursively over all originals having the given specimen as a derivate.
|
444
|
* The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
|
445
|
* @param specimen the start element for which the originals are iterated recursively
|
446
|
* @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
|
447
|
*/
|
448
|
public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
|
449
|
if(specimen.isInstanceOf(FieldUnit.class)){
|
450
|
return specimen;
|
451
|
}
|
452
|
else if(specimen instanceof DerivedUnit
|
453
|
&& ((DerivedUnit) specimen).getOriginals()!=null
|
454
|
&& !((DerivedUnit) specimen).getOriginals().isEmpty()){
|
455
|
for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
|
456
|
return getTopMostDerivate(original);
|
457
|
}
|
458
|
//needed to add this for compilation although this is unreachable
|
459
|
return specimen;
|
460
|
}
|
461
|
else{
|
462
|
return specimen;
|
463
|
}
|
464
|
}
|
465
|
|
466
|
/**
|
467
|
* If the current selection is a single {@link TreeNode} it will be returned.
|
468
|
* @param selection the selection to check
|
469
|
* @return the selected TreeNode or <code>null</code> if no TreeNode selected
|
470
|
*/
|
471
|
public static TreeNode getTreeNodeOfSelection(ISelection selection){
|
472
|
if(selection instanceof IStructuredSelection
|
473
|
&& ((IStructuredSelection) selection).size()==1
|
474
|
&& ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
|
475
|
return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
|
476
|
|
477
|
}
|
478
|
return null;
|
479
|
}
|
480
|
|
481
|
/**
|
482
|
* Opens a taxon editor for the given object if the given object is a valid input.
|
483
|
* @param object the object for which the editor will be opened
|
484
|
* @throws PartInitException
|
485
|
*/
|
486
|
public static void openTaxonEditor(Object object) throws PartInitException {
|
487
|
if(object instanceof TaxonBase<?>){
|
488
|
openTaxonBase(((TaxonBase<?>) object).getUuid());
|
489
|
}
|
490
|
}
|
491
|
|
492
|
public static boolean closeObsoleteEditor(TaxonNode taxonNode, IWorkbenchPage activePage){
|
493
|
boolean result = true;
|
494
|
for (IEditorReference ref : activePage.getEditorReferences()) {
|
495
|
try {
|
496
|
String treeIndex = ((ITreeNode)taxonNode).treeIndex();
|
497
|
|
498
|
|
499
|
IEditorInput input = ref.getEditorInput();
|
500
|
if (input instanceof TaxonEditorInput) {
|
501
|
TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
|
502
|
//if node is a child of taxonNode then close the editor
|
503
|
if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
|
504
|
//if (taxonNode.equals(node)) {
|
505
|
result &= activePage.closeEditor(ref.getEditor(false), true);
|
506
|
|
507
|
}
|
508
|
}
|
509
|
} catch (PartInitException e) {
|
510
|
continue;
|
511
|
}
|
512
|
}
|
513
|
return result;
|
514
|
}
|
515
|
}
|