c65115942dc472a742ae4d7307320a949b1b97ce
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / handler / CreateChildNodeHandler.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.key.polytomous.handler;
5
6 import org.eclipse.core.commands.AbstractHandler;
7 import org.eclipse.core.commands.ExecutionEvent;
8 import org.eclipse.core.commands.ExecutionException;
9 import org.eclipse.core.commands.common.NotDefinedException;
10 import org.eclipse.core.commands.operations.IUndoContext;
11 import org.eclipse.jface.dialogs.MessageDialog;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.ui.IEditorPart;
14 import org.eclipse.ui.handlers.HandlerUtil;
15
16 import eu.etaxonomy.cdm.model.description.PolytomousKey;
17 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
18 import eu.etaxonomy.taxeditor.editor.EditorUtil;
19 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
20 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
21 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListEditor;
22 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.CreateNodeOperation;
23 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
24
25 /**
26 * @author n.hoffmann
27 *
28 */
29 public class CreateChildNodeHandler 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 PolytomousKeyListEditor klEditor = (PolytomousKeyListEditor) editorPage;
49 if(klEditor.getTableItemCount() == 0) {
50 PolytomousKey pk = (PolytomousKey)klEditor.getViewerInputKey();
51 try {
52 String label = event.getCommand().getName();
53 IUndoContext undoContext = EditorUtil.getUndoContext();
54
55
56 AbstractPostOperation operation = new CreateNodeOperation(
57 label, undoContext, pk.getRoot(), editorPage);
58 EditorUtil.executeOperation(operation);
59 } catch (NotDefinedException e) {
60 EditorUtil.warn(getClass(), "Command name not set.");
61 }
62 } else {
63
64 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
65 .getCurrentSelection(event);
66
67 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
68 try {
69 String label = event.getCommand().getName();
70 IUndoContext undoContext = EditorUtil.getUndoContext();
71
72 PolytomousKeyNode keyNode = (PolytomousKeyNode) selection
73 .getFirstElement();
74
75 AbstractPostOperation operation = new CreateNodeOperation(
76 label, undoContext, keyNode, editorPage);
77 EditorUtil.executeOperation(operation);
78 } catch (NotDefinedException e) {
79 EditorUtil.warn(getClass(), "Command name not set.");
80 }
81 } else {
82 MessageDialog.openInformation(
83 EditorUtil.getShell(),
84 "No Key Node Selected",
85 "Please right-click on a specific key node to create a new child key node.");
86 }
87 }
88 }
89 }
90
91 return null;
92 }
93
94 }