Project

General

Profile

Download (9.88 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.ui.basic.MPart;
19
import org.eclipse.e4.ui.workbench.modeling.EPartService;
20
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
21
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.viewers.ISelection;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.jface.viewers.TreeNode;
25
import org.eclipse.swt.widgets.Shell;
26
import org.eclipse.ui.IEditorPart;
27
import org.eclipse.ui.handlers.HandlerUtil;
28

    
29
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
30
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
31
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
36
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
37
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
38
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
39
import eu.etaxonomy.taxeditor.model.AbstractUtility;
40
import eu.etaxonomy.taxeditor.model.MessagingUtils;
41

    
42
/**
43
 * Utility for the editor package
44
 *
45
 * @author n.hoffmann
46
 * @created 20.01.2009
47
 * @version 1.0
48
 */
49
public class EditorUtil extends AbstractUtility {
50

    
51
    private static final String NAME_EDITOR_ID = "eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4";
52
    private static boolean isSaving = false;
53

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

    
69
	/**
70
	 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
71
	 * initialised here and is passed to the editor
72
	 *
73
	 * @param taxonNodeUuid
74
	 *            a {@link java.util.UUID} object.
75
	 * @throws java.lang.Exception
76
	 *             if any.
77
	 */
78
	public static void openTaxonNodeE4(UUID taxonNodeUuid) throws Exception {
79
	    //FIXME E4 this can probably be removed when fully migrated
80
        TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
81
        openNameEditor_internal(input);
82
	}
83

    
84
    public static void openTaxonBaseE4(UUID taxonBaseUuid) {
85
        //FIXME E4 this can probably be removed when fully migrated
86
        TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
87
        openNameEditor_internal(input);
88
    }
89

    
90
    private static void openNameEditor_internal(TaxonEditorInputE4 input) {
91
        TaxonBase taxonBase = input.getTaxon();
92

    
93
        if (taxonBase != null && taxonBase.isOrphaned()) {
94
            if(taxonBase.isInstanceOf(Synonym.class)){
95
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
96
                return;
97
            }
98
            else{
99
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
100
                return;
101
            }
102
        }
103

    
104
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
105
        Collection<MPart> parts = partService.getParts();
106
        MPart part = null;
107
        //check if part is already opened
108
        for (MPart mPart : parts) {
109
            if(mPart.getObject() instanceof TaxonNameEditorE4
110
                    && ((TaxonNameEditorE4) mPart.getObject()).getTaxon().equals(input.getTaxon())){
111
                part = mPart;
112
                break;
113
            }
114
        }
115
        if(part==null){
116
            part = partService.createPart(NAME_EDITOR_ID);
117
            part = partService.showPart(part, PartState.ACTIVATE);
118
            TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
119
            editor.init(input);
120
        }
121
        part = partService.showPart(part, PartState.ACTIVATE);
122
    }
123

    
124
	/**
125
	 * An uninitialized taxon is one that hasn't been saved yet. As such, it
126
	 * should appear in neither the list of recent names nor in the taxonomic
127
	 * tree when opened.
128
	 *
129
	 * @param parentNodeUuid
130
	 *            a {@link java.util.UUID} object.
131
	 */
132
	public static void openEmptyE4(UUID parentNodeUuid) {
133
		TaxonEditorInputE4 input = TaxonEditorInputE4
134
				.NewEmptyInstance(parentNodeUuid);
135
        EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
136
        MPart part = partService.createPart(NAME_EDITOR_ID);
137
        part = partService.showPart(part, PartState.ACTIVATE);
138
        TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
139
        editor.init(input);
140
	}
141

    
142
	/**
143
	 * <p>
144
	 * setSaving
145
	 * </p>
146
	 *
147
	 * @param isSaving
148
	 *            a boolean.
149
	 */
150
	public static void setSaving(boolean isSaving) {
151
		EditorUtil.isSaving = isSaving;
152
	}
153

    
154
	/**
155
	 * <p>
156
	 * isSaving
157
	 * </p>
158
	 *
159
	 * @return a boolean.
160
	 */
161
	public static boolean isSaving() {
162
		return isSaving;
163
	}
164

    
165
	/**
166
	 * <p>
167
	 * getUndoContext
168
	 * </p>
169
	 *
170
	 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
171
	 *         object.
172
	 */
173
	public static IUndoContext getUndoContext() {
174
		return IOperationHistory.GLOBAL_UNDO_CONTEXT;
175
	}
176

    
177
	/**
178
	 * <p>
179
	 * forceUserSave
180
	 * </p>
181
	 *
182
	 * @param editor
183
	 *            a {@link org.eclipse.ui.IEditorPart} object.
184
	 * @param shell
185
	 *            a {@link org.eclipse.swt.widgets.Shell} object.
186
	 * @return a boolean.
187
	 */
188
	public static boolean forceUserSave(IEditorPart editor, Shell shell) {
189
		if (editor.isDirty()) {
190

    
191
			boolean doSave = MessageDialog
192
					.openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
193
							Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
194

    
195
			if (!doSave) {
196
				return false;
197
			}
198

    
199
			editor.doSave(AbstractUtility.getMonitor());
200
		}
201
		return true;
202
	}
203

    
204
	public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
205
	    if (editor.isDirty()) {
206

    
207
	        boolean doSave = MessageDialog
208
	                .openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
209
	                        Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
210

    
211
	        if (!doSave) {
212
	            return false;
213
	        }
214

    
215
	        editor.save(AbstractUtility.getMonitor());
216
	    }
217
	    return true;
218
	}
219

    
220
	/**
221
	 * <p>
222
	 * getSelection
223
	 * </p>
224
	 *
225
	 * @param event
226
	 *            a {@link org.eclipse.core.commands.ExecutionEvent} object.
227
	 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
228
	 */
229
	public static IStructuredSelection getSelection(ExecutionEvent event) {
230
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
231

    
232
		return (IStructuredSelection) activeEditor.getSite()
233
				.getSelectionProvider().getSelection();
234
	}
235

    
236
	/**
237
	 * <p>
238
	 * getPluginId
239
	 * </p>
240
	 *
241
	 * @return a {@link java.lang.String} object.
242
	 */
243
	public static String getPluginId() {
244
		return TaxeditorEditorPlugin.PLUGIN_ID;
245
	}
246

    
247
	/**
248
	 * Iterates recursively over all originals having the given specimen as a derivate.
249
	 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
250
	 * @param specimen the start element for which the originals are iterated recursively
251
	 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
252
	 */
253
	public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
254
	    if(specimen.isInstanceOf(FieldUnit.class)){
255
	        return specimen;
256
	    }
257
	    else if(specimen instanceof DerivedUnit
258
	            && ((DerivedUnit) specimen).getOriginals()!=null
259
	            && !((DerivedUnit) specimen).getOriginals().isEmpty()){
260
	        for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
261
	            return getTopMostDerivate(original);
262
	        }
263
	        //needed to add this for compilation although this is unreachable
264
	        return specimen;
265
	    }
266
	    else{
267
	        return specimen;
268
	    }
269
	}
270

    
271
    /**
272
     * If the current selection is a single {@link TreeNode} it will be returned.
273
     * @param selection the selection to check
274
     * @return the selected TreeNode or <code>null</code> if no TreeNode selected
275
     */
276
    public static TreeNode getTreeNodeOfSelection(ISelection selection){
277
        if(selection instanceof IStructuredSelection
278
                && ((IStructuredSelection) selection).size()==1
279
                && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
280
            return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
281

    
282
        }
283
        return null;
284
    }
285

    
286
    public static void closeObsoleteEditor(TaxonNode taxonNode, EPartService partService){
287
        String treeIndex = taxonNode.treeIndex();
288
        Collection<MPart> parts = partService.getParts();
289
        for (MPart part : parts) {
290
            Object object = part.getObject();
291
            if(object instanceof TaxonNameEditorE4){
292
                TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
293
                TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
294
                if(node.treeIndex().startsWith(treeIndex)){
295
                    partService.hidePart(part, true);
296
                }
297
            }
298
        }
299
    }
300
}
(3-3/10)