- 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 / CreateDescriptionElementHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
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.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.swt.widgets.Event;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22
23 import eu.etaxonomy.cdm.model.description.DescriptionBase;
24 import eu.etaxonomy.cdm.model.description.Feature;
25 import eu.etaxonomy.taxeditor.editor.EditorUtil;
26 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
27 import eu.etaxonomy.taxeditor.model.AbstractUtility;
28 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
29 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
30
31 /**
32 * <p>CreateDescriptionElementHandler class.</p>
33 *
34 * @author n.hoffmann
35 * @created 16.04.2009
36 * @version 1.0
37 */
38 public class CreateDescriptionElementHandler extends AbstractHandler {
39
40 /*
41 * (non-Javadoc)
42 *
43 * @see
44 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
45 * 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
52 : null;
53
54
55 DescriptionBase<?> description = null;
56
57 ISelection selection = HandlerUtil.getCurrentSelection(event);
58 if (selection instanceof IStructuredSelection) {
59 Object selectedElement = ((IStructuredSelection) selection).getFirstElement();
60 if (selectedElement instanceof DescriptionBase<?>) {
61 description = (DescriptionBase<?>) selectedElement;
62 }
63 }
64
65 if (description != null) {
66 AbstractPostOperation operation = null;
67 try {
68 // TODO use undo context specific to editor
69 operation = operationCreationInstance(event.getCommand().getName(), event, description, postOperationEnabled);
70 AbstractUtility.executeOperation(operation);
71 } catch (NotDefinedException e) {
72 AbstractUtility.warn(getClass(), "Command name not set");
73 }
74 } else {
75 AbstractUtility.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
76 return null;
77 }
78 return null;
79
80 }
81
82 /**
83 * Added to make the Class more generic and limit the amount of code changes required
84 * @param label
85 * @param event
86 * @param taxon
87 * @param description
88 * @param postOperationEnabled
89 * @return
90 */
91 protected AbstractPostOperation operationCreationInstance(String label, ExecutionEvent event, DescriptionBase<?> description, IPostOperationEnabled postOperationEnabled) {
92 Feature feature = (Feature) ((Event) event.getTrigger()).data;
93 return new CreateDescriptionElementOperation(label, EditorUtil.getUndoContext(), description, feature, postOperationEnabled);
94 }
95
96 }