Project

General

Profile

Download (16 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.handlers.HandlerUtil;
31

    
32
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
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.cdm.persistence.dto.TaxonNodeDto;
40
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.DescriptiveDataSetEditor;
41
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrixPart;
42
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
43
import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
44
import eu.etaxonomy.taxeditor.editor.group.authority.e4.CdmAuthorityEditorE4;
45
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
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.e4.DistributionEditorPart;
49
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
50
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
51
import eu.etaxonomy.taxeditor.model.AbstractUtility;
52
import eu.etaxonomy.taxeditor.model.MessagingUtils;
53
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
54

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

    
64
    private static final String NAME_EDITOR_ID = "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
65

    
66
    public static void openDescriptiveDataSetEditor(UUID descriptiveDataSetUuid, EModelService modelService, EPartService partService, MApplication application){
67
        Collection<MPart> parts = partService.getParts();
68
        for (MPart part : parts) {
69
            if(part.getObject() instanceof DescriptiveDataSetEditor
70
                && ((DescriptiveDataSetEditor) part.getObject()).getDescriptiveDataSet().getUuid().equals(descriptiveDataSetUuid)){
71
                partService.showPart(part, PartState.ACTIVATE);
72
                return;
73
            }
74
        }
75
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVEDATASET_DESCRIPTIVEDATASETEDITOR;
76
        MPart part = showPart(partId, modelService, partService, application);
77
        DescriptiveDataSetEditor editor = (DescriptiveDataSetEditor) part.getObject();
78
        editor.init(descriptiveDataSetUuid);
79
    }
80

    
81
    public static void openCharacterMatrix(UUID descriptiveDataSetUuid, EModelService modelService, EPartService partService, MApplication application){
82
        Collection<MPart> parts = partService.getParts();
83
        for (MPart part : parts) {
84
            if(part.getObject() instanceof CharacterMatrixPart
85
                && ((CharacterMatrixPart) part.getObject()).getDescriptiveDataSet().getUuid().equals(descriptiveDataSetUuid)){
86
                partService.showPart(part, PartState.ACTIVATE);
87
                return;
88
            }
89
        }
90
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_DESCRIPTIVEDATASET_MATRIX_CHARACTERMATRIXPART;
91
        MPart part = showPart(partId, modelService, partService, application);
92
        CharacterMatrixPart editor = (CharacterMatrixPart) part.getObject();
93
        editor.init(descriptiveDataSetUuid, true);
94
    }
95

    
96
    public static void openDistributionEditor(UUID parentTaxonUuid, EModelService modelService, EPartService partService, MApplication application){
97
        Collection<MPart> parts = partService.getParts();
98
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_CHECKLIST_E4_DISTRIBUTIONEDITORPART;
99
        MPart part = showPart(partId, modelService, partService, application);
100
        DistributionEditorPart editor = (DistributionEditorPart) part.getObject();
101
        editor.init(parentTaxonUuid);
102
    }
103

    
104

    
105
    public static void openSpecimenEditor(DerivateViewEditorInput input, EModelService modelService, EPartService partService, MApplication application){
106
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW;
107
        MPart part = showPart(partId, modelService, partService, application);
108
        DerivateView derivateView = (DerivateView) part.getObject();
109
        derivateView.init(input);
110
    }
111

    
112
    public static void openRightsEditor(CdmAuthorityEditorInput input, EModelService modelService, EPartService partService, MApplication application){
113
        Collection<MPart> parts = partService.getParts();
114
        for (MPart part : parts) {
115
            if(part.getObject() instanceof CdmAuthorityEditorE4
116
                && ((CdmAuthorityEditorE4) part.getObject()).getInput().getGroup().equals(input.getGroup())){
117
                partService.showPart(part, PartState.ACTIVATE);
118
                return;
119
            }
120
        }
121
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_GROUP_AUTHORITY_E4_CDMAUTHORITYEDITORE4;
122
        MPart part = showPart(partId, modelService, partService, application);
123
        CdmAuthorityEditorE4 authorityView = (CdmAuthorityEditorE4) part.getObject();
124
        authorityView.init(input);
125
    }
126

    
127
    public static MPart showPart(String partId, EModelService modelService, EPartService partService, MApplication application){
128
        MPart part = partService.findPart(partId);
129
        if(part==null || modelService.getPartDescriptor(partId).isAllowMultiple()){
130
            part = partService.createPart(partId);
131
        }
132
        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
133
        if(editorAreaPartStack!=null){
134
            editorAreaPartStack.getChildren().add(part);
135
        }
136
        return partService.showPart(part, PartState.ACTIVATE);
137
    }
138

    
139
	public static void openTaxonNodeE4(UUID taxonNodeUuid, EModelService modelService, EPartService partService, MApplication application) {
140
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
141
	    openNameEditor_internal(input, modelService, partService, application);
142
	}
143

    
144
	public static void openTaxonBaseE4(UUID taxonBaseUuid, EModelService modelService, EPartService partService, MApplication application) {
145
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
146
	    openNameEditor_internal(input, modelService, partService, application);
147
	}
148

    
149
    public static void openTaxonBaseE4(UUID taxonBaseUuid) {
150
        //FIXME E4 this can probably be removed when fully migrated
151
        TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
152

    
153
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
154
        EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
155
        openNameEditor_internal(input, modelService, partService, null);
156
    }
157

    
158
    private static void openNameEditor_internal(TaxonEditorInputE4 input, EModelService modelService, EPartService partService, MApplication application) {
159
        TaxonBase taxonBase = input.getTaxon();
160
        if(taxonBase==null){
161
            return;
162
        }
163
        if (taxonBase.isOrphaned()) {
164
            if(taxonBase.isInstanceOf(Synonym.class)){
165
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
166
                return;
167
            }
168
            else{
169
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
170
                return;
171
            }
172
        }
173

    
174
        Collection<MPart> parts = partService.getParts();
175
        //check if part is already opened
176
        for (MPart part : parts) {
177
        	if(part.getObject() instanceof TaxonNameEditorE4
178
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
179
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon().getUuid().equals(input.getTaxon().getUuid())){
180
        	    if (part.isDirty()){
181
        	        forceUserSaveE4Editor(((TaxonNameEditorE4) part.getObject()), getShell());
182
        	    }
183
                partService.hidePart(part);
184
                break;
185
            }
186
        }
187
        MPart part = showPart(NAME_EDITOR_ID, modelService, partService, application);
188

    
189
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
190
        editor.init(input);
191
    }
192

    
193
    public static MPart checkForChanges(UUID taxonUUID, EPartService partService ){
194
        Collection<MPart> parts = partService.getParts();
195
        //check if part is already opened
196
        boolean isDirty = false;
197
        for (MPart part : parts) {
198
            if(part.getObject() instanceof TaxonNameEditorE4
199
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
200
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon().getUuid().equals(taxonUUID)){
201
                if (part.isDirty()){
202
                    return part;
203
                }
204

    
205
                break;
206
            }
207
        }
208
        return null;
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
	 * @param parentNodeUuid
217
	 *            a {@link java.util.UUID} object.
218
	 */
219
	public static void openEmptyE4(UUID parentNodeUuid) {
220
		TaxonEditorInputE4 input = TaxonEditorInputE4
221
				.NewEmptyInstance(parentNodeUuid);
222
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
223
        MPart part = partService.createPart(NAME_EDITOR_ID);
224
        part = partService.showPart(part, PartState.ACTIVATE);
225
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
226
        editor.init(input);
227
	}
228

    
229
	/**
230
	 * <p>
231
	 * getUndoContext
232
	 * </p>
233
	 *
234
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
235
	 *         object.
236
	 */
237
	public static IUndoContext getUndoContext() {
238
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
239
	}
240

    
241
	/**
242
	 * <p>
243
	 * forceUserSave
244
	 * </p>
245
	 *
246
	 * @param editor
247
	 *            a {@link org.eclipse.ui.IEditorPart} object.
248
	 * @param shell
249
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
250
	 * @return a boolean.
251
	 */
252
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
253
		if (editor.isDirty()) {
254

    
255
			boolean doSave = MessageDialog
256
					.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
257
							Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
258

    
259
			if (!doSave) {
260
				return false;
261
			}
262

    
263
			editor.doSave(AbstractUtility.getMonitor());
264
		}
265
		return true;
266
	}
267

    
268
	public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
269
	    if (editor.isDirty()) {
270

    
271
	        boolean doSave = MessageDialog
272
	                .openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
273
	                        Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
274

    
275
	        if (!doSave) {
276
	            return false;
277
	        }
278

    
279
	        editor.save(AbstractUtility.getMonitor());
280
	    }
281
	    return true;
282
	}
283

    
284
	/**
285
	 * <p>
286
	 * getSelection
287
	 * </p>
288
	 *
289
	 * @param event
290
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
291
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
292
	 */
293
	public static IStructuredSelection getSelection(ExecutionEvent event) {
294
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
295

    
296
		return (IStructuredSelection) activeEditor.getSite()
297
				.getSelectionProvider().getSelection();
298
	}
299

    
300
	/**
301
	 * <p>
302
	 * getPluginId
303
	 * </p>
304
	 *
305
	 * @return a {@link java.lang.String} object.
306
	 */
307
	public static String getPluginId() {
308
		return TaxeditorEditorPlugin.PLUGIN_ID;
309
	}
310

    
311
	/**
312
	 * Iterates recursively over all originals having the given specimen as a derivate.
313
	 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
314
	 * @param specimen the start element for which the originals are iterated recursively
315
	 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
316
	 */
317
	public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
318
	    if(specimen==null){
319
	        return null;
320
	    }
321
	    if(specimen.isInstanceOf(FieldUnit.class)){
322
	        return specimen;
323
	    }
324
	    else if(specimen.isInstanceOf(DerivedUnit.class)){
325
	        DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(specimen, DerivedUnit.class);
326
	        if(derivedUnit.getOriginals()!=null
327
	                && !(derivedUnit.getOriginals().isEmpty())){
328
	            for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
329
	                return getTopMostDerivate(original);
330
	            }
331
	        }
332
	    }
333
	    return specimen;
334
	}
335

    
336
    /**
337
     * If the current selection is a single {@link TreeNode} it will be returned.
338
     * @param selection the selection to check
339
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
340
     */
341
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
342
        if(selection instanceof IStructuredSelection
343
                && ((IStructuredSelection) selection).size()==1
344
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
345
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
346

    
347
        }
348
        return null;
349
    }
350

    
351
    public static void closeObsoleteEditor(TaxonNodeDto taxonNode, EPartService partService){
352
        String treeIndex = taxonNode.getTreeIndex();
353
        Collection<MPart> parts = partService.getParts();
354
        for (MPart part : parts) {
355
            Object object = part.getObject();
356
            if(object instanceof TaxonNameEditorE4){
357
                TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
358
                TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
359
                if (node.treeIndex()!= null){
360
	                if(node.treeIndex().startsWith(treeIndex)){
361
	                    if (part.isDirty()){
362
	                        forceUserSaveE4Editor(taxonEditor, getShell());
363
	                    }
364
	                    partService.hidePart(part);
365
	                }
366
                }else{
367
                	logger.debug("The taxonnode of taxon " + node.getTaxon().getTitleCache() + " uuid: " + node.getUuid() + " has no treeindex");;
368
                }
369
            }
370
        }
371
    }
372

    
373
    public static void updateEditor(TaxonNode taxonNode, TaxonNameEditorE4 editor){
374
        String treeIndex = taxonNode.treeIndex();
375
        TaxonNode node = editor.getEditorInput().getTaxonNode();
376
        if(node.treeIndex().equals(treeIndex)){
377
        	TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(node.getUuid());
378
        	editor.init(input);
379

    
380

    
381
        }
382
    }
383
}
(5-5/9)