51d351315fc38dc890318ee3b60a1372c4f5acb2
[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.Map;
14 import java.util.Set;
15 import java.util.UUID;
16
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;
33
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;
43
44 /**
45 * @author n.hoffmann
46 * @created 20.01.2009
47 * @version 1.0
48 */
49 public class EditorUtil extends AbstractUtility{
50 private static final Logger logger = Logger.getLogger(EditorUtil.class);
51
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;
55
56 private static boolean isSaving = false;
57
58 private static IStatusLineManager statusLineManager;
59
60 private static IViewPart view;
61
62 /**
63 * @param input
64 * @return
65 * @throws PartInitException
66 */
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);
72 return editor;
73 }
74 }
75 return null;
76 }
77
78 public static boolean closeAll() {
79 for (IEditorPart editor : getOpenEditors()) {
80 if (!getActivePage().closeEditor(editor, true)) {
81 return false;
82 }
83 }
84 return true;
85 }
86
87 /**
88 * Close the given editor.
89 *
90 * @param editor The <tt>MultipageTaxonEditor</tt> to close.
91 * @return <tt>true</tt> on success
92 */
93 public static boolean close(MultiPageTaxonEditor editor) {
94 return getActivePage().closeEditor(editor, true);
95 }
96
97 /**
98 * Opens a new editor window with the given input
99 *
100 * @param input
101 * @param editorId
102 * @return
103 * @throws PartInitException
104 */
105 private static IEditorPart open(IEditorInput input, String editorId)
106 throws PartInitException {
107 return getActivePage().openEditor(input, editorId);
108 }
109
110
111 /**
112 * Taxon Editors may be opened by supplying a taxon uuid only.
113 * Session gets initialised here and is passed to the editor
114 *
115 * @param uuid
116 * @return
117 * @throws PartInitException
118 */
119 public static IEditorPart open(UUID uuid) throws PartInitException {
120 IEditorInput input = TaxonEditorInput.NewInstance(uuid);
121 return open(input, MultiPageTaxonEditor.ID);
122 }
123
124 /**
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.
127 *
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
132 */
133 public static IEditorPart openEmpty(UUID parentTaxonUuid) throws PartInitException{
134 TaxonEditorInput input = TaxonEditorInput.NewEmptyInstance(parentTaxonUuid);
135 return open(input, MultiPageTaxonEditor.ID);
136 }
137
138
139 private static boolean redraw(AbstractTaxonEditor editor) {
140 if(editor == null || editor.redraw()){
141
142 // if (getActiveEditor().setFocus())
143 // getActivePage()
144
145 // Mark editor as changed and unsaved
146 editor.setDirty();
147 return true;
148 }
149 return false;
150 }
151
152
153 public static void setSaving(boolean isSaving) {
154 EditorUtil.isSaving = isSaving;
155 }
156
157 public static boolean isSaving() {
158 return isSaving;
159 }
160
161 /**
162 * Returns a set of all currently open
163 * <code>MultiPageTaxonEditor</code>s.
164 *
165 * @return
166 */
167 public static Set<IEditorPart> getOpenEditors() {
168 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
169
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);
175 }
176 }
177 }
178
179 return taxonEditors;
180 }
181
182 /**
183 * Returns the currently active taxon editor
184 *
185 * @return the taxon editor that has focus
186 */
187 public static MultiPageTaxonEditor getActiveEditor(){
188 IEditorPart editorPart = getActivePage().getActiveEditor();
189 if(editorPart instanceof MultiPageTaxonEditor){
190 MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
191 editor.getConversationHolder().bind();
192 return editor;
193 }
194 return null;
195 }
196
197 public static AbstractTaxonEditor getActiveEditorPage(Page page){
198 MultiPageTaxonEditor editor = getActiveEditor();
199
200 return editor.getPage(page);
201 }
202
203 public static IOperationHistory getOperationHistory() {
204 return TaxeditorEditorPlugin.getDefault().getWorkbench().
205 getOperationSupport().getOperationHistory();
206 }
207
208 public static IStatus executeOperation(IUndoableOperation operation){
209 try {
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 + ".");
214 return status;
215 } catch (ExecutionException e) {
216 logger.error("Error executing operation: " + operation.getLabel(), e);
217 }
218 return null;
219 }
220
221 public static IUndoContext getUndoContext(MultiPageTaxonEditor editor){
222 return editor.getUndoContext();
223 }
224
225 public static IUndoContext getUndoContext() {
226 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
227 }
228
229 private static IProgressMonitor getMonitor() {
230 statusLineManager.setCancelEnabled(false);
231 return statusLineManager.getProgressMonitor();
232 }
233
234 public static void setStatusLineManager(IStatusLineManager manager) {
235 statusLineManager = manager;
236 }
237
238 public static void setStatusLine(String message) {
239 statusLineManager.setMessage(message);
240 }
241
242 public static void showPropertySheet() {
243 if (getActivePage() == null) {
244 return;
245 }
246 try {
247 view = getActivePage().showView(IPageLayout.ID_PROP_SHEET);
248 } catch (PartInitException e) {
249 // TODO Auto-generated catch block
250 e.printStackTrace();
251 }
252 }
253
254 public static void checkHidePropertySheet() {
255 if (getOpenEditors().size() == 0) {
256 if (view != null && getActivePage() != null) {
257 getActivePage().hideView(view);
258 }
259 }
260 }
261 }