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
.swt
.widgets
.Shell
;
23 import org
.eclipse
.ui
.IEditorInput
;
24 import org
.eclipse
.ui
.IEditorPart
;
25 import org
.eclipse
.ui
.IEditorReference
;
26 import org
.eclipse
.ui
.PartInitException
;
27 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
29 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
30 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
31 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
32 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditor
;
33 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditorInput
;
34 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
35 import eu
.etaxonomy
.taxeditor
.editor
.key
.KeyEditor
;
36 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorInput
;
37 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
38 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
39 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
42 * Utility for the editor package
48 public class EditorUtil
extends AbstractUtility
{
50 private static boolean isSaving
= false;
53 * Opens a new editor window with the given input
59 * @throws PartInitException
61 private static IEditorPart
open(final IEditorInput input
,
62 final String editorId
) throws PartInitException
{
63 return getActivePage().openEditor(input
, editorId
);
67 * Opens a new editor window with the given TaxonEditorInput
70 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
72 * @throws org.eclipse.ui.PartInitException
75 public static void open(TaxonEditorInput input
) throws PartInitException
{
76 open(input
, MultiPageTaxonEditor
.ID
);
79 public static void open(PolytomousKeyEditorInput input
)
80 throws PartInitException
{
81 open(input
, KeyEditor
.ID
);
84 public static void open(CdmAuthorityEditorInput input
)
85 throws PartInitException
{
86 open(input
, CdmAuthorityEditor
.ID
);
90 * Opens a new DerivateView for the given input
91 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
92 * @throws PartInitException
94 public static void open(DerivateViewEditorInput input
)
95 throws PartInitException
{
96 open(input
, DerivateView
.ID
);
100 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
101 * initialised here and is passed to the editor
103 * @param taxonNodeUuid
104 * a {@link java.util.UUID} object.
105 * @throws java.lang.Exception
108 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
109 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
118 * @param taxonBaseUuid
119 * a {@link java.util.UUID} object.
120 * @throws org.eclipse.ui.PartInitException
123 public static void openTaxonBase(UUID taxonBaseUuid
)
124 throws PartInitException
{
125 TaxonEditorInput input
= TaxonEditorInput
126 .NewInstanceFromTaxonBase(taxonBaseUuid
);
132 * findEditorByTaxonNodeUuid
135 * @param taxonNodeUuid
136 * a {@link java.util.UUID} object.
137 * @return a {@link org.eclipse.ui.IEditorPart} object.
138 * @throws java.lang.Exception
141 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
143 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
144 return getActivePage().findEditor(input
);
148 * An uninitialized taxon is one that hasn't been saved yet. As such, it
149 * should appear in neither the list of recent names nor in the taxonomic
152 * @throws org.eclipse.ui.PartInitException
154 * @param parentNodeUuid
155 * a {@link java.util.UUID} object.
157 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
158 TaxonEditorInput input
= TaxonEditorInput
159 .NewEmptyInstance(parentNodeUuid
);
160 open(input
, MultiPageTaxonEditor
.ID
);
162 getActiveMultiPageTaxonEditor().changed(null);
174 public static void setSaving(boolean isSaving
) {
175 EditorUtil
.isSaving
= isSaving
;
185 public static boolean isSaving() {
190 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
192 * @return a {@link java.util.Set} object.
194 public static Set
<IEditorPart
> getOpenEditors() {
195 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
197 if (getActivePage() != null) {
198 for (IEditorReference reference
: getActivePage()
199 .getEditorReferences()) {
200 IEditorPart editor
= reference
.getEditor(false);
201 if (editor
instanceof MultiPageTaxonEditor
) {
202 taxonEditors
.add(editor
);
211 * Returns the currently active taxon editor
213 * @return the taxon editor that has focus
215 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
216 IEditorPart editorPart
= getActiveEditor();
217 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
218 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
219 editor
.getConversationHolder().bind();
227 * getActiveEditorPage
231 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
232 * @return a {@link org.eclipse.ui.IEditorPart} object.
234 public static IEditorPart
getActiveEditorPage(Page page
) {
235 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
237 return editor
!= null ? editor
.getPage(page
) : null;
241 * Returns the selection of the currently active taxon editor
243 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
245 public static ISelection
getCurrentSelection() {
246 if (getActiveMultiPageTaxonEditor() == null) {
249 return getActiveMultiPageTaxonEditor().getSite()
250 .getSelectionProvider().getSelection();
260 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
262 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
265 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
266 return editor
.getUndoContext();
274 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
277 public static IUndoContext
getUndoContext() {
278 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
287 * a {@link org.eclipse.ui.IEditorPart} object.
289 * a {@link org.eclipse.swt.widgets.Shell} object.
292 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
293 if (editor
.isDirty()) {
295 boolean doSave
= MessageDialog
296 .openConfirm(shell
, "Confirm save",
297 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
298 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
304 editor
.doSave(AbstractUtility
.getMonitor());
315 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
316 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
318 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
319 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
321 return (IStructuredSelection
) activeEditor
.getSite()
322 .getSelectionProvider().getSelection();
330 * @return a {@link java.lang.String} object.
332 public static String
getPluginId() {
333 return TaxeditorEditorPlugin
.PLUGIN_ID
;
336 public static void openPolytomousKey(UUID polytomousKeyUuid
)
338 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
339 .NewInstance(polytomousKeyUuid
);
343 public static void openCdmAuthorities(UUID groupUuid
)
345 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
349 public static SpecimenOrObservationBase
getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
351 if(specimen
.isInstanceOf(FieldUnit
.class)){
354 else if(specimen
instanceof DerivedUnit
){
355 if(((DerivedUnit
) specimen
).getOriginals()==null || ((DerivedUnit
) specimen
).getOriginals().isEmpty()){
359 for(SpecimenOrObservationBase original
:((DerivedUnit
) specimen
).getOriginals()){
360 return getTopMostDerivate(original
);
368 public static FieldUnit
getFieldUnit(SpecimenOrObservationBase
<?
> specimen
){
369 SpecimenOrObservationBase topMostDerivate
= getTopMostDerivate(specimen
);
370 if(topMostDerivate
instanceof FieldUnit
) {
371 return (FieldUnit
) topMostDerivate
;