1 24 package org.ofbiz.widget.tree; 25 26 import java.io.IOException ; 27 import java.io.StringWriter ; 28 import java.io.Writer ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.ListIterator ; 34 import java.util.Map ; 35 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import org.ofbiz.base.util.Debug; 40 import org.ofbiz.base.util.GeneralException; 41 import org.ofbiz.base.util.StringUtil; 42 import org.ofbiz.base.util.UtilFormatOut; 43 import org.ofbiz.base.util.UtilMisc; 44 import org.ofbiz.base.util.UtilValidate; 45 import org.ofbiz.base.util.UtilXml; 46 import org.ofbiz.base.util.collections.MapStack; 47 import org.ofbiz.base.util.string.FlexibleStringExpander; 48 import org.ofbiz.widget.screen.ModelScreen; 49 import org.ofbiz.widget.screen.ScreenFactory; 50 import org.ofbiz.widget.screen.ScreenStringRenderer; 51 import org.ofbiz.entity.GenericDelegator; 52 import org.ofbiz.entity.GenericEntityException; 53 import org.ofbiz.entity.GenericValue; 54 import org.ofbiz.entity.model.ModelEntity; 55 import org.ofbiz.entity.model.ModelField; 56 import org.ofbiz.entity.util.EntityListIterator; 57 import org.ofbiz.service.LocalDispatcher; 58 import org.w3c.dom.Element ; 59 import org.xml.sax.SAXException ; 60 61 63 70 public class ModelTree { 71 72 public static final String module = ModelTree.class.getName(); 73 74 protected String name; 75 protected String rootNodeName; 76 protected String defaultRenderStyle; 77 protected FlexibleStringExpander defaultWrapStyleExdr; 78 protected List nodeList = new ArrayList (); 79 protected Map nodeMap = new HashMap (); 80 protected GenericDelegator delegator; 81 protected LocalDispatcher dispatcher; 82 protected FlexibleStringExpander expandCollapseRequestExdr; 83 protected FlexibleStringExpander trailNameExdr; 84 protected List trail = new ArrayList (); 85 protected List currentNodeTrail; 86 protected int openDepth; 87 protected int postTrailOpenDepth; 88 protected int [] nodeIndices = new int[20]; 89 protected String defaultEntityName; 90 protected String defaultPkName; 91 protected boolean forceChildCheck; 92 93 95 96 97 public ModelTree() {} 98 99 public ModelTree(Element treeElement, GenericDelegator delegator, LocalDispatcher dispatcher) { 100 101 this.name = treeElement.getAttribute("name"); 102 this.rootNodeName = treeElement.getAttribute("root-node-name"); 103 this.defaultRenderStyle = UtilFormatOut.checkEmpty(treeElement.getAttribute("default-render-style"), "simple"); 104 if (UtilValidate.isEmpty(this.defaultRenderStyle) || this.defaultRenderStyle.equals("simple")) { 106 String rStyle = treeElement.getAttribute("render-style"); 107 if (UtilValidate.isNotEmpty(rStyle)) 108 this.defaultRenderStyle = rStyle; 109 } 110 this.defaultWrapStyleExdr = new FlexibleStringExpander(treeElement.getAttribute("default-wrap-style")); 111 this.expandCollapseRequestExdr = new FlexibleStringExpander(treeElement.getAttribute("expand-collapse-request")); 112 this.trailNameExdr = new FlexibleStringExpander(UtilFormatOut.checkEmpty(treeElement.getAttribute("trail-name"), "trail")); 113 this.delegator = delegator; 114 this.dispatcher = dispatcher; 115 this.forceChildCheck = !"false".equals(treeElement.getAttribute("force-child-check")); 116 setDefaultEntityName(treeElement.getAttribute("entity-name")); 117 try { 118 openDepth = Integer.parseInt(treeElement.getAttribute("open-depth")); 119 } catch(NumberFormatException e) { 120 openDepth = 0; 121 } 122 123 try { 124 postTrailOpenDepth = Integer.parseInt(treeElement.getAttribute("post-trail-open-depth")); 125 } catch(NumberFormatException e) { 126 postTrailOpenDepth = 999; 127 } 128 129 List nodeElements = UtilXml.childElementList(treeElement, "node"); 130 Iterator nodeElementIter = nodeElements.iterator(); 131 while (nodeElementIter.hasNext()) { 132 Element nodeElementEntry = (Element ) nodeElementIter.next(); 133 ModelNode node = new ModelNode(nodeElementEntry, this); 134 String nodeName = node.getName(); 135 nodeList.add(node); 136 nodeMap.put(nodeName,node); 137 } 138 139 if (nodeList.size() == 0) { 140 throw new IllegalArgumentException ("No node elements found for the tree definition with name: " + this.name); 141 } 142 143 } 144 145 public String getName() { 146 return name; 147 } 148 149 public void setDefaultEntityName(String name) { 150 String nm = name; 151 if (UtilValidate.isEmpty(nm)) { 152 nm = "Content"; 153 } 154 this.defaultEntityName = nm; 155 ModelEntity modelEntity = delegator.getModelEntity(this.defaultEntityName); 156 if (modelEntity.getPksSize() == 1) { 157 ModelField modelField = modelEntity.getOnlyPk(); 158 this.defaultPkName = modelField.getName(); 159 } 160 } 161 162 public String getDefaultEntityName() { 163 return this.defaultEntityName; 164 } 165 166 public String getDefaultPkName() { 167 return this.defaultPkName; 168 } 169 170 public String getRootNodeName() { 171 return rootNodeName; 172 } 173 174 public String getWrapStyle(Map context) { 175 return this.defaultWrapStyleExdr.expandString(context); 176 } 177 178 public int getOpenDepth() { 179 return openDepth; 180 } 181 182 public int getPostTrailOpenDepth() { 183 return postTrailOpenDepth; 184 } 185 186 public int getNodeIndexAtDepth(int i) { 187 return nodeIndices[i]; 188 } 189 190 public void setNodeIndexAtDepth(int i, int val) { 191 nodeIndices[i] = val; 192 } 193 194 public String getExpandCollapseRequest(Map context) { 195 String expColReq = this.expandCollapseRequestExdr.expandString(context); 196 if (UtilValidate.isEmpty(expColReq)) { 197 HttpServletRequest request = (HttpServletRequest )context.get("request"); 198 String s1 = request.getRequestURI(); 199 int pos = s1.lastIndexOf("/"); 200 if (pos >= 0) 201 expColReq = s1.substring(pos + 1); 202 else 203 expColReq = s1; 204 } 205 return expColReq; 206 } 207 208 public String getTrailName(Map context) { 209 return this.trailNameExdr.expandString(context); 210 } 211 212 public List getTrailList() { 213 return trail; 214 } 215 216 public void setTrailList(List trailList) { 217 this.trail = trailList; 218 } 219 220 public List getCurrentNodeTrail() { 221 return currentNodeTrail; 222 } 223 224 225 240 public void renderTreeString(StringBuffer buf, Map context, TreeStringRenderer treeStringRenderer) throws GeneralException { 241 ModelNode node = (ModelNode)nodeMap.get(rootNodeName); 242 248 currentNodeTrail = new ArrayList (); 249 250 String trailName = trailNameExdr.expandString(context); 253 String treeString = (String )context.get(trailName); 254 if (UtilValidate.isEmpty(treeString)) { 255 Map parameters = (Map )context.get("parameters"); 256 treeString = (String )parameters.get(trailName); 257 } 258 if (UtilValidate.isNotEmpty(treeString)) { 259 trail = StringUtil.split(treeString, "|"); 260 if (trail == null || trail.size() == 0) 261 throw new RuntimeException ("Tree 'trail' value is empty."); 262 263 context.put("rootEntityId", trail.get(0)); 264 context.put(defaultPkName, trail.get(0)); 265 context.put("targetNodeTrail", trail); 266 } else { 267 Debug.logError("Trail value is empty.", module); 268 } 269 StringWriter writer = new StringWriter (); 270 try { 271 node.renderNodeString(writer, context, treeStringRenderer, 0, true); 272 buf.append(writer.toString()); 273 } catch (IOException e2) { 274 String errMsg = "Error rendering included label with name [" + name + "] : " + e2.toString(); 275 Debug.logError(e2, errMsg, module); 276 throw new RuntimeException (errMsg); 277 } 278 286 287 } 288 289 public LocalDispatcher getDispatcher() { 290 return this.dispatcher; 291 } 292 293 public GenericDelegator getDelegator() { 294 return this.delegator; 295 } 296 297 public String getRenderStyle() { 298 return this.defaultRenderStyle; 299 } 300 301 302 public static class ModelNode { 303 304 protected FlexibleStringExpander screenNameExdr; 305 protected FlexibleStringExpander screenLocationExdr; 306 protected String shareScope; 307 protected Label label; 308 protected Link link; 309 protected Image image; 310 protected List subNodeList = new ArrayList (); 311 protected List actions = new ArrayList (); 312 protected String name; 313 protected ModelTree modelTree; 314 protected List subNodeValues; 315 protected String expandCollapseStyle; 316 protected FlexibleStringExpander wrapStyleExdr; 317 protected ModelTreeCondition condition; 318 protected String renderStyle; 319 protected String entryName; 320 protected String entityName; 321 protected String pkName; 322 323 public ModelNode() {} 324 325 public ModelNode(Element nodeElement, ModelTree modelTree) { 326 327 this.modelTree = modelTree; 328 this.name = nodeElement.getAttribute("name"); 329 this.expandCollapseStyle = nodeElement.getAttribute("expand-collapse-style"); 330 this.wrapStyleExdr = new FlexibleStringExpander(nodeElement.getAttribute("wrap-style")); 331 this.renderStyle = nodeElement.getAttribute("render-style"); 332 this.entryName = UtilFormatOut.checkEmpty(nodeElement.getAttribute("entry-name"), null); 333 setEntityName(nodeElement.getAttribute("entity-name")); 334 if (this.pkName == null || nodeElement.hasAttribute("join-field-name")) 335 this.pkName = nodeElement.getAttribute("join-field-name"); 336 337 Element actionElement = UtilXml.firstChildElement(nodeElement, "entity-one"); 338 if (actionElement != null) { 339 actions.add(new ModelTreeAction.EntityOne(this, actionElement)); 340 } 341 342 actionElement = UtilXml.firstChildElement(nodeElement, "service"); 343 if (actionElement != null) { 344 actions.add(new ModelTreeAction.Service(this, actionElement)); 345 } 346 347 actionElement = UtilXml.firstChildElement(nodeElement, "script"); 348 if (actionElement != null) { 349 actions.add(new ModelTreeAction.Script(this, actionElement)); 350 } 351 352 Element screenElement = UtilXml.firstChildElement(nodeElement, "include-screen"); 353 if (screenElement != null) { 354 this.screenNameExdr = new FlexibleStringExpander(screenElement.getAttribute("name")); 355 this.screenLocationExdr = new FlexibleStringExpander(screenElement.getAttribute("location")); 356 this.shareScope = screenElement.getAttribute("share-scope"); 357 } 358 359 Element labelElement = UtilXml.firstChildElement(nodeElement, "label"); 360 if (labelElement != null) { 361 this.label = new Label(labelElement); 362 } 363 364 Element linkElement = UtilXml.firstChildElement(nodeElement, "link"); 365 if (linkElement != null) { 366 this.link = new Link(linkElement); 367 } 368 369 Element imageElement = UtilXml.firstChildElement(nodeElement, "image"); 370 if (imageElement != null) { 371 this.image = new Image(imageElement); 372 } 373 374 379 Element conditionElement = UtilXml.firstChildElement(nodeElement, "condition"); 380 if (conditionElement != null) { 381 this.condition = new ModelTreeCondition(modelTree, conditionElement); 382 } 383 384 List subNodeElements = UtilXml.childElementList(nodeElement, "sub-node"); 385 Iterator subNodeElementIter = subNodeElements.iterator(); 386 while (subNodeElementIter.hasNext()) { 387 Element subNodeElementEntry = (Element ) subNodeElementIter.next(); 388 ModelSubNode subNode = new ModelSubNode(subNodeElementEntry, this); 389 subNodeList.add(subNode); 390 } 391 392 393 } 394 395 public void renderNodeString(Writer writer, Map context, 396 TreeStringRenderer treeStringRenderer, int depth, boolean isLast) 397 throws IOException , GeneralException { 398 boolean passed = true; 399 if (this.condition != null) { 400 if (!this.condition.eval(context)) { 401 passed = false; 402 } 403 } 404 if (passed) { 406 context.put("processChildren", new Boolean (true)); 410 ModelTreeAction.runSubActions(this.actions, context); 412 String pkName = getPkName(); 413 String id = null; 414 if (UtilValidate.isNotEmpty(this.entryName)) { 415 Map map = (Map )context.get(this.entryName); 416 id = (String )map.get(pkName); 417 } else { 418 id = (String ) context.get(pkName); 419 } 420 if (id != null) { 421 modelTree.currentNodeTrail.add(id); 422 } 423 context.put("currentNodeTrail", modelTree.currentNodeTrail); 424 String currentNodeTrailPiped = StringUtil.join(modelTree.currentNodeTrail, "|"); 425 context.put("currentNodeTrailPiped", currentNodeTrailPiped); 426 treeStringRenderer.renderNodeBegin(writer, context, this, depth, isLast); 427 try { 430 String screenName = null; 431 if (screenNameExdr != null) 432 screenName = screenNameExdr.expandString(context); 433 String screenLocation = null; 434 if (screenLocationExdr != null) 435 screenLocation = screenLocationExdr.expandString(context); 436 if (screenName != null && screenLocation != null) { 437 ScreenStringRenderer screenStringRenderer = treeStringRenderer .getScreenStringRenderer(context); 438 ModelScreen modelScreen = ScreenFactory .getScreenFromLocation(screenLocation, screenName); 439 modelScreen.renderScreenString(writer, context, screenStringRenderer); 440 } 441 if (label != null) { 442 label.renderLabelString(writer, context, treeStringRenderer); 443 } 444 if (link != null) { 445 link.renderLinkString(writer, context, treeStringRenderer); 446 } 447 Boolean processChildren = (Boolean ) context .get("processChildren"); 448 if (processChildren.booleanValue()) { 450 getChildren(context); 451 Iterator nodeIter = this.subNodeValues.iterator(); 452 int nodeIndex = -1; 453 int newDepth = depth + 1; 454 while (nodeIter.hasNext()) { 455 nodeIndex++; 456 modelTree.setNodeIndexAtDepth(newDepth, nodeIndex); 457 Object [] arr = (Object []) nodeIter.next(); 458 ModelNode node = (ModelNode) arr[0]; 459 Map val = (Map ) arr[1]; 460 String thisPkName = node.getPkName(); 464 String thisEntityId = (String ) val.get(thisPkName); 465 Map newContext = ((MapStack) context) .standAloneChildStack(); 466 String nodeEntryName = node.getEntryName(); 467 if (UtilValidate.isNotEmpty(nodeEntryName)) { 468 newContext.put(nodeEntryName, val); 469 } else { 470 newContext.putAll(val); 471 } 472 newContext.put("currentNodeIndex", new Integer (nodeIndex)); 473 String targetEntityId = null; 474 List targetNodeTrail = this.modelTree .getTrailList(); 475 if (newDepth < targetNodeTrail.size()) { 476 targetEntityId = (String ) targetNodeTrail .get(newDepth); 477 } 478 if ((targetEntityId != null && targetEntityId .equals(thisEntityId)) || this.showPeers(newDepth)) { 479 boolean lastNode = !nodeIter.hasNext(); 480 newContext.put("lastNode", new Boolean (lastNode)); 481 node.renderNodeString(writer, newContext, treeStringRenderer, newDepth, lastNode); 482 } 483 } 484 } 485 } catch (SAXException e) { 486 String errMsg = "Error rendering included label with name [" 487 + name + "] : " + e.toString(); 488 Debug.logError(e, errMsg, module); 489 throw new RuntimeException (errMsg); 490 } catch (ParserConfigurationException e3) { 491 String errMsg = "Error rendering included label with name [" 492 + name + "] : " + e3.toString(); 493 Debug.logError(e3, errMsg, module); 494 throw new RuntimeException (errMsg); 495 } catch (IOException e2) { 496 String errMsg = "Error rendering included label with name [" 497 + name + "] : " + e2.toString(); 498 Debug.logError(e2, errMsg, module); 499 throw new RuntimeException (errMsg); 500 } 501 treeStringRenderer.renderNodeEnd(writer, context, this); 502 int removeIdx = modelTree.currentNodeTrail.size() - 1; 503 if (removeIdx >= 0) modelTree.currentNodeTrail.remove(removeIdx); 504 } 505 } 506 507 public boolean hasChildren(Map context) { 508 509 boolean hasChildren = false; 510 Long nodeCount = null; 511 String countFieldName = "childBranchCount"; 512 Object obj = null; 513 if (UtilValidate.isNotEmpty(this.entryName)) { 514 Map map = (Map )context.get(this.entryName); 515 if (map instanceof GenericValue) { 516 ModelEntity modelEntity = ((GenericValue)map).getModelEntity(); 517 if (modelEntity.isField(countFieldName)) { 518 obj = map.get(countFieldName); 519 } 520 } 521 } else { 522 obj = context.get(countFieldName); 523 } 524 if (obj != null) { 525 nodeCount = (Long )obj; 526 } 527 String entName = this.getEntityName(); 528 GenericDelegator delegator = modelTree.getDelegator(); 529 ModelEntity modelEntity = delegator.getModelEntity(entName); 530 ModelField modelField = null; 531 if (modelEntity.isField(countFieldName)) { 532 modelField = modelEntity.getField(countFieldName); 533 } 534 if (nodeCount == null && modelField != null || this.modelTree.forceChildCheck) { 535 getChildren(context); 536 551 nodeCount = new Long (this.subNodeValues.size()); 552 String pkName = this.getPkName(); 553 String id = null; 554 if (UtilValidate.isNotEmpty(this.entryName)) { 555 Map map = (Map )context.get(this.entryName); 556 id = (String )map.get(pkName); 557 } else { 558 id = (String ) context.get(pkName); 559 } 560 try { 561 if (id != null && modelEntity.getPksSize() == 1) { 562 GenericValue entity = delegator.findByPrimaryKey(entName, UtilMisc.toMap(pkName, id)); 563 if (modelEntity.isField("childBranchCount")) { 564 entity.put("childBranchCount", nodeCount); 565 } 566 entity.store(); 567 } 568 } catch(GenericEntityException e) { 569 Debug.logError(e, module); 570 throw new RuntimeException (e.getMessage()); 571 } 572 } else if (nodeCount == null) { 573 getChildren(context); 574 if (subNodeValues != null) 575 nodeCount = new Long (subNodeValues.size()); 576 } 577 578 if (nodeCount != null && nodeCount.intValue() > 0) 579 hasChildren = true; 580 581 582 return hasChildren; 583 } 584 585 public void getChildren(Map context) { 586 this.subNodeValues = new ArrayList (); 587 Iterator nodeIter = subNodeList.iterator(); 588 while (nodeIter.hasNext()) { 589 ModelSubNode subNode = (ModelSubNode)nodeIter.next(); 590 String nodeName = subNode.getNodeName(context); 591 ModelNode node = (ModelNode)modelTree.nodeMap.get(nodeName); 592 List subNodeActions = subNode.getActions(); 593 ModelTreeAction.runSubActions(subNodeActions, context); 595 ListIterator dataIter = subNode.getListIterator(); 597 if (dataIter instanceof EntityListIterator) { 598 EntityListIterator eli = (EntityListIterator) dataIter; 599 Map val = null; 600 while ((val = (Map ) eli.next()) != null) { 601 Object [] arr = {node, val}; 602 this.subNodeValues.add(arr); 603 } 604 try { 605 eli.close(); 606 } catch(GenericEntityException e) { 607 Debug.logError(e, module); 608 throw new RuntimeException (e.getMessage()); 609 } 610 } else if (dataIter != null) { 611 while (dataIter.hasNext()) { 612 Map val = (Map ) dataIter.next(); 613 Object [] arr = {node, val}; 614 this.subNodeValues.add(arr); 615 } 616 } 617 } 618 return; 619 } 620 621 public String getName() { 622 return name; 623 } 624 625 public String getEntryName() { 626 return this.entryName; 627 } 628 629 public String getRenderStyle() { 630 String rStyle = this.renderStyle; 631 if (UtilValidate.isEmpty(rStyle)) 632 rStyle = modelTree.getRenderStyle(); 633 return rStyle; 634 } 635 636 public boolean isExpandCollapse() { 637 boolean isExpCollapse = false; 638 String rStyle = getRenderStyle(); 639 if (rStyle != null && rStyle.equals("expand-collapse")) 640 isExpCollapse = true; 641 642 return isExpCollapse; 643 } 644 645 public boolean isFollowTrail() { 646 boolean isFollowTrail = false; 647 String rStyle = getRenderStyle(); 648 if (rStyle != null && (rStyle.equals("follow-trail") || rStyle.equals("show-peers") || rStyle.equals("follow-trail"))) { 649 isFollowTrail = true; 650 } 651 652 return isFollowTrail; 653 } 654 655 public boolean showPeers(int currentDepth) { 656 int trailSize = 0; 657 List trail = modelTree.getTrailList(); 658 int openDepth = modelTree.getOpenDepth(); 659 int postTrailOpenDepth = modelTree.getPostTrailOpenDepth(); 660 if (trail != null) trailSize = trail.size(); 661 662 boolean showPeers = false; 663 String rStyle = getRenderStyle(); 664 if (rStyle == null) { 665 showPeers = true; 666 } else if (!isFollowTrail()) { 667 showPeers = true; 668 } else if ((currentDepth < trailSize) && (rStyle != null) && (rStyle.equals("show-peers") || rStyle.equals("expand-collapse"))) { 669 showPeers = true; 670 } else if (openDepth >= currentDepth) { 671 showPeers = true; 672 } else { 673 int depthAfterTrail = currentDepth - trailSize; 674 if (depthAfterTrail >= 0 && depthAfterTrail <= postTrailOpenDepth) showPeers = true; 675 } 676 677 return showPeers; 678 } 679 680 public String getExpandCollapseStyle() { 681 return expandCollapseStyle; 682 } 683 684 public String getWrapStyle(Map context) { 685 String val = this.wrapStyleExdr.expandString(context); 686 if (UtilValidate.isEmpty(val)) { 687 val = this.modelTree.getWrapStyle(context); 688 } 689 return val; 690 } 691 692 public ModelTree getModelTree() { 693 return this.modelTree; 694 } 695 696 public void setEntityName(String name) { 697 this.entityName = name; 698 if (UtilValidate.isNotEmpty(this.entityName)) { 699 ModelEntity modelEntity = modelTree.delegator.getModelEntity(this.entityName); 700 if (modelEntity.getPksSize() == 1) { 701 ModelField modelField = modelEntity.getOnlyPk(); 702 this.pkName = modelField.getName(); 703 } else { 704 } 706 } 707 } 708 709 public String getEntityName() { 710 if (UtilValidate.isNotEmpty(this.entityName)) { 711 return this.entityName; 712 } else { 713 return this.modelTree.getDefaultEntityName(); 714 } 715 } 716 717 public String getPkName() { 718 if (UtilValidate.isNotEmpty(this.pkName)) { 719 return this.pkName; 720 } else { 721 return this.modelTree.getDefaultPkName(); 722 } 723 } 724 725 public void setPkName(String pkName) { 726 this.pkName = pkName; 727 } 728 729 public static class ModelSubNode { 730 731 protected ModelNode rootNode; 732 protected FlexibleStringExpander nodeNameExdr; 733 protected List actions = new ArrayList (); 734 protected List outFieldMaps; 735 protected ListIterator listIterator; 736 737 public ModelSubNode() {} 738 739 public ModelSubNode(Element nodeElement, ModelNode modelNode) { 740 741 this.rootNode = modelNode; 742 this.nodeNameExdr = new FlexibleStringExpander(nodeElement.getAttribute("node-name")); 743 744 Element actionElement = UtilXml.firstChildElement(nodeElement, "entity-and"); 745 if (actionElement != null) { 746 actions.add(new ModelTreeAction.EntityAnd(this, actionElement)); 747 } 748 749 actionElement = UtilXml.firstChildElement(nodeElement, "service"); 750 if (actionElement != null) { 751 actions.add(new ModelTreeAction.Service(this, actionElement)); 752 } 753 754 actionElement = UtilXml.firstChildElement(nodeElement, "entity-condition"); 755 if (actionElement != null) { 756 actions.add(new ModelTreeAction.EntityCondition(this, actionElement)); 757 } 758 759 actionElement = UtilXml.firstChildElement(nodeElement, "script"); 760 if (actionElement != null) { 761 actions.add(new ModelTreeAction.Script(this, actionElement)); 762 } 763 764 } 765 766 public ModelTree.ModelNode getNode() { 767 return this.rootNode; 768 } 769 770 public String getNodeName(Map context) { 771 return this.nodeNameExdr.expandString(context); 772 } 773 774 public List getActions() { 775 return actions; 776 } 777 778 public void setListIterator(ListIterator iter) { 779 listIterator = iter; 780 } 781 782 public ListIterator getListIterator() { 783 return listIterator; 784 } 785 } 786 787 public static class Label { 788 protected FlexibleStringExpander textExdr; 789 790 protected FlexibleStringExpander idExdr; 791 protected FlexibleStringExpander styleExdr; 792 793 public Label(Element labelElement) { 794 795 String textAttr = UtilFormatOut.checkNull(labelElement.getAttribute("text")); 797 String pcdata = UtilFormatOut.checkNull(UtilXml.elementValue(labelElement)); 798 this.textExdr = new FlexibleStringExpander(textAttr + pcdata); 799 800 this.idExdr = new FlexibleStringExpander(labelElement.getAttribute("id")); 801 this.styleExdr = new FlexibleStringExpander(labelElement.getAttribute("style")); 802 } 803 804 public void renderLabelString(Writer writer, Map context, TreeStringRenderer treeStringRenderer) { 805 try { 806 treeStringRenderer.renderLabel(writer, context, this); 807 } catch (IOException e) { 808 String errMsg = "Error rendering label with id [" + getId(context) + "]: " + e.toString(); 809 Debug.logError(e, errMsg, module); 810 throw new RuntimeException (errMsg); 811 } 812 } 813 814 public String getText(Map context) { 815 return this.textExdr.expandString(context); 816 } 817 818 public String getId(Map context) { 819 return this.idExdr.expandString(context); 820 } 821 822 public String getStyle(Map context) { 823 return this.styleExdr.expandString(context); 824 } 825 } 826 827 828 public static class Link { 829 protected FlexibleStringExpander textExdr; 830 protected FlexibleStringExpander idExdr; 831 protected FlexibleStringExpander styleExdr; 832 protected FlexibleStringExpander targetExdr; 833 protected FlexibleStringExpander targetWindowExdr; 834 protected FlexibleStringExpander prefixExdr; 835 protected FlexibleStringExpander nameExdr; 836 protected FlexibleStringExpander titleExdr; 837 protected Image image; 838 protected String urlMode = "intra-app"; 839 protected boolean fullPath = false; 840 protected boolean secure = false; 841 protected boolean encode = false; 842 843 public Link() { 844 setText(null); 845 setId(null); 846 setStyle(null); 847 setTarget(null); 848 setTargetWindow(null); 849 setPrefix(null); 850 setUrlMode(null); 851 setFullPath(null); 852 setSecure(null); 853 setEncode(null); 854 setName(null); 855 setTitle(null); 856 } 857 858 public Link(Element linkElement) { 859 860 setText(linkElement.getAttribute("text")); 861 setId(linkElement.getAttribute("id")); 862 setStyle(linkElement.getAttribute("style")); 863 setTarget(linkElement.getAttribute("target")); 864 setTargetWindow(linkElement.getAttribute("target-window")); 865 setPrefix(linkElement.getAttribute("prefix")); 866 setUrlMode(linkElement.getAttribute("url-mode")); 867 setFullPath(linkElement.getAttribute("full-path")); 868 setSecure(linkElement.getAttribute("secure")); 869 setEncode(linkElement.getAttribute("encode")); 870 setName(linkElement.getAttribute("name")); 871 setTitle(linkElement.getAttribute("title")); 872 Element imageElement = UtilXml.firstChildElement(linkElement, "image"); 873 if (imageElement != null) { 874 this.image = new Image(imageElement); 875 } 876 877 } 878 879 public void renderLinkString(Writer writer, Map context, TreeStringRenderer treeStringRenderer) { 880 try { 881 treeStringRenderer.renderLink(writer, context, this); 882 } catch (IOException e) { 883 String errMsg = "Error rendering link with id [" + getId(context) + "]: " + e.toString(); 884 Debug.logError(e, errMsg, module); 885 throw new RuntimeException (errMsg); 886 } 887 } 888 889 public String getText(Map context) { 890 return this.textExdr.expandString(context); 891 } 892 893 public String getId(Map context) { 894 return this.idExdr.expandString(context); 895 } 896 897 public String getStyle(Map context) { 898 return this.styleExdr.expandString(context); 899 } 900 901 public String getName(Map context) { 902 return this.nameExdr.expandString(context); 903 } 904 public String getTitle(Map context) { 905 return this.titleExdr.expandString(context); 906 } 907 908 public String getTarget(Map context) { 909 return this.targetExdr.expandString(context); 910 } 911 912 public String getTargetWindow(Map context) { 913 return this.targetWindowExdr.expandString(context); 914 } 915 916 public String getUrlMode() { 917 return this.urlMode; 918 } 919 920 public String getPrefix(Map context) { 921 return this.prefixExdr.expandString(context); 922 } 923 924 public boolean getFullPath() { 925 return this.fullPath; 926 } 927 928 public boolean getSecure() { 929 return this.secure; 930 } 931 932 public boolean getEncode() { 933 return this.encode; 934 } 935 936 public Image getImage() { 937 return this.image; 938 } 939 940 public void setText(String val) { 941 String textAttr = UtilFormatOut.checkNull(val); 942 this.textExdr = new FlexibleStringExpander(textAttr); 943 } 944 public void setId(String val) { 945 this.idExdr = new FlexibleStringExpander(val); 946 } 947 public void setStyle(String val) { 948 this.styleExdr = new FlexibleStringExpander(val); 949 } 950 public void setName(String val) { 951 this.nameExdr = new FlexibleStringExpander(val); 952 } 953 public void setTitle(String val) { 954 this.titleExdr = new FlexibleStringExpander(val); 955 } 956 public void setTarget(String val) { 957 this.targetExdr = new FlexibleStringExpander(val); 958 } 959 public void setTargetWindow(String val) { 960 this.targetWindowExdr = new FlexibleStringExpander(val); 961 } 962 public void setPrefix(String val) { 963 this.prefixExdr = new FlexibleStringExpander(val); 964 } 965 public void setUrlMode(String val) { 966 if (UtilValidate.isNotEmpty(val)) 967 this.urlMode = val; 968 } 969 public void setFullPath(String val) { 970 String sFullPath = val; 971 if (sFullPath != null && sFullPath.equalsIgnoreCase("true")) 972 this.fullPath = true; 973 else 974 this.fullPath = false; 975 } 976 977 public void setSecure(String val) { 978 String sSecure = val; 979 if (sSecure != null && sSecure.equalsIgnoreCase("true")) 980 this.secure = true; 981 else 982 this.secure = false; 983 } 984 985 public void setEncode(String val) { 986 String sEncode = val; 987 if (sEncode != null && sEncode.equalsIgnoreCase("true")) 988 this.encode = true; 989 else 990 this.encode = false; 991 } 992 public void setImage(Image img) { 993 this.image = img; 994 } 995 996 } 997 998 public static class Image { 999 1000 protected FlexibleStringExpander srcExdr; 1001 protected FlexibleStringExpander idExdr; 1002 protected FlexibleStringExpander styleExdr; 1003 protected FlexibleStringExpander widthExdr; 1004 protected FlexibleStringExpander heightExdr; 1005 protected FlexibleStringExpander borderExdr; 1006 protected String urlMode; 1007 1008 public Image() { 1009 1010 setSrc(null); 1011 setId(null); 1012 setStyle(null); 1013 setWidth(null); 1014 setHeight(null); 1015 setBorder("0"); 1016 setUrlMode(null); 1017 } 1018 1019 public Image(Element imageElement) { 1020 1021 setSrc(imageElement.getAttribute("src")); 1022 setId(imageElement.getAttribute("id")); 1023 setStyle(imageElement.getAttribute("style")); 1024 setWidth(imageElement.getAttribute("width")); 1025 setHeight(imageElement.getAttribute("height")); 1026 setBorder(UtilFormatOut.checkEmpty(imageElement.getAttribute("border"), "0")); 1027 setUrlMode(UtilFormatOut.checkEmpty(imageElement.getAttribute("url-mode"), "content")); 1028 1029 } 1030 1031 public void renderImageString(Writer writer, Map context, TreeStringRenderer treeStringRenderer) { 1032 try { 1033 treeStringRenderer.renderImage(writer, context, this); 1034 } catch (IOException e) { 1035 String errMsg = "Error rendering image with id [" + getId(context) + "]: " + e.toString(); 1036 Debug.logError(e, errMsg, module); 1037 throw new RuntimeException (errMsg); 1038 } 1039 } 1040 1041 public String getSrc(Map context) { 1042 return this.srcExdr.expandString(context); 1043 } 1044 1045 public String getId(Map context) { 1046 return this.idExdr.expandString(context); 1047 } 1048 1049 public String getStyle(Map context) { 1050 return this.styleExdr.expandString(context); 1051 } 1052 1053 public String getWidth(Map context) { 1054 return this.widthExdr.expandString(context); 1055 } 1056 1057 public String getHeight(Map context) { 1058 return this.heightExdr.expandString(context); 1059 } 1060 1061 public String getBorder(Map context) { 1062 return this.borderExdr.expandString(context); 1063 } 1064 1065 public String getUrlMode() { 1066 return this.urlMode; 1067 } 1068 1069 public void setSrc(String val) { 1070 String textAttr = UtilFormatOut.checkNull(val); 1071 this.srcExdr = new FlexibleStringExpander(textAttr); 1072 } 1073 public void setId(String val) { 1074 this.idExdr = new FlexibleStringExpander(val); 1075 } 1076 public void setStyle(String val) { 1077 this.styleExdr = new FlexibleStringExpander(val); 1078 } 1079 public void setWidth(String val) { 1080 this.widthExdr = new FlexibleStringExpander(val); 1081 } 1082 public void setHeight(String val) { 1083 this.heightExdr = new FlexibleStringExpander(val); 1084 } 1085 public void setBorder(String val) { 1086 this.borderExdr = new FlexibleStringExpander(val); 1087 } 1088 public void setUrlMode(String val) { 1089 if (UtilValidate.isEmpty(val)) 1090 this.urlMode = "content"; 1091 else 1092 this.urlMode = val; 1093 } 1094 1095 } 1096 1097 } 1098} 1099 1100 | Popular Tags |