Project

General

Profile

Download (14.6 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.derivate.DerivateView;
49
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
50
import eu.etaxonomy.taxeditor.model.AbstractUtility;
51
import eu.etaxonomy.taxeditor.model.MessagingUtils;
52
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
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 final String NAME_EDITOR_ID = "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
64

    
65
    public static void openDescriptiveDataSetEditor(UUID descriptiveDataSetUuid, EModelService modelService, EPartService partService, MApplication application){
66
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DESCRIPTIVEDATASET_DESCRIPTIVEDATASETEDITOR;
67
        MPart part = showPart(partId, modelService, partService, application);
68
        DescriptiveDataSetEditor editor = (DescriptiveDataSetEditor) part.getObject();
69
        editor.init(descriptiveDataSetUuid);
70
    }
71

    
72
    public static void openCharacterMatrix(UUID descriptiveDataSetUuid, EModelService modelService, EPartService partService, MApplication application){
73
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_DESCRIPTIVEDATASET_MATRIX_CHARACTERMATRIXPART;
74
        MPart part = showPart(partId, modelService, partService, application);
75
        CharacterMatrixPart editor = (CharacterMatrixPart) part.getObject();
76
        editor.init(descriptiveDataSetUuid, true);
77
    }
78

    
79
    public static void openSpecimenEditor(DerivateViewEditorInput input, EModelService modelService, EPartService partService, MApplication application){
80
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW;
81
        MPart part = showPart(partId, modelService, partService, application);
82
        DerivateView derivateView = (DerivateView) part.getObject();
83
        derivateView.init(input);
84
    }
85

    
86
    public static void openRightsEditor(CdmAuthorityEditorInput input, EModelService modelService, EPartService partService, MApplication application){
87
        Collection<MPart> parts = partService.getParts();
88
        for (MPart part : parts) {
89
            if(part.getObject() instanceof CdmAuthorityEditorE4
90
                && ((CdmAuthorityEditorE4) part.getObject()).getInput().getGroup().equals(input.getGroup())){
91
                partService.showPart(part, PartState.ACTIVATE);
92
                return;
93
            }
94
        }
95
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_GROUP_AUTHORITY_E4_CDMAUTHORITYEDITORE4;
96
        MPart part = showPart(partId, modelService, partService, application);
97
        CdmAuthorityEditorE4 authorityView = (CdmAuthorityEditorE4) part.getObject();
98
        authorityView.init(input);
99
    }
100

    
101
    public static MPart showPart(String partId, EModelService modelService, EPartService partService, MApplication application){
102
        MPart part = partService.findPart(partId);
103
        if(part==null || modelService.getPartDescriptor(partId).isAllowMultiple()){
104
            part = partService.createPart(partId);
105
        }
106
        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
107
        if(editorAreaPartStack!=null){
108
            editorAreaPartStack.getChildren().add(part);
109
        }
110
        return partService.showPart(part, PartState.ACTIVATE);
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
        	    if (part.isDirty()){
155
        	        forceUserSaveE4Editor(((TaxonNameEditorE4) part.getObject()), getShell());
156
        	    }
157
                partService.hidePart(part);
158
                break;
159
            }
160
        }
161
        MPart part = showPart(NAME_EDITOR_ID, modelService, partService, application);
162
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
163
        editor.init(input);
164
    }
165

    
166
    public static MPart checkForChanges(UUID taxonUUID, EPartService partService ){
167
        Collection<MPart> parts = partService.getParts();
168
        //check if part is already opened
169
        boolean isDirty = false;
170
        for (MPart part : parts) {
171
            if(part.getObject() instanceof TaxonNameEditorE4
172
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
173
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon().getUuid().equals(taxonUUID)){
174
                if (part.isDirty()){
175
                    return part;
176
                }
177

    
178
                break;
179
            }
180
        }
181
        return null;
182
    }
183

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

    
202
	/**
203
	 * <p>
204
	 * getUndoContext
205
	 * </p>
206
	 *
207
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
208
	 *         object.
209
	 */
210
	public static IUndoContext getUndoContext() {
211
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
212
	}
213

    
214
	/**
215
	 * <p>
216
	 * forceUserSave
217
	 * </p>
218
	 *
219
	 * @param editor
220
	 *            a {@link org.eclipse.ui.IEditorPart} object.
221
	 * @param shell
222
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
223
	 * @return a boolean.
224
	 */
225
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
226
		if (editor.isDirty()) {
227

    
228
			boolean doSave = MessageDialog
229
					.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
230
							Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
231

    
232
			if (!doSave) {
233
				return false;
234
			}
235

    
236
			editor.doSave(AbstractUtility.getMonitor());
237
		}
238
		return true;
239
	}
240

    
241
	public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
242
	    if (editor.isDirty()) {
243

    
244
	        boolean doSave = MessageDialog
245
	                .openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
246
	                        Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
247

    
248
	        if (!doSave) {
249
	            return false;
250
	        }
251

    
252
	        editor.save(AbstractUtility.getMonitor());
253
	    }
254
	    return true;
255
	}
256

    
257
	/**
258
	 * <p>
259
	 * getSelection
260
	 * </p>
261
	 *
262
	 * @param event
263
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
264
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
265
	 */
266
	public static IStructuredSelection getSelection(ExecutionEvent event) {
267
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
268

    
269
		return (IStructuredSelection) activeEditor.getSite()
270
				.getSelectionProvider().getSelection();
271
	}
272

    
273
	/**
274
	 * <p>
275
	 * getPluginId
276
	 * </p>
277
	 *
278
	 * @return a {@link java.lang.String} object.
279
	 */
280
	public static String getPluginId() {
281
		return TaxeditorEditorPlugin.PLUGIN_ID;
282
	}
283

    
284
	/**
285
	 * Iterates recursively over all originals having the given specimen as a derivate.
286
	 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
287
	 * @param specimen the start element for which the originals are iterated recursively
288
	 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
289
	 */
290
	public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
291
	    if(specimen==null){
292
	        return null;
293
	    }
294
	    if(specimen.isInstanceOf(FieldUnit.class)){
295
	        return specimen;
296
	    }
297
	    else if(specimen.isInstanceOf(DerivedUnit.class)){
298
	        DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(specimen, DerivedUnit.class);
299
	        if(derivedUnit.getOriginals()!=null
300
	                && !(derivedUnit.getOriginals().isEmpty())){
301
	            for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
302
	                return getTopMostDerivate(original);
303
	            }
304
	        }
305
	    }
306
	    return specimen;
307
	}
308

    
309
    /**
310
     * If the current selection is a single {@link TreeNode} it will be returned.
311
     * @param selection the selection to check
312
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
313
     */
314
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
315
        if(selection instanceof IStructuredSelection
316
                && ((IStructuredSelection) selection).size()==1
317
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
318
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
319

    
320
        }
321
        return null;
322
    }
323

    
324
    public static void closeObsoleteEditor(TaxonNodeDto taxonNode, EPartService partService){
325
        String treeIndex = taxonNode.getTreeIndex();
326
        Collection<MPart> parts = partService.getParts();
327
        for (MPart part : parts) {
328
            Object object = part.getObject();
329
            if(object instanceof TaxonNameEditorE4){
330
                TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
331
                TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
332
                if (node.treeIndex()!= null){
333
	                if(node.treeIndex().startsWith(treeIndex)){
334
	                    if (part.isDirty()){
335
	                        forceUserSaveE4Editor(taxonEditor, getShell());
336
	                    }
337
	                    partService.hidePart(part);
338
	                }
339
                }else{
340
                	logger.debug("The taxonnode of taxon " + node.getTaxon().getTitleCache() + " uuid: " + node.getUuid() + " has no treeindex");;
341
                }
342
            }
343
        }
344
    }
345

    
346
    public static void updateEditor(TaxonNode taxonNode, TaxonNameEditorE4 editor){
347
        String treeIndex = taxonNode.treeIndex();
348
        TaxonNode node = editor.getEditorInput().getTaxonNode();
349
        if(node.treeIndex().equals(treeIndex)){
350
        	TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(node.getUuid());
351
        	editor.init(input);
352

    
353

    
354
        }
355
    }
356
}
(5-5/9)