Xcos palette: fix most of the palette manipulation issues 26/12126/2
Clément DAVID [Wed, 24 Jul 2013 15:51:28 +0000 (17:51 +0200)]
Change-Id: I1e0306347b559d2a35502d2aa8fc4517057d8685

scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/Palette.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/LoadAsPalAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/listener/PaletteManagerMouseListener.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/listener/PaletteTreeTransferHandler.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/model/PaletteNode.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/view/PaletteManagerPanel.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosMessages.java

index 4e568eb..c343257 100644 (file)
@@ -205,7 +205,7 @@ public final class Palette {
                         cat.getNode().add(pal);
                         pal.setParent(cat);
 
-                        PaletteNode.refreshView(pal);
+                        PaletteNode.refreshView(cat, pal);
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
@@ -274,7 +274,7 @@ public final class Palette {
                         throw new RuntimeException(String.format(WRONG_INPUT_ARGUMENT_S_INVALID_TREE_PATH, NAME));
                     }
 
-                    PaletteNode.refreshView(node.getParent());
+                    PaletteNode.refreshView(node.getParent(), node);
                 }
             });
         } catch (final InterruptedException e) {
@@ -342,7 +342,7 @@ public final class Palette {
 
                     node.setEnable(status);
 
-                    PaletteNode.refreshView(node.getParent());
+                    PaletteNode.refreshView(node.getParent(), node);
                 }
             });
         } catch (final InterruptedException e) {
@@ -393,8 +393,8 @@ public final class Palette {
                     destination.getNode().add(src);
                     src.setParent(destination);
 
-                    PaletteNode.refreshView(toBeReloaded[0]);
-                    PaletteNode.refreshView(toBeReloaded[1]);
+                    PaletteNode.refreshView(toBeReloaded[0], null);
+                    PaletteNode.refreshView(toBeReloaded[1], src);
                 }
             });
         } catch (final InterruptedException e) {
index a6b4301..8d62958 100644 (file)
@@ -110,6 +110,6 @@ public final class LoadAsPalAction extends DefaultAction {
         final Category root = PaletteManager.getInstance().getRoot();
         root.getNode().add(c);
         c.setParent(root);
-        PaletteNode.refreshView(c);
+        PaletteNode.refreshView(root, c);
     }
 }
index 7a0fe1b..a96bd54 100644 (file)
@@ -115,29 +115,31 @@ public class PaletteManagerMouseListener implements MouseListener {
         create.setCallback(new CommonCallBack(XcosMessages.CREATE) {
             @Override
             public void callBack() {
+                String name = JOptionPane.showInputDialog(XcosMessages.ASK_FOR_A_NAME, XcosMessages.DEFAULT_CATEGORY_NAME);
+                if (name == null || name.isEmpty()) {
+                    return;
+                }
+
                 Category nonModifiedRoot = currentNode.getParent();
                 final Category c = new Category();
                 c.setEnable(true);
-                c.setName(XcosMessages.DEFAULT_CATEGORY_NAME);
+                c.setName(name);
 
                 if (currentNode instanceof Category) {
                     ((Category) currentNode).getNode().add(c);
                     c.setParent((Category) currentNode);
-                    if (path != null) {
-                        path.pathByAddingChild(c);
-                    } else {
-                        nonModifiedRoot = (Category) currentNode;
-                    }
+
+                    PaletteNode.refreshView(currentNode, c);
                 } else if (currentNode instanceof Palette) {
                     final int index = nonModifiedRoot.getIndex(currentNode);
+
                     nonModifiedRoot.getNode().set(index, c);
                     c.getNode().add(currentNode);
                     currentNode.setParent(c);
                     c.setParent(nonModifiedRoot);
-                    path.getParentPath().pathByAddingChild(c);
-                }
 
-                PaletteNode.refreshView(c);
+                    PaletteNode.refreshView(nonModifiedRoot, currentNode);
+                }
             }
         });
 
@@ -167,7 +169,7 @@ public class PaletteManagerMouseListener implements MouseListener {
                 try {
                     final PaletteNode currentNode = (PaletteNode) path.getLastPathComponent();
 
-                    final String s = JOptionPane.showInputDialog(Messages.gettext("Enter a name"), currentNode.getName());
+                    final String s = JOptionPane.showInputDialog(XcosMessages.ASK_FOR_A_NAME, currentNode.getName());
                     if (s == null || s.isEmpty()) {
                         return;
                     }
index 38ebffb..d5009b1 100644 (file)
@@ -92,7 +92,6 @@ public class PaletteTreeTransferHandler extends TransferHandler {
      *            the transferable
      */
     private void updateTree(final JTree t, final Transferable data) {
-        final DefaultTreeModel model = (DefaultTreeModel) t.getModel();
         final TreePath path = t.getSelectionPath();
         PaletteNode newNode;
 
@@ -106,8 +105,8 @@ public class PaletteTreeTransferHandler extends TransferHandler {
             return;
         }
 
-        model.nodeStructureChanged(newNode.getParent());
-        model.nodeStructureChanged((TreeNode) path.getParentPath().getLastPathComponent());
+        PaletteNode.refreshView((PaletteNode) path.getParentPath().getLastPathComponent(), null);
+        PaletteNode.refreshView(newNode.getParent(), newNode);
     }
 
     /**
index 262987c..0240e00 100644 (file)
@@ -12,8 +12,8 @@
 
 package org.scilab.modules.xcos.palette.model;
 
-import java.util.Deque;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.logging.Logger;
 
 import javax.swing.JTree;
@@ -180,10 +180,25 @@ public abstract class PaletteNode implements TreeNode {
             throw new RuntimeException("Parent node is 'null'");
         }
 
+        // select the right "next" node
+        final TreeNode toBeSelected;
+        if (toBeReloaded.getChildCount() > 1) {
+            final int index = toBeReloaded.getIndex(node);
+            if (index > 0) {
+                toBeSelected = toBeReloaded.getChildAt(index - 1);
+            } else {
+                toBeSelected = toBeReloaded.getChildAt(index + 1);
+            }
+        } else {
+            toBeSelected = toBeReloaded;
+        }
+
+        // remove the node
         node.setParent(null);
         toBeReloaded.getNode().remove(node);
 
-        refreshView(toBeReloaded);
+
+        refreshView(toBeReloaded, toBeSelected);
     }
 
     /**
@@ -192,44 +207,27 @@ public abstract class PaletteNode implements TreeNode {
      * @param toBeReloaded
      *            the category to refresh
      */
-    public static void refreshView(final PaletteNode toBeReloaded) {
+    public static void refreshView(final TreeNode toBeReloaded, final TreeNode selected) {
         if (PaletteManagerView.get() != null) {
             final JTree tree = PaletteManagerView.get().getTree();
             final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
 
             /*
-             * Reload the model
+             * Reload the whole model
              */
-            if (toBeReloaded.isLeaf()) {
-                model.reload(toBeReloaded.getParent());
-            } else {
-                model.reload(toBeReloaded);
-            }
+            model.reload();
 
             /*
-             * Select the better path
+             * Select the better path, if applicable
              */
-
-            // getting the current path
-            final Deque<TreeNode> objectPath = new LinkedList<TreeNode>();
-            TreeNode current = toBeReloaded;
-            do {
-                objectPath.addFirst(current);
-                current = current.getParent();
-            } while (current != null);
-
-            // appending the all first children to the path
-            // this will force a leaf to be selected
-            current = toBeReloaded;
-            while (!current.isLeaf() && current.getAllowsChildren() && current.children().hasMoreElements()) {
-                current = current.getChildAt(0);
-                objectPath.addLast(current);
+            ArrayList<Object> path = new ArrayList<Object>();
+            for (TreeNode n = selected; n != null; n = n.getParent()) {
+                path.add(n);
             }
+            path.add(tree.getModel().getRoot());
+            Collections.reverse(path);
 
-            // select and expand the better found path
-            final TreePath path = new TreePath(objectPath.toArray());
-            tree.setSelectionPath(path);
-            tree.expandPath(path);
+            tree.setSelectionPath(new TreePath(path.toArray()));
         }
     }
 
index bfb63f7..bd31b56 100644 (file)
@@ -20,6 +20,7 @@ import javax.swing.JScrollPane;
 import javax.swing.JSplitPane;
 import javax.swing.JTree;
 import javax.swing.tree.TreeNode;
+import javax.swing.tree.TreePath;
 import javax.swing.tree.TreeSelectionModel;
 
 import org.scilab.modules.xcos.palette.PaletteManager;
@@ -61,6 +62,7 @@ public class PaletteManagerPanel extends JSplitPane {
 
         TreeNode root = controller.getRoot();
         JTree tree = new JTree(new PaletteTreeModel(root));
+        JScrollPane treeScrollPane = new JScrollPane(tree);
 
         /** Setup tree */
         tree.getSelectionModel().setSelectionMode(
@@ -71,10 +73,11 @@ public class PaletteManagerPanel extends JSplitPane {
 
         tree.setEditable(false);
         tree.setDragEnabled(true);
+        tree.setExpandsSelectedPaths(true);
         tree.setDropMode(DropMode.INSERT);
         tree.setTransferHandler(new PaletteTreeTransferHandler());
 
-        setLeftComponent(new JScrollPane(tree));
+        setLeftComponent(treeScrollPane);
         panel.setViewportView(rootPalette);
         setRightComponent(panel);
     }
@@ -110,8 +113,11 @@ public class PaletteManagerPanel extends JSplitPane {
                      .getView();
 
         /* Tree layout */
-        tree.expandRow(1);
-        tree.setSelectionRow(2);
+        final Object root = tree.getModel().getRoot();
+        final Object firstChild = tree.getModel().getChild(root, 0);
+        final Object secondChild = tree.getModel().getChild(firstChild, 0);
+        tree.setSelectionPath(new TreePath(new Object[] {root, firstChild, secondChild}));
+
         tree.setRootVisible(false);
         tree.setScrollsOnExpand(true);
 
index 45d4d97..56f24f8 100644 (file)
@@ -68,6 +68,7 @@ public final class XcosMessages {
     public static final String CREATE_CATEGORY = Messages.gettext("Create a category");
     public static final String ADDTO_CATEGORY = Messages.gettext("Add to a new category");
     public static final String DEFAULT_CATEGORY_NAME = Messages.gettext("Category");
+    public static final String ASK_FOR_A_NAME = Messages.gettext("Enter a name");
 
     public static final String DUMP = Messages.gettext("Dump");
     public static final String VIEW_IN_SCICOS = Messages.gettext("View in Scicos");