Merge branch 'release/4.8.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / CreateDescriptionHandler.java
1 /**
2 * Copyright (C) 2011 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 package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
10
11 import java.util.Collection;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.common.NotDefinedException;
17 import org.eclipse.core.expressions.EvaluationContext;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.forms.editor.FormEditor;
22 import org.eclipse.ui.handlers.HandlerUtil;
23
24 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27 import eu.etaxonomy.taxeditor.editor.EditorUtil;
28 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
29 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
30 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateSpecimenDescriptionOperation;
31 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
32 import eu.etaxonomy.taxeditor.model.AbstractUtility;
33 import eu.etaxonomy.taxeditor.model.MessagingUtils;
34 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
35 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
36
37 /**
38 * <p>AddDescriptionHandler class.</p>
39 *
40 * @author p.ciardelli
41 * @created 11.08.2009
42 * @version 1.0
43 */
44 public class CreateDescriptionHandler extends AbstractHandler {
45
46 /* (non-Javadoc)
47 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
48 */
49 /** {@inheritDoc} */
50 @Override
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52 IWorkbenchPart part = HandlerUtil.getActivePart(event);
53 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
54
55 IEditorPart editor = HandlerUtil.getActiveEditor(event);
56
57 if (editor instanceof FormEditor) {
58 editor = ((FormEditor) editor).getActiveEditor();
59 }
60 Object input;
61 if (editor == null && part instanceof DescriptiveViewPart){
62 input = ((DescriptiveViewPart)part).getViewer().getInput();
63 }else{
64 input = editor.getEditorInput();
65 }
66 AbstractPostOperation<?> operation;
67
68 // taxon description
69 if (input instanceof TaxonEditorInput) {
70 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
71 try {
72 operation = createTaxonOperation(event.getCommand().getName(), taxon, postOperationEnabled);
73 AbstractUtility.executeOperation(operation);
74 } catch (NotDefinedException e) {
75 MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
76 }
77 }
78 // specimen description
79 else if(part instanceof DescriptiveViewPart){
80 Object viewerInput = ((DescriptiveViewPart)part).getViewer().getInput();
81 if(viewerInput instanceof SpecimenOrObservationBase<?>){
82 try {
83 operation = new CreateSpecimenDescriptionOperation(event.getCommand().getName(), EditorUtil.getUndoContext(), (SpecimenOrObservationBase<?>) viewerInput, postOperationEnabled);
84 AbstractUtility.executeOperation(operation);
85 } catch (NotDefinedException e) {
86 MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
87 }
88 }
89 if(viewerInput instanceof TaxonBase){
90 try{
91 operation = createTaxonOperation(event.getCommand().getName(), (Taxon) viewerInput, postOperationEnabled);
92 AbstractUtility.executeOperation(operation);
93 } catch (NotDefinedException e) {
94 MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
95 }
96
97 }
98 }
99 return null;
100 }
101
102 /** {@inheritDoc} */
103 protected CreateTaxonDescriptionOperation createTaxonOperation(String eventLabel, Taxon taxon, IPostOperationEnabled postOperationEnabled) {
104 return new CreateTaxonDescriptionOperation(eventLabel, EditorUtil.getUndoContext(), taxon, postOperationEnabled);
105 }
106
107 }