Project

General

Profile

Download (9.92 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
        if(taxonBase==null){
93
            return;
94
        }
95
        if (taxonBase.isOrphaned()) {
96
            if(taxonBase.isInstanceOf(Synonym.class)){
97
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
98
                return;
99
            }
100
            else{
101
                MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
102
                return;
103
            }
104
        }
105

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

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

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

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

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

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

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

    
197
			if (!doSave) {
198
				return false;
199
			}
200

    
201
			editor.doSave(AbstractUtility.getMonitor());
202
		}
203
		return true;
204
	}
205

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

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

    
213
	        if (!doSave) {
214
	            return false;
215
	        }
216

    
217
	        editor.save(AbstractUtility.getMonitor());
218
	    }
219
	    return true;
220
	}
221

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

    
234
		return (IStructuredSelection) activeEditor.getSite()
235
				.getSelectionProvider().getSelection();
236
	}
237

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

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

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

    
284
        }
285
        return null;
286
    }
287

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