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
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditor
;
30 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditorInput
;
31 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
32 import eu
.etaxonomy
.taxeditor
.editor
.key
.KeyEditor
;
33 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorInput
;
34 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
37 * Utility for the editor package
43 public class EditorUtil
extends AbstractUtility
{
45 private static boolean isSaving
= false;
48 * Opens a new editor window with the given input
54 * @throws PartInitException
56 private static IEditorPart
open(final IEditorInput input
,
57 final String editorId
) throws PartInitException
{
58 return getActivePage().openEditor(input
, editorId
);
62 * Opens a new editor window with the given TaxonEditorInput
65 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
67 * @throws org.eclipse.ui.PartInitException
70 public static void open(TaxonEditorInput input
) throws PartInitException
{
71 open(input
, MultiPageTaxonEditor
.ID
);
74 public static void open(PolytomousKeyEditorInput input
)
75 throws PartInitException
{
76 open(input
, KeyEditor
.ID
);
79 public static void open(CdmAuthorityEditorInput input
)
80 throws PartInitException
{
81 open(input
, CdmAuthorityEditor
.ID
);
85 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
86 * initialised here and is passed to the editor
88 * @param taxonNodeUuid
89 * a {@link java.util.UUID} object.
90 * @throws java.lang.Exception
93 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
94 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
103 * @param taxonBaseUuid
104 * a {@link java.util.UUID} object.
105 * @throws org.eclipse.ui.PartInitException
108 public static void openTaxonBase(UUID taxonBaseUuid
)
109 throws PartInitException
{
110 TaxonEditorInput input
= TaxonEditorInput
111 .NewInstanceFromTaxonBase(taxonBaseUuid
);
117 * findEditorByTaxonNodeUuid
120 * @param taxonNodeUuid
121 * a {@link java.util.UUID} object.
122 * @return a {@link org.eclipse.ui.IEditorPart} object.
123 * @throws java.lang.Exception
126 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
128 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
129 return getActivePage().findEditor(input
);
133 * An uninitialized taxon is one that hasn't been saved yet. As such, it
134 * should appear in neither the list of recent names nor in the taxonomic
137 * @throws org.eclipse.ui.PartInitException
139 * @param parentNodeUuid
140 * a {@link java.util.UUID} object.
142 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
143 TaxonEditorInput input
= TaxonEditorInput
144 .NewEmptyInstance(parentNodeUuid
);
145 open(input
, MultiPageTaxonEditor
.ID
);
147 getActiveMultiPageTaxonEditor().changed(null);
159 public static void setSaving(boolean isSaving
) {
160 EditorUtil
.isSaving
= isSaving
;
170 public static boolean isSaving() {
175 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
177 * @return a {@link java.util.Set} object.
179 public static Set
<IEditorPart
> getOpenEditors() {
180 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
182 if (getActivePage() != null) {
183 for (IEditorReference reference
: getActivePage()
184 .getEditorReferences()) {
185 IEditorPart editor
= reference
.getEditor(false);
186 if (editor
instanceof MultiPageTaxonEditor
) {
187 taxonEditors
.add(editor
);
196 * Returns the currently active taxon editor
198 * @return the taxon editor that has focus
200 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
201 IEditorPart editorPart
= getActiveEditor();
202 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
203 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
204 editor
.getConversationHolder().bind();
212 * getActiveEditorPage
216 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
217 * @return a {@link org.eclipse.ui.IEditorPart} object.
219 public static IEditorPart
getActiveEditorPage(Page page
) {
220 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
222 return editor
!= null ? editor
.getPage(page
) : null;
226 * Returns the selection of the currently active taxon editor
228 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
230 public static ISelection
getCurrentSelection() {
231 if (getActiveMultiPageTaxonEditor() == null) {
234 return getActiveMultiPageTaxonEditor().getSite()
235 .getSelectionProvider().getSelection();
245 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
247 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
250 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
251 return editor
.getUndoContext();
259 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
262 public static IUndoContext
getUndoContext() {
263 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
272 * a {@link org.eclipse.ui.IEditorPart} object.
274 * a {@link org.eclipse.swt.widgets.Shell} object.
277 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
278 if (editor
.isDirty()) {
280 boolean doSave
= MessageDialog
281 .openConfirm(shell
, "Confirm save",
282 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
283 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
289 editor
.doSave(EditorUtil
.getMonitor());
300 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
301 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
303 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
304 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
306 return (IStructuredSelection
) activeEditor
.getSite()
307 .getSelectionProvider().getSelection();
315 * @return a {@link java.lang.String} object.
317 public static String
getPluginId() {
318 return TaxeditorEditorPlugin
.PLUGIN_ID
;
321 public static void openPolytomousKey(UUID polytomousKeyUuid
)
323 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
324 .NewInstance(polytomousKeyUuid
);
328 public static void openCdmAuthorities(UUID groupUuid
)
330 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);