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
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.commands
.ExecutionException
;
19 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
20 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
21 import org
.eclipse
.core
.commands
.operations
.IUndoableOperation
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.IStatus
;
24 import org
.eclipse
.core
.runtime
.Status
;
25 import org
.eclipse
.jface
.action
.IStatusLineManager
;
26 import org
.eclipse
.ui
.IEditorInput
;
27 import org
.eclipse
.ui
.IEditorPart
;
28 import org
.eclipse
.ui
.IEditorReference
;
29 import org
.eclipse
.ui
.IPageLayout
;
30 import org
.eclipse
.ui
.IViewPart
;
31 import org
.eclipse
.ui
.PartInitException
;
32 import org
.eclipse
.ui
.ide
.undo
.WorkspaceUndoUtil
;
34 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
35 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.taxeditor
.editor
.description
.TaxonDescriptionEditor
;
38 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
39 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
40 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
41 import eu
.etaxonomy
.taxeditor
.store
.model
.AbstractUtility
;
42 import eu
.etaxonomy
.taxeditor
.store
.preference
.PreferencesUtil
;
49 public class EditorUtil
extends AbstractUtility
{
50 private static final Logger logger
= Logger
.getLogger(EditorUtil
.class);
52 // TODO I am not quite sure if this is the right approach
53 private static Map
<Taxon
, TaxonNameEditor
> taxonNameEditors
;
54 private static Map
<Taxon
, TaxonDescriptionEditor
> taxonDescriptionEditors
;
56 private static boolean isSaving
= false;
58 private static IStatusLineManager statusLineManager
;
60 private static IViewPart view
;
65 * @throws PartInitException
67 private static IEditorPart
getEditorByInput(IEditorInput input
)
68 throws PartInitException
{
69 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
70 if (reference
.getEditorInput().equals(input
)) {
71 IEditorPart editor
= reference
.getEditor(false);
78 public static boolean closeAll() {
79 for (IEditorPart editor
: getOpenEditors()) {
80 if (!getActivePage().closeEditor(editor
, true)) {
88 * Close the given editor.
90 * @param editor The <tt>MultipageTaxonEditor</tt> to close.
91 * @return <tt>true</tt> on success
93 public static boolean close(MultiPageTaxonEditor editor
) {
94 return getActivePage().closeEditor(editor
, true);
98 * Opens a new editor window with the given input
103 * @throws PartInitException
105 private static IEditorPart
open(IEditorInput input
, String editorId
)
106 throws PartInitException
{
107 return getActivePage().openEditor(input
, editorId
);
112 * Taxon Editors may be opened by supplying a taxon uuid only.
113 * Session gets initialised here and is passed to the editor
117 * @throws PartInitException
119 public static IEditorPart
open(UUID uuid
) throws PartInitException
{
120 IEditorInput input
= TaxonEditorInput
.NewInstance(uuid
);
121 return open(input
, MultiPageTaxonEditor
.ID
);
125 * An unitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
126 * the list of recent names nor in the taxonomic tree when opened.
128 * @param parentTaxon The UUID of the parental taxon or <code>null</code> if this is
129 * supposed to be a root taxon.
130 * @return The EditorPart.
131 * @throws PartInitException
133 public static IEditorPart
openEmpty(UUID parentTaxonUuid
) throws PartInitException
{
134 TaxonEditorInput input
= TaxonEditorInput
.NewEmptyInstance(parentTaxonUuid
);
135 return open(input
, MultiPageTaxonEditor
.ID
);
139 private static boolean redraw(AbstractTaxonEditor editor
) {
140 if(editor
== null || editor
.redraw()){
142 // if (getActiveEditor().setFocus())
145 // Mark editor as changed and unsaved
153 public static void setSaving(boolean isSaving
) {
154 EditorUtil
.isSaving
= isSaving
;
157 public static boolean isSaving() {
162 * Returns a set of all currently open
163 * <code>MultiPageTaxonEditor</code>s.
167 public static Set
<IEditorPart
> getOpenEditors() {
168 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
170 if(getActivePage() != null){
171 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
172 IEditorPart editor
= reference
.getEditor(false);
173 if (editor
instanceof MultiPageTaxonEditor
) {
174 taxonEditors
.add(editor
);
183 * Returns the currently active taxon editor
185 * @return the taxon editor that has focus
187 public static MultiPageTaxonEditor
getActiveEditor(){
188 IEditorPart editorPart
= getActivePage().getActiveEditor();
189 if(editorPart
instanceof MultiPageTaxonEditor
){
190 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
191 editor
.getConversationHolder().bind();
197 public static AbstractTaxonEditor
getActiveEditorPage(Page page
){
198 MultiPageTaxonEditor editor
= getActiveEditor();
200 return editor
.getPage(page
);
203 public static IOperationHistory
getOperationHistory() {
204 return TaxeditorEditorPlugin
.getDefault().getWorkbench().
205 getOperationSupport().getOperationHistory();
208 public static IStatus
executeOperation(IUndoableOperation operation
){
210 IStatus status
= getOperationHistory().execute(operation
, getMonitor(),
211 WorkspaceUndoUtil
.getUIInfoAdapter(getShell()));
212 String statusString
= status
.equals(Status
.OK_STATUS
) ?
"completed" : "cancelled";
213 setStatusLine(operation
.getLabel() + " " + statusString
+ ".");
215 } catch (ExecutionException e
) {
216 logger
.error("Error executing operation: " + operation
.getLabel(), e
);
221 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
){
222 return editor
.getUndoContext();
225 public static IUndoContext
getUndoContext() {
226 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
229 private static IProgressMonitor
getMonitor() {
230 statusLineManager
.setCancelEnabled(false);
231 return statusLineManager
.getProgressMonitor();
234 public static void setStatusLineManager(IStatusLineManager manager
) {
235 statusLineManager
= manager
;
238 public static void setStatusLine(String message
) {
239 statusLineManager
.setMessage(message
);
242 public static void showPropertySheet() {
243 if (getActivePage() == null) {
247 view
= getActivePage().showView(IPageLayout
.ID_PROP_SHEET
);
248 } catch (PartInitException e
) {
249 // TODO Auto-generated catch block
254 public static void checkHidePropertySheet() {
255 if (getOpenEditors().size() == 0) {
256 if (view
!= null && getActivePage() != null) {
257 getActivePage().hideView(view
);