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
.internal
.TaxeditorEditorPlugin
;
30 import eu
.etaxonomy
.taxeditor
.editor
.key
.KeyEditor
;
31 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorInput
;
32 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
35 * Utility for the editor package
41 public class EditorUtil
extends AbstractUtility
{
43 private static boolean isSaving
= false;
46 * Opens a new editor window with the given input
52 * @throws PartInitException
54 private static IEditorPart
open(final IEditorInput input
,
55 final String editorId
) throws PartInitException
{
56 return getActivePage().openEditor(input
, editorId
);
60 * Opens a new editor window with the given TaxonEditorInput
63 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
65 * @throws org.eclipse.ui.PartInitException
68 public static void open(TaxonEditorInput input
) throws PartInitException
{
69 open(input
, MultiPageTaxonEditor
.ID
);
72 public static void open(PolytomousKeyEditorInput input
)
73 throws PartInitException
{
74 open(input
, KeyEditor
.ID
);
78 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
79 * initialised here and is passed to the editor
81 * @param taxonNodeUuid
82 * a {@link java.util.UUID} object.
83 * @throws java.lang.Exception
86 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
87 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
96 * @param taxonBaseUuid
97 * a {@link java.util.UUID} object.
98 * @throws org.eclipse.ui.PartInitException
101 public static void openTaxonBase(UUID taxonBaseUuid
)
102 throws PartInitException
{
103 TaxonEditorInput input
= TaxonEditorInput
104 .NewInstanceFromTaxonBase(taxonBaseUuid
);
110 * findEditorByTaxonNodeUuid
113 * @param taxonNodeUuid
114 * a {@link java.util.UUID} object.
115 * @return a {@link org.eclipse.ui.IEditorPart} object.
116 * @throws java.lang.Exception
119 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
121 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
122 return getActivePage().findEditor(input
);
126 * An uninitialized taxon is one that hasn't been saved yet. As such, it
127 * should appear in neither the list of recent names nor in the taxonomic
130 * @throws org.eclipse.ui.PartInitException
132 * @param parentNodeUuid
133 * a {@link java.util.UUID} object.
135 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
136 TaxonEditorInput input
= TaxonEditorInput
137 .NewEmptyInstance(parentNodeUuid
);
138 open(input
, MultiPageTaxonEditor
.ID
);
140 getActiveMultiPageTaxonEditor().changed(null);
152 public static void setSaving(boolean isSaving
) {
153 EditorUtil
.isSaving
= isSaving
;
163 public static boolean isSaving() {
168 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
170 * @return a {@link java.util.Set} object.
172 public static Set
<IEditorPart
> getOpenEditors() {
173 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
175 if (getActivePage() != null) {
176 for (IEditorReference reference
: getActivePage()
177 .getEditorReferences()) {
178 IEditorPart editor
= reference
.getEditor(false);
179 if (editor
instanceof MultiPageTaxonEditor
) {
180 taxonEditors
.add(editor
);
189 * Returns the currently active taxon editor
191 * @return the taxon editor that has focus
193 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
194 IEditorPart editorPart
= getActiveEditor();
195 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
196 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
197 editor
.getConversationHolder().bind();
205 * getActiveEditorPage
209 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
210 * @return a {@link org.eclipse.ui.IEditorPart} object.
212 public static IEditorPart
getActiveEditorPage(Page page
) {
213 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
215 return editor
!= null ? editor
.getPage(page
) : null;
219 * Returns the selection of the currently active taxon editor
221 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
223 public static ISelection
getCurrentSelection() {
224 if (getActiveMultiPageTaxonEditor() == null) {
227 return getActiveMultiPageTaxonEditor().getSite()
228 .getSelectionProvider().getSelection();
238 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
240 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
243 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
244 return editor
.getUndoContext();
252 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
255 public static IUndoContext
getUndoContext() {
256 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
265 * a {@link org.eclipse.ui.IEditorPart} object.
267 * a {@link org.eclipse.swt.widgets.Shell} object.
270 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
271 if (editor
.isDirty()) {
273 boolean doSave
= MessageDialog
274 .openConfirm(shell
, "Confirm save",
275 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
276 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
282 editor
.doSave(EditorUtil
.getMonitor());
293 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
294 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
296 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
297 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
299 return (IStructuredSelection
) activeEditor
.getSite()
300 .getSelectionProvider().getSelection();
308 * @return a {@link java.lang.String} object.
310 public static String
getPluginId() {
311 return TaxeditorEditorPlugin
.PLUGIN_ID
;
314 public static void openPolytomousKey(UUID polytomousKeyUuid
)
316 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
317 .NewInstance(polytomousKeyUuid
);