Project

General

Profile

Download (12.8 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.UUID;
13

    
14
import org.eclipse.core.commands.ExecutionEvent;
15
import org.eclipse.core.commands.operations.IOperationHistory;
16
import org.eclipse.core.commands.operations.IUndoContext;
17
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18
import org.eclipse.e4.ui.workbench.modeling.EPartService;
19
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
20
import org.eclipse.jface.dialogs.MessageDialog;
21
import org.eclipse.jface.viewers.ISelection;
22
import org.eclipse.jface.viewers.IStructuredSelection;
23
import org.eclipse.jface.viewers.TreeNode;
24
import org.eclipse.swt.widgets.Shell;
25
import org.eclipse.ui.IEditorInput;
26
import org.eclipse.ui.IEditorPart;
27
import org.eclipse.ui.IEditorReference;
28
import org.eclipse.ui.IWorkbenchPage;
29
import org.eclipse.ui.PartInitException;
30
import org.eclipse.ui.handlers.HandlerUtil;
31

    
32
import eu.etaxonomy.cdm.api.service.ITaxonService;
33
import eu.etaxonomy.cdm.model.common.ITreeNode;
34
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
35
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
36
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
37
import eu.etaxonomy.cdm.model.taxon.Synonym;
38
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
41
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
42
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
43
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
44
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
45
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
46
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
47
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
48
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
49
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
50
import eu.etaxonomy.taxeditor.model.AbstractUtility;
51
import eu.etaxonomy.taxeditor.model.MessagingUtils;
52
import eu.etaxonomy.taxeditor.store.CdmStore;
53

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

    
63
	private static boolean isSaving = false;
64

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

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

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

    
96
	/**
97
	 * Opens a new ChecklistView for the given input
98
	 * @param input a {@link ChecklistEditorInput} representing the selected checklist
99
	 * @throws PartInitException
100
	 */
101
	public static void open(ChecklistEditorInput input)
102
	        throws PartInitException {
103
	    open(input, ChecklistEditor.ID);
104
	}
105

    
106
	/**
107
	 * Opens a new {@link DataImportEditor} for the given input
108
	 * @param input a {@link DataImportEditorInput}
109
	 * @throws PartInitException
110
	 */
111
//	public static void open(DataImportEditorInput<?> input)
112
//	        throws PartInitException {
113
//	    if(input instanceof BioCaseEditorInput){
114
//	        open(input, SpecimenImportEditor.ID);
115
//	    }
116
//	    else if(input instanceof GbifImportEditorInput){
117
//	        open(input, GbifImportEditor.ID);
118
//	    }
119
//	}
120

    
121
	/**
122
	 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
123
	 * initialised here and is passed to the editor
124
	 *
125
	 * @param taxonNodeUuid
126
	 *            a {@link java.util.UUID} object.
127
	 * @throws java.lang.Exception
128
	 *             if any.
129
	 */
130
	public static void openTaxonNodeE4(UUID taxonNodeUuid) throws Exception {
131
	    //FIXME E4 this can probably be removed when fully migrated
132
        TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
133
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
134
        MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
135
        partService.showPart(part, PartState.ACTIVATE);
136
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
137
        editor.init(input);
138
	}
139

    
140
	public static void openTaxonBaseE4(UUID taxonBaseUuid) throws PartInitException{
141
	    //FIXME E4 this can probably be removed when fully migrated
142
	    TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
143

    
144
	    if (taxonBase != null && taxonBase.isOrphaned()) {
145
	        if(taxonBase.isInstanceOf(Synonym.class)){
146
	            MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
147
	            return;
148
	        }
149
	        else{
150
	            MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
151
	            return;
152
	        }
153
	    }
154

    
155
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
156
	    EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
157
	    MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
158
	    partService.showPart(part, PartState.ACTIVATE);
159
	    TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
160
	    editor.init(input);
161
	}
162

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

    
180
	/**
181
	 * An uninitialized taxon is one that hasn't been saved yet. As such, it
182
	 * should appear in neither the list of recent names nor in the taxonomic
183
	 * tree when opened.
184
	 *
185
	 * @throws org.eclipse.ui.PartInitException
186
	 *             if any.
187
	 * @param parentNodeUuid
188
	 *            a {@link java.util.UUID} object.
189
	 */
190
	public static void openEmptyE4(UUID parentNodeUuid) throws PartInitException {
191
		TaxonEditorInputE4 input = TaxonEditorInputE4
192
				.NewEmptyInstance(parentNodeUuid);
193
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
194
        MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
195
        partService.showPart(part, PartState.ACTIVATE);
196
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
197
        editor.init(input);
198
	}
199

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

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

    
223
	/**
224
	 * <p>
225
	 * getUndoContext
226
	 * </p>
227
	 *
228
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
229
	 *         object.
230
	 */
231
	public static IUndoContext getUndoContext() {
232
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
233
	}
234

    
235
	/**
236
	 * <p>
237
	 * forceUserSave
238
	 * </p>
239
	 *
240
	 * @param editor
241
	 *            a {@link org.eclipse.ui.IEditorPart} object.
242
	 * @param shell
243
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
244
	 * @return a boolean.
245
	 */
246
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
247
		if (editor.isDirty()) {
248

    
249
			boolean doSave = MessageDialog
250
					.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
251
							Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
252

    
253
			if (!doSave) {
254
				return false;
255
			}
256

    
257
			editor.doSave(AbstractUtility.getMonitor());
258
		}
259
		return true;
260
	}
261

    
262
	public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
263
	    if (editor.isDirty()) {
264

    
265
	        boolean doSave = MessageDialog
266
	                .openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
267
	                        Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
268

    
269
	        if (!doSave) {
270
	            return false;
271
	        }
272

    
273
	        editor.save(AbstractUtility.getMonitor());
274
	    }
275
	    return true;
276
	}
277

    
278
	/**
279
	 * <p>
280
	 * getSelection
281
	 * </p>
282
	 *
283
	 * @param event
284
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
285
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
286
	 */
287
	public static IStructuredSelection getSelection(ExecutionEvent event) {
288
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
289

    
290
		return (IStructuredSelection) activeEditor.getSite()
291
				.getSelectionProvider().getSelection();
292
	}
293

    
294
	/**
295
	 * <p>
296
	 * getPluginId
297
	 * </p>
298
	 *
299
	 * @return a {@link java.lang.String} object.
300
	 */
301
	public static String getPluginId() {
302
		return TaxeditorEditorPlugin.PLUGIN_ID;
303
	}
304

    
305
	public static void openPolytomousKey(UUID polytomousKeyUuid)
306
			throws Exception {
307
		PolytomousKeyEditorInput input = PolytomousKeyEditorInput
308
				.NewInstance(polytomousKeyUuid);
309
		open(input);
310
	}
311

    
312
//	public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
313
//	        throws Exception {
314
//	    PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
315
//	    open(input);
316
//	}
317

    
318
	public static void openCdmAuthorities(UUID groupUuid)
319
			throws Exception {
320
		CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
321
		open(input);
322
	}
323

    
324
	/**
325
	 * Iterates recursively over all originals having the given specimen as a derivate.
326
	 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
327
	 * @param specimen the start element for which the originals are iterated recursively
328
	 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
329
	 */
330
	public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
331
	    if(specimen.isInstanceOf(FieldUnit.class)){
332
	        return specimen;
333
	    }
334
	    else if(specimen instanceof DerivedUnit
335
	            && ((DerivedUnit) specimen).getOriginals()!=null
336
	            && !((DerivedUnit) specimen).getOriginals().isEmpty()){
337
	        for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
338
	            return getTopMostDerivate(original);
339
	        }
340
	        //needed to add this for compilation although this is unreachable
341
	        return specimen;
342
	    }
343
	    else{
344
	        return specimen;
345
	    }
346
	}
347

    
348
    /**
349
     * If the current selection is a single {@link TreeNode} it will be returned.
350
     * @param selection the selection to check
351
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
352
     */
353
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
354
        if(selection instanceof IStructuredSelection
355
                && ((IStructuredSelection) selection).size()==1
356
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
357
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
358

    
359
        }
360
        return null;
361
    }
362

    
363
    public static boolean closeObsoleteEditor(TaxonNode taxonNode, IWorkbenchPage activePage){
364
        boolean result = true;
365
        for (IEditorReference ref : activePage.getEditorReferences()) {
366
            try {
367
                String treeIndex = ((ITreeNode)taxonNode).treeIndex();
368

    
369

    
370
                IEditorInput input = ref.getEditorInput();
371
                if (input instanceof TaxonEditorInput) {
372
                    TaxonNode node = ((TaxonEditorInput) input).getTaxonNode();
373
                    //if node is a child of taxonNode then close the editor
374
                    if( ((ITreeNode) node).treeIndex().startsWith(treeIndex)){
375
                    //if (taxonNode.equals(node)) {
376
                        result &= activePage.closeEditor(ref.getEditor(false), true);
377

    
378
                    }
379
                }
380
            } catch (PartInitException e) {
381
                continue;
382
            }
383
        }
384
        return result;
385
    }
386
}
(3-3/10)