listbox and combobox can display icon 53/13953/3
Antoine ELIAS [Fri, 7 Mar 2014 13:58:28 +0000 (14:58 +0100)]
str = ["top", "go-up";"left", "go-previous";"bottom", "go-down";"right", "go-next"];
u = uicontrol("style", "popupmenu", "position", [100 100 100 30], "string", str)
u = uicontrol("style", "listbox", "position", [200 100 100 100], "string", str)

Change-Id: I0e69d62122958ef0e592449ee1f6f420c7bf9bcb

scilab/CHANGES_5.5.X
scilab/modules/gui/src/cpp/GetUicontrolString.cpp
scilab/modules/gui/src/cpp/SetUicontrolString.cpp
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/listbox/SwingScilabListBox.java
scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/popupmenu/SwingScilabPopupMenu.java
scilab/modules/gui/src/java/org/scilab/modules/gui/utils/SwingScilabIconItem.java [new file with mode: 0644]

index 9ceb293..837cc2b 100644 (file)
@@ -29,10 +29,11 @@ GUI Refactoring and Improvements
 * New uimenu properties:
  - icon: add an icon on the left of the label menu.
 
-* "listbox" and "popupmenu" style uicontrols can now manage colors when the
+* "listbox" and "popupmenu" style uicontrols can now manage colors and icons when the
   "String" property is set to a matrix matching the format:
   ["Item1", "#XXXXXX"; "Item2", "#XXXXXX", ...]
-  Then the component will display a colored box on the left of the associated string.
+  ["Item1", "icon_name"; "Item2", "icon_name", ...]
+  Then the component will display a colored box or icon on the left of the associated string.
 
 * New management of uicontrols positioning:
   In previous versions, uicontrols position was managed in an absolute way through
index fb9950e..77d5ff5 100644 (file)
@@ -18,22 +18,29 @@ extern "C"
 
 int GetUicontrolString(void* _pvCtx, int iObjUID)
 {
+    int iNbColStrings = 0;
+    int *piNbColStrings = &iNbColStrings;
     int iNbStrings = 0;
     int *piNbStrings = &iNbStrings;
     char **pstString = NULL;
 
     getGraphicObjectProperty(iObjUID, __GO_UI_STRING_SIZE__, jni_int, (void **) &piNbStrings);
+    getGraphicObjectProperty(iObjUID, __GO_UI_STRING_COLNB__, jni_int, (void **) &piNbColStrings);
     getGraphicObjectProperty(iObjUID, __GO_UI_STRING__, jni_string_vector, (void **) &pstString);
     if (pstString != NULL)
     {
-        if (iNbStrings == 0)
+        if (iNbStrings == 0 || iNbColStrings == 0)
         {
             return sciReturnEmptyMatrix(_pvCtx);
         }
-        else
+        else if (iNbColStrings == 1)
         {
             return sciReturnStringMatrix(_pvCtx, pstString, 1, iNbStrings);
         }
+        else
+        {
+            return sciReturnStringMatrix(_pvCtx, pstString, iNbStrings / iNbColStrings, iNbColStrings);
+        }
     }
     else
     {
index 1121610..6f7fd33 100644 (file)
@@ -69,31 +69,31 @@ int SetUicontrolString(void* _pvCtx, int iObjUID, void* _pvData, int valueType,
         case __GO_UI_LISTBOX__ :
         case __GO_UI_POPUPMENU__ :
         {
-            ////combo and list color
+            //combo and list color
 
-            //// ["white",    "#FFFFFF";
-            ////  "black",    "#000000";
-            ////  "red",      "#FF0000";
-            ////  "green",    "#00FF00";
-            ////  "blue",     "#0000FF"]
+            // ["white",    "#FFFFFF";
+            //  "black",    "#000000";
+            //  "red",      "#FF0000";
+            //  "green",    "#00FF00";
+            //  "blue",     "#0000FF"]
 
-            ////Matrix must be n*2
-            //if(nbCol == 2)
-            //{
-            //    if (setGraphicObjectProperty(iObjUID, __GO_UI_STRING_COLNB__, &nbCol, jni_int, 1) == FALSE)
-            //    {
-            //        Scierror(999, const_cast<char*>(_("'%s' property does not exist for this handle.\n")), "String");
-            //        return SET_PROPERTY_ERROR;
-            //    }
+            //Matrix must be n*2
+            if (nbCol == 2)
+            {
+                if (setGraphicObjectProperty(iObjUID, __GO_UI_STRING_COLNB__, &nbCol, jni_int, 1) == FALSE)
+                {
+                    Scierror(999, const_cast<char*>(_("'%s' property does not exist for this handle.\n")), "String");
+                    return SET_PROPERTY_ERROR;
+                }
 
-            //    if(setGraphicObjectProperty(iObjUID, __GO_UI_STRING__, (char**)_pvData, jni_string_vector, nbRow * nbCol) == FALSE)
-            //    {
-            //        Scierror(999, const_cast<char*>(_("'%s' property does not exist for this handle.\n")), "String");
-            //        return SET_PROPERTY_ERROR;
-            //    }
+                if (setGraphicObjectProperty(iObjUID, __GO_UI_STRING__, (char**)_pvData, jni_string_vector, nbRow * nbCol) == FALSE)
+                {
+                    Scierror(999, const_cast<char*>(_("'%s' property does not exist for this handle.\n")), "String");
+                    return SET_PROPERTY_ERROR;
+                }
 
-            //    return SET_PROPERTY_SUCCEED;
-            //}
+                return SET_PROPERTY_SUCCEED;
+            }
             break;
         }
         default:
index 63c9bfe..6cca520 100644 (file)
 
 package org.scilab.modules.gui.bridge.listbox;
 
-import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LISTBOXTOP__;
+import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MAX__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MIN__;
-import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING_COLNB__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE__;
+import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING_COLNB__;
 
 import java.awt.Color;
 import java.awt.Component;
 import java.awt.Font;
 import java.awt.event.AdjustmentEvent;
 import java.awt.event.AdjustmentListener;
+import java.io.File;
+import java.io.IOException;
 
+import javax.imageio.ImageIO;
 import javax.swing.DefaultListModel;
+import javax.swing.ImageIcon;
 import javax.swing.JLabel;
 import javax.swing.JList;
 import javax.swing.JScrollPane;
@@ -39,6 +43,7 @@ import javax.swing.border.Border;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
 
+import org.scilab.modules.commons.gui.FindIconHelper;
 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
 import org.scilab.modules.gui.SwingViewObject;
 import org.scilab.modules.gui.SwingViewWidget;
@@ -53,8 +58,9 @@ import org.scilab.modules.gui.utils.PositionConverter;
 import org.scilab.modules.gui.utils.ScilabRelief;
 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
 import org.scilab.modules.gui.utils.Size;
-import org.scilab.modules.gui.utils.SwingScilabColorItem;
+import org.scilab.modules.gui.utils.SwingScilabIconItem;
 import org.scilab.modules.gui.utils.SwingScilabTextItem;
+import org.scilab.modules.gui.utils.SwingScilabColorItem;
 
 /**
  * Swing implementation for Scilab ListBox in GUIs
@@ -85,8 +91,10 @@ public class SwingScilabListBox extends JScrollPane implements SwingViewObject,
     private ListCellRenderer defaultRenderer = null;
     private ListCellRenderer textRenderer = null;
     private ListCellRenderer colorRenderer = null;
+    private ListCellRenderer iconRenderer = null;
 
     private boolean colorBox = false;
+    private boolean iconBox = false;
 
     /**
      * Constructor
@@ -368,7 +376,11 @@ public class SwingScilabListBox extends JScrollPane implements SwingViewObject,
         GraphicController controller = GraphicController.getController();
         Integer nbCol = (Integer)controller.getProperty(getId(), __GO_UI_STRING_COLNB__);
 
-        colorBox  = false;
+        /* Remove the listener to avoid the callback to be executed */
+        getList().removeListSelectionListener(listListener);
+
+        colorBox = false;
+        iconBox = false;
         if (nbCol == 2) {
             //combocolor ?
             colorBox = true;
@@ -403,11 +415,54 @@ public class SwingScilabListBox extends JScrollPane implements SwingViewObject,
             } catch (NumberFormatException e) {
                 model.clear();
                 colorBox = false;
+                iconBox = true;
             }
+
+            //try to convert 2nd col to icon ( only if color convertion failed )
+            if (iconBox) {
+                if (iconRenderer == null) {
+                    iconRenderer = new ListCellRenderer() {
+                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+                            JLabel label = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+
+                            if (value instanceof SwingScilabIconItem) {
+                                SwingScilabIconItem item = (SwingScilabIconItem)value;
+                                label.setText(item.toString());
+                                label.setIcon(item.getIcon());
+                            }
+                            return label;
+                        }
+                    };
+                }
+
+                getList().setCellRenderer(iconRenderer);
+
+                //fill items
+                int iconOffset = text.length / 2;
+                try {
+                    for (int i = 0; i < iconOffset; i++) {
+                        String iconFile = FindIconHelper.findIcon((text[iconOffset + i]));
+                        //add item in combobox
+                        File file = new File(iconFile);
+                        if (file.exists() == false) {
+                            String filename = FindIconHelper.findImage(iconFile);
+                            file = new File(filename);
+                        }
+
+                        model.addElement(new SwingScilabIconItem(text[i], new ImageIcon(ImageIO.read(file))));
+                    }
+                } catch (IOException e) {
+                    model.clear();
+                    iconBox = false;
+                    colorBox = false;
+                }
+            }
+
         }
 
+
         //default case or colorBox failed
-        if (colorBox == false) {
+        if (colorBox == false && iconBox == false) {
             getList().setCellRenderer(textRenderer);
             /* Do we need to update the strings */
             /* Test performed to avoid loops when the model is updated from here */
@@ -435,6 +490,13 @@ public class SwingScilabListBox extends JScrollPane implements SwingViewObject,
         getList().setModel(model);
         getList().revalidate();
         revalidate();
+
+        //reset selected index
+        getList().setSelectedIndex(-1);
+
+        //take care to add listener BEFORE set Property to avoid multiple remove and multiple add
+        getList().addListSelectionListener(listListener);
+
     }
 
     /**
index e065a5b..e8550c5 100644 (file)
@@ -23,8 +23,13 @@ import java.awt.Color;
 import java.awt.Component;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.File;
+import java.io.IOException;
 import java.util.Arrays;
 
+import javax.imageio.ImageIO;
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
 import javax.swing.JComboBox;
 import javax.swing.JLabel;
 import javax.swing.JList;
@@ -32,6 +37,8 @@ import javax.swing.ListCellRenderer;
 import javax.swing.UIManager;
 import javax.swing.border.Border;
 
+import org.apache.fop.area.inline.Image;
+import org.scilab.modules.commons.gui.FindIconHelper;
 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
 import org.scilab.modules.gui.SwingViewObject;
 import org.scilab.modules.gui.SwingViewWidget;
@@ -47,6 +54,7 @@ import org.scilab.modules.gui.utils.ScilabRelief;
 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
 import org.scilab.modules.gui.utils.Size;
 import org.scilab.modules.gui.utils.SwingScilabColorItem;
+import org.scilab.modules.gui.utils.SwingScilabIconItem;
 import org.scilab.modules.gui.utils.SwingScilabTextItem;
 
 /**
@@ -67,10 +75,13 @@ public class SwingScilabPopupMenu extends JComboBox implements SwingViewObject,
     private Border defaultBorder = null;
 
     private boolean colorBox = false;
+    private boolean iconBox = false;
 
     private ListCellRenderer defaultRenderer = null;
     private ListCellRenderer textRenderer = null;
     private ListCellRenderer colorRenderer = null;
+    private ListCellRenderer iconRenderer = null;
+
 
     /**
      * Constructor
@@ -303,10 +314,12 @@ public class SwingScilabPopupMenu extends JComboBox implements SwingViewObject,
         removeAllItems();
 
         colorBox = false;
+        iconBox = false;
         if (nbCol == 2) {
             //combocolor ?
             colorBox = true;
 
+            //first try to convert 2nd col to color
             if (colorRenderer == null) {
                 colorRenderer = new ListCellRenderer() {
                     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
@@ -317,6 +330,9 @@ public class SwingScilabPopupMenu extends JComboBox implements SwingViewObject,
                             String text = item.toString();
                             label.setText(text);
                             label.setIcon(ColorBox.createColorBox(16, 16, item.getColor()));
+                        } else {
+                            label.setText("");
+                            label.setIcon(null);
                         }
                         return label;
                     }
@@ -327,21 +343,65 @@ public class SwingScilabPopupMenu extends JComboBox implements SwingViewObject,
             setRenderer(colorRenderer);
 
             int colorOffset = text.length / 2;
-            for (int i = 0 ; i < colorOffset ; i++) {
-                try {
+            try {
+                for (int i = 0; i < colorOffset; i++) {
                     Color color = Color.decode(text[colorOffset + i]);
                     //add item in combobox
                     addItem(new SwingScilabColorItem(text[i], color));
-                } catch (NumberFormatException e) {
+                }
+            } catch (NumberFormatException e) {
+                //second color can be a icon
+                colorBox = false;
+                iconBox = true;
+                removeAllItems();
+            }
+
+            //try to convert 2nd col to icon ( only if color convertion failed )
+            if (iconBox) {
+                if (iconRenderer == null) {
+                    iconRenderer = new ListCellRenderer() {
+                        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+                            JLabel label = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+                            if (value instanceof SwingScilabIconItem) {
+                                SwingScilabIconItem item = (SwingScilabIconItem)value;
+                                String text = item.toString();
+                                label.setText(text);
+                                label.setIcon(item.getIcon());
+                            } else {
+                                label.setText("");
+                                label.setIcon(null);
+                            }
+                            return label;
+                        }
+                    };
+                }
+
+                setRenderer(iconRenderer);
+
+                //fill items
+                int iconOffset = text.length / 2;
+                try {
+                    for (int i = 0; i < iconOffset; i++) {
+                        String iconFile = FindIconHelper.findIcon((text[iconOffset + i]));
+                        //add item in combobox
+                        File file = new File(iconFile);
+                        if (file.exists() == false) {
+                            String filename = FindIconHelper.findImage(iconFile);
+                            file = new File(filename);
+                        }
+
+                        addItem(new SwingScilabIconItem(text[i], new ImageIcon(ImageIO.read(file))));
+                    }
+                } catch (IOException e) {
                     removeAllItems();
+                    iconBox = false;
                     colorBox = false;
-                    break;
                 }
             }
         }
 
-        //default case or colorBox failed
-        if (colorBox == false) {
+        //default case or colorBox and iconBox failed
+        if (colorBox == false && iconBox == false) {
             setRenderer(textRenderer);
 
             if (text.length == 1 && text[0].length() == 0) {
@@ -357,7 +417,6 @@ public class SwingScilabPopupMenu extends JComboBox implements SwingViewObject,
         //take care to add listener BEFORE set Property to avoid multiple remove and multiple add
         addActionListener(defaultActionListener);
         controller.setProperty(uid, __GO_UI_VALUE__, new Double[] {});
-        /* Remove the listener to avoid the callback to be executed */
     }
 
     /**
diff --git a/scilab/modules/gui/src/java/org/scilab/modules/gui/utils/SwingScilabIconItem.java b/scilab/modules/gui/src/java/org/scilab/modules/gui/utils/SwingScilabIconItem.java
new file mode 100644 (file)
index 0000000..2ffcf8c
--- /dev/null
@@ -0,0 +1,31 @@
+package org.scilab.modules.gui.utils;
+
+import javax.swing.Icon;
+
+public class SwingScilabIconItem {
+    private String text;
+    private Icon icon;
+
+    /**
+     * Constructor
+     * @param text the text displayed in the item
+     */
+    public SwingScilabIconItem(String text, Icon icon) {
+        this.text = text;
+        this.icon = icon;
+    }
+
+    /**
+     * Overload Object toString() method
+     * @return the item converted to String
+     * @see java.lang.Object#toString()
+     */
+    public String toString() {
+        return text;
+    }
+
+    public Icon getIcon() {
+        return icon;
+    }
+
+}