Merge branch 'develop' into bulkEditorE4
[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.Collection;
13 import java.util.UUID;
14
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.operations.IOperationHistory;
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19 import org.eclipse.e4.ui.workbench.modeling.EPartService;
20 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.viewers.TreeNode;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.handlers.HandlerUtil;
30
31 import eu.etaxonomy.cdm.api.service.ITaxonService;
32 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
33 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
34 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
35 import eu.etaxonomy.cdm.model.taxon.Synonym;
36 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
37 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38 import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
39 import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditor;
40 import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
41 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
42 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
43 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
44 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
45 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
46 import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
47 import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
48 import eu.etaxonomy.taxeditor.model.AbstractUtility;
49 import eu.etaxonomy.taxeditor.model.MessagingUtils;
50 import eu.etaxonomy.taxeditor.store.CdmStore;
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 IEditorPart editor = getActivePage().openEditor(input, editorId);
75 if(input != null &&
76 editor.getEditorInput() != null &&
77 input != editor.getEditorInput() &&
78 input instanceof CdmEntitySessionInput) {
79 ((CdmEntitySessionInput)input).dispose();
80 }
81 return editor;
82 }
83
84 public static void open(PolytomousKeyEditorInput input)
85 throws PartInitException {
86 open(input, KeyEditor.ID);
87 }
88
89 public static void open(CdmAuthorityEditorInput input)
90 throws PartInitException {
91 open(input, CdmAuthorityEditor.ID);
92 }
93
94 /**
95 * Opens a new ChecklistView for the given input
96 * @param input a {@link ChecklistEditorInput} representing the selected checklist
97 * @throws PartInitException
98 */
99 public static void open(ChecklistEditorInput input)
100 throws PartInitException {
101 open(input, ChecklistEditor.ID);
102 }
103
104 /**
105 * Opens a new {@link DataImportEditor} for the given input
106 * @param input a {@link DataImportEditorInput}
107 * @throws PartInitException
108 */
109 // public static void open(DataImportEditorInput<?> input)
110 // throws PartInitException {
111 // if(input instanceof BioCaseEditorInput){
112 // open(input, SpecimenImportEditor.ID);
113 // }
114 // else if(input instanceof GbifImportEditorInput){
115 // open(input, GbifImportEditor.ID);
116 // }
117 // }
118
119 /**
120 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
121 * initialised here and is passed to the editor
122 *
123 * @param taxonNodeUuid
124 * a {@link java.util.UUID} object.
125 * @throws java.lang.Exception
126 * if any.
127 */
128 public static void openTaxonNodeE4(UUID taxonNodeUuid) throws Exception {
129 //FIXME E4 this can probably be removed when fully migrated
130 TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstance(taxonNodeUuid);
131 EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
132 MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
133 partService.showPart(part, PartState.ACTIVATE);
134 TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
135 editor.init(input);
136 }
137
138 public static void openTaxonBaseE4(UUID taxonBaseUuid) throws PartInitException{
139 //FIXME E4 this can probably be removed when fully migrated
140 TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).find(taxonBaseUuid);
141
142 if (taxonBase != null && taxonBase.isOrphaned()) {
143 if(taxonBase.isInstanceOf(Synonym.class)){
144 MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_ACCEPTED_TAXON_MESSAGE);
145 return;
146 }
147 else{
148 MessagingUtils.warningDialog(Messages.EditorUtil_ORPHAN_TAXON, TaxonEditorInputE4.class, Messages.EditorUtil_ORPHAN_TAXON_MESSAGE);
149 return;
150 }
151 }
152
153 TaxonEditorInputE4 input = TaxonEditorInputE4.NewInstanceFromTaxonBase(taxonBaseUuid);
154 EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
155 MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
156 partService.showPart(part, PartState.ACTIVATE);
157 TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
158 editor.init(input);
159 }
160
161 /**
162 * <p>
163 * findEditorByTaxonNodeUuid
164 * </p>
165 *
166 * @param taxonNodeUuid
167 * a {@link java.util.UUID} object.
168 * @return a {@link org.eclipse.ui.IEditorPart} object.
169 * @throws java.lang.Exception
170 * if any.
171 */
172 public static IEditorPart findEditorByTaxonNodeUuid(UUID taxonNodeUuid)
173 throws Exception {
174 IEditorInput input = TaxonEditorInput.NewInstance(taxonNodeUuid);
175 return getActivePage().findEditor(input);
176 }
177
178 /**
179 * An uninitialized taxon is one that hasn't been saved yet. As such, it
180 * should appear in neither the list of recent names nor in the taxonomic
181 * tree when opened.
182 *
183 * @throws org.eclipse.ui.PartInitException
184 * if any.
185 * @param parentNodeUuid
186 * a {@link java.util.UUID} object.
187 */
188 public static void openEmptyE4(UUID parentNodeUuid) throws PartInitException {
189 TaxonEditorInputE4 input = TaxonEditorInputE4
190 .NewEmptyInstance(parentNodeUuid);
191 EPartService partService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getService(EPartService.class);
192 MPart part = partService.createPart("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4");
193 partService.showPart(part, PartState.ACTIVATE);
194 TaxonNameEditorE4 editor = (TaxonNameEditorE4) part.getObject();
195 editor.init(input);
196 }
197
198 /**
199 * <p>
200 * setSaving
201 * </p>
202 *
203 * @param isSaving
204 * a boolean.
205 */
206 public static void setSaving(boolean isSaving) {
207 EditorUtil.isSaving = isSaving;
208 }
209
210 /**
211 * <p>
212 * isSaving
213 * </p>
214 *
215 * @return a boolean.
216 */
217 public static boolean isSaving() {
218 return isSaving;
219 }
220
221 /**
222 * <p>
223 * getUndoContext
224 * </p>
225 *
226 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
227 * object.
228 */
229 public static IUndoContext getUndoContext() {
230 return IOperationHistory.GLOBAL_UNDO_CONTEXT;
231 }
232
233 /**
234 * <p>
235 * forceUserSave
236 * </p>
237 *
238 * @param editor
239 * a {@link org.eclipse.ui.IEditorPart} object.
240 * @param shell
241 * a {@link org.eclipse.swt.widgets.Shell} object.
242 * @return a boolean.
243 */
244 public static boolean forceUserSave(IEditorPart editor, Shell shell) {
245 if (editor.isDirty()) {
246
247 boolean doSave = MessageDialog
248 .openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
249 Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
250
251 if (!doSave) {
252 return false;
253 }
254
255 editor.doSave(AbstractUtility.getMonitor());
256 }
257 return true;
258 }
259
260 public static boolean forceUserSaveE4Editor(TaxonNameEditorE4 editor, Shell shell) {
261 if (editor.isDirty()) {
262
263 boolean doSave = MessageDialog
264 .openConfirm(shell, Messages.EditorUtil_COMFIRM_SAVE,
265 Messages.EditorUtil_CONFIRM_SAVE_MESSAGE);
266
267 if (!doSave) {
268 return false;
269 }
270
271 editor.save(AbstractUtility.getMonitor());
272 }
273 return true;
274 }
275
276 /**
277 * <p>
278 * getSelection
279 * </p>
280 *
281 * @param event
282 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
283 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
284 */
285 public static IStructuredSelection getSelection(ExecutionEvent event) {
286 IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
287
288 return (IStructuredSelection) activeEditor.getSite()
289 .getSelectionProvider().getSelection();
290 }
291
292 /**
293 * <p>
294 * getPluginId
295 * </p>
296 *
297 * @return a {@link java.lang.String} object.
298 */
299 public static String getPluginId() {
300 return TaxeditorEditorPlugin.PLUGIN_ID;
301 }
302
303 public static void openPolytomousKey(UUID polytomousKeyUuid)
304 throws Exception {
305 PolytomousKeyEditorInput input = PolytomousKeyEditorInput
306 .NewInstance(polytomousKeyUuid);
307 open(input);
308 }
309
310 // public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
311 // throws Exception {
312 // PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
313 // open(input);
314 // }
315
316 public static void openCdmAuthorities(UUID groupUuid)
317 throws Exception {
318 CdmAuthorityEditorInput input = CdmAuthorityEditorInput.NewInstance(groupUuid);
319 open(input);
320 }
321
322 /**
323 * Iterates recursively over all originals having the given specimen as a derivate.
324 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
325 * @param specimen the start element for which the originals are iterated recursively
326 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
327 */
328 public static SpecimenOrObservationBase<?> getTopMostDerivate(SpecimenOrObservationBase<?> specimen){
329 if(specimen.isInstanceOf(FieldUnit.class)){
330 return specimen;
331 }
332 else if(specimen instanceof DerivedUnit
333 && ((DerivedUnit) specimen).getOriginals()!=null
334 && !((DerivedUnit) specimen).getOriginals().isEmpty()){
335 for(SpecimenOrObservationBase<?> original:((DerivedUnit) specimen).getOriginals()){
336 return getTopMostDerivate(original);
337 }
338 //needed to add this for compilation although this is unreachable
339 return specimen;
340 }
341 else{
342 return specimen;
343 }
344 }
345
346 /**
347 * If the current selection is a single {@link TreeNode} it will be returned.
348 * @param selection the selection to check
349 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
350 */
351 public static TreeNode getTreeNodeOfSelection(ISelection selection){
352 if(selection instanceof IStructuredSelection
353 && ((IStructuredSelection) selection).size()==1
354 && ((IStructuredSelection) selection).getFirstElement() instanceof TreeNode){
355 return (TreeNode) ((IStructuredSelection) selection).getFirstElement();
356
357 }
358 return null;
359 }
360
361 public static void closeObsoleteEditor(TaxonNode taxonNode, EPartService partService){
362 String treeIndex = taxonNode.treeIndex();
363 Collection<MPart> parts = partService.getParts();
364 for (MPart part : parts) {
365 Object object = part.getObject();
366 if(object instanceof TaxonNameEditorE4){
367 TaxonNameEditorE4 taxonEditor = (TaxonNameEditorE4)object;
368 TaxonNode node = taxonEditor.getEditorInput().getTaxonNode();
369 if(node.treeIndex().startsWith(treeIndex)){
370 partService.hidePart(part, true);
371 }
372 }
373 }
374 }
375 }