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
.AbstractCdmDataViewer
;
33 import eu
.etaxonomy
.taxeditor
.editor
.view
.detail
.DetailsViewPart
;
34 import eu
.etaxonomy
.taxeditor
.editor
.view
.detail
.DetailsViewer
;
35 import eu
.etaxonomy
.taxeditor
.editor
.view
.supplementaldata
.SupplementalDataViewPart
;
36 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
39 * Utility for the editor package
45 public class EditorUtil
extends AbstractUtility
{
47 private static boolean isSaving
= false;
50 * Opens a new editor window with the given input
56 * @throws PartInitException
58 private static IEditorPart
open(final IEditorInput input
, final String editorId
) throws PartInitException
{
59 return getActivePage().openEditor(input
, editorId
);
63 * Opens a new editor window with the given TaxonEditorInput
65 * @param input a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput} object.
66 * @throws org.eclipse.ui.PartInitException if any.
68 public static void open(TaxonEditorInput input
) throws PartInitException
{
69 open(input
, MultiPageTaxonEditor
.ID
);
72 public static void open(PolytomousKeyEditorInput input
) throws PartInitException
{
73 open(input
, PolytomousKeyEditor
.ID
);
77 * Taxon Editors may be opened by supplying a taxon node uuid.
78 * Session gets initialised here and is passed to the editor
80 * @param taxonNodeUuid a {@link java.util.UUID} object.
81 * @throws java.lang.Exception if any.
83 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
84 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
89 * <p>openTaxonBase</p>
91 * @param taxonBaseUuid a {@link java.util.UUID} object.
92 * @throws org.eclipse.ui.PartInitException if any.
94 public static void openTaxonBase(UUID taxonBaseUuid
) throws PartInitException
{
95 TaxonEditorInput input
= TaxonEditorInput
.NewInstanceFromTaxonBase(taxonBaseUuid
);
101 * <p>findEditorByTaxonNodeUuid</p>
103 * @param taxonNodeUuid a {@link java.util.UUID} object.
104 * @return a {@link org.eclipse.ui.IEditorPart} object.
105 * @throws java.lang.Exception if any.
107 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
) throws Exception
{
108 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
109 return getActivePage().findEditor(input
);
113 * An uninitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
114 * the list of recent names nor in the taxonomic tree when opened.
116 * @throws org.eclipse.ui.PartInitException if any.
117 * @param parentNodeUuid a {@link java.util.UUID} object.
119 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
120 TaxonEditorInput input
= TaxonEditorInput
.NewEmptyInstance(parentNodeUuid
);
121 open(input
, MultiPageTaxonEditor
.ID
);
123 getActiveMultiPageTaxonEditor().changed(null);
131 * @param isSaving a boolean.
133 public static void setSaving(boolean isSaving
) {
134 EditorUtil
.isSaving
= isSaving
;
142 public static boolean isSaving() {
147 * Returns a set of all currently open
148 * <code>MultiPageTaxonEditor</code>s.
150 * @return a {@link java.util.Set} object.
152 public static Set
<IEditorPart
> getOpenEditors() {
153 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
155 if(getActivePage() != null){
156 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
157 IEditorPart editor
= reference
.getEditor(false);
158 if (editor
instanceof MultiPageTaxonEditor
) {
159 taxonEditors
.add(editor
);
168 * <p>getActiveEditor</p>
170 * @return a {@link org.eclipse.ui.IEditorPart} object.
172 public static IEditorPart
getActiveEditor(){
173 return getActivePage() != null ?
getActivePage().getActiveEditor() : null;
177 * Returns the currently active taxon editor
179 * @return the taxon editor that has focus
181 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor(){
182 IEditorPart editorPart
= getActiveEditor();
183 if(editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
){
184 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
185 editor
.getConversationHolder().bind();
192 * <p>getActiveEditorPage</p>
194 * @param page a {@link eu.etaxonomy.taxeditor.editor.Page} object.
195 * @return a {@link org.eclipse.ui.IEditorPart} object.
197 public static IEditorPart
getActiveEditorPage(Page page
){
198 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
200 return editor
.getPage(page
);
204 * Returns the selection of the currently active taxon editor
206 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
208 public static ISelection
getCurrentSelection(){
209 if(getActiveMultiPageTaxonEditor() == null){
212 return getActiveMultiPageTaxonEditor().getSite().getSelectionProvider().getSelection();
217 * <p>getUndoContext</p>
219 * @param editor a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor} object.
220 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
222 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
){
223 return editor
.getUndoContext();
227 * <p>getUndoContext</p>
229 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
231 public static IUndoContext
getUndoContext() {
232 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
237 * <p>forceUserSave</p>
239 * @param editor a {@link org.eclipse.ui.IEditorPart} object.
240 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
243 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
244 if (editor
.isDirty()) {
246 boolean doSave
= MessageDialog
.openConfirm(shell
, "Confirm save", "The current editor must be saved before this action can be executed.");
252 editor
.doSave(EditorUtil
.getMonitor());
258 * <p>getDetailsView</p>
260 * @return a {@link eu.etaxonomy.taxeditor.editor.view.detail.DetailsViewPart} object.
262 public static DetailsViewPart
getDetailsView(){
263 return (DetailsViewPart
) EditorUtil
.getView(DetailsViewPart
.ID
, false);
267 * <p>refreshDetailsViewer</p>
269 public static void refreshDetailsViewer(){
270 ((AbstractCdmDataViewer
) getDetailsView().getViewer()).refresh();
274 * <p>reflowDetailsViewer</p>
276 public static void reflowDetailsViewer(){
277 ((DetailsViewer
) getDetailsView().getViewer()).reflow();
280 public static SupplementalDataViewPart
getSupplementalDataView(){
281 return (SupplementalDataViewPart
) EditorUtil
.getView(SupplementalDataViewPart
.ID
, false);
284 public static void reflowSupplementalViewer(){
285 ((AbstractCdmDataViewer
) getSupplementalDataView().getViewer()).reflow();
291 * <p>getSelection</p>
293 * @param event 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().getSelectionProvider().getSelection();
305 * @return a {@link java.lang.String} object.
307 protected static String
getPluginId(){
308 return TaxeditorEditorPlugin
.PLUGIN_ID
;
311 public static void openPolytomousKey(UUID polytomousKeyUuid
) throws Exception
{
312 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
.NewInstance(polytomousKeyUuid
);