001: /* Slider.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Thu Sep 29 20:16:03 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import org.zkoss.lang.Objects;
022: import org.zkoss.xml.HTMLs;
023:
024: import org.zkoss.zk.ui.WrongValueException;
025: import org.zkoss.zk.ui.event.Events;
026: import org.zkoss.zk.ui.ext.client.Scrollable;
027:
028: import org.zkoss.zul.impl.XulElement;
029:
030: /**
031: * A slider.
032: *
033: * <p>To customize the look, you can specify the style class with
034: * {@link #setSclass}. Then, the following style classes are generated
035: * to style the look. Assume that the style class is "slider"
036: * <ul>
037: * <li>slider-bk: the center background</li>
038: * <li>slider-bkl: the left background</li>
039: * <li>slider-bkr: the right background</li>
040: * <li>slider-btn: the bottom background</li>
041: * </ul>
042: *
043: * <p>If {@link #getSclass} is empty and {@link #getMold} is "default",
044: * "slider" is assumed. If {@link #getMold} is "sphere", "slidersph" is assumed.
045: *
046: * @author tomyeh
047: */
048: public class Slider extends XulElement {
049: private int _curpos, _maxpos = 100, _pginc = 10;
050: /** The name. */
051: private String _name;
052: private String _slidingtext = "{0}";
053:
054: public Slider() {
055: setWidth("100px");
056: }
057:
058: /**
059: * @param curpos the current position (default: 0)
060: */
061: public Slider(int curpos) {
062: this ();
063: setCurpos(curpos);
064: }
065:
066: /**
067: * Returns the sliding text.
068: * <p>Default : "{0}"
069: * @since 3.0.1
070: */
071: public String getSlidingtext() {
072: return _slidingtext;
073: }
074:
075: /**
076: * Sets the sliding text.
077: * The syntax "{0}" will be replaced with the position at client side.
078: * @since 3.0.1
079: */
080: public void setSlidingtext(String slidingtext) {
081: if (slidingtext == null || slidingtext.length() == 0)
082: slidingtext = "{0}";
083: if (!_slidingtext.equals(slidingtext)) {
084: _slidingtext = slidingtext;
085: smartUpdate("z.slidingtext", _slidingtext);
086: }
087: }
088:
089: /** Returns the current position of the slider.
090: *
091: * <p>Default: 0.
092: */
093: public final int getCurpos() {
094: return _curpos;
095: }
096:
097: /** Sets the current position of the slider.
098: * If negative, 0 is assumed. If larger than {@link #getMaxpos},
099: * {@link #getMaxpos} is assumed.
100: */
101: public final void setCurpos(int curpos) throws WrongValueException {
102: if (curpos < 0)
103: curpos = 0;
104: else if (curpos > _maxpos)
105: curpos = _maxpos;
106:
107: if (_curpos != curpos) {
108: _curpos = curpos;
109: smartUpdate("z.curpos", _curpos);
110: }
111: }
112:
113: /** Returns the maximum position of the slider.
114: *
115: * <p>Default: 100.
116: */
117: public final int getMaxpos() {
118: return _maxpos;
119: }
120:
121: /** Sets the maximum position of the slider.
122: *
123: * @exception WrongValueException if non-positive maxpos is passed
124: */
125: public final void setMaxpos(int maxpos) throws WrongValueException {
126: if (maxpos <= 0)
127: throw new WrongValueException(
128: "Nonpositive is not allowed: " + maxpos);
129:
130: if (_maxpos != maxpos) {
131: if (_curpos > maxpos)
132: setCurpos(maxpos);
133: _maxpos = maxpos;
134: smartUpdate("z.maxpos", _maxpos);
135: }
136: }
137:
138: /** Returns the amount that the value of {@link #getCurpos}
139: * changes by when the tray of the scroll bar is clicked.
140: *
141: * <p>Default: 10.
142: */
143: public final int getPageIncrement() {
144: return _pginc;
145: }
146:
147: /** Sets the amount that the value of {@link #getCurpos}
148: * changes by when the tray of the scroll bar is clicked.
149: */
150: public final void setPageIncrement(int pginc)
151: throws WrongValueException {
152: if (pginc <= 0)
153: throw new WrongValueException(
154: "Nonpositive is not allowed: " + pginc);
155: if (_pginc != pginc) {
156: _pginc = pginc;
157: smartUpdate("z.pginc", _pginc);
158: }
159: }
160:
161: /** Returns the name of this component.
162: * <p>Default: null.
163: * <p>The name is used only to work with "legacy" Web application that
164: * handles user's request by servlets.
165: * It works only with HTTP/HTML-based browsers. It doesn't work
166: * with other kind of clients.
167: * <p>Don't use this method if your application is purely based
168: * on ZK's event-driven model.
169: * @since 3.0.0
170: */
171: public String getName() {
172: return _name;
173: }
174:
175: /** Sets the name of this component.
176: * <p>The name is used only to work with "legacy" Web application that
177: * handles user's request by servlets.
178: * It works only with HTTP/HTML-based browsers. It doesn't work
179: * with other kind of clients.
180: * <p>Don't use this method if your application is purely based
181: * on ZK's event-driven model.
182: *
183: * @param name the name of this component.
184: * @since 3.0.0
185: */
186: public void setName(String name) {
187: if (name != null && name.length() == 0)
188: name = null;
189: if (!Objects.equals(_name, name)) {
190: _name = name;
191: smartUpdate("z.name", _name);
192: }
193: }
194:
195: //-- super --//
196: public String getOuterAttrs() {
197: final StringBuffer sb = new StringBuffer(64).append(super
198: .getOuterAttrs());
199: HTMLs.appendAttribute(sb, "z.name", _name);
200: HTMLs.appendAttribute(sb, "z.curpos", _curpos);
201: HTMLs.appendAttribute(sb, "z.maxpos", _maxpos);
202: HTMLs.appendAttribute(sb, "z.pginc", _pginc);
203: HTMLs.appendAttribute(sb, "z.slidingtext", getSlidingtext());
204:
205: appendAsapAttr(sb, Events.ON_SCROLL);
206: appendAsapAttr(sb, Events.ON_SCROLLING);
207: appendAsapAttr(sb, Events.ON_RIGHT_CLICK);
208: //no z.dbclk to avoid confusion
209: //no z.lfclk since it will be supported by sld.js
210:
211: return sb.toString();
212: }
213:
214: //-- Component --//
215: /** Not childable. */
216: public boolean isChildable() {
217: return false;
218: }
219:
220: //-- ComponentCtrl --//
221: protected Object newExtraCtrl() {
222: return new ExtraCtrl();
223: }
224:
225: /** A utility class to implement {@link #getExtraCtrl}.
226: * It is used only by component developers.
227: */
228: protected class ExtraCtrl extends XulElement.ExtraCtrl implements
229: Scrollable {
230: //-- Scrollable --//
231: public final void setCurposByClient(int curpos) {
232: if (curpos < 0)
233: throw new WrongValueException(
234: "Negative is not allowed: " + curpos);
235: _curpos = curpos;
236: }
237: }
238: }
|