Project

General

Profile

Download (14.5 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
		return getActivePage().openEditor(input, editorId);
80
	}
81

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

    
95
	public static void open(PolytomousKeyEditorInput input)
96
			throws PartInitException {
97
		open(input, KeyEditor.ID);
98
	}
99

    
100
	public static void open(CdmAuthorityEditorInput input)
101
			throws PartInitException {
102
		open(input, CdmAuthorityEditor.ID);
103
	}
104

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

    
115

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

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

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

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

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

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

    
211
		getActiveMultiPageTaxonEditor().changed(null);
212

    
213
	}
214

    
215
	/**
216
	 * <p>
217
	 * setSaving
218
	 * </p>
219
	 *
220
	 * @param isSaving
221
	 *            a boolean.
222
	 */
223
	public static void setSaving(boolean isSaving) {
224
		EditorUtil.isSaving = isSaving;
225
	}
226

    
227
	/**
228
	 * <p>
229
	 * isSaving
230
	 * </p>
231
	 *
232
	 * @return a boolean.
233
	 */
234
	public static boolean isSaving() {
235
		return isSaving;
236
	}
237

    
238
	/**
239
	 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
240
	 *
241
	 * @return a {@link java.util.Set} object.
242
	 */
243
	public static Set<IEditorPart> getOpenEditors() {
244
		Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
245

    
246
		if (getActivePage() != null) {
247
			for (IEditorReference reference : getActivePage()
248
					.getEditorReferences()) {
249
				IEditorPart editor = reference.getEditor(false);
250
				if (editor instanceof MultiPageTaxonEditor) {
251
					taxonEditors.add(editor);
252
				}
253
			}
254
		}
255

    
256
		return taxonEditors;
257
	}
258

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

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

    
286
		return editor != null ? editor.getPage(page) : null;
287
	}
288

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

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

    
318
	/**
319
	 * <p>
320
	 * getUndoContext
321
	 * </p>
322
	 *
323
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
324
	 *         object.
325
	 */
326
	public static IUndoContext getUndoContext() {
327
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
328
	}
329

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

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

    
349
			if (!doSave) {
350
				return false;
351
			}
352

    
353
			editor.doSave(AbstractUtility.getMonitor());
354
		}
355
		return true;
356
	}
357

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

    
370
		return (IStructuredSelection) activeEditor.getSite()
371
				.getSelectionProvider().getSelection();
372
	}
373

    
374
	/**
375
	 * <p>
376
	 * getPluginId
377
	 * </p>
378
	 *
379
	 * @return a {@link java.lang.String} object.
380
	 */
381
	public static String getPluginId() {
382
		return TaxeditorEditorPlugin.PLUGIN_ID;
383
	}
384

    
385
	public static void openPolytomousKey(UUID polytomousKeyUuid)
386
			throws Exception {
387
		PolytomousKeyEditorInput input = PolytomousKeyEditorInput
388
				.NewInstance(polytomousKeyUuid);
389
		open(input);
390
	}
391

    
392
//	public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
393
//	        throws Exception {
394
//	    PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
395
//	    open(input);
396
//	}
397

    
398
	public static void openCdmAuthorities(UUID groupUuid)
399
			throws Exception {
400
		CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
401
		open(input);
402
	}
403

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

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

    
439
        }
440
        return null;
441
    }
442

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

    
454
    public static boolean closeObsoleteEditor(TaxonNode taxonNode, IWorkbenchPage activePage){
455
        boolean result = true;
456
        for (IEditorReference ref : activePage.getEditorReferences()) {
457
            try {
458
                String treeIndex = ((ITreeNode)taxonNode).treeIndex();
459

    
460

    
461
                IEditorInput input = ref.getEditorInput();
462
                if (input instanceof TaxonEditorInput) {
463
                    TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
464
                    //if node is a child of taxonNode then close the editor
465
                    if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
466
                    //if (taxonNode.equals(node)) {
467
                        result &= activePage.closeEditor(ref.getEditor(false), true);
468

    
469
                    }
470
                }
471
            } catch (PartInitException e) {
472
                continue;
473
            }
474
        }
475
        return result;
476
    }
477
}
(5-5/19)