KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > outputprogress > OutputProgressRenderer


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.faces.component.outputprogress;
35
36 import java.io.IOException JavaDoc;
37
38 import javax.faces.FacesException;
39 import javax.faces.component.UIComponent;
40 import javax.faces.context.FacesContext;
41
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44 import org.w3c.dom.Text JavaDoc;
45
46 import com.icesoft.faces.component.CSS_DEFAULT;
47 import com.icesoft.faces.component.ext.taglib.Util;
48 import com.icesoft.faces.context.DOMContext;
49 import com.icesoft.faces.renderkit.dom_html_basic.DomBasicInputRenderer;
50 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
51 import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer;
52
53 public class OutputProgressRenderer extends DomBasicInputRenderer {
54
55
56     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
57             throws IOException JavaDoc {
58
59
60         validateParameters(facesContext, uiComponent, OutputProgress.class);
61         DOMContext domContext =
62                 DOMContext.attachDOMContext(facesContext, uiComponent);
63
64         if (!domContext.isInitialized()) {
65             Element JavaDoc table = domContext.createRootElement(HTML.TABLE_ELEM);
66             setRootElementId(facesContext, table, uiComponent);
67             table.setAttribute(HTML.CELLPADDING_ATTR, "0");
68             table.setAttribute(HTML.CELLSPACING_ATTR, "0");
69             table.setAttribute(HTML.BORDER_ATTR, "0");
70         }
71         Element JavaDoc table = (Element JavaDoc) domContext.getRootNode();
72         String JavaDoc style = ((OutputProgress) uiComponent).getStyle();
73         if(style != null && style.length() > 0)
74             table.setAttribute(HTML.STYLE_ATTR, style);
75         else
76             table.removeAttribute(HTML.STYLE_ATTR);
77         //In order to fix IRAPtor Bug 291, we took out buildLayout() from the intialized block,
78
//Because of variouse text position, layout could have different combination of tr and td
79
//therefore we are storing nodes to the component itself.
80
buildLayout(table, uiComponent, domContext);
81
82         setPercentage(facesContext, uiComponent);
83         if (PassThruAttributeRenderer.passThruAttributeExists(uiComponent)) {
84             PassThruAttributeRenderer
85                     .renderAttributes(facesContext, uiComponent, null);
86         }
87
88         domContext.stepOver();
89         domContext.streamWrite(facesContext, uiComponent);
90     }
91
92
93     private void setPercentage(FacesContext facesContext,
94                                UIComponent uiComponent) {
95         DOMContext domContext =
96                 DOMContext.getDOMContext(facesContext, uiComponent);
97         String JavaDoc space = " ";
98         if (domContext.isStreamWriting()) {
99             space = "";
100         }
101
102         OutputProgress progressBar = (OutputProgress) uiComponent;
103
104         Text JavaDoc percentageText = progressBar.getTextNode();
105         Element JavaDoc bgBar = progressBar.getBarNode();
106         Element JavaDoc fillBar = (Element JavaDoc) bgBar.getFirstChild();
107
108         String JavaDoc progressLabel = progressBar.getProgressLabel();
109         int percentValue = progressBar.getValue();
110         if (percentValue > 100) {
111             percentValue = 100;
112         }
113         if (percentValue < 0) {
114             percentValue = 0;
115         }
116         //update percent value in determinate mode only
117
if (progressBar.getIndeterminate() == false) {
118             percentageText.setData(percentValue + " %");
119         }
120
121         if (percentValue < 100) {
122
123             if (progressLabel != null && progressLabel.length() > 0) {
124                 percentageText.setData(progressLabel);
125             }
126             //following if block is for Indeterminate mode only
127
if (progressBar.getIndeterminate()) {
128                 if (percentValue < 1) {
129                     fillBar.setAttribute(HTML.CLASS_ATTR,
130                             progressBar.getIndeterminateInactiveClass());
131                     percentageText.setData(space);
132                 } else {
133                     fillBar.setAttribute(HTML.CLASS_ATTR,
134                             progressBar.getIndeterminateActiveClass());
135                     fillBar.setAttribute(HTML.STYLE_ATTR,
136                                          "position:absolute;width:100%");
137
138                     if (progressLabel != null && progressLabel.length() > 0) {
139                         percentageText.setData(progressLabel);
140                     } else {
141                         percentageText.setData(space);
142                     }
143                 }
144             }
145
146         } else {
147             if (progressBar.getIndeterminate()) {
148                 fillBar.setAttribute(HTML.CLASS_ATTR,
149                         progressBar.getIndeterminateInactiveClass());
150                 fillBar.setAttribute(HTML.STYLE_ATTR,
151                                      "position:absolute;width:100%;");
152             }
153             String JavaDoc progressCompleteLabel = progressBar.getProgressLabelComplete();
154             if (progressCompleteLabel != null && progressCompleteLabel.length() > 0) {
155                 percentageText.setData(progressCompleteLabel);
156             }
157         }
158
159         // The following fix is required for determinate mode only
160
if (progressBar.getIndeterminate() == false) {
161             // This code it to fix IE renderering. If a nbsp is present and the value is zero
162
// then a tiny bit of the bar is rendered. However if this value is missing then
163
// firefox will not render the bar. Therefore we don't add the nbsp until we
164
// need to render the bar
165
Node JavaDoc node = fillBar.getFirstChild();
166             if (node instanceof Text JavaDoc) {
167                 if (percentValue <= 0) {
168                     fillBar.removeChild(node);
169                 }
170             } else if (node == null) {
171                 if (percentValue > 0) {
172                     Text JavaDoc nbsp4opera = domContext.createTextNode("&nbsp;");
173                     fillBar.appendChild(nbsp4opera);
174                 }
175             }
176         }
177         //set the percent value for determinate mode only
178
if (progressBar.getIndeterminate() == false) {
179             fillBar.setAttribute(HTML.STYLE_ATTR, "position:absolute;width:" +
180                                                   percentValue + "%;");
181         }
182     }
183
184     private void buildLayout(Element JavaDoc table, UIComponent uiComponent,
185                              DOMContext domContext) {
186         String JavaDoc space = "&nbsp;";
187         if (domContext.isStreamWriting()) {
188             space = "";
189         }
190         Node JavaDoc node = table.getFirstChild();
191         Element JavaDoc tbody = domContext.createElement(HTML.TBODY_ELEM);
192         if (node != null) {
193             table.replaceChild(tbody, node);
194         } else {
195             table.appendChild(tbody);
196         }
197
198
199         OutputProgress progressBar = (OutputProgress) uiComponent;
200         table.setAttribute(HTML.CLASS_ATTR, progressBar.getStyleClass());
201
202         Element JavaDoc row = domContext.createElement(HTML.TR_ELEM);
203         Element JavaDoc textTd = domContext.createElement(HTML.TD_ELEM);
204         textTd.setAttribute(HTML.CLASS_ATTR, progressBar.getTextClass());
205
206         Element JavaDoc barTd = domContext.createElement(HTML.TD_ELEM);
207         tbody.appendChild(row);
208         Text JavaDoc percentageText = null;
209         if (progressBar.getProgressLabel() != null) {
210             //add the blank label initially
211
percentageText = domContext.createTextNode(space);
212         } else {
213             percentageText = domContext.createTextNode("0 %");
214         }
215
216         textTd.appendChild(percentageText);
217         textTd.setAttribute("id", uiComponent
218                 .getClientId(FacesContext.getCurrentInstance()) +
219                                                                 "percentageText");
220
221         Element JavaDoc bgBar = domContext.createElement(HTML.DIV_ELEM);
222         bgBar.setAttribute(HTML.CLASS_ATTR, progressBar.getBackgroundClass());
223         bgBar.setAttribute(HTML.STYLE_ATTR, "position:relative;");
224
225         Element JavaDoc fillBar = domContext.createElement(HTML.DIV_ELEM);
226         fillBar.setAttribute(HTML.ID_ATTR, uiComponent
227                 .getClientId(FacesContext.getCurrentInstance()) + "bar");
228
229         if (progressBar.getIndeterminate() == false) { //determinate mode
230
fillBar.setAttribute(HTML.CLASS_ATTR, progressBar.getFillClass());
231             fillBar.setAttribute(HTML.STYLE_ATTR, "position:absolute;width:0%");
232         } else {// indeterminate mode
233
fillBar.setAttribute(HTML.CLASS_ATTR,
234                     progressBar.getIndeterminateInactiveClass());
235             fillBar.setAttribute(HTML.STYLE_ATTR,
236                                  "position:absolute;width:100%;");
237         }
238
239
240         bgBar.appendChild(fillBar);
241         Text JavaDoc nbsp4mozila = domContext.createTextNode(space);
242
243         barTd.appendChild(bgBar);
244
245         progressBar.setTextNode(percentageText);
246         progressBar.setBarNode(bgBar);
247
248         String JavaDoc textPosition = progressBar.getLabelPosition();
249
250         if (!isValidTextPosition(textPosition.toString().toLowerCase())) {
251             throw new FacesException(
252                     "Please define valid textPosition [top|bottom|left|right|topcenter|bottomcenter|topright|bottomright|embed]");
253         }
254
255         if (textPosition.toString().equalsIgnoreCase("left")) {
256             textTd.setAttribute("style", "vertical-align: middle;");
257             row.appendChild(textTd);
258             row.appendChild(barTd);
259         }
260         if (textPosition.toString().equalsIgnoreCase("right")) {
261             textTd.setAttribute("style", "vertical-align: middle;");
262             row.appendChild(barTd);
263             row.appendChild(textTd);
264         }
265
266         if (textPosition.toString().toLowerCase().startsWith("top")) {
267             Element JavaDoc row2 = domContext.createElement(HTML.TR_ELEM);
268             row.appendChild(textTd);
269             row2.appendChild(barTd);
270             tbody.appendChild(row2);
271             if (textPosition.toString().equalsIgnoreCase("topcenter")) {
272                 textTd.setAttribute("align", "center");
273             }
274             if (textPosition.toString().equalsIgnoreCase("topright")) {
275                 textTd.setAttribute("align", "right");
276             }
277         }
278
279         if (textPosition.toString().toLowerCase().startsWith("bottom")) {
280             Element JavaDoc row2 = domContext.createElement(HTML.TR_ELEM);
281             row.appendChild(barTd);
282             row2.appendChild(textTd);
283             tbody.appendChild(row2);
284             if (textPosition.toString().equalsIgnoreCase("bottomcenter")) {
285                 textTd.setAttribute("align", "center");
286             }
287             if (textPosition.toString().equalsIgnoreCase("bottomright")) {
288                 textTd.setAttribute("align", "right");
289             }
290         }
291
292         if (textPosition.toString().equalsIgnoreCase("embed")) {
293             Element JavaDoc embedDiv = domContext.createElement(HTML.DIV_ELEM);
294             embedDiv.setAttribute(HTML.CLASS_ATTR,
295                     progressBar.getTextClass());
296             embedDiv.setAttribute(HTML.STYLE_ATTR,
297                                   "text-align:center;position:relative;background-color:transparent;width:100%;z-index:1;");
298             embedDiv.appendChild(percentageText);
299
300             if (progressBar.getIndeterminate() == false) {//determinate mode
301
bgBar.appendChild(embedDiv);
302             } else {//indeterminate mode
303
fillBar.appendChild(embedDiv);
304             }
305             row.appendChild(barTd);
306         } else {
307             //&nbsp fix for mozila
308
Text JavaDoc nbsp4opera = domContext.createTextNode(space);
309             //&nbsp fix for opera
310
fillBar.appendChild(nbsp4opera);
311             bgBar.appendChild(nbsp4mozila);
312         }
313     }
314
315     private boolean isValidTextPosition(String JavaDoc textPosition) {
316         String JavaDoc[] validPosition = {"top", "bottom", "left", "right", "topcenter",
317                                   "bottomcenter", "topright", "bottomright",
318                                   "embed"};
319         for (int i = 0; i < validPosition.length; i++) {
320             if (validPosition[i].equals(textPosition)) {
321                 return true;
322             }
323         }
324         return false;
325     }
326 }
327
Popular Tags