001: /*
002: * Copyright (C) 2004 TiongHiang Lee
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Email: thlee@onemindsoft.org
019: */
020:
021: package org.onemind.swingweb.client.gwt.widget;
022:
023: import com.google.gwt.user.client.*;
024: import com.google.gwt.user.client.ui.*;
025:
026: public class Slider extends SimplePanel implements HasOrientation,
027: SourcesChangeEvents {
028:
029: private class TrackSlide extends SimplePanel implements
030: SourcesChangeEvents, MouseListener {
031:
032: private int _trackSize = 4;
033:
034: private int _slideSize = 10;
035:
036: private int _slideLength = _slideSize / 2;
037:
038: private int _minValue = 0;
039:
040: private int _maxValue = 100;
041:
042: private int _value = 0;
043:
044: private int _tempValue;
045:
046: private boolean _capture;
047:
048: private int _startX, _startY;
049:
050: private ChangeListenerCollection _changeListenerCol = new ChangeListenerCollection();
051:
052: private SimplePanel _track = new SimplePanel();
053:
054: private MousePanel _slide = new MousePanel();
055:
056: private Label _slideTip = new Label();
057:
058: private TrackSlide() {
059: super ();
060: //setup track
061: DOM.setAttribute(_track.getElement(), "className",
062: "slider-track");
063: setWidget(_track);
064: //setup slide
065: _track.setWidget(_slide);
066: DOM.setAttribute(_slide.getElement(), "className",
067: "slider-slide");
068: DOM.setStyleAttribute(_slide.getElement(), "zIndex", "1");
069: DOM.setStyleAttribute(_slide.getElement(), "position",
070: "relative");
071: _slide.addMouseListener(this );
072: _slide.setWidget(_slideTip);
073: DOM.setStyleAttribute(_slide.getElement(), "float", "left");
074: }
075:
076: public void onMouseDown(Widget sender, int x, int y) {
077: _capture = true;
078: _startX = x;
079: _startY = y;
080: _tempValue = _value;
081: DOM.setCapture(_slide.getElement());
082: }
083:
084: public void onMouseEnter(Widget sender) {
085: // TODO Auto-generated method stub
086: }
087:
088: public void onMouseLeave(Widget sender) {
089: // TODO Auto-generated method stub
090: }
091:
092: public void onMouseMove(Widget sender, int x, int y) {
093: if (_capture) {
094: if (_orientation == HORIZONTAL) {
095: int absX = _slide.getAbsoluteLeft() + x;
096: int newX = absX - _startX;
097: int maxX = _width - _slideLength;
098: System.out.println("move:MaxX = " + maxX);
099: //find the relative
100: int relativeX = newX - _track.getAbsoluteLeft();
101: if (relativeX < 0) {
102: relativeX = 0;
103: } else if (relativeX > maxX) {
104: relativeX = maxX;
105: }
106: _tempValue = computeValue(maxX, relativeX);
107: checkSlidePos(_tempValue);
108: // _slideTip.setText(String.valueOf(_tempValue));
109: // DOM.setStyleAttribute(_slide.getElement(), "left", String.valueOf(relativeX));
110: } else {
111: int absY = _slide.getAbsoluteTop() + y;
112: int newY = absY - _startY;
113: int maxY = _height - _slideLength;
114: //find the relative
115: int relativeY = newY - _track.getAbsoluteTop();
116: if (relativeY < 0) {
117: relativeY = 0;
118: } else if (relativeY > maxY) {
119: relativeY = maxY;
120: }
121: _tempValue = computeValue(maxY, relativeY);
122: checkSlidePos(_tempValue);
123: // _slideTip.setText(String.valueOf(_tempValue));
124: // DOM.setStyleAttribute(_slide.getElement(), "top", String.valueOf(relativeY));
125: }
126: }
127: }
128:
129: public void onMouseUp(Widget sender, int x, int y) {
130: _capture = false;
131: _slideTip.setText("");
132: DOM.releaseCapture(_slide.getElement());
133: if (_tempValue != _value) {
134: _value = _tempValue;
135: _changeListenerCol.fireChange(Slider.this );
136: System.out.println("Fire changes");
137: }
138: }
139:
140: private int computeValue(int physicalRange, int physicalPos) {
141: float percent = physicalPos / (float) physicalRange;
142: int range = _maxValue - _minValue;
143: int newValue = _minValue + (int) (range * percent);
144: return newValue;
145: }
146:
147: /**
148: * Set the maxValue
149: * @param maxValue The maxValue to set.
150: */
151: public final void setMaxValue(int maxValue) {
152: if (maxValue != _maxValue) {
153: _maxValue = maxValue;
154: checkSlidePos();
155: }
156: }
157:
158: /**
159: * Set the minValue
160: * @param minValue The minValue to set.
161: */
162: public final void setMinValue(int minValue) {
163: if (minValue != _minValue) {
164: _minValue = minValue;
165: checkSlidePos();
166: }
167: }
168:
169: /**
170: * Return the maxValue
171: * @return the maxValue.
172: */
173: public final int getMaxValue() {
174: return _maxValue;
175: }
176:
177: /**
178: * Return the minValue
179: * @return the minValue.
180: */
181: public final int getMinValue() {
182: return _minValue;
183: }
184:
185: /**
186: * Return the value
187: * @return the value.
188: */
189: public final int getValue() {
190: return _value;
191: }
192:
193: /**
194: * Set the value
195: * @param value The value to set.
196: */
197: public final void setValue(int value) {
198: if (_value != value) {
199: _value = value;
200: checkSlidePos();
201: }
202: }
203:
204: public void init(OrientationConstant orientation, int width,
205: int height) {
206: int slideOffset = (_trackSize - _slideSize) / 2;
207: if (orientation == VERTICAL) {
208: int margin = (width - _trackSize) / 2;
209: DOM.setStyleAttribute(_track.getElement(), "float",
210: "left");
211: DOM.setStyleAttribute(_track.getElement(), "width",
212: String.valueOf(_trackSize));
213: DOM.setStyleAttribute(_track.getElement(), "height",
214: String.valueOf(height));
215: DOM.setStyleAttribute(_track.getElement(),
216: "marginLeft", String.valueOf(margin));
217: DOM.setStyleAttribute(_track.getElement(), "marginTop",
218: "0");
219: DOM.setStyleAttribute(_slide.getElement(), "width",
220: String.valueOf(_slideSize));
221: DOM.setStyleAttribute(_slide.getElement(), "height",
222: String.valueOf(_slideLength));
223: DOM.setStyleAttribute(_slide.getElement(), "left",
224: String.valueOf(slideOffset));
225: } else {
226: int margin = (height - _trackSize) / 2;
227: DOM.setStyleAttribute(_track.getElement(), "width",
228: String.valueOf(width));
229: DOM.setStyleAttribute(_track.getElement(), "height",
230: String.valueOf(_trackSize));
231: DOM.setStyleAttribute(_track.getElement(), "marginTop",
232: String.valueOf(margin));
233: DOM.setStyleAttribute(_track.getElement(),
234: "marginLeft", "0");
235: DOM.setStyleAttribute(_slide.getElement(), "height",
236: String.valueOf(_slideSize));
237: DOM.setStyleAttribute(_slide.getElement(), "width",
238: String.valueOf(_slideLength));
239: DOM.setStyleAttribute(_slide.getElement(), "top",
240: String.valueOf(slideOffset));
241: }
242: checkSlidePos();
243: }
244:
245: private void checkSlidePos() {
246: checkSlidePos(_value);
247: }
248:
249: private void checkSlidePos(int value) {
250: if (_orientation == HORIZONTAL) {
251: if (_width > 0) {
252: int fullRange = _maxValue - _minValue;
253: float valuePercent = (float) (value - _minValue)
254: / fullRange;
255: int maxX = _width - _slideLength;
256: int left = (int) (valuePercent * maxX);
257: DOM
258: .setStyleAttribute(
259: _slide.getElement(),
260: "top",
261: String
262: .valueOf(-((_slideSize - _trackSize) / 2)));
263: DOM.setStyleAttribute(_slide.getElement(), "left",
264: String.valueOf(left));
265: }
266: } else {
267: if (_height > 0) {
268: int fullRange = _maxValue - _minValue;
269: float valuePercent = (float) (value - _minValue)
270: / fullRange;
271: int maxY = _height - _slideLength;
272: int top = (int) (valuePercent * maxY);
273: DOM
274: .setStyleAttribute(
275: _slide.getElement(),
276: "left",
277: String
278: .valueOf(-((_slideSize - _trackSize) / 2)));
279: DOM.setStyleAttribute(_slide.getElement(), "top",
280: String.valueOf(top));
281: }
282: }
283: }
284:
285: public void addChangeListener(ChangeListener listener) {
286: _changeListenerCol.add(listener);
287: }
288:
289: public void removeChangeListener(ChangeListener listener) {
290: _changeListenerCol.remove(listener);
291: }
292: }
293:
294: private OrientationConstant _orientation;
295:
296: private SimplePanel _tickArea = new SimplePanel();
297:
298: private int _width, _height;
299:
300: private FlexTable _flexTable;
301:
302: private TrackSlide _trackSlide = new TrackSlide();
303:
304: public Slider() {
305: this (VERTICAL);
306: initSlider();
307: }
308:
309: private void initSlider() {
310: _flexTable = new FlexTable();
311: _flexTable.setWidth("100%");
312: _flexTable.setHeight("100%");
313: setWidget(_flexTable);
314: if (_orientation == VERTICAL) {
315: _flexTable.setWidget(0, 0, _trackSlide);
316: _flexTable.setWidget(0, 1, _tickArea);
317: DOM.setStyleAttribute(_flexTable.getCellFormatter()
318: .getElement(0, 0), "align", "center");
319: DOM.setStyleAttribute(_flexTable.getCellFormatter()
320: .getElement(0, 1), "align", "center");
321: DOM.setStyleAttribute(_flexTable.getCellFormatter()
322: .getElement(0, 0), "width", "50%");
323: DOM.setStyleAttribute(_flexTable.getCellFormatter()
324: .getElement(0, 1), "width", "50%");
325: } else {
326: _flexTable.setWidget(0, 0, _trackSlide);
327: _flexTable.setWidget(1, 0, _tickArea);
328: DOM.setStyleAttribute(_flexTable.getCellFormatter()
329: .getElement(0, 0), "verticalAlign", "middle");
330: DOM.setStyleAttribute(_flexTable.getCellFormatter()
331: .getElement(1, 0), "verticalAlign", "middle");
332: DOM.setStyleAttribute(_flexTable.getCellFormatter()
333: .getElement(0, 0), "height", "50%");
334: DOM.setStyleAttribute(_flexTable.getCellFormatter()
335: .getElement(1, 0), "height", "50%");
336: }
337: _trackSlide.init(_orientation, _width, _height);
338: }
339:
340: public Slider(OrientationConstant orientation) {
341: setOrientation(orientation);
342: }
343:
344: public OrientationConstant getOrientation() {
345: return _orientation;
346: }
347:
348: public void setOrientation(OrientationConstant orientation) {
349: if (_orientation != orientation) {
350: _orientation = orientation;
351: initSlider();
352: }
353: }
354:
355: /**
356: * Set the maxValue
357: * @param maxValue The maxValue to set.
358: */
359: public final void setMaxValue(int maxValue) {
360: _trackSlide.setMaxValue(maxValue);
361: }
362:
363: /**
364: * Set the minValue
365: * @param minValue The minValue to set.
366: */
367: public final void setMinValue(int minValue) {
368: _trackSlide.setMinValue(minValue);
369: }
370:
371: /**
372: * Return the maxValue
373: * @return the maxValue.
374: */
375: public final int getMaxValue() {
376: return _trackSlide.getMaxValue();
377: }
378:
379: /**
380: * Return the minValue
381: * @return the minValue.
382: */
383: public final int getMinValue() {
384: return _trackSlide.getMinValue();
385: }
386:
387: /**
388: * Return the value
389: * @return the value.
390: */
391: public final int getValue() {
392: return _trackSlide.getValue();
393: }
394:
395: /**
396: * Set the value
397: * @param value The value to set.
398: */
399: public final void setValue(int value) {
400: _trackSlide.setValue(value);
401: }
402:
403: /**
404: * Set the width
405: * @param width The width to set.
406: */
407: public final void setWidth(String width) {
408: _width = Integer.parseInt(width);
409: _trackSlide.init(_orientation, _width, _height);
410: super .setWidth(width);
411: }
412:
413: /**
414: * Set the height
415: * @param height The height to set.
416: */
417: public final void setHeight(String height) {
418: _height = Integer.parseInt(height);
419: _trackSlide.init(_orientation, _width, _height);
420: super .setHeight(height);
421: }
422:
423: public void addChangeListener(ChangeListener listener) {
424: _trackSlide.addChangeListener(listener);
425: }
426:
427: public void removeChangeListener(ChangeListener listener) {
428: // TODO Auto-generated method stub
429: }
430: }
|