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