Project

General

Profile

Download (14.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.IWorkbenchPage;
28
import org.eclipse.ui.PartInitException;
29
import org.eclipse.ui.handlers.HandlerUtil;
30

    
31
import eu.etaxonomy.cdm.api.service.ITaxonService;
32
import eu.etaxonomy.cdm.model.common.ITreeNode;
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.TaxonBase;
37
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
39
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
40
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
41
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
42
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
43
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
44
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
45
import eu.etaxonomy.taxeditor.editor.view.dataimport.BioCaseEditorInput;
46
import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditor;
47
import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditorInput;
48
import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditor;
49
import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditorInput;
50
import eu.etaxonomy.taxeditor.editor.view.dataimport.SpecimenImportEditor;
51
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
52
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
53
import eu.etaxonomy.taxeditor.model.AbstractUtility;
54
import eu.etaxonomy.taxeditor.model.MessagingUtils;
55
import eu.etaxonomy.taxeditor.store.CdmStore;
56

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

    
66
	private static boolean isSaving = false;
67

    
68
	/**
69
	 * Opens a new editor window with the given input
70
	 *
71
	 * @param input
72
	 * @param editorId
73
	 * @return
74
	 * @return
75
	 * @throws PartInitException
76
	 */
77
	private static IEditorPart open(final IEditorInput input,
78
			final String editorId) throws PartInitException {
79
		IEditorPart editor = getActivePage().openEditor(input, editorId);
80
		if(input != null &&
81
		        editor.getEditorInput() != null &&
82
		        input != editor.getEditorInput() &&
83
		        input instanceof CdmEntitySessionInput) {
84
		    ((CdmEntitySessionInput)input).dispose();
85
		}
86
		return editor;
87
	}
88

    
89
	/**
90
	 * Opens a new editor window with the given TaxonEditorInput
91
	 *
92
	 * @param input
93
	 *            a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
94
	 *            object.
95
	 * @throws org.eclipse.ui.PartInitException
96
	 *             if any.
97
	 */
98
	public static void open(TaxonEditorInput input) throws PartInitException {
99
		open(input, MultiPageTaxonEditor.ID);
100
	}
101

    
102
	public static void open(PolytomousKeyEditorInput input)
103
			throws PartInitException {
104
		open(input, KeyEditor.ID);
105
	}
106

    
107
	public static void open(CdmAuthorityEditorInput input)
108
			throws PartInitException {
109
		open(input, CdmAuthorityEditor.ID);
110
	}
111

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

    
122

    
123
	/**
124
	 * Opens a new ChecklistView for the given input
125
	 * @param input a {@link ChecklistEditorInput} representing the selected checklist
126
	 * @throws PartInitException
127
	 */
128
	public static void open(ChecklistEditorInput input)
129
	        throws PartInitException {
130
	    open(input, ChecklistEditor.ID);
131
	}
132

    
133
	/**
134
	 * Opens a new {@link DataImportEditor} for the given input
135
	 * @param input a {@link DataImportEditorInput}
136
	 * @throws PartInitException
137
	 */
138
	public static void open(DataImportEditorInput<?> input)
139
	        throws PartInitException {
140
	    if(input instanceof BioCaseEditorInput){
141
	        open(input, SpecimenImportEditor.ID);
142
	    }
143
	    else if(input instanceof GbifImportEditorInput){
144
	        open(input, GbifImportEditor.ID);
145
	    }
146
	}
147

    
148
	/**
149
	 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
150
	 * initialised here and is passed to the editor
151
	 *
152
	 * @param taxonNodeUuid
153
	 *            a {@link java.util.UUID} object.
154
	 * @throws java.lang.Exception
155
	 *             if any.
156
	 */
157
	public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
158
		TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
159
		open(input);
160
	}
161

    
162
	/**
163
	 * <p>
164
	 * openTaxonBase
165
	 * </p>
166
	 *
167
	 * @param taxonBaseUuid
168
	 *            a {@link java.util.UUID} object.
169
	 * @throws org.eclipse.ui.PartInitException
170
	 *             if any.
171
	 */
172
	public static void openTaxonBase(UUID taxonBaseUuid)
173
			throws PartInitException {
174
	    TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
175
        if (taxonBase != null && taxonBase.isOrphaned()) {
176
                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.");
177
                return;
178
        }
179
		TaxonEditorInput input = TaxonEditorInput
180
				.NewInstanceFromTaxonBase(taxonBaseUuid);
181
		if(input!=null){
182
		    open(input);
183
		}
184
	}
185

    
186
	/**
187
	 * <p>
188
	 * findEditorByTaxonNodeUuid
189
	 * </p>
190
	 *
191
	 * @param taxonNodeUuid
192
	 *            a {@link java.util.UUID} object.
193
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
194
	 * @throws java.lang.Exception
195
	 *             if any.
196
	 */
197
	public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
198
			throws Exception {
199
		IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
200
		return getActivePage().findEditor(input);
201
	}
202

    
203
	/**
204
	 * An uninitialized taxon is one that hasn't been saved yet. As such, it
205
	 * should appear in neither the list of recent names nor in the taxonomic
206
	 * tree when opened.
207
	 *
208
	 * @throws org.eclipse.ui.PartInitException
209
	 *             if any.
210
	 * @param parentNodeUuid
211
	 *            a {@link java.util.UUID} object.
212
	 */
213
	public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
214
		TaxonEditorInput input = TaxonEditorInput
215
				.NewEmptyInstance(parentNodeUuid);
216
		open(input, MultiPageTaxonEditor.ID);
217

    
218
		getActiveMultiPageTaxonEditor().changed(null);
219

    
220
	}
221

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

    
234
	/**
235
	 * <p>
236
	 * isSaving
237
	 * </p>
238
	 *
239
	 * @return a boolean.
240
	 */
241
	public static boolean isSaving() {
242
		return isSaving;
243
	}
244

    
245
	/**
246
	 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
247
	 *
248
	 * @return a {@link java.util.Set} object.
249
	 */
250
	public static Set<IEditorPart> getOpenEditors() {
251
		Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
252

    
253
		if (getActivePage() != null) {
254
			for (IEditorReference reference : getActivePage()
255
					.getEditorReferences()) {
256
				IEditorPart editor = reference.getEditor(false);
257
				if (editor instanceof MultiPageTaxonEditor) {
258
					taxonEditors.add(editor);
259
				}
260
			}
261
		}
262

    
263
		return taxonEditors;
264
	}
265

    
266
	/**
267
	 * Returns the currently active taxon editor
268
	 *
269
	 * @return the taxon editor that has focus
270
	 */
271
	public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
272
		IEditorPart editorPart = getActiveEditor();
273
		if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
274
			MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
275
			editor.getConversationHolder().bind();
276
			return editor;
277
		}
278
		return null;
279
	}
280

    
281
	/**
282
	 * <p>
283
	 * getActiveEditorPage
284
	 * </p>
285
	 *
286
	 * @param page
287
	 *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
288
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
289
	 */
290
	public static IEditorPart getActiveEditorPage(Page page) {
291
		MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
292

    
293
		return editor != null ? editor.getPage(page) : null;
294
	}
295

    
296
	/**
297
	 * Returns the selection of the currently active taxon editor
298
	 *
299
	 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
300
	 */
301
	public static ISelection getCurrentSelection() {
302
		if (getActiveMultiPageTaxonEditor() == null) {
303
			return null;
304
		} else {
305
			return getActiveMultiPageTaxonEditor().getSite()
306
					.getSelectionProvider().getSelection();
307
		}
308
	}
309

    
310
	/**
311
	 * <p>
312
	 * getUndoContext
313
	 * </p>
314
	 *
315
	 * @param editor
316
	 *            a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
317
	 *            object.
318
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
319
	 *         object.
320
	 */
321
	public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
322
		return editor.getUndoContext();
323
	}
324

    
325
	/**
326
	 * <p>
327
	 * getUndoContext
328
	 * </p>
329
	 *
330
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
331
	 *         object.
332
	 */
333
	public static IUndoContext getUndoContext() {
334
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
335
	}
336

    
337
	/**
338
	 * <p>
339
	 * forceUserSave
340
	 * </p>
341
	 *
342
	 * @param editor
343
	 *            a {@link org.eclipse.ui.IEditorPart} object.
344
	 * @param shell
345
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
346
	 * @return a boolean.
347
	 */
348
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
349
		if (editor.isDirty()) {
350

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

    
356
			if (!doSave) {
357
				return false;
358
			}
359

    
360
			editor.doSave(AbstractUtility.getMonitor());
361
		}
362
		return true;
363
	}
364

    
365
	/**
366
	 * <p>
367
	 * getSelection
368
	 * </p>
369
	 *
370
	 * @param event
371
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
372
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
373
	 */
374
	public static IStructuredSelection getSelection(ExecutionEvent event) {
375
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
376

    
377
		return (IStructuredSelection) activeEditor.getSite()
378
				.getSelectionProvider().getSelection();
379
	}
380

    
381
	/**
382
	 * <p>
383
	 * getPluginId
384
	 * </p>
385
	 *
386
	 * @return a {@link java.lang.String} object.
387
	 */
388
	public static String getPluginId() {
389
		return TaxeditorEditorPlugin.PLUGIN_ID;
390
	}
391

    
392
	public static void openPolytomousKey(UUID polytomousKeyUuid)
393
			throws Exception {
394
		PolytomousKeyEditorInput input = PolytomousKeyEditorInput
395
				.NewInstance(polytomousKeyUuid);
396
		open(input);
397
	}
398

    
399
//	public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
400
//	        throws Exception {
401
//	    PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
402
//	    open(input);
403
//	}
404

    
405
	public static void openCdmAuthorities(UUID groupUuid)
406
			throws Exception {
407
		CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
408
		open(input);
409
	}
410

    
411
	/**
412
	 * Iterates recursively over all originals having the given specimen as a derivate.
413
	 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
414
	 * @param specimen the start element for which the originals are iterated recursively
415
	 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
416
	 */
417
	public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
418
	    if(specimen.isInstanceOf(FieldUnit.class)){
419
	        return specimen;
420
	    }
421
	    else if(specimen instanceof DerivedUnit
422
	            && ((DerivedUnit) specimen).getOriginals()!=null
423
	            && !((DerivedUnit) specimen).getOriginals().isEmpty()){
424
	        for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
425
	            return getTopMostDerivate(original);
426
	        }
427
	        //needed to add this for compilation although this is unreachable
428
	        return specimen;
429
	    }
430
	    else{
431
	        return specimen;
432
	    }
433
	}
434

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

    
446
        }
447
        return null;
448
    }
449

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

    
461
    public static boolean closeObsoleteEditor(TaxonNode taxonNode, IWorkbenchPage activePage){
462
        boolean result = true;
463
        for (IEditorReference ref : activePage.getEditorReferences()) {
464
            try {
465
                String treeIndex = ((ITreeNode)taxonNode).treeIndex();
466

    
467

    
468
                IEditorInput input = ref.getEditorInput();
469
                if (input instanceof TaxonEditorInput) {
470
                    TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
471
                    //if node is a child of taxonNode then close the editor
472
                    if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
473
                    //if (taxonNode.equals(node)) {
474
                        result &= activePage.closeEditor(ref.getEditor(false), true);
475

    
476
                    }
477
                }
478
            } catch (PartInitException e) {
479
                continue;
480
            }
481
        }
482
        return result;
483
    }
484
}
(5-5/19)