1 18 package org.apache.beehive.netui.tags.tree; 19 20 import org.apache.beehive.netui.tags.HtmlUtils; 21 import org.apache.beehive.netui.tags.html.IUrlParams; 22 import org.apache.beehive.netui.util.ParamHelper; 23 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 import java.io.Serializable ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 30 33 public class TreeElement implements IUrlParams, Serializable 34 { 35 private static final int EXPANDED = 0x01; 39 private static final int SELECTED = 0x02; 40 private static final int DISABLED = 0x04; 41 private static final int EXPAND_ON_SERVER = 0x08; 42 43 public static final String SELECTED_NODE = "netui_treeselected"; 44 public static final String EXPAND_NODE = "netui_treenode"; 45 public static final String TREE_ID = "netui_treeid"; 46 public static final String TREE_JAVASCRIPT_CLASS = "NetUITree"; 47 48 51 public static final String TREE_ANCHOR = "netui:treeAnchor"; 52 public static final String TREE_ANCHOR_INIT = "netui:treeAnchorInit"; 53 public static final String TREE_ANCHOR_ID = "netui:treeId"; 54 public static final String TREE_EXPAND = "netui:expandOnServer"; 55 public static final String TREE_EXPAND_PATH = "netui:expandPath"; 56 public static final String TREE_NODE_LAST = "netui:expandLast"; 57 public static final String TREE_EXPAND_STATE = "expand"; 58 public static final String TREE_COLLAPSE_STATE = "collapse"; 59 public static final String TREE_LEVEL = "netui:treeLevel"; 60 61 public static final String TREE_EXPAND_IMAGE = "netui:imageExpand"; 62 public static final String TREE_COLLAPSE_IMAGE = "netui:imageCollapse"; 63 64 67 public static final String TREE_EXPANDED = "expand"; 68 69 private static final TreeElement[] EMPTY_TREE = new TreeElement[0]; 71 72 private String _content; private String _label; private Boolean _labelAsAlt; private String _title; 76 private String _name; private int _boolState; 79 private TreeElement _parent; private ArrayList _children; public ArrayList _attribute; 83 private ExtendedInfo _info; 85 88 public TreeElement() 89 { 90 _name = "0"; 91 } 92 93 97 public TreeElement(String label, boolean expanded) 98 { 99 this(); 100 _label = label; 101 setExpanded(expanded); 102 } 103 104 106 111 public String getIcon() 112 { 113 return (_info == null) ? null : _info._icon; 114 } 115 116 121 public void setIcon(String icon) 122 { 123 ExtendedInfo info = getInfo(icon); 124 if (info != null) 125 info._icon = icon; 126 } 127 128 132 public String getLabel() 133 { 134 return _label; 135 } 136 137 141 public void setLabel(String label) 142 { 143 _label = label; 144 } 145 146 public boolean isLabelLegalAsAlt() 147 { 148 if (_labelAsAlt == null) 149 _labelAsAlt = new Boolean (!HtmlUtils.containsHtml(_label)); 150 return _labelAsAlt.booleanValue(); 151 } 152 153 158 public String getContent() 159 { 160 return _content; 161 } 162 163 167 public void setContent(String content) 168 { 169 _content = content; 170 } 171 172 177 public String getAction() 178 { 179 return (_info == null) ? null : _info._action; 180 } 181 182 187 public void setAction(String action) 188 { 189 ExtendedInfo info = getInfo(action); 190 if (info != null) 191 info._action = action; 192 } 193 194 198 public String getClientAction() 199 { 200 return (_info == null) ? null : _info._clientAction; 201 } 202 203 208 public void setClientAction(String clientAction) 209 { 210 ExtendedInfo info = getInfo(clientAction); 211 if (info != null) 212 info._clientAction = clientAction; 213 } 214 215 222 public String getTagId() 223 { 224 return (_info == null) ? null : _info._tagId; 225 } 226 227 231 public void setTagId(String tagId) 232 { 233 ExtendedInfo info = getInfo(tagId); 234 if (info != null) 235 info._tagId = tagId; 236 } 237 238 243 public String getHref() 244 { 245 return (_info == null) ? null : _info._href; 246 } 247 248 253 public void setHref(String href) 254 { 255 ExtendedInfo info = getInfo(href); 256 if (info != null) 257 info._href = href; 258 } 259 260 265 public String getTarget() 266 { 267 return (_info == null) ? null : _info._target; 268 } 269 270 274 public void setTarget(String target) 275 { 276 ExtendedInfo info = getInfo(target); 277 if (info != null) 278 info._target = target; 279 } 280 281 285 public boolean isExpanded() 286 { 287 return ((_boolState & EXPANDED) != 0); 288 } 289 290 294 public void setExpanded(boolean expanded) 295 { 296 if (expanded) { 297 _boolState = _boolState | EXPANDED; 298 } 299 else 300 _boolState = _boolState & (-1 ^ EXPANDED); 301 } 302 303 307 public boolean isExpandOnServer() 308 { 309 return ((_boolState & EXPAND_ON_SERVER) != 0); 310 } 311 312 318 public void setExpandOnServer(boolean expandOnServer) 319 { 320 if (expandOnServer) 321 _boolState = _boolState | EXPAND_ON_SERVER; 322 else 323 _boolState = _boolState & (-1 ^ EXPAND_ON_SERVER); 324 } 325 326 331 public String getScope() 332 { 333 return (_info == null) ? null : _info._scope; 334 } 335 336 345 public void setScope(String scope) 346 { 347 ExtendedInfo info = getInfo(scope); 348 if (info != null) 349 info._scope = scope; 350 } 351 352 356 public String getTitle() 357 { 358 return _title; 359 } 360 361 365 public void setTitle(String title) 366 { 367 _title = title; 368 } 369 370 public InheritableState getInheritableState() 371 { 372 return (_info == null) ? null : _info._state; 373 } 374 375 public void setInheritableState(InheritableState state) 376 { 377 ExtendedInfo info = getInfo(state); 378 if (info != null) { 379 info._state = state; 380 } 381 } 382 383 public void addParameter(String name, Object value, String facet) 384 { 385 ExtendedInfo info = getInfo(this); 387 if (info._params == null) { 388 info._params = new HashMap (); 389 } 390 ParamHelper.addParam(info._params, name, value); 391 } 392 393 public HashMap getParams() 394 { 395 return (_info == null) ? null : _info._params; 396 } 397 398 400 405 public boolean isLast() 406 { 407 if (_parent == null) 408 return true; 409 int last = _parent.size() - 1; 410 assert(last >= 0); 411 return (_parent.getChild(last) == this); 412 } 413 414 public int getLevel() 415 { 416 TreeElement t = getParent(); 417 int level = 0; 418 while (t != null) { 419 t = t.getParent(); 420 level++; 421 } 422 return level; 423 } 424 425 429 public boolean isLeaf() 430 { 431 if (_children == null) 432 return true; 433 return (_children.size() < 1); 434 } 435 436 440 public String getName() 441 { 442 return _name; 443 } 444 445 450 public TreeElement getParent() 451 { 452 return _parent; 453 } 454 455 459 public boolean isSelected() 460 { 461 return ((_boolState & SELECTED) != 0); 462 } 463 464 468 public void setSelected(boolean selected) 469 { 470 if (selected) 471 _boolState = _boolState | SELECTED; 472 else 473 _boolState = _boolState & (-1 ^ SELECTED); 474 } 475 476 480 public boolean isDisabled() 481 { 482 return ((_boolState & DISABLED) != 0); 483 } 484 485 489 public void setDisabled(boolean disabled) 490 { 491 if (disabled) 492 _boolState = _boolState | DISABLED; 493 else 494 _boolState = _boolState & (-1 ^ DISABLED); 495 } 496 497 502 public void addAttribute(TreeHtmlAttributeInfo attr) 503 { 504 if (_attribute == null) { 505 _attribute = new ArrayList (); 506 } 507 attr.setParent(this); 508 _attribute.add(attr); 509 } 510 511 516 public ArrayList getAttributeList() 517 { 518 return _attribute; 519 } 520 521 527 protected void setName(String name) 528 { 529 _name = name; 530 } 531 532 537 protected void setParent(TreeElement parent) 538 { 539 _parent = parent; 540 } 541 542 544 547 public void onExpand(ServletRequest request, ServletResponse response) 548 { 549 } 551 552 555 public void onSelect(ServletRequest request) 556 { 557 } 559 560 562 568 public void addChild(TreeElement child) 569 throws IllegalArgumentException 570 { 571 572 TreeElement theChild = child; 573 theChild.setParent(this); 574 if (getName() == null) { 575 setName("0"); 576 } 577 if (_children == null) { 578 _children = new ArrayList (); 579 } 580 _children.add(child); 587 int n = _children.size(); 588 theChild.updateName(this, n - 1); 589 } 590 591 595 public int size() 596 { 597 if (_children == null) 598 return 0; 599 return _children.size(); 600 } 601 602 610 public void addChild(int offset, TreeElement child) 611 throws IllegalArgumentException 612 { 613 child.setSelected(false); 614 child.setParent(this); 615 if (_children == null) 616 _children = new ArrayList (); 617 _children.add(offset, child); 618 619 int size = _children.size(); 621 for (int i = offset; i < size; i++) { 622 TreeElement thisChild = (TreeElement) _children.get(i); 623 thisChild.updateName(this, i); 624 } 625 626 } 627 628 631 public void clearChildren() 632 { 633 _children.clear(); 634 } 635 636 640 public TreeElement[] getChildren() 641 { 642 if (_children == null) 643 return EMPTY_TREE; 644 TreeElement results[] = new TreeElement[_children.size()]; 645 return (TreeElement[]) _children.toArray(results); 646 } 647 648 653 public TreeElement getChild(int index) 654 { 655 TreeElement childNode = null; 656 if (_children == null) 657 return null; 658 Object child = _children.get(index); 659 if (child != null) { 660 childNode = (TreeElement) child; 661 } 662 return childNode; 663 } 664 665 671 public void removeChild(int offset) 672 { 673 if (_children == null) 674 return; 675 676 TreeElement child = (TreeElement)_children.remove(offset); 677 child.setParent(null); 678 child.setName(null); 679 680 int size = _children.size(); 682 for (int i = offset; i < size; i++) { 683 TreeElement thisChild = (TreeElement) _children.get(i); 684 thisChild.updateName(this, i); 685 } 686 } 687 688 693 public void removeChild(TreeElement child) 694 { 695 696 if (child == null || _children == null) 697 return; 698 699 int size = _children.size(); 700 int removeIndex = -1; 701 for (int i = 0; i < size; i++) { 702 if (child == (TreeElement) _children.get(i)) { 703 _children.remove(i); 704 child.setParent(null); 705 removeIndex = i; 706 break; 707 } 708 } 709 if (removeIndex >= 0) { 710 size = _children.size(); 711 for (int i = removeIndex; i < size; i++) { 712 TreeElement thisChild = (TreeElement) _children.get(i); 713 thisChild.updateName(this, i); 714 } 715 } 716 } 717 718 724 protected void updateName(TreeElement parentNode, int index) 725 { 726 setName(getNodeName(parentNode, index)); 727 TreeElement[] children = getChildren(); 728 for (int i = 0; i < children.length; i++) { 729 children[i].updateName(this, i); 730 } 731 } 732 733 739 public static TreeElement getRoot(TreeElement node) 740 { 741 TreeElement parentNode = node.getParent(); 742 while (parentNode != null) { 743 node = parentNode; 744 parentNode = node.getParent(); 745 } 746 return node; 747 } 748 749 754 public TreeElement findNode(String nodeName) 755 { 756 TreeElement root = getRoot(this); 757 TreeElement node = root.findNodeRecurse(nodeName, nodeName); 758 return node; 759 } 760 761 766 private String getNodeName(TreeElement parentNode, int index) 767 { 768 String nodeName = "" + index; 769 if (parentNode != null) { 770 nodeName = parentNode.getName() + "." + nodeName; 771 } 772 773 return nodeName; 774 } 775 776 782 private TreeElement findNodeRecurse(String fullName, String currentName) 783 { 784 String remainingName = null; 785 786 if ((currentName == null) || (fullName == null)) { 787 return null; 788 } 789 if (getName().equals(fullName)) { 790 return this; 791 } 792 if (currentName.indexOf('.') > 0) { 793 remainingName = currentName.substring(currentName.indexOf('.') + 1); 794 int nextIndex = -1; 795 if (remainingName.indexOf(".") > -1) { 796 nextIndex = new Integer (remainingName.substring(0, remainingName.indexOf('.'))).intValue(); 797 } 798 else { 799 nextIndex = new Integer (remainingName).intValue(); 800 } 801 802 TreeElement child = getChild(nextIndex); 803 if (child != null) { 804 return child.findNodeRecurse(fullName, remainingName); 805 } 806 else { 807 return null; 808 } 809 } 810 return null; 811 } 812 813 819 private ExtendedInfo getInfo(Object o) 820 { 821 if (_info == null && o != null) { 822 _info = new ExtendedInfo(); 823 } 824 return _info; 825 } 826 827 832 private static class ExtendedInfo implements Serializable 833 { 834 public String _clientAction; public String _href; public String _scope; public String _icon; public String _tagId; public String _target; public String _action; public HashMap _params; public InheritableState _state; } 844 } 845 | Popular Tags |