merging in latest changes from trunk
[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.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.editor.view.descriptive.operation.CreateDescriptionElementOperation;
34 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
35 import eu.etaxonomy.taxeditor.operation.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 AbstractPostOperation operation = null;
86 try {
87 // TODO use undo context specific to editor
88 operation = operationCreationInstance(event.getCommand().getName(), event, taxon, description, postOperationEnabled);
89 EditorUtil.executeOperation(operation);
90 } catch (NotDefinedException e) {
91 EditorUtil.warn(getClass(), "Command name not set");
92 }
93 } else {
94 EditorUtil.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
95 return null;
96 }
97 }
98 return null;
99
100 }
101
102 /**
103 * Added to make the Class more generic and limit the amount of code changes required
104 * @param label
105 * @param event
106 * @param taxon
107 * @param description
108 * @param postOperationEnabled
109 * @return
110 */
111 protected AbstractPostOperation operationCreationInstance(String label, ExecutionEvent event, Taxon taxon, TaxonDescription description, IPostOperationEnabled postOperationEnabled) {
112 Feature feature = (Feature) ((Event) event.getTrigger()).data;
113 return new CreateDescriptionElementOperation(label,
114 EditorUtil.getUndoContext(), taxon,
115 description, feature, postOperationEnabled);
116 }
117
118 }