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