Xcos palettes: add a rename action 23/12123/3
Clément DAVID [Wed, 24 Jul 2013 09:01:04 +0000 (11:01 +0200)]
Change-Id: I00ba96797f10ed2ddf66b1b7e72f46bd924a5c6f

scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/listener/PaletteManagerMouseListener.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosMessages.java

index 59a7e95..7a0fe1b 100644 (file)
@@ -17,6 +17,7 @@ import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.util.logging.Logger;
 
+import javax.swing.JOptionPane;
 import javax.swing.JTree;
 import javax.swing.SwingUtilities;
 import javax.swing.tree.TreePath;
@@ -27,6 +28,8 @@ import org.scilab.modules.gui.contextmenu.ScilabContextMenu;
 import org.scilab.modules.gui.events.callback.CommonCallBack;
 import org.scilab.modules.gui.menuitem.MenuItem;
 import org.scilab.modules.gui.menuitem.ScilabMenuItem;
+import org.scilab.modules.gui.messagebox.MessageBox;
+import org.scilab.modules.localization.Messages;
 import org.scilab.modules.xcos.palette.model.Category;
 import org.scilab.modules.xcos.palette.model.Palette;
 import org.scilab.modules.xcos.palette.model.PaletteNode;
@@ -67,6 +70,10 @@ public class PaletteManagerMouseListener implements MouseListener {
             setupCreateOrAdd(paletteTree, path, create);
             menu.add(create);
 
+            final MenuItem rename = ScilabMenuItem.createMenuItem();
+            setupRename(paletteTree, path, rename);
+            menu.add(rename);
+
             final MenuItem remove = ScilabMenuItem.createMenuItem();
             setupRemove(paletteTree, path, remove);
             menu.add(remove);
@@ -138,6 +145,42 @@ public class PaletteManagerMouseListener implements MouseListener {
     }
 
     /**
+     * Setup the rename item
+     *
+     * @param paletteTree
+     *            the current tree
+     * @param path
+     *            the current path
+     * @param rename
+     *            the menu item
+     */
+    // CSOFF: IllegalCatch
+    private void setupRename(final JTree paletteTree, final TreePath path, final MenuItem rename) {
+        rename.setText(XcosMessages.RENAME);
+        rename.setCallback(new CommonCallBack(XcosMessages.RENAME) {
+            @Override
+            public void callBack() {
+                if (path == null) {
+                    return;
+                }
+
+                try {
+                    final PaletteNode currentNode = (PaletteNode) path.getLastPathComponent();
+
+                    final String s = JOptionPane.showInputDialog(Messages.gettext("Enter a name"), currentNode.getName());
+                    if (s == null || s.isEmpty()) {
+                        return;
+                    }
+
+                    currentNode.setName(s);
+                } catch (final Exception e) {
+                    Logger.getLogger(PaletteManagerMouseListener.class.getName()).severe(e.toString());
+                }
+            }
+        });
+    }
+
+    /**
      * Setup the remove item
      *
      * @param paletteTree
index bdf8da3..45d4d97 100644 (file)
@@ -56,6 +56,7 @@ public final class XcosMessages {
     public static final String REGION_TO_SUPERBLOCK = Messages.gettext("Region to superblock");
     public static final String SUPERBLOCK_MASK = Messages.gettext("Superblock mask");
     public static final String CREATE = Messages.gettext("Create");
+    public static final String RENAME = Messages.gettext("Rename");
     public static final String REMOVE = Messages.gettext("Remove");
     public static final String CUSTOMIZE = Messages.gettext("Customize") + DOTS;
     public static final String SAVE_BLOCK_GUI = Messages.gettext("Save block GUI");