Project

General

Profile

Download (11 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.workbench.modeling.EModelService;
21
import org.eclipse.e4.ui.workbench.modeling.EPartService;
22
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
23
import org.eclipse.jface.dialogs.MessageDialog;
24
import org.eclipse.jface.viewers.ISelection;
25
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.TreeNode;
27
import org.eclipse.swt.widgets.Shell;
28
import org.eclipse.ui.IEditorPart;
29
import org.eclipse.ui.PartInitException;
30
import org.eclipse.ui.handlers.HandlerUtil;
31

    
32
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
33
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
34
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
35
import eu.etaxonomy.cdm.model.taxon.Synonym;
36
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
37
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
39
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
40
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
41
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
42
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
43
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
44
import eu.etaxonomy.taxeditor.model.AbstractUtility;
45
import eu.etaxonomy.taxeditor.model.MessagingUtils;
46

    
47
/**
48
 * Utility for the editor package
49
 *
50
 * @author n.hoffmann
51
 * @created 20.01.2009
52
 * @version 1.0
53
 */
54
public class EditorUtil extends AbstractUtility {
55

    
56
    private static final String NAME_EDITOR_ID = "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
57
    private static boolean isSaving = false;
58

    
59
	/**
60
	 * Opens a new {@link DataImportEditor} for the given input
61
	 * @param input a {@link DataImportEditorInput}
62
	 * @throws PartInitException
63
	 */
64
//	public static void open(DataImportEditorInput<?> input)
65
//	        throws PartInitException {
66
//	    if(input instanceof BioCaseEditorInput){
67
//	        open(input, SpecimenImportEditor.ID);
68
//	    }
69
//	    else if(input instanceof GbifImportEditorInput){
70
//	        open(input, GbifImportEditor.ID);
71
//	    }
72
//	}
73

    
74
    public static void openSpecimenEditor(DerivateViewEditorInput input, EModelService modelService, EPartService partService, MApplication application){
75
        MPart part = partService.showPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_EDITOR_VIEW_DERIVATE_DERIVATEVIEW, PartState.ACTIVATE);
76
        DerivateView derivateView = (DerivateView) part.getObject();
77
        derivateView.init(input);
78
    }
79

    
80
	public static void openTaxonNodeE4(UUID taxonNodeUuid, EModelService modelService, EPartService partService, MApplication application) throws Exception {
81
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
82
	    openNameEditor_internal(input, modelService, partService, application);
83
	}
84

    
85
	public static void openTaxonBaseE4(UUID taxonBaseUuid, EModelService modelService, EPartService partService, MApplication application) {
86
	    TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
87
	    openNameEditor_internal(input, modelService, partService, application);
88
	}
89

    
90
    public static void openTaxonBaseE4(UUID taxonBaseUuid) {
91
        //FIXME E4 this can probably be removed when fully migrated
92
        TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
93

    
94
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
95
        EModelService modelService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EModelService.class);
96
        openNameEditor_internal(input, modelService, partService, null);
97
    }
98

    
99
    private static void openNameEditor_internal(TaxonEditorInputE4 input, EModelService modelService, EPartService partService, MApplication application) {
100
        TaxonBase taxonBase = input.getTaxon();
101
        if(taxonBase==null){
102
            return;
103
        }
104
        if (taxonBase.isOrphaned()) {
105
            if(taxonBase.isInstanceOf(Synonym.class)){
106
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
107
                return;
108
            }
109
            else{
110
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
111
                return;
112
            }
113
        }
114

    
115
        Collection<MPart> parts = partService.getParts();
116
        MPart part = null;
117
        //check if part is already opened
118
        for (MPart mPart : parts) {
119
            if(mPart.getObject() instanceof TaxonNameEditorE4
120
                    && ((TaxonNameEditorE4) mPart.getObject()).getTaxon()!=null
121
                    && ((TaxonNameEditorE4) mPart.getObject()).getTaxon().equals(input.getTaxon())){
122
                part = mPart;
123
                break;
124
            }
125
        }
126
        if(part==null){
127
            part = partService.showPart(NAME_EDITOR_ID, PartState.ACTIVATE);
128

    
129
            TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
130
            editor.init(input);
131
        }
132
        else{
133
            part = partService.showPart(part, PartState.ACTIVATE);
134
        }
135
    }
136

    
137
	/**
138
	 * An uninitialized taxon is one that hasn't been saved yet. As such, it
139
	 * should appear in neither the list of recent names nor in the taxonomic
140
	 * tree when opened.
141
	 *
142
	 * @param parentNodeUuid
143
	 *            a {@link java.util.UUID} object.
144
	 */
145
	public static void openEmptyE4(UUID parentNodeUuid) {
146
		TaxonEditorInputE4 input = TaxonEditorInputE4
147
				.NewEmptyInstance(parentNodeUuid);
148
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
149
        MPart part = partService.createPart(NAME_EDITOR_ID);
150
        part = partService.showPart(part, PartState.ACTIVATE);
151
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
152
        editor.init(input);
153
	}
154

    
155
	/**
156
	 * <p>
157
	 * setSaving
158
	 * </p>
159
	 *
160
	 * @param isSaving
161
	 *            a boolean.
162
	 */
163
	public static void setSaving(boolean isSaving) {
164
		EditorUtil.isSaving = isSaving;
165
	}
166

    
167
	/**
168
	 * <p>
169
	 * isSaving
170
	 * </p>
171
	 *
172
	 * @return a boolean.
173
	 */
174
	public static boolean isSaving() {
175
		return isSaving;
176
	}
177

    
178
	/**
179
	 * <p>
180
	 * getUndoContext
181
	 * </p>
182
	 *
183
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
184
	 *         object.
185
	 */
186
	public static IUndoContext getUndoContext() {
187
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
188
	}
189

    
190
	/**
191
	 * <p>
192
	 * forceUserSave
193
	 * </p>
194
	 *
195
	 * @param editor
196
	 *            a {@link org.eclipse.ui.IEditorPart} object.
197
	 * @param shell
198
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
199
	 * @return a boolean.
200
	 */
201
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
202
		if (editor.isDirty()) {
203

    
204
			boolean doSave = MessageDialog
205
					.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
206
							Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
207

    
208
			if (!doSave) {
209
				return false;
210
			}
211

    
212
			editor.doSave(AbstractUtility.getMonitor());
213
		}
214
		return true;
215
	}
216

    
217
	public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
218
	    if (editor.isDirty()) {
219

    
220
	        boolean doSave = MessageDialog
221
	                .openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
222
	                        Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
223

    
224
	        if (!doSave) {
225
	            return false;
226
	        }
227

    
228
	        editor.save(AbstractUtility.getMonitor());
229
	    }
230
	    return true;
231
	}
232

    
233
	/**
234
	 * <p>
235
	 * getSelection
236
	 * </p>
237
	 *
238
	 * @param event
239
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
240
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
241
	 */
242
	public static IStructuredSelection getSelection(ExecutionEvent event) {
243
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
244

    
245
		return (IStructuredSelection) activeEditor.getSite()
246
				.getSelectionProvider().getSelection();
247
	}
248

    
249
	/**
250
	 * <p>
251
	 * getPluginId
252
	 * </p>
253
	 *
254
	 * @return a {@link java.lang.String} object.
255
	 */
256
	public static String getPluginId() {
257
		return TaxeditorEditorPlugin.PLUGIN_ID;
258
	}
259

    
260
	/**
261
	 * Iterates recursively over all originals having the given specimen as a derivate.
262
	 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
263
	 * @param specimen the start element for which the originals are iterated recursively
264
	 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
265
	 */
266
	public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
267
	    if(specimen.isInstanceOf(FieldUnit.class)){
268
	        return specimen;
269
	    }
270
	    else if(specimen instanceof DerivedUnit
271
	            && ((DerivedUnit) specimen).getOriginals()!=null
272
	            && !((DerivedUnit) specimen).getOriginals().isEmpty()){
273
	        for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
274
	            return getTopMostDerivate(original);
275
	        }
276
	        //needed to add this for compilation although this is unreachable
277
	        return specimen;
278
	    }
279
	    else{
280
	        return specimen;
281
	    }
282
	}
283

    
284
    /**
285
     * If the current selection is a single {@link TreeNode} it will be returned.
286
     * @param selection the selection to check
287
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
288
     */
289
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
290
        if(selection instanceof IStructuredSelection
291
                && ((IStructuredSelection) selection).size()==1
292
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
293
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
294

    
295
        }
296
        return null;
297
    }
298

    
299
    public static void closeObsoleteEditor(TaxonNode taxonNode, EPartService partService){
300
        String treeIndex = taxonNode.treeIndex();
301
        Collection<MPart> parts = partService.getParts();
302
        for (MPart part : parts) {
303
            Object object = part.getObject();
304
            if(object instanceof TaxonNameEditorE4){
305
                TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
306
                TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
307
                if(node.treeIndex().startsWith(treeIndex)){
308
                    partService.hidePart(part, true);
309
                }
310
            }
311
        }
312
    }
313
}
(4-4/11)