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
.view
.derivate
.DerivateView
;
39 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
40 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
43 * Utility for the editor package
49 public class EditorUtil
extends AbstractUtility
{
51 private static boolean isSaving
= false;
54 * Opens a new editor window with the given input
60 * @throws PartInitException
62 private static IEditorPart
open(final IEditorInput input
,
63 final String editorId
) throws PartInitException
{
64 return getActivePage().openEditor(input
, editorId
);
68 * Opens a new editor window with the given TaxonEditorInput
71 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
73 * @throws org.eclipse.ui.PartInitException
76 public static void open(TaxonEditorInput input
) throws PartInitException
{
77 open(input
, MultiPageTaxonEditor
.ID
);
80 public static void open(PolytomousKeyEditorInput input
)
81 throws PartInitException
{
82 open(input
, KeyEditor
.ID
);
85 public static void open(CdmAuthorityEditorInput input
)
86 throws PartInitException
{
87 open(input
, CdmAuthorityEditor
.ID
);
91 * Opens a new DerivateView for the given input
92 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
93 * @throws PartInitException
95 public static void open(DerivateViewEditorInput input
)
96 throws PartInitException
{
97 open(input
, DerivateView
.ID
);
101 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
102 * initialised here and is passed to the editor
104 * @param taxonNodeUuid
105 * a {@link java.util.UUID} object.
106 * @throws java.lang.Exception
109 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
110 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
119 * @param taxonBaseUuid
120 * a {@link java.util.UUID} object.
121 * @throws org.eclipse.ui.PartInitException
124 public static void openTaxonBase(UUID taxonBaseUuid
)
125 throws PartInitException
{
126 TaxonEditorInput input
= TaxonEditorInput
127 .NewInstanceFromTaxonBase(taxonBaseUuid
);
133 * findEditorByTaxonNodeUuid
136 * @param taxonNodeUuid
137 * a {@link java.util.UUID} object.
138 * @return a {@link org.eclipse.ui.IEditorPart} object.
139 * @throws java.lang.Exception
142 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
144 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
145 return getActivePage().findEditor(input
);
149 * An uninitialized taxon is one that hasn't been saved yet. As such, it
150 * should appear in neither the list of recent names nor in the taxonomic
153 * @throws org.eclipse.ui.PartInitException
155 * @param parentNodeUuid
156 * a {@link java.util.UUID} object.
158 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
159 TaxonEditorInput input
= TaxonEditorInput
160 .NewEmptyInstance(parentNodeUuid
);
161 open(input
, MultiPageTaxonEditor
.ID
);
163 getActiveMultiPageTaxonEditor().changed(null);
175 public static void setSaving(boolean isSaving
) {
176 EditorUtil
.isSaving
= isSaving
;
186 public static boolean isSaving() {
191 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
193 * @return a {@link java.util.Set} object.
195 public static Set
<IEditorPart
> getOpenEditors() {
196 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
198 if (getActivePage() != null) {
199 for (IEditorReference reference
: getActivePage()
200 .getEditorReferences()) {
201 IEditorPart editor
= reference
.getEditor(false);
202 if (editor
instanceof MultiPageTaxonEditor
) {
203 taxonEditors
.add(editor
);
212 * Returns the currently active taxon editor
214 * @return the taxon editor that has focus
216 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
217 IEditorPart editorPart
= getActiveEditor();
218 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
219 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
220 editor
.getConversationHolder().bind();
228 * getActiveEditorPage
232 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
233 * @return a {@link org.eclipse.ui.IEditorPart} object.
235 public static IEditorPart
getActiveEditorPage(Page page
) {
236 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
238 return editor
!= null ? editor
.getPage(page
) : null;
242 * Returns the selection of the currently active taxon editor
244 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
246 public static ISelection
getCurrentSelection() {
247 if (getActiveMultiPageTaxonEditor() == null) {
250 return getActiveMultiPageTaxonEditor().getSite()
251 .getSelectionProvider().getSelection();
261 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
263 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
266 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
267 return editor
.getUndoContext();
275 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
278 public static IUndoContext
getUndoContext() {
279 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
288 * a {@link org.eclipse.ui.IEditorPart} object.
290 * a {@link org.eclipse.swt.widgets.Shell} object.
293 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
294 if (editor
.isDirty()) {
296 boolean doSave
= MessageDialog
297 .openConfirm(shell
, "Confirm save",
298 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
299 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
305 editor
.doSave(AbstractUtility
.getMonitor());
316 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
317 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
319 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
320 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
322 return (IStructuredSelection
) activeEditor
.getSite()
323 .getSelectionProvider().getSelection();
331 * @return a {@link java.lang.String} object.
333 public static String
getPluginId() {
334 return TaxeditorEditorPlugin
.PLUGIN_ID
;
337 public static void openPolytomousKey(UUID polytomousKeyUuid
)
339 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
340 .NewInstance(polytomousKeyUuid
);
344 public static void openCdmAuthorities(UUID groupUuid
)
346 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
351 * Iterates recursively over all originals having the given specimen as a derivate.
352 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
353 * @param specimen the start element for which the originals are iterated recursively
354 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
356 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
357 if(specimen
.isInstanceOf(FieldUnit
.class)){
360 else if(specimen
instanceof DerivedUnit
361 && ((DerivedUnit
) specimen
).getOriginals()!=null
362 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
363 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
364 return getTopMostDerivate(original
);
366 //needed to add this for compilation although this is unreachable
375 * Iterates recursively over all originals having the given specimen as a derivate.
376 * If a {@link FieldUnit} is found it is returned
377 * @param specimen the start element for which the originals are iterated recursively
378 * @return the FieldUnit if found, <code>null</code> otherwise
380 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
381 SpecimenOrObservationBase
<?
> topMostDerivate
= getTopMostDerivate(specimen
);
382 if(topMostDerivate
instanceof FieldUnit
) {
383 return (FieldUnit
) topMostDerivate
;
389 * If the current selection is a single {@link TreeNode} it will be returned.
390 * @param selection the selection to check
391 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
393 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
394 if(selection
instanceof IStructuredSelection
395 && ((IStructuredSelection
) selection
).size()==1
396 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
397 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();