- enabled DescriptiveView (Factual Data) to show descriptions of SpecimenOrObservati...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / CreateDescriptionHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2011 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
11
12 import org.eclipse.core.commands.AbstractHandler;
13 import org.eclipse.core.commands.ExecutionEvent;
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.core.commands.common.NotDefinedException;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
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.description.SpecimenDescription;
25 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.taxeditor.editor.EditorUtil;
28 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
29 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
30 import eu.etaxonomy.taxeditor.model.AbstractUtility;
31 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33
34 /**
35 * <p>AddDescriptionHandler class.</p>
36 *
37 * @author p.ciardelli
38 * @created 11.08.2009
39 * @version 1.0
40 */
41 public class CreateDescriptionHandler extends AbstractHandler {
42
43 /* (non-Javadoc)
44 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
45 */
46 /** {@inheritDoc} */
47 @Override
48 public Object execute(ExecutionEvent event) throws ExecutionException {
49 IWorkbenchPart part = HandlerUtil.getActivePart(event);
50 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
51
52 IEditorPart editor = HandlerUtil.getActiveEditor(event);
53 if (editor instanceof FormEditor) {
54 editor = ((FormEditor) editor).getActiveEditor();
55 }
56 IEditorInput input = editor.getEditorInput();
57 // taxon description
58 if (input instanceof TaxonEditorInput) {
59 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
60 AbstractPostOperation operation;
61 try {
62
63 operation = createOperationInstance(event.getCommand().getName(), taxon, postOperationEnabled);
64 AbstractUtility.executeOperation(operation);
65 } catch (NotDefinedException e) {
66 AbstractUtility.warn(getClass(), "Command name not set.");
67 }
68 }
69 // specimen description
70 else{
71 //FIXME: no undo and redo supported. Should also use postOperation
72 ISelection selection = editor.getSite().getSelectionProvider().getSelection();
73 if(selection instanceof IStructuredSelection){
74 Object selectedElement = ((IStructuredSelection) selection).getFirstElement();
75 if(selectedElement instanceof SpecimenOrObservationBase<?>){
76 SpecimenDescription description = SpecimenDescription.NewInstance((SpecimenOrObservationBase<?>) selectedElement);
77 postOperationEnabled.postOperation(description);
78 }
79 }
80 }
81 return null;
82 }
83
84 /**
85 * The function is used to make the specific object creation more generic
86 * @param eventLabel
87 * @param taxon
88 * @param postOperationEnabled
89 * @return
90 */
91 protected AbstractPostOperation createOperationInstance(String eventLabel, Taxon taxon, IPostOperationEnabled postOperationEnabled) {
92 // TODO use undo context specific to editor
93 return new CreateTaxonDescriptionOperation(eventLabel, EditorUtil.getUndoContext(), taxon, postOperationEnabled);
94 }
95
96
97 }