8df96a7ea99925fe9ab51e8b843bbb5af6963abf
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / EditorUtil.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor;
11
12 import java.util.HashSet;
13 import java.util.Set;
14 import java.util.UUID;
15
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;
32
33 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
34 import eu.etaxonomy.taxeditor.store.model.AbstractUtility;
35
36 /**
37 * @author n.hoffmann
38 * @created 20.01.2009
39 * @version 1.0
40 */
41 public class EditorUtil extends AbstractUtility{
42 private static final Logger logger = Logger.getLogger(EditorUtil.class);
43
44 private static boolean isSaving = false;
45
46 private static IStatusLineManager statusLineManager;
47
48 private static IViewPart view;
49
50 public static boolean closeAll() {
51 for (IEditorPart editor : getOpenEditors()) {
52 if (!getActivePage().closeEditor(editor, true)) {
53 return false;
54 }
55 }
56 return true;
57 }
58
59 /**
60 * Close the given editor.
61 *
62 * @param editor The <tt>MultipageTaxonEditor</tt> to close.
63 * @return <tt>true</tt> on success
64 */
65 public static boolean close(MultiPageTaxonEditor editor) {
66 return getActivePage().closeEditor(editor, true);
67 }
68
69 /**
70 * Opens a new editor window with the given input
71 *
72 * @param input
73 * @param editorId
74 * @return
75 * @throws PartInitException
76 */
77 private static IEditorPart open(IEditorInput input, String editorId)
78 throws PartInitException {
79 return getActivePage().openEditor(input, editorId);
80 }
81
82
83 /**
84 * Taxon Editors may be opened by supplying a taxon uuid only.
85 * Session gets initialised here and is passed to the editor
86 *
87 * @param uuid
88 * @return
89 * @throws PartInitException
90 */
91 public static IEditorPart open(UUID uuid) throws PartInitException {
92 IEditorInput input = TaxonEditorInput.NewInstance(uuid);
93 return open(input, MultiPageTaxonEditor.ID);
94 }
95
96 /**
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.
99 *
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
104 */
105 public static IEditorPart openEmpty(UUID parentTaxonUuid) throws PartInitException{
106 TaxonEditorInput input = TaxonEditorInput.NewEmptyInstance(parentTaxonUuid);
107 return open(input, MultiPageTaxonEditor.ID);
108 }
109
110 public static void setSaving(boolean isSaving) {
111 EditorUtil.isSaving = isSaving;
112 }
113
114 public static boolean isSaving() {
115 return isSaving;
116 }
117
118 /**
119 * Returns a set of all currently open
120 * <code>MultiPageTaxonEditor</code>s.
121 *
122 * @return
123 */
124 public static Set<IEditorPart> getOpenEditors() {
125 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
126
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);
132 }
133 }
134 }
135
136 return taxonEditors;
137 }
138
139 /**
140 * Returns the currently active taxon editor
141 *
142 * @return the taxon editor that has focus
143 */
144 public static MultiPageTaxonEditor getActiveEditor(){
145 IEditorPart editorPart = getActivePage().getActiveEditor();
146 if(editorPart instanceof MultiPageTaxonEditor){
147 MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
148 editor.getConversationHolder().bind();
149 return editor;
150 }
151 return null;
152 }
153
154 public static AbstractTaxonEditor getActiveEditorPage(Page page){
155 MultiPageTaxonEditor editor = getActiveEditor();
156
157 return editor.getPage(page);
158 }
159
160 public static IOperationHistory getOperationHistory() {
161 return TaxeditorEditorPlugin.getDefault().getWorkbench().
162 getOperationSupport().getOperationHistory();
163 }
164
165 public static IStatus executeOperation(IUndoableOperation operation){
166 try {
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 + ".");
171 return status;
172 } catch (ExecutionException e) {
173 logger.error("Error executing operation: " + operation.getLabel(), e);
174 }
175 return null;
176 }
177
178 public static IUndoContext getUndoContext(MultiPageTaxonEditor editor){
179 return editor.getUndoContext();
180 }
181
182 public static IUndoContext getUndoContext() {
183 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
184 }
185
186 private static IProgressMonitor getMonitor() {
187 statusLineManager.setCancelEnabled(false);
188 return statusLineManager.getProgressMonitor();
189 }
190
191 public static void setStatusLineManager(IStatusLineManager manager) {
192 statusLineManager = manager;
193 }
194
195 public static void setStatusLine(String message) {
196 statusLineManager.setMessage(message);
197 }
198
199 public static void showPropertySheet() {
200 if (getActivePage() == null) {
201 return;
202 }
203 try {
204 view = getActivePage().showView(IPageLayout.ID_PROP_SHEET);
205 } catch (PartInitException e) {
206 // TODO Auto-generated catch block
207 e.printStackTrace();
208 }
209 }
210
211 public static void checkHidePropertySheet() {
212 if (getOpenEditors().size() == 0) {
213 if (view != null && getActivePage() != null) {
214 getActivePage().hideView(view);
215 }
216 }
217 }
218 }