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.jface.dialogs.MessageDialog;
|
20
|
import org.eclipse.jface.viewers.ISelection;
|
21
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
22
|
import org.eclipse.jface.viewers.TreeNode;
|
23
|
import org.eclipse.swt.widgets.Shell;
|
24
|
import org.eclipse.ui.IEditorInput;
|
25
|
import org.eclipse.ui.IEditorPart;
|
26
|
import org.eclipse.ui.IEditorReference;
|
27
|
import org.eclipse.ui.PartInitException;
|
28
|
import org.eclipse.ui.handlers.HandlerUtil;
|
29
|
|
30
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
31
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
32
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
33
|
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
|
34
|
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
|
35
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
36
|
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
|
37
|
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
|
38
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
|
39
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
|
40
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
41
|
|
42
|
/**
|
43
|
* Utility for the editor package
|
44
|
*
|
45
|
* @author n.hoffmann
|
46
|
* @created 20.01.2009
|
47
|
* @version 1.0
|
48
|
*/
|
49
|
public class EditorUtil extends AbstractUtility {
|
50
|
|
51
|
private static boolean isSaving = false;
|
52
|
|
53
|
/**
|
54
|
* Opens a new editor window with the given input
|
55
|
*
|
56
|
* @param input
|
57
|
* @param editorId
|
58
|
* @return
|
59
|
* @return
|
60
|
* @throws PartInitException
|
61
|
*/
|
62
|
private static IEditorPart open(final IEditorInput input,
|
63
|
final String editorId) throws PartInitException {
|
64
|
return getActivePage().openEditor(input, editorId);
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Opens a new editor window with the given TaxonEditorInput
|
69
|
*
|
70
|
* @param input
|
71
|
* a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
|
72
|
* object.
|
73
|
* @throws org.eclipse.ui.PartInitException
|
74
|
* if any.
|
75
|
*/
|
76
|
public static void open(TaxonEditorInput input) throws PartInitException {
|
77
|
open(input, MultiPageTaxonEditor.ID);
|
78
|
}
|
79
|
|
80
|
public static void open(PolytomousKeyEditorInput input)
|
81
|
throws PartInitException {
|
82
|
open(input, KeyEditor.ID);
|
83
|
}
|
84
|
|
85
|
public static void open(CdmAuthorityEditorInput input)
|
86
|
throws PartInitException {
|
87
|
open(input, CdmAuthorityEditor.ID);
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Opens a new DerivateView for the given input
|
92
|
* @param input a {@link DerivateViewEditorInput} representing the selected derivate
|
93
|
* @throws PartInitException
|
94
|
*/
|
95
|
public static void open(DerivateViewEditorInput input)
|
96
|
throws PartInitException {
|
97
|
open(input, DerivateView.ID);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Taxon Editors may be opened by supplying a taxon node uuid. Session gets
|
102
|
* initialised here and is passed to the editor
|
103
|
*
|
104
|
* @param taxonNodeUuid
|
105
|
* a {@link java.util.UUID} object.
|
106
|
* @throws java.lang.Exception
|
107
|
* if any.
|
108
|
*/
|
109
|
public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
|
110
|
TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
|
111
|
open(input);
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* <p>
|
116
|
* openTaxonBase
|
117
|
* </p>
|
118
|
*
|
119
|
* @param taxonBaseUuid
|
120
|
* a {@link java.util.UUID} object.
|
121
|
* @throws org.eclipse.ui.PartInitException
|
122
|
* if any.
|
123
|
*/
|
124
|
public static void openTaxonBase(UUID taxonBaseUuid)
|
125
|
throws PartInitException {
|
126
|
TaxonEditorInput input = TaxonEditorInput
|
127
|
.NewInstanceFromTaxonBase(taxonBaseUuid);
|
128
|
open(input);
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* <p>
|
133
|
* findEditorByTaxonNodeUuid
|
134
|
* </p>
|
135
|
*
|
136
|
* @param taxonNodeUuid
|
137
|
* a {@link java.util.UUID} object.
|
138
|
* @return a {@link org.eclipse.ui.IEditorPart} object.
|
139
|
* @throws java.lang.Exception
|
140
|
* if any.
|
141
|
*/
|
142
|
public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
|
143
|
throws Exception {
|
144
|
IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
|
145
|
return getActivePage().findEditor(input);
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* An uninitialized taxon is one that hasn't been saved yet. As such, it
|
150
|
* should appear in neither the list of recent names nor in the taxonomic
|
151
|
* tree when opened.
|
152
|
*
|
153
|
* @throws org.eclipse.ui.PartInitException
|
154
|
* if any.
|
155
|
* @param parentNodeUuid
|
156
|
* a {@link java.util.UUID} object.
|
157
|
*/
|
158
|
public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
|
159
|
TaxonEditorInput input = TaxonEditorInput
|
160
|
.NewEmptyInstance(parentNodeUuid);
|
161
|
open(input, MultiPageTaxonEditor.ID);
|
162
|
|
163
|
getActiveMultiPageTaxonEditor().changed(null);
|
164
|
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* <p>
|
169
|
* setSaving
|
170
|
* </p>
|
171
|
*
|
172
|
* @param isSaving
|
173
|
* a boolean.
|
174
|
*/
|
175
|
public static void setSaving(boolean isSaving) {
|
176
|
EditorUtil.isSaving = isSaving;
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* <p>
|
181
|
* isSaving
|
182
|
* </p>
|
183
|
*
|
184
|
* @return a boolean.
|
185
|
*/
|
186
|
public static boolean isSaving() {
|
187
|
return isSaving;
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
|
192
|
*
|
193
|
* @return a {@link java.util.Set} object.
|
194
|
*/
|
195
|
public static Set<IEditorPart> getOpenEditors() {
|
196
|
Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
|
197
|
|
198
|
if (getActivePage() != null) {
|
199
|
for (IEditorReference reference : getActivePage()
|
200
|
.getEditorReferences()) {
|
201
|
IEditorPart editor = reference.getEditor(false);
|
202
|
if (editor instanceof MultiPageTaxonEditor) {
|
203
|
taxonEditors.add(editor);
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
|
208
|
return taxonEditors;
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* Returns the currently active taxon editor
|
213
|
*
|
214
|
* @return the taxon editor that has focus
|
215
|
*/
|
216
|
public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
|
217
|
IEditorPart editorPart = getActiveEditor();
|
218
|
if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
|
219
|
MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
|
220
|
editor.getConversationHolder().bind();
|
221
|
return editor;
|
222
|
}
|
223
|
return null;
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* <p>
|
228
|
* getActiveEditorPage
|
229
|
* </p>
|
230
|
*
|
231
|
* @param page
|
232
|
* a {@link eu.etaxonomy.taxeditor.editor.Page} object.
|
233
|
* @return a {@link org.eclipse.ui.IEditorPart} object.
|
234
|
*/
|
235
|
public static IEditorPart getActiveEditorPage(Page page) {
|
236
|
MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
|
237
|
|
238
|
return editor != null ? editor.getPage(page) : null;
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Returns the selection of the currently active taxon editor
|
243
|
*
|
244
|
* @return a {@link org.eclipse.jface.viewers.ISelection} object.
|
245
|
*/
|
246
|
public static ISelection getCurrentSelection() {
|
247
|
if (getActiveMultiPageTaxonEditor() == null) {
|
248
|
return null;
|
249
|
} else {
|
250
|
return getActiveMultiPageTaxonEditor().getSite()
|
251
|
.getSelectionProvider().getSelection();
|
252
|
}
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* <p>
|
257
|
* getUndoContext
|
258
|
* </p>
|
259
|
*
|
260
|
* @param editor
|
261
|
* a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
|
262
|
* object.
|
263
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext}
|
264
|
* object.
|
265
|
*/
|
266
|
public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
|
267
|
return editor.getUndoContext();
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* <p>
|
272
|
* getUndoContext
|
273
|
* </p>
|
274
|
*
|
275
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext}
|
276
|
* object.
|
277
|
*/
|
278
|
public static IUndoContext getUndoContext() {
|
279
|
return IOperationHistory.GLOBAL_UNDO_CONTEXT;
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* <p>
|
284
|
* forceUserSave
|
285
|
* </p>
|
286
|
*
|
287
|
* @param editor
|
288
|
* a {@link org.eclipse.ui.IEditorPart} object.
|
289
|
* @param shell
|
290
|
* a {@link org.eclipse.swt.widgets.Shell} object.
|
291
|
* @return a boolean.
|
292
|
*/
|
293
|
public static boolean forceUserSave(IEditorPart editor, Shell shell) {
|
294
|
if (editor.isDirty()) {
|
295
|
|
296
|
boolean doSave = MessageDialog
|
297
|
.openConfirm(shell, "Confirm save",
|
298
|
"Warning - this operation will save the editor. This will also save all other unsaved changes " +
|
299
|
"in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
|
300
|
|
301
|
if (!doSave) {
|
302
|
return false;
|
303
|
}
|
304
|
|
305
|
editor.doSave(AbstractUtility.getMonitor());
|
306
|
}
|
307
|
return true;
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* <p>
|
312
|
* getSelection
|
313
|
* </p>
|
314
|
*
|
315
|
* @param event
|
316
|
* a {@link org.eclipse.core.commands.ExecutionEvent} object.
|
317
|
* @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
|
318
|
*/
|
319
|
public static IStructuredSelection getSelection(ExecutionEvent event) {
|
320
|
IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
|
321
|
|
322
|
return (IStructuredSelection) activeEditor.getSite()
|
323
|
.getSelectionProvider().getSelection();
|
324
|
}
|
325
|
|
326
|
/**
|
327
|
* <p>
|
328
|
* getPluginId
|
329
|
* </p>
|
330
|
*
|
331
|
* @return a {@link java.lang.String} object.
|
332
|
*/
|
333
|
public static String getPluginId() {
|
334
|
return TaxeditorEditorPlugin.PLUGIN_ID;
|
335
|
}
|
336
|
|
337
|
public static void openPolytomousKey(UUID polytomousKeyUuid)
|
338
|
throws Exception {
|
339
|
PolytomousKeyEditorInput input = PolytomousKeyEditorInput
|
340
|
.NewInstance(polytomousKeyUuid);
|
341
|
open(input);
|
342
|
}
|
343
|
|
344
|
public static void openCdmAuthorities(UUID groupUuid)
|
345
|
throws Exception {
|
346
|
CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
|
347
|
open(input);
|
348
|
}
|
349
|
|
350
|
/**
|
351
|
* Iterates recursively over all originals having the given specimen as a derivate.
|
352
|
* The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
|
353
|
* @param specimen the start element for which the originals are iterated recursively
|
354
|
* @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
|
355
|
*/
|
356
|
public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
|
357
|
if(specimen.isInstanceOf(FieldUnit.class)){
|
358
|
return specimen;
|
359
|
}
|
360
|
else if(specimen instanceof DerivedUnit
|
361
|
&& ((DerivedUnit) specimen).getOriginals()!=null
|
362
|
&& !((DerivedUnit) specimen).getOriginals().isEmpty()){
|
363
|
for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
|
364
|
return getTopMostDerivate(original);
|
365
|
}
|
366
|
//needed to add this for compilation although this is unreachable
|
367
|
return specimen;
|
368
|
}
|
369
|
else{
|
370
|
return specimen;
|
371
|
}
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Iterates recursively over all originals having the given specimen as a derivate.
|
376
|
* If a {@link FieldUnit} is found it is returned
|
377
|
* @param specimen the start element for which the originals are iterated recursively
|
378
|
* @return the FieldUnit if found, <code>null</code> otherwise
|
379
|
*/
|
380
|
public static FieldUnit getFieldUnit(SpecimenOrObservationBase<?> specimen){
|
381
|
SpecimenOrObservationBase<?> topMostDerivate = getTopMostDerivate(specimen);
|
382
|
if(topMostDerivate instanceof FieldUnit) {
|
383
|
return (FieldUnit) topMostDerivate;
|
384
|
}
|
385
|
return null;
|
386
|
}
|
387
|
|
388
|
/**
|
389
|
* If the current selection is a single {@link TreeNode} it will be returned.
|
390
|
* @param selection the selection to check
|
391
|
* @return the selected TreeNode or <code>null</code> if no TreeNode selected
|
392
|
*/
|
393
|
public static TreeNode getTreeNodeOfSelection(ISelection selection){
|
394
|
if(selection instanceof IStructuredSelection
|
395
|
&& ((IStructuredSelection) selection).size()==1
|
396
|
&& ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
|
397
|
return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
|
398
|
|
399
|
}
|
400
|
return null;
|
401
|
}
|
402
|
}
|