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
.polytomous
.PolytomousKeyEditor
;
31 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorInput
;
32 import eu
.etaxonomy
.taxeditor
.editor
.view
.detail
.DetailsViewPart
;
33 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
36 * Utility for the editor package
42 public class EditorUtil
extends AbstractUtility
{
44 private static boolean isSaving
= false;
47 * Opens a new editor window with the given input
53 * @throws PartInitException
55 private static IEditorPart
open(final IEditorInput input
, final String editorId
) throws PartInitException
{
56 return getActivePage().openEditor(input
, editorId
);
60 * Opens a new editor window with the given TaxonEditorInput
62 * @param input a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
63 * @throws org.eclipse.ui.PartInitException if any.
65 public static void open(TaxonEditorInput input
) throws PartInitException
{
66 open(input
, MultiPageTaxonEditor
.ID
);
69 public static void open(PolytomousKeyEditorInput input
) throws PartInitException
{
70 open(input
, PolytomousKeyEditor
.ID
);
74 * Taxon Editors may be opened by supplying a taxon node uuid.
75 * Session gets initialised here and is passed to the editor
77 * @param taxonNodeUuid a {@link java.util.UUID} object.
78 * @throws java.lang.Exception if any.
80 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
81 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
86 * <p>openTaxonBase</p>
88 * @param taxonBaseUuid a {@link java.util.UUID} object.
89 * @throws org.eclipse.ui.PartInitException if any.
91 public static void openTaxonBase(UUID taxonBaseUuid
) throws PartInitException
{
92 TaxonEditorInput input
= TaxonEditorInput
.NewInstanceFromTaxonBase(taxonBaseUuid
);
98 * <p>findEditorByTaxonNodeUuid</p>
100 * @param taxonNodeUuid a {@link java.util.UUID} object.
101 * @return a {@link org.eclipse.ui.IEditorPart} object.
102 * @throws java.lang.Exception if any.
104 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
) throws Exception
{
105 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
106 return getActivePage().findEditor(input
);
110 * An uninitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
111 * the list of recent names nor in the taxonomic tree when opened.
113 * @throws org.eclipse.ui.PartInitException if any.
114 * @param parentNodeUuid a {@link java.util.UUID} object.
116 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
117 TaxonEditorInput input
= TaxonEditorInput
.NewEmptyInstance(parentNodeUuid
);
118 open(input
, MultiPageTaxonEditor
.ID
);
120 getActiveMultiPageTaxonEditor().changed(null);
128 * @param isSaving a boolean.
130 public static void setSaving(boolean isSaving
) {
131 EditorUtil
.isSaving
= isSaving
;
139 public static boolean isSaving() {
144 * Returns a set of all currently open
145 * <code>MultiPageTaxonEditor</code>s.
147 * @return a {@link java.util.Set} object.
149 public static Set
<IEditorPart
> getOpenEditors() {
150 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
152 if(getActivePage() != null){
153 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
154 IEditorPart editor
= reference
.getEditor(false);
155 if (editor
instanceof MultiPageTaxonEditor
) {
156 taxonEditors
.add(editor
);
165 * <p>getActiveEditor</p>
167 * @return a {@link org.eclipse.ui.IEditorPart} object.
169 public static IEditorPart
getActiveEditor(){
170 return getActivePage() != null ?
getActivePage().getActiveEditor() : null;
174 * Returns the currently active taxon editor
176 * @return the taxon editor that has focus
178 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor(){
179 IEditorPart editorPart
= getActiveEditor();
180 if(editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
){
181 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
182 editor
.getConversationHolder().bind();
189 * <p>getActiveEditorPage</p>
191 * @param page a {@link eu.etaxonomy.taxeditor.editor.Page} object.
192 * @return a {@link org.eclipse.ui.IEditorPart} object.
194 public static IEditorPart
getActiveEditorPage(Page page
){
195 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
197 return editor
.getPage(page
);
201 * Returns the selection of the currently active taxon editor
203 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
205 public static ISelection
getCurrentSelection(){
206 if(getActiveMultiPageTaxonEditor() == null){
209 return getActiveMultiPageTaxonEditor().getSite().getSelectionProvider().getSelection();
214 * <p>getUndoContext</p>
216 * @param editor a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor} object.
217 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
219 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
){
220 return editor
.getUndoContext();
224 * <p>getUndoContext</p>
226 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
228 public static IUndoContext
getUndoContext() {
229 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
234 * <p>forceUserSave</p>
236 * @param editor a {@link org.eclipse.ui.IEditorPart} object.
237 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
240 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
241 if (editor
.isDirty()) {
243 boolean doSave
= MessageDialog
.openConfirm(shell
, "Confirm save", "The current editor must be saved before this action can be executed.");
249 editor
.doSave(EditorUtil
.getMonitor());
255 * <p>getDetailsView</p>
257 * @return a {@link eu.etaxonomy.taxeditor.editor.view.detail.DetailsViewPart} object.
259 public static DetailsViewPart
getDetailsView(){
260 return (DetailsViewPart
) EditorUtil
.showView(DetailsViewPart
.ID
);
264 * <p>refreshDetailsViewer</p>
266 public static void refreshDetailsViewer(){
267 getDetailsView().getDetailsViewer().refresh();
271 * <p>reflowDetailsViewer</p>
273 public static void reflowDetailsViewer(){
274 getDetailsView().getDetailsViewer().reflow();
278 * <p>getSelection</p>
280 * @param event a {@link org.eclipse.core.commands.ExecutionEvent} object.
281 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
283 public static IStructuredSelection
getSelection(ExecutionEvent event
){
284 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
286 return (IStructuredSelection
) activeEditor
.getSite().getSelectionProvider().getSelection();
292 * @return a {@link java.lang.String} object.
294 protected static String
getPluginId(){
295 return TaxeditorEditorPlugin
.PLUGIN_ID
;
298 public static void openPolytomousKey(UUID polytomousKeyUuid
) throws Exception
{
299 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
.NewInstance(polytomousKeyUuid
);