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