Fixes #2235
[taxeditor.git] / eu.etaxonomy.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.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.operations.IOperationHistory;
18 import org.eclipse.core.commands.operations.IUndoContext;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IEditorReference;
26 import org.eclipse.ui.PartInitException;
27 import org.eclipse.ui.handlers.HandlerUtil;
28
29 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
30 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
31 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
32 import eu.etaxonomy.taxeditor.model.AbstractUtility;
33
34 /**
35 * Utility for the editor package
36 *
37 * @author n.hoffmann
38 * @created 20.01.2009
39 * @version 1.0
40 */
41 public class EditorUtil extends AbstractUtility {
42
43 private static boolean isSaving = false;
44
45 /**
46 * Opens a new editor window with the given input
47 *
48 * @param input
49 * @param editorId
50 * @return
51 * @return
52 * @throws PartInitException
53 */
54 private static IEditorPart open(final IEditorInput input,
55 final String editorId) throws PartInitException {
56 return getActivePage().openEditor(input, editorId);
57 }
58
59 /**
60 * Opens a new editor window with the given TaxonEditorInput
61 *
62 * @param input
63 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
64 * object.
65 * @throws org.eclipse.ui.PartInitException
66 * if any.
67 */
68 public static void open(TaxonEditorInput input) throws PartInitException {
69 open(input, MultiPageTaxonEditor.ID);
70 }
71
72 public static void open(PolytomousKeyEditorInput input)
73 throws PartInitException {
74 open(input, KeyEditor.ID);
75 }
76
77 /**
78 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
79 * initialised here and is passed to the editor
80 *
81 * @param taxonNodeUuid
82 * a {@link java.util.UUID} object.
83 * @throws java.lang.Exception
84 * if any.
85 */
86 public static void openTaxonNode(UUID taxonNodeUuid) throws Exception {
87 TaxonEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
88 open(input);
89 }
90
91 /**
92 * <p>
93 * openTaxonBase
94 * </p>
95 *
96 * @param taxonBaseUuid
97 * a {@link java.util.UUID} object.
98 * @throws org.eclipse.ui.PartInitException
99 * if any.
100 */
101 public static void openTaxonBase(UUID taxonBaseUuid)
102 throws PartInitException {
103 TaxonEditorInput input = TaxonEditorInput
104 .NewInstanceFromTaxonBase(taxonBaseUuid);
105 open(input);
106 }
107
108 /**
109 * <p>
110 * findEditorByTaxonNodeUuid
111 * </p>
112 *
113 * @param taxonNodeUuid
114 * a {@link java.util.UUID} object.
115 * @return a {@link org.eclipse.ui.IEditorPart} object.
116 * @throws java.lang.Exception
117 * if any.
118 */
119 public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
120 throws Exception {
121 IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
122 return getActivePage().findEditor(input);
123 }
124
125 /**
126 * An uninitialized taxon is one that hasn't been saved yet. As such, it
127 * should appear in neither the list of recent names nor in the taxonomic
128 * tree when opened.
129 *
130 * @throws org.eclipse.ui.PartInitException
131 * if any.
132 * @param parentNodeUuid
133 * a {@link java.util.UUID} object.
134 */
135 public static void openEmpty(UUID parentNodeUuid) throws PartInitException {
136 TaxonEditorInput input = TaxonEditorInput
137 .NewEmptyInstance(parentNodeUuid);
138 open(input, MultiPageTaxonEditor.ID);
139
140 getActiveMultiPageTaxonEditor().changed(null);
141
142 }
143
144 /**
145 * <p>
146 * setSaving
147 * </p>
148 *
149 * @param isSaving
150 * a boolean.
151 */
152 public static void setSaving(boolean isSaving) {
153 EditorUtil.isSaving = isSaving;
154 }
155
156 /**
157 * <p>
158 * isSaving
159 * </p>
160 *
161 * @return a boolean.
162 */
163 public static boolean isSaving() {
164 return isSaving;
165 }
166
167 /**
168 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
169 *
170 * @return a {@link java.util.Set} object.
171 */
172 public static Set<IEditorPart> getOpenEditors() {
173 Set<IEditorPart> taxonEditors = new HashSet<IEditorPart>();
174
175 if (getActivePage() != null) {
176 for (IEditorReference reference : getActivePage()
177 .getEditorReferences()) {
178 IEditorPart editor = reference.getEditor(false);
179 if (editor instanceof MultiPageTaxonEditor) {
180 taxonEditors.add(editor);
181 }
182 }
183 }
184
185 return taxonEditors;
186 }
187
188 /**
189 * Returns the currently active taxon editor
190 *
191 * @return the taxon editor that has focus
192 */
193 public static MultiPageTaxonEditor getActiveMultiPageTaxonEditor() {
194 IEditorPart editorPart = getActiveEditor();
195 if (editorPart != null && editorPart instanceof MultiPageTaxonEditor) {
196 MultiPageTaxonEditor editor = (MultiPageTaxonEditor) editorPart;
197 editor.getConversationHolder().bind();
198 return editor;
199 }
200 return null;
201 }
202
203 /**
204 * <p>
205 * getActiveEditorPage
206 * </p>
207 *
208 * @param page
209 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
210 * @return a {@link org.eclipse.ui.IEditorPart} object.
211 */
212 public static IEditorPart getActiveEditorPage(Page page) {
213 MultiPageTaxonEditor editor = getActiveMultiPageTaxonEditor();
214
215 return editor != null ? editor.getPage(page) : null;
216 }
217
218 /**
219 * Returns the selection of the currently active taxon editor
220 *
221 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
222 */
223 public static ISelection getCurrentSelection() {
224 if (getActiveMultiPageTaxonEditor() == null) {
225 return null;
226 } else {
227 return getActiveMultiPageTaxonEditor().getSite()
228 .getSelectionProvider().getSelection();
229 }
230 }
231
232 /**
233 * <p>
234 * getUndoContext
235 * </p>
236 *
237 * @param editor
238 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
239 * object.
240 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
241 * object.
242 */
243 public static IUndoContext getUndoContext(MultiPageTaxonEditor editor) {
244 return editor.getUndoContext();
245 }
246
247 /**
248 * <p>
249 * getUndoContext
250 * </p>
251 *
252 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
253 * object.
254 */
255 public static IUndoContext getUndoContext() {
256 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
257 }
258
259 /**
260 * <p>
261 * forceUserSave
262 * </p>
263 *
264 * @param editor
265 * a {@link org.eclipse.ui.IEditorPart} object.
266 * @param shell
267 * a {@link org.eclipse.swt.widgets.Shell} object.
268 * @return a boolean.
269 */
270 public static boolean forceUserSave(IEditorPart editor, Shell shell) {
271 if (editor.isDirty()) {
272
273 boolean doSave = MessageDialog
274 .openConfirm(shell, "Confirm save",
275 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
276 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
277
278 if (!doSave) {
279 return false;
280 }
281
282 editor.doSave(EditorUtil.getMonitor());
283 }
284 return true;
285 }
286
287 /**
288 * <p>
289 * getSelection
290 * </p>
291 *
292 * @param event
293 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
294 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
295 */
296 public static IStructuredSelection getSelection(ExecutionEvent event) {
297 IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
298
299 return (IStructuredSelection) activeEditor.getSite()
300 .getSelectionProvider().getSelection();
301 }
302
303 /**
304 * <p>
305 * getPluginId
306 * </p>
307 *
308 * @return a {@link java.lang.String} object.
309 */
310 public static String getPluginId() {
311 return TaxeditorEditorPlugin.PLUGIN_ID;
312 }
313
314 public static void openPolytomousKey(UUID polytomousKeyUuid)
315 throws Exception {
316 PolytomousKeyEditorInput input = PolytomousKeyEditorInput
317 .NewInstance(polytomousKeyUuid);
318 open(input);
319 }
320 }