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
.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
;
57 * Utility for the editor package
63 public class EditorUtil
extends AbstractUtility
{
65 private static boolean isSaving
= false;
68 * Opens a new editor window with the given input
74 * @throws PartInitException
76 private static IEditorPart
open(final IEditorInput input
,
77 final String editorId
) throws PartInitException
{
78 return getActivePage().openEditor(input
, editorId
);
82 * Opens a new editor window with the given TaxonEditorInput
85 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
87 * @throws org.eclipse.ui.PartInitException
90 public static void open(TaxonEditorInput input
) throws PartInitException
{
91 open(input
, MultiPageTaxonEditor
.ID
);
94 public static void open(PolytomousKeyEditorInput input
)
95 throws PartInitException
{
96 open(input
, KeyEditor
.ID
);
99 public static void open(CdmAuthorityEditorInput input
)
100 throws PartInitException
{
101 open(input
, CdmAuthorityEditor
.ID
);
105 * Opens a new DerivateView for the given input
106 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
107 * @throws PartInitException
109 public static void open(DerivateViewEditorInput input
)
110 throws PartInitException
{
111 open(input
, DerivateView
.ID
);
116 * Opens a new ChecklistView for the given input
117 * @param input a {@link ChecklistEditorInput} representing the selected checklist
118 * @throws PartInitException
120 public static void open(ChecklistEditorInput input
)
121 throws PartInitException
{
122 open(input
, ChecklistEditor
.ID
);
126 * Opens a new AlignmentEditor for the given input
128 * @throws PartInitException
130 public static void open(AlignmentEditorInput input
)
131 throws PartInitException
{
132 open(input
, AlignmentEditor
.ID
);
136 * Opens a new {@link DataImportEditor} for the given input
137 * @param input a {@link DataImportEditorInput}
138 * @throws PartInitException
140 public static void open(DataImportEditorInput
<?
> input
)
141 throws PartInitException
{
142 if(input
instanceof BioCaseEditorInput
){
143 open(input
, SpecimenImportEditor
.ID
);
145 else if(input
instanceof GbifImportEditorInput
){
146 open(input
, GbifImportEditor
.ID
);
151 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
152 * initialised here and is passed to the editor
154 * @param taxonNodeUuid
155 * a {@link java.util.UUID} object.
156 * @throws java.lang.Exception
159 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
160 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
169 * @param taxonBaseUuid
170 * a {@link java.util.UUID} object.
171 * @throws org.eclipse.ui.PartInitException
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.");
181 TaxonEditorInput input
= TaxonEditorInput
182 .NewInstanceFromTaxonBase(taxonBaseUuid
);
188 * findEditorByTaxonNodeUuid
191 * @param taxonNodeUuid
192 * a {@link java.util.UUID} object.
193 * @return a {@link org.eclipse.ui.IEditorPart} object.
194 * @throws java.lang.Exception
197 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
199 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
200 return getActivePage().findEditor(input
);
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
208 * @throws org.eclipse.ui.PartInitException
210 * @param parentNodeUuid
211 * a {@link java.util.UUID} object.
213 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
214 TaxonEditorInput input
= TaxonEditorInput
215 .NewEmptyInstance(parentNodeUuid
);
216 open(input
, MultiPageTaxonEditor
.ID
);
218 getActiveMultiPageTaxonEditor().changed(null);
230 public static void setSaving(boolean isSaving
) {
231 EditorUtil
.isSaving
= isSaving
;
241 public static boolean isSaving() {
246 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
248 * @return a {@link java.util.Set} object.
250 public static Set
<IEditorPart
> getOpenEditors() {
251 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
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
);
267 * Returns the currently active taxon editor
269 * @return the taxon editor that has focus
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();
283 * getActiveEditorPage
287 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
288 * @return a {@link org.eclipse.ui.IEditorPart} object.
290 public static IEditorPart
getActiveEditorPage(Page page
) {
291 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
293 return editor
!= null ? editor
.getPage(page
) : null;
297 * Returns the selection of the currently active taxon editor
299 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
301 public static ISelection
getCurrentSelection() {
302 if (getActiveMultiPageTaxonEditor() == null) {
305 return getActiveMultiPageTaxonEditor().getSite()
306 .getSelectionProvider().getSelection();
316 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
318 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
321 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
322 return editor
.getUndoContext();
330 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
333 public static IUndoContext
getUndoContext() {
334 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
343 * a {@link org.eclipse.ui.IEditorPart} object.
345 * a {@link org.eclipse.swt.widgets.Shell} object.
348 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
349 if (editor
.isDirty()) {
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.");
360 editor
.doSave(AbstractUtility
.getMonitor());
371 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
372 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
374 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
375 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
377 return (IStructuredSelection
) activeEditor
.getSite()
378 .getSelectionProvider().getSelection();
386 * @return a {@link java.lang.String} object.
388 public static String
getPluginId() {
389 return TaxeditorEditorPlugin
.PLUGIN_ID
;
392 public static void openPolytomousKey(UUID polytomousKeyUuid
)
394 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
395 .NewInstance(polytomousKeyUuid
);
399 public static void openCdmAuthorities(UUID groupUuid
)
401 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
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)
411 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
412 if(specimen
.isInstanceOf(FieldUnit
.class)){
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
);
421 //needed to add this for compilation although this is unreachable
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
435 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
436 SpecimenOrObservationBase
<?
> topMostDerivate
= getTopMostDerivate(specimen
);
437 if(topMostDerivate
instanceof FieldUnit
) {
438 return (FieldUnit
) topMostDerivate
;
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
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();
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
463 public static void openTaxonEditor(Object object
) throws PartInitException
{
464 if(object
instanceof TaxonBase
<?
>){
465 openTaxonBase(((TaxonBase
<?
>) object
).getUuid());