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
);
178 * findEditorByTaxonNodeUuid
181 * @param taxonNodeUuid
182 * a {@link java.util.UUID} object.
183 * @return a {@link org.eclipse.ui.IEditorPart} object.
184 * @throws java.lang.Exception
187 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
189 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
190 return getActivePage().findEditor(input
);
194 * An uninitialized taxon is one that hasn't been saved yet. As such, it
195 * should appear in neither the list of recent names nor in the taxonomic
198 * @throws org.eclipse.ui.PartInitException
200 * @param parentNodeUuid
201 * a {@link java.util.UUID} object.
203 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
204 TaxonEditorInput input
= TaxonEditorInput
205 .NewEmptyInstance(parentNodeUuid
);
206 open(input
, MultiPageTaxonEditor
.ID
);
208 getActiveMultiPageTaxonEditor().changed(null);
220 public static void setSaving(boolean isSaving
) {
221 EditorUtil
.isSaving
= isSaving
;
231 public static boolean isSaving() {
236 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
238 * @return a {@link java.util.Set} object.
240 public static Set
<IEditorPart
> getOpenEditors() {
241 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
243 if (getActivePage() != null) {
244 for (IEditorReference reference
: getActivePage()
245 .getEditorReferences()) {
246 IEditorPart editor
= reference
.getEditor(false);
247 if (editor
instanceof MultiPageTaxonEditor
) {
248 taxonEditors
.add(editor
);
257 * Returns the currently active taxon editor
259 * @return the taxon editor that has focus
261 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
262 IEditorPart editorPart
= getActiveEditor();
263 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
264 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
265 editor
.getConversationHolder().bind();
273 * getActiveEditorPage
277 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
278 * @return a {@link org.eclipse.ui.IEditorPart} object.
280 public static IEditorPart
getActiveEditorPage(Page page
) {
281 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
283 return editor
!= null ? editor
.getPage(page
) : null;
287 * Returns the selection of the currently active taxon editor
289 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
291 public static ISelection
getCurrentSelection() {
292 if (getActiveMultiPageTaxonEditor() == null) {
295 return getActiveMultiPageTaxonEditor().getSite()
296 .getSelectionProvider().getSelection();
306 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
308 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
311 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
312 return editor
.getUndoContext();
320 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
323 public static IUndoContext
getUndoContext() {
324 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
333 * a {@link org.eclipse.ui.IEditorPart} object.
335 * a {@link org.eclipse.swt.widgets.Shell} object.
338 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
339 if (editor
.isDirty()) {
341 boolean doSave
= MessageDialog
342 .openConfirm(shell
, "Confirm save",
343 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
344 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
350 editor
.doSave(AbstractUtility
.getMonitor());
361 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
362 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
364 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
365 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
367 return (IStructuredSelection
) activeEditor
.getSite()
368 .getSelectionProvider().getSelection();
376 * @return a {@link java.lang.String} object.
378 public static String
getPluginId() {
379 return TaxeditorEditorPlugin
.PLUGIN_ID
;
382 public static void openPolytomousKey(UUID polytomousKeyUuid
)
384 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
385 .NewInstance(polytomousKeyUuid
);
389 public static void openCdmAuthorities(UUID groupUuid
)
391 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
396 * Iterates recursively over all originals having the given specimen as a derivate.
397 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
398 * @param specimen the start element for which the originals are iterated recursively
399 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
401 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
402 if(specimen
.isInstanceOf(FieldUnit
.class)){
405 else if(specimen
instanceof DerivedUnit
406 && ((DerivedUnit
) specimen
).getOriginals()!=null
407 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
408 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
409 return getTopMostDerivate(original
);
411 //needed to add this for compilation although this is unreachable
420 * If the current selection is a single {@link TreeNode} it will be returned.
421 * @param selection the selection to check
422 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
424 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
425 if(selection
instanceof IStructuredSelection
426 && ((IStructuredSelection
) selection
).size()==1
427 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
428 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();
435 * Opens a taxon editor for the given object if the given object is a valid input.
436 * @param object the object for which the editor will be opened
437 * @throws PartInitException
439 public static void openTaxonEditor(Object object
) throws PartInitException
{
440 if(object
instanceof TaxonBase
<?
>){
441 openTaxonBase(((TaxonBase
<?
>) object
).getUuid());