Example object demonstrating pherogram and alignment data to be stored in CDM added.
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
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.HashSet;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.operations.IOperationHistory;
18 import org.eclipse.core.commands.operations.IUndoContext;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.TreeNode;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IEditorReference;
27 import org.eclipse.ui.PartInitException;
28 import org.eclipse.ui.handlers.HandlerUtil;
29
30 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
31 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
32 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
33 import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
34 import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
35 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
36 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
37 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
38 import eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditor;
39 import eu.etaxonomy.taxeditor.editor.molecular.AlignmentEditorInput;
40 import eu.etaxonomy.taxeditor.editor.view.dataimport.BioCaseEditorInput;
41 import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditor;
42 import eu.etaxonomy.taxeditor.editor.view.dataimport.DataImportEditorInput;
43 import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditor;
44 import eu.etaxonomy.taxeditor.editor.view.dataimport.GbifImportEditorInput;
45 import eu.etaxonomy.taxeditor.editor.view.dataimport.SpecimenImportEditor;
46 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
47 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
48 import eu.etaxonomy.taxeditor.model.AbstractUtility;
49
50 /**
51 * Utility for the editor package
52 *
53 * @author n.hoffmann
54 * @created 20.01.2009
55 * @version 1.0
56 */
57 public class EditorUtil extends AbstractUtility {
58
59 private static boolean isSaving = false;
60
61 /**
62 * Opens a new editor window with the given input
63 *
64 * @param input
65 * @param editorId
66 * @return
67 * @return
68 * @throws PartInitException
69 */
70 private static IEditorPart open(final IEditorInput input,
71 final String editorId) throws PartInitException {
72 return getActivePage().openEditor(input, editorId);
73 }
74
75 /**
76 * Opens a new editor window with the given TaxonEditorInput
77 *
78 * @param input
79 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
80 * object.
81 * @throws org.eclipse.ui.PartInitException
82 * if any.
83 */
84 public static void open(TaxonEditorInput input) throws PartInitException {
85 open(input, MultiPageTaxonEditor.ID);
86 }
87
88 public static void open(PolytomousKeyEditorInput input)
89 throws PartInitException {
90 open(input, KeyEditor.ID);
91 }
92
93 public static void open(CdmAuthorityEditorInput input)
94 throws PartInitException {
95 open(input, CdmAuthorityEditor.ID);
96 }
97
98 /**
99 * Opens a new DerivateView for the given input
100 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
101 * @throws PartInitException
102 */
103 public static void open(DerivateViewEditorInput input)
104 throws PartInitException {
105 open(input, DerivateView.ID);
106 }
107
108 /**
109 * Opens a new AlignmentEditor for the given input
110 * @param input
111 * @throws PartInitException
112 */
113 public static void open(AlignmentEditorInput input)
114 throws PartInitException {
115 open(input, AlignmentEditor.ID);
116 }
117
118 /**
119 * Opens a new {@link DataImportEditor} for the given input
120 * @param input a {@link DataImportEditorInput}
121 * @throws PartInitException
122 */
123 public static void open(DataImportEditorInput<?> input)
124 throws PartInitException {
125 if(input instanceof BioCaseEditorInput){
126 open(input, SpecimenImportEditor.ID);
127 }
128 else if(input instanceof GbifImportEditorInput){
129 open(input, GbifImportEditor.ID);
130 }
131 }
132
133 /**
134 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
135 * initialised here and is passed to the editor
136 *
137 * @param taxonNodeUuid
138 * a {@link java.util.UUID} object.
139 * @throws java.lang.Exception
140 * if any.
141 */
142 public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
143 TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
144 open(input);
145 }
146
147 /**
148 * <p>
149 * openTaxonBase
150 * </p>
151 *
152 * @param taxonBaseUuid
153 * a {@link java.util.UUID} object.
154 * @throws org.eclipse.ui.PartInitException
155 * if any.
156 */
157 public static void openTaxonBase(UUID taxonBaseUuid)
158 throws PartInitException {
159 TaxonEditorInput input = TaxonEditorInput
160 .NewInstanceFromTaxonBase(taxonBaseUuid);
161 open(input);
162 }
163
164 /**
165 * <p>
166 * findEditorByTaxonNodeUuid
167 * </p>
168 *
169 * @param taxonNodeUuid
170 * a {@link java.util.UUID} object.
171 * @return a {@link org.eclipse.ui.IEditorPart} object.
172 * @throws java.lang.Exception
173 * if any.
174 */
175 public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
176 throws Exception {
177 IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
178 return getActivePage().findEditor(input);
179 }
180
181 /**
182 * An uninitialized taxon is one that hasn't been saved yet. As such, it
183 * should appear in neither the list of recent names nor in the taxonomic
184 * tree when opened.
185 *
186 * @throws org.eclipse.ui.PartInitException
187 * if any.
188 * @param parentNodeUuid
189 * a {@link java.util.UUID} object.
190 */
191 public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
192 TaxonEditorInput input = TaxonEditorInput
193 .NewEmptyInstance(parentNodeUuid);
194 open(input, MultiPageTaxonEditor.ID);
195
196 getActiveMultiPageTaxonEditor().changed(null);
197
198 }
199
200 /**
201 * <p>
202 * setSaving
203 * </p>
204 *
205 * @param isSaving
206 * a boolean.
207 */
208 public static void setSaving(boolean isSaving) {
209 EditorUtil.isSaving = isSaving;
210 }
211
212 /**
213 * <p>
214 * isSaving
215 * </p>
216 *
217 * @return a boolean.
218 */
219 public static boolean isSaving() {
220 return isSaving;
221 }
222
223 /**
224 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
225 *
226 * @return a {@link java.util.Set} object.
227 */
228 public static Set<IEditorPart> getOpenEditors() {
229 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
230
231 if (getActivePage() != null) {
232 for (IEditorReference reference : getActivePage()
233 .getEditorReferences()) {
234 IEditorPart editor = reference.getEditor(false);
235 if (editor instanceof MultiPageTaxonEditor) {
236 taxonEditors.add(editor);
237 }
238 }
239 }
240
241 return taxonEditors;
242 }
243
244 /**
245 * Returns the currently active taxon editor
246 *
247 * @return the taxon editor that has focus
248 */
249 public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
250 IEditorPart editorPart = getActiveEditor();
251 if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
252 MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
253 editor.getConversationHolder().bind();
254 return editor;
255 }
256 return null;
257 }
258
259 /**
260 * <p>
261 * getActiveEditorPage
262 * </p>
263 *
264 * @param page
265 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
266 * @return a {@link org.eclipse.ui.IEditorPart} object.
267 */
268 public static IEditorPart getActiveEditorPage(Page page) {
269 MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
270
271 return editor != null ? editor.getPage(page) : null;
272 }
273
274 /**
275 * Returns the selection of the currently active taxon editor
276 *
277 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
278 */
279 public static ISelection getCurrentSelection() {
280 if (getActiveMultiPageTaxonEditor() == null) {
281 return null;
282 } else {
283 return getActiveMultiPageTaxonEditor().getSite()
284 .getSelectionProvider().getSelection();
285 }
286 }
287
288 /**
289 * <p>
290 * getUndoContext
291 * </p>
292 *
293 * @param editor
294 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
295 * object.
296 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
297 * object.
298 */
299 public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
300 return editor.getUndoContext();
301 }
302
303 /**
304 * <p>
305 * getUndoContext
306 * </p>
307 *
308 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
309 * object.
310 */
311 public static IUndoContext getUndoContext() {
312 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
313 }
314
315 /**
316 * <p>
317 * forceUserSave
318 * </p>
319 *
320 * @param editor
321 * a {@link org.eclipse.ui.IEditorPart} object.
322 * @param shell
323 * a {@link org.eclipse.swt.widgets.Shell} object.
324 * @return a boolean.
325 */
326 public static boolean forceUserSave(IEditorPart editor, Shell shell) {
327 if (editor.isDirty()) {
328
329 boolean doSave = MessageDialog
330 .openConfirm(shell, "Confirm save",
331 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
332 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
333
334 if (!doSave) {
335 return false;
336 }
337
338 editor.doSave(AbstractUtility.getMonitor());
339 }
340 return true;
341 }
342
343 /**
344 * <p>
345 * getSelection
346 * </p>
347 *
348 * @param event
349 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
350 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
351 */
352 public static IStructuredSelection getSelection(ExecutionEvent event) {
353 IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
354
355 return (IStructuredSelection) activeEditor.getSite()
356 .getSelectionProvider().getSelection();
357 }
358
359 /**
360 * <p>
361 * getPluginId
362 * </p>
363 *
364 * @return a {@link java.lang.String} object.
365 */
366 public static String getPluginId() {
367 return TaxeditorEditorPlugin.PLUGIN_ID;
368 }
369
370 public static void openPolytomousKey(UUID polytomousKeyUuid)
371 throws Exception {
372 PolytomousKeyEditorInput input = PolytomousKeyEditorInput
373 .NewInstance(polytomousKeyUuid);
374 open(input);
375 }
376
377 public static void openCdmAuthorities(UUID groupUuid)
378 throws Exception {
379 CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
380 open(input);
381 }
382
383 /**
384 * Iterates recursively over all originals having the given specimen as a derivate.
385 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
386 * @param specimen the start element for which the originals are iterated recursively
387 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
388 */
389 public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
390 if(specimen.isInstanceOf(FieldUnit.class)){
391 return specimen;
392 }
393 else if(specimen instanceof DerivedUnit
394 && ((DerivedUnit) specimen).getOriginals()!=null
395 && !((DerivedUnit) specimen).getOriginals().isEmpty()){
396 for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
397 return getTopMostDerivate(original);
398 }
399 //needed to add this for compilation although this is unreachable
400 return specimen;
401 }
402 else{
403 return specimen;
404 }
405 }
406
407 /**
408 * Iterates recursively over all originals having the given specimen as a derivate.
409 * If a {@link FieldUnit} is found it is returned
410 * @param specimen the start element for which the originals are iterated recursively
411 * @return the FieldUnit if found, <code>null</code> otherwise
412 */
413 public static FieldUnit getFieldUnit(SpecimenOrObservationBase<?> specimen){
414 SpecimenOrObservationBase<?> topMostDerivate = getTopMostDerivate(specimen);
415 if(topMostDerivate instanceof FieldUnit) {
416 return (FieldUnit) topMostDerivate;
417 }
418 return null;
419 }
420
421 /**
422 * If the current selection is a single {@link TreeNode} it will be returned.
423 * @param selection the selection to check
424 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
425 */
426 public static TreeNode getTreeNodeOfSelection(ISelection selection){
427 if(selection instanceof IStructuredSelection
428 && ((IStructuredSelection) selection).size()==1
429 && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
430 return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
431
432 }
433 return null;
434 }
435 }