01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.ui;
21 
22 import js.Id;
23 import js.Initializer;
24 import js.ObjectLike;
25 import jsx.dom.Styles;
26 import jsx.ui.event.OnProgress;
27 import jsx.ui.event.Progress;
28 
29 /**
30  * <p>A base class for progress widgets.</p>
31  * <p>A {@link ProgressBar} is a {@link Widget}.</p>
32  * 
33  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
34  */
35 public class ProgressBar extends Widget implements OnProgress
36 {
37     /**
38      * <p>The default constructor.</p>
39      * <p>This constructor invokes the default constructor of this class.</p>
40      * @since 1.0
41      */
42     public ProgressBar() {
43         super(Component.div());
44         addListener(Progress.class, this);
45         addClasses();
46         Component e = unwrap();
47         Component c = Component.div();
48         ini(e).var(PROGRESS, c);
49         Component.appendChild(e, c);
50     }
51 
52     private final static Id<Component> PROGRESS = new Id<Component>();
53 
54     /**
55      * <p>Sets rate of progress.</p>
56      * @param rate The rate of percentage for the progress.
57      * @since 1.0
58      */
59     public void setRate(int rate) {
60         ObjectLike p = new Initializer().var();
61         Styles.width(p, Styles.pc(rate));
62         Component.applyStyle(ini(unwrap()).var(PROGRESS), p);
63     }
64 
65     /**
66      * <p>Performs an action on the dispatched event.</p>
67      * <p>This method sets {@link Progress#RATE} to this {@link ProgressBar}.</p>
68      * @param evt The event dispatched to this listener.
69      * @see #setRate(int)
70      * @since 1.0
71      */
72     public void onEvent(Progress evt) {
73         setRate(ini(evt).var(Progress.RATE));
74     }
75 }