Project

General

Profile

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

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

    
68
	private static boolean isSaving = false;
69

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

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

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

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

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

    
124

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

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

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

    
164
	/**
165
	 * <p>
166
	 * openTaxonBase
167
	 * </p>
168
	 *
169
	 * @param taxonBaseUuid
170
	 *            a {@link java.util.UUID} object.
171
	 * @throws org.eclipse.ui.PartInitException
172
	 *             if any.
173
	 */
174
	public static void openTaxonBase(UUID taxonBaseUuid)
175
			throws PartInitException {
176
	    TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
177
        if (taxonBase != null && taxonBase.isOrphaned()) {
178
            if(taxonBase.isInstanceOf(Synonym.class)){
179
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInput.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
180
                return;
181
            }
182
            else{
183
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInput.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
184
                return;
185
            }
186
        }
187
		TaxonEditorInput input = TaxonEditorInput
188
				.NewInstanceFromTaxonBase(taxonBaseUuid);
189
		if(input!=null && input.getTaxonNode()!=null){
190
		    open(input);
191
		}
192
	}
193

    
194
	/**
195
	 * <p>
196
	 * findEditorByTaxonNodeUuid
197
	 * </p>
198
	 *
199
	 * @param taxonNodeUuid
200
	 *            a {@link java.util.UUID} object.
201
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
202
	 * @throws java.lang.Exception
203
	 *             if any.
204
	 */
205
	public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
206
			throws Exception {
207
		IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
208
		return getActivePage().findEditor(input);
209
	}
210

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

    
226
		getActiveMultiPageTaxonEditor().changed(null);
227

    
228
	}
229

    
230
	/**
231
	 * <p>
232
	 * setSaving
233
	 * </p>
234
	 *
235
	 * @param isSaving
236
	 *            a boolean.
237
	 */
238
	public static void setSaving(boolean isSaving) {
239
		EditorUtil.isSaving = isSaving;
240
	}
241

    
242
	/**
243
	 * <p>
244
	 * isSaving
245
	 * </p>
246
	 *
247
	 * @return a boolean.
248
	 */
249
	public static boolean isSaving() {
250
		return isSaving;
251
	}
252

    
253
	/**
254
	 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
255
	 *
256
	 * @return a {@link java.util.Set} object.
257
	 */
258
	public static Set<IEditorPart> getOpenEditors() {
259
		Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
260

    
261
		if (getActivePage() != null) {
262
			for (IEditorReference reference : getActivePage()
263
					.getEditorReferences()) {
264
				IEditorPart editor = reference.getEditor(false);
265
				if (editor instanceof MultiPageTaxonEditor) {
266
					taxonEditors.add(editor);
267
				}
268
			}
269
		}
270

    
271
		return taxonEditors;
272
	}
273

    
274
	/**
275
	 * Returns the currently active taxon editor
276
	 *
277
	 * @return the taxon editor that has focus
278
	 */
279
	public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
280
		IEditorPart editorPart = getActiveEditor();
281
		if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
282
			MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
283
			editor.getConversationHolder().bind();
284
			return editor;
285
		}
286
		return null;
287
	}
288

    
289
	/**
290
	 * <p>
291
	 * getActiveEditorPage
292
	 * </p>
293
	 *
294
	 * @param page
295
	 *            a {@link eu.etaxonomy.taxeditor.editor.Page} object.
296
	 * @return a {@link org.eclipse.ui.IEditorPart} object.
297
	 */
298
	public static IEditorPart getActiveEditorPage(Page page) {
299
		MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
300

    
301
		return editor != null ? editor.getPage(page) : null;
302
	}
303

    
304
	/**
305
	 * Returns the selection of the currently active taxon editor
306
	 *
307
	 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
308
	 */
309
	public static ISelection getCurrentSelection() {
310
		if (getActiveMultiPageTaxonEditor() == null) {
311
			return null;
312
		} else {
313
			return getActiveMultiPageTaxonEditor().getSite()
314
					.getSelectionProvider().getSelection();
315
		}
316
	}
317

    
318
	/**
319
	 * <p>
320
	 * getUndoContext
321
	 * </p>
322
	 *
323
	 * @param editor
324
	 *            a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
325
	 *            object.
326
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
327
	 *         object.
328
	 */
329
	public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
330
		return editor.getUndoContext();
331
	}
332

    
333
	/**
334
	 * <p>
335
	 * getUndoContext
336
	 * </p>
337
	 *
338
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
339
	 *         object.
340
	 */
341
	public static IUndoContext getUndoContext() {
342
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
343
	}
344

    
345
	/**
346
	 * <p>
347
	 * forceUserSave
348
	 * </p>
349
	 *
350
	 * @param editor
351
	 *            a {@link org.eclipse.ui.IEditorPart} object.
352
	 * @param shell
353
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
354
	 * @return a boolean.
355
	 */
356
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
357
		if (editor.isDirty()) {
358

    
359
			boolean doSave = MessageDialog
360
					.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
361
							Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
362

    
363
			if (!doSave) {
364
				return false;
365
			}
366

    
367
			editor.doSave(AbstractUtility.getMonitor());
368
		}
369
		return true;
370
	}
371

    
372
	/**
373
	 * <p>
374
	 * getSelection
375
	 * </p>
376
	 *
377
	 * @param event
378
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
379
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
380
	 */
381
	public static IStructuredSelection getSelection(ExecutionEvent event) {
382
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
383

    
384
		return (IStructuredSelection) activeEditor.getSite()
385
				.getSelectionProvider().getSelection();
386
	}
387

    
388
	/**
389
	 * <p>
390
	 * getPluginId
391
	 * </p>
392
	 *
393
	 * @return a {@link java.lang.String} object.
394
	 */
395
	public static String getPluginId() {
396
		return TaxeditorEditorPlugin.PLUGIN_ID;
397
	}
398

    
399
	public static void openPolytomousKey(UUID polytomousKeyUuid)
400
			throws Exception {
401
		PolytomousKeyEditorInput input = PolytomousKeyEditorInput
402
				.NewInstance(polytomousKeyUuid);
403
		open(input);
404
	}
405

    
406
//	public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
407
//	        throws Exception {
408
//	    PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
409
//	    open(input);
410
//	}
411

    
412
	public static void openCdmAuthorities(UUID groupUuid)
413
			throws Exception {
414
		CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
415
		open(input);
416
	}
417

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

    
442
    /**
443
     * If the current selection is a single {@link TreeNode} it will be returned.
444
     * @param selection the selection to check
445
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
446
     */
447
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
448
        if(selection instanceof IStructuredSelection
449
                && ((IStructuredSelection) selection).size()==1
450
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
451
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
452

    
453
        }
454
        return null;
455
    }
456

    
457
    /**
458
     * Opens a taxon editor for the given object if the given object is a valid input.
459
     * @param object the object for which the editor will be opened
460
     * @throws PartInitException
461
     */
462
    public static void openTaxonEditor(Object object) throws PartInitException {
463
        if(object instanceof TaxonBase<?>){
464
            openTaxonBase(((TaxonBase<?>) object).getUuid());
465
        }
466
    }
467

    
468
    public static boolean closeObsoleteEditor(TaxonNode taxonNode, IWorkbenchPage activePage){
469
        boolean result = true;
470
        for (IEditorReference ref : activePage.getEditorReferences()) {
471
            try {
472
                String treeIndex = ((ITreeNode)taxonNode).treeIndex();
473

    
474

    
475
                IEditorInput input = ref.getEditorInput();
476
                if (input instanceof TaxonEditorInput) {
477
                    TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
478
                    //if node is a child of taxonNode then close the editor
479
                    if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
480
                    //if (taxonNode.equals(node)) {
481
                        result &= activePage.closeEditor(ref.getEditor(false), true);
482

    
483
                    }
484
                }
485
            } catch (PartInitException e) {
486
                continue;
487
            }
488
        }
489
        return result;
490
    }
491
}
(4-4/17)