Project

General

Profile

Download (13.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
    private static boolean isSaving = false;
65

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

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

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

    
87
    public static void openRightsEditor(CdmAuthorityEditorInput input, EModelService modelService, EPartService partService, MApplication application){
88
        String partId = AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_GROUP_AUTHORITY_E4_CDMAUTHORITYEDITORE4;
89
        MPart part = showPart(partId, modelService, partService, application);
90
        CdmAuthorityEditorE4 authorityView = (CdmAuthorityEditorE4) part.getObject();
91
        authorityView.init(input);
92
    }
93

    
94
    private static MPart showPart(String partId, EModelService modelService, EPartService partService, MApplication application){
95
        MPart part = partService.findPart(partId);
96
        if(part==null || modelService.getPartDescriptor(partId).isAllowMultiple()){
97
            part = partService.createPart(partId);
98
        }
99
        MPartStack editorAreaPartStack = WorkbenchUtility.getEditorAreaPartStack(application, modelService);
100
        if(editorAreaPartStack!=null){
101
            editorAreaPartStack.getChildren().add(part);
102
        }
103
        return partService.showPart(part, PartState.ACTIVATE);
104
    }
105

    
106
	public static void openTaxonNodeE4(UUID taxonNodeUuid, EModelService modelService, EPartService partService, MApplication application) {
107
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
108
	    openNameEditor_internal(input, modelService, partService, application);
109
	}
110

    
111
	public static void openTaxonBaseE4(UUID taxonBaseUuid, EModelService modelService, EPartService partService, MApplication application) {
112
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
113
	    openNameEditor_internal(input, modelService, partService, application);
114
	}
115

    
116
    public static void openTaxonBaseE4(UUID taxonBaseUuid) {
117
        //FIXME E4 this can probably be removed when fully migrated
118
        TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
119

    
120
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
121
        EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
122
        openNameEditor_internal(input, modelService, partService, null);
123
    }
124

    
125
    private static void openNameEditor_internal(TaxonEditorInputE4 input, EModelService modelService, EPartService partService, MApplication application) {
126
        TaxonBase taxonBase = input.getTaxon();
127
        if(taxonBase==null){
128
            return;
129
        }
130
        if (taxonBase.isOrphaned()) {
131
            if(taxonBase.isInstanceOf(Synonym.class)){
132
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
133
                return;
134
            }
135
            else{
136
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
137
                return;
138
            }
139
        }
140

    
141
        Collection<MPart> parts = partService.getParts();
142
        //check if part is already opened
143
        for (MPart part : parts) {
144
        	if(part.getObject() instanceof TaxonNameEditorE4
145
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon()!=null
146
                    && ((TaxonNameEditorE4) part.getObject()).getTaxon().getUuid().equals(input.getTaxon().getUuid())){
147
                partService.hidePart(part);
148
                break;
149
            }
150
        }
151
        MPart part = showPart(NAME_EDITOR_ID, modelService, partService, application);
152
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
153
        editor.init(input);
154
    }
155

    
156
	/**
157
	 * An uninitialized taxon is one that hasn't been saved yet. As such, it
158
	 * should appear in neither the list of recent names nor in the taxonomic
159
	 * tree when opened.
160
	 *
161
	 * @param parentNodeUuid
162
	 *            a {@link java.util.UUID} object.
163
	 */
164
	public static void openEmptyE4(UUID parentNodeUuid) {
165
		TaxonEditorInputE4 input = TaxonEditorInputE4
166
				.NewEmptyInstance(parentNodeUuid);
167
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
168
        MPart part = partService.createPart(NAME_EDITOR_ID);
169
        part = partService.showPart(part, PartState.ACTIVATE);
170
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
171
        editor.init(input);
172
	}
173

    
174
	/**
175
	 * <p>
176
	 * setSaving
177
	 * </p>
178
	 *
179
	 * @param isSaving
180
	 *            a boolean.
181
	 */
182
	public static void setSaving(boolean isSaving) {
183
		EditorUtil.isSaving = isSaving;
184
	}
185

    
186
	/**
187
	 * <p>
188
	 * isSaving
189
	 * </p>
190
	 *
191
	 * @return a boolean.
192
	 */
193
	public static boolean isSaving() {
194
		return isSaving;
195
	}
196

    
197
	/**
198
	 * <p>
199
	 * getUndoContext
200
	 * </p>
201
	 *
202
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
203
	 *         object.
204
	 */
205
	public static IUndoContext getUndoContext() {
206
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
207
	}
208

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

    
223
			boolean doSave = MessageDialog
224
					.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
225
							Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
226

    
227
			if (!doSave) {
228
				return false;
229
			}
230

    
231
			editor.doSave(AbstractUtility.getMonitor());
232
		}
233
		return true;
234
	}
235

    
236
	public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
237
	    if (editor.isDirty()) {
238

    
239
	        boolean doSave = MessageDialog
240
	                .openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
241
	                        Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
242

    
243
	        if (!doSave) {
244
	            return false;
245
	        }
246

    
247
	        editor.save(AbstractUtility.getMonitor());
248
	    }
249
	    return true;
250
	}
251

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

    
264
		return (IStructuredSelection) activeEditor.getSite()
265
				.getSelectionProvider().getSelection();
266
	}
267

    
268
	/**
269
	 * <p>
270
	 * getPluginId
271
	 * </p>
272
	 *
273
	 * @return a {@link java.lang.String} object.
274
	 */
275
	public static String getPluginId() {
276
		return TaxeditorEditorPlugin.PLUGIN_ID;
277
	}
278

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

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

    
315
        }
316
        return null;
317
    }
318

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

    
338
    public static void updateEditor(TaxonNode taxonNode, TaxonNameEditorE4 editor){
339
        String treeIndex = taxonNode.treeIndex();
340
        TaxonNode node = editor.getEditorInput().getTaxonNode();
341
        if(node.treeIndex().equals(treeIndex)){
342
        	TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(node.getUuid());
343
        	editor.init(input);
344

    
345

    
346
        }
347
    }
348
}
(4-4/8)