merging in latest changes from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / handler / DeleteNodeHandler.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.key.polytomous.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.core.commands.operations.IUndoContext;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.handlers.HandlerUtil;
21
22 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
23 import eu.etaxonomy.taxeditor.editor.EditorUtil;
24 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
25 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
26 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
27 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
28
29 /**
30 * @author n.hoffmann
31 * @created Dec 6, 2010
32 * @version 1.0
33 */
34 public class DeleteNodeHandler extends AbstractHandler {
35
36 /*
37 * (non-Javadoc)
38 *
39 * @see
40 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
41 * ExecutionEvent)
42 */
43 @Override
44 public Object execute(ExecutionEvent event) throws ExecutionException {
45 IEditorPart editor = HandlerUtil.getActiveEditor(event);
46
47 if (editor instanceof KeyEditor) {
48 IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
49 .getActiveEditor();
50
51 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
52 .getCurrentSelection(event);
53
54 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
55 try {
56 String label = event.getCommand().getName();
57 IUndoContext undoContext = EditorUtil.getUndoContext();
58
59 for (Object element : selection.toArray()) {
60 PolytomousKeyNode keyNode = (PolytomousKeyNode) element;
61
62 AbstractPostOperation operation = new DeleteNodeOperation(
63 label, undoContext, keyNode, editorPage);
64 EditorUtil.executeOperation(operation);
65 }
66
67 } catch (NotDefinedException e) {
68 EditorUtil.warn(getClass(), "Command name not set.");
69 }
70
71 }
72 }
73
74 return null;
75 }
76
77 }