2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
12 import java
.util
.HashSet
;
14 import java
.util
.UUID
;
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
;
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
.checklist
.ChecklistEditorInput
;
41 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
42 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.BioCaseEditorInput
;
43 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.DataImportEditor
;
44 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.DataImportEditorInput
;
45 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.GbifImportEditor
;
46 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.GbifImportEditorInput
;
47 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.SpecimenImportEditor
;
48 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
49 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
50 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
53 * Utility for the editor package
59 public class EditorUtil
extends AbstractUtility
{
61 private static boolean isSaving
= false;
64 * Opens a new editor window with the given input
70 * @throws PartInitException
72 private static IEditorPart
open(final IEditorInput input
,
73 final String editorId
) throws PartInitException
{
74 return getActivePage().openEditor(input
, editorId
);
78 * Opens a new editor window with the given TaxonEditorInput
81 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
83 * @throws org.eclipse.ui.PartInitException
86 public static void open(TaxonEditorInput input
) throws PartInitException
{
87 open(input
, MultiPageTaxonEditor
.ID
);
90 public static void open(PolytomousKeyEditorInput input
)
91 throws PartInitException
{
92 open(input
, KeyEditor
.ID
);
95 public static void open(CdmAuthorityEditorInput input
)
96 throws PartInitException
{
97 open(input
, CdmAuthorityEditor
.ID
);
101 * Opens a new DerivateView for the given input
102 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
103 * @throws PartInitException
105 public static void open(DerivateViewEditorInput input
)
106 throws PartInitException
{
107 open(input
, DerivateView
.ID
);
112 * Opens a new ChecklistView for the given input
113 * @param input a {@link ChecklistEditorInput} representing the selected checklist
114 * @throws PartInitException
116 public static void open(ChecklistEditorInput input
)
117 throws PartInitException
{
118 open(input
, ChecklistEditor
.ID
);
122 * Opens a new AlignmentEditor for the given input
124 * @throws PartInitException
126 public static void open(AlignmentEditorInput input
)
127 throws PartInitException
{
128 open(input
, AlignmentEditor
.ID
);
132 * Opens a new {@link DataImportEditor} for the given input
133 * @param input a {@link DataImportEditorInput}
134 * @throws PartInitException
136 public static void open(DataImportEditorInput
<?
> input
)
137 throws PartInitException
{
138 if(input
instanceof BioCaseEditorInput
){
139 open(input
, SpecimenImportEditor
.ID
);
141 else if(input
instanceof GbifImportEditorInput
){
142 open(input
, GbifImportEditor
.ID
);
147 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
148 * initialised here and is passed to the editor
150 * @param taxonNodeUuid
151 * a {@link java.util.UUID} object.
152 * @throws java.lang.Exception
155 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
156 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
165 * @param taxonBaseUuid
166 * a {@link java.util.UUID} object.
167 * @throws org.eclipse.ui.PartInitException
170 public static void openTaxonBase(UUID taxonBaseUuid
)
171 throws PartInitException
{
172 TaxonEditorInput input
= TaxonEditorInput
173 .NewInstanceFromTaxonBase(taxonBaseUuid
);
179 * findEditorByTaxonNodeUuid
182 * @param taxonNodeUuid
183 * a {@link java.util.UUID} object.
184 * @return a {@link org.eclipse.ui.IEditorPart} object.
185 * @throws java.lang.Exception
188 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
190 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
191 return getActivePage().findEditor(input
);
195 * An uninitialized taxon is one that hasn't been saved yet. As such, it
196 * should appear in neither the list of recent names nor in the taxonomic
199 * @throws org.eclipse.ui.PartInitException
201 * @param parentNodeUuid
202 * a {@link java.util.UUID} object.
204 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
205 TaxonEditorInput input
= TaxonEditorInput
206 .NewEmptyInstance(parentNodeUuid
);
207 open(input
, MultiPageTaxonEditor
.ID
);
209 getActiveMultiPageTaxonEditor().changed(null);
221 public static void setSaving(boolean isSaving
) {
222 EditorUtil
.isSaving
= isSaving
;
232 public static boolean isSaving() {
237 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
239 * @return a {@link java.util.Set} object.
241 public static Set
<IEditorPart
> getOpenEditors() {
242 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
244 if (getActivePage() != null) {
245 for (IEditorReference reference
: getActivePage()
246 .getEditorReferences()) {
247 IEditorPart editor
= reference
.getEditor(false);
248 if (editor
instanceof MultiPageTaxonEditor
) {
249 taxonEditors
.add(editor
);
258 * Returns the currently active taxon editor
260 * @return the taxon editor that has focus
262 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
263 IEditorPart editorPart
= getActiveEditor();
264 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
265 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
266 editor
.getConversationHolder().bind();
274 * getActiveEditorPage
278 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
279 * @return a {@link org.eclipse.ui.IEditorPart} object.
281 public static IEditorPart
getActiveEditorPage(Page page
) {
282 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
284 return editor
!= null ? editor
.getPage(page
) : null;
288 * Returns the selection of the currently active taxon editor
290 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
292 public static ISelection
getCurrentSelection() {
293 if (getActiveMultiPageTaxonEditor() == null) {
296 return getActiveMultiPageTaxonEditor().getSite()
297 .getSelectionProvider().getSelection();
307 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
309 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
312 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
313 return editor
.getUndoContext();
321 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
324 public static IUndoContext
getUndoContext() {
325 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
334 * a {@link org.eclipse.ui.IEditorPart} object.
336 * a {@link org.eclipse.swt.widgets.Shell} object.
339 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
340 if (editor
.isDirty()) {
342 boolean doSave
= MessageDialog
343 .openConfirm(shell
, "Confirm save",
344 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
345 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
351 editor
.doSave(AbstractUtility
.getMonitor());
362 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
363 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
365 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
366 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
368 return (IStructuredSelection
) activeEditor
.getSite()
369 .getSelectionProvider().getSelection();
377 * @return a {@link java.lang.String} object.
379 public static String
getPluginId() {
380 return TaxeditorEditorPlugin
.PLUGIN_ID
;
383 public static void openPolytomousKey(UUID polytomousKeyUuid
)
385 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
386 .NewInstance(polytomousKeyUuid
);
390 public static void openCdmAuthorities(UUID groupUuid
)
392 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
397 * Iterates recursively over all originals having the given specimen as a derivate.
398 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
399 * @param specimen the start element for which the originals are iterated recursively
400 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
402 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
403 if(specimen
.isInstanceOf(FieldUnit
.class)){
406 else if(specimen
instanceof DerivedUnit
407 && ((DerivedUnit
) specimen
).getOriginals()!=null
408 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
409 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
410 return getTopMostDerivate(original
);
412 //needed to add this for compilation although this is unreachable
421 * Iterates recursively over all originals having the given specimen as a derivate.
422 * If a {@link FieldUnit} is found it is returned
423 * @param specimen the start element for which the originals are iterated recursively
424 * @return the FieldUnit if found, <code>null</code> otherwise
426 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
427 SpecimenOrObservationBase
<?
> topMostDerivate
= getTopMostDerivate(specimen
);
428 if(topMostDerivate
instanceof FieldUnit
) {
429 return (FieldUnit
) topMostDerivate
;
435 * If the current selection is a single {@link TreeNode} it will be returned.
436 * @param selection the selection to check
437 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
439 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
440 if(selection
instanceof IStructuredSelection
441 && ((IStructuredSelection
) selection
).size()==1
442 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
443 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();