added a readme file
[taxeditor.git] / 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.jface.viewers.ITreeSelection;
20 import org.eclipse.jface.viewers.TreePath;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IWorkbenchPart;
25 import org.eclipse.ui.forms.editor.FormEditor;
26 import org.eclipse.ui.handlers.HandlerUtil;
27
28 import eu.etaxonomy.cdm.model.description.Feature;
29 import eu.etaxonomy.cdm.model.description.TaxonDescription;
30 import eu.etaxonomy.cdm.model.taxon.Taxon;
31 import eu.etaxonomy.taxeditor.editor.EditorUtil;
32 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
33 import eu.etaxonomy.taxeditor.operations.AbstractPostOperation;
34 import eu.etaxonomy.taxeditor.operations.CreateDescriptionElementOperation;
35 import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
36
37 /**
38 * <p>CreateDescriptionElementHandler class.</p>
39 *
40 * @author n.hoffmann
41 * @created 16.04.2009
42 * @version 1.0
43 */
44 public class CreateDescriptionElementHandler extends AbstractHandler {
45
46 /*
47 * (non-Javadoc)
48 *
49 * @see
50 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
51 * ExecutionEvent)
52 */
53 /** {@inheritDoc} */
54 public Object execute(ExecutionEvent event) throws ExecutionException {
55 IWorkbenchPart part = HandlerUtil.getActivePart(event);
56 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
57 : null;
58
59 IEditorPart editor = HandlerUtil.getActiveEditor(event);
60 if (editor instanceof FormEditor) {
61 editor = ((FormEditor) editor).getActiveEditor();
62 }
63 IEditorInput input = editor.getEditorInput();
64 if (input instanceof TaxonEditorInput) {
65 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
66
67 TaxonDescription description = null;
68
69 ISelection selection = HandlerUtil.getCurrentSelection(event);
70 if (selection instanceof ITreeSelection) {
71 TreePath[] paths = ((ITreeSelection) selection).getPaths();
72 Object firstSegment = paths[0].getFirstSegment();
73 if (firstSegment instanceof TaxonDescription) {
74 description = (TaxonDescription) firstSegment;
75 }
76 }else if (selection instanceof IStructuredSelection) {
77 Object selectedElement = ((IStructuredSelection) selection)
78 .getFirstElement();
79 if (selectedElement instanceof TaxonDescription){
80 description = (TaxonDescription) selectedElement;
81 }
82 }
83
84 if (description != null) {
85
86 Feature feature = (Feature) ((Event) event.getTrigger()).data;
87
88 AbstractPostOperation operation = null;
89 try {
90 // TODO use undo context specific to editor
91 operation = new CreateDescriptionElementOperation(event
92 .getCommand().getName(),
93 EditorUtil.getUndoContext(), taxon,
94 description, feature, postOperationEnabled);
95 EditorUtil.executeOperation(operation);
96 } catch (NotDefinedException e) {
97 EditorUtil.warn(getClass(), "Command name not set");
98 }
99 } else {
100 EditorUtil.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
101 return null;
102 }
103 }
104 return null;
105
106 }
107 }