001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008: package com.gwtext.client.widgets;
009:
010: import com.google.gwt.core.client.JavaScriptObject;
011: import com.google.gwt.user.client.Element;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013: import com.gwtext.client.widgets.event.ProgressBarListener;
014:
015: /**
016: * An updateable progress bar component. The progress bar supports two different modes: manual and automatic.
017: * <br><br>
018: * In manual mode, you are responsible for showing, updating (via updateProgress) and clearing the progress bar as needed
019: * from your own code. This method is most appropriate when you want to show progress throughout an operation that has
020: * predictable points of interest at which you can update the control.
021: * <br><br>
022: * In automatic mode, you simply call wait and let the progress bar run indefinitely, only clearing it once the operation is
023: * complete. You can optionally have the progress bar wait for a specific amount of time and then clear itself. Automatic mode
024: * is most appropriate for timed operations or asymchronous operations in which you have no need for indicating intermediate
025: * progress.
026: */
027: public class ProgressBar extends BoxComponent {
028:
029: private static JavaScriptObject configPrototype;
030:
031: static {
032: init();
033: }
034:
035: private static native void init()/*-{
036: var c = new $wnd.Ext.Toolbar();
037: @com.gwtext.client.widgets.ProgressBar::configPrototype = c.initialConfig;
038: }-*/;
039:
040: protected JavaScriptObject getConfigPrototype() {
041: return configPrototype;
042: }
043:
044: public String getXType() {
045: return "progress";
046: }
047:
048: protected native JavaScriptObject create(JavaScriptObject config) /*-{
049: return new $wnd.Ext.ProgressBar(config);
050: }-*/;
051:
052: public ProgressBar() {
053: }
054:
055: public ProgressBar(JavaScriptObject jsObj) {
056: super (jsObj);
057: }
058:
059: /**
060: * Returns true if the progress bar is currently in a {@link #wait} operation.
061: *
062: * @return true if in wait
063: */
064: public native boolean isWaiting() /*-{
065: var pb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
066: return pb.isWaiting();
067: }-*/;
068:
069: /**
070: * Resets the progress bar value to 0 and text to empty string.
071: */
072: public void reset() {
073: reset(false);
074: }
075:
076: private native void resetRendered(boolean hide) /*-{
077: var pb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
078: pb.reset(hide);
079: }-*/;
080:
081: /**
082: * Resets the progress bar value to 0 and text to empty string. If hide = true, the progress bar will also be hidden
083: * (using the hideMode property internally).
084: *
085: * @param hide true to hide the progress bar (defaults to false)
086: */
087: public void reset(boolean hide) {
088: if (!isRendered()) {
089: setValue(0);
090: } else {
091: resetRendered(false);
092: }
093: }
094:
095: /**
096: * Sets the size of the progress bar.
097: *
098: * @param width the width
099: * @param height the height
100: */
101: public native void setSize(int width, int height) /*-{
102: var pb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
103: pb.setSize(width, height);
104: }-*/;
105:
106: /**
107: * Updates the progress bar value. Any existing text value will be unchanged. Note that even if the progress bar
108: * value exceeds 1, it will never automatically reset -- you are responsible for determining when the progress is
109: * complete and calling reset to clear and/or hide the control.
110: *
111: * @param value the new value
112: */
113: public native void updateProgress(float value) /*-{
114: var pb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
115: pb.updateProgress(value);
116: }-*/;
117:
118: /**
119: * Updates the progress bar value, and its text. Note that even if the progress bar value exceeds 1, it will never
120: * automatically reset -- you are responsible for determining when the progress is complete and calling reset to
121: * clear and/or hide the control.
122: *
123: * @param value the new value
124: * @param text the new text
125: */
126: public native void updateProgress(float value, String text) /*-{
127: var pb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
128: pb.updateProgress(value, text);
129: }-*/;
130:
131: /**
132: * Updates the progress bar text. If specified, textEl will be updated, otherwise the progress bar
133: * itself will display the updated text.
134: *
135: * @param text the text to display
136: */
137: public native void updateText(String text) /*-{
138: var pb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
139: pb.updateText(text);
140: }-*/;
141:
142: /**
143: * Initiates an auto-updating progress bar. A duration can be specified, in which case the progress bar will automatically
144: * reset after a fixed amount of time and optionally call a callback function if specified. If no duration is passed in,
145: * then the progress bar will run indefinitely and must be manually cleared by calling reset.
146: *
147: * @param waitConfig the wait configuration
148: */
149: public native void wait(WaitConfig waitConfig) /*-{
150: var pb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
151: var wcJS = waitConfig == null ? null : waitConfig.@com.gwtext.client.core.JsObject::getJsObj()();
152: pb.wait(wcJS);
153: }-*/;
154:
155: /**
156: * Add a ProgressBar listener.
157: *
158: * @param listener the listener
159: */
160: public native void addListener(ProgressBarListener listener)/*-{
161: this.@com.gwtext.client.widgets.BoxComponent::addListener(Lcom/gwtext/client/widgets/event/BoxComponentListener;)(listener);
162: var pbJ = this;
163:
164: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('update',
165: function(source, value, text) {
166: listener.@com.gwtext.client.widgets.event.ProgressBarListener::onUpdate(Lcom/gwtext/client/widgets/ProgressBar;FLjava/lang/String;)(pbJ, value, text);
167: }
168: );
169: }-*/;
170:
171: // --- config properties
172:
173: /**
174: * The progress bar text (defaults to '').
175: *
176: * @param text the progress bar text
177: */
178: public void setText(String text) {
179: if (!isCreated()) {
180: JavaScriptObjectHelper.setAttribute(config, "text", text);
181: } else {
182: updateText(text);
183: }
184: }
185:
186: /**
187: * The progress bar text.
188: *
189: * @return the progress bar text (defaults to '')
190: */
191: public String getText() {
192: return getAttribute("text");
193: }
194:
195: /**
196: * The element to render the progress text to (defaults to the progress bar's internal text element).
197: * <br><br>
198: * <b>Note that the text element must already be rendered to the DOM before creation of the progress bar.</b>
199: * You can call textElComponent.render(RootPanel.getBodyElement()) to force addition of the textEl to the broswer DOM.
200: *
201: * @param textElID the text element ID
202: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
203: */
204: public void setTextEl(String textElID) throws IllegalStateException {
205: setAttribute("textEl", textElID, true);
206: }
207:
208: /**
209: * The element to render the progress text to (defaults to the progress bar's internal text element).
210: * <br><br>
211: * <b>Note that the text element must already be rendered to the DOM before creation of the progress bar.</b>
212: * You can call textElComponent.render(RootPanel.getBodyElement()) to force addition of the textEl to the broswer DOM.
213: *
214: *
215: * @param textEl the text element
216: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
217: */
218: public void setTextEl(Element textEl) throws IllegalStateException {
219: setAttribute("textEl", textEl, true);
220: }
221:
222: /**
223: * The value of the progress bar. A floating point value between 0 and 1 (e.g., .5, defaults to 0)
224: *
225: * @param value the value of the progress bar
226: */
227: public void setValue(float value) {
228: if (!isCreated()) {
229: JavaScriptObjectHelper.setAttribute(config, "value", value);
230: } else {
231: updateProgress(value);
232: }
233: }
234:
235: /**
236: * @return the value of the progress bar
237: */
238: public float getValue() {
239: return getAttributeAsFloat("value");
240: }
241: }
|