1 33 34 package com.icesoft.faces.component.outputprogress; 35 36 import java.io.IOException ; 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 ; 43 import org.w3c.dom.Node ; 44 import org.w3c.dom.Text ; 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 { 58 59 60 validateParameters(facesContext, uiComponent, OutputProgress.class); 61 DOMContext domContext = 62 DOMContext.attachDOMContext(facesContext, uiComponent); 63 64 if (!domContext.isInitialized()) { 65 Element 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 table = (Element ) domContext.getRootNode(); 72 String 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 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 space = " "; 98 if (domContext.isStreamWriting()) { 99 space = ""; 100 } 101 102 OutputProgress progressBar = (OutputProgress) uiComponent; 103 104 Text percentageText = progressBar.getTextNode(); 105 Element bgBar = progressBar.getBarNode(); 106 Element fillBar = (Element ) bgBar.getFirstChild(); 107 108 String 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 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 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 progressCompleteLabel = progressBar.getProgressLabelComplete(); 154 if (progressCompleteLabel != null && progressCompleteLabel.length() > 0) { 155 percentageText.setData(progressCompleteLabel); 156 } 157 } 158 159 if (progressBar.getIndeterminate() == false) { 161 Node node = fillBar.getFirstChild(); 166 if (node instanceof Text ) { 167 if (percentValue <= 0) { 168 fillBar.removeChild(node); 169 } 170 } else if (node == null) { 171 if (percentValue > 0) { 172 Text nbsp4opera = domContext.createTextNode(" "); 173 fillBar.appendChild(nbsp4opera); 174 } 175 } 176 } 177 if (progressBar.getIndeterminate() == false) { 179 fillBar.setAttribute(HTML.STYLE_ATTR, "position:absolute;width:" + 180 percentValue + "%;"); 181 } 182 } 183 184 private void buildLayout(Element table, UIComponent uiComponent, 185 DOMContext domContext) { 186 String space = " "; 187 if (domContext.isStreamWriting()) { 188 space = ""; 189 } 190 Node node = table.getFirstChild(); 191 Element 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 row = domContext.createElement(HTML.TR_ELEM); 203 Element textTd = domContext.createElement(HTML.TD_ELEM); 204 textTd.setAttribute(HTML.CLASS_ATTR, progressBar.getTextClass()); 205 206 Element barTd = domContext.createElement(HTML.TD_ELEM); 207 tbody.appendChild(row); 208 Text percentageText = null; 209 if (progressBar.getProgressLabel() != null) { 210 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 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 fillBar = domContext.createElement(HTML.DIV_ELEM); 226 fillBar.setAttribute(HTML.ID_ATTR, uiComponent 227 .getClientId(FacesContext.getCurrentInstance()) + "bar"); 228 229 if (progressBar.getIndeterminate() == false) { fillBar.setAttribute(HTML.CLASS_ATTR, progressBar.getFillClass()); 231 fillBar.setAttribute(HTML.STYLE_ATTR, "position:absolute;width:0%"); 232 } else { 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 nbsp4mozila = domContext.createTextNode(space); 242 243 barTd.appendChild(bgBar); 244 245 progressBar.setTextNode(percentageText); 246 progressBar.setBarNode(bgBar); 247 248 String 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 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 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 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) { bgBar.appendChild(embedDiv); 302 } else { fillBar.appendChild(embedDiv); 304 } 305 row.appendChild(barTd); 306 } else { 307 Text nbsp4opera = domContext.createTextNode(space); 309 fillBar.appendChild(nbsp4opera); 311 bgBar.appendChild(nbsp4mozila); 312 } 313 } 314 315 private boolean isValidTextPosition(String textPosition) { 316 String [] 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 |