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
.view
.checklist
.ChecklistEditor
;
41 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditorInput
;
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
;
51 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
52 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
55 * Utility for the editor package
61 public class EditorUtil
extends AbstractUtility
{
63 private static boolean isSaving
= false;
66 * Opens a new editor window with the given input
72 * @throws PartInitException
74 private static IEditorPart
open(final IEditorInput input
,
75 final String editorId
) throws PartInitException
{
76 return getActivePage().openEditor(input
, editorId
);
80 * Opens a new editor window with the given TaxonEditorInput
83 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
85 * @throws org.eclipse.ui.PartInitException
88 public static void open(TaxonEditorInput input
) throws PartInitException
{
89 open(input
, MultiPageTaxonEditor
.ID
);
92 public static void open(PolytomousKeyEditorInput input
)
93 throws PartInitException
{
94 open(input
, KeyEditor
.ID
);
97 public static void open(CdmAuthorityEditorInput input
)
98 throws PartInitException
{
99 open(input
, CdmAuthorityEditor
.ID
);
103 * Opens a new DerivateView for the given input
104 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
105 * @throws PartInitException
107 public static void open(DerivateViewEditorInput input
)
108 throws PartInitException
{
109 open(input
, DerivateView
.ID
);
114 * Opens a new ChecklistView for the given input
115 * @param input a {@link ChecklistEditorInput} representing the selected checklist
116 * @throws PartInitException
118 public static void open(ChecklistEditorInput input
)
119 throws PartInitException
{
120 open(input
, ChecklistEditor
.ID
);
124 * Opens a new {@link DataImportEditor} for the given input
125 * @param input a {@link DataImportEditorInput}
126 * @throws PartInitException
128 public static void open(DataImportEditorInput
<?
> input
)
129 throws PartInitException
{
130 if(input
instanceof BioCaseEditorInput
){
131 open(input
, SpecimenImportEditor
.ID
);
133 else if(input
instanceof GbifImportEditorInput
){
134 open(input
, GbifImportEditor
.ID
);
139 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
140 * initialised here and is passed to the editor
142 * @param taxonNodeUuid
143 * a {@link java.util.UUID} object.
144 * @throws java.lang.Exception
147 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
148 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
157 * @param taxonBaseUuid
158 * a {@link java.util.UUID} object.
159 * @throws org.eclipse.ui.PartInitException
162 public static void openTaxonBase(UUID taxonBaseUuid
)
163 throws PartInitException
{
164 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).find(taxonBaseUuid
);
165 if (taxonBase
!= null && taxonBase
.isOrphaned()) {
166 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.");
169 TaxonEditorInput input
= TaxonEditorInput
170 .NewInstanceFromTaxonBase(taxonBaseUuid
);
176 * findEditorByTaxonNodeUuid
179 * @param taxonNodeUuid
180 * a {@link java.util.UUID} object.
181 * @return a {@link org.eclipse.ui.IEditorPart} object.
182 * @throws java.lang.Exception
185 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
187 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
188 return getActivePage().findEditor(input
);
192 * An uninitialized taxon is one that hasn't been saved yet. As such, it
193 * should appear in neither the list of recent names nor in the taxonomic
196 * @throws org.eclipse.ui.PartInitException
198 * @param parentNodeUuid
199 * a {@link java.util.UUID} object.
201 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
202 TaxonEditorInput input
= TaxonEditorInput
203 .NewEmptyInstance(parentNodeUuid
);
204 open(input
, MultiPageTaxonEditor
.ID
);
206 getActiveMultiPageTaxonEditor().changed(null);
218 public static void setSaving(boolean isSaving
) {
219 EditorUtil
.isSaving
= isSaving
;
229 public static boolean isSaving() {
234 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
236 * @return a {@link java.util.Set} object.
238 public static Set
<IEditorPart
> getOpenEditors() {
239 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
241 if (getActivePage() != null) {
242 for (IEditorReference reference
: getActivePage()
243 .getEditorReferences()) {
244 IEditorPart editor
= reference
.getEditor(false);
245 if (editor
instanceof MultiPageTaxonEditor
) {
246 taxonEditors
.add(editor
);
255 * Returns the currently active taxon editor
257 * @return the taxon editor that has focus
259 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
260 IEditorPart editorPart
= getActiveEditor();
261 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
262 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
263 editor
.getConversationHolder().bind();
271 * getActiveEditorPage
275 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
276 * @return a {@link org.eclipse.ui.IEditorPart} object.
278 public static IEditorPart
getActiveEditorPage(Page page
) {
279 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
281 return editor
!= null ? editor
.getPage(page
) : null;
285 * Returns the selection of the currently active taxon editor
287 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
289 public static ISelection
getCurrentSelection() {
290 if (getActiveMultiPageTaxonEditor() == null) {
293 return getActiveMultiPageTaxonEditor().getSite()
294 .getSelectionProvider().getSelection();
304 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
306 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
309 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
310 return editor
.getUndoContext();
318 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
321 public static IUndoContext
getUndoContext() {
322 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
331 * a {@link org.eclipse.ui.IEditorPart} object.
333 * a {@link org.eclipse.swt.widgets.Shell} object.
336 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
337 if (editor
.isDirty()) {
339 boolean doSave
= MessageDialog
340 .openConfirm(shell
, "Confirm save",
341 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
342 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
348 editor
.doSave(AbstractUtility
.getMonitor());
359 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
360 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
362 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
363 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
365 return (IStructuredSelection
) activeEditor
.getSite()
366 .getSelectionProvider().getSelection();
374 * @return a {@link java.lang.String} object.
376 public static String
getPluginId() {
377 return TaxeditorEditorPlugin
.PLUGIN_ID
;
380 public static void openPolytomousKey(UUID polytomousKeyUuid
)
382 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
383 .NewInstance(polytomousKeyUuid
);
387 public static void openCdmAuthorities(UUID groupUuid
)
389 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
394 * Iterates recursively over all originals having the given specimen as a derivate.
395 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
396 * @param specimen the start element for which the originals are iterated recursively
397 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
399 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
400 if(specimen
.isInstanceOf(FieldUnit
.class)){
403 else if(specimen
instanceof DerivedUnit
404 && ((DerivedUnit
) specimen
).getOriginals()!=null
405 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
406 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
407 return getTopMostDerivate(original
);
409 //needed to add this for compilation although this is unreachable
418 * Iterates recursively over all originals having the given specimen as a derivate.
419 * If a {@link FieldUnit} is found it is returned
420 * @param specimen the start element for which the originals are iterated recursively
421 * @return the FieldUnit if found, <code>null</code> otherwise
423 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
424 SpecimenOrObservationBase
<?
> topMostDerivate
= getTopMostDerivate(specimen
);
425 if(topMostDerivate
instanceof FieldUnit
) {
426 return (FieldUnit
) topMostDerivate
;
432 * If the current selection is a single {@link TreeNode} it will be returned.
433 * @param selection the selection to check
434 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
436 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
437 if(selection
instanceof IStructuredSelection
438 && ((IStructuredSelection
) selection
).size()==1
439 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
440 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();
447 * Opens a taxon editor for the given object if the given object is a valid input.
448 * @param object the object for which the editor will be opened
449 * @throws PartInitException
451 public static void openTaxonEditor(Object object
) throws PartInitException
{
452 if(object
instanceof TaxonBase
<?
>){
453 openTaxonBase(((TaxonBase
<?
>) object
).getUuid());