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 * Copyright (C) 2012 - 2016 - Scilab Enterprises
9 * This file is hereby licensed under the terms of the GNU GPL v2.0,
10 * pursuant to article 5.3.4 of the CeCILL v.2.1.
11 * This file was originally licensed under the terms of the CeCILL v2.1,
12 * and continues to be available under such terms.
13 * For more information, see the COPYING file which you should have received
14 * along with this program.
18 package org.scilab.modules.gui.bridge.listbox;
20 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_BACKGROUNDCOLOR__;
21 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_LISTBOXTOP__;
22 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MAX__;
23 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MIN__;
24 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING_COLNB__;
25 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
26 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE__;
28 import java.awt.Color;
29 import java.awt.Component;
31 import java.awt.event.AdjustmentEvent;
32 import java.awt.event.AdjustmentListener;
33 import java.io.IOException;
34 import java.awt.event.MouseAdapter;
35 import java.awt.event.MouseEvent;
37 import javax.swing.DefaultListModel;
38 import javax.swing.Icon;
39 import javax.swing.JLabel;
40 import javax.swing.JList;
41 import javax.swing.JScrollPane;
42 import javax.swing.ListCellRenderer;
43 import javax.swing.ListSelectionModel;
44 import javax.swing.UIManager;
45 import javax.swing.border.Border;
46 import javax.swing.event.ListSelectionEvent;
47 import javax.swing.event.ListSelectionListener;
49 import org.scilab.modules.commons.gui.FindIconHelper;
50 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
51 import org.scilab.modules.gui.SwingViewObject;
52 import org.scilab.modules.gui.SwingViewWidget;
53 import org.scilab.modules.gui.events.callback.CommonCallBack;
54 import org.scilab.modules.gui.menubar.MenuBar;
55 import org.scilab.modules.gui.textbox.TextBox;
56 import org.scilab.modules.gui.toolbar.ToolBar;
57 import org.scilab.modules.gui.utils.ColorBox;
58 import org.scilab.modules.gui.utils.Position;
59 import org.scilab.modules.gui.utils.PositionConverter;
60 import org.scilab.modules.gui.utils.ScilabRelief;
61 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
62 import org.scilab.modules.gui.utils.Size;
63 import org.scilab.modules.gui.utils.SwingScilabListItem;
64 import org.scilab.modules.gui.widget.Widget;
67 * Swing implementation for Scilab ListBox in GUIs
68 * @author Vincent COUVERT
69 * @author Marouane BEN JELLOUL
71 public class SwingScilabListBox extends JScrollPane implements SwingViewObject, Widget {
73 private static final long serialVersionUID = 3507396207331058895L;
75 private static final int COLORS_COEFF = 255;
79 private Border defaultBorder = null;
81 private CommonCallBack callback;
83 private ListSelectionListener listListener;
85 private AdjustmentListener adjustmentListener;
92 private ListCellRenderer defaultRenderer = null;
93 private ListCellRenderer listRenderer = null;
98 public SwingScilabListBox() {
100 getViewport().add(getList());
101 defaultRenderer = getList().getCellRenderer();
103 listRenderer = new ListCellRenderer() {
104 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
105 JLabel label = (JLabel) defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
107 if (value instanceof SwingScilabListItem) {
108 SwingScilabListItem item = (SwingScilabListItem) value;
109 label.setText(item.toString());
110 label.setIcon(item.getIcon());
112 if (isSelected == false && item.getBackground() != null) {
113 label.setBackground(item.getBackground());
116 if (isSelected == false && item.getForeground() != null) {
117 label.setForeground(item.getForeground());
127 getList().setCellRenderer(listRenderer);
129 listListener = new ListSelectionListener() {
130 public void valueChanged(ListSelectionEvent e) {
133 if (e.getValueIsAdjusting()) {
137 // Scilab indices in Value begin at 1 and Java indices begin at 0
138 int[] javaIndices = getList().getSelectedIndices().clone();
139 Double[] scilabIndices = new Double[javaIndices.length];
140 for (int i = 0; i < getList().getSelectedIndices().length; i++) {
141 scilabIndices[i] = (double) javaIndices[i] + 1;
144 GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, scilabIndices);
145 if (callback != null && getList().getSelectionMode() == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) {
146 callback.actionPerformed(null);
150 getList().addListSelectionListener(listListener);
152 getList().addMouseListener(new MouseAdapter() {
153 public void mouseReleased(MouseEvent evt) {
154 if (callback != null && getList().getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) {
155 callback.actionPerformed(null);
160 adjustmentListener = new AdjustmentListener() {
161 public void adjustmentValueChanged(AdjustmentEvent arg0) {
162 int listboxtopValue = getList().getUI().locationToIndex(getList(), getViewport().getViewPosition()) + 1;
163 GraphicController.getController().setProperty(uid, __GO_UI_LISTBOXTOP__, new Integer[] { listboxtopValue });
166 getVerticalScrollBar().addAdjustmentListener(adjustmentListener);
170 * To get the Background color of the element.
171 * @return color the Color
173 public Color getBackground() {
174 return getList().getBackground();
178 * To get the Font of the element.
179 * @return font the Font
181 public Font getFont() {
182 return getList().getFont();
186 * To get the Foreground color of the element.
187 * @return color the Color
189 public Color getForeground() {
190 return getList().getForeground();
194 * To set the Background color of the element.
195 * @param color the Color
197 public void setListBackground(Color color) {
198 getList().setBackground(color);
202 * To set the Font of the element.
203 * @param font the Font
205 public void setFont(Font font) {
206 getList().setFont(font);
210 * To set the Foreground color of the element.
211 * @param color the Color
213 public void setForeground(Color color) {
214 getList().setForeground(color);
218 * Draws a swing Scilab tab
219 * @see org.scilab.modules.gui.uielement.UIElement#draw()
222 this.setVisible(true);
227 * Gets the dimensions (width and height) of a swing Scilab tab
228 * @return the dimensions of the tab
229 * @see org.scilab.modules.gui.uielement.UIElement#getDims()
231 public Size getDims() {
232 return new Size(getWidth(), getHeight());
236 * Gets the position (X-coordinate and Y-coordinate) of a swing Scilab tab
237 * @return the position of the tab
238 * @see org.scilab.modules.gui.uielement.UIElement#getPosition()
240 public Position getPosition() {
241 return PositionConverter.javaToScilab(getLocation(), getSize(), getParent());
245 * Sets the dimensions (width and height) of a swing Scilab tab
246 * @param newSize the dimensions we want to set to the tab
247 * @see org.scilab.modules.gui.uielement.UIElement#setDims(org.scilab.modules.gui.utils.Size)
249 public void setDims(Size newSize) {
250 setSize(newSize.getWidth(), newSize.getHeight());
254 * Sets the position (X-coordinate and Y-coordinate) of a swing Scilab tab
255 * @param newPosition the position we want to set to the tab
256 * @see org.scilab.modules.gui.uielement.UIElement#setPosition(org.scilab.modules.gui.utils.Position)
258 public void setPosition(Position newPosition) {
259 Position javaPosition = PositionConverter.scilabToJava(newPosition, getDims(), getParent());
260 setLocation(javaPosition.getX(), javaPosition.getY());
264 * Sets the visibility status of an UIElement
265 * @param newVisibleState the visibility status we want to set for the
266 * UIElement (true if the UIElement is visible, false if not)
268 public void setVisible(boolean newVisibleState) {
269 super.setVisible(newVisibleState);
270 list.setVisible(newVisibleState);
274 * Sets the enable status of an UIElement
275 * @param newEnableState the enable status we want to set for the UIElement
276 * (true if the UIElement is enabled, false if not)
278 public void setEnabled(boolean newEnableState) {
279 if (newEnableState == isEnabled()) {
283 super.setEnabled(newEnableState);
284 getList().setEnabled(newEnableState);
285 if (newEnableState) {
286 if (listListener != null) {
287 getList().addListSelectionListener(listListener);
290 if (listListener != null) {
291 getList().removeListSelectionListener(listListener);
297 * Add a callback to the CheckBox
298 * @param cb the callback to set.
300 public void setCallback(CommonCallBack cb) {
306 * @param menuBarToAdd the MenuBar associated to the Tab.
308 public void addMenuBar(MenuBar menuBarToAdd) {
309 /* Unimplemented for ListBoxes */
310 throw new UnsupportedOperationException();
315 * @param toolBarToAdd the ToolBar associated to the Tab.
317 public void addToolBar(ToolBar toolBarToAdd) {
318 /* Unimplemented for ListBoxes */
319 throw new UnsupportedOperationException();
324 * @return MenuBar: the MenuBar associated to the Tab.
326 public MenuBar getMenuBar() {
327 /* Unimplemented for ListBoxes */
328 throw new UnsupportedOperationException();
333 * @return ToolBar: the ToolBar associated to the Tab.
335 public ToolBar getToolBar() {
336 /* Unimplemented for ListBoxes */
337 throw new UnsupportedOperationException();
341 * Get the first item text
343 * @see org.scilab.modules.gui.widget.Widget#getText()
345 public String getText() {
346 /* Unimplemented for ListBoxes */
347 throw new UnsupportedOperationException();
351 * Get the text of all the list items
353 * @see org.scilab.modules.gui.listbox.ListBox#getAllItemsText()
355 public String[] getAllItemsText() {
356 String[] retValue = new String[getList().getModel().getSize()];
357 for (int i = 0; i < getList().getModel().getSize(); i++) {
358 retValue[i] = getList().getModel().getElementAt(i).toString();
364 * Get the number of items in the list
365 * @return the number of items
366 * @see org.scilab.modules.gui.listbox.ListBox#getNumberOfItems()
368 public int getNumberOfItems() {
369 return getList().getModel().getSize();
373 * Set the text of the list items
374 * @param text the text of the items
375 * @see org.scilab.modules.gui.widget.Widget#setText(java.lang.String)
377 public void setText(String text) {
378 DefaultListModel model = new DefaultListModel();
379 model.addElement(text);
380 getList().setModel(model);
385 * Set the text of the list items
386 * @param text the text of the items
387 * @see org.scilab.modules.gui.widget.Widget#setText(java.lang.String)
389 public void setText(String[] text) {
390 DefaultListModel model = new DefaultListModel();
392 // check numbers of columns
393 GraphicController controller = GraphicController.getController();
394 Integer nbCol = (Integer) controller.getProperty(getId(), __GO_UI_STRING_COLNB__);
396 /* Remove the listener to avoid the callback to be executed */
397 getList().removeListSelectionListener(listListener);
399 boolean tryColorBox = true;
400 boolean tryColor = true;
401 boolean tryIcon = true;
402 int nbRow = text.length / nbCol;
404 for (int i = 0; i < nbRow; i++) {
407 Color background = null;
408 Color foreground = null;
411 // - 1st icon or colorBox
417 // - 1st icon or colorBox
426 // - 1st icon or colorBox
432 if (tryColorBox) { //color
435 if (text[i].startsWith("#") == false) {
436 throw new NumberFormatException();
439 Color color = Color.decode(text[i]);
440 icon = ColorBox.createColorBox(16, 16, color);
441 } catch (NumberFormatException e) {
444 //restart loop with icon
452 icon = FindIconHelper.loadIcon(text[i]);
453 } catch (IOException e) {
456 //restart loop with text only
465 if (tryColorBox || tryIcon) {
469 str = text[(nbRow * colIndex) + i];
470 if (nbCol > (1 + colIndex)) {
471 if (text[nbRow * (1 + colIndex) + i].startsWith("#") == false) {
472 throw new NumberFormatException();
475 background = Color.decode(text[nbRow * (1 + colIndex) + i]);
476 if (nbCol > (2 + colIndex)) {
477 if (text[nbRow * (2 + colIndex) + i].startsWith("#") == false) {
478 throw new NumberFormatException();
480 foreground = Color.decode(text[nbRow * (2 + colIndex) + i]);
484 //add item in list box
485 model.addElement(new SwingScilabListItem(str, icon, background, foreground));
486 } catch (NumberFormatException e) {
489 //restart loop with text only
494 for (int j = 0; j < nbCol; j++) {
495 model.addElement(new SwingScilabListItem(text[nbRow * j + i], icon, background, foreground));
500 //reset selected index
501 getList().setSelectedIndex(-1);
502 getList().setModel(model);
504 //take care to add listener BEFORE set Property to avoid multiple remove and multiple add
505 getList().addListSelectionListener(listListener);
508 public void setEmptyText() {
509 setText(new String[] {});
513 * Set the horizontal alignment for the ListBox text
514 * @param alignment the value for the alignment (See ScilabAlignment.java)
516 public void setHorizontalAlignment(String alignment) {
517 // Nothing to do here
521 * Set the vertical alignment for the ListBox text
522 * @param alignment the value for the alignment (See ScilabAlignment.java)
524 public void setVerticalAlignment(String alignment) {
525 // Nothing to do here
529 * Set if more than one item can be selected in a ListBox
530 * @param status true if multiple selection is enabled
532 public void setMultipleSelectionEnabled(boolean status) {
534 getList().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
536 getList().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
541 * Set the selected indices of the ListBox
542 * @param indices the indices of the items to be selected
544 public void setSelectedIndices(int[] indices) {
545 // Scilab indices in Value begin at 1 and Java indices begin at 0
546 int[] javaIndices = indices.clone();
547 for (int i = 0; i < javaIndices.length; i++) {
548 javaIndices[i] = javaIndices[i] - 1;
551 /* Remove the listener to avoid the callback to be executed */
552 if (listListener != null) {
553 getList().removeListSelectionListener(listListener);
556 getList().setSelectedIndices(javaIndices);
558 /* Put back the listener */
559 if (listListener != null) {
560 getList().addListSelectionListener(listListener);
565 * Get the selected indices of the ListBox
566 * @return the indices of the items selected
568 public int[] getSelectedIndices() {
569 // Scilab indices in Value begin at 1 and Java indices begin at 0
570 int[] javaIndices = getList().getSelectedIndices().clone();
571 int[] scilabIndices = javaIndices.clone();
572 for (int i = 0; i < getList().getSelectedIndices().length; i++) {
573 scilabIndices[i] = scilabIndices[i] + 1;
575 return scilabIndices;
579 * Get the number of items selected in the ListBox
580 * @return the number of items selected
582 public int getSelectionSize() {
583 return getList().getSelectedIndices().length;
587 * Get or create the list component
590 private JList getList() {
593 list.setLayoutOrientation(JList.VERTICAL);
594 list.setModel(new DefaultListModel());
600 * Set the Relief of the ListBox
601 * @param reliefType the type of the relief to set (See ScilabRelief.java)
603 public void setRelief(String reliefType) {
604 if (defaultBorder == null) {
605 defaultBorder = getBorder();
607 setBorder(ScilabRelief.getBorderFromRelief(reliefType, defaultBorder));
611 * Destroy the ListBox
613 public void destroy() {
614 ScilabSwingUtilities.removeFromParent(this);
619 * @param infoBarToAdd the InfoBar associated to the ListBox.
621 public void addInfoBar(TextBox infoBarToAdd) {
622 /* Unimplemented for ListBoxes */
623 throw new UnsupportedOperationException();
628 * @return the InfoBar associated to the ListBox.
630 public TextBox getInfoBar() {
631 /* Unimplemented for ListBoxes */
632 throw new UnsupportedOperationException();
636 * Adjusts the view so that the element given by index is displayed at the
637 * top of the ListBox.
638 * @param index the index of the element to be displayed at the top of the
641 public void setListBoxTop(int index) {
642 getVerticalScrollBar().removeAdjustmentListener(adjustmentListener);
643 if (index > 0 && index != getListBoxTop() && getList().getModel().getSize() != 0) {
644 getViewport().setViewPosition(getList().getUI().indexToLocation(getList(), index - 1));
647 getVerticalScrollBar().addAdjustmentListener(adjustmentListener);
651 * Gets the index of the element displayed at the top of the ListBox
652 * @return the index of the element displayed at the top of the ListBox
654 public int getListBoxTop() {
655 return getList().getUI().locationToIndex(getList(), getViewport().getViewPosition()) + 1;
662 public void setId(Integer id) {
670 public Integer getId() {
675 * Generic update method
676 * @param property property name
677 * @param value property value
679 public void update(int property, Object value) {
680 GraphicController controller = GraphicController.getController();
682 case __GO_UI_VALUE__: {
683 Double[] indexes = (Double[]) value;
684 int[] index = new int[indexes.length];
685 for (int i = 0; i < indexes.length; i++) {
686 index[i] = indexes[i].intValue();
688 setSelectedIndices(index);
691 case __GO_UI_BACKGROUNDCOLOR__: {
692 Double[] allColors = ((Double[]) value);
693 if (allColors[0] != -1) {
694 setListBackground(new Color((int) (allColors[0] * COLORS_COEFF), (int) (allColors[1] * COLORS_COEFF), (int) (allColors[2] * COLORS_COEFF)));
700 case __GO_UI_STRING__: {
701 // Listboxes manage string vectors
702 setText((String[]) value);
705 case __GO_UI_MAX__: {
706 Double maxValue = ((Double) value);
707 // Enable/Disable multiple selection
708 double minValue = (Double) controller.getProperty(uid, __GO_UI_MIN__);
709 setMultipleSelectionEnabled(maxValue - minValue > 1);
712 case __GO_UI_MIN__: {
713 Double minValue = ((Double) value);
714 // Enable/Disable multiple selection
715 Double maxValue = (Double) controller.getProperty(uid, __GO_UI_MAX__);
716 setMultipleSelectionEnabled(maxValue - minValue > 1);
719 case __GO_UI_LISTBOXTOP__: {
720 Integer[] listboxtopValue = ((Integer[]) value);
721 if (listboxtopValue.length > 0) {
722 setListBoxTop(listboxtopValue[0]);
727 SwingViewWidget.update(this, property, value);
733 public void resetBackground() {
734 Color color = (Color) UIManager.getLookAndFeelDefaults().get("List.background");
736 getList().setBackground(color);
740 public void resetForeground() {
741 Color color = (Color) UIManager.getLookAndFeelDefaults().get("List.foreground");
743 getList().setForeground(color);