Project

General

Profile

Download (13.7 KB) Statistics
| Branch: | Tag: | Revision:
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.api.service.ITaxonService;
31
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
32
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
33
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
36
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
37
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
38
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
39
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
40
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
41
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
42
import eu.etaxonomy.taxeditor.editor.view.dataimport.BioCaseEditorInput;
43
import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditor;
44
import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditorInput;
45
import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditor;
46
import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditorInput;
47
import eu.etaxonomy.taxeditor.editor.view.dataimport.SpecimenImportEditor;
48
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
49
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
50
import eu.etaxonomy.taxeditor.model.AbstractUtility;
51
import eu.etaxonomy.taxeditor.model.MessagingUtils;
52
import eu.etaxonomy.taxeditor.store.CdmStore;
53

    
54
/**
55
 * Utility for the editor package
56
 *
57
 * @author n.hoffmann
58
 * @created 20.01.2009
59
 * @version 1.0
60
 */
61
public class EditorUtil extends AbstractUtility {
62

    
63
	private static boolean isSaving = false;
64

    
65
	/**
66
	 * Opens a new editor window with the given input
67
	 *
68
	 * @param input
69
	 * @param editorId
70
	 * @return
71
	 * @return
72
	 * @throws PartInitException
73
	 */
74
	private static IEditorPart open(final IEditorInput input,
75
			final String editorId) throws PartInitException {
76
		return getActivePage().openEditor(input, editorId);
77
	}
78

    
79
	/**
80
	 * Opens a new editor window with the given TaxonEditorInput
81
	 *
82
	 * @param input
83
	 *            a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
84
	 *            object.
85
	 * @throws org.eclipse.ui.PartInitException
86
	 *             if any.
87
	 */
88
	public static void open(TaxonEditorInput input) throws PartInitException {
89
		open(input, MultiPageTaxonEditor.ID);
90
	}
91

    
92
	public static void open(PolytomousKeyEditorInput input)
93
			throws PartInitException {
94
		open(input, KeyEditor.ID);
95
	}
96

    
97
	public static void open(CdmAuthorityEditorInput input)
98
			throws PartInitException {
99
		open(input, CdmAuthorityEditor.ID);
100
	}
101

    
102
	/**
103
	 * Opens a new DerivateView for the given input
104
	 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
105
	 * @throws PartInitException
106
	 */
107
	public static void open(DerivateViewEditorInput input)
108
	        throws PartInitException {
109
	    open(input, DerivateView.ID);
110
	}
111

    
112

    
113
	/**
114
	 * Opens a new ChecklistView for the given input
115
	 * @param input a {@link ChecklistEditorInput} representing the selected checklist
116
	 * @throws PartInitException
117
	 */
118
	public static void open(ChecklistEditorInput input)
119
	        throws PartInitException {
120
	    open(input, ChecklistEditor.ID);
121
	}
122

    
123
	/**
124
	 * Opens a new {@link DataImportEditor} for the given input
125
	 * @param input a {@link DataImportEditorInput}
126
	 * @throws PartInitException
127
	 */
128
	public static void open(DataImportEditorInput<?> input)
129
	        throws PartInitException {
130
	    if(input instanceof BioCaseEditorInput){
131
	        open(input, SpecimenImportEditor.ID);
132
	    }
133
	    else if(input instanceof GbifImportEditorInput){
134
	        open(input, GbifImportEditor.ID);
135
	    }
136
	}
137

    
138
	/**
139
	 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
140
	 * initialised here and is passed to the editor
141
	 *
142
	 * @param taxonNodeUuid
143
	 *            a {@link java.util.UUID} object.
144
	 * @throws java.lang.Exception
145
	 *             if any.
146
	 */
147
	public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
148
		TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
149
		open(input);
150
	}
151

    
152
	/**
153
	 * <p>
154
	 * openTaxonBase
155
	 * </p>
156
	 *
157
	 * @param taxonBaseUuid
158
	 *            a {@link java.util.UUID} object.
159
	 * @throws org.eclipse.ui.PartInitException
160
	 *             if any.
161
	 */
162
	public static void openTaxonBase(UUID taxonBaseUuid)
163
			throws PartInitException {
164
	    TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
165
        if (taxonBase != null && taxonBase.isOrphaned()) {
166
                MessagingUtils.warningDialog("Orphaned Taxon", TaxonEditorInput.class, "This is an orphaned taxon i.e. a taxon that is not connected to a classification and not having any taxonomic relationships. Editing of orphaned taxon is currently not supported.");
167
                return;
168
        }
169
		TaxonEditorInput input = TaxonEditorInput
170
				.NewInstanceFromTaxonBase(taxonBaseUuid);
171
		open(input);
172
	}
173

    
174
	/**
175
	 * <p>
176
	 * findEditorByTaxonNodeUuid
177
	 * </p>
178
	 *
179
	 * @param taxonNodeUuid
180
	 *            a {@link java.util.UUID} object.
181
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
182
	 * @throws java.lang.Exception
183
	 *             if any.
184
	 */
185
	public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
186
			throws Exception {
187
		IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
188
		return getActivePage().findEditor(input);
189
	}
190

    
191
	/**
192
	 * An uninitialized taxon is one that hasn't been saved yet. As such, it
193
	 * should appear in neither the list of recent names nor in the taxonomic
194
	 * tree when opened.
195
	 *
196
	 * @throws org.eclipse.ui.PartInitException
197
	 *             if any.
198
	 * @param parentNodeUuid
199
	 *            a {@link java.util.UUID} object.
200
	 */
201
	public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
202
		TaxonEditorInput input = TaxonEditorInput
203
				.NewEmptyInstance(parentNodeUuid);
204
		open(input, MultiPageTaxonEditor.ID);
205

    
206
		getActiveMultiPageTaxonEditor().changed(null);
207

    
208
	}
209

    
210
	/**
211
	 * <p>
212
	 * setSaving
213
	 * </p>
214
	 *
215
	 * @param isSaving
216
	 *            a boolean.
217
	 */
218
	public static void setSaving(boolean isSaving) {
219
		EditorUtil.isSaving = isSaving;
220
	}
221

    
222
	/**
223
	 * <p>
224
	 * isSaving
225
	 * </p>
226
	 *
227
	 * @return a boolean.
228
	 */
229
	public static boolean isSaving() {
230
		return isSaving;
231
	}
232

    
233
	/**
234
	 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
235
	 *
236
	 * @return a {@link java.util.Set} object.
237
	 */
238
	public static Set<IEditorPart> getOpenEditors() {
239
		Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
240

    
241
		if (getActivePage() != null) {
242
			for (IEditorReference reference : getActivePage()
243
					.getEditorReferences()) {
244
				IEditorPart editor = reference.getEditor(false);
245
				if (editor instanceof MultiPageTaxonEditor) {
246
					taxonEditors.add(editor);
247
				}
248
			}
249
		}
250

    
251
		return taxonEditors;
252
	}
253

    
254
	/**
255
	 * Returns the currently active taxon editor
256
	 *
257
	 * @return the taxon editor that has focus
258
	 */
259
	public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
260
		IEditorPart editorPart = getActiveEditor();
261
		if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
262
			MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
263
			editor.getConversationHolder().bind();
264
			return editor;
265
		}
266
		return null;
267
	}
268

    
269
	/**
270
	 * <p>
271
	 * getActiveEditorPage
272
	 * </p>
273
	 *
274
	 * @param page
275
	 *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
276
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
277
	 */
278
	public static IEditorPart getActiveEditorPage(Page page) {
279
		MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
280

    
281
		return editor != null ? editor.getPage(page) : null;
282
	}
283

    
284
	/**
285
	 * Returns the selection of the currently active taxon editor
286
	 *
287
	 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
288
	 */
289
	public static ISelection getCurrentSelection() {
290
		if (getActiveMultiPageTaxonEditor() == null) {
291
			return null;
292
		} else {
293
			return getActiveMultiPageTaxonEditor().getSite()
294
					.getSelectionProvider().getSelection();
295
		}
296
	}
297

    
298
	/**
299
	 * <p>
300
	 * getUndoContext
301
	 * </p>
302
	 *
303
	 * @param editor
304
	 *            a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
305
	 *            object.
306
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
307
	 *         object.
308
	 */
309
	public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
310
		return editor.getUndoContext();
311
	}
312

    
313
	/**
314
	 * <p>
315
	 * getUndoContext
316
	 * </p>
317
	 *
318
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
319
	 *         object.
320
	 */
321
	public static IUndoContext getUndoContext() {
322
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
323
	}
324

    
325
	/**
326
	 * <p>
327
	 * forceUserSave
328
	 * </p>
329
	 *
330
	 * @param editor
331
	 *            a {@link org.eclipse.ui.IEditorPart} object.
332
	 * @param shell
333
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
334
	 * @return a boolean.
335
	 */
336
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
337
		if (editor.isDirty()) {
338

    
339
			boolean doSave = MessageDialog
340
					.openConfirm(shell, "Confirm save",
341
							"Warning - this operation will save the editor. This will also save all other unsaved changes " +
342
							"in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
343

    
344
			if (!doSave) {
345
				return false;
346
			}
347

    
348
			editor.doSave(AbstractUtility.getMonitor());
349
		}
350
		return true;
351
	}
352

    
353
	/**
354
	 * <p>
355
	 * getSelection
356
	 * </p>
357
	 *
358
	 * @param event
359
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
360
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
361
	 */
362
	public static IStructuredSelection getSelection(ExecutionEvent event) {
363
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
364

    
365
		return (IStructuredSelection) activeEditor.getSite()
366
				.getSelectionProvider().getSelection();
367
	}
368

    
369
	/**
370
	 * <p>
371
	 * getPluginId
372
	 * </p>
373
	 *
374
	 * @return a {@link java.lang.String} object.
375
	 */
376
	public static String getPluginId() {
377
		return TaxeditorEditorPlugin.PLUGIN_ID;
378
	}
379

    
380
	public static void openPolytomousKey(UUID polytomousKeyUuid)
381
			throws Exception {
382
		PolytomousKeyEditorInput input = PolytomousKeyEditorInput
383
				.NewInstance(polytomousKeyUuid);
384
		open(input);
385
	}
386

    
387
	public static void openCdmAuthorities(UUID groupUuid)
388
			throws Exception {
389
		CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
390
		open(input);
391
	}
392

    
393
	/**
394
	 * Iterates recursively over all originals having the given specimen as a derivate.
395
	 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
396
	 * @param specimen the start element for which the originals are iterated recursively
397
	 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
398
	 */
399
	public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
400
	    if(specimen.isInstanceOf(FieldUnit.class)){
401
	        return specimen;
402
	    }
403
	    else if(specimen instanceof DerivedUnit
404
	            && ((DerivedUnit) specimen).getOriginals()!=null
405
	            && !((DerivedUnit) specimen).getOriginals().isEmpty()){
406
	        for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
407
	            return getTopMostDerivate(original);
408
	        }
409
	        //needed to add this for compilation although this is unreachable
410
	        return specimen;
411
	    }
412
	    else{
413
	        return specimen;
414
	    }
415
	}
416

    
417
	/**
418
	 * Iterates recursively over all originals having the given specimen as a derivate.
419
	 * If a {@link FieldUnit} is found it is returned
420
	 * @param specimen the start element for which the originals are iterated recursively
421
	 * @return the FieldUnit if found, <code>null</code> otherwise
422
	 */
423
    public static FieldUnit getFieldUnit(SpecimenOrObservationBase<?> specimen){
424
        SpecimenOrObservationBase<?> topMostDerivate = getTopMostDerivate(specimen);
425
        if(topMostDerivate instanceof FieldUnit) {
426
            return (FieldUnit) topMostDerivate;
427
        }
428
        return null;
429
    }
430

    
431
    /**
432
     * If the current selection is a single {@link TreeNode} it will be returned.
433
     * @param selection the selection to check
434
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
435
     */
436
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
437
        if(selection instanceof IStructuredSelection
438
                && ((IStructuredSelection) selection).size()==1
439
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
440
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
441

    
442
        }
443
        return null;
444
    }
445

    
446
    /**
447
     * Opens a taxon editor for the given object if the given object is a valid input.
448
     * @param object the object for which the editor will be opened
449
     * @throws PartInitException
450
     */
451
    public static void openTaxonEditor(Object object) throws PartInitException {
452
        if(object instanceof TaxonBase<?>){
453
            openTaxonBase(((TaxonBase<?>) object).getUuid());
454
        }
455
    }
456
}
(4-4/17)