Updated Polytomous Key List Editor Component
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / handler / CreateNodeHandler.java
index c0a5a57efd0709fec40244b32c721083e498e9df..0b9eef5331cdc9fa32fde904d043c2c846102d36 100644 (file)
@@ -8,14 +8,17 @@ import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.core.commands.operations.IUndoContext;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.handlers.HandlerUtil;
 
+import eu.etaxonomy.cdm.model.description.PolytomousKey;
 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
+import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListEditor;
 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.CreateNodeOperation;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 
@@ -35,32 +38,55 @@ public class CreateNodeHandler extends AbstractHandler {
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
 
-               IEditorPart editor = HandlerUtil.getActiveEditor(event);
+           IEditorPart editor = HandlerUtil.getActiveEditor(event);
 
-               if (editor instanceof KeyEditor) {
-                       IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
-                                       .getActiveEditor();
+           if (editor instanceof KeyEditor) {
+               IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
+                       .getActiveEditor();
 
-                       IStructuredSelection selection = (IStructuredSelection) HandlerUtil
-                                       .getCurrentSelection(event);
+               if (editorPage instanceof PolytomousKeyListEditor) {
+                   PolytomousKeyListEditor klEditor = (PolytomousKeyListEditor) editorPage;
+                   if(klEditor.getTableItemCount() == 0) {
+                       PolytomousKey pk = (PolytomousKey)klEditor.getViewerInputKey();
+                       try {
+                           String label = event.getCommand().getName();
+                           IUndoContext undoContext = EditorUtil.getUndoContext();
 
-                       if (selection.getFirstElement() instanceof PolytomousKeyNode) {
-                               try {
-                                       String label = event.getCommand().getName();
-                                       IUndoContext undoContext = EditorUtil.getUndoContext();
 
-                                       PolytomousKeyNode keyNode = (PolytomousKeyNode) selection
-                                                       .getFirstElement();
+                           AbstractPostOperation operation = new CreateNodeOperation(
+                                   label, undoContext, pk.getRoot(), editorPage);
+                           EditorUtil.executeOperation(operation);
+                       } catch (NotDefinedException e) {
+                           EditorUtil.warn(getClass(), "Command name not set.");
+                       }
+                   } else {
 
-                                       AbstractPostOperation operation = new CreateNodeOperation(
-                                                       label, undoContext, keyNode, editorPage);
-                                       EditorUtil.executeOperation(operation);
-                               } catch (NotDefinedException e) {
-                                       EditorUtil.warn(getClass(), "Command name not set.");
-                               }
+                       IStructuredSelection selection = (IStructuredSelection) HandlerUtil
+                               .getCurrentSelection(event);
 
-                       }
-               }
+                       if (selection.getFirstElement() instanceof PolytomousKeyNode) {
+                           try {
+                               String label = event.getCommand().getName();
+                               IUndoContext undoContext = EditorUtil.getUndoContext();
+
+                               PolytomousKeyNode keyNode = (PolytomousKeyNode) selection
+                                       .getFirstElement();
+
+                               AbstractPostOperation operation = new CreateNodeOperation(
+                                       label, undoContext, keyNode, editorPage);
+                               EditorUtil.executeOperation(operation);
+                           } catch (NotDefinedException e) {
+                               EditorUtil.warn(getClass(), "Command name not set.");
+                           }
+                       } else {
+                           MessageDialog.openInformation(
+                                   EditorUtil.getShell(),
+                                   "No Key Node Selected",
+                                   "Please right-click on a specific key node to create a new child key node.");
+                       }
+                   }
+               }
+           }
 
                return null;
        }