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
.model
.AbstractUtility
;
33 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmDataViewer
;
34 import eu
.etaxonomy
.taxeditor
.view
.detail
.DetailsViewPart
;
35 import eu
.etaxonomy
.taxeditor
.view
.supplementaldata
.SupplementalDataViewPart
;
38 * Utility for the editor package
44 public class EditorUtil
extends AbstractUtility
{
46 private static boolean isSaving
= false;
49 * Opens a new editor window with the given input
55 * @throws PartInitException
57 private static IEditorPart
open(final IEditorInput input
, final String editorId
) throws PartInitException
{
58 return getActivePage().openEditor(input
, editorId
);
62 * Opens a new editor window with the given TaxonEditorInput
64 * @param input a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
65 * @throws org.eclipse.ui.PartInitException if any.
67 public static void open(TaxonEditorInput input
) throws PartInitException
{
68 open(input
, MultiPageTaxonEditor
.ID
);
71 public static void open(PolytomousKeyEditorInput input
) throws PartInitException
{
72 open(input
, PolytomousKeyEditor
.ID
);
76 * Taxon Editors may be opened by supplying a taxon node uuid.
77 * Session gets initialised here and is passed to the editor
79 * @param taxonNodeUuid a {@link java.util.UUID} object.
80 * @throws java.lang.Exception if any.
82 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
83 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
88 * <p>openTaxonBase</p>
90 * @param taxonBaseUuid a {@link java.util.UUID} object.
91 * @throws org.eclipse.ui.PartInitException if any.
93 public static void openTaxonBase(UUID taxonBaseUuid
) throws PartInitException
{
94 TaxonEditorInput input
= TaxonEditorInput
.NewInstanceFromTaxonBase(taxonBaseUuid
);
100 * <p>findEditorByTaxonNodeUuid</p>
102 * @param taxonNodeUuid a {@link java.util.UUID} object.
103 * @return a {@link org.eclipse.ui.IEditorPart} object.
104 * @throws java.lang.Exception if any.
106 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
) throws Exception
{
107 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
108 return getActivePage().findEditor(input
);
112 * An uninitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
113 * the list of recent names nor in the taxonomic tree when opened.
115 * @throws org.eclipse.ui.PartInitException if any.
116 * @param parentNodeUuid a {@link java.util.UUID} object.
118 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
119 TaxonEditorInput input
= TaxonEditorInput
.NewEmptyInstance(parentNodeUuid
);
120 open(input
, MultiPageTaxonEditor
.ID
);
122 getActiveMultiPageTaxonEditor().changed(null);
130 * @param isSaving a boolean.
132 public static void setSaving(boolean isSaving
) {
133 EditorUtil
.isSaving
= isSaving
;
141 public static boolean isSaving() {
146 * Returns a set of all currently open
147 * <code>MultiPageTaxonEditor</code>s.
149 * @return a {@link java.util.Set} object.
151 public static Set
<IEditorPart
> getOpenEditors() {
152 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
154 if(getActivePage() != null){
155 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
156 IEditorPart editor
= reference
.getEditor(false);
157 if (editor
instanceof MultiPageTaxonEditor
) {
158 taxonEditors
.add(editor
);
167 * Returns the currently active taxon editor
169 * @return the taxon editor that has focus
171 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor(){
172 IEditorPart editorPart
= getActiveEditor();
173 if(editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
){
174 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
175 editor
.getConversationHolder().bind();
182 * <p>getActiveEditorPage</p>
184 * @param page a {@link eu.etaxonomy.taxeditor.editor.Page} object.
185 * @return a {@link org.eclipse.ui.IEditorPart} object.
187 public static IEditorPart
getActiveEditorPage(Page page
){
188 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
190 return editor
!= null ? editor
.getPage(page
) : null;
194 * Returns the selection of the currently active taxon editor
196 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
198 public static ISelection
getCurrentSelection(){
199 if(getActiveMultiPageTaxonEditor() == null){
202 return getActiveMultiPageTaxonEditor().getSite().getSelectionProvider().getSelection();
207 * <p>getUndoContext</p>
209 * @param editor a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor} object.
210 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
212 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
){
213 return editor
.getUndoContext();
217 * <p>getUndoContext</p>
219 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
221 public static IUndoContext
getUndoContext() {
222 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
227 * <p>forceUserSave</p>
229 * @param editor a {@link org.eclipse.ui.IEditorPart} object.
230 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
233 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
234 if (editor
.isDirty()) {
236 boolean doSave
= MessageDialog
.openConfirm(shell
, "Confirm save", "The current editor must be saved before this action can be executed.");
242 editor
.doSave(EditorUtil
.getMonitor());
248 * <p>getSelection</p>
250 * @param event a {@link org.eclipse.core.commands.ExecutionEvent} object.
251 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
253 public static IStructuredSelection
getSelection(ExecutionEvent event
){
254 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
256 return (IStructuredSelection
) activeEditor
.getSite().getSelectionProvider().getSelection();
262 * @return a {@link java.lang.String} object.
264 protected static String
getPluginId(){
265 return TaxeditorEditorPlugin
.PLUGIN_ID
;
268 public static void openPolytomousKey(UUID polytomousKeyUuid
) throws Exception
{
269 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
.NewInstance(polytomousKeyUuid
);