Project

General

Profile

Download (14 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.model.AbstractUtility;
48
import eu.etaxonomy.taxeditor.model.MessagingUtils;
49
import eu.etaxonomy.taxeditor.store.CdmStore;
50

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

    
60
	private static boolean isSaving = false;
61

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

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

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

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

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

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

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

    
145
	/**
146
	 * <p>
147
	 * openTaxonBase
148
	 * </p>
149
	 *
150
	 * @param taxonBaseUuid
151
	 *            a {@link java.util.UUID} object.
152
	 * @throws org.eclipse.ui.PartInitException
153
	 *             if any.
154
	 */
155
	public static void openTaxonBase(UUID taxonBaseUuid)
156
			throws PartInitException {
157
	    TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
158
        if (taxonBase != null && taxonBase.isOrphaned()) {
159
            if(taxonBase.isInstanceOf(Synonym.class)){
160
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInput.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
161
                return;
162
            }
163
            else{
164
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInput.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
165
                return;
166
            }
167
        }
168
		TaxonEditorInput input = TaxonEditorInput
169
				.NewInstanceFromTaxonBase(taxonBaseUuid);
170
		if(input!=null && input.getTaxonNode()!=null){
171
		    open(input);
172
		}
173
	}
174

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

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

    
207
		getActiveMultiPageTaxonEditor().changed(null);
208

    
209
	}
210

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

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

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

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

    
252
		return taxonEditors;
253
	}
254

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

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

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

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

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

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

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

    
340
			boolean doSave = MessageDialog
341
					.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
342
							Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
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 openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
388
//	        throws Exception {
389
//	    PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
390
//	    open(input);
391
//	}
392

    
393
	public static void openCdmAuthorities(UUID groupUuid)
394
			throws Exception {
395
		CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
396
		open(input);
397
	}
398

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

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

    
434
        }
435
        return null;
436
    }
437

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

    
449
    public static boolean closeObsoleteEditor(TaxonNode taxonNode, IWorkbenchPage activePage){
450
        boolean result = true;
451
        for (IEditorReference ref : activePage.getEditorReferences()) {
452
            try {
453
                String treeIndex = ((ITreeNode)taxonNode).treeIndex();
454

    
455

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

    
464
                    }
465
                }
466
            } catch (PartInitException e) {
467
                continue;
468
            }
469
        }
470
        return result;
471
    }
472
}
(4-4/17)