KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > progressBar > OutputProgressRenderBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.icefaces.samples.showcase.components.progressBar;
35
36 import javax.faces.event.ActionEvent;
37
38 import com.icesoft.faces.async.render.RenderManager;
39 import com.icesoft.faces.async.render.Renderable;
40 import com.icesoft.faces.component.outputprogress.OutputProgress;
41 import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
42 import com.icesoft.faces.webapp.xmlhttp.RenderingException;
43
44 /**
45  * <p>The OutputProgressRenderBean backs the determinate mode of the
46  * outputProgress component.</p>
47  *
48  * @see OutputProgressPropertyBean
49  * @since 1.0
50  */

51 public class OutputProgressRenderBean implements Renderable {
52
53     /**
54      * Renderable Interface
55      */

56     private PersistentFacesState state;
57     private RenderManager renderManager;
58
59     /**
60      * Get the PersistentFacesState.
61      *
62      * @return state the PersistantFacesState
63      */

64     public PersistentFacesState getState() {
65
66         return state;
67     }
68
69     /**
70      * Handles rendering exceptions for the progress bar.
71      *
72      * @param renderingException the exception that occured
73      */

74     public void renderingException(RenderingException renderingException) {
75         renderingException.printStackTrace();
76     }
77
78     /**
79      * Sets the Render Manager.
80      *
81      * @param renderManager
82      */

83     public void setRenderManager(RenderManager renderManager) {
84
85         this.renderManager = renderManager;
86     }
87
88     /**
89      * Gets RenderManager, just try to satisfy WAS
90      *
91      * @return RenderManager null
92      */

93     public RenderManager getRenderManager() {
94         return null;
95     }
96
97     // flag to disable start button when progress bar is started
98
private boolean disableStartButton = false;
99
100     // binding back to jsp page
101
private OutputProgress progressBar;
102
103     // value bound to component as an indicator of progress
104
private int percent = 0;
105
106     /**
107      * Default construction for the backing bean.
108      */

109     public OutputProgressRenderBean() {
110         state = PersistentFacesState.getInstance();
111     }
112
113     /**
114      * Gets the disabled state for the start button.
115      *
116      * @return true if the button should be disabled; false otherwise.
117      */

118     public boolean isDisableStartButton() {
119         return disableStartButton;
120     }
121
122     /**
123      * Start a new thread to do some work which is monitored for progress.
124      */

125     public void start(ActionEvent event) {
126         Thread JavaDoc testThread = new Thread JavaDoc(new LongOperationRunner(this));
127         testThread.start();
128     }
129
130     /**
131      * Get the current percent value.
132      *
133      * @return percent complete of progress bar
134      */

135     public int getPercent() {
136         return percent;
137     }
138
139     /**
140      * Sets the current percent value.
141      *
142      * @param percent percent value of progress bar state.
143      */

144     public void setPercent(int percent) {
145         this.percent = percent;
146     }
147
148     /**
149      * Gets the progress bar binding.
150      *
151      * @return bound progress bar.
152      */

153     public OutputProgress getProgressBar() {
154         return progressBar;
155     }
156
157     /**
158      * Sets the progress bar binding.
159      *
160      * @param progressBar progress bar to bind to this bean.
161      */

162     public void setProgressBar(OutputProgress progressBar) {
163         this.progressBar = progressBar;
164     }
165
166     /**
167      * Helper class that simulates a long running task. The progress bar
168      * updates based on this task via requestRender() calls.
169      */

170     protected class LongOperationRunner implements Runnable JavaDoc {
171
172         PersistentFacesState state = null;
173         OutputProgressRenderBean outputBean;
174
175         public LongOperationRunner(OutputProgressRenderBean outputBean) {
176             disableStartButton = true;
177             percent = 0;
178             this.outputBean = outputBean;
179         }
180
181         public void run() {
182             try {
183                 for (int i = 0; i <= 100; i += 10) {
184                     // pause the thread
185
Thread.sleep(300);
186                     // update the percent value
187
percent = i;
188                     // call a render to update the component state
189
try {
190                         renderManager.requestRender(outputBean);
191                     } catch (IllegalStateException JavaDoc e) {
192                         e.printStackTrace();
193                     }
194                 }
195                 // now that thread work is complete enable "start" button
196
disableStartButton = false;
197
198                 // refresh DOM one more time to update "start" button
199
renderManager.requestRender(outputBean);
200             }
201             catch (InterruptedException JavaDoc e) {
202                 e.printStackTrace();
203             }
204         }
205     }
206
207     /**
208      * Determine whether the progress bar is active.
209      *
210      * @return the activity status
211      */

212     public boolean isRunningTask() {
213         return disableStartButton;
214     }
215
216     /**
217      * Set whether the progress bar is active.
218      *
219      * @param runningTask the new activity status
220      */

221     public void setRunningTask(boolean runningTask) {
222         disableStartButton = runningTask;
223     }
224
225 }
Popular Tags