1 16 package org.apache.myfaces.custom.datascroller; 17 18 import org.apache.myfaces.renderkit.RendererUtils; 19 import org.apache.myfaces.renderkit.html.HtmlRenderer; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import javax.faces.application.Application; 25 import javax.faces.component.UIComponent; 26 import javax.faces.component.UIData; 27 import javax.faces.component.UIParameter; 28 import javax.faces.component.html.HtmlCommandLink; 29 import javax.faces.component.html.HtmlOutputText; 30 import javax.faces.context.FacesContext; 31 import javax.faces.context.ResponseWriter; 32 import java.io.IOException ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 59 public class HtmlDataScrollerRenderer 60 extends HtmlRenderer 61 { 62 private static final Log log = LogFactory.getLog(HtmlDataScrollerRenderer.class); 63 64 protected static final String FACET_FIRST = "first".intern(); 65 protected static final String FACET_PREVOIUS = "previous".intern(); 66 protected static final String FACET_NEXT = "next".intern(); 67 protected static final String FACET_LAST = "last".intern(); 68 protected static final String FACET_FAST_FORWARD = "fastf".intern(); 69 protected static final String FACET_FAST_REWIND = "fastr".intern(); 70 protected static final String PAGE_NAVIGATION = "idx".intern(); 71 72 public static final String RENDERER_TYPE = "org.apache.myfaces.DataScroller"; 73 74 public boolean getRendersChildren() 75 { 76 return true; 77 } 78 79 public void decode(FacesContext context, UIComponent component) 80 { 81 RendererUtils.checkParamValidity(context, component, HtmlDataScroller.class); 82 83 HtmlDataScroller scroller = (HtmlDataScroller)component; 84 85 UIData uiData = findUIData(scroller, component); 86 if (uiData == null) 87 { 88 return; 89 } 90 91 Map parameter = context.getExternalContext().getRequestParameterMap(); 92 String param = (String )parameter.get(component.getClientId(context)); 93 if (param != null) 94 { 95 if (param.equals(FACET_FIRST)) 96 { 97 uiData.setFirst(0); 98 } 99 else if (param.equals(FACET_PREVOIUS)) 100 { 101 int previous = uiData.getFirst() - uiData.getRows(); 102 if (previous >= 0) 103 uiData.setFirst(previous); 104 } 105 else if (param.equals(FACET_NEXT)) 106 { 107 int next = uiData.getFirst() + uiData.getRows(); 108 if (next < uiData.getRowCount()) 109 uiData.setFirst(next); 110 } 111 else if (param.equals(FACET_FAST_FORWARD)) 112 { 113 int fastStep = scroller.getFastStep(); 114 if (fastStep <= 0) 115 fastStep = 1; 116 int next = uiData.getFirst() + uiData.getRows() * fastStep; 117 int rowcount = uiData.getRowCount(); 118 if (next > rowcount) 119 next = (rowcount - 1) - ((rowcount - 1) % uiData.getRows()); 120 uiData.setFirst(next); 121 } 122 else if (param.equals(FACET_FAST_REWIND)) 123 { 124 int fastStep = scroller.getFastStep(); 125 if (fastStep <= 0) 126 fastStep = 1; 127 int previous = uiData.getFirst() - uiData.getRows() * fastStep; 128 if (previous < 0) 129 previous = 0; 130 uiData.setFirst(previous); 131 } 132 else if (param.equals(FACET_LAST)) 133 { 134 int rowcount = uiData.getRowCount(); 135 int rows = uiData.getRows(); 136 int delta = rowcount % rows; 137 int first = delta > 0 && delta < rows ? rowcount - delta : rowcount - rows; 138 if (first >= 0) 139 { 140 uiData.setFirst(first); 141 } 142 else 143 { 144 uiData.setFirst(0); 145 } 146 } 147 else if (param.startsWith(PAGE_NAVIGATION)) 148 { 149 int index = Integer.parseInt(param.substring(PAGE_NAVIGATION.length(), param.length())); 150 int pageCount = getPageCount(uiData); 151 if (index > pageCount) 152 { 153 index = pageCount; 154 } 155 else if (index <= 0) 156 { 157 index = 1; 158 } 159 uiData.setFirst(uiData.getRows() * (index - 1)); 160 } 161 } 162 } 163 164 165 public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) throws IOException 166 { 167 RendererUtils.checkParamValidity(facescontext, uicomponent, HtmlDataScroller.class); 168 169 Map requestMap = facescontext.getExternalContext().getRequestMap(); 170 HtmlDataScroller scroller = (HtmlDataScroller)uicomponent; 171 172 UIData uiData = findUIData(scroller, uicomponent); 173 if (uiData == null) 174 { 175 return; 176 } 177 178 179 180 String pageCountVar = scroller.getPageCountVar(); 181 if (pageCountVar != null) 182 { 183 int pageCount = getPageCount(uiData); 184 requestMap.put(pageCountVar, new Integer (pageCount)); 185 } 186 String pageIndexVar = scroller.getPageIndexVar(); 187 if (pageIndexVar != null) 188 { 189 int pageIndex = getPageIndex(uiData); 190 requestMap.put(pageIndexVar, new Integer (pageIndex)); 191 } 192 String rowsCountVar = scroller.getRowsCountVar(); 193 if (rowsCountVar != null) 194 { 195 int rowsCount = uiData.getRowCount(); 196 requestMap.put(rowsCountVar, new Integer (rowsCount)); 197 } 198 String displayedRowsCountVar = scroller.getDisplayedRowsCountVar(); 199 if (displayedRowsCountVar != null) 200 { 201 int displayedRowsCount = uiData.getRows(); 202 int max = uiData.getRowCount()-uiData.getFirst(); 203 if( displayedRowsCount > max ) 204 displayedRowsCount = max; 205 requestMap.put(displayedRowsCountVar, new Integer (displayedRowsCount)); 206 } 207 String firstRowIndexVar = scroller.getFirstRowIndexVar(); 208 if (firstRowIndexVar != null) 209 { 210 int firstRowIndex = uiData.getFirst()+1; 211 requestMap.put(firstRowIndexVar, new Integer (firstRowIndex)); 212 } 213 String lastRowIndexVar = scroller.getLastRowIndexVar(); 214 if (lastRowIndexVar != null) 215 { 216 int lastRowIndex = uiData.getFirst()+uiData.getRows(); 217 int count = uiData.getRowCount(); 218 if( lastRowIndex > count ) 219 lastRowIndex = count; 220 requestMap.put(lastRowIndexVar, new Integer (lastRowIndex)); 221 } 222 223 RendererUtils.renderChildren(facescontext, uicomponent); 224 225 if (pageCountVar != null) 226 { 227 requestMap.remove(pageCountVar); 228 } 229 if (pageIndexVar != null) 230 { 231 requestMap.remove(pageIndexVar); 232 } 233 if (rowsCountVar != null) 234 { 235 requestMap.remove(rowsCountVar); 236 } 237 if (displayedRowsCountVar != null) 238 { 239 requestMap.remove(displayedRowsCountVar); 240 } 241 if (firstRowIndexVar != null) 242 { 243 requestMap.remove(firstRowIndexVar); 244 } 245 if (lastRowIndexVar != null) 246 { 247 requestMap.remove(lastRowIndexVar); 248 } 249 } 250 251 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException 252 { 253 RendererUtils.checkParamValidity(facesContext, uiComponent, HtmlDataScroller.class); 254 255 ResponseWriter writer = facesContext.getResponseWriter(); 256 HtmlDataScroller scroller = (HtmlDataScroller)uiComponent; 257 258 UIData uiData = findUIData(scroller, uiComponent); 259 if (uiData == null) 260 { 261 return; 262 } 263 264 writer.startElement("table", scroller); 265 String styleClass = scroller.getStyleClass(); 266 if (styleClass != null) 267 { 268 writer.writeAttribute("class", styleClass, null); 269 } 270 String style = scroller.getStyle(); 271 if (style != null) 272 { 273 writer.writeAttribute("style", style, null); 274 } 275 writer.startElement("tr", scroller); 276 277 UIComponent facetComp = scroller.getFirst(); 278 if (facetComp != null) 279 { 280 writer.startElement("td", scroller); 281 renderFacet(facesContext, scroller, facetComp, FACET_FIRST); 282 writer.endElement("td"); 283 } 284 facetComp = scroller.getFastRewind(); 285 if (facetComp != null) 286 { 287 writer.startElement("td", scroller); 288 renderFacet(facesContext, scroller, facetComp, FACET_FAST_REWIND); 289 writer.endElement("td"); 290 } 291 facetComp = scroller.getPrevious(); 292 if (facetComp != null) 293 { 294 writer.startElement("td", scroller); 295 renderFacet(facesContext, scroller, facetComp, FACET_PREVOIUS); 296 writer.endElement("td"); 297 } 298 if (scroller.isPaginator()) 299 { 300 writer.startElement("td", scroller); 301 renderPaginator(facesContext, scroller, uiData); 302 writer.endElement("td"); 303 } 304 facetComp = scroller.getNext(); 305 if (facetComp != null) 306 { 307 writer.startElement("td", scroller); 308 renderFacet(facesContext, scroller, facetComp, FACET_NEXT); 309 writer.endElement("td"); 310 } 311 facetComp = scroller.getFastForward(); 312 if (facetComp != null) 313 { 314 writer.startElement("td", scroller); 315 renderFacet(facesContext, scroller, facetComp, FACET_FAST_FORWARD); 316 writer.endElement("td"); 317 } 318 facetComp = scroller.getLast(); 319 if (facetComp != null) 320 { 321 writer.startElement("td", scroller); 322 renderFacet(facesContext, scroller, facetComp, FACET_LAST); 323 writer.endElement("td"); 324 } 325 326 writer.endElement("tr"); 327 writer.endElement("table"); 328 } 329 330 private void renderFacet(FacesContext facesContext, 331 HtmlDataScroller scroller, 332 UIComponent facetComp, 333 String facetName) 334 throws IOException 335 { 336 UIComponent link = getLink(facesContext, scroller, facetComp, facetName); 337 link.encodeBegin(facesContext); 338 facetComp.encodeBegin(facesContext); 339 if (facetComp.getRendersChildren()) 340 facetComp.encodeChildren(facesContext); 341 facetComp.encodeEnd(facesContext); 342 link.encodeEnd(facesContext); 343 } 344 345 protected void renderPaginator(FacesContext facesContext, 346 HtmlDataScroller scroller, 347 UIData uiData) 348 throws IOException 349 { 350 ResponseWriter writer = facesContext.getResponseWriter(); 351 352 int maxPages = scroller.getPaginatorMaxPages(); 353 if (maxPages <= 1) 354 { 355 maxPages = 2; 356 } 357 int pageCount = getPageCount(uiData); 358 if (pageCount <= 1) 359 { 360 return; 361 } 362 int pageIndex = getPageIndex(uiData); 363 int delta = maxPages / 2; 364 365 int pages; 366 int start; 367 if (pageCount > maxPages && pageIndex > delta) 368 { 369 pages = maxPages; 370 start = pageIndex - pages / 2 - 1; 371 if (start + pages > pageCount) 372 { 373 start = pageCount - pages; 374 } 375 } 376 else 377 { 378 pages = pageCount < maxPages ? pageCount : maxPages; 379 start = 0; 380 } 381 382 writer.startElement("table", scroller); 383 384 String styleClass = scroller.getPaginatorTableClass(); 385 if (styleClass != null) 386 { 387 writer.writeAttribute("class", styleClass, null); 388 } 389 String style = scroller.getPaginatorTableStyle(); 390 if (style != null) 391 { 392 writer.writeAttribute("style", style, null); 393 } 394 395 writer.startElement("tr", scroller); 396 397 for (int i = start, size = start + pages; i < size; i++) 398 { 399 int idx = i + 1; 400 writer.startElement("td", scroller); 401 String cStyleClass; 402 String cStyle; 403 if (idx == pageIndex) 404 { 405 cStyleClass = scroller.getPaginatorActiveColumnClass(); 406 cStyle = scroller.getPaginatorActiveColumnStyle(); 407 } 408 else 409 { 410 cStyleClass = scroller.getPaginatorColumnClass(); 411 cStyle = scroller.getPaginatorColumnStyle(); 412 } 413 if (cStyleClass != null) 414 { 415 writer.writeAttribute("class", cStyleClass, null); 416 } 417 if (cStyle != null) 418 { 419 writer.writeAttribute("style", cStyle, null); 420 } 421 422 HtmlCommandLink link = getLink(facesContext, scroller, Integer.toString(idx), idx); 423 link.encodeBegin(facesContext); 424 link.encodeChildren(facesContext); 425 link.encodeEnd(facesContext); 426 427 writer.endElement("td"); 428 } 429 430 writer.endElement("tr"); 431 writer.endElement("table"); 432 } 433 434 protected HtmlCommandLink getLink(FacesContext facesContext, 435 HtmlDataScroller scroller, 436 String text, 437 int pageIndex) 438 { 439 String id = PAGE_NAVIGATION + Integer.toString(pageIndex); 440 Application application = facesContext.getApplication(); 441 442 HtmlCommandLink link = 443 (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE); 444 link.setId(scroller.getId() + id); 445 link.setTransient(true); 446 UIParameter parameter 447 = (UIParameter)application.createComponent(UIParameter.COMPONENT_TYPE); 448 parameter.setId(scroller.getId() + id + "_param"); 449 parameter.setTransient(true); 450 parameter.setName(scroller.getClientId(facesContext)); 451 parameter.setValue(id); 452 List children = link.getChildren(); 453 children.add(parameter); 454 if (text != null) 455 { 456 HtmlOutputText uiText = 457 (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE); 458 uiText.setTransient(true); 459 uiText.setValue(text); 460 children.add(uiText); 461 } 462 scroller.getChildren().add(link); 463 return link; 464 } 465 466 protected HtmlCommandLink getLink(FacesContext facesContext, 467 HtmlDataScroller scroller, 468 UIComponent facetComp, 469 String facetName) 470 { 471 Application application = facesContext.getApplication(); 472 473 HtmlCommandLink link 474 = (HtmlCommandLink)application.createComponent(HtmlCommandLink.COMPONENT_TYPE); 475 link.setId(scroller.getId() + facetName); 476 link.setTransient(true); 477 UIParameter parameter 478 = (UIParameter)application.createComponent(UIParameter.COMPONENT_TYPE); 479 parameter.setId(scroller.getId() + facetName + "_param"); 480 parameter.setTransient(true); 481 parameter.setName(scroller.getClientId(facesContext)); 482 parameter.setValue(facetName); 483 List children = link.getChildren(); 484 children.add(parameter); 485 if (facetComp != null) 486 children.add(facetComp); 487 scroller.getChildren().add(link); 489 return link; 490 } 491 492 protected int getPageIndex(UIData uiData) 493 { 494 int rows = uiData.getRows(); 495 int pageIndex; 496 if (rows > 0) 497 { 498 pageIndex = uiData.getFirst() / rows + 1; 499 } 500 else 501 { 502 log.warn("DataTable " + uiData.getClientId(FacesContext.getCurrentInstance()) + " has invalid rows attribute."); 503 pageIndex = 0; 504 } 505 if (uiData.getFirst() % rows > 0) 506 { 507 pageIndex++; 508 } 509 return pageIndex; 510 } 511 512 protected int getPageCount(UIData uiData) 513 { 514 int rows = uiData.getRows(); 515 int pageCount; 516 if (rows > 0) 517 { 518 pageCount = rows <= 0 ? 1 : uiData.getRowCount() / rows; 519 if (uiData.getRowCount() % rows > 0) 520 { 521 pageCount++; 522 } 523 } 524 else 525 { 526 rows = 1; 527 pageCount = 1; 528 } 529 return pageCount; 530 } 531 532 protected UIData findUIData(HtmlDataScroller scroller, UIComponent component) 533 { 534 String forStr = scroller.getFor(); 535 UIComponent forComp; 536 if (forStr == null) 537 { 538 forComp = component.getParent(); 540 } 541 else 542 { 543 forComp = component.findComponent(scroller.getFor()); 544 if (forComp == null) 545 { 546 log.warn("could not find UIData referenced by attribute dataScroller@for = '" + scroller.getFor() + "'"); 547 } 548 } 549 if (!(forComp instanceof UIData)) 550 { 551 throw new IllegalArgumentException ("uiComponent referenced by attribute tableScroller@for must be of type " + UIData.class.getName()); 552 } 553 return (UIData)forComp; 554 } 555 556 } 557 | Popular Tags |