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