KickJava   Java API By Example, From Geeks To Geeks.

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


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.webapp.xmlhttp.PersistentFacesState;
41 import com.icesoft.faces.webapp.xmlhttp.RenderingException;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 /**
46  * Backs the indeterminate mode of the outputProgress component.
47  */

48 public class OutputProgressIndeterminateBean implements Renderable {
49
50     private static Log log =
51             LogFactory.getLog(OutputProgressIndeterminateBean.class);
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         this.renderManager = renderManager;
85     }
86
87     /**
88      * Gets RenderManager, just try to satisfy WAS
89      *
90      * @return RenderManager null
91      */

92     public RenderManager getRenderManager() {
93         return null;
94     }
95
96     // progress active status
97
private boolean runningTask;
98
99     // percentage completed
100
private int percent;
101
102     /**
103      * The constructor gets the PersistentFacesState for manual render
104      * requests.
105      */

106     public OutputProgressIndeterminateBean() {
107         state = PersistentFacesState.getInstance();
108     }
109
110     /**
111      * Gets the percentage completed.
112      *
113      * @return the current percentage
114      */

115     public int getPercent() {
116         return percent;
117     }
118
119     /**
120      * Sets the percentage.
121      *
122      * @param percent the new percentage
123      */

124     public void setPercent(int percent) {
125         this.percent = percent;
126     }
127
128     /**
129      * Determine whether the progress bar is active.
130      *
131      * @return the activity status
132      */

133     public boolean isRunningTask() {
134         return runningTask;
135     }
136
137     /**
138      * Set whether the progress bar is active.
139      *
140      * @param runningTask the new activity status
141      */

142     public void setRunningTask(boolean runningTask) {
143         this.runningTask = runningTask;
144     }
145
146     /**
147      * Start a routine that will take time and allow progress updates.
148      */

149     public void longOperation(ActionEvent event) {
150         setPercent(1);
151         Thread JavaDoc copyThread = new Thread JavaDoc(new LongOperationRunner(this,
152                                                                PersistentFacesState.getInstance()));
153         copyThread.start();
154     }
155
156     /**
157      * Routine that takes time and updates percentage as it runs.
158      */

159     class LongOperationRunner implements Runnable JavaDoc {
160         PersistentFacesState state = null;
161         private OutputProgressIndeterminateBean pbBean;
162
163         public LongOperationRunner(OutputProgressIndeterminateBean progress,
164                                    PersistentFacesState state) {
165             this.state = state;
166             this.pbBean = progress;
167         }
168
169         public void run() {
170
171             runningTask = true;
172             pbBean.setPercent(1);
173
174             try {
175                 Thread.sleep(3000);
176             } catch (InterruptedException JavaDoc e) {
177                 if (log.isErrorEnabled()) {
178                     log.error("interrupted", e);
179                 }
180             }
181
182             //enable the "start" button
183
runningTask = false;
184             pbBean.setPercent(100);
185             renderManager.requestRender(pbBean);
186         }
187     }
188 }
Popular Tags