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