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