Moved all logging and dialog functionality to the new class MessagingUtils.
[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.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22
23 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
24 import eu.etaxonomy.taxeditor.editor.EditorUtil;
25 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
26 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
27 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
28 import eu.etaxonomy.taxeditor.model.MessagingUtils;
29 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
30
31 /**
32 * @author n.hoffmann
33 * @created Dec 6, 2010
34 * @version 1.0
35 */
36 public class DeleteNodeHandler extends AbstractHandler {
37
38 /*
39 * (non-Javadoc)
40 *
41 * @see
42 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
43 * ExecutionEvent)
44 */
45 @Override
46 public Object execute(ExecutionEvent event) throws ExecutionException {
47 IEditorPart editor = HandlerUtil.getActiveEditor(event);
48
49 if (editor instanceof KeyEditor) {
50 IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
51 .getActiveEditor();
52
53 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
54 .getCurrentSelection(event);
55
56 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
57 try {
58 String label = event.getCommand().getName();
59 IUndoContext undoContext = EditorUtil.getUndoContext();
60
61 for (Object element : selection.toArray()) {
62 PolytomousKeyNode keyNode = (PolytomousKeyNode) element;
63
64 AbstractPostOperation operation = new DeleteNodeOperation(
65 label, undoContext, keyNode, editorPage);
66 EditorUtil.executeOperation(operation);
67 }
68
69 } catch (NotDefinedException e) {
70 MessagingUtils.warn(getClass(), "Command name not set.");
71 }
72 } else {
73 MessageDialog.openInformation(
74 EditorUtil.getShell(),
75 "No Key Node Selected",
76 "Please right-click on a specific key node to delete a key node.");
77 }
78 }
79
80 return null;
81 }
82
83 }