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;
25 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
26 import org.scilab.modules.gui.SwingViewWidget;
27 import org.scilab.modules.gui.SwingViewObject;
28 import org.scilab.modules.gui.events.callback.CommonCallBack;
29 import org.scilab.modules.gui.menubar.MenuBar;
30 import org.scilab.modules.gui.slider.SimpleSlider;
31 import org.scilab.modules.gui.textbox.TextBox;
32 import org.scilab.modules.gui.toolbar.ToolBar;
33 import org.scilab.modules.gui.utils.Position;
34 import org.scilab.modules.gui.utils.PositionConverter;
35 import org.scilab.modules.gui.utils.ScilabRelief;
36 import org.scilab.modules.gui.utils.ScilabSwingUtilities;
37 import org.scilab.modules.gui.utils.Size;
40 * Swing implementation for Scilab Slider in GUIs
41 * @author Vincent COUVERT
42 * @author Marouane BEN JELLOUL
44 public class SwingScilabSlider extends JScrollBar implements SwingViewObject, SimpleSlider {
46 private static final long serialVersionUID = -4262320156090829309L;
48 private static final int MIN_KNOB_SIZE = 40;
50 private static final int MINIMUM_VALUE = 0;
51 private static final int MAXIMUM_VALUE = 1000;
55 private CommonCallBack callback;
57 private AdjustmentListener adjustmentListener;
62 public SwingScilabSlider() {
64 // needed to have slider working with GLCanvas
66 setMinimum(MINIMUM_VALUE);
67 setMaximum(MAXIMUM_VALUE + getVisibleAmount());
68 adjustmentListener = new AdjustmentListener() {
69 public void adjustmentValueChanged(AdjustmentEvent arg0) {
71 if (callback != null) {
72 callback.actionPerformed(null);
76 addAdjustmentListener(adjustmentListener);
80 * Draws a swing Scilab Slider
81 * @see org.scilab.modules.gui.UIElement#draw()
84 this.setVisible(true);
89 * Gets the dimensions (width and height) of a swing Scilab Slider
90 * @return the dimensions of the Slider
91 * @see org.scilab.modules.gui.uielement.UIElement#getDims()
93 public Size getDims() {
94 return new Size(super.getSize().width, super.getSize().height);
98 * Gets the position (X-coordinate and Y-coordinate) of a swing Scilab Slider
99 * @return the position of the Slider
100 * @see org.scilab.modules.gui.uielement.UIElement#getPosition()
102 public Position getPosition() {
103 return PositionConverter.javaToScilab(getLocation(), getSize(), getParent());
107 * Sets the dimensions (width and height) of a swing Scilab Slider
108 * @param newSize the dimensions to set to the Slider
109 * @see org.scilab.modules.gui.uielement.UIElement#setDims(org.scilab.modules.gui.utils.Size)
111 public void setDims(Size newSize) {
112 super.setSize(newSize.getWidth(), newSize.getHeight());
113 super.doLayout(); // Usefull in case of resize
117 * Sets the position (X-coordinate and Y-coordinate) of a swing Scilab Slider
118 * @param newPosition the position to set to the Slider
119 * @see org.scilab.modules.gui.uielement.UIElement#setPosition(org.scilab.modules.gui.utils.Position)
121 public void setPosition(Position newPosition) {
122 Position javaPosition = PositionConverter.scilabToJava(newPosition, getDims(), getParent());
123 setLocation(javaPosition.getX(), javaPosition.getY());
127 * Add a callback to the Slider
128 * @param cb the callback to set.
130 public void setCallback(CommonCallBack cb) {
131 /* Create a callback */
137 * @param menuBarToAdd the MenuBar associated to the Tab.
139 public void addMenuBar(MenuBar menuBarToAdd) {
140 /* Unimplemented for CheckBoxes */
141 throw new UnsupportedOperationException();
146 * @param toolBarToAdd the ToolBar associated to the Tab.
148 public void addToolBar(ToolBar toolBarToAdd) {
149 /* Unimplemented for CheckBoxes */
150 throw new UnsupportedOperationException();
155 * @return MenuBar: the MenuBar associated to the Tab.
157 public MenuBar getMenuBar() {
158 /* Unimplemented for CheckBoxes */
159 throw new UnsupportedOperationException();
164 * @return ToolBar: the ToolBar associated to the Tab.
166 public ToolBar getToolBar() {
167 /* Unimplemented for CheckBoxes */
168 throw new UnsupportedOperationException();
172 * Get the text of the Slider
174 * @see org.scilab.modules.gui.widget.Widget#getText()
176 public String getText() {
177 return this.getName();
181 * Set the text of the Slider
182 * @param text the text to set to the Slider
183 * @see org.scilab.modules.gui.widget.Widget#setText(java.lang.String)
185 public void setText(String text) {
190 * Set the horizontal alignment for the Slider text
191 * @param alignment the value for the alignment (See ScilabAlignment.java)
193 public void setHorizontalAlignment(String alignment) {
194 // Nothing to do here
198 * Set the vertical alignment for the Slider text
199 * @param alignment the value for the alignment (See ScilabAlignment.java)
201 public void setVerticalAlignment(String alignment) {
202 // Nothing to do here
206 * Set the minimum value of a Slider
207 * @param value the minimum value
209 public void setMinimumValue(double value) {
214 * Set the maximum value of a Slider
215 * @param value the maximum value
217 public void setMaximumValue(double value) {
222 * Set the Relief of the Slider
223 * @param reliefType the type of the relief to set (See ScilabRelief.java)
225 public void setRelief(String reliefType) {
226 setBorder(ScilabRelief.getBorderFromRelief(reliefType));
230 * Set the major tick spacing for a Slider
231 * @param space the increment value
233 public void setMajorTickSpacing(double space) {
234 /* Remove the listener to avoid the callback to be executed */
235 removeAdjustmentListener(adjustmentListener);
237 setBlockIncrement((int) (space * (MAXIMUM_VALUE - MINIMUM_VALUE)));
238 int oldMax = getMaximum() - getVisibleAmount();
239 setVisibleAmount(Math.max((int) (space * (MAXIMUM_VALUE - MINIMUM_VALUE)), MIN_KNOB_SIZE));
240 setMaximum(oldMax + getVisibleAmount());
242 /* Put back the listener */
243 addAdjustmentListener(adjustmentListener);
247 * Set the minor tick spacing for a Slider
248 * @param space the increment value
250 public void setMinorTickSpacing(double space) {
251 /* Remove the listener to avoid the callback to be executed */
252 removeAdjustmentListener(adjustmentListener);
254 setUnitIncrement((int) (space * (MAXIMUM_VALUE - MINIMUM_VALUE)));
256 /* Put back the listener */
257 addAdjustmentListener(adjustmentListener);
261 * Set the slider orientation to vertical
263 public void setVertical() {
264 setOrientation(JScrollBar.VERTICAL);
268 * Set the slider orientation to horizontal
270 public void setHorizontal() {
271 setOrientation(JScrollBar.HORIZONTAL);
277 public void destroy() {
278 ScilabSwingUtilities.removeFromParent(this);
283 * @param infoBarToAdd the InfoBar associated to the Slider.
285 public void addInfoBar(TextBox infoBarToAdd) {
286 /* Unimplemented for Sliders */
287 throw new UnsupportedOperationException();
292 * @return the InfoBar associated to the Slider.
294 public TextBox getInfoBar() {
295 /* Unimplemented for Sliders */
296 throw new UnsupportedOperationException();
300 * Set the current value of the Slider
301 * @param value the new value
303 public void setUserValue(double value) {
304 /* Remove the listener to avoid the callback to be executed */
305 removeAdjustmentListener(adjustmentListener);
307 double userMin = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
308 double userMax = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
309 super.setValue(MINIMUM_VALUE + (int) ((value - userMin) * (MAXIMUM_VALUE - MINIMUM_VALUE) / (userMax - userMin)));
311 /* Put back the listener */
312 addAdjustmentListener(adjustmentListener);
319 public void setId(String id) {
327 public String getId() {
332 * Generic update method
333 * @param property property name
334 * @param value property value
336 public void update(int property, Object value) {
337 SwingViewWidget.update(this, property, value);
341 * Update values in the model when needed
343 private void updateModel() {
344 Double[] value = new Double[1];
345 double userMin = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MIN__);
346 double userMax = (Double) GraphicController.getController().getProperty(uid, __GO_UI_MAX__);
347 value[0] = userMin + ((getValue() - MINIMUM_VALUE) * (userMax - userMin) / (MAXIMUM_VALUE - MINIMUM_VALUE));
348 GraphicController.getController().setProperty(uid, __GO_UI_VALUE__, value);