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-en.txt
15 package org.scilab.modules.gui.bridge.listbox;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_STRING__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE__;
20 import java.awt.Color;
22 import java.awt.event.MouseEvent;
23 import java.awt.event.MouseListener;
24 import java.util.StringTokenizer;
26 import javax.swing.DefaultListModel;
27 import javax.swing.JList;
28 import javax.swing.JScrollPane;
29 import javax.swing.ListSelectionModel;
31 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
32 import org.scilab.modules.gui.SwingViewWidget;
33 import org.scilab.modules.gui.SwingViewObject;
34 import org.scilab.modules.gui.events.callback.CommonCallBack;
35 import org.scilab.modules.gui.listbox.SimpleListBox;
36 import org.scilab.modules.gui.menubar.MenuBar;
37 import org.scilab.modules.gui.textbox.TextBox;
38 import org.scilab.modules.gui.toolbar.ToolBar;
39 import org.scilab.modules.gui.utils.Position;
40 import org.scilab.modules.gui.utils.PositionConverter;
41 import org.scilab.modules.gui.utils.ScilabRelief;
42 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
43 import org.scilab.modules.gui.utils.Size;
46 * Swing implementation for Scilab ListBox in GUIs
47 * @author Vincent COUVERT
48 * @author Marouane BEN JELLOUL
50 public class SwingScilabListBox extends JScrollPane implements SwingViewObject, SimpleListBox {
52 private static final long serialVersionUID = 3507396207331058895L;
54 private static final String STRING_SEPARATOR = "|";
58 private CommonCallBack callback;
60 private MouseListener mouseListener;
70 public SwingScilabListBox() {
72 getViewport().add(getList());
74 mouseListener = new MouseListener() {
75 public void mouseClicked(MouseEvent e) {
76 // Scilab indices in Value begin at 1 and Java indices begin at 0
77 int[] javaIndices = getList().getSelectedIndices().clone();
78 Double[] scilabIndices = new Double[javaIndices.length];
79 for (int i = 0; i < getList().getSelectedIndices().length; i++) {
80 scilabIndices[i] = (double) javaIndices[i] + 1;
82 GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, scilabIndices);
83 if (e.getButton() == MouseEvent.BUTTON1 && callback != null) {
84 callback.actionPerformed(null);
87 public void mouseEntered(MouseEvent arg0) { }
88 public void mouseExited(MouseEvent arg0) { }
89 public void mousePressed(MouseEvent arg0) { }
90 public void mouseReleased(MouseEvent arg0) { }
92 getList().addMouseListener(mouseListener);
96 * To get the Background color of the element.
97 * @return color the Color
99 public Color getBackground() {
100 return getList().getBackground();
104 * To get the Font of the element.
105 * @return font the Font
107 public Font getFont() {
108 return getList().getFont();
112 * To get the Foreground color of the element.
113 * @return color the Color
115 public Color getForeground() {
116 return getList().getForeground();
120 * To set the Background color of the element.
121 * @param color the Color
123 public void setBackground(Color color) {
124 getList().setBackground(color);
128 * To set the Font of the element.
129 * @param font the Font
131 public void setFont(Font font) {
132 getList().setFont(font);
136 * To set the Foreground color of the element.
137 * @param color the Color
139 public void setForeground(Color color) {
140 getList().setForeground(color);
144 * Draws a swing Scilab tab
145 * @see org.scilab.modules.gui.uielement.UIElement#draw()
148 this.setVisible(true);
153 * Gets the dimensions (width and height) of a swing Scilab tab
154 * @return the dimensions of the tab
155 * @see org.scilab.modules.gui.uielement.UIElement#getDims()
157 public Size getDims() {
158 return new Size(getWidth(), getHeight());
162 * Gets the position (X-coordinate and Y-coordinate) of a swing Scilab tab
163 * @return the position of the tab
164 * @see org.scilab.modules.gui.uielement.UIElement#getPosition()
166 public Position getPosition() {
167 return PositionConverter.javaToScilab(getLocation(), getSize(), getParent());
171 * Sets the dimensions (width and height) of a swing Scilab tab
172 * @param newSize the dimensions we want to set to the tab
173 * @see org.scilab.modules.gui.uielement.UIElement#setDims(org.scilab.modules.gui.utils.Size)
175 public void setDims(Size newSize) {
176 setSize(newSize.getWidth(), newSize.getHeight());
180 * Sets the position (X-coordinate and Y-coordinate) of a swing Scilab tab
181 * @param newPosition the position we want to set to the tab
182 * @see org.scilab.modules.gui.uielement.UIElement#setPosition(org.scilab.modules.gui.utils.Position)
184 public void setPosition(Position newPosition) {
185 Position javaPosition = PositionConverter.scilabToJava(newPosition, getDims(), getParent());
186 setLocation(javaPosition.getX(), javaPosition.getY());
190 * Sets the visibility status of an UIElement
191 * @param newVisibleState the visibility status we want to set for the UIElement
192 * (true if the UIElement is visible, false if not)
194 public void setVisible(boolean newVisibleState) {
195 super.setVisible(newVisibleState);
196 list.setVisible(newVisibleState);
200 * Sets the enable status of an UIElement
201 * @param newEnableState the enable status we want to set for the UIElement
202 * (true if the UIElement is enabled, false if not)
204 public void setEnabled(boolean newEnableState) {
205 super.setEnabled(newEnableState);
206 getList().setEnabled(newEnableState);
207 if (newEnableState) {
208 if (mouseListener != null) {
209 getList().addMouseListener(mouseListener);
212 if (mouseListener != null) {
213 getList().removeMouseListener(mouseListener);
219 * Add a callback to the CheckBox
220 * @param cb the callback to set.
222 public void setCallback(CommonCallBack cb) {
228 * @param menuBarToAdd the MenuBar associated to the Tab.
230 public void addMenuBar(MenuBar menuBarToAdd) {
231 /* Unimplemented for ListBoxes */
232 throw new UnsupportedOperationException();
237 * @param toolBarToAdd the ToolBar associated to the Tab.
239 public void addToolBar(ToolBar toolBarToAdd) {
240 /* Unimplemented for ListBoxes */
241 throw new UnsupportedOperationException();
246 * @return MenuBar: the MenuBar associated to the Tab.
248 public MenuBar getMenuBar() {
249 /* Unimplemented for ListBoxes */
250 throw new UnsupportedOperationException();
255 * @return ToolBar: the ToolBar associated to the Tab.
257 public ToolBar getToolBar() {
258 /* Unimplemented for ListBoxes */
259 throw new UnsupportedOperationException();
263 * Get the first item text
265 * @see org.scilab.modules.gui.widget.Widget#getText()
267 public String getText() {
268 /* Unimplemented for ListBoxes */
269 throw new UnsupportedOperationException();
273 * Get the text of all the list items
275 * @see org.scilab.modules.gui.listbox.ListBox#getAllItemsText()
277 public String[] getAllItemsText() {
278 String[] retValue = new String[getList().getModel().getSize()];
279 for (int i = 0; i < getList().getModel().getSize(); i++) {
280 retValue[i] = (String) getList().getModel().getElementAt(i);
286 * Get the number of items in the list
287 * @return the number of items
288 * @see org.scilab.modules.gui.listbox.ListBox#getNumberOfItems()
290 public int getNumberOfItems() {
291 return getList().getModel().getSize();
295 * Set the text of the list items
296 * @param text the text of the items
297 * @see org.scilab.modules.gui.widget.Widget#setText(java.lang.String)
299 public void setText(String text) {
300 DefaultListModel model = new DefaultListModel();
301 model.addElement(text);
302 getList().setModel(model);
307 * Set the text of the list items
308 * @param text the text of the items
309 * @see org.scilab.modules.gui.widget.Widget#setText(java.lang.String)
311 public void setText(String[] text) {
312 /* Do we need to update the strings */
313 /* Test performed to avoid loops when the model is updated from here */
314 boolean updateNeeded = false;
315 String[] previousText = getAllItemsText();
316 if (previousText.length != text.length) {
319 for (int k = 0; k < text.length; k++) {
320 if (text[k].compareTo(previousText[k]) != 0) {
330 final String[] textF = text;
331 DefaultListModel model = new DefaultListModel();
332 if (textF.length == 1 & text[0].contains(STRING_SEPARATOR)) {
333 StringTokenizer strTok = new StringTokenizer(textF[0], STRING_SEPARATOR);
334 while (strTok.hasMoreTokens()) {
335 model.addElement(strTok.nextToken());
337 /* Update the model with the parsed string */
338 GraphicController.getController().setProperty(uid, __GO_UI_STRING__, getAllItemsText());
340 for (int i = 0; i < textF.length; i++) {
341 model.addElement(textF[i]);
344 getList().setModel(model);
349 * Set the horizontal alignment for the ListBox text
350 * @param alignment the value for the alignment (See ScilabAlignment.java)
352 public void setHorizontalAlignment(String alignment) {
353 // Nothing to do here
357 * Set the vertical alignment for the ListBox text
358 * @param alignment the value for the alignment (See ScilabAlignment.java)
360 public void setVerticalAlignment(String alignment) {
361 // Nothing to do here
365 * Set if more than one item can be selected in a ListBox
366 * @param status true if multiple selection is enabled
368 public void setMultipleSelectionEnabled(boolean status) {
370 getList().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
372 getList().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
377 * Set the selected indices of the ListBox
378 * @param indices the indices of the items to be selected
380 public void setSelectedIndices(int[] indices) {
381 // Scilab indices in Value begin at 1 and Java indices begin at 0
382 int[] javaIndices = indices.clone();
383 for (int i = 0; i < javaIndices.length; i++) {
384 javaIndices[i] = javaIndices[i] - 1;
387 /* Remove the listener to avoid the callback to be executed */
388 if (mouseListener != null) {
389 getList().removeMouseListener(mouseListener);
392 getList().setSelectedIndices(javaIndices);
394 /* Put back the listener */
395 if (mouseListener != null) {
396 getList().addMouseListener(mouseListener);
401 * Get the selected indices of the ListBox
402 * @return the indices of the items selected
404 public int[] getSelectedIndices() {
405 // Scilab indices in Value begin at 1 and Java indices begin at 0
406 int[] javaIndices = getList().getSelectedIndices().clone();
407 int[] scilabIndices = javaIndices.clone();
408 for (int i = 0; i < getList().getSelectedIndices().length; i++) {
409 scilabIndices[i] = scilabIndices[i] + 1;
411 return scilabIndices;
415 * Get the number of items selected in the ListBox
416 * @return the number of items selected
418 public int getSelectionSize() {
419 return getList().getSelectedIndices().length;
423 * Get or create the list component
426 private JList getList() {
429 list.setLayoutOrientation(JList.VERTICAL);
430 list.setModel(new DefaultListModel());
436 * Set the Relief of the ListBox
437 * @param reliefType the type of the relief to set (See ScilabRelief.java)
439 public void setRelief(String reliefType) {
440 setBorder(ScilabRelief.getBorderFromRelief(reliefType));
444 * Destroy the ListBox
446 public void destroy() {
447 ScilabSwingUtilities.removeFromParent(this);
452 * @param infoBarToAdd the InfoBar associated to the ListBox.
454 public void addInfoBar(TextBox infoBarToAdd) {
455 /* Unimplemented for ListBoxes */
456 throw new UnsupportedOperationException();
461 * @return the InfoBar associated to the ListBox.
463 public TextBox getInfoBar() {
464 /* Unimplemented for ListBoxes */
465 throw new UnsupportedOperationException();
469 * Adjusts the view so that the element given by index is displayed at the top of the ListBox.
470 * @param index the index of the element to be displayed at the top of the ListBox.
472 public void setListBoxTop(int index) {
474 getViewport().setViewPosition(getList().getUI().indexToLocation(getList(), index - 1));
480 * Gets the index of the element displayed at the top of the ListBox
481 * @return the index of the element displayed at the top of the ListBox
483 public int getListBoxTop() {
484 return getList().getUI().locationToIndex(getList(), getViewport().getViewPosition()) + 1;
491 public void setId(String id) {
499 public String getId() {
504 * Generic update method
505 * @param property property name
506 * @param value property value
508 public void update(int property, Object value) {
509 SwingViewWidget.update(this, property, value);