| 1 33 34 package com.icesoft.faces.component.panelseries; 35 36 import com.icesoft.faces.component.tree.TreeDataModel; 37 38 import javax.faces.application.FacesMessage; 39 import javax.faces.component.EditableValueHolder; 40 import javax.faces.component.NamingContainer; 41 import javax.faces.component.UIComponent; 42 import javax.faces.component.UIData; 43 import javax.faces.component.html.HtmlDataTable; 44 import javax.faces.component.html.HtmlForm; 45 import javax.faces.context.FacesContext; 46 import javax.faces.el.ValueBinding; 47 import javax.faces.event.AbortProcessingException; 48 import javax.faces.event.FacesEvent; 49 import javax.faces.event.FacesListener; 50 import javax.faces.event.PhaseId; 51 import javax.faces.model.ArrayDataModel; 52 import javax.faces.model.DataModel; 53 import javax.faces.model.ListDataModel; 54 import javax.faces.model.ResultDataModel; 55 import javax.faces.model.ResultSetDataModel; 56 import javax.faces.model.ScalarDataModel; 57 import javax.servlet.jsp.jstl.sql.Result; 58 import javax.swing.tree.TreeModel ; 59 import java.io.IOException ; 60 import java.io.Serializable ; 61 import java.sql.ResultSet ; 62 import java.util.Collections ; 63 import java.util.HashMap ; 64 import java.util.Iterator ; 65 import java.util.List ; 66 import java.util.Map ; 67 68 69 74 public class UISeries extends HtmlDataTable { 75 public static final String COMPONENT_TYPE = "com.icesoft.faces.series"; 76 public static final String RENDERER_TYPE = "com.icesoft.faces.seriesRenderer"; 77 protected transient DataModel dataModel = null; 78 private int rowIndex = -1; 79 protected Map savedChildren = new HashMap (); 80 private String var = null; 81 82 83 public UISeries() { 84 super(); 85 setRendererType(RENDERER_TYPE); 86 } 87 88 91 public boolean isRowAvailable() { 92 return (getDataModel().isRowAvailable()); 93 } 94 95 96 99 public int getRowCount() { 100 return (getDataModel().getRowCount()); 101 } 102 103 104 107 public Object getRowData() { 108 return (getDataModel().getRowData()); 109 } 110 111 112 115 public int getRowIndex() { 116 return (this.rowIndex); 117 } 118 119 public void setRowIndex(int rowIndex) { 120 FacesContext facesContext = getFacesContext(); 121 saveChildrenState(facesContext); 123 processCurrentRowData(facesContext, rowIndex); 125 restoreChildrenState(facesContext); 127 } 128 129 private void processCurrentRowData(FacesContext facesContext, 130 int rowIndex) { 131 this.rowIndex = rowIndex; 132 DataModel model = getDataModel(); 133 model.setRowIndex(rowIndex); 134 135 if (var != null) { 136 Map requestMap = facesContext.getExternalContext().getRequestMap(); 137 if (rowIndex == -1) { 138 loadRowToRequestMap(requestMap, false); 139 } else if (isRowAvailable()) { 140 loadRowToRequestMap(requestMap, true); 141 } else { 142 loadRowToRequestMap(requestMap, false); 143 } 144 } 145 } 146 147 private void loadRowToRequestMap(Map requestMap, boolean loadRow) { 148 if (loadRow) { 149 requestMap.put(var, getRowData()); 150 } else { 151 requestMap.remove(var); 152 } 153 } 154 155 156 159 public String getVar() { 160 return (this.var); 161 } 162 163 164 167 public void setVar(String var) { 168 this.var = var; 169 } 170 171 174 public void setValue(Object value) { 175 this.dataModel = null; 176 super.setValue(value); 177 } 178 179 180 public void setValueBinding(String name, ValueBinding binding) { 181 if ("value".equals(name)) { 182 this.dataModel = null; 183 } else if ("var".equals(name) || "rowIndex".equals(name)) { 184 throw new IllegalArgumentException (); 185 } 186 super.setValueBinding(name, binding); 187 } 188 189 190 193 public String getClientId(FacesContext context) { 194 if (context == null) { 195 throw new NullPointerException (); 196 } 197 String baseClientId = super.getClientId(context); 198 if (getRowIndex() >= 0) { 199 if (!baseClientId.endsWith( 203 "" + NamingContainer.SEPARATOR_CHAR + getRowIndex())) { 204 return (baseClientId + NamingContainer.SEPARATOR_CHAR + 205 getRowIndex()); 206 } 207 return (baseClientId); 208 } else { 209 return (baseClientId); 210 } 211 } 212 213 214 217 public void queueEvent(FacesEvent event) { 218 super.queueEvent(new RowEvent(this, event, getRowIndex())); 219 } 220 221 222 225 public void broadcast(FacesEvent event) throws AbortProcessingException { 226 if (!(event instanceof RowEvent)) { 227 super.broadcast(event); 228 return; 229 } 230 231 ((RowEvent) event).broadcast(); 233 return; 234 } 235 236 237 240 public void encodeBegin(FacesContext context) throws IOException { 241 dataModel = null; 242 if (!keepSaved(context)) { 243 savedChildren = new HashMap (); 244 } 245 super.encodeBegin(context); 246 } 247 248 249 public void processDecodes(FacesContext context) { 250 if (context == null) { 251 throw new NullPointerException (); 252 } 253 if (!isRendered()) { 254 return; 255 } 256 257 dataModel = null; 258 if (null == savedChildren || !keepSaved(context)) { 259 savedChildren = new HashMap (); 260 } 261 262 iterate(context, PhaseId.APPLY_REQUEST_VALUES); 263 decode(context); 264 } 265 266 public void processValidators(FacesContext context) { 267 if (context == null) { 268 throw new NullPointerException (); 269 } 270 if (!isRendered()) { 271 return; 272 } 273 if (isNestedWithinUIData()) { 274 dataModel = null; 275 } 276 iterate(context, PhaseId.PROCESS_VALIDATIONS); 277 } 278 279 280 public void processUpdates(FacesContext context) { 281 if (context == null) { 282 throw new NullPointerException (); 283 } 284 if (!isRendered()) { 285 return; 286 } 287 if (isNestedWithinUIData()) { 288 dataModel = null; 289 } 290 iterate(context, PhaseId.UPDATE_MODEL_VALUES); 291 } 292 293 299 private DataModel getDataModel() { 300 if (null != this.dataModel) { 301 return (dataModel); 302 } 303 304 Object currentValue = getValue(); 305 306 if (null == currentValue) { 307 this.dataModel = new ListDataModel(Collections.EMPTY_LIST); 308 } else if (currentValue instanceof DataModel) { 309 this.dataModel = (DataModel) currentValue; 310 } else if (currentValue instanceof List ) { 311 this.dataModel = new ListDataModel((List ) currentValue); 312 } else if (Object [].class.isAssignableFrom(currentValue.getClass())) { 313 this.dataModel = new ArrayDataModel((Object []) currentValue); 314 } else if (currentValue instanceof ResultSet ) { 315 this.dataModel = new ResultSetDataModel((ResultSet ) currentValue); 316 } else if (currentValue instanceof Result) { 317 this.dataModel = new ResultDataModel((Result) currentValue); 318 } else if (currentValue instanceof TreeModel ) { 319 this.dataModel = new TreeDataModel((TreeModel ) currentValue); 320 } else { 321 this.dataModel = new ScalarDataModel(currentValue); 322 } 323 324 return (dataModel); 325 } 326 327 protected void iterate(FacesContext facesContext, PhaseId phase) { 328 setRowIndex(-1); 330 331 int rowsProcessed = 0; 332 int currentRowIndex = getFirst() - 1; 333 int displayedRows = getRows(); 334 while (1 == 1) { 336 if ((displayedRows > 0) && (++rowsProcessed > displayedRows)) { 338 break; 339 } 340 setRowIndex(++currentRowIndex); 342 if (!isRowAvailable()) { 344 break; 345 } 346 Iterator children = getFacetsAndChildren(); 348 while (children.hasNext()) { 349 UIComponent child = (UIComponent) children.next(); 350 if (phase == PhaseId.APPLY_REQUEST_VALUES) { 351 child.processDecodes(facesContext); 352 } else if (phase == PhaseId.PROCESS_VALIDATIONS) { 353 child.processValidators(facesContext); 354 } else if (phase == PhaseId.UPDATE_MODEL_VALUES) { 355 child.processUpdates(facesContext); 356 } else { 357 throw new IllegalArgumentException (); 358 } 359 } 360 } 361 362 setRowIndex(-1); 364 } 365 366 373 private boolean keepSaved(FacesContext facesContext) { 374 Iterator childIds = savedChildren.keySet().iterator(); 375 while (childIds.hasNext()) { 376 String childId = (String ) childIds.next(); 377 Iterator childMessages = facesContext.getMessages(childId); 378 while (childMessages.hasNext()) { 379 FacesMessage childMessage = (FacesMessage) childMessages.next(); 380 if (childMessage.getSeverity() 381 .compareTo(FacesMessage.SEVERITY_ERROR) >= 0) { 382 return (true); 383 } 384 } 385 } 386 return (isNestedWithinUIData()); 388 } 389 390 private boolean isNestedWithinUIData() { 391 UIComponent parent = this; 392 while (null != (parent = parent.getParent())) { 393 if (parent instanceof UIData) { 394 return true; 395 } 396 } 397 return (false); 398 } 399 400 401 protected void restoreChildrenState(FacesContext facesContext) { 402 Iterator children = getFacetsAndChildren(); 403 while (children.hasNext()) { 404 UIComponent child = (UIComponent) children.next(); 405 restoreChildState(facesContext, child); 406 } 407 } 408 409 410 protected void restoreChildState(FacesContext facesContext, 411 UIComponent component) { 412 String id = component.getId(); 413 if (!isValid(id)) { 414 return; 415 } 416 component.setId(id); 417 restoreChild(facesContext, component); 418 Iterator children = component.getFacetsAndChildren(); 419 while (children.hasNext()) { 420 restoreChildState(facesContext, (UIComponent) children.next()); 421 } 422 } 423 424 protected void saveChild(FacesContext facesContext, UIComponent component) { 425 if (component instanceof EditableValueHolder) { 426 EditableValueHolder input = (EditableValueHolder) component; 427 String clientId = component.getClientId(facesContext); 428 ChildState state = (ChildState) savedChildren.get(clientId); 429 if (state == null) { 430 state = new ChildState(); 431 savedChildren.put(clientId, state); 432 } 433 state.setValue(input.getLocalValue()); 434 state.setValid(input.isValid()); 435 state.setSubmittedValue(input.getSubmittedValue()); 436 state.setLocalValueSet(input.isLocalValueSet()); 437 } else if (component instanceof HtmlForm) { 438 String clientId = component.getClientId(facesContext); 439 Boolean isThisFormSubmitted = (Boolean ) savedChildren.get(clientId); 440 if (isThisFormSubmitted == null) { 441 isThisFormSubmitted = 442 new Boolean (((HtmlForm) component).isSubmitted()); 443 savedChildren.put(clientId, isThisFormSubmitted); 444 } 445 } 446 } 447 448 protected void restoreChild(FacesContext facesContext, 449 UIComponent component) { 450 if (component instanceof EditableValueHolder) { 451 EditableValueHolder input = (EditableValueHolder) component; 452 String clientId = component.getClientId(facesContext); 453 ChildState state = (ChildState) savedChildren.get(clientId); 454 if (state == null) { 455 state = new ChildState(); 456 } 457 input.setValue(state.getValue()); 458 input.setValid(state.isValid()); 459 input.setSubmittedValue(state.getSubmittedValue()); 460 input.setLocalValueSet(state.isLocalValueSet()); 461 } else if (component instanceof HtmlForm) { 462 String clientId = component.getClientId(facesContext); 463 Boolean isThisFormSubmitted = (Boolean ) savedChildren.get(clientId); 464 if (isThisFormSubmitted == null) { 465 isThisFormSubmitted = Boolean.FALSE; 466 } 467 ((HtmlForm) component) 468 .setSubmitted(isThisFormSubmitted.booleanValue()); 469 } 470 } 471 472 protected void saveChildrenState(FacesContext facesContext) { 473 Iterator children = getFacetsAndChildren(); 474 while (children.hasNext()) { 475 UIComponent child = (UIComponent) children.next(); 476 saveChildState(facesContext, child); 477 } 478 } 479 480 protected void saveChildState(FacesContext facesContext, 481 UIComponent component) { 482 saveChild(facesContext, component); 483 Iterator children = component.getFacetsAndChildren(); 484 while (children.hasNext()) { 485 saveChildState(facesContext, (UIComponent) children.next()); 486 } 487 } 488 489 class RowEvent extends FacesEvent { 491 private FacesEvent event = null; 492 private int eventRowIndex = -1; 493 494 public RowEvent(UIComponent component, FacesEvent event, 495 int eventRowIndex) { 496 super(component); 497 this.event = event; 498 this.eventRowIndex = eventRowIndex; 499 } 500 501 public FacesEvent getFacesEvent() { 502 return (this.event); 503 } 504 505 public boolean isAppropriateListener(FacesListener listener) { 506 return false; 507 } 508 509 public void processListener(FacesListener listener) { 510 throw new IllegalStateException (); 511 } 512 513 public PhaseId getPhaseId() { 514 return (this.event.getPhaseId()); 515 } 516 517 public void setPhaseId(PhaseId phaseId) { 518 this.event.setPhaseId(phaseId); 519 } 520 521 public void broadcast() { 522 int oldRowIndex = getRowIndex(); 523 setRowIndex(eventRowIndex); 524 event.getComponent().broadcast(event); 525 setRowIndex(oldRowIndex); 526 } 527 } 528 529 public Object saveState(FacesContext context) { 530 Object values[] = new Object [4]; 531 values[0] = super.saveState(context); 532 values[1] = new Integer (rowIndex); 533 values[2] = savedChildren; 534 values[3] = var; 535 return (values); 536 } 537 538 539 public void restoreState(FacesContext context, Object state) { 540 Object values[] = (Object []) state; 541 super.restoreState(context, values[0]); 542 rowIndex = ((Integer ) values[1]).intValue(); 543 savedChildren = (Map ) values[2]; 544 var = (String ) values[3]; 545 } 546 547 private boolean isValid(String id) { 548 if (id == null) { 549 return false; 550 } 551 if (!Character.isLetter(id.charAt(0)) && (id.charAt(0) != '_')) { 552 return false; 553 } 554 return true; 555 } 556 557 } 558 559 class ChildState implements Serializable { 560 561 private Object submittedValue; 562 private boolean valid = true; 563 private Object value; 564 private boolean localValueSet; 565 566 Object getSubmittedValue() { 567 return submittedValue; 568 } 569 570 void setSubmittedValue(Object submittedValue) { 571 this.submittedValue = submittedValue; 572 } 573 574 boolean isValid() { 575 return valid; 576 } 577 578 void setValid(boolean valid) { 579 this.valid = valid; 580 } 581 582 Object getValue() { 583 return value; 584 } 585 586 public void setValue(Object value) { 587 this.value = value; 588 } 589 590 boolean isLocalValueSet() { 591 return localValueSet; 592 } 593 594 public void setLocalValueSet(boolean localValueSet) { 595 this.localValueSet = localValueSet; 596 } 597 } 598 599 600 | Popular Tags |