b358658e2764f92e0a9c67d767c5149c1c1d21b3
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / 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.checklist.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.IWorkbenchPart;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.model.description.DescriptionBase;
26 import eu.etaxonomy.cdm.model.description.Feature;
27 import eu.etaxonomy.taxeditor.editor.EditorUtil;
28 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
29 import eu.etaxonomy.taxeditor.model.AbstractUtility;
30 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
31 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
32
33 /**
34 * <p>CreateDescriptionElementHandler class.</p>
35 *
36 * @author n.hoffmann
37 * @created 16.04.2009
38 * @version 1.0
39 */
40 public class CreateDescriptionElementHandler extends AbstractHandler {
41
42 /*
43 * (non-Javadoc)
44 *
45 * @see
46 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
47 * ExecutionEvent)
48 */
49 /** {@inheritDoc} */
50 @Override
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52 IWorkbenchPart part = HandlerUtil.getActivePart(event);
53 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
54 : null;
55
56
57 DescriptionBase<?> description = null;
58
59 ISelection selection = HandlerUtil.getCurrentSelection(event);
60 Object selectedElement = null;
61 if (selection instanceof ITreeSelection) {
62 TreePath[] paths = ((ITreeSelection) selection).getPaths();
63 selectedElement = paths[0].getFirstSegment();
64 }
65 else if (selection instanceof IStructuredSelection) {
66 selectedElement = ((IStructuredSelection) selection).getFirstElement();
67 }
68 if (selectedElement instanceof DescriptionBase<?>) {
69 description = (DescriptionBase<?>) selectedElement;
70 }
71
72 if (description != null) {
73 AbstractPostOperation operation = null;
74 try {
75 // TODO use undo context specific to editor
76 operation = operationCreationInstance(event.getCommand().getName(), event, description, postOperationEnabled);
77 AbstractUtility.executeOperation(operation);
78 } catch (NotDefinedException e) {
79 AbstractUtility.warn(getClass(), "Command name not set");
80 }
81 } else {
82 AbstractUtility.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
83 return null;
84 }
85 return null;
86
87 }
88
89 /**
90 * Added to make the Class more generic and limit the amount of code changes required
91 * @param label
92 * @param event
93 * @param taxon
94 * @param description
95 * @param postOperationEnabled
96 * @return
97 */
98 protected AbstractPostOperation operationCreationInstance(String label, ExecutionEvent event, DescriptionBase<?> description, IPostOperationEnabled postOperationEnabled) {
99 Feature feature = (Feature) ((Event) event.getTrigger()).data;
100 return new CreateDescriptionElementOperation(label, EditorUtil.getUndoContext(), description, feature, postOperationEnabled);
101 }
102
103 }