1 142 package com.ivata.groupware.web.tag.webgui.tree; 143 144 import java.io.IOException ; 145 import java.util.List ; 146 import java.util.Properties ; 147 148 import javax.servlet.jsp.JspException ; 149 import javax.servlet.jsp.JspWriter ; 150 import javax.swing.tree.TreeModel ; 151 152 import com.ivata.groupware.web.tree.TreeNode; 153 import com.ivata.mask.util.CollectionHandling; 154 import com.ivata.mask.util.StringHandling; 155 import com.ivata.mask.web.tag.webgui.ControlTag; 156 import com.ivata.mask.web.theme.Theme; 157 158 159 288 public class TreeSelectTag extends ControlTag { 289 296 static final String childrenProperty = "treeChildren"; 297 300 private String controlName; 301 304 private String defaultCaption = null; 305 310 private String defaultValue = ""; 311 314 private Integer listSize = new Integer (15); 315 319 private Integer maxDepth = null; 320 323 private TreeModel model = null; 324 327 private String onChange = null; 328 331 private boolean readOnly; 332 335 private String selected = null; 336 339 private List selectedList = null; 340 343 private String treeName = null; 344 347 private String treeStyle = "combo"; 348 351 private boolean useJavaScript = false; 352 353 357 public TreeSelectTag() { 358 super(); 359 } 360 361 375 private void createChildren(final Object parent, 376 final JspWriter out, 377 final int actualDepth) throws JspException { 378 String prependString = ""; 379 for (int i=1; i<actualDepth; i++) { 380 if (useJavaScript) { 381 prependString += "- "; 382 } else { 383 prependString += "- "; 384 } 385 } 386 387 try { 388 int totalNodes = model.getChildCount(parent); 389 390 for (int nodeNumber = 0; nodeNumber < totalNodes; ++nodeNumber) { 391 TreeNode node = (TreeNode) model.getChild(parent, nodeNumber); 392 String javaSript = "javascript:document.frmFolder" + node.getId() + ".submit( )"; 393 String sSpaces; 395 396 if (useJavaScript) { 397 out.println(controlName + ".options[ " + controlName + ".length ] = new Option( \"" + prependString + node.getName() + "\", " + node.getId() + " );"); 398 if (isSelected(node.getId())) { 399 out.println(controlName + ".options[ " + controlName + ".length - 1 ].selected = true;"); 400 } 401 } else { 402 Theme theme = getTheme(); 403 Properties optionProperties = CollectionHandling.splice(getProperties(), new Properties ()); 404 405 optionProperties.setProperty("value", StringHandling.getNotNull(node.getId(), "null")); 406 if (isSelected(node.getId())) { 407 optionProperties.setProperty("selected", "selected"); 408 } 409 out.print(theme.parseSection("treeSelectOptionStart", optionProperties)); 410 out.print(prependString + node.getName()); 411 out.print(theme.parseSection("treeSelectOptionEnd", optionProperties)); 412 } 413 if ( ((this.maxDepth==null) || (this.maxDepth!=null && actualDepth < this.maxDepth.intValue())) && (model.getChildCount(node) > 0)) { 416 createChildren(node, out, actualDepth+1); 417 } 418 } 419 } catch (IOException e) { 420 throw new JspException (e); 421 } 422 } 423 424 440 public int doStartTag() throws JspException { 441 super.doStartTag(); 442 try { 443 JspWriter out = pageContext.getOut(); 445 446 if (useJavaScript) { 448 out.println(controlName + ".options.length=0;"); 449 } else { 450 Properties properties = CollectionHandling.splice(getProperties(), new Properties ()); 452 453 properties.setProperty("name", controlName); 454 if (readOnly) { 457 properties.setProperty("readOnly", "readOnly"); 458 } 459 if (onChange != null) { 461 properties.setProperty("onChange", onChange); 462 } 463 464 String styleId = getStyleId(); 465 if (styleId != null) { 466 properties.setProperty("styleId", styleId); 467 } 468 String style = getStyle(); 470 if (style != null) { 471 properties.setProperty("style", style); 472 } 473 String styleClass = getStyleClass(); 474 if (styleClass != null) { 475 properties.setProperty("styleClass", styleClass); 476 } 477 TreeNode root = (TreeNode) model.getRoot(); 478 479 if (treeStyle.equalsIgnoreCase("list")) { 481 properties.setProperty("multiple", "multiple"); 482 properties.setProperty("listSize", listSize.toString()); 483 } 484 Theme theme = getTheme(); 485 486 out.print(theme.parseSection("treeSelectStart", properties)); 487 if (defaultCaption != null) { 488 Properties optionProperties = CollectionHandling.splice(getProperties(), new Properties ()); 489 490 optionProperties.setProperty("value", defaultValue); 491 out.print(theme.parseSection("treeSelectOptionStart", optionProperties)); 492 out.print(defaultCaption); 493 out.print(theme.parseSection("treeSelectOptionEnd", optionProperties)); 494 } 495 createChildren(root, out, 1); 496 if (!useJavaScript) { 498 out.print(theme.parseSection("treeSelectEnd", properties)); 499 } 500 } 501 } catch (IOException ioException) { 502 throw new JspException ( 503 "Error in TreeSelectTag: IOException whilst printing select.", 504 ioException); 505 } catch (Exception e) { 506 throw new JspException ("Error in TreeSelectTag: unhandled exception of class '" 507 + e.getClass().getName() + "'", 508 e); 509 } 510 return SKIP_BODY; 512 } 513 514 523 public final String getControlName() { 524 return controlName; 525 } 526 527 538 public final String getDefaultCaption() { 539 return defaultCaption; 540 } 541 548 public final String getDefaultValue() { 549 return defaultValue; 550 } 551 552 561 public final Integer getListSize() { 562 return listSize; 563 } 564 565 572 public final Integer getMaxDepth() { 573 return this.maxDepth; 574 } 575 576 588 public final TreeModel getModel() { 589 return model; 590 } 591 592 602 public final String getOnChange() { 603 return onChange; 604 } 605 606 615 public final boolean getReadOnly() { 616 return readOnly; 617 } 618 619 624 public final String getSelected() { 625 return selected; 626 } 627 628 633 public List getSelectedList() { 634 return selectedList; 635 } 636 637 647 public final String getTreeName() { 648 return treeName; 649 } 650 651 663 public final String getTreeStyle() { 664 return treeStyle; 665 } 666 667 679 public final boolean getUseJavaScript() { 680 return useJavaScript; 681 } 682 683 695 private boolean isSelected(Integer integerParam) { 696 if (selectedList != null) { 697 return (selectedList.indexOf(id) != -1); 698 } else { 699 return StringHandling.getNotNull(id).equals(selected); 700 } 701 } 702 703 713 public final void setControlName(final String controlName) { 714 this.controlName = controlName; 715 } 716 717 729 public final void setDefaultCaption(final String defaultCaption) { 730 this.defaultCaption = defaultCaption; 731 } 732 739 public final void setDefaultValue(final String defaultValue) { 740 this.defaultValue = defaultValue; 741 } 742 743 753 public final void setListSize(final Integer listSize) { 754 this.listSize = listSize; 755 } 756 757 764 public final void setMaxDepth(final Integer maxDepth) { 765 this.maxDepth = maxDepth; 766 } 767 768 780 public final void setModel(final TreeModel model) { 781 this.model = model; 782 } 783 784 795 public final void setOnChange(final String onChange) { 796 this.onChange = onChange; 797 } 798 799 809 public final void setReadOnly(final boolean readOnly) { 810 this.readOnly = readOnly; 811 } 812 813 818 public final void setSelected(final String selected) { 819 this.selected = selected; 820 } 821 822 827 public final void setSelectedList(final List selectedList) { 828 this.selectedList = selectedList; 829 } 830 831 842 public final void setTreeName(final String treeName) { 843 this.treeName = treeName; 844 } 845 846 859 public final void setTreeStyle(final String treeStyle) { 860 this.treeStyle = treeStyle; 861 } 862 863 876 public final void setUseJavaScript(final boolean useJavaScript) { 877 this.useJavaScript = useJavaScript; 878 } 879 } 880 | Popular Tags |