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.Collection;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import org.eclipse.core.commands.ExecutionEvent;
|
16
|
import org.eclipse.core.commands.operations.IOperationHistory;
|
17
|
import org.eclipse.core.commands.operations.IUndoContext;
|
18
|
import org.eclipse.e4.ui.model.application.MApplication;
|
19
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
20
|
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
|
21
|
import org.eclipse.e4.ui.workbench.modeling.EModelService;
|
22
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
23
|
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
|
24
|
import org.eclipse.jface.dialogs.MessageDialog;
|
25
|
import org.eclipse.jface.viewers.ISelection;
|
26
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
27
|
import org.eclipse.jface.viewers.TreeNode;
|
28
|
import org.eclipse.swt.widgets.Shell;
|
29
|
import org.eclipse.ui.IEditorPart;
|
30
|
import org.eclipse.ui.PartInitException;
|
31
|
import org.eclipse.ui.handlers.HandlerUtil;
|
32
|
|
33
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
34
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
35
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
38
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
39
|
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
|
40
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
41
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
42
|
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
|
43
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
|
44
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
|
45
|
import eu.etaxonomy.taxeditor.editor.workingSet.WorkingSetEditor;
|
46
|
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.CharacterMatrix;
|
47
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
48
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
49
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
50
|
|
51
|
/**
|
52
|
* Utility for the editor package
|
53
|
*
|
54
|
* @author n.hoffmann
|
55
|
* @created 20.01.2009
|
56
|
* @version 1.0
|
57
|
*/
|
58
|
public class EditorUtil extends AbstractUtility {
|
59
|
|
60
|
private static final String NAME_EDITOR_ID = "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
|
61
|
private static boolean isSaving = false;
|
62
|
|
63
|
/**
|
64
|
* Opens a new {@link DataImportEditor} for the given input
|
65
|
* @param input a {@link DataImportEditorInput}
|
66
|
* @throws PartInitException
|
67
|
*/
|
68
|
// public static void open(DataImportEditorInput<?> input)
|
69
|
// throws PartInitException {
|
70
|
// if(input instanceof BioCaseEditorInput){
|
71
|
// open(input, SpecimenImportEditor.ID);
|
72
|
// }
|
73
|
// else if(input instanceof GbifImportEditorInput){
|
74
|
// open(input, GbifImportEditor.ID);
|
75
|
// }
|
76
|
// }
|
77
|
|
78
|
public static void openWorkingSetEditor(UUID workingSetUuid, EModelService modelService, EPartService partService, MApplication application){
|
79
|
MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.view.workingSet.WorkingSetEditor");
|
80
|
MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
|
81
|
if(editorAreaPartStack!=null){
|
82
|
editorAreaPartStack.getChildren().add(part);
|
83
|
}
|
84
|
part = partService.showPart(part, PartState.ACTIVATE);
|
85
|
WorkingSetEditor editor = (WorkingSetEditor) part.getObject();
|
86
|
editor.init(workingSetUuid);
|
87
|
}
|
88
|
|
89
|
public static void openCharacterMatrix(UUID workingSetUuid, EModelService modelService, EPartService partService, MApplication application){
|
90
|
MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.workingSet.matrix.CharacterMatrix");
|
91
|
MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
|
92
|
if(editorAreaPartStack!=null){
|
93
|
editorAreaPartStack.getChildren().add(part);
|
94
|
}
|
95
|
part = partService.showPart(part, PartState.ACTIVATE);
|
96
|
CharacterMatrix editor = (CharacterMatrix) part.getObject();
|
97
|
editor.init(workingSetUuid);
|
98
|
}
|
99
|
|
100
|
public static void openSpecimenEditor(DerivateViewEditorInput input, EModelService modelService, EPartService partService, MApplication application){
|
101
|
MPart part = partService.createPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW);
|
102
|
MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
|
103
|
if(editorAreaPartStack!=null){
|
104
|
editorAreaPartStack.getChildren().add(part);
|
105
|
}
|
106
|
part = partService.showPart(part, PartState.ACTIVATE);
|
107
|
DerivateView derivateView = (DerivateView) part.getObject();
|
108
|
derivateView.init(input);
|
109
|
}
|
110
|
|
111
|
public static void openTaxonNodeE4(UUID taxonNodeUuid, EModelService modelService, EPartService partService, MApplication application) {
|
112
|
TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
|
113
|
openNameEditor_internal(input, modelService, partService, application);
|
114
|
}
|
115
|
|
116
|
public static void openTaxonBaseE4(UUID taxonBaseUuid, EModelService modelService, EPartService partService, MApplication application) {
|
117
|
TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
|
118
|
openNameEditor_internal(input, modelService, partService, application);
|
119
|
}
|
120
|
|
121
|
public static void openTaxonBaseE4(UUID taxonBaseUuid) {
|
122
|
//FIXME E4 this can probably be removed when fully migrated
|
123
|
TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
|
124
|
|
125
|
EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
|
126
|
EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
|
127
|
openNameEditor_internal(input, modelService, partService, null);
|
128
|
}
|
129
|
|
130
|
private static void openNameEditor_internal(TaxonEditorInputE4 input, EModelService modelService, EPartService partService, MApplication application) {
|
131
|
TaxonBase taxonBase = input.getTaxon();
|
132
|
if(taxonBase==null){
|
133
|
return;
|
134
|
}
|
135
|
if (taxonBase.isOrphaned()) {
|
136
|
if(taxonBase.isInstanceOf(Synonym.class)){
|
137
|
MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
|
138
|
return;
|
139
|
}
|
140
|
else{
|
141
|
MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
|
142
|
return;
|
143
|
}
|
144
|
}
|
145
|
|
146
|
Collection<MPart> parts = partService.getParts();
|
147
|
//check if part is already opened
|
148
|
for (MPart part : parts) {
|
149
|
if(part.getObject() instanceof TaxonNameEditorE4
|
150
|
&& ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
|
151
|
&& ((TaxonNameEditorE4) part.getObject()).getTaxon().equals(input.getTaxon())){
|
152
|
//close part to invoke refresh for new part
|
153
|
partService.hidePart(part);
|
154
|
break;
|
155
|
}
|
156
|
}
|
157
|
MPart part = partService.createPart(NAME_EDITOR_ID);
|
158
|
|
159
|
MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
|
160
|
if(editorAreaPartStack!=null){
|
161
|
editorAreaPartStack.getChildren().add(part);
|
162
|
}
|
163
|
part = partService.showPart(part, PartState.ACTIVATE);
|
164
|
|
165
|
TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
|
166
|
editor.init(input);
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* An uninitialized taxon is one that hasn't been saved yet. As such, it
|
171
|
* should appear in neither the list of recent names nor in the taxonomic
|
172
|
* tree when opened.
|
173
|
*
|
174
|
* @param parentNodeUuid
|
175
|
* a {@link java.util.UUID} object.
|
176
|
*/
|
177
|
public static void openEmptyE4(UUID parentNodeUuid) {
|
178
|
TaxonEditorInputE4 input = TaxonEditorInputE4
|
179
|
.NewEmptyInstance(parentNodeUuid);
|
180
|
EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
|
181
|
MPart part = partService.createPart(NAME_EDITOR_ID);
|
182
|
part = partService.showPart(part, PartState.ACTIVATE);
|
183
|
TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
|
184
|
editor.init(input);
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* <p>
|
189
|
* setSaving
|
190
|
* </p>
|
191
|
*
|
192
|
* @param isSaving
|
193
|
* a boolean.
|
194
|
*/
|
195
|
public static void setSaving(boolean isSaving) {
|
196
|
EditorUtil.isSaving = isSaving;
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* <p>
|
201
|
* isSaving
|
202
|
* </p>
|
203
|
*
|
204
|
* @return a boolean.
|
205
|
*/
|
206
|
public static boolean isSaving() {
|
207
|
return isSaving;
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* <p>
|
212
|
* getUndoContext
|
213
|
* </p>
|
214
|
*
|
215
|
* @return a {@link org.eclipse.core.commands.operations.IUndoContext}
|
216
|
* object.
|
217
|
*/
|
218
|
public static IUndoContext getUndoContext() {
|
219
|
return IOperationHistory.GLOBAL_UNDO_CONTEXT;
|
220
|
}
|
221
|
|
222
|
/**
|
223
|
* <p>
|
224
|
* forceUserSave
|
225
|
* </p>
|
226
|
*
|
227
|
* @param editor
|
228
|
* a {@link org.eclipse.ui.IEditorPart} object.
|
229
|
* @param shell
|
230
|
* a {@link org.eclipse.swt.widgets.Shell} object.
|
231
|
* @return a boolean.
|
232
|
*/
|
233
|
public static boolean forceUserSave(IEditorPart editor, Shell shell) {
|
234
|
if (editor.isDirty()) {
|
235
|
|
236
|
boolean doSave = MessageDialog
|
237
|
.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
|
238
|
Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
|
239
|
|
240
|
if (!doSave) {
|
241
|
return false;
|
242
|
}
|
243
|
|
244
|
editor.doSave(AbstractUtility.getMonitor());
|
245
|
}
|
246
|
return true;
|
247
|
}
|
248
|
|
249
|
public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
|
250
|
if (editor.isDirty()) {
|
251
|
|
252
|
boolean doSave = MessageDialog
|
253
|
.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
|
254
|
Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
|
255
|
|
256
|
if (!doSave) {
|
257
|
return false;
|
258
|
}
|
259
|
|
260
|
editor.save(AbstractUtility.getMonitor());
|
261
|
}
|
262
|
return true;
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* <p>
|
267
|
* getSelection
|
268
|
* </p>
|
269
|
*
|
270
|
* @param event
|
271
|
* a {@link org.eclipse.core.commands.ExecutionEvent} object.
|
272
|
* @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
|
273
|
*/
|
274
|
public static IStructuredSelection getSelection(ExecutionEvent event) {
|
275
|
IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
|
276
|
|
277
|
return (IStructuredSelection) activeEditor.getSite()
|
278
|
.getSelectionProvider().getSelection();
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
* <p>
|
283
|
* getPluginId
|
284
|
* </p>
|
285
|
*
|
286
|
* @return a {@link java.lang.String} object.
|
287
|
*/
|
288
|
public static String getPluginId() {
|
289
|
return TaxeditorEditorPlugin.PLUGIN_ID;
|
290
|
}
|
291
|
|
292
|
/**
|
293
|
* Iterates recursively over all originals having the given specimen as a derivate.
|
294
|
* The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
|
295
|
* @param specimen the start element for which the originals are iterated recursively
|
296
|
* @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
|
297
|
*/
|
298
|
public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
|
299
|
if(specimen.isInstanceOf(FieldUnit.class)){
|
300
|
return specimen;
|
301
|
}
|
302
|
else if(specimen instanceof DerivedUnit
|
303
|
&& ((DerivedUnit) specimen).getOriginals()!=null
|
304
|
&& !((DerivedUnit) specimen).getOriginals().isEmpty()){
|
305
|
for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
|
306
|
return getTopMostDerivate(original);
|
307
|
}
|
308
|
//needed to add this for compilation although this is unreachable
|
309
|
return specimen;
|
310
|
}
|
311
|
else{
|
312
|
return specimen;
|
313
|
}
|
314
|
}
|
315
|
|
316
|
/**
|
317
|
* If the current selection is a single {@link TreeNode} it will be returned.
|
318
|
* @param selection the selection to check
|
319
|
* @return the selected TreeNode or <code>null</code> if no TreeNode selected
|
320
|
*/
|
321
|
public static TreeNode getTreeNodeOfSelection(ISelection selection){
|
322
|
if(selection instanceof IStructuredSelection
|
323
|
&& ((IStructuredSelection) selection).size()==1
|
324
|
&& ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
|
325
|
return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
|
326
|
|
327
|
}
|
328
|
return null;
|
329
|
}
|
330
|
|
331
|
public static void closeObsoleteEditor(TaxonNode taxonNode, EPartService partService){
|
332
|
String treeIndex = taxonNode.treeIndex();
|
333
|
Collection<MPart> parts = partService.getParts();
|
334
|
for (MPart part : parts) {
|
335
|
Object object = part.getObject();
|
336
|
if(object instanceof TaxonNameEditorE4){
|
337
|
TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
|
338
|
TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
|
339
|
if(node.treeIndex().startsWith(treeIndex)){
|
340
|
partService.hidePart(part, true);
|
341
|
}
|
342
|
}
|
343
|
}
|
344
|
}
|
345
|
}
|