Implements the ZoomAction class to perform zoomIn and zoomOut 89/16689/3
Marcos CARDINOT [Fri, 12 Jun 2015 22:15:11 +0000 (19:15 -0300)]
Now we handle all the zoomAction stuff in a single class!
It enable us to manage the status of both zoom buttons on the toolbar, disabling them when there is no next/prev zoom level.

Change-Id: I89193a07b6a5b18b4f84b4df931190685fd4727e

scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomAction.java [new file with mode: 0644]
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomInAction.java [deleted file]
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomOutAction.java [deleted file]
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/view/PaletteManagerPanel.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/view/PaletteManagerView.java

diff --git a/scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomAction.java b/scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomAction.java
new file mode 100644 (file)
index 0000000..87555e6
--- /dev/null
@@ -0,0 +1,222 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2015 - Marcos CARDINOT
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+ *
+ */
+
+package org.scilab.modules.xcos.palette.actions;
+
+import static org.scilab.modules.commons.OS.MAC;
+
+import java.awt.Toolkit;
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.swing.ActionMap;
+import javax.swing.ImageIcon;
+import javax.swing.InputMap;
+import javax.swing.JButton;
+import javax.swing.KeyStroke;
+
+import org.scilab.modules.commons.OS;
+import org.scilab.modules.commons.gui.FindIconHelper;
+import org.scilab.modules.commons.gui.ScilabLAF;
+import org.scilab.modules.gui.events.callback.CommonCallBack;
+import org.scilab.modules.xcos.palette.view.PaletteManagerView;
+import org.scilab.modules.xcos.utils.XcosMessages;
+
+/**
+ * Zoom Management
+ * @author Marcos CARDINOT <mcardinot@gmail.com>
+ */
+public class ZoomAction extends CommonCallBack {
+
+    private static final long serialVersionUID = 1L;
+    
+    private static final String LABEL_ZOOMIN = XcosMessages.ZOOM_IN;
+    private static final String LABEL_ZOOMOUT = XcosMessages.ZOOM_OUT;
+    private static final String ICON_ZOOMIN = FindIconHelper.findIcon("zoom-in");
+    private static final String ICON_ZOOMOUT = FindIconHelper.findIcon("zoom-out");
+
+    private static final int ACCELERATOR_KEY = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
+    private static final String ZOOM_IN = "ZoomIn";
+    private static final String ZOOM_OUT = "ZoomOut";
+
+    private static JButton btnZoomIn;
+    private static JButton btnZoomOut;
+
+    /**
+     * Constructor
+     */
+    public ZoomAction() {
+        super("");
+    }
+
+    /**
+     * Register the key for the action
+     * @param view PaletteManagerPanel instance
+     */
+    public static void registerKeyAction(PaletteManagerView view) {
+        // Multi-shortcut action
+        ActionMap[] ams = new ActionMap[] {
+                view.getTree().getActionMap(),
+                view.getPanel().getActionMap()
+        };
+        InputMap[] maps = new InputMap[] {
+                view.getTree().getInputMap(),
+                view.getPanel().getInputMap()
+        };
+
+        // register the action to a unique action keyword
+        for (ActionMap am : ams) {
+            am.put(ZOOM_IN, new ZoomAction());
+            am.put(ZOOM_OUT, new ZoomAction());
+        }
+
+        // add custom key stroke for this action
+        final KeyStroke[] keystrokesIn;
+        final KeyStroke[] keystrokesOut;
+        if (OS.get() == MAC) {
+            // AZERTY for Mac has a non-supported classic layout
+            keystrokesIn = new KeyStroke[] {
+                KeyStroke.getKeyStroke('/', ACCELERATOR_KEY),
+                KeyStroke.getKeyStroke('/', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
+            };
+            keystrokesOut = new KeyStroke[] {
+                    KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
+                    KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
+            };
+        } else {
+            keystrokesIn = new KeyStroke[] {
+                KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
+                KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
+                KeyStroke.getKeyStroke('+', ACCELERATOR_KEY),
+                KeyStroke.getKeyStroke('+', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
+                KeyStroke.getKeyStroke(KeyEvent.VK_ADD, ACCELERATOR_KEY)
+            };
+            keystrokesOut = new KeyStroke[] {
+                KeyStroke.getKeyStroke('-', ACCELERATOR_KEY),
+                KeyStroke.getKeyStroke('-', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
+                KeyStroke.getKeyStroke('_', ACCELERATOR_KEY),
+                KeyStroke.getKeyStroke('_', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
+                KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, ACCELERATOR_KEY)
+            };
+        }
+
+        for (InputMap map : maps) {
+            for (KeyStroke k : keystrokesIn) {
+                map.put(k, ZOOM_IN);
+            }
+            for (KeyStroke k : keystrokesOut) {
+                map.put(k, ZOOM_OUT);
+            }
+        }
+    }
+
+    /**
+     * Create the button 'zoom in'
+     * @return the button
+     */
+    public static JButton createButtonZoomIn() {
+        btnZoomIn = new JButton();
+        ScilabLAF.setDefaultProperties(btnZoomIn);
+        btnZoomIn.setIcon(new ImageIcon(ICON_ZOOMIN));
+        btnZoomIn.setToolTipText(LABEL_ZOOMIN);
+        btnZoomIn.addActionListener(getCallBack());
+        setEnabledZoomIn(true);
+        return btnZoomIn;
+    }
+
+    /**
+     * Create the button 'zoom out'
+     * @return the button
+     */
+    public static JButton createButtonZoomOut() {
+        btnZoomOut = new JButton();
+        ScilabLAF.setDefaultProperties(btnZoomOut);
+        btnZoomOut.setIcon(new ImageIcon(ICON_ZOOMOUT));
+        btnZoomOut.setToolTipText(LABEL_ZOOMOUT);
+        btnZoomOut.addActionListener(getCallBack());
+        setEnabledZoomOut(true);
+        return btnZoomOut;
+    }
+
+    /**
+     * Create a new class instance
+     * @return the instance
+     */
+    private static CommonCallBack getCallBack() {
+        CommonCallBack callback = null;
+        try {
+            callback = ZoomAction.class.getConstructor().newInstance();
+        } catch (IllegalArgumentException e) {
+            e.printStackTrace();
+        } catch (SecurityException e) {
+            e.printStackTrace();
+        } catch (InstantiationException e) {
+            e.printStackTrace();
+        } catch (IllegalAccessException e) {
+            e.printStackTrace();
+        } catch (InvocationTargetException e) {
+            e.printStackTrace();
+        } catch (NoSuchMethodException e) {
+            e.printStackTrace();
+        }
+        return callback;
+    }
+
+    /**
+     * Action
+     * @param e ActionEvent
+     */
+    public void actionPerformed(ActionEvent e) {
+        String cmd = e.getActionCommand();
+        if (cmd.isEmpty()) {
+            Object src = e.getSource();
+            if (btnZoomIn != null && src.equals(btnZoomIn)) {
+                PaletteManagerView.get().getPanel().zoomIn();
+            } else if (btnZoomOut != null && src.equals(btnZoomOut)) {
+                PaletteManagerView.get().getPanel().zoomOut();
+            }
+        } else if (OS.get() == MAC) {
+            if (cmd.equals("/")) {
+                PaletteManagerView.get().getPanel().zoomIn();
+            } else if (cmd.equals("=")) {
+                PaletteManagerView.get().getPanel().zoomOut();
+            }
+        } else {
+            if (cmd.equals("+") || cmd.equals("=")) {
+                PaletteManagerView.get().getPanel().zoomIn();
+            } else if (cmd.equals("-") || cmd.equals("_")) {
+                PaletteManagerView.get().getPanel().zoomOut();
+            }
+        }
+    }
+
+    /**
+     * setEnabled property of the button 'zoomIn'
+     * @param enabled enabled 
+     */
+    public static void setEnabledZoomIn(boolean enabled) {
+        btnZoomIn.setEnabled(enabled);
+    }
+
+    /**
+     * setEnabled property of the button 'zoomOut'
+     * @param enabled enabled 
+     */
+    public static void setEnabledZoomOut(boolean enabled) {
+        btnZoomOut.setEnabled(enabled);
+    }
+
+    @Override
+    public void callBack() {
+    }
+}
diff --git a/scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomInAction.java b/scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomInAction.java
deleted file mode 100644 (file)
index 53f6904..0000000
+++ /dev/null
@@ -1,145 +0,0 @@
-/*
- * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) 2015 - Marcos CARDINOT
- *
- * This file must be used under the terms of the CeCILL.
- * This source file is licensed as described in the file COPYING, which
- * you should have received as part of this distribution.  The terms
- * are also available at
- * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
- *
- */
-
-package org.scilab.modules.xcos.palette.actions;
-
-import static org.scilab.modules.commons.OS.MAC;
-
-import java.awt.Toolkit;
-import java.awt.event.KeyEvent;
-import java.lang.reflect.InvocationTargetException;
-
-import javax.swing.ActionMap;
-import javax.swing.ImageIcon;
-import javax.swing.InputMap;
-import javax.swing.JButton;
-import javax.swing.KeyStroke;
-
-import org.scilab.modules.commons.OS;
-import org.scilab.modules.commons.gui.FindIconHelper;
-import org.scilab.modules.commons.gui.ScilabLAF;
-import org.scilab.modules.gui.events.callback.CommonCallBack;
-import org.scilab.modules.xcos.palette.view.PaletteManagerView;
-import org.scilab.modules.xcos.utils.XcosMessages;
-
-/**
- * Zoom In Management
- * @author Marcos CARDINOT <mcardinot@gmail.com>
- */
-public class ZoomInAction extends CommonCallBack {
-
-    private static final long serialVersionUID = 1L;
-    
-    private static final String LABEL = XcosMessages.ZOOM_IN;
-    private static final String ICON = FindIconHelper.findIcon("zoom-in");
-
-    private static final int ACCELERATOR_KEY = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
-    private static final String ZOOM_IN = "ZoomIn";
-
-    /**
-     * Constructor
-     */
-    public ZoomInAction() {
-        super("");
-    }
-
-    /**
-     * Register the key for the action
-     * @param view PaletteManagerPanel instance
-     */
-    public static void registerKeyAction(PaletteManagerView view) {
-        // Multi-shortcut action
-        ActionMap[] ams = new ActionMap[] {
-                view.getTree().getActionMap(),
-                view.getPanel().getActionMap()
-        };
-        InputMap[] maps = new InputMap[] {
-                view.getTree().getInputMap(),
-                view.getPanel().getInputMap()
-        };
-
-        // register the action to a unique action keyword
-        for (ActionMap am : ams) {
-            am.put(ZOOM_IN, new ZoomInAction());
-        }
-
-        // add custom key stroke for this action
-        final KeyStroke[] keystrokes;
-        if (OS.get() == MAC) {
-            // AZERTY for Mac has a non-supported classic layout
-            keystrokes = new KeyStroke[] {
-                KeyStroke.getKeyStroke('/', ACCELERATOR_KEY),
-                KeyStroke.getKeyStroke('/', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
-            };
-        } else {
-            keystrokes = new KeyStroke[] {
-                KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
-                KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
-                KeyStroke.getKeyStroke('+', ACCELERATOR_KEY),
-                KeyStroke.getKeyStroke('+', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
-                KeyStroke.getKeyStroke(KeyEvent.VK_ADD, ACCELERATOR_KEY)
-            };
-        }
-
-        for (InputMap map : maps) {
-            for (KeyStroke k : keystrokes) {
-                map.put(k, ZOOM_IN);
-            }
-        }
-    }
-
-    /**
-     * Create the associated button
-     * @return the button
-     */
-    public static JButton createPushButton() {
-        JButton pushButton = new JButton();
-        ScilabLAF.setDefaultProperties(pushButton);
-        pushButton.setIcon(new ImageIcon(ICON));
-        pushButton.setToolTipText(LABEL);
-        pushButton.addActionListener(getCallBack());
-        return pushButton;
-    }
-
-    /**
-     * Create a new class instance
-     * @return the instance
-     */
-    private static CommonCallBack getCallBack() {
-        CommonCallBack callback = null;
-        try {
-            callback = ZoomInAction.class.getConstructor().newInstance();
-        } catch (IllegalArgumentException e) {
-            e.printStackTrace();
-        } catch (SecurityException e) {
-            e.printStackTrace();
-        } catch (InstantiationException e) {
-            e.printStackTrace();
-        } catch (IllegalAccessException e) {
-            e.printStackTrace();
-        } catch (InvocationTargetException e) {
-            e.printStackTrace();
-        } catch (NoSuchMethodException e) {
-            e.printStackTrace();
-        }
-        return callback;
-    }
-
-    /**
-     * Action!
-     * @see org.scilab.modules.gui.events.callback.CallBack#callBack()
-     */
-    @Override
-    public void callBack() {
-        PaletteManagerView.get().getPanel().zoomIn();
-    }
-}
diff --git a/scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomOutAction.java b/scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/ZoomOutAction.java
deleted file mode 100644 (file)
index f68aa52..0000000
+++ /dev/null
@@ -1,145 +0,0 @@
-/*
- * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) 2015 - Marcos CARDINOT
- *
- * This file must be used under the terms of the CeCILL.
- * This source file is licensed as described in the file COPYING, which
- * you should have received as part of this distribution.  The terms
- * are also available at
- * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
- *
- */
-
-package org.scilab.modules.xcos.palette.actions;
-
-import static org.scilab.modules.commons.OS.MAC;
-
-import java.awt.Toolkit;
-import java.awt.event.KeyEvent;
-import java.lang.reflect.InvocationTargetException;
-
-import javax.swing.ActionMap;
-import javax.swing.ImageIcon;
-import javax.swing.InputMap;
-import javax.swing.JButton;
-import javax.swing.KeyStroke;
-
-import org.scilab.modules.commons.OS;
-import org.scilab.modules.commons.gui.FindIconHelper;
-import org.scilab.modules.commons.gui.ScilabLAF;
-import org.scilab.modules.gui.events.callback.CommonCallBack;
-import org.scilab.modules.xcos.palette.view.PaletteManagerView;
-import org.scilab.modules.xcos.utils.XcosMessages;
-
-/**
- * Zoom Out Management
- * @author Marcos CARDINOT <mcardinot@gmail.com>
- */
-public class ZoomOutAction extends CommonCallBack {
-
-    private static final long serialVersionUID = 1L;
-
-    private static final String LABEL = XcosMessages.ZOOM_OUT;
-    private static final String ICON = FindIconHelper.findIcon("zoom-out");
-
-    private static final int ACCELERATOR_KEY = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
-    private static final String ZOOM_OUT = "ZoomOut";
-
-    /**
-     * Constructor
-     */
-    public ZoomOutAction() {
-        super("");
-    }
-
-    /**
-     * Register the key for the action
-     * @param view PaletteManagerPanel instance
-     */
-    public static void registerKeyAction(PaletteManagerView view) {
-        // Multi-shortcut action
-        ActionMap[] ams = new ActionMap[] {
-                view.getTree().getActionMap(),
-                view.getPanel().getActionMap()
-        };
-        InputMap[] maps = new InputMap[] {
-                view.getTree().getInputMap(),
-                view.getPanel().getInputMap()
-        };
-
-        // register the action to a unique action keyword
-        for (ActionMap am : ams) {
-            am.put(ZOOM_OUT, new ZoomOutAction());
-        }
-
-        // add custom key stroke for this action
-        final KeyStroke[] keystrokes;
-        if (OS.get() == MAC) {
-            // AZERTY for Mac has a non-supported classic layout
-            keystrokes = new KeyStroke[] {
-                KeyStroke.getKeyStroke('=', ACCELERATOR_KEY),
-                KeyStroke.getKeyStroke('=', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK)
-            };
-        } else {
-            keystrokes = new KeyStroke[] {
-                KeyStroke.getKeyStroke('-', ACCELERATOR_KEY),
-                KeyStroke.getKeyStroke('-', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
-                KeyStroke.getKeyStroke('_', ACCELERATOR_KEY),
-                KeyStroke.getKeyStroke('_', ACCELERATOR_KEY | KeyEvent.SHIFT_DOWN_MASK),
-                KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, ACCELERATOR_KEY)
-            };
-        }
-
-        for (InputMap map : maps) {
-            for (KeyStroke k : keystrokes) {
-                map.put(k, ZOOM_OUT);
-            }
-        }
-    }
-
-    /**
-     * Create the associated button
-     * @return the button
-     */
-    public static JButton createPushButton() {
-        JButton pushButton = new JButton();
-        ScilabLAF.setDefaultProperties(pushButton);
-        pushButton.setIcon(new ImageIcon(ICON));
-        pushButton.setToolTipText(LABEL);
-        pushButton.addActionListener(getCallBack());
-        return pushButton;
-    }
-
-    /**
-     * Create a new class instance
-     * @return the instance
-     */
-    private static CommonCallBack getCallBack() {
-        CommonCallBack callback = null;
-        try {
-            callback = ZoomOutAction.class.getConstructor().newInstance();
-        } catch (IllegalArgumentException e) {
-            e.printStackTrace();
-        } catch (SecurityException e) {
-            e.printStackTrace();
-        } catch (InstantiationException e) {
-            e.printStackTrace();
-        } catch (IllegalAccessException e) {
-            e.printStackTrace();
-        } catch (InvocationTargetException e) {
-            e.printStackTrace();
-        } catch (NoSuchMethodException e) {
-            e.printStackTrace();
-        }
-        return callback;
-    }
-
-    /**
-     * Action!
-     * @see org.scilab.modules.gui.events.callback.CallBack#callBack()
-     */
-    @Override
-    public void callBack() {
-        PaletteManagerView.get().getPanel().zoomOut();
-    }
-}
index f84f5e7..18e282d 100644 (file)
@@ -35,6 +35,7 @@ import javax.swing.tree.TreeSelectionModel;
 import org.scilab.modules.xcos.graph.PaletteDiagram;
 import org.scilab.modules.xcos.palette.PaletteManager;
 import org.scilab.modules.xcos.palette.actions.NavigationAction;
+import org.scilab.modules.xcos.palette.actions.ZoomAction;
 import org.scilab.modules.xcos.palette.listener.PaletteManagerMouseListener;
 import org.scilab.modules.xcos.palette.listener.PaletteManagerTreeSelectionListener;
 import org.scilab.modules.xcos.palette.listener.PaletteTreeTransferHandler;
@@ -123,7 +124,7 @@ public class PaletteManagerPanel extends JSplitPane {
      * @param newSize new paletteblocksize enum
      */
     private void zoom(PaletteBlockSize newSize) {
-        if (newSize == null) {
+        if (newSize == null || newSize == currentSize) {
             return;
         }
 
@@ -146,6 +147,21 @@ public class PaletteManagerPanel extends JSplitPane {
             } else {
                 return;
             }
+
+            // update the status of both zoom buttons
+            if (newSize.next() == null) {
+                // is it the last zoom-in level?
+                ZoomAction.setEnabledZoomIn(false);
+                ZoomAction.setEnabledZoomOut(true);
+            } else if (newSize.previous() == null) {
+                // is it the last zoom-out level?
+                ZoomAction.setEnabledZoomIn(true);
+                ZoomAction.setEnabledZoomOut(false);
+            } else {
+                ZoomAction.setEnabledZoomIn(true);
+                ZoomAction.setEnabledZoomOut(true);
+            }
+
             currentSize = newSize;
             jspR.revalidate();
         } catch (NullPointerException e) {
index ebf8834..2a7b6ea 100644 (file)
@@ -41,8 +41,7 @@ import org.scilab.modules.xcos.palette.PaletteManager;
 import org.scilab.modules.xcos.palette.actions.ClosePalettesAction;
 import org.scilab.modules.xcos.palette.actions.LoadAsPalAction;
 import org.scilab.modules.xcos.palette.actions.NavigationAction;
-import org.scilab.modules.xcos.palette.actions.ZoomInAction;
-import org.scilab.modules.xcos.palette.actions.ZoomOutAction;
+import org.scilab.modules.xcos.palette.actions.ZoomAction;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
 /**
@@ -182,11 +181,10 @@ public class PaletteManagerView extends SwingScilabDockablePanel implements Simp
 
         stb.addSeparator();
 
-        stb.add(ZoomInAction.createPushButton());
-        stb.add(ZoomOutAction.createPushButton());
+        stb.add(ZoomAction.createButtonZoomIn());
+        stb.add(ZoomAction.createButtonZoomOut());
 
-        ZoomInAction.registerKeyAction(this);
-        ZoomOutAction.registerKeyAction(this);
+        ZoomAction.registerKeyAction(this);
 
         setToolBar(toolbar);