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 / CreateSiblingNodeHandler.java
1 package eu.etaxonomy.taxeditor.editor.key.polytomous.handler;
2
3 import org.eclipse.core.commands.AbstractHandler;
4 import org.eclipse.core.commands.ExecutionEvent;
5 import org.eclipse.core.commands.ExecutionException;
6 import org.eclipse.core.commands.common.NotDefinedException;
7 import org.eclipse.core.commands.operations.IUndoContext;
8 import org.eclipse.jface.dialogs.MessageDialog;
9 import org.eclipse.jface.viewers.IStructuredSelection;
10 import org.eclipse.ui.IEditorPart;
11 import org.eclipse.ui.handlers.HandlerUtil;
12
13 import eu.etaxonomy.cdm.model.description.PolytomousKey;
14 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
15 import eu.etaxonomy.taxeditor.editor.EditorUtil;
16 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
17 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
18 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListEditor;
19 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.CreateNodeOperation;
20 import eu.etaxonomy.taxeditor.model.MessagingUtils;
21 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
22
23 /**
24 * Handler responsible for creating sibling nodes of Polytomous Key Nodes
25 * @author c.mathew
26 *
27 */
28
29 public class CreateSiblingNodeHandler extends AbstractHandler {
30
31 /*
32 * (non-Javadoc)
33 *
34 * @see
35 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
36 * ExecutionEvent)
37 */
38 @Override
39 public Object execute(ExecutionEvent event) throws ExecutionException {
40
41 IEditorPart editor = HandlerUtil.getActiveEditor(event);
42
43 if (editor instanceof KeyEditor) {
44 IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
45 .getActiveEditor();
46
47 if (editorPage instanceof PolytomousKeyListEditor) {
48
49 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
50 .getCurrentSelection(event);
51
52 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
53 try {
54 String label = event.getCommand().getName();
55 IUndoContext undoContext = EditorUtil.getUndoContext();
56
57 PolytomousKeyNode keyNode = (PolytomousKeyNode) selection
58 .getFirstElement();
59
60 AbstractPostOperation operation = new CreateNodeOperation(
61 label, undoContext, keyNode.getParent(), editorPage);
62 EditorUtil.executeOperation(operation);
63 } catch (NotDefinedException e) {
64 MessagingUtils.warn(getClass(), "Command name not set.");
65 }
66 } else {
67 MessageDialog.openInformation(
68 EditorUtil.getShell(),
69 "No Key Node Selected",
70 "Please right-click on a specific key node to create a new sibling key node.");
71 }
72 }
73 }
74
75 return null;
76 }
77
78 }
79