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