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