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
.apache
.log4j
.Logger
;
17 import org
.eclipse
.core
.commands
.ExecutionException
;
18 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
19 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
20 import org
.eclipse
.core
.commands
.operations
.IUndoableOperation
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.Status
;
24 import org
.eclipse
.jface
.action
.IStatusLineManager
;
25 import org
.eclipse
.ui
.IEditorInput
;
26 import org
.eclipse
.ui
.IEditorPart
;
27 import org
.eclipse
.ui
.IEditorReference
;
28 import org
.eclipse
.ui
.IPageLayout
;
29 import org
.eclipse
.ui
.IViewPart
;
30 import org
.eclipse
.ui
.PartInitException
;
31 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
33 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
34 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
41 public class EditorUtil
extends AbstractUtility
{
42 private static final Logger logger
= Logger
.getLogger(EditorUtil
.class);
44 private static boolean isSaving
= false;
46 private static IStatusLineManager statusLineManager
;
48 private static IViewPart view
;
50 public static boolean closeAll() {
51 for (IEditorPart editor
: getOpenEditors()) {
52 if (!getActivePage().closeEditor(editor
, true)) {
60 * Close the given editor.
62 * @param editor The <tt>MultipageTaxonEditor</tt> to close.
63 * @return <tt>true</tt> on success
65 public static boolean close(MultiPageTaxonEditor editor
) {
66 return getActivePage().closeEditor(editor
, true);
70 * Opens a new editor window with the given input
75 * @throws PartInitException
77 private static IEditorPart
open(IEditorInput input
, String editorId
)
78 throws PartInitException
{
79 return getActivePage().openEditor(input
, editorId
);
84 * Taxon Editors may be opened by supplying a taxon uuid only.
85 * Session gets initialised here and is passed to the editor
89 * @throws PartInitException
91 public static IEditorPart
open(UUID uuid
) throws PartInitException
{
92 IEditorInput input
= TaxonEditorInput
.NewInstance(uuid
);
93 return open(input
, MultiPageTaxonEditor
.ID
);
97 * An unitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
98 * the list of recent names nor in the taxonomic tree when opened.
100 * @param parentTaxon The UUID of the parental taxon or <code>null</code> if this is
101 * supposed to be a root taxon.
102 * @return The EditorPart.
103 * @throws PartInitException
105 public static IEditorPart
openEmpty(UUID parentTaxonUuid
) throws PartInitException
{
106 TaxonEditorInput input
= TaxonEditorInput
.NewEmptyInstance(parentTaxonUuid
);
107 return open(input
, MultiPageTaxonEditor
.ID
);
110 public static void setSaving(boolean isSaving
) {
111 EditorUtil
.isSaving
= isSaving
;
114 public static boolean isSaving() {
119 * Returns a set of all currently open
120 * <code>MultiPageTaxonEditor</code>s.
124 public static Set
<IEditorPart
> getOpenEditors() {
125 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
127 if(getActivePage() != null){
128 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
129 IEditorPart editor
= reference
.getEditor(false);
130 if (editor
instanceof MultiPageTaxonEditor
) {
131 taxonEditors
.add(editor
);
140 * Returns the currently active taxon editor
142 * @return the taxon editor that has focus
144 public static MultiPageTaxonEditor
getActiveEditor(){
145 IEditorPart editorPart
= getActivePage().getActiveEditor();
146 if(editorPart
instanceof MultiPageTaxonEditor
){
147 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
148 editor
.getConversationHolder().bind();
154 public static AbstractTaxonEditor
getActiveEditorPage(Page page
){
155 MultiPageTaxonEditor editor
= getActiveEditor();
157 return editor
.getPage(page
);
160 public static IOperationHistory
getOperationHistory() {
161 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
162 getOperationSupport().getOperationHistory();
165 public static IStatus
executeOperation(IUndoableOperation operation
){
167 IStatus status
= getOperationHistory().execute(operation
, getMonitor(),
168 WorkspaceUndoUtil
.getUIInfoAdapter(getShell()));
169 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed" : "cancelled";
170 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
172 } catch (ExecutionException e
) {
173 logger
.error("Error executing operation: " + operation
.getLabel(), e
);
178 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
){
179 return editor
.getUndoContext();
182 public static IUndoContext
getUndoContext() {
183 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
186 private static IProgressMonitor
getMonitor() {
187 statusLineManager
.setCancelEnabled(false);
188 return statusLineManager
.getProgressMonitor();
191 public static void setStatusLineManager(IStatusLineManager manager
) {
192 statusLineManager
= manager
;
195 public static void setStatusLine(String message
) {
196 statusLineManager
.setMessage(message
);
199 public static void showPropertySheet() {
200 if (getActivePage() == null) {
204 view
= getActivePage().showView(IPageLayout
.ID_PROP_SHEET
);
205 } catch (PartInitException e
) {
206 // TODO Auto-generated catch block
211 public static void checkHidePropertySheet() {
212 if (getOpenEditors().size() == 0) {
213 if (view
!= null && getActivePage() != null) {
214 getActivePage().hideView(view
);