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