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