Updated Polytomous Key List Editor Component
authorCherian Mathew <c.mathew@bgbm.org>
Wed, 26 Sep 2012 15:10:19 +0000 (15:10 +0000)
committerCherian Mathew <c.mathew@bgbm.org>
Wed, 26 Sep 2012 15:10:19 +0000 (15:10 +0000)
 - added extra column for taxa
 - 'New' and 'Delete' both lead to error popups when no node is selected
 - 'New' menu click creates an empty node linked to the root node, when no nodes are present.

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/key/polytomous/PolytomousKeyListEditor.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/key/polytomous/PolytomousKeyListLabelProvider.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/key/polytomous/handler/CreateNodeHandler.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/key/polytomous/handler/DeleteNodeHandler.java

index 3ee69768839a738d2b93f822d28791363e87d8c6..22b139305c3d29383f6de3d493dbd2eb2e1df05f 100644 (file)
@@ -66,27 +66,32 @@ public class PolytomousKeyListEditor extends EditorPart implements
                @Override
                public void mouseUp(MouseEvent event) {
 
-                       Table table = (Table) event.widget;
-                       // Determine where the mouse was clicked
-                       Point point = new Point(event.x, event.y);
-
-                       int selectedColumn = getSelectedColumn(table, point);
-
-                       if (selectedColumn == 3) {
-                               PolytomousKeyNode node = (PolytomousKeyNode) getTableItem(
-                                               table, point).getData();
-                               Object linkData = getItemLinkData(node);
-                               if (linkData instanceof PolytomousKeyNode) {
-                                       viewer.setSelection(new StructuredSelection(linkData), true);
-                               } else if (linkData instanceof Taxon) {
-                                       try {
-                                               EditorUtil.openTaxonBase(((Taxon) linkData).getUuid());
-                                       } catch (PartInitException e) {
-                                               EditorUtil.error(getClass(), e);
-                                       }
-                               }
-                       }
-
+                   if(event.button == 1 && event.count == 2) {
+                       Table table = (Table) event.widget;
+                       // Determine where the mouse was clicked
+                       Point point = new Point(event.x, event.y);
+
+                       int selectedColumn = getSelectedColumn(table, point);
+                       PolytomousKeyNode node = (PolytomousKeyNode) getTableItem(
+                        table, point).getData();
+                
+                       if (selectedColumn == 3) {              
+                           PolytomousKeyNode linkData = getItemLinkData(node);
+                           if (linkData != null) {
+                               viewer.setSelection(new StructuredSelection(linkData), true);
+                           } 
+                       }
+                       if (selectedColumn == 4) {
+                           Taxon taxon = getItemTaxon(node);
+                           if (taxon != null) {
+                               try {
+                                   EditorUtil.openTaxonBase((taxon).getUuid());
+                               } catch (PartInitException e) {
+                                   EditorUtil.error(getClass(), e);
+                               }
+                           }
+                       }
+                   }
                }
 
                private int getSelectedColumn(Table table, Point point) {
@@ -110,10 +115,17 @@ public class PolytomousKeyListEditor extends EditorPart implements
                /**
                 * @return
                 */
-               private Object getItemLinkData(PolytomousKeyNode node) {
-                       return node.getChildren().isEmpty() ? node.getTaxon() : node
+               private PolytomousKeyNode getItemLinkData(PolytomousKeyNode node) {
+                       return node.getChildren().isEmpty() ? null : node
                                        .getChildAt(0);
                }
+               
+             /**
+         * @return
+         */
+        private Taxon getItemTaxon(PolytomousKeyNode node) {
+            return node.getTaxon();
+        }
        }
 
        public static final String ID = "eu.etaxonomy.taxeditor.editor.key.polytomous.list";
@@ -239,6 +251,17 @@ public class PolytomousKeyListEditor extends EditorPart implements
                viewer.setInput(key);
        }
 
+       public int getTableItemCount() {
+           if (viewer != null && viewer.getTable() != null) {
+               return viewer.getTable().getItemCount();
+           }
+           
+           return 0;
+       }
+       
+       public PolytomousKey getViewerInputKey() {
+           return (PolytomousKey) viewer.getInput();
+       }
        private void createMenu() {
                // register context menu
                MenuManager menuManager = new MenuManager();
@@ -254,8 +277,8 @@ public class PolytomousKeyListEditor extends EditorPart implements
        // This will create the columns for the table
        private void createColumns(TableViewer viewer) {
                Table table = viewer.getTable();
-               String[] titles = { "Node Number", "Question", "Statement", "Link" };
-               int[] bounds = { 50, 200, 200, 100 };
+               String[] titles = { "Node Number", "Question", "Statement", "Link", "Taxon" };
+               int[] bounds = { 50, 200, 200, 100, 200 };
 
                for (int i = 0; i < titles.length; i++) {
                        TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
@@ -288,7 +311,9 @@ public class PolytomousKeyListEditor extends EditorPart implements
         */
        @Override
        public void changed(Object element) {
-               viewer.update(element, null);
+           if(element != null) {
+               viewer.update(element, null);
+           }
 
                if (element instanceof PolytomousKeyNode) {
                        List<PolytomousKeyNode> children = ((PolytomousKeyNode) element)
index a41bc3bd97a652997a7ccd801eeb1e4a1d821153..a79850249c553c22778002c2c4e0fe2894207ca2 100644 (file)
@@ -58,7 +58,7 @@ public class PolytomousKeyListLabelProvider extends StyledCellLabelProvider {
                                        columnIndex);
                        cell.setText(text);
 
-                       if (columnIndex == 3) {
+                       if (columnIndex == 3 || columnIndex == 4) {
                                StyledString styledString = new StyledString(text, getStyler());
                                StyleRange[] styleRanges;
                                styleRanges = styledString.getStyleRanges();
@@ -79,6 +79,8 @@ public class PolytomousKeyListLabelProvider extends StyledCellLabelProvider {
                        return getItemStatement(node);
                case 3:
                        return getItemLink(node);
+               case 4:
+            return getItemTaxon(node);
                }
                return EMPTY;
        }
@@ -137,11 +139,15 @@ public class PolytomousKeyListLabelProvider extends StyledCellLabelProvider {
        }
 
        private String getItemLink(PolytomousKeyNode node) {
-               String taxonString = node.getTaxon() != null ? node.getTaxon()
-                               .getName().getTitleCache() : EMPTY;
+               String linkString = node.getChildren().isEmpty() ? EMPTY : node.getNodeNumber().toString();
 
-               return node.getChildren().isEmpty() ? taxonString : node
-                               .getNodeNumber().toString();
+               return linkString;
+       }
+       
+       private String getItemTaxon(PolytomousKeyNode node) {
+           String taxonString = node.getTaxon() != null ? node.getTaxon().getName().getTitleCache() : EMPTY;
+
+           return taxonString;
        }
 
        private PolytomousKeyNode getParent(PolytomousKeyNode node) {
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;
        }
index 988564378556ac3167280f5702a803e238e2f71c..b027da92a995a67d4bda5c35ac5da9d9426790cb 100644 (file)
@@ -15,6 +15,7 @@ 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;
@@ -67,8 +68,12 @@ public class DeleteNodeHandler extends AbstractHandler {
                                } 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 delete a key node.");                 
+                       }                       
                }
 
                return null;