Project

General

Profile

Download (12.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.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.checklist.ChecklistEditorInput;
39
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
40
import eu.etaxonomy.taxeditor.editor.view.dataimport.BioCaseEditorInput;
41
import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditor;
42
import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditorInput;
43
import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditor;
44
import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditorInput;
45
import eu.etaxonomy.taxeditor.editor.view.dataimport.SpecimenImportEditor;
46
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
47
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
48
import eu.etaxonomy.taxeditor.model.AbstractUtility;
49

    
50
/**
51
 * Utility for the editor package
52
 *
53
 * @author n.hoffmann
54
 * @created 20.01.2009
55
 * @version 1.0
56
 */
57
public class EditorUtil extends AbstractUtility {
58

    
59
	private static boolean isSaving = false;
60

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

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

    
88
	public static void open(PolytomousKeyEditorInput input)
89
			throws PartInitException {
90
		open(input, KeyEditor.ID);
91
	}
92

    
93
	public static void open(CdmAuthorityEditorInput input)
94
			throws PartInitException {
95
		open(input, CdmAuthorityEditor.ID);
96
	}
97

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

    
108

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

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

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

    
148
	/**
149
	 * <p>
150
	 * openTaxonBase
151
	 * </p>
152
	 *
153
	 * @param taxonBaseUuid
154
	 *            a {@link java.util.UUID} object.
155
	 * @throws org.eclipse.ui.PartInitException
156
	 *             if any.
157
	 */
158
	public static void openTaxonBase(UUID taxonBaseUuid)
159
			throws PartInitException {
160
		TaxonEditorInput input = TaxonEditorInput
161
				.NewInstanceFromTaxonBase(taxonBaseUuid);
162
		open(input);
163
	}
164

    
165
	/**
166
	 * <p>
167
	 * findEditorByTaxonNodeUuid
168
	 * </p>
169
	 *
170
	 * @param taxonNodeUuid
171
	 *            a {@link java.util.UUID} object.
172
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
173
	 * @throws java.lang.Exception
174
	 *             if any.
175
	 */
176
	public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
177
			throws Exception {
178
		IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
179
		return getActivePage().findEditor(input);
180
	}
181

    
182
	/**
183
	 * An uninitialized taxon is one that hasn't been saved yet. As such, it
184
	 * should appear in neither the list of recent names nor in the taxonomic
185
	 * tree when opened.
186
	 *
187
	 * @throws org.eclipse.ui.PartInitException
188
	 *             if any.
189
	 * @param parentNodeUuid
190
	 *            a {@link java.util.UUID} object.
191
	 */
192
	public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
193
		TaxonEditorInput input = TaxonEditorInput
194
				.NewEmptyInstance(parentNodeUuid);
195
		open(input, MultiPageTaxonEditor.ID);
196

    
197
		getActiveMultiPageTaxonEditor().changed(null);
198

    
199
	}
200

    
201
	/**
202
	 * <p>
203
	 * setSaving
204
	 * </p>
205
	 *
206
	 * @param isSaving
207
	 *            a boolean.
208
	 */
209
	public static void setSaving(boolean isSaving) {
210
		EditorUtil.isSaving = isSaving;
211
	}
212

    
213
	/**
214
	 * <p>
215
	 * isSaving
216
	 * </p>
217
	 *
218
	 * @return a boolean.
219
	 */
220
	public static boolean isSaving() {
221
		return isSaving;
222
	}
223

    
224
	/**
225
	 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
226
	 *
227
	 * @return a {@link java.util.Set} object.
228
	 */
229
	public static Set<IEditorPart> getOpenEditors() {
230
		Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
231

    
232
		if (getActivePage() != null) {
233
			for (IEditorReference reference : getActivePage()
234
					.getEditorReferences()) {
235
				IEditorPart editor = reference.getEditor(false);
236
				if (editor instanceof MultiPageTaxonEditor) {
237
					taxonEditors.add(editor);
238
				}
239
			}
240
		}
241

    
242
		return taxonEditors;
243
	}
244

    
245
	/**
246
	 * Returns the currently active taxon editor
247
	 *
248
	 * @return the taxon editor that has focus
249
	 */
250
	public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
251
		IEditorPart editorPart = getActiveEditor();
252
		if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
253
			MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
254
			editor.getConversationHolder().bind();
255
			return editor;
256
		}
257
		return null;
258
	}
259

    
260
	/**
261
	 * <p>
262
	 * getActiveEditorPage
263
	 * </p>
264
	 *
265
	 * @param page
266
	 *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
267
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
268
	 */
269
	public static IEditorPart getActiveEditorPage(Page page) {
270
		MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
271

    
272
		return editor != null ? editor.getPage(page) : null;
273
	}
274

    
275
	/**
276
	 * Returns the selection of the currently active taxon editor
277
	 *
278
	 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
279
	 */
280
	public static ISelection getCurrentSelection() {
281
		if (getActiveMultiPageTaxonEditor() == null) {
282
			return null;
283
		} else {
284
			return getActiveMultiPageTaxonEditor().getSite()
285
					.getSelectionProvider().getSelection();
286
		}
287
	}
288

    
289
	/**
290
	 * <p>
291
	 * getUndoContext
292
	 * </p>
293
	 *
294
	 * @param editor
295
	 *            a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
296
	 *            object.
297
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
298
	 *         object.
299
	 */
300
	public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
301
		return editor.getUndoContext();
302
	}
303

    
304
	/**
305
	 * <p>
306
	 * getUndoContext
307
	 * </p>
308
	 *
309
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
310
	 *         object.
311
	 */
312
	public static IUndoContext getUndoContext() {
313
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
314
	}
315

    
316
	/**
317
	 * <p>
318
	 * forceUserSave
319
	 * </p>
320
	 *
321
	 * @param editor
322
	 *            a {@link org.eclipse.ui.IEditorPart} object.
323
	 * @param shell
324
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
325
	 * @return a boolean.
326
	 */
327
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
328
		if (editor.isDirty()) {
329

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

    
335
			if (!doSave) {
336
				return false;
337
			}
338

    
339
			editor.doSave(AbstractUtility.getMonitor());
340
		}
341
		return true;
342
	}
343

    
344
	/**
345
	 * <p>
346
	 * getSelection
347
	 * </p>
348
	 *
349
	 * @param event
350
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
351
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
352
	 */
353
	public static IStructuredSelection getSelection(ExecutionEvent event) {
354
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
355

    
356
		return (IStructuredSelection) activeEditor.getSite()
357
				.getSelectionProvider().getSelection();
358
	}
359

    
360
	/**
361
	 * <p>
362
	 * getPluginId
363
	 * </p>
364
	 *
365
	 * @return a {@link java.lang.String} object.
366
	 */
367
	public static String getPluginId() {
368
		return TaxeditorEditorPlugin.PLUGIN_ID;
369
	}
370

    
371
	public static void openPolytomousKey(UUID polytomousKeyUuid)
372
			throws Exception {
373
		PolytomousKeyEditorInput input = PolytomousKeyEditorInput
374
				.NewInstance(polytomousKeyUuid);
375
		open(input);
376
	}
377

    
378
	public static void openCdmAuthorities(UUID groupUuid)
379
			throws Exception {
380
		CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
381
		open(input);
382
	}
383

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

    
408
	/**
409
	 * Iterates recursively over all originals having the given specimen as a derivate.
410
	 * If a {@link FieldUnit} is found it is returned
411
	 * @param specimen the start element for which the originals are iterated recursively
412
	 * @return the FieldUnit if found, <code>null</code> otherwise
413
	 */
414
    public static FieldUnit getFieldUnit(SpecimenOrObservationBase<?> specimen){
415
        SpecimenOrObservationBase<?> topMostDerivate = getTopMostDerivate(specimen);
416
        if(topMostDerivate instanceof FieldUnit) {
417
            return (FieldUnit) topMostDerivate;
418
        }
419
        return null;
420
    }
421

    
422
    /**
423
     * If the current selection is a single {@link TreeNode} it will be returned.
424
     * @param selection the selection to check
425
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
426
     */
427
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
428
        if(selection instanceof IStructuredSelection
429
                && ((IStructuredSelection) selection).size()==1
430
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
431
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
432

    
433
        }
434
        return null;
435
    }
436
}
(4-4/17)