* New uimenu properties:
- icon: add an icon on the left of the label menu.
-* "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", ...]
- ["Item1", "icon_name"; "Item2", "icon_name", ...]
- Then the component will display a colored box or icon on the left of the associated string.
+* "listbox" and "popupmenu" style uicontrols can now manage color boxes, icons, background and
+ foreground colors when the "String" property is set to a matrix matching the format:
+ ["#color1", "Item1", "#background1", "#foreground1"; "#color2", "Item2", ..., ...]
+ ["icon1", "Item1", "#background1", "#foreground1"; "icon2", "Item2", ..., ...]
+ with "#color1", "#background1" and "#foreground1" in HTML format #XXXXXX.
+ Then the component will display a colored box or icon on the left of the associated string,
+ and different background/foreground colors for items.
* New management of uicontrols positioning:
In previous versions, uicontrols position was managed in an absolute way through
import java.util.logging.Level;
import java.util.logging.Logger;
+import javax.imageio.ImageIO;
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
+
import org.scilab.modules.commons.OS;
import org.scilab.modules.commons.ScilabCommonsUtils;
THEME_BASENAME.add(path);
}
+
+ public static Icon loadIcon(String icon) throws IOException {
+ String iconFile = findIcon(icon, false);
+ if (iconFile == null) {
+ iconFile = "";
+ }
+
+ //add item in listbox
+ File file = new File(iconFile);
+ if (file.exists() == false) {
+ String filename = findImage(iconFile, false);
+ if (filename == null) {
+ filename = "";
+ }
+
+ file = new File(filename);
+ }
+
+ return new ImageIcon(ImageIO.read(file));
+ }
}
subdemolist = [ subdemolist ;
_("Show images with uicontrols ") , "uicontrol_image.dem.sce" ];
+ subdemolist = [ subdemolist ;
+ _("Listboxes and Popupmenus") , "uicontrol_listbox_popupmenu.dem.sce" ];
+
subdemolist(:,2) = demopath + subdemolist(:,2);
endfunction
--- /dev/null
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2014 - Scilab Enterprises - Antoine ELIAS
+//
+// 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
+//
+
+function f = demo_list_combo()
+ c = get(0);c.usedeprecatedskin = "off";
+ f = figure("dockable", "off" ,"infobar_visible", "off", "toolbar_visible", "off", "menubar_visible", "off", "default_axes", "off", "visible", "off", "layout", "gridbag");
+ set(f, "figure_id", 100001);
+ set(f, "figure_name", gettext("Listboxes and Popupmenus"));
+ set(f, "axes_size", [850 500]);
+
+ colors4 = [
+ "#000000", "Color 01", "#000000", "#FFFFFF";
+ "#000080", "Color 02", "#FFFFFF", "#000000";
+ "#0000FF", "Color 03", "#000000", "#FFFFFF";
+ "#008000", "Color 04", "#FFFFFF", "#000000";
+ "#008080", "Color 05", "#000000", "#FFFFFF";
+ "#0080FF", "Color 06", "#FFFFFF", "#000000";
+ "#00FF00", "Color 07", "#000000", "#FFFFFF";
+ "#00FF80", "Color 08", "#FFFFFF", "#000000";
+ "#00FFFF", "Color 09", "#000000", "#FFFFFF";
+ "#800000", "Color 10", "#FFFFFF", "#000000";
+ "#800080", "Color 11", "#000000", "#FFFFFF";
+ "#8000FF", "Color 12", "#FFFFFF", "#000000";
+ "#808000", "Color 13", "#000000", "#FFFFFF";
+ "#808080", "Color 14", "#FFFFFF", "#000000";
+ "#8080FF", "Color 15", "#000000", "#FFFFFF";
+ "#80FF00", "Color 16", "#FFFFFF", "#000000";
+ "#80FF80", "Color 17", "#000000", "#FFFFFF";
+ "#80FFFF", "Color 18", "#FFFFFF", "#000000";
+ "#FF0000", "Color 19", "#000000", "#FFFFFF";
+ "#FF0080", "Color 20", "#FFFFFF", "#000000";
+ "#FF00FF", "Color 21", "#000000", "#FFFFFF";
+ "#FF8000", "Color 22", "#FFFFFF", "#000000";
+ "#FF8080", "Color 23", "#000000", "#FFFFFF";
+ "#FF80FF", "Color 24", "#FFFFFF", "#000000";
+ "#FFFF00", "Color 25", "#000000", "#FFFFFF";
+ "#FFFF80", "Color 26", "#FFFFFF", "#000000";
+ "#FFFFFF", "Color 27", "#000000", "#FFFFFF"];
+
+ c = createConstraints("gridbag", [1, 1, 1, 1], [1 1], "both");
+ uicontrol(f, "style", "listbox", "constraints", c, "margins", [5 5 5 5], "string", colors4);
+
+ c = createConstraints("gridbag", [1, 2, 1, 1], [0 0], "both");
+ uicontrol(f, "style", "popupmenu", "constraints", c, "margins", [5 5 5 5], "string", colors4);
+
+ colors4(1:4:$,1) = "go-up";
+ colors4(2:4:$,1) = "go-previous";
+ colors4(3:4:$,1) = "go-down";
+ colors4(4:4:$,1) = "go-next";
+
+ c = createConstraints("gridbag", [1, 3, 1, 1], [1 1], "both");
+ uicontrol(f, "style", "listbox", "constraints", c, "margins", [5 5 5 5], "string", colors4);
+
+ c = createConstraints("gridbag", [1, 4, 1, 1], [0 0], "both");
+ uicontrol(f, "style", "popupmenu", "constraints", c, "margins", [5 5 5 5], "string", colors4);
+
+ colors3_1 = [
+ "#000000", "Color 01", "#D0D0D0";
+ "#000080", "Color 02", "#FFFFFF";
+ "#0000FF", "Color 03", "#D0D0D0";
+ "#008000", "Color 04", "#FFFFFF";
+ "#008080", "Color 05", "#D0D0D0";
+ "#0080FF", "Color 06", "#FFFFFF";
+ "#00FF00", "Color 07", "#D0D0D0";
+ "#00FF80", "Color 08", "#FFFFFF";
+ "#00FFFF", "Color 09", "#D0D0D0";
+ "#800000", "Color 10", "#FFFFFF";
+ "#800080", "Color 11", "#D0D0D0";
+ "#8000FF", "Color 12", "#FFFFFF";
+ "#808000", "Color 13", "#D0D0D0";
+ "#808080", "Color 14", "#FFFFFF";
+ "#8080FF", "Color 15", "#D0D0D0";
+ "#80FF00", "Color 16", "#FFFFFF";
+ "#80FF80", "Color 17", "#D0D0D0";
+ "#80FFFF", "Color 18", "#FFFFFF";
+ "#FF0000", "Color 19", "#D0D0D0";
+ "#FF0080", "Color 20", "#FFFFFF";
+ "#FF00FF", "Color 21", "#D0D0D0";
+ "#FF8000", "Color 22", "#FFFFFF";
+ "#FF8080", "Color 23", "#D0D0D0";
+ "#FF80FF", "Color 24", "#FFFFFF";
+ "#FFFF00", "Color 25", "#D0D0D0";
+ "#FFFF80", "Color 26", "#FFFFFF";
+ "#FFFFFF", "Color 27", "#D0D0D0"];
+
+ c = createConstraints("gridbag", [2, 1, 1, 1], [1 1], "both");
+ uicontrol(f, "style", "listbox", "constraints", c, "margins", [5 5 5 5], "string", colors3_1);
+
+ c = createConstraints("gridbag", [2, 2, 1, 1], [0 0], "both");
+ cb3_1 = uicontrol(f, "style", "popupmenu", "constraints", c, "margins", [5 5 5 5], "string", colors3_1);
+
+ colors3_2 = [
+ "Color 01", "#000000", "#FFFFFF";
+ "Color 02", "#FFFFFF", "#000000";
+ "Color 03", "#000000", "#FFFFFF";
+ "Color 04", "#FFFFFF", "#000000";
+ "Color 05", "#000000", "#FFFFFF";
+ "Color 06", "#FFFFFF", "#000000";
+ "Color 07", "#000000", "#FFFFFF";
+ "Color 08", "#FFFFFF", "#000000";
+ "Color 09", "#000000", "#FFFFFF";
+ "Color 10", "#FFFFFF", "#000000";
+ "Color 11", "#000000", "#FFFFFF";
+ "Color 12", "#FFFFFF", "#000000";
+ "Color 13", "#000000", "#FFFFFF";
+ "Color 14", "#FFFFFF", "#000000";
+ "Color 15", "#000000", "#FFFFFF";
+ "Color 16", "#FFFFFF", "#000000";
+ "Color 17", "#000000", "#FFFFFF";
+ "Color 18", "#FFFFFF", "#000000";
+ "Color 19", "#000000", "#FFFFFF";
+ "Color 20", "#FFFFFF", "#000000";
+ "Color 21", "#000000", "#FFFFFF";
+ "Color 22", "#FFFFFF", "#000000";
+ "Color 23", "#000000", "#FFFFFF";
+ "Color 24", "#FFFFFF", "#000000";
+ "Color 25", "#000000", "#FFFFFF";
+ "Color 26", "#FFFFFF", "#000000";
+ "Color 27", "#000000", "#FFFFFF"];
+
+ c = createConstraints("gridbag", [2, 3, 1, 1], [1 1], "both");
+ lst3_2 = uicontrol(f, "style", "listbox", "constraints", c, "margins", [5 5 5 5], "string", colors3_2);
+
+ c = createConstraints("gridbag", [2, 4, 1, 1], [0 0], "both");
+ cb3_2 = uicontrol(f, "style", "popupmenu", "constraints", c, "margins", [5 5 5 5], "string", colors3_2);
+
+ colors2_1 = [
+ "#000000", "Color 01";
+ "#000080", "Color 02";
+ "#0000FF", "Color 03";
+ "#008000", "Color 04";
+ "#008080", "Color 05";
+ "#0080FF", "Color 06";
+ "#00FF00", "Color 07";
+ "#00FF80", "Color 08";
+ "#00FFFF", "Color 09";
+ "#800000", "Color 10";
+ "#800080", "Color 11";
+ "#8000FF", "Color 12";
+ "#808000", "Color 13";
+ "#808080", "Color 14";
+ "#8080FF", "Color 15";
+ "#80FF00", "Color 16";
+ "#80FF80", "Color 17";
+ "#80FFFF", "Color 18";
+ "#FF0000", "Color 19";
+ "#FF0080", "Color 20";
+ "#FF00FF", "Color 21";
+ "#FF8000", "Color 22";
+ "#FF8080", "Color 23";
+ "#FF80FF", "Color 24";
+ "#FFFF00", "Color 25";
+ "#FFFF80", "Color 26";
+ "#FFFFFF", "Color 27"];
+
+ c = createConstraints("gridbag", [3, 1, 1, 1], [1 1], "both");
+ uicontrol(f, "style", "listbox", "constraints", c, "margins", [5 5 5 5], "string", colors2_1);
+
+ c = createConstraints("gridbag", [3, 2, 1, 1], [0 0], "both");
+ uicontrol(f, "style", "popupmenu", "constraints", c, "margins", [5 5 5 5], "string", colors2_1);
+
+ colors2_2 = [
+ "Color 01", "#D0D0D0";
+ "Color 02", "#FFFFFF";
+ "Color 03", "#D0D0D0";
+ "Color 04", "#FFFFFF";
+ "Color 05", "#D0D0D0";
+ "Color 06", "#FFFFFF";
+ "Color 07", "#D0D0D0";
+ "Color 08", "#FFFFFF";
+ "Color 09", "#D0D0D0";
+ "Color 10", "#FFFFFF";
+ "Color 11", "#D0D0D0";
+ "Color 12", "#FFFFFF";
+ "Color 13", "#D0D0D0";
+ "Color 14", "#FFFFFF";
+ "Color 15", "#D0D0D0";
+ "Color 16", "#FFFFFF";
+ "Color 17", "#D0D0D0";
+ "Color 18", "#FFFFFF";
+ "Color 19", "#D0D0D0";
+ "Color 20", "#FFFFFF";
+ "Color 21", "#D0D0D0";
+ "Color 22", "#FFFFFF";
+ "Color 23", "#D0D0D0";
+ "Color 24", "#FFFFFF";
+ "Color 25", "#D0D0D0";
+ "Color 26", "#FFFFFF";
+ "Color 27", "#D0D0D0"];
+
+ c = createConstraints("gridbag", [3, 3, 1, 1], [1 1], "both");
+ uicontrol(f, "style", "listbox", "constraints", c, "margins", [5 5 5 5], "string", colors2_2);
+
+ c = createConstraints("gridbag", [3, 4, 1, 1], [0 0], "both");
+ uicontrol(f, "style", "popupmenu", "constraints", c, "margins", [5 5 5 5], "string", colors2_2);
+
+ c = createConstraints("gridbag", [4, 1, 1, 1], [1 1], "both");
+ uicontrol(f, "style", "listbox", "constraints", c, "margins", [5 5 5 5], "string", colors4(:,1:2));
+
+ c = createConstraints("gridbag", [4, 2, 1, 1], [0 0], "both");
+ uicontrol(f, "style", "popupmenu", "constraints", c, "margins", [5 5 5 5], "string", colors4(:,1:2));
+
+ c = createConstraints("gridbag", [4, 3, 1, 1], [1 1], "both");
+ uicontrol(f, "style", "listbox", "constraints", c, "margins", [5 5 5 5], "string", colors4(:,2));
+
+ c = createConstraints("gridbag", [4, 4, 1, 1], [0 0], "both");
+ uicontrol(f, "style", "popupmenu", "constraints", c, "margins", [5 5 5 5], "string", colors4(:,2));
+
+ set(f, "visible", "on");
+endfunction
+
+f = demo_list_combo();
+clear demo_list_combo;
package org.scilab.modules.gui.bridge.listbox;
-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_LISTBOXTOP__;
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.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
-import javax.swing.ImageIcon;
+import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
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.SwingScilabIconItem;
-import org.scilab.modules.gui.utils.SwingScilabTextItem;
-import org.scilab.modules.gui.utils.SwingScilabColorItem;
+import org.scilab.modules.gui.utils.SwingScilabListItem;
/**
* Swing implementation for Scilab ListBox in GUIs
private JList list;
private ListCellRenderer defaultRenderer = null;
- private ListCellRenderer textRenderer = null;
- private ListCellRenderer colorRenderer = null;
- private ListCellRenderer iconRenderer = null;
-
- private boolean colorBox = false;
- private boolean iconBox = false;
+ private ListCellRenderer listRenderer = null;
/**
* Constructor
getViewport().add(getList());
defaultRenderer = getList().getCellRenderer();
- textRenderer = new DefaultListCellRenderer();
- getList().setCellRenderer(textRenderer);
+ listRenderer = 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 SwingScilabListItem) {
+ SwingScilabListItem item = (SwingScilabListItem) value;
+ label.setText(item.toString());
+ label.setIcon(item.getIcon());
+
+ if (isSelected == false && item.getBackground() != null) {
+ label.setBackground(item.getBackground());
+ }
+
+ if (isSelected == false && item.getForeground() != null) {
+ label.setForeground(item.getForeground());
+ }
+ }
+ return label;
+ }
+ };
+
+ getList().setCellRenderer(listRenderer);
listListener = new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
adjustmentListener = new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent arg0) {
int listboxtopValue = getList().getUI().locationToIndex(getList(), getViewport().getViewPosition()) + 1;
- GraphicController.getController().setProperty(uid, __GO_UI_LISTBOXTOP__, new Integer[] {listboxtopValue});
+ GraphicController.getController().setProperty(uid, __GO_UI_LISTBOXTOP__, new Integer[] { listboxtopValue });
}
};
getVerticalScrollBar().addAdjustmentListener(adjustmentListener);
/**
* Sets the visibility status of an UIElement
- * @param newVisibleState the visibility status we want to set for the UIElement
- * (true if the UIElement is visible, false if not)
+ * @param newVisibleState the visibility status we want to set for the
+ * UIElement (true if the UIElement is visible, false if not)
*/
public void setVisible(boolean newVisibleState) {
super.setVisible(newVisibleState);
/**
* Sets the enable status of an UIElement
* @param newEnableState the enable status we want to set for the UIElement
- * (true if the UIElement is enabled, false if not)
+ * (true if the UIElement is enabled, false if not)
*/
public void setEnabled(boolean newEnableState) {
if (newEnableState != super.isEnabled()) {
// check numbers of columns
GraphicController controller = GraphicController.getController();
- Integer nbCol = (Integer)controller.getProperty(getId(), __GO_UI_STRING_COLNB__);
+ Integer nbCol = (Integer) controller.getProperty(getId(), __GO_UI_STRING_COLNB__);
/* Remove the listener to avoid the callback to be executed */
getList().removeListSelectionListener(listListener);
- getList().removeAll();
-
- 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) {
- JLabel label = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
-
- if (value instanceof SwingScilabColorItem) {
- SwingScilabColorItem item = (SwingScilabColorItem)value;
- String text = item.toString();
- label.setText(text);
- label.setIcon(ColorBox.createColorBox(16, 16, item.getColor()));
- }
- return label;
- }
- };
-
- }
-
- getList().setCellRenderer(colorRenderer);
-
- int colorOffset = text.length / 2;
- try {
- for (int i = 0 ; i < colorOffset ; i++) {
- Color color = Color.decode(text[colorOffset + i]);
- //add item in combobox
- model.addElement(new SwingScilabColorItem(text[i], color));
+ boolean tryColorBox = true;
+ boolean tryColor = true;
+ boolean tryIcon = true;
+ int nbRow = text.length / nbCol;
+
+ for (int i = 0; i < nbRow; i++) {
+ Icon icon = null;
+ String str = null;
+ Color background = null;
+ Color foreground = null;
+
+ //4 cols :
+ // - 1st icon or colorBox
+ // - 2nd text
+ // - 3rd BG
+ // - 4th FG
+
+ //3 cols : 2 cases
+ // - 1st icon or colorBox
+ // - 2nd text
+ // - 3rd BG
+ //or
+ // - 1st text
+ // - 2nd BG
+ // - 3rd FG
+
+ //2 cols : 2 cases
+ // - 1st icon or colorBox
+ // - 2nd text
+ //or
+ // - 1st text
+ // - 2nd BG
+
+ if (tryColorBox) { //color
+ try {
+ Color color = Color.decode(text[i]);
+ icon = ColorBox.createColorBox(16, 16, color);
+ } catch (NumberFormatException e) {
+ tryColorBox = false;
+ model.clear();
+ //restart loop with icon
+ i = -1;
+ continue;
}
-
- } 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;
- }
- };
+ if (tryIcon) {
+ try {
+ icon = FindIconHelper.loadIcon(text[i]);
+ } catch (IOException e) {
+ tryIcon = false;
+ model.clear();
+ //restart loop with text only
+ i = -1;
+ continue;
}
+ }
- getList().setCellRenderer(iconRenderer);
-
- //fill items
- int iconOffset = text.length / 2;
+ if (tryColor) {
try {
- for (int i = 0; i < iconOffset; i++) {
- String iconFile = FindIconHelper.findIcon((text[iconOffset + i]), false);
- if (iconFile == null) {
- iconFile = "";
- }
-
- //add item in listbox
- File file = new File(iconFile);
- if (file.exists() == false) {
- String filename = FindIconHelper.findImage(iconFile, false);
- if (filename == null) {
- filename = "";
- }
+ int colIndex = 0;
+ if (tryColorBox || tryIcon) {
+ colIndex = 1;
+ }
- file = new File(filename);
+ str = text[(nbRow * colIndex) + i];
+ if (nbCol > (1 + colIndex)) {
+ background = Color.decode(text[nbRow * (1 + colIndex) + i]);
+ if (nbCol > (2 + colIndex)) {
+ foreground = Color.decode(text[nbRow * (2 + colIndex) + i]);
}
-
- model.addElement(new SwingScilabIconItem(text[i], new ImageIcon(ImageIO.read(file))));
}
- } catch (IOException e) {
+
+ //add item in list box
+ model.addElement(new SwingScilabListItem(str, icon, background, foreground));
+ } catch (NumberFormatException e) {
+ tryColor = false;
model.clear();
- iconBox = false;
- colorBox = false;
+ //restart loop with text only
+ i = -1;
+ continue;
+ }
+ } else { //text only
+ for (int j = 0; j < nbCol; j++) {
+ model.addElement(new SwingScilabListItem(text[nbRow * j + i], icon, background, foreground));
}
- }
-
- }
-
- //default case or colorBox failed
- if (colorBox == false && iconBox == false) {
- getList().setCellRenderer(textRenderer);
- for (int i = 0; i < text.length; i++) {
- model.addElement(new SwingScilabTextItem(text[i]));
}
}
//reset selected index
getList().setSelectedIndex(-1);
getList().setModel(model);
- getList().revalidate();
-
-
+ invalidate();
//take care to add listener BEFORE set Property to avoid multiple remove and multiple add
getList().addListSelectionListener(listListener);
-
}
/**
}
/**
- * Adjusts the view so that the element given by index is displayed at the top of the ListBox.
- * @param index the index of the element to be displayed at the top of the ListBox.
+ * Adjusts the view so that the element given by index is displayed at the
+ * top of the ListBox.
+ * @param index the index of the element to be displayed at the top of the
+ * ListBox.
*/
public void setListBoxTop(int index) {
getVerticalScrollBar().removeAdjustmentListener(adjustmentListener);
public void update(int property, Object value) {
GraphicController controller = GraphicController.getController();
switch (property) {
- case __GO_UI_VALUE__ : {
- Double[] indexes = (Double[])value;
+ case __GO_UI_VALUE__: {
+ Double[] indexes = (Double[]) value;
int[] index = new int[indexes.length];
- for (int i = 0 ; i < indexes.length ; i++) {
+ for (int i = 0; i < indexes.length; i++) {
index[i] = indexes[i].intValue();
}
setSelectedIndices(index);
break;
}
- case __GO_UI_BACKGROUNDCOLOR__ : {
+ case __GO_UI_BACKGROUNDCOLOR__: {
Double[] allColors = ((Double[]) value);
if (allColors[0] != -1) {
- setListBackground(new Color((int) (allColors[0] * COLORS_COEFF),
- (int) (allColors[1] * COLORS_COEFF),
- (int) (allColors[2] * COLORS_COEFF)));
+ setListBackground(new Color((int) (allColors[0] * COLORS_COEFF), (int) (allColors[1] * COLORS_COEFF), (int) (allColors[2] * COLORS_COEFF)));
} else {
resetBackground();
}
break;
}
- case __GO_UI_STRING__ : {
+ case __GO_UI_STRING__: {
// Listboxes manage string vectors
setText((String[]) value);
break;
}
- case __GO_UI_MAX__ : {
+ case __GO_UI_MAX__: {
Double maxValue = ((Double) value);
// Enable/Disable multiple selection
double minValue = (Double) controller.getProperty(uid, __GO_UI_MIN__);
setMultipleSelectionEnabled(maxValue - minValue > 1);
break;
}
- case __GO_UI_MIN__ : {
+ case __GO_UI_MIN__: {
Double minValue = ((Double) value);
// Enable/Disable multiple selection
Double maxValue = (Double) controller.getProperty(uid, __GO_UI_MAX__);
}
public void resetBackground() {
- Color color = (Color)UIManager.getLookAndFeelDefaults().get("List.background");
+ Color color = (Color) UIManager.getLookAndFeelDefaults().get("List.background");
if (color != null) {
getList().setBackground(color);
}
}
public void resetForeground() {
- Color color = (Color)UIManager.getLookAndFeelDefaults().get("List.foreground");
+ Color color = (Color) UIManager.getLookAndFeelDefaults().get("List.foreground");
if (color != null) {
getList().setForeground(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.DefaultComboBoxModel;
import javax.swing.Icon;
-import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
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.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.SwingScilabListItem;
/**
* Swing implementation for Scilab PopupMenu in GUIs
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;
-
+ private ListCellRenderer listRenderer = null;
/**
* Constructor
*/
public SwingScilabPopupMenu() {
super();
+
defaultRenderer = getRenderer();
- textRenderer = new ListCellRenderer() {
+ listRenderer = 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 SwingScilabTextItem) {
- label.setText(value.toString());
- label.setIcon(null);
+
+ if (value instanceof SwingScilabListItem) {
+ SwingScilabListItem item = (SwingScilabListItem) value;
+
+ label.setText(item.toString());
+ label.setIcon(item.getIcon());
+
+ //index == -1 is for selected item after click
+ //so let standard FG and BG
+ if (index != - 1 && isSelected == false && item.getBackground() != null) {
+ label.setBackground(item.getBackground());
+ }
+
+ if (index != - 1 && isSelected == false && item.getForeground() != null) {
+ label.setForeground(item.getForeground());
+ }
}
return label;
}
};
- setRenderer(textRenderer);
+ setRenderer(listRenderer);
/* Bug 3635 fixed: allow arrow keys to browse items */
putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
defaultActionListener = new ActionListener() {
* @param text the text of the items
*/
public void setText(String[] text) {
+ DefaultComboBoxModel model = new DefaultComboBoxModel();
- // check numbers of columns
+ //get numbers of columns
GraphicController controller = GraphicController.getController();
- Integer nbCol = (Integer)controller.getProperty(getId(), __GO_UI_STRING_COLNB__);
+ Integer nbCol = (Integer) controller.getProperty(getId(), __GO_UI_STRING_COLNB__);
/* Remove the listener to avoid the callback to be executed */
removeActionListener(defaultActionListener);
- /* Clear previous items */
- 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) {
- JLabel label = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
-
- if (value instanceof SwingScilabColorItem) {
- SwingScilabColorItem item = (SwingScilabColorItem)value;
- String text = item.toString();
- label.setText(text);
- label.setIcon(ColorBox.createColorBox(16, 16, item.getColor()));
- } else {
- label.setText("");
- label.setIcon(null);
- }
- return label;
- }
- };
-
- }
-
- setRenderer(colorRenderer);
-
- int colorOffset = text.length / 2;
- 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));
+ boolean tryColorBox = true;
+ boolean tryColor = true;
+ boolean tryIcon = true;
+ int nbRow = text.length / nbCol;
+
+ for (int i = 0; i < nbRow; i++) {
+ Icon icon = null;
+ String str = null;
+ Color background = null;
+ Color foreground = null;
+
+ //4 cols :
+ // - 1st icon or colorBox
+ // - 2nd text
+ // - 3rd BG
+ // - 4th FG
+
+ //3 cols : 2 cases
+ // - 1st icon or colorBox
+ // - 2nd text
+ // - 3rd BG
+ //or
+ // - 1st text
+ // - 2nd BG
+ // - 3rd FG
+
+ //2 cols : 2 cases
+ // - 1st icon or colorBox
+ // - 2nd text
+ //or
+ // - 1st text
+ // - 2nd BG
+
+ if (tryColorBox) { //color
+ try {
+ Color color = Color.decode(text[i]);
+ icon = ColorBox.createColorBox(16, 16, color);
+ } catch (NumberFormatException e) {
+ tryColorBox = false;
+ model.removeAllElements();
+ //restart loop with icon
+ i = -1;
+ continue;
}
- } 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;
- }
- };
+ if (tryIcon) {
+ try {
+ icon = FindIconHelper.loadIcon(text[i]);
+ } catch (IOException e) {
+ tryIcon = false;
+ model.removeAllElements();
+ //restart loop with text only
+ i = -1;
+ continue;
}
+ }
- setRenderer(iconRenderer);
-
- //fill items
- int iconOffset = text.length / 2;
+ if (tryColor) {
try {
- for (int i = 0; i < iconOffset; i++) {
- String iconFile = FindIconHelper.findIcon((text[iconOffset + i]), false);
- if (iconFile == null) {
- iconFile = "";
- }
-
- //add item in combobox
- File file = new File(iconFile);
- if (file.exists() == false) {
- String filename = FindIconHelper.findImage(iconFile, false);
- if (filename == null) {
- filename = "";
- }
+ int colIndex = 0;
+ if (tryColorBox || tryIcon) {
+ colIndex = 1;
+ }
- file = new File(filename);
+ str = text[(nbRow * colIndex) + i];
+ if (nbCol > (1 + colIndex)) {
+ background = Color.decode(text[nbRow * (1 + colIndex) + i]);
+ if (nbCol > (2 + colIndex)) {
+ foreground = Color.decode(text[nbRow * (2 + colIndex) + i]);
}
-
- addItem(new SwingScilabIconItem(text[i], new ImageIcon(ImageIO.read(file))));
}
- } catch (IOException e) {
- removeAllItems();
- iconBox = false;
- colorBox = false;
- }
- }
- }
-
- //default case or colorBox and iconBox failed
- if (colorBox == false && iconBox == false) {
- setRenderer(textRenderer);
- if (text.length == 1 && text[0].length() == 0) {
- /* Clear the popup items */
- } else {
- for (int i = 0; i < text.length; i++) {
- addItem(new SwingScilabTextItem(text[i]));
+ //add item in list box
+ model.addElement(new SwingScilabListItem(str, icon, background, foreground));
+ } catch (NumberFormatException e) {
+ tryColor = false;
+ model.removeAllElements();
+ //restart loop with text only
+ i = -1;
+ continue;
+ }
+ } else { //text only
+ for (int j = 0; j < nbCol; j++) {
+ model.addElement(new SwingScilabListItem(text[nbRow * j + i], icon, background, foreground));
}
}
}
+ //reset selected index
setSelectedIndex(-1);
+ setModel(model);
+ invalidate();
//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[] {});
}
/**
+++ /dev/null
-package org.scilab.modules.gui.utils;
-
-import java.awt.Color;
-
-public class SwingScilabColorItem {
- private String text;
- private Color color;
-
- /**
- * Constructor
- * @param text the text displayed in the item
- */
- public SwingScilabColorItem(String text, Color color) {
- this.text = text;
- this.color = color;
- }
-
- /**
- * Overload Object toString() method
- * @return the item converted to String
- * @see java.lang.Object#toString()
- */
- public String toString() {
- return text;
- }
-
- public Color getColor() {
- return color;
- }
-}
package org.scilab.modules.gui.utils;
+import java.awt.Color;
+
import javax.swing.Icon;
-public class SwingScilabIconItem {
+public class SwingScilabListItem {
private String text;
private Icon icon;
+ private Color background;
+ private Color foreground;
/**
* Constructor
* @param text the text displayed in the item
*/
- public SwingScilabIconItem(String text, Icon icon) {
+ public SwingScilabListItem(String text, Icon icon, Color background, Color foreground) {
this.text = text;
this.icon = icon;
+ this.background = background;
+ this.foreground = foreground;
}
/**
return icon;
}
+ public Color getBackground() {
+ return background;
+ }
+
+ public Color getForeground() {
+ return foreground;
+ }
}