Navigation Menu - initial implementation 05/16605/5
Marcos CARDINOT [Fri, 5 Jun 2015 23:44:00 +0000 (20:44 -0300)]
This commit has an initial implementation of the Navigation feature (history - PREV and NEXT buttons), which enable users to go backwards or forward on the Palette Browser.

Change-Id: I910cd461ced3b2e69b41ec12ea840a0a8789d5ee

scilab/CHANGES_5.6.X
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/NavigationAction.java [new file with mode: 0644]
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/listener/PaletteManagerTreeSelectionListener.java
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
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosConstants.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosMessages.java

index fabfced..e59c4f3 100644 (file)
@@ -12,6 +12,7 @@ Xcos
 
 * Palette browser - zoom feature is now available via CTRL(+), CTRL(-) and CTRL(mousewheel).
 * Palette browser is now able to load SVG icons.
+* Palette browser handles the action of going forward or backward.
 
 Compilation
 ============
diff --git a/scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/NavigationAction.java b/scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/actions/NavigationAction.java
new file mode 100644 (file)
index 0000000..7f0ec41
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * 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 java.awt.event.ActionEvent;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+
+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;
+
+/**
+ * Navigation menu management.
+ * Actions 'next' and 'previous' of the palette browser.
+ * @author Marcos CARDINOT <mcardinot@gmail.com>
+ */
+public class NavigationAction extends CommonCallBack {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final String LABEL_NEXT = XcosMessages.NEXT;
+    private static final String LABEL_PREV = XcosMessages.PREVIOUS;
+    private static final String ICON_NEXT = FindIconHelper.findIcon("go-next");
+    private static final String ICON_PREV = FindIconHelper.findIcon("go-previous");
+
+    private static JButton btnNEXT;
+    private static JButton btnPREV;
+
+    /**
+     * Constructor
+     */
+    public NavigationAction() {
+        super("");
+    }
+
+    /**
+     * Create the button 'next'
+     * @return the button
+     */
+    public static JButton createButtonNext() {
+        btnNEXT = new JButton();
+        ScilabLAF.setDefaultProperties(btnNEXT);
+        btnNEXT.setIcon(new ImageIcon(ICON_NEXT));
+        btnNEXT.setToolTipText(LABEL_NEXT);
+        btnNEXT.addActionListener(getCallBack());
+        setEnabledNext(false);
+        return btnNEXT;
+    }
+
+    /**
+     * Create the button 'previous'
+     * @return the button
+     */
+    public static JButton createButtonPrev() {
+        btnPREV = new JButton();
+        ScilabLAF.setDefaultProperties(btnPREV);
+        btnPREV.setIcon(new ImageIcon(ICON_PREV));
+        btnPREV.setToolTipText(LABEL_PREV);
+        btnPREV.addActionListener(getCallBack());
+        setEnabledPrev(false);
+        return btnPREV;
+    }
+
+    /**
+     * Create a new class instance
+     * @return the instance
+     */
+    private static CommonCallBack getCallBack() {
+        CommonCallBack callback = null;
+        try {
+            callback = NavigationAction.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) {
+        Object src = e.getSource();
+        if (btnNEXT != null && src.equals(btnNEXT)) {
+            PaletteManagerView.get().getPanel().goNext();
+        } else if (btnPREV != null && src.equals(btnPREV)) {
+            PaletteManagerView.get().getPanel().goPrevious();
+        }
+    }
+
+    /**
+     * setEnabled property of the button 'next'
+     * @param enabled enabled 
+     */
+    public static void setEnabledNext(boolean enabled) {
+        btnNEXT.setEnabled(enabled);
+    }
+
+    /**
+     * setEnabled property of the button 'previous'
+     * @param enabled enabled 
+     */
+    public static void setEnabledPrev(boolean enabled) {
+        btnPREV.setEnabled(enabled);
+    }
+
+    @Override
+    public void callBack() {
+    }
+}
index 4924959..3226cfb 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) 2010 - DIGITEO - Clement DAVID
+ * 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
@@ -22,7 +23,6 @@ import javax.swing.JTree;
 import javax.swing.event.TreeSelectionEvent;
 import javax.swing.event.TreeSelectionListener;
 
-import org.scilab.modules.xcos.graph.PaletteDiagram;
 import org.scilab.modules.xcos.palette.PaletteBlockCtrl;
 import org.scilab.modules.xcos.palette.model.Category;
 import org.scilab.modules.xcos.palette.model.Custom;
@@ -97,9 +97,9 @@ public class PaletteManagerTreeSelectionListener implements TreeSelectionListene
         }
 
         // update
+        paletteManagerPanel.updateHistory();
         nodeView.setPreferredSize(dimension);
         splitPanel.setRightComponent(nodeView);
         nodeView.validate();
     }
-
 }
index 9658354..9a830b1 100644 (file)
@@ -19,6 +19,8 @@ import java.awt.Dimension;
 import java.awt.Toolkit;
 import java.awt.event.MouseWheelEvent;
 import java.awt.event.MouseWheelListener;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.swing.DropMode;
 import javax.swing.JPanel;
@@ -32,6 +34,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.listener.PaletteManagerMouseListener;
 import org.scilab.modules.xcos.palette.listener.PaletteManagerTreeSelectionListener;
 import org.scilab.modules.xcos.palette.listener.PaletteTreeTransferHandler;
@@ -52,6 +55,9 @@ public class PaletteManagerPanel extends JSplitPane {
     private CustomMouseWheelListener mouseWheelListener;
     private JTree tree;
     private PaletteDiagram diagramInstance;
+    private List<TreePath> historyNext;
+    private List<TreePath> historyPrev;
+    private TreePath currentPath;
 
     /**
      * Default constructor
@@ -63,6 +69,9 @@ public class PaletteManagerPanel extends JSplitPane {
         super(JSplitPane.HORIZONTAL_SPLIT);
         this.controller = controller;
         this.mouseWheelListener = new CustomMouseWheelListener();
+        this.historyNext = new ArrayList<TreePath>();
+        this.historyPrev = new ArrayList<TreePath>();
+        this.currentPath = null;
         currentSize = XcosConstants.PaletteBlockSize.NORMAL;
         fillUpContentPane();
     }
@@ -215,6 +224,81 @@ public class PaletteManagerPanel extends JSplitPane {
     }
 
     /**
+     * Updates the history.
+     */
+    public void updateHistory() {
+        TreePath lastPath = currentPath;
+        currentPath = tree.getSelectionPath();
+
+        if (lastPath != null && !currentPath.equals(lastPath)) {
+
+            if (historyPrev.isEmpty()) {
+                historyPrev.add(lastPath);
+            } else {
+                int prevPathIdx = historyPrev.size() - 1;
+                // going backwards?
+                if (currentPath.equals(historyPrev.get(prevPathIdx))) {
+                    historyNext.add(lastPath);
+                    historyPrev.remove(prevPathIdx);
+                    updateHistorySettings();
+                    return;
+                } else {
+                    // it seems to be a new path, just store it!
+                    // Soon we'll check if we are just going forward
+                    // or if it's really a new path!
+                    historyPrev.add(lastPath);
+                }
+            }
+
+            if (!historyNext.isEmpty()) {
+                int nextPathIdx = historyNext.size() - 1;
+                // going forward?
+                if (currentPath.equals(historyNext.get(nextPathIdx))) {
+                    historyNext.remove(nextPathIdx);
+                } else {
+                    // it is really a new path!
+                    // make sure it's on the top of the history!
+                    historyNext.clear();
+                }
+            }
+            updateHistorySettings();
+        }
+    }
+
+    /**
+     * Update the status of the buttons and the history length.
+     */
+    private void updateHistorySettings() {
+        if (historyNext.size() + historyPrev.size() > XcosConstants.HISTORY_LENGTH) {
+            historyPrev.remove(0);
+        }
+        NavigationAction.setEnabledNext(historyNext.size() > 0);
+        NavigationAction.setEnabledPrev(historyPrev.size() > 0);
+    }
+
+    /**
+     * Go to the next path (history).
+     */
+    public void goNext() {
+        try {
+            tree.setSelectionPath(historyNext.get(historyNext.size() - 1));
+        } catch (ArrayIndexOutOfBoundsException err) {
+            NavigationAction.setEnabledNext(false);
+        }
+    }
+
+    /**
+     * Go to the previous path (history).
+     */
+    public void goPrevious() {
+        try {
+            tree.setSelectionPath(historyPrev.get(historyPrev.size() - 1));
+        } catch (ArrayIndexOutOfBoundsException err) {
+            NavigationAction.setEnabledPrev(false);
+        }
+    }
+
+    /**
      * Implement custom mouse handling for the zoom
      */
     private static final class CustomMouseWheelListener implements MouseWheelListener {
index 461db03..ebf8834 100644 (file)
@@ -40,6 +40,7 @@ import org.scilab.modules.xcos.Xcos;
 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.utils.XcosMessages;
@@ -171,10 +172,16 @@ public class PaletteManagerView extends SwingScilabDockablePanel implements Simp
         /* Create the toolbar */
         final ToolBar toolbar = ScilabToolBar.createToolBar();
         SwingScilabToolBar stb = (SwingScilabToolBar) toolbar.getAsSimpleToolBar();
+
         stb.add(LoadAsPalAction.createButton(null));
 
         stb.addSeparator();
 
+        stb.add(NavigationAction.createButtonPrev());
+        stb.add(NavigationAction.createButtonNext());
+
+        stb.addSeparator();
+
         stb.add(ZoomInAction.createPushButton());
         stb.add(ZoomOutAction.createPushButton());
 
index a24f1b1..ba15c88 100644 (file)
@@ -38,6 +38,9 @@ public final class XcosConstants extends ScilabGraphConstants {
     /** Define the maximum number of char that might be represented as style */
     public static final int MAX_CHAR_IN_STYLE = 24;
 
+    /** Define the history length */
+    public static final int HISTORY_LENGTH = 20;
+
     /** the size of the palette block **/
     public enum PaletteBlockSize {
         /** small size **/
index 048ef39..0e19cdb 100644 (file)
@@ -63,6 +63,8 @@ public final class XcosMessages {
     public static final String SAVE_BLOCK_GUI = Messages.gettext("Save block GUI");
 
     /* Palette browser */
+    public static final String NEXT = Messages.gettext("Next");
+    public static final String PREVIOUS = Messages.gettext("Previous");
     public static final String ZOOM_IN = Messages.gettext("Zoom In");
     public static final String ZOOM_OUT = Messages.gettext("Zoom Out");