2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007 - INRIA - Vincent Couvert
4 * Copyright (C) 2007 - INRIA - Marouane BEN JELLOUL
5 * Copyright (C) 2010-2011 - DIGITEO - Vincent COUVERT
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
15 package org.scilab.modules.gui.bridge.listbox;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LISTBOXTOP__;
19 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MAX__;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MIN__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING_COLNB__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE__;
25 import java.awt.Color;
26 import java.awt.Component;
28 import java.awt.event.AdjustmentEvent;
29 import java.awt.event.AdjustmentListener;
30 import java.io.IOException;
32 import javax.swing.DefaultListModel;
33 import javax.swing.Icon;
34 import javax.swing.JLabel;
35 import javax.swing.JList;
36 import javax.swing.JScrollPane;
37 import javax.swing.ListCellRenderer;
38 import javax.swing.ListSelectionModel;
39 import javax.swing.UIManager;
40 import javax.swing.border.Border;
41 import javax.swing.event.ListSelectionEvent;
42 import javax.swing.event.ListSelectionListener;
44 import org.scilab.modules.commons.gui.FindIconHelper;
45 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
46 import org.scilab.modules.gui.SwingViewObject;
47 import org.scilab.modules.gui.SwingViewWidget;
48 import org.scilab.modules.gui.events.callback.CommonCallBack;
49 import org.scilab.modules.gui.listbox.SimpleListBox;
50 import org.scilab.modules.gui.menubar.MenuBar;
51 import org.scilab.modules.gui.textbox.TextBox;
52 import org.scilab.modules.gui.toolbar.ToolBar;
53 import org.scilab.modules.gui.utils.ColorBox;
54 import org.scilab.modules.gui.utils.Position;
55 import org.scilab.modules.gui.utils.PositionConverter;
56 import org.scilab.modules.gui.utils.ScilabRelief;
57 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
58 import org.scilab.modules.gui.utils.Size;
59 import org.scilab.modules.gui.utils.SwingScilabListItem;
62 * Swing implementation for Scilab ListBox in GUIs
63 * @author Vincent COUVERT
64 * @author Marouane BEN JELLOUL
66 public class SwingScilabListBox extends JScrollPane implements SwingViewObject, SimpleListBox {
68 private static final long serialVersionUID = 3507396207331058895L;
70 private static final int COLORS_COEFF = 255;
74 private Border defaultBorder = null;
76 private CommonCallBack callback;
78 private ListSelectionListener listListener;
80 private AdjustmentListener adjustmentListener;
87 private ListCellRenderer defaultRenderer = null;
88 private ListCellRenderer listRenderer = null;
93 public SwingScilabListBox() {
95 getViewport().add(getList());
96 defaultRenderer = getList().getCellRenderer();
98 listRenderer = new ListCellRenderer() {
99 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
100 JLabel label = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
102 if (value instanceof SwingScilabListItem) {
103 SwingScilabListItem item = (SwingScilabListItem) value;
104 label.setText(item.toString());
105 label.setIcon(item.getIcon());
107 if (isSelected == false && item.getBackground() != null) {
108 label.setBackground(item.getBackground());
111 if (isSelected == false && item.getForeground() != null) {
112 label.setForeground(item.getForeground());
119 getList().setCellRenderer(listRenderer);
121 listListener = new ListSelectionListener() {
122 public void valueChanged(ListSelectionEvent e) {
125 if (e.getValueIsAdjusting()) {
129 // Scilab indices in Value begin at 1 and Java indices begin at 0
130 int[] javaIndices = getList().getSelectedIndices().clone();
131 Double[] scilabIndices = new Double[javaIndices.length];
132 for (int i = 0; i < getList().getSelectedIndices().length; i++) {
133 scilabIndices[i] = (double) javaIndices[i] + 1;
136 GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, scilabIndices);
137 if (callback != null) {
138 callback.actionPerformed(null);
142 getList().addListSelectionListener(listListener);
144 adjustmentListener = new AdjustmentListener() {
145 public void adjustmentValueChanged(AdjustmentEvent arg0) {
146 int listboxtopValue = getList().getUI().locationToIndex(getList(), getViewport().getViewPosition()) + 1;
147 GraphicController.getController().setProperty(uid, __GO_UI_LISTBOXTOP__, new Integer[] { listboxtopValue });
150 getVerticalScrollBar().addAdjustmentListener(adjustmentListener);
154 * To get the Background color of the element.
155 * @return color the Color
157 public Color getBackground() {
158 return getList().getBackground();
162 * To get the Font of the element.
163 * @return font the Font
165 public Font getFont() {
166 return getList().getFont();
170 * To get the Foreground color of the element.
171 * @return color the Color
173 public Color getForeground() {
174 return getList().getForeground();
178 * To set the Background color of the element.
179 * @param color the Color
181 public void setListBackground(Color color) {
182 getList().setBackground(color);
186 * To set the Font of the element.
187 * @param font the Font
189 public void setFont(Font font) {
190 getList().setFont(font);
194 * To set the Foreground color of the element.
195 * @param color the Color
197 public void setForeground(Color color) {
198 getList().setForeground(color);
202 * Draws a swing Scilab tab
203 * @see org.scilab.modules.gui.uielement.UIElement#draw()
206 this.setVisible(true);
211 * Gets the dimensions (width and height) of a swing Scilab tab
212 * @return the dimensions of the tab
213 * @see org.scilab.modules.gui.uielement.UIElement#getDims()
215 public Size getDims() {
216 return new Size(getWidth(), getHeight());
220 * Gets the position (X-coordinate and Y-coordinate) of a swing Scilab tab
221 * @return the position of the tab
222 * @see org.scilab.modules.gui.uielement.UIElement#getPosition()
224 public Position getPosition() {
225 return PositionConverter.javaToScilab(getLocation(), getSize(), getParent());
229 * Sets the dimensions (width and height) of a swing Scilab tab
230 * @param newSize the dimensions we want to set to the tab
231 * @see org.scilab.modules.gui.uielement.UIElement#setDims(org.scilab.modules.gui.utils.Size)
233 public void setDims(Size newSize) {
234 setSize(newSize.getWidth(), newSize.getHeight());
238 * Sets the position (X-coordinate and Y-coordinate) of a swing Scilab tab
239 * @param newPosition the position we want to set to the tab
240 * @see org.scilab.modules.gui.uielement.UIElement#setPosition(org.scilab.modules.gui.utils.Position)
242 public void setPosition(Position newPosition) {
243 Position javaPosition = PositionConverter.scilabToJava(newPosition, getDims(), getParent());
244 setLocation(javaPosition.getX(), javaPosition.getY());
248 * Sets the visibility status of an UIElement
249 * @param newVisibleState the visibility status we want to set for the
250 * UIElement (true if the UIElement is visible, false if not)
252 public void setVisible(boolean newVisibleState) {
253 super.setVisible(newVisibleState);
254 list.setVisible(newVisibleState);
258 * Sets the enable status of an UIElement
259 * @param newEnableState the enable status we want to set for the UIElement
260 * (true if the UIElement is enabled, false if not)
262 public void setEnabled(boolean newEnableState) {
263 if (newEnableState != super.isEnabled()) {
264 super.setEnabled(newEnableState);
265 getList().setEnabled(newEnableState);
266 if (newEnableState) {
267 if (listListener != null) {
268 getList().addListSelectionListener(listListener);
271 if (listListener != null) {
272 getList().removeListSelectionListener(listListener);
279 * Add a callback to the CheckBox
280 * @param cb the callback to set.
282 public void setCallback(CommonCallBack cb) {
288 * @param menuBarToAdd the MenuBar associated to the Tab.
290 public void addMenuBar(MenuBar menuBarToAdd) {
291 /* Unimplemented for ListBoxes */
292 throw new UnsupportedOperationException();
297 * @param toolBarToAdd the ToolBar associated to the Tab.
299 public void addToolBar(ToolBar toolBarToAdd) {
300 /* Unimplemented for ListBoxes */
301 throw new UnsupportedOperationException();
306 * @return MenuBar: the MenuBar associated to the Tab.
308 public MenuBar getMenuBar() {
309 /* Unimplemented for ListBoxes */
310 throw new UnsupportedOperationException();
315 * @return ToolBar: the ToolBar associated to the Tab.
317 public ToolBar getToolBar() {
318 /* Unimplemented for ListBoxes */
319 throw new UnsupportedOperationException();
323 * Get the first item text
325 * @see org.scilab.modules.gui.widget.Widget#getText()
327 public String getText() {
328 /* Unimplemented for ListBoxes */
329 throw new UnsupportedOperationException();
333 * Get the text of all the list items
335 * @see org.scilab.modules.gui.listbox.ListBox#getAllItemsText()
337 public String[] getAllItemsText() {
338 String[] retValue = new String[getList().getModel().getSize()];
339 for (int i = 0; i < getList().getModel().getSize(); i++) {
340 retValue[i] = getList().getModel().getElementAt(i).toString();
346 * Get the number of items in the list
347 * @return the number of items
348 * @see org.scilab.modules.gui.listbox.ListBox#getNumberOfItems()
350 public int getNumberOfItems() {
351 return getList().getModel().getSize();
355 * Set the text of the list items
356 * @param text the text of the items
357 * @see org.scilab.modules.gui.widget.Widget#setText(java.lang.String)
359 public void setText(String text) {
360 DefaultListModel model = new DefaultListModel();
361 model.addElement(text);
362 getList().setModel(model);
367 * Set the text of the list items
368 * @param text the text of the items
369 * @see org.scilab.modules.gui.widget.Widget#setText(java.lang.String)
371 public void setText(String[] text) {
372 DefaultListModel model = new DefaultListModel();
374 // check numbers of columns
375 GraphicController controller = GraphicController.getController();
376 Integer nbCol = (Integer) controller.getProperty(getId(), __GO_UI_STRING_COLNB__);
378 /* Remove the listener to avoid the callback to be executed */
379 getList().removeListSelectionListener(listListener);
381 boolean tryColorBox = true;
382 boolean tryColor = true;
383 boolean tryIcon = true;
384 int nbRow = text.length / nbCol;
386 for (int i = 0; i < nbRow; i++) {
389 Color background = null;
390 Color foreground = null;
393 // - 1st icon or colorBox
399 // - 1st icon or colorBox
408 // - 1st icon or colorBox
414 if (tryColorBox) { //color
417 if (text[i].startsWith("#") == false) {
418 throw new NumberFormatException();
421 Color color = Color.decode(text[i]);
422 icon = ColorBox.createColorBox(16, 16, color);
423 } catch (NumberFormatException e) {
426 //restart loop with icon
434 icon = FindIconHelper.loadIcon(text[i]);
435 } catch (IOException e) {
438 //restart loop with text only
447 if (tryColorBox || tryIcon) {
451 str = text[(nbRow * colIndex) + i];
452 if (nbCol > (1 + colIndex)) {
453 if (text[nbRow * (1 + colIndex) + i].startsWith("#") == false) {
454 throw new NumberFormatException();
457 background = Color.decode(text[nbRow * (1 + colIndex) + i]);
458 if (nbCol > (2 + colIndex)) {
459 if (text[nbRow * (2 + colIndex) + i].startsWith("#") == false) {
460 throw new NumberFormatException();
462 foreground = Color.decode(text[nbRow * (2 + colIndex) + i]);
466 //add item in list box
467 model.addElement(new SwingScilabListItem(str, icon, background, foreground));
468 } catch (NumberFormatException e) {
471 //restart loop with text only
476 for (int j = 0; j < nbCol; j++) {
477 model.addElement(new SwingScilabListItem(text[nbRow * j + i], icon, background, foreground));
482 //reset selected index
483 getList().setSelectedIndex(-1);
484 getList().setModel(model);
486 //take care to add listener BEFORE set Property to avoid multiple remove and multiple add
487 getList().addListSelectionListener(listListener);
491 * Set the horizontal alignment for the ListBox text
492 * @param alignment the value for the alignment (See ScilabAlignment.java)
494 public void setHorizontalAlignment(String alignment) {
495 // Nothing to do here
499 * Set the vertical alignment for the ListBox text
500 * @param alignment the value for the alignment (See ScilabAlignment.java)
502 public void setVerticalAlignment(String alignment) {
503 // Nothing to do here
507 * Set if more than one item can be selected in a ListBox
508 * @param status true if multiple selection is enabled
510 public void setMultipleSelectionEnabled(boolean status) {
512 getList().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
514 getList().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
519 * Set the selected indices of the ListBox
520 * @param indices the indices of the items to be selected
522 public void setSelectedIndices(int[] indices) {
523 // Scilab indices in Value begin at 1 and Java indices begin at 0
524 int[] javaIndices = indices.clone();
525 for (int i = 0; i < javaIndices.length; i++) {
526 javaIndices[i] = javaIndices[i] - 1;
529 /* Remove the listener to avoid the callback to be executed */
530 if (listListener != null) {
531 getList().removeListSelectionListener(listListener);
534 getList().setSelectedIndices(javaIndices);
536 /* Put back the listener */
537 if (listListener != null) {
538 getList().addListSelectionListener(listListener);
543 * Get the selected indices of the ListBox
544 * @return the indices of the items selected
546 public int[] getSelectedIndices() {
547 // Scilab indices in Value begin at 1 and Java indices begin at 0
548 int[] javaIndices = getList().getSelectedIndices().clone();
549 int[] scilabIndices = javaIndices.clone();
550 for (int i = 0; i < getList().getSelectedIndices().length; i++) {
551 scilabIndices[i] = scilabIndices[i] + 1;
553 return scilabIndices;
557 * Get the number of items selected in the ListBox
558 * @return the number of items selected
560 public int getSelectionSize() {
561 return getList().getSelectedIndices().length;
565 * Get or create the list component
568 private JList getList() {
571 list.setLayoutOrientation(JList.VERTICAL);
572 list.setModel(new DefaultListModel());
578 * Set the Relief of the ListBox
579 * @param reliefType the type of the relief to set (See ScilabRelief.java)
581 public void setRelief(String reliefType) {
582 if (defaultBorder == null) {
583 defaultBorder = getBorder();
585 setBorder(ScilabRelief.getBorderFromRelief(reliefType, defaultBorder));
589 * Destroy the ListBox
591 public void destroy() {
592 ScilabSwingUtilities.removeFromParent(this);
597 * @param infoBarToAdd the InfoBar associated to the ListBox.
599 public void addInfoBar(TextBox infoBarToAdd) {
600 /* Unimplemented for ListBoxes */
601 throw new UnsupportedOperationException();
606 * @return the InfoBar associated to the ListBox.
608 public TextBox getInfoBar() {
609 /* Unimplemented for ListBoxes */
610 throw new UnsupportedOperationException();
614 * Adjusts the view so that the element given by index is displayed at the
615 * top of the ListBox.
616 * @param index the index of the element to be displayed at the top of the
619 public void setListBoxTop(int index) {
620 getVerticalScrollBar().removeAdjustmentListener(adjustmentListener);
621 if (index > 0 & index != getListBoxTop()) {
622 getViewport().setViewPosition(getList().getUI().indexToLocation(getList(), index - 1));
625 getVerticalScrollBar().addAdjustmentListener(adjustmentListener);
629 * Gets the index of the element displayed at the top of the ListBox
630 * @return the index of the element displayed at the top of the ListBox
632 public int getListBoxTop() {
633 return getList().getUI().locationToIndex(getList(), getViewport().getViewPosition()) + 1;
640 public void setId(Integer id) {
648 public Integer getId() {
653 * Generic update method
654 * @param property property name
655 * @param value property value
657 public void update(int property, Object value) {
658 GraphicController controller = GraphicController.getController();
660 case __GO_UI_VALUE__: {
661 Double[] indexes = (Double[]) value;
662 int[] index = new int[indexes.length];
663 for (int i = 0; i < indexes.length; i++) {
664 index[i] = indexes[i].intValue();
666 setSelectedIndices(index);
669 case __GO_UI_BACKGROUNDCOLOR__: {
670 Double[] allColors = ((Double[]) value);
671 if (allColors[0] != -1) {
672 setListBackground(new Color((int) (allColors[0] * COLORS_COEFF), (int) (allColors[1] * COLORS_COEFF), (int) (allColors[2] * COLORS_COEFF)));
678 case __GO_UI_STRING__: {
679 // Listboxes manage string vectors
680 setText((String[]) value);
683 case __GO_UI_MAX__: {
684 Double maxValue = ((Double) value);
685 // Enable/Disable multiple selection
686 double minValue = (Double) controller.getProperty(uid, __GO_UI_MIN__);
687 setMultipleSelectionEnabled(maxValue - minValue > 1);
690 case __GO_UI_MIN__: {
691 Double minValue = ((Double) value);
692 // Enable/Disable multiple selection
693 Double maxValue = (Double) controller.getProperty(uid, __GO_UI_MAX__);
694 setMultipleSelectionEnabled(maxValue - minValue > 1);
697 case __GO_UI_LISTBOXTOP__: {
698 Integer[] listboxtopValue = ((Integer[]) value);
699 if (listboxtopValue.length > 0) {
700 setListBoxTop(listboxtopValue[0]);
705 SwingViewWidget.update(this, property, value);
711 public void resetBackground() {
712 Color color = (Color) UIManager.getLookAndFeelDefaults().get("List.background");
714 getList().setBackground(color);
718 public void resetForeground() {
719 Color color = (Color) UIManager.getLookAndFeelDefaults().get("List.foreground");
721 getList().setForeground(color);