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
.dataimport
.BioCaseEditorInput
;
39 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.DataImportEditor
;
40 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.DataImportEditorInput
;
41 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.GbifImportEditor
;
42 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.GbifImportEditorInput
;
43 import eu
.etaxonomy
.taxeditor
.editor
.view
.dataimport
.SpecimenImportEditor
;
44 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
45 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
46 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
49 * Utility for the editor package
55 public class EditorUtil
extends AbstractUtility
{
57 private static boolean isSaving
= false;
60 * Opens a new editor window with the given input
66 * @throws PartInitException
68 private static IEditorPart
open(final IEditorInput input
,
69 final String editorId
) throws PartInitException
{
70 return getActivePage().openEditor(input
, editorId
);
74 * Opens a new editor window with the given TaxonEditorInput
77 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
79 * @throws org.eclipse.ui.PartInitException
82 public static void open(TaxonEditorInput input
) throws PartInitException
{
83 open(input
, MultiPageTaxonEditor
.ID
);
86 public static void open(PolytomousKeyEditorInput input
)
87 throws PartInitException
{
88 open(input
, KeyEditor
.ID
);
91 public static void open(CdmAuthorityEditorInput input
)
92 throws PartInitException
{
93 open(input
, CdmAuthorityEditor
.ID
);
97 * Opens a new DerivateView for the given input
98 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
99 * @throws PartInitException
101 public static void open(DerivateViewEditorInput input
)
102 throws PartInitException
{
103 open(input
, DerivateView
.ID
);
107 * Opens a new {@link DataImportEditor} for the given input
108 * @param input a {@link DataImportEditorInput}
109 * @throws PartInitException
111 public static void open(DataImportEditorInput
<?
> input
)
112 throws PartInitException
{
113 if(input
instanceof BioCaseEditorInput
){
114 open(input
, SpecimenImportEditor
.ID
);
116 else if(input
instanceof GbifImportEditorInput
){
117 open(input
, GbifImportEditor
.ID
);
122 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
123 * initialised here and is passed to the editor
125 * @param taxonNodeUuid
126 * a {@link java.util.UUID} object.
127 * @throws java.lang.Exception
130 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
131 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
140 * @param taxonBaseUuid
141 * a {@link java.util.UUID} object.
142 * @throws org.eclipse.ui.PartInitException
145 public static void openTaxonBase(UUID taxonBaseUuid
)
146 throws PartInitException
{
147 TaxonEditorInput input
= TaxonEditorInput
148 .NewInstanceFromTaxonBase(taxonBaseUuid
);
154 * findEditorByTaxonNodeUuid
157 * @param taxonNodeUuid
158 * a {@link java.util.UUID} object.
159 * @return a {@link org.eclipse.ui.IEditorPart} object.
160 * @throws java.lang.Exception
163 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
165 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
166 return getActivePage().findEditor(input
);
170 * An uninitialized taxon is one that hasn't been saved yet. As such, it
171 * should appear in neither the list of recent names nor in the taxonomic
174 * @throws org.eclipse.ui.PartInitException
176 * @param parentNodeUuid
177 * a {@link java.util.UUID} object.
179 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
180 TaxonEditorInput input
= TaxonEditorInput
181 .NewEmptyInstance(parentNodeUuid
);
182 open(input
, MultiPageTaxonEditor
.ID
);
184 getActiveMultiPageTaxonEditor().changed(null);
196 public static void setSaving(boolean isSaving
) {
197 EditorUtil
.isSaving
= isSaving
;
207 public static boolean isSaving() {
212 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
214 * @return a {@link java.util.Set} object.
216 public static Set
<IEditorPart
> getOpenEditors() {
217 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
219 if (getActivePage() != null) {
220 for (IEditorReference reference
: getActivePage()
221 .getEditorReferences()) {
222 IEditorPart editor
= reference
.getEditor(false);
223 if (editor
instanceof MultiPageTaxonEditor
) {
224 taxonEditors
.add(editor
);
233 * Returns the currently active taxon editor
235 * @return the taxon editor that has focus
237 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
238 IEditorPart editorPart
= getActiveEditor();
239 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
240 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
241 editor
.getConversationHolder().bind();
249 * getActiveEditorPage
253 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
254 * @return a {@link org.eclipse.ui.IEditorPart} object.
256 public static IEditorPart
getActiveEditorPage(Page page
) {
257 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
259 return editor
!= null ? editor
.getPage(page
) : null;
263 * Returns the selection of the currently active taxon editor
265 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
267 public static ISelection
getCurrentSelection() {
268 if (getActiveMultiPageTaxonEditor() == null) {
271 return getActiveMultiPageTaxonEditor().getSite()
272 .getSelectionProvider().getSelection();
282 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
284 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
287 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
288 return editor
.getUndoContext();
296 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
299 public static IUndoContext
getUndoContext() {
300 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
309 * a {@link org.eclipse.ui.IEditorPart} object.
311 * a {@link org.eclipse.swt.widgets.Shell} object.
314 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
315 if (editor
.isDirty()) {
317 boolean doSave
= MessageDialog
318 .openConfirm(shell
, "Confirm save",
319 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
320 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
326 editor
.doSave(AbstractUtility
.getMonitor());
337 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
338 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
340 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
341 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
343 return (IStructuredSelection
) activeEditor
.getSite()
344 .getSelectionProvider().getSelection();
352 * @return a {@link java.lang.String} object.
354 public static String
getPluginId() {
355 return TaxeditorEditorPlugin
.PLUGIN_ID
;
358 public static void openPolytomousKey(UUID polytomousKeyUuid
)
360 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
361 .NewInstance(polytomousKeyUuid
);
365 public static void openCdmAuthorities(UUID groupUuid
)
367 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
372 * Iterates recursively over all originals having the given specimen as a derivate.
373 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
374 * @param specimen the start element for which the originals are iterated recursively
375 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
377 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
378 if(specimen
.isInstanceOf(FieldUnit
.class)){
381 else if(specimen
instanceof DerivedUnit
382 && ((DerivedUnit
) specimen
).getOriginals()!=null
383 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
384 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
385 return getTopMostDerivate(original
);
387 //needed to add this for compilation although this is unreachable
396 * Iterates recursively over all originals having the given specimen as a derivate.
397 * If a {@link FieldUnit} is found it is returned
398 * @param specimen the start element for which the originals are iterated recursively
399 * @return the FieldUnit if found, <code>null</code> otherwise
401 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
402 SpecimenOrObservationBase
<?
> topMostDerivate
= getTopMostDerivate(specimen
);
403 if(topMostDerivate
instanceof FieldUnit
) {
404 return (FieldUnit
) topMostDerivate
;
410 * If the current selection is a single {@link TreeNode} it will be returned.
411 * @param selection the selection to check
412 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
414 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
415 if(selection
instanceof IStructuredSelection
416 && ((IStructuredSelection
) selection
).size()==1
417 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
418 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();