1 24 package org.ofbiz.widget.menu; 25 26 import java.io.IOException ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.xml.parsers.ParserConfigurationException ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.UtilFormatOut; 37 import org.ofbiz.base.util.UtilValidate; 38 import org.ofbiz.base.util.UtilXml; 39 import org.ofbiz.base.util.string.FlexibleStringExpander; 40 import org.ofbiz.entityext.permission.EntityPermissionChecker; 41 import org.w3c.dom.Element ; 42 import org.xml.sax.SAXException ; 43 44 52 public class ModelMenuItem { 53 54 public static final String module = ModelMenuItem.class.getName(); 55 56 protected ModelMenu modelMenu; 57 58 protected Map dataMap = new HashMap (); 59 protected String name; 60 protected String entityName; 61 protected FlexibleStringExpander title; 62 protected FlexibleStringExpander tooltip; 63 protected String titleStyle; 64 protected String disabledTitleStyle; 65 protected String widgetStyle; 66 protected String tooltipStyle; 67 protected String selectedStyle; 68 protected Integer position = null; 69 70 protected FlexibleStringExpander associatedContentId; 71 protected String cellWidth; 72 protected Boolean hideIfSelected; 73 protected Boolean hasPermission; 74 protected String disableIfEmpty; 75 protected ModelMenu subMenu; 76 protected Link link; 77 78 protected List menuItemList = new LinkedList (); 79 protected Map menuItemMap = new HashMap (); 80 81 82 public static String DEFAULT_TARGET_TYPE = "intra-app"; 83 84 protected EntityPermissionChecker permissionChecker; 85 protected ModelMenuItem parentMenuItem; 86 protected ModelMenuCondition condition; 87 protected boolean disabled = false; 88 protected List actions; 89 protected String align; 90 protected String alignStyle; 91 92 94 public ModelMenuItem(ModelMenu modelMenu) { 95 this.modelMenu = modelMenu; 96 } 97 98 99 public ModelMenuItem(Element fieldElement, ModelMenuItem modelMenuItem) { 100 parentMenuItem = modelMenuItem; 101 loadMenuItem(fieldElement, modelMenuItem.getModelMenu()); 102 } 103 104 105 public ModelMenuItem(Element fieldElement, ModelMenu modelMenu) { 106 loadMenuItem(fieldElement, modelMenu); 107 } 108 109 public void loadMenuItem(Element fieldElement, ModelMenu modelMenu) { 110 this.modelMenu = modelMenu; 111 this.name = fieldElement.getAttribute("name"); 112 this.entityName = fieldElement.getAttribute("entity-name"); 113 this.setTitle(fieldElement.getAttribute("title")); 114 this.setTooltip(fieldElement.getAttribute("tooltip")); 115 this.titleStyle = fieldElement.getAttribute("title-style"); 116 this.disabledTitleStyle = fieldElement.getAttribute("disabled-title-style"); 117 this.widgetStyle = fieldElement.getAttribute("widget-style"); 118 this.tooltipStyle = fieldElement.getAttribute("tooltip-style"); 119 this.selectedStyle = fieldElement.getAttribute("selected-style"); 120 this.setHideIfSelected(fieldElement.getAttribute("hide-if-selected")); 121 this.disableIfEmpty = fieldElement.getAttribute("disable-if-empty"); 122 this.align = fieldElement.getAttribute("align"); 123 this.alignStyle = fieldElement.getAttribute("align-style"); 124 125 String positionStr = fieldElement.getAttribute("position"); 126 try { 127 if (positionStr != null && positionStr.length() > 0) { 128 position = Integer.valueOf(positionStr); 129 } 130 } catch (Exception e) { 131 Debug.logError( 132 e, 133 "Could not convert position attribute of the field element to an integer: [" + positionStr + "], using the default of the menu renderer", 134 module); 135 } 136 137 this.setAssociatedContentId( fieldElement.getAttribute("associated-content-id")); 138 this.cellWidth = fieldElement.getAttribute("cell-width"); 139 140 dataMap.put("name", this.name); 141 143 Element subMenuElement = UtilXml.firstChildElement(fieldElement, "sub-menu"); 144 if (subMenuElement != null) { 145 String subMenuLocation = subMenuElement.getAttribute("location"); 146 String subMenuName = subMenuElement.getAttribute("name"); 147 try { 148 this.subMenu = MenuFactory.getMenuFromLocation(subMenuLocation, subMenuName, modelMenu.getDelegator(), modelMenu.getDispacher()); 149 } catch (IOException e) { 150 String errMsg = "Error getting subMenu in menu named [" + this.modelMenu.getName() + "]: " + e.toString(); 151 Debug.logError(e, errMsg, module); 152 throw new RuntimeException (errMsg); 153 } catch (SAXException e2) { 154 String errMsg = "Error getting subMenu in menu named [" + this.modelMenu.getName() + "]: " + e2.toString(); 155 Debug.logError(e2, errMsg, module); 156 throw new RuntimeException (errMsg); 157 } catch (ParserConfigurationException e3) { 158 String errMsg = "Error getting subMenu in menu named [" + this.modelMenu.getName() + "]: " + e3.toString(); 159 Debug.logError(e3, errMsg, module); 160 throw new RuntimeException (errMsg); 161 } 162 } 163 Element linkElement = UtilXml.firstChildElement(fieldElement, "link"); 164 if (linkElement != null) { 166 link = new Link(linkElement, this); 167 } 168 169 173 List itemElements = UtilXml.childElementList(fieldElement, "menu-item"); 175 Iterator itemElementIter = itemElements.iterator(); 176 while (itemElementIter.hasNext()) { 177 Element itemElement = (Element ) itemElementIter.next(); 178 ModelMenuItem modelMenuItem = new ModelMenuItem(itemElement, this); 179 modelMenuItem = this.addUpdateMenuItem(modelMenuItem); 180 } 182 Element conditionElement = UtilXml.firstChildElement(fieldElement, "condition"); 184 if (conditionElement != null) { 185 this.condition = new ModelMenuCondition(this, conditionElement); 186 } 187 Element actionsElement = UtilXml.firstChildElement(conditionElement, "actions"); 189 if (actionsElement != null) { 190 this.actions = ModelMenuAction.readSubActions(this, actionsElement); 191 } 192 193 } 194 195 public ModelMenuItem addUpdateMenuItem(ModelMenuItem modelMenuItem) { 196 197 ModelMenuItem existingMenuItem = (ModelMenuItem) this.menuItemMap.get(modelMenuItem.getName()); 199 if (existingMenuItem != null) { 200 existingMenuItem.mergeOverrideModelMenuItem(modelMenuItem); 202 return existingMenuItem; 203 } else { 204 this.menuItemList.add(modelMenuItem); 206 this.menuItemMap.put(modelMenuItem.getName(), modelMenuItem); 207 return modelMenuItem; 208 } 209 } 210 211 212 public void setHideIfSelected(String val) { 213 if (UtilValidate.isNotEmpty(val)) 214 if (val.equalsIgnoreCase("true")) 215 hideIfSelected = new Boolean (true); 216 else 217 hideIfSelected = new Boolean (false); 218 else 219 hideIfSelected = null; 220 221 return; 222 } 223 224 public void setDisabled(boolean val) { 225 this.disabled = val; 226 } 227 228 public boolean getDisabled() { 229 return this.disabled; 230 } 231 232 public void mergeOverrideModelMenuItem(ModelMenuItem overrideModelMenuItem) { 233 if (overrideModelMenuItem == null) 234 return; 235 258 return; 259 } 260 261 public void renderMenuItemString(StringBuffer buffer, Map context, MenuStringRenderer menuStringRenderer) { 262 263 boolean passed = true; 264 if (this.condition != null) { 265 if (!this.condition.eval(context)) { 266 passed = false; 267 } 268 } 269 if (passed) { 271 ModelMenuAction.runSubActions(this.actions, context); 272 menuStringRenderer.renderMenuItem(buffer, context, this); 273 } 274 } 275 276 277 280 public ModelMenu getModelMenu() { 281 return modelMenu; 282 } 283 284 285 288 public String getEntityName() { 289 if (UtilValidate.isNotEmpty(this.entityName)) { 290 return this.entityName; 291 } else if (parentMenuItem != null) { 292 return parentMenuItem.getEntityName(); 293 } else { 294 return this.modelMenu.getDefaultEntityName(); 295 } 296 } 297 298 301 public String getAlign() { 302 if (UtilValidate.isNotEmpty(this.align)) { 303 return this.align; 304 } else if (parentMenuItem != null) { 305 return parentMenuItem.getAlign(); 306 } else { 307 return this.modelMenu.getDefaultAlign(); 308 } 309 } 310 311 312 315 public String getName() { 316 return name; 317 } 318 319 322 public int getPosition() { 323 if (this.position == null) { 324 return 1; 325 } else { 326 return position.intValue(); 327 } 328 } 329 330 333 public String getTitle(Map context) { 334 return title.expandString(context); 335 } 336 337 340 public String getTitleStyle() { 341 if (UtilValidate.isNotEmpty(this.titleStyle)) { 342 return this.titleStyle; 343 } else if (parentMenuItem != null) { 344 return parentMenuItem.getTitleStyle(); 345 } else { 346 return this.modelMenu.getDefaultTitleStyle(); 347 } 348 } 349 350 353 public String getDisabledTitleStyle() { 354 if (UtilValidate.isNotEmpty(this.disabledTitleStyle)) { 355 return this.disabledTitleStyle; 356 } else if (parentMenuItem != null) { 357 return parentMenuItem.getDisabledTitleStyle(); 358 } else { 359 return this.modelMenu.getDefaultDisabledTitleStyle(); 360 } 361 } 362 363 public void setDisabledTitleStyle(String style) { 364 this.disabledTitleStyle = style; 365 } 366 367 370 public String getSelectedStyle() { 371 if (UtilValidate.isNotEmpty(this.selectedStyle)) { 372 return this.selectedStyle; 373 } else if (parentMenuItem != null) { 374 return parentMenuItem.getSelectedStyle(); 375 } else { 376 return this.modelMenu.getDefaultSelectedStyle(); 377 } 378 } 379 380 383 public String getTooltip(Map context) { 384 if (tooltip != null && !tooltip.isEmpty()) { 385 return tooltip.expandString(context); 386 } else { 387 return ""; 388 } 389 } 390 391 392 395 public String getWidgetStyle() { 396 if (UtilValidate.isNotEmpty(this.widgetStyle)) { 397 return this.widgetStyle; 398 } else if (parentMenuItem != null) { 399 return parentMenuItem.getWidgetStyle(); 400 } else { 401 return this.modelMenu.getDefaultWidgetStyle(); 402 } 403 } 404 405 408 public String getAlignStyle() { 409 if (UtilValidate.isNotEmpty(this.alignStyle)) { 410 return this.alignStyle; 411 } else if (parentMenuItem != null) { 412 return parentMenuItem.getAlignStyle(); 413 } else { 414 return this.modelMenu.getDefaultAlignStyle(); 415 } 416 } 417 418 421 public String getTooltipStyle() { 422 if (UtilValidate.isNotEmpty(this.tooltipStyle)) { 423 return this.tooltipStyle; 424 } else if (parentMenuItem != null) { 425 return parentMenuItem.getTooltipStyle(); 426 } else { 427 return this.modelMenu.getDefaultTooltipStyle(); 428 } 429 } 430 431 434 public void setEntityName(String string) { 435 entityName = string; 436 } 437 438 441 public void setName(String string) { 442 name = string; 443 } 444 445 448 public void setPosition(int i) { 449 position = new Integer (i); 450 } 451 452 453 456 public void setTitle(String string) { 457 this.title = new FlexibleStringExpander(string); 458 } 459 460 463 public void setTitleStyle(String string) { 464 this.titleStyle = string; 465 } 466 467 470 public void setTooltip(String string) { 471 this.tooltip = new FlexibleStringExpander(string); 472 } 473 474 475 478 public void setWidgetStyle(String string) { 479 this.widgetStyle = string; 480 } 481 482 485 public void setTooltipStyle(String string) { 486 this.tooltipStyle = string; 487 } 488 489 490 493 public void setAssociatedContentId(String string) { 494 this.associatedContentId = new FlexibleStringExpander(string); 495 } 496 497 500 public String getAssociatedContentId(Map context) { 501 String retStr = null; 502 if (this.associatedContentId != null) { 503 retStr = associatedContentId.expandString(context); 504 } 505 if (UtilValidate.isEmpty(retStr)) { 506 retStr = this.modelMenu.getDefaultAssociatedContentId(context); 507 } 508 return retStr; 509 } 510 511 512 515 public void setCellWidth(String string) { 516 this.cellWidth = string; 517 } 518 519 522 public String getCellWidth() { 523 if (UtilValidate.isNotEmpty(this.cellWidth )) { 524 return this.cellWidth ; 525 } else { 526 return this.modelMenu.getDefaultCellWidth (); 527 } 528 } 529 530 533 public void setHideIfSelected(Boolean val) { 534 this.hideIfSelected = val; 535 } 536 537 540 public Boolean getHideIfSelected() { 541 if (hideIfSelected != null) { 542 return this.hideIfSelected; 543 } else { 544 return this.modelMenu.getDefaultHideIfSelected(); 545 } 546 } 547 548 551 public String getDisableIfEmpty() { 552 return this.disableIfEmpty; 553 } 554 555 558 public void setHasPermission(Boolean val) { 559 this.hasPermission = val; 560 } 561 562 565 public Boolean getHasPermission() { 566 return this.hasPermission; 567 } 568 569 public void dump(StringBuffer buffer ) { 570 buffer.append("ModelMenuItem:" 571 + "\n title=" + this.title 572 + "\n name=" + this.name 573 + "\n entityName=" + this.entityName 574 + "\n titleStyle=" + this.titleStyle 575 + "\n widgetStyle=" + this.widgetStyle 576 + "\n tooltipStyle=" + this.tooltipStyle 577 + "\n selectedStyle=" + this.selectedStyle 578 + "\n\n"); 579 580 581 return; 582 } 583 584 public Link getLink() { 585 return this.link; 586 } 587 588 public boolean isSelected(Map context) { 589 590 String currentMenuItemName = modelMenu.getSelectedMenuItemContextFieldName(context); 591 if (currentMenuItemName != null && currentMenuItemName.equals(this.name)) 592 return true; 593 else 594 return false; 595 } 596 597 598 599 600 public static class Link { 601 602 protected ModelMenuItem linkMenuItem; 603 protected FlexibleStringExpander textExdr; 604 protected FlexibleStringExpander idExdr; 605 protected FlexibleStringExpander styleExdr; 606 protected FlexibleStringExpander targetExdr; 607 protected FlexibleStringExpander targetWindowExdr; 608 protected FlexibleStringExpander prefixExdr; 609 protected FlexibleStringExpander nameExdr; 610 protected Image image; 611 protected String urlMode = "intra-app"; 612 protected boolean fullPath = false; 613 protected boolean secure = false; 614 protected boolean encode = false; 615 616 public Link( Element linkElement, ModelMenuItem parentMenuItem) { 617 618 this.linkMenuItem = parentMenuItem; 619 setText(linkElement.getAttribute("text")); 620 setId(linkElement.getAttribute("id")); 621 setStyle(linkElement.getAttribute("style")); 622 setTarget(linkElement.getAttribute("target")); 623 setTargetWindow(linkElement.getAttribute("target-window")); 624 setPrefix(linkElement.getAttribute("prefix")); 625 setUrlMode(linkElement.getAttribute("url-mode")); 626 setFullPath(linkElement.getAttribute("full-path")); 627 setSecure(linkElement.getAttribute("secure")); 628 setEncode(linkElement.getAttribute("encode")); 629 setName(linkElement.getAttribute("name")); 630 Element imageElement = UtilXml.firstChildElement(linkElement, "image"); 631 if (imageElement != null) { 632 this.image = new Image(imageElement); 633 } 634 635 } 636 637 public void renderLinkString(StringBuffer buffer, Map context, MenuStringRenderer menuStringRenderer) { 638 menuStringRenderer.renderLink(buffer, context, this); 639 } 640 641 public String getText(Map context) { 642 String txt = this.textExdr.expandString(context); 643 if (UtilValidate.isEmpty(txt)) 644 txt = linkMenuItem.getTitle(context); 645 return txt; 646 } 647 648 public String getId(Map context) { 649 return this.idExdr.expandString(context); 650 } 651 652 public String getStyle(Map context) { 653 String style = this.styleExdr.expandString(context); 654 if (UtilValidate.isEmpty(style)) { 655 style = this.linkMenuItem.getWidgetStyle(); 656 } 657 return style; 658 } 659 660 public String getName(Map context) { 661 return this.nameExdr.expandString(context); 662 } 663 664 public String getTarget(Map context) { 665 return this.targetExdr.expandString(context); 666 } 667 668 public String getTargetWindow(Map context) { 669 return this.targetWindowExdr.expandString(context); 670 } 671 672 public String getUrlMode() { 673 return this.urlMode; 674 } 675 676 public String getPrefix(Map context) { 677 return this.prefixExdr.expandString(context); 678 } 679 680 public boolean getFullPath() { 681 return this.fullPath; 682 } 683 684 public boolean getSecure() { 685 return this.secure; 686 } 687 688 public boolean getEncode() { 689 return this.encode; 690 } 691 692 public Image getImage() { 693 return this.image; 694 } 695 696 public void setText( String val ) { 697 String textAttr = UtilFormatOut.checkNull(val); 698 this.textExdr = new FlexibleStringExpander(textAttr); 699 } 700 public void setId( String val ) { 701 this.idExdr = new FlexibleStringExpander(val); 702 } 703 public void setStyle( String val ) { 704 this.styleExdr = new FlexibleStringExpander(val); 705 } 706 public void setTarget( String val ) { 707 this.targetExdr = new FlexibleStringExpander(val); 708 } 709 public void setTargetWindow( String val ) { 710 this.targetWindowExdr = new FlexibleStringExpander(val); 711 } 712 public void setPrefix( String val ) { 713 this.prefixExdr = new FlexibleStringExpander(val); 714 } 715 public void setUrlMode( String val ) { 716 if (UtilValidate.isNotEmpty(val)) 717 this.urlMode = val; 718 } 719 public void setName( String val ) { 720 this.nameExdr = new FlexibleStringExpander(val); 721 } 722 public void setFullPath( String val ) { 723 String sFullPath = val; 724 if (sFullPath != null && sFullPath.equalsIgnoreCase("true")) 725 this.fullPath = true; 726 else 727 this.fullPath = false; 728 } 729 730 public void setSecure( String val ) { 731 String sSecure = val; 732 if (sSecure != null && sSecure.equalsIgnoreCase("true")) 733 this.secure = true; 734 else 735 this.secure = false; 736 } 737 738 public void setEncode( String val ) { 739 String sEncode = val; 740 if (sEncode != null && sEncode.equalsIgnoreCase("true")) 741 this.encode = true; 742 else 743 this.encode = false; 744 } 745 public void setImage( Image img ) { 746 this.image = img; 747 } 748 749 public ModelMenuItem getLinkMenuItem() { 750 return linkMenuItem; 751 } 752 753 } 754 755 public static class Image { 756 757 protected FlexibleStringExpander srcExdr; 758 protected FlexibleStringExpander idExdr; 759 protected FlexibleStringExpander styleExdr; 760 protected FlexibleStringExpander widthExdr; 761 protected FlexibleStringExpander heightExdr; 762 protected FlexibleStringExpander borderExdr; 763 protected String urlMode; 764 765 public Image( Element imageElement) { 766 767 setSrc(imageElement.getAttribute("src")); 768 setId(imageElement.getAttribute("id")); 769 setStyle(imageElement.getAttribute("style")); 770 setWidth(imageElement.getAttribute("width")); 771 setHeight(imageElement.getAttribute("height")); 772 setBorder(UtilFormatOut.checkEmpty(imageElement.getAttribute("border"), "0")); 773 setUrlMode(UtilFormatOut.checkEmpty(imageElement.getAttribute("url-mode"), "content")); 774 775 } 776 777 public void renderImageString(StringBuffer buffer, Map context, MenuStringRenderer menuStringRenderer) { 778 menuStringRenderer.renderImage(buffer, context, this); 779 } 780 781 public String getSrc(Map context) { 782 return this.srcExdr.expandString(context); 783 } 784 785 public String getId(Map context) { 786 return this.idExdr.expandString(context); 787 } 788 789 public String getStyle(Map context) { 790 return this.styleExdr.expandString(context); 791 } 792 793 public String getWidth(Map context) { 794 return this.widthExdr.expandString(context); 795 } 796 797 public String getHeight(Map context) { 798 return this.heightExdr.expandString(context); 799 } 800 801 public String getBorder(Map context) { 802 return this.borderExdr.expandString(context); 803 } 804 805 public String getUrlMode() { 806 return this.urlMode; 807 } 808 809 public void setSrc( String val ) { 810 String textAttr = UtilFormatOut.checkNull(val); 811 this.srcExdr = new FlexibleStringExpander(textAttr); 812 } 813 public void setId( String val ) { 814 this.idExdr = new FlexibleStringExpander(val); 815 } 816 public void setStyle( String val ) { 817 this.styleExdr = new FlexibleStringExpander(val); 818 } 819 public void setWidth( String val ) { 820 this.widthExdr = new FlexibleStringExpander(val); 821 } 822 public void setHeight( String val ) { 823 this.heightExdr = new FlexibleStringExpander(val); 824 } 825 public void setBorder( String val ) { 826 this.borderExdr = new FlexibleStringExpander(val); 827 } 828 public void setUrlMode( String val ) { 829 if (UtilValidate.isEmpty(val)) 830 this.urlMode = "content"; 831 else 832 this.urlMode = val; 833 } 834 835 } 836 837 838 } 839 | Popular Tags |