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.Collection;
13
import java.util.UUID;
14

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

    
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.e4.TaxonEditorInputE4;
40
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
41
import eu.etaxonomy.taxeditor.editor.group.authority.e4.CdmAuthorityEditorE4;
42
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
43
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
44
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
45
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
46
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
47
import eu.etaxonomy.taxeditor.editor.workingSet.WorkingSetEditor;
48
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.CharacterMatrix;
49
import eu.etaxonomy.taxeditor.model.AbstractUtility;
50
import eu.etaxonomy.taxeditor.model.MessagingUtils;
51
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
52

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

    
62
    private static final String NAME_EDITOR_ID = "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
63
    private static boolean isSaving = false;
64

    
65
	/**
66
	 * Opens a new {@link DataImportEditor} for the given input
67
	 * @param input a {@link DataImportEditorInput}
68
	 * @throws PartInitException
69
	 */
70
//	public static void open(DataImportEditorInput<?> input)
71
//	        throws PartInitException {
72
//	    if(input instanceof BioCaseEditorInput){
73
//	        open(input, SpecimenImportEditor.ID);
74
//	    }
75
//	    else if(input instanceof GbifImportEditorInput){
76
//	        open(input, GbifImportEditor.ID);
77
//	    }
78
//	}
79

    
80
    public static void openWorkingSetEditor(UUID workingSetUuid, EModelService modelService, EPartService partService, MApplication application){
81
        MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.view.workingSet.WorkingSetEditor");
82
        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
83
        if(editorAreaPartStack!=null){
84
            editorAreaPartStack.getChildren().add(part);
85
        }
86
        part = partService.showPart(part, PartState.ACTIVATE);
87
        WorkingSetEditor editor = (WorkingSetEditor) part.getObject();
88
        editor.init(workingSetUuid);
89
    }
90

    
91
    public static void openCharacterMatrix(UUID workingSetUuid, EModelService modelService, EPartService partService, MApplication application){
92
        MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.workingSet.matrix.CharacterMatrix");
93
        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
94
        if(editorAreaPartStack!=null){
95
            editorAreaPartStack.getChildren().add(part);
96
        }
97
        part = partService.showPart(part, PartState.ACTIVATE);
98
        CharacterMatrix editor = (CharacterMatrix) part.getObject();
99
        editor.init(workingSetUuid);
100
    }
101

    
102
    public static void openSpecimenEditor(DerivateViewEditorInput input, EModelService modelService, EPartService partService, MApplication application){
103
        MPart part = partService.createPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW);
104
        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
105
        if(editorAreaPartStack!=null){
106
            editorAreaPartStack.getChildren().add(part);
107
        }
108
        part = partService.showPart(part, PartState.ACTIVATE);
109
        DerivateView derivateView = (DerivateView) part.getObject();
110
        derivateView.init(input);
111
    }
112

    
113
	public static void openTaxonNodeE4(UUID taxonNodeUuid, EModelService modelService, EPartService partService, MApplication application) {
114
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
115
	    openNameEditor_internal(input, modelService, partService, application);
116
	}
117

    
118
	public static void openTaxonBaseE4(UUID taxonBaseUuid, EModelService modelService, EPartService partService, MApplication application) {
119
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
120
	    openNameEditor_internal(input, modelService, partService, application);
121
	}
122

    
123
    public static void openTaxonBaseE4(UUID taxonBaseUuid) {
124
        //FIXME E4 this can probably be removed when fully migrated
125
        TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
126

    
127
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
128
        EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
129
        openNameEditor_internal(input, modelService, partService, null);
130
    }
131

    
132
    private static void openNameEditor_internal(TaxonEditorInputE4 input, EModelService modelService, EPartService partService, MApplication application) {
133
        TaxonBase taxonBase = input.getTaxon();
134
        if(taxonBase==null){
135
            return;
136
        }
137
        if (taxonBase.isOrphaned()) {
138
            if(taxonBase.isInstanceOf(Synonym.class)){
139
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
140
                return;
141
            }
142
            else{
143
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
144
                return;
145
            }
146
        }
147

    
148
        Collection<MPart> parts = partService.getParts();
149
        //check if part is already opened
150
        for (MPart part : parts) {
151
        	if(part.getObject() instanceof TaxonNameEditorE4
152
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
153
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon().getUuid().equals(input.getTaxon().getUuid())){
154
                partService.hidePart(part);
155
                break;
156
            }
157
        }
158
        MPart part = partService.createPart(NAME_EDITOR_ID);
159

    
160
        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
161
        if(editorAreaPartStack!=null){
162
            editorAreaPartStack.getChildren().add(part);
163
        }
164
        part = partService.showPart(part, PartState.ACTIVATE);
165

    
166
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
167
        editor.init(input);
168
    }
169
    public static void openRightsEditor(CdmAuthorityEditorInput input, EModelService modelService, EPartService partService, MApplication application){
170
        MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.group.authority.e4.CdmAuthorityEditorE4");
171
        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
172
        if(editorAreaPartStack!=null){
173
            editorAreaPartStack.getChildren().add(part);
174
        }
175
        part = partService.showPart(part, PartState.ACTIVATE);
176
        CdmAuthorityEditorE4 authorityView = (CdmAuthorityEditorE4) part.getObject();
177
        authorityView.init(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
	 * @param parentNodeUuid
186
	 *            a {@link java.util.UUID} object.
187
	 */
188
	public static void openEmptyE4(UUID parentNodeUuid) {
189
		TaxonEditorInputE4 input = TaxonEditorInputE4
190
				.NewEmptyInstance(parentNodeUuid);
191
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
192
        MPart part = partService.createPart(NAME_EDITOR_ID);
193
        part = partService.showPart(part, PartState.ACTIVATE);
194
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
195
        editor.init(input);
196
	}
197

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

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

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

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

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

    
251
			if (!doSave) {
252
				return false;
253
			}
254

    
255
			editor.doSave(AbstractUtility.getMonitor());
256
		}
257
		return true;
258
	}
259

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

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

    
267
	        if (!doSave) {
268
	            return false;
269
	        }
270

    
271
	        editor.save(AbstractUtility.getMonitor());
272
	    }
273
	    return true;
274
	}
275

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

    
288
		return (IStructuredSelection) activeEditor.getSite()
289
				.getSelectionProvider().getSelection();
290
	}
291

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

    
303
	/**
304
	 * Iterates recursively over all originals having the given specimen as a derivate.
305
	 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
306
	 * @param specimen the start element for which the originals are iterated recursively
307
	 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
308
	 */
309
	public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
310
	    if(specimen.isInstanceOf(FieldUnit.class)){
311
	        return specimen;
312
	    }
313
	    else if(specimen instanceof DerivedUnit
314
	            && ((DerivedUnit) specimen).getOriginals()!=null
315
	            && !((DerivedUnit) specimen).getOriginals().isEmpty()){
316
	        for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
317
	            return getTopMostDerivate(original);
318
	        }
319
	        //needed to add this for compilation although this is unreachable
320
	        return specimen;
321
	    }
322
	    else{
323
	        return specimen;
324
	    }
325
	}
326

    
327
    /**
328
     * If the current selection is a single {@link TreeNode} it will be returned.
329
     * @param selection the selection to check
330
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
331
     */
332
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
333
        if(selection instanceof IStructuredSelection
334
                && ((IStructuredSelection) selection).size()==1
335
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
336
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
337

    
338
        }
339
        return null;
340
    }
341

    
342
    public static void closeObsoleteEditor(TaxonNode taxonNode, EPartService partService){
343
        String treeIndex = taxonNode.treeIndex();
344
        Collection<MPart> parts = partService.getParts();
345
        for (MPart part : parts) {
346
            Object object = part.getObject();
347
            if(object instanceof TaxonNameEditorE4){
348
                TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
349
                TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
350
                if (node.treeIndex()!= null){
351
	                if(node.treeIndex().startsWith(treeIndex)){
352
	                    partService.hidePart(part, true);
353
	                }
354
                }else{
355
                	logger.debug("The taxonnode of taxon " + node.getTaxon().getTitleCache() + " uuid: " + node.getUuid() + " has no treeindex");;
356
                }
357
            }
358
        }
359
    }
360
    
361
    public static void updateEditor(TaxonNode taxonNode, EPartService partService, MApplication application){
362
        String treeIndex = taxonNode.treeIndex();
363
        Collection<MPart> parts = partService.getParts();
364
        for (MPart part : parts) {
365
            Object object = part.getObject();
366
            if(object instanceof TaxonNameEditorE4){
367
                TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
368
                TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
369
                if(node.treeIndex().startsWith(treeIndex)){
370
                	TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(node.getTaxon().getUuid());
371
                	taxonEditor.init(input);
372
                }
373
            }
374
        }
375
    }
376
}
(4-4/11)