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