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
.checklist
.ChecklistEditorInput
;
39 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
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
);
110 * Opens a new ChecklistView for the given input
111 * @param input a {@link ChecklistEditorInput} representing the selected checklist
112 * @throws PartInitException
114 public static void open(ChecklistEditorInput input
)
115 throws PartInitException
{
116 open(input
, ChecklistEditor
.ID
);
120 * Opens a new {@link DataImportEditor} for the given input
121 * @param input a {@link DataImportEditorInput}
122 * @throws PartInitException
124 public static void open(DataImportEditorInput
<?
> input
)
125 throws PartInitException
{
126 if(input
instanceof BioCaseEditorInput
){
127 open(input
, SpecimenImportEditor
.ID
);
129 else if(input
instanceof GbifImportEditorInput
){
130 open(input
, GbifImportEditor
.ID
);
135 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
136 * initialised here and is passed to the editor
138 * @param taxonNodeUuid
139 * a {@link java.util.UUID} object.
140 * @throws java.lang.Exception
143 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
144 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
153 * @param taxonBaseUuid
154 * a {@link java.util.UUID} object.
155 * @throws org.eclipse.ui.PartInitException
158 public static void openTaxonBase(UUID taxonBaseUuid
)
159 throws PartInitException
{
160 TaxonEditorInput input
= TaxonEditorInput
161 .NewInstanceFromTaxonBase(taxonBaseUuid
);
167 * findEditorByTaxonNodeUuid
170 * @param taxonNodeUuid
171 * a {@link java.util.UUID} object.
172 * @return a {@link org.eclipse.ui.IEditorPart} object.
173 * @throws java.lang.Exception
176 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
178 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
179 return getActivePage().findEditor(input
);
183 * An uninitialized taxon is one that hasn't been saved yet. As such, it
184 * should appear in neither the list of recent names nor in the taxonomic
187 * @throws org.eclipse.ui.PartInitException
189 * @param parentNodeUuid
190 * a {@link java.util.UUID} object.
192 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
193 TaxonEditorInput input
= TaxonEditorInput
194 .NewEmptyInstance(parentNodeUuid
);
195 open(input
, MultiPageTaxonEditor
.ID
);
197 getActiveMultiPageTaxonEditor().changed(null);
209 public static void setSaving(boolean isSaving
) {
210 EditorUtil
.isSaving
= isSaving
;
220 public static boolean isSaving() {
225 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
227 * @return a {@link java.util.Set} object.
229 public static Set
<IEditorPart
> getOpenEditors() {
230 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
232 if (getActivePage() != null) {
233 for (IEditorReference reference
: getActivePage()
234 .getEditorReferences()) {
235 IEditorPart editor
= reference
.getEditor(false);
236 if (editor
instanceof MultiPageTaxonEditor
) {
237 taxonEditors
.add(editor
);
246 * Returns the currently active taxon editor
248 * @return the taxon editor that has focus
250 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
251 IEditorPart editorPart
= getActiveEditor();
252 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
253 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
254 editor
.getConversationHolder().bind();
262 * getActiveEditorPage
266 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
267 * @return a {@link org.eclipse.ui.IEditorPart} object.
269 public static IEditorPart
getActiveEditorPage(Page page
) {
270 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
272 return editor
!= null ? editor
.getPage(page
) : null;
276 * Returns the selection of the currently active taxon editor
278 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
280 public static ISelection
getCurrentSelection() {
281 if (getActiveMultiPageTaxonEditor() == null) {
284 return getActiveMultiPageTaxonEditor().getSite()
285 .getSelectionProvider().getSelection();
295 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
297 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
300 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
301 return editor
.getUndoContext();
309 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
312 public static IUndoContext
getUndoContext() {
313 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
322 * a {@link org.eclipse.ui.IEditorPart} object.
324 * a {@link org.eclipse.swt.widgets.Shell} object.
327 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
328 if (editor
.isDirty()) {
330 boolean doSave
= MessageDialog
331 .openConfirm(shell
, "Confirm save",
332 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
333 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
339 editor
.doSave(AbstractUtility
.getMonitor());
350 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
351 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
353 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
354 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
356 return (IStructuredSelection
) activeEditor
.getSite()
357 .getSelectionProvider().getSelection();
365 * @return a {@link java.lang.String} object.
367 public static String
getPluginId() {
368 return TaxeditorEditorPlugin
.PLUGIN_ID
;
371 public static void openPolytomousKey(UUID polytomousKeyUuid
)
373 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
374 .NewInstance(polytomousKeyUuid
);
378 public static void openCdmAuthorities(UUID groupUuid
)
380 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
385 * Iterates recursively over all originals having the given specimen as a derivate.
386 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
387 * @param specimen the start element for which the originals are iterated recursively
388 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
390 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
391 if(specimen
.isInstanceOf(FieldUnit
.class)){
394 else if(specimen
instanceof DerivedUnit
395 && ((DerivedUnit
) specimen
).getOriginals()!=null
396 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
397 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
398 return getTopMostDerivate(original
);
400 //needed to add this for compilation although this is unreachable
409 * Iterates recursively over all originals having the given specimen as a derivate.
410 * If a {@link FieldUnit} is found it is returned
411 * @param specimen the start element for which the originals are iterated recursively
412 * @return the FieldUnit if found, <code>null</code> otherwise
414 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
415 SpecimenOrObservationBase
<?
> topMostDerivate
= getTopMostDerivate(specimen
);
416 if(topMostDerivate
instanceof FieldUnit
) {
417 return (FieldUnit
) topMostDerivate
;
423 * If the current selection is a single {@link TreeNode} it will be returned.
424 * @param selection the selection to check
425 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
427 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
428 if(selection
instanceof IStructuredSelection
429 && ((IStructuredSelection
) selection
).size()==1
430 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
431 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();