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
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 package org.scilab.modules.gui.bridge.slider;
16 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MAX__;
17 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_MIN__;
18 import static org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties.__GO_UI_VALUE__;
20 import java.awt.event.AdjustmentEvent;
21 import java.awt.event.AdjustmentListener;
23 import javax.swing.JScrollBar;
24 import javax.swing.SwingUtilities;
26 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
27 import org.scilab.modules.gui.SwingViewWidget;
28 import org.scilab.modules.gui.SwingViewObject;
29 import org.scilab.modules.gui.events.callback.CommonCallBack;
30 import org.scilab.modules.gui.menubar.MenuBar;
31 import org.scilab.modules.gui.slider.SimpleSlider;
32 import org.scilab.modules.gui.textbox.TextBox;
33 import org.scilab.modules.gui.toolbar.ToolBar;
34 import org.scilab.modules.gui.utils.Position;
35 import org.scilab.modules.gui.utils.PositionConverter;
36 import org.scilab.modules.gui.utils.ScilabRelief;
37 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
38 import org.scilab.modules.gui.utils.Size;
41 * Swing implementation for Scilab Slider in GUIs
42 * @author Vincent COUVERT
43 * @author Marouane BEN JELLOUL
45 public class SwingScilabSlider extends JScrollBar implements SwingViewObject, SimpleSlider {
47 private static final long serialVersionUID = -4262320156090829309L;
49 private static final int MIN_KNOB_SIZE = 40;
51 private static final int MINIMUM_VALUE = 0;
52 private static final int MAXIMUM_VALUE = 10000;
56 private CommonCallBack callback;
58 private AdjustmentListener adjustmentListener;
63 public SwingScilabSlider() {
65 // needed to have slider working with GLCanvas
67 setMinimum(MINIMUM_VALUE);
68 setMaximum(MAXIMUM_VALUE + getVisibleAmount());
69 adjustmentListener = new AdjustmentListener() {
70 public void adjustmentValueChanged(AdjustmentEvent arg0) {
72 if (callback != null) {
73 callback.actionPerformed(null);
77 addAdjustmentListener(adjustmentListener);
81 * Draws a swing Scilab Slider
82 * @see org.scilab.modules.gui.UIElement#draw()
85 this.setVisible(true);
90 * Gets the dimensions (width and height) of a swing Scilab Slider
91 * @return the dimensions of the Slider
92 * @see org.scilab.modules.gui.uielement.UIElement#getDims()
94 public Size getDims() {
95 return new Size(super.getSize().width, super.getSize().height);
99 * Gets the position (X-coordinate and Y-coordinate) of a swing Scilab Slider
100 * @return the position of the Slider
101 * @see org.scilab.modules.gui.uielement.UIElement#getPosition()
103 public Position getPosition() {
104 return PositionConverter.javaToScilab(getLocation(), getSize(), getParent());
108 * Sets the dimensions (width and height) of a swing Scilab Slider
109 * @param newSize the dimensions to set to the Slider
110 * @see org.scilab.modules.gui.uielement.UIElement#setDims(org.scilab.modules.gui.utils.Size)
112 public void setDims(Size newSize) {
113 super.setSize(newSize.getWidth(), newSize.getHeight());
114 super.doLayout(); // Usefull in case of resize
118 * Sets the position (X-coordinate and Y-coordinate) of a swing Scilab Slider
119 * @param newPosition the position to set to the Slider
120 * @see org.scilab.modules.gui.uielement.UIElement#setPosition(org.scilab.modules.gui.utils.Position)
122 public void setPosition(Position newPosition) {
123 Position javaPosition = PositionConverter.scilabToJava(newPosition, getDims(), getParent());
124 setLocation(javaPosition.getX(), javaPosition.getY());
128 * Add a callback to the Slider
129 * @param cb the callback to set.
131 public void setCallback(CommonCallBack cb) {
132 /* Create a callback */
138 * @param menuBarToAdd the MenuBar associated to the Tab.
140 public void addMenuBar(MenuBar menuBarToAdd) {
141 /* Unimplemented for CheckBoxes */
142 throw new UnsupportedOperationException();
147 * @param toolBarToAdd the ToolBar associated to the Tab.
149 public void addToolBar(ToolBar toolBarToAdd) {
150 /* Unimplemented for CheckBoxes */
151 throw new UnsupportedOperationException();
156 * @return MenuBar: the MenuBar associated to the Tab.
158 public MenuBar getMenuBar() {
159 /* Unimplemented for CheckBoxes */
160 throw new UnsupportedOperationException();
165 * @return ToolBar: the ToolBar associated to the Tab.
167 public ToolBar getToolBar() {
168 /* Unimplemented for CheckBoxes */
169 throw new UnsupportedOperationException();
173 * Get the text of the Slider
175 * @see org.scilab.modules.gui.widget.Widget#getText()
177 public String getText() {
178 return this.getName();
182 * Set the text of the Slider
183 * @param text the text to set to the Slider
184 * @see org.scilab.modules.gui.widget.Widget#setText(java.lang.String)
186 public void setText(String text) {
191 * Set the horizontal alignment for the Slider text
192 * @param alignment the value for the alignment (See ScilabAlignment.java)
194 public void setHorizontalAlignment(String alignment) {
195 // Nothing to do here
199 * Set the vertical alignment for the Slider text
200 * @param alignment the value for the alignment (See ScilabAlignment.java)
202 public void setVerticalAlignment(String alignment) {
203 // Nothing to do here
207 * Set the minimum value of a Slider
208 * @param value the minimum value
210 public void setMinimumValue(double value) {
211 updateModel(); /* Update the model according to the knob position */
215 * Set the maximum value of a Slider
216 * @param value the maximum value
218 public void setMaximumValue(double value) {
219 updateModel(); /* Update the model according to the knob position */
223 * Set the Relief of the Slider
224 * @param reliefType the type of the relief to set (See ScilabRelief.java)
226 public void setRelief(String reliefType) {
227 setBorder(ScilabRelief.getBorderFromRelief(reliefType));
231 * Set the major tick spacing for a Slider
232 * @param space the increment value
234 public void setMajorTickSpacing(double space) {
235 /* Remove the listener to avoid the callback to be executed */
236 removeAdjustmentListener(adjustmentListener);
238 double userMin = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
239 double userMax = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
240 setBlockIncrement((int) (space * (MAXIMUM_VALUE - MINIMUM_VALUE) / (userMax - userMin)));
241 int oldMax = getMaximum() - getVisibleAmount();
242 setVisibleAmount(Math.max((int) ((MAXIMUM_VALUE - MINIMUM_VALUE) / space), MIN_KNOB_SIZE));
243 setMaximum(oldMax + getVisibleAmount());
245 /* Put back the listener */
246 addAdjustmentListener(adjustmentListener);
250 * Set the minor tick spacing for a Slider
251 * @param space the increment value
253 public void setMinorTickSpacing(double space) {
254 /* Remove the listener to avoid the callback to be executed */
255 removeAdjustmentListener(adjustmentListener);
257 double userMin = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
258 double userMax = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
259 setUnitIncrement((int) (space * (MAXIMUM_VALUE - MINIMUM_VALUE) / (userMax - userMin)));
261 /* Put back the listener */
262 addAdjustmentListener(adjustmentListener);
266 * Set the slider orientation to vertical
268 public void setVertical() {
269 setOrientation(JScrollBar.VERTICAL);
273 * Set the slider orientation to horizontal
275 public void setHorizontal() {
276 setOrientation(JScrollBar.HORIZONTAL);
282 public void destroy() {
283 ScilabSwingUtilities.removeFromParent(this);
288 * @param infoBarToAdd the InfoBar associated to the Slider.
290 public void addInfoBar(TextBox infoBarToAdd) {
291 /* Unimplemented for Sliders */
292 throw new UnsupportedOperationException();
297 * @return the InfoBar associated to the Slider.
299 public TextBox getInfoBar() {
300 /* Unimplemented for Sliders */
301 throw new UnsupportedOperationException();
305 * Set the current value of the Slider
306 * @param value the new value
308 public void setUserValue(double value) {
309 if (!SwingUtilities.isEventDispatchThread()) { /* Avoid double-update when Model is updated from the callback */
310 /* Remove the listener to avoid the callback to be executed */
311 removeAdjustmentListener(adjustmentListener);
313 double userMin = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
314 double userMax = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
315 super.setValue(MINIMUM_VALUE + (int) ((value - userMin) * (MAXIMUM_VALUE - MINIMUM_VALUE) / (userMax - userMin)));
317 /* Put back the listener */
318 addAdjustmentListener(adjustmentListener);
326 public void setId(String id) {
334 public String getId() {
339 * Generic update method
340 * @param property property name
341 * @param value property value
343 public void update(int property, Object value) {
344 SwingViewWidget.update(this, property, value);
348 * Update values in the model when needed
350 private void updateModel() {
351 Double[] value = new Double[1];
352 double userMin = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
353 double userMax = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
354 value[0] = userMin + ((getValue() - MINIMUM_VALUE) * (userMax - userMin) / (MAXIMUM_VALUE - MINIMUM_VALUE));
355 GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, value);