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
.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
;
51 * Utility for the editor package
57 public class EditorUtil
extends AbstractUtility
{
59 private static boolean isSaving
= false;
62 * Opens a new editor window with the given input
68 * @throws PartInitException
70 private static IEditorPart
open(final IEditorInput input
,
71 final String editorId
) throws PartInitException
{
72 return getActivePage().openEditor(input
, editorId
);
76 * Opens a new editor window with the given TaxonEditorInput
79 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
81 * @throws org.eclipse.ui.PartInitException
84 public static void open(TaxonEditorInput input
) throws PartInitException
{
85 open(input
, MultiPageTaxonEditor
.ID
);
88 public static void open(PolytomousKeyEditorInput input
)
89 throws PartInitException
{
90 open(input
, KeyEditor
.ID
);
93 public static void open(CdmAuthorityEditorInput input
)
94 throws PartInitException
{
95 open(input
, CdmAuthorityEditor
.ID
);
99 * Opens a new DerivateView for the given input
100 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
101 * @throws PartInitException
103 public static void open(DerivateViewEditorInput input
)
104 throws PartInitException
{
105 open(input
, DerivateView
.ID
);
109 * Opens a new AlignmentEditor for the given input
111 * @throws PartInitException
113 public static void open(AlignmentEditorInput input
)
114 throws PartInitException
{
115 open(input
, AlignmentEditor
.ID
);
119 * Opens a new {@link DataImportEditor} for the given input
120 * @param input a {@link DataImportEditorInput}
121 * @throws PartInitException
123 public static void open(DataImportEditorInput
<?
> input
)
124 throws PartInitException
{
125 if(input
instanceof BioCaseEditorInput
){
126 open(input
, SpecimenImportEditor
.ID
);
128 else if(input
instanceof GbifImportEditorInput
){
129 open(input
, GbifImportEditor
.ID
);
134 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
135 * initialised here and is passed to the editor
137 * @param taxonNodeUuid
138 * a {@link java.util.UUID} object.
139 * @throws java.lang.Exception
142 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
143 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
152 * @param taxonBaseUuid
153 * a {@link java.util.UUID} object.
154 * @throws org.eclipse.ui.PartInitException
157 public static void openTaxonBase(UUID taxonBaseUuid
)
158 throws PartInitException
{
159 TaxonEditorInput input
= TaxonEditorInput
160 .NewInstanceFromTaxonBase(taxonBaseUuid
);
166 * findEditorByTaxonNodeUuid
169 * @param taxonNodeUuid
170 * a {@link java.util.UUID} object.
171 * @return a {@link org.eclipse.ui.IEditorPart} object.
172 * @throws java.lang.Exception
175 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
177 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
178 return getActivePage().findEditor(input
);
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
186 * @throws org.eclipse.ui.PartInitException
188 * @param parentNodeUuid
189 * a {@link java.util.UUID} object.
191 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
192 TaxonEditorInput input
= TaxonEditorInput
193 .NewEmptyInstance(parentNodeUuid
);
194 open(input
, MultiPageTaxonEditor
.ID
);
196 getActiveMultiPageTaxonEditor().changed(null);
208 public static void setSaving(boolean isSaving
) {
209 EditorUtil
.isSaving
= isSaving
;
219 public static boolean isSaving() {
224 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
226 * @return a {@link java.util.Set} object.
228 public static Set
<IEditorPart
> getOpenEditors() {
229 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
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
);
245 * Returns the currently active taxon editor
247 * @return the taxon editor that has focus
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();
261 * getActiveEditorPage
265 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
266 * @return a {@link org.eclipse.ui.IEditorPart} object.
268 public static IEditorPart
getActiveEditorPage(Page page
) {
269 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
271 return editor
!= null ? editor
.getPage(page
) : null;
275 * Returns the selection of the currently active taxon editor
277 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
279 public static ISelection
getCurrentSelection() {
280 if (getActiveMultiPageTaxonEditor() == null) {
283 return getActiveMultiPageTaxonEditor().getSite()
284 .getSelectionProvider().getSelection();
294 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
296 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
299 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
300 return editor
.getUndoContext();
308 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
311 public static IUndoContext
getUndoContext() {
312 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
321 * a {@link org.eclipse.ui.IEditorPart} object.
323 * a {@link org.eclipse.swt.widgets.Shell} object.
326 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
327 if (editor
.isDirty()) {
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.");
338 editor
.doSave(AbstractUtility
.getMonitor());
349 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
350 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
352 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
353 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
355 return (IStructuredSelection
) activeEditor
.getSite()
356 .getSelectionProvider().getSelection();
364 * @return a {@link java.lang.String} object.
366 public static String
getPluginId() {
367 return TaxeditorEditorPlugin
.PLUGIN_ID
;
370 public static void openPolytomousKey(UUID polytomousKeyUuid
)
372 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
373 .NewInstance(polytomousKeyUuid
);
377 public static void openCdmAuthorities(UUID groupUuid
)
379 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
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)
389 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
390 if(specimen
.isInstanceOf(FieldUnit
.class)){
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
);
399 //needed to add this for compilation although this is unreachable
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
413 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
414 SpecimenOrObservationBase
<?
> topMostDerivate
= getTopMostDerivate(specimen
);
415 if(topMostDerivate
instanceof FieldUnit
) {
416 return (FieldUnit
) topMostDerivate
;
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
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();