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
.operations
.IOperationHistory
;
17 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
18 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
19 import org
.eclipse
.swt
.widgets
.Shell
;
20 import org
.eclipse
.ui
.IEditorInput
;
21 import org
.eclipse
.ui
.IEditorPart
;
22 import org
.eclipse
.ui
.IEditorReference
;
23 import org
.eclipse
.ui
.IPageLayout
;
24 import org
.eclipse
.ui
.IViewPart
;
25 import org
.eclipse
.ui
.PartInitException
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
28 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
35 public class EditorUtil
extends AbstractUtility
{
37 private static boolean isSaving
= false;
39 private static IViewPart view
;
41 public static boolean closeAll() {
42 for (IEditorPart editor
: getOpenEditors()) {
43 if (!getActivePage().closeEditor(editor
, true)) {
51 * Close the given editor.
53 * @param editor The <tt>MultipageTaxonEditor</tt> to close.
54 * @return <tt>true</tt> on success
56 public static boolean close(MultiPageTaxonEditor editor
) {
57 return getActivePage().closeEditor(editor
, true);
61 * Opens a new editor window with the given input
66 * @throws PartInitException
68 private static IEditorPart
open(IEditorInput input
, String editorId
)
69 throws PartInitException
{
70 return getActivePage().openEditor(input
, editorId
);
75 * Taxon Editors may be opened by supplying a taxon uuid only.
76 * Session gets initialised here and is passed to the editor
78 * @param taxonNodeUuid
80 * @throws PartInitException
82 public static IEditorPart
open(UUID taxonNodeUuid
) throws PartInitException
{
83 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
84 return open(input
, MultiPageTaxonEditor
.ID
);
88 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
){
89 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
90 return getActivePage().findEditor(input
);
94 * An uninitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
95 * the list of recent names nor in the taxonomic tree when opened.
97 * @param parentTaxon The UUID of the parental taxon or <code>null</code> if this is
98 * supposed to be a root taxon.
99 * @return The EditorPart.
100 * @throws PartInitException
102 public static IEditorPart
openEmpty(UUID parentNodeUuid
) throws PartInitException
{
103 TaxonEditorInput input
= TaxonEditorInput
.NewEmptyInstance(parentNodeUuid
);
104 IEditorPart editorPart
= open(input
, MultiPageTaxonEditor
.ID
);
106 if(editorPart
instanceof MultiPageTaxonEditor
){
107 ((MultiPageTaxonEditor
) editorPart
).setDirty();
113 public static void setSaving(boolean isSaving
) {
114 EditorUtil
.isSaving
= isSaving
;
117 public static boolean isSaving() {
122 * Returns a set of all currently open
123 * <code>MultiPageTaxonEditor</code>s.
127 public static Set
<IEditorPart
> getOpenEditors() {
128 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
130 if(getActivePage() != null){
131 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
132 IEditorPart editor
= reference
.getEditor(false);
133 if (editor
instanceof MultiPageTaxonEditor
) {
134 taxonEditors
.add(editor
);
143 * Returns the currently active taxon editor
145 * @return the taxon editor that has focus
147 public static MultiPageTaxonEditor
getActiveEditor(){
148 IEditorPart editorPart
= getActivePage().getActiveEditor();
149 if(editorPart
instanceof MultiPageTaxonEditor
){
150 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
151 editor
.getConversationHolder().bind();
157 public static IEditorPart
getActiveEditorPage(Page page
){
158 MultiPageTaxonEditor editor
= getActiveEditor();
160 return editor
.getPage(page
);
163 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
){
164 return editor
.getUndoContext();
167 public static IUndoContext
getUndoContext() {
168 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
171 public static void showPropertySheet() {
172 if (getActivePage() == null) {
176 view
= getActivePage().showView(IPageLayout
.ID_PROP_SHEET
);
177 } catch (PartInitException e
) {
178 // TODO Auto-generated catch block
183 public static void checkHidePropertySheet() {
184 if (getOpenEditors().size() == 0) {
185 if (view
!= null && getActivePage() != null) {
186 getActivePage().hideView(view
);
196 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
197 if (editor
.isDirty()) {
199 boolean doSave
= MessageDialog
.openConfirm(shell
, "Confirm save", "The current editor must be saved before this action can be executed.");