1 58 package org.apache.ecs.examples; 59 60 import java.io.*; 61 import java.util.*; 62 import javax.swing.tree.*; 63 import org.apache.ecs.xml.*; 64 import org.apache.ecs.html.*; 65 66 129 public class HtmlTree implements Serializable 130 { 131 136 protected int indentation = 2; 137 138 143 protected String path = ""; 144 145 150 protected String parameterName = "path"; 151 152 156 private int[] pathArray = new int[0]; 157 158 162 protected String leafIcon = ""; 163 164 168 protected String openIcon = ""; 169 170 174 protected String closedIcon = ""; 175 176 181 protected String action = "?"; 182 183 188 protected boolean rootVisible = true; 189 190 194 protected DefaultMutableTreeNode root = getDefaultTreeModel(); 195 196 200 protected DefaultMutableTreeNode displayNode = root; 201 202 205 public HtmlTree() 206 { 207 } 209 210 217 public HtmlTree(DefaultMutableTreeNode root) 218 { 219 setRoot(root); 220 } 221 222 229 public HtmlTree(DefaultTreeModel model) 230 { 231 setModel(model); 232 } 233 234 241 public void setRoot(DefaultMutableTreeNode root) 242 { 243 if (root == null) 245 { 246 root = getDefaultTreeModel(); 247 } 248 249 this.root = root; 251 } 252 253 259 public DefaultMutableTreeNode getRoot() 260 { 261 return root; 262 } 263 264 271 public void setModel(DefaultTreeModel model) 272 { 273 if (model != null) 274 { 275 setRoot((DefaultMutableTreeNode) 276 model.getRoot()); 277 } 278 else 279 { 280 setRoot(null); 281 } 282 } 283 284 290 public DefaultTreeModel getModel() 291 { 292 return new DefaultTreeModel(root); 293 } 294 295 301 public void setPath(String path) 302 { 303 try 304 { 305 pathArray = getPathResolved(path); 307 308 this.path = path; 310 } 311 catch (NumberFormatException nfe) 312 { 313 this.path = ""; 315 } 316 } 317 318 323 public String getPath() 324 { 325 return path; 326 } 327 328 335 public String getPath(int level) 336 { 337 if (level > pathArray.length) 339 { 340 level = pathArray.length; 341 } 342 343 StringBuffer autoPath = new StringBuffer (); 345 346 for (int i = 0; i < level; i++) 348 { 349 autoPath.append(pathArray[i]); 351 352 if (i < (level - 1)) 354 { 355 autoPath.append(":"); 356 } 357 } 358 359 return autoPath.toString(); 361 } 362 363 371 public static int[] getPathResolved(String path) 372 throws NumberFormatException 373 { 374 try 375 { 376 int[] returnArray = new int[0]; 378 379 StringTokenizer st = new StringTokenizer(path,":"); 381 382 returnArray = new int[st.countTokens()]; 384 385 if (st.countTokens() > 0) 387 { 388 for (int i = 0; i < returnArray.length; i++) 389 { 390 returnArray[i] = Integer.parseInt 391 (st.nextToken().trim()); 392 } 393 } 394 395 return returnArray; 397 } 398 catch (Exception e) 399 { 400 throw new NumberFormatException (e.getMessage()); 402 } 403 } 404 405 415 public void setParameterName(String parameterName) 416 { 417 if (parameterName == null 419 || parameterName.equals("")) 420 { 421 parameterName = "path"; 422 } 423 424 this.parameterName = parameterName; 426 } 427 428 435 public String getParameterName() 436 { 437 return parameterName; 438 } 439 440 446 public void setRootVisible(boolean rootVisible) 447 { 448 this.rootVisible = rootVisible; 449 } 450 451 456 public boolean isRootVisible() 457 { 458 return rootVisible; 459 } 460 461 467 public void setIndentation(int indentation) 468 { 469 this.indentation = indentation; 470 } 471 472 479 public int getIndentation() 480 { 481 return indentation; 482 } 483 484 489 public String getNodeName() 490 { 491 return displayNode.toString(); 492 } 493 494 500 public int getRowCount() 501 { 502 int rowCount = displayNode.getLevel(); 504 505 rowCount = rowCount + displayNode.getChildCount(); 507 508 if (rootVisible) 510 { 511 rowCount++; 512 } 513 514 return rowCount; 516 } 517 518 523 public void setOpenIcon(String openIcon) 524 { 525 this.openIcon = openIcon; 526 } 527 528 533 public void setClosedIcon(String closedIcon) 534 { 535 this.closedIcon = closedIcon; 536 } 537 538 543 public void setLeafIcon(String leafIcon) 544 { 545 this.leafIcon = leafIcon; 546 } 547 548 558 public void setAction(String action) 559 { 560 if (action == null) 562 { 563 action = ""; 564 } 565 566 if (action.indexOf("=") != -1) 570 { 571 action = action + "&"; 572 } 573 else 574 { 575 action = action + "?"; 576 } 577 578 this.action = action; 580 } 581 582 590 public String getImg(String icon) 591 { 592 XML img = new XML("img",false); 594 595 if (icon == null) 597 { 598 icon = ""; 599 } 600 601 img.addAttribute("src",icon); 603 604 return img.toString(); 606 } 607 608 614 public DefaultMutableTreeNode getRequestedNode() 615 { 616 displayNode = root; 618 619 for (int i = 0; i < pathArray.length; i++) 621 { 622 if (displayNode.getDepth() > 1) 623 { 624 displayNode = (DefaultMutableTreeNode) 625 displayNode.getChildAt(pathArray[i]); 626 } 627 } 628 629 return displayNode; 631 } 632 633 656 public String getHtml() 657 { 658 displayNode = getRequestedNode(); 660 661 Table table = new Table(); 663 table.setClass("tree"); 664 665 int autoIndentation = 0; 667 668 TreeNode ancestor = displayNode.getFirstChild(); 671 672 ArrayList list = new ArrayList(); 674 while ((ancestor = ancestor.getParent()) != null) 675 { 676 list.add(ancestor); 677 } 678 679 for (int i = list.size(); i > 0; i--) 681 { 682 DefaultMutableTreeNode parent = 684 (DefaultMutableTreeNode)list.get(i-1); 685 686 if (!parent.isRoot() || rootVisible) 689 { 690 TD td = new TD(); 692 td.setClass("parent"); 693 694 String href = action + parameterName 698 + "=" + getPath(parent.getLevel()-1); 699 700 for (int j = 0; j < autoIndentation; j++) 702 { 703 td.addElement(" "); 704 } 705 706 td.addElement(new A(href,getImg(openIcon))); 708 709 td.addElement(new NOBR(parent.toString())); 711 712 table.addElement(new TR(td)); 714 715 autoIndentation = autoIndentation + indentation; 717 } 718 } 719 720 for (int i = 0; i < displayNode.getChildCount(); i++) 722 { 723 TD td = new TD(); 725 td.setClass("child"); 726 727 DefaultMutableTreeNode child = 729 (DefaultMutableTreeNode)displayNode 730 .getChildAt(i); 731 732 String autoPath = getPath(child 734 .getLevel()-1) + ":" + i; 735 736 if (autoPath.startsWith(":")) 738 { 739 autoPath = autoPath.substring(1); 740 } 741 742 String href = action + parameterName 744 + "=" + autoPath; 745 746 for (int j = 0; j < autoIndentation; j++) 748 { 749 td.addElement(" "); 750 } 751 752 String img = getImg(leafIcon); 754 755 if (!child.isLeaf()) 757 { 758 img = getImg(closedIcon); 759 } 760 761 if (!child.isLeaf()) 763 { 764 td.addElement(new A(href,img)); 765 } 766 767 if (child.isLeaf()) 769 { 770 td.addElement(img); 771 } 772 773 td.addElement(new NOBR(child.toString())); 775 776 table.addElement(new TR(td)); 778 } 779 780 return table.toString(); 782 } 783 784 791 public String toString() 792 { 793 return getHtml(); 794 } 795 796 803 protected static DefaultMutableTreeNode getDefaultTreeModel() 804 { 805 DefaultMutableTreeNode root = 807 new DefaultMutableTreeNode("Root"); 808 809 for (int i = 1; i <= 5; i++) 811 { 812 DefaultMutableTreeNode folder = 814 new DefaultMutableTreeNode("Folder-" + i); 815 816 root.add(folder); 818 819 for (int j = 1; j <= 3; j++) 821 { 822 DefaultMutableTreeNode subfolder = 824 new DefaultMutableTreeNode("Subfolder-" + j); 825 826 folder.add(subfolder); 828 829 for (int k = 1; k <= 3; k++) 831 { 832 A a = new A("http://jakarta.apache.org"); 834 a.setTarget("target").addElement("Document-" + k); 835 836 DefaultMutableTreeNode document = 838 new DefaultMutableTreeNode(a.toString()); 839 840 subfolder.add(document); 842 } 843 } 844 } 845 846 return root; 848 } 849 850 863 public static String encodeToHtml(String in) 864 { 865 StringBuffer out = new StringBuffer (); 867 868 for (int i = 0; in != null && i < in.length(); i++) 870 { 871 char c = in.charAt(i); 873 874 if (c == '\'') 876 { 877 out.append("'"); 878 } 879 else if (c == '\"') 880 { 881 out.append("""); 882 } 883 else if (c == '<') 884 { 885 out.append("<"); 886 } 887 else if (c == '>') 888 { 889 out.append(">"); 890 } 891 else if (c == '&') 892 { 893 out.append("&"); 894 } 895 else 896 { 897 out.append(c); 898 } 899 } 900 901 return out.toString(); 903 } 904 } 905 | Popular Tags |