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