1 24 package org.ofbiz.widget.menu; 25 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.ofbiz.base.util.BshUtil; 33 import org.ofbiz.base.util.Debug; 34 import org.ofbiz.base.util.UtilValidate; 35 import org.ofbiz.base.util.UtilXml; 36 import org.ofbiz.base.util.string.FlexibleStringExpander; 37 import org.ofbiz.base.util.collections.FlexibleMapAccessor; 38 import org.ofbiz.entity.GenericDelegator; 39 import org.ofbiz.service.LocalDispatcher; 40 import org.w3c.dom.Element ; 41 42 import bsh.EvalError; 43 import bsh.Interpreter; 44 45 53 public class ModelMenu { 54 55 public static final String module = ModelMenu.class.getName(); 56 57 protected GenericDelegator delegator; 58 protected LocalDispatcher dispatcher; 59 60 protected String name; 61 protected String type; 62 protected String target; 63 protected String title; 64 protected String tooltip; 65 protected String defaultEntityName; 66 protected String defaultTitleStyle; 67 protected String defaultWidgetStyle; 68 protected String defaultTooltipStyle; 69 protected String defaultSelectedStyle; 70 protected String defaultMenuItemName; 71 protected String currentMenuItemName; 72 protected String defaultPermissionOperation; 73 protected String defaultPermissionEntityAction; 74 protected FlexibleStringExpander defaultAssociatedContentId; 75 protected String defaultPermissionStatusId; 76 protected String defaultPrivilegeEnumId; 77 protected String orientation = "horizontal"; 78 protected String menuWidth; 79 protected String defaultCellWidth; 80 protected Boolean defaultHideIfSelected; 81 protected String defaultDisabledTitleStyle; 82 protected FlexibleMapAccessor selectedMenuItemContextFieldName; 83 protected FlexibleStringExpander menuContainerStyleExdr; 84 protected String defaultAlign; 85 protected String defaultAlignStyle; 86 protected String fillStyle; 87 88 97 protected List menuItemList = new LinkedList (); 98 99 103 protected Map menuItemMap = new HashMap (); 104 105 protected List actions; 106 107 108 110 public ModelMenu() {} 111 112 113 public ModelMenu(Element menuElement, GenericDelegator delegator, LocalDispatcher dispatcher) { 114 this.delegator = delegator; 115 this.dispatcher = dispatcher; 116 117 String parentResource = menuElement.getAttribute("extends-resource"); 119 String parentMenu = menuElement.getAttribute("extends"); 120 if (parentMenu.length() > 0 && !parentMenu.equals(menuElement.getAttribute("name"))) { 122 ModelMenu parent = null; 123 if (parentResource.length() > 0) { 125 try { 126 parent = MenuFactory.getMenuFromClass(parentResource, parentMenu, delegator, dispatcher); 127 } catch (Exception e) { 128 Debug.logError(e, "Failed to load parent menu definition '" + parentMenu + "' at resource '" + parentResource + "'", module); 129 } 130 } else { 131 Element rootElement = menuElement.getOwnerDocument().getDocumentElement(); 133 List menuElements = UtilXml.childElementList(rootElement, "menu"); 134 Iterator menuElementIter = menuElements.iterator(); 137 while (menuElementIter.hasNext()) { 138 Element menuElementEntry = (Element ) menuElementIter.next(); 139 if (menuElementEntry.getAttribute("name").equals(parentMenu)) { 140 parent = new ModelMenu(menuElementEntry, delegator, dispatcher); 141 break; 142 } 143 } 144 if (parent == null) { 145 Debug.logError("Failed to find parent menu defenition '" + parentMenu + "' in same document.", module); 146 } 147 } 148 149 if (parent != null) { 150 this.type = parent.type; 151 this.target = parent.target; 152 this.title = parent.title; 153 this.tooltip = parent.tooltip; 154 this.tooltip = parent.tooltip; 155 this.defaultEntityName = parent.defaultEntityName; 156 this.defaultTitleStyle = parent.defaultTitleStyle; 157 this.defaultSelectedStyle = parent.defaultSelectedStyle; 158 this.defaultWidgetStyle = parent.defaultWidgetStyle; 159 this.defaultTooltipStyle = parent.defaultTooltipStyle; 160 this.defaultMenuItemName = parent.defaultMenuItemName; 161 this.menuItemList = parent.menuItemList; 162 this.menuItemMap = parent.menuItemMap; 163 this.defaultPermissionOperation = parent.defaultPermissionOperation; 164 this.defaultPermissionEntityAction = parent.defaultPermissionEntityAction; 165 this.defaultAssociatedContentId = parent.defaultAssociatedContentId; 166 this.defaultPermissionStatusId = parent.defaultPermissionStatusId; 167 this.defaultPrivilegeEnumId = parent.defaultPrivilegeEnumId; 168 this.defaultHideIfSelected = parent.defaultHideIfSelected; 169 this.orientation = parent.orientation; 170 this.menuWidth = parent.menuWidth; 171 this.defaultCellWidth = parent.defaultCellWidth; 172 this.defaultDisabledTitleStyle = parent.defaultDisabledTitleStyle; 173 this.defaultAlign = parent.defaultAlign; 174 this.defaultAlignStyle = parent.defaultAlignStyle; 175 this.fillStyle = parent.fillStyle; 176 } 177 } 178 179 this.name = menuElement.getAttribute("name"); 180 if (this.type == null || menuElement.hasAttribute("type")) 181 this.type = menuElement.getAttribute("type"); 182 if (this.target == null || menuElement.hasAttribute("target")) 183 this.target = menuElement.getAttribute("target"); 184 if (this.title == null || menuElement.hasAttribute("title")) 185 this.title = menuElement.getAttribute("title"); 186 if (this.tooltip == null || menuElement.hasAttribute("tooltip")) 187 this.tooltip = menuElement.getAttribute("tooltip"); 188 if (this.defaultEntityName == null || menuElement.hasAttribute("default-entity-name")) 189 this.defaultEntityName = menuElement.getAttribute("default-entity-name"); 190 if (this.defaultTitleStyle == null || menuElement.hasAttribute("default-title-style")) 191 this.defaultTitleStyle = menuElement.getAttribute("default-title-style"); 192 if (this.defaultSelectedStyle == null || menuElement.hasAttribute("default-selected-style")) 193 this.defaultSelectedStyle = menuElement.getAttribute("default-selected-style"); 194 if (this.defaultWidgetStyle == null || menuElement.hasAttribute("default-widget-style")) 195 this.defaultWidgetStyle = menuElement.getAttribute("default-widget-style"); 196 if (this.defaultTooltipStyle == null || menuElement.hasAttribute("default-tooltip-style")) 197 this.defaultTooltipStyle = menuElement.getAttribute("default-tooltip-style"); 198 if (this.defaultMenuItemName == null || menuElement.hasAttribute("default-menu-item-name")) 199 this.defaultMenuItemName = menuElement.getAttribute("default-menu-item-name"); 200 if (this.defaultPermissionOperation == null || menuElement.hasAttribute("default-permission-operation")) 201 this.defaultPermissionOperation = menuElement.getAttribute("default-permission-operation"); 202 if (this.defaultPermissionEntityAction == null || menuElement.hasAttribute("default-permission-entity-action")) 203 this.defaultPermissionEntityAction = menuElement.getAttribute("default-permission-entity-action"); 204 if (this.defaultPermissionStatusId == null || menuElement.hasAttribute("defaultPermissionStatusId")) 205 this.defaultPermissionStatusId = menuElement.getAttribute("default-permission-status-id"); 206 if (this.defaultPrivilegeEnumId == null || menuElement.hasAttribute("defaultPrivilegeEnumId")) 207 this.defaultPrivilegeEnumId = menuElement.getAttribute("default-privilege-enum-id"); 208 if (this.defaultAssociatedContentId == null || menuElement.hasAttribute("defaultAssociatedContentId")) 209 this.setDefaultAssociatedContentId( menuElement.getAttribute("default-associated-content-id")); 210 if (this.orientation == null || menuElement.hasAttribute("orientation")) 211 this.orientation = menuElement.getAttribute("orientation"); 212 if (this.menuWidth == null || menuElement.hasAttribute("menu-width")) 213 this.menuWidth = menuElement.getAttribute("menu-width"); 214 if (this.defaultCellWidth == null || menuElement.hasAttribute("default-cell-width")) 215 this.defaultCellWidth = menuElement.getAttribute("default-cell-width"); 216 if (menuElement.hasAttribute("default-hide-if-selected")) { 217 String val = menuElement.getAttribute("default-hide-if-selected"); 218 if (val != null && val.equalsIgnoreCase("true")) 220 defaultHideIfSelected = new Boolean (true); 221 else 222 defaultHideIfSelected = new Boolean (false); 223 } 224 if (this.defaultDisabledTitleStyle == null || menuElement.hasAttribute("default-disabled-title-style")) 225 this.defaultDisabledTitleStyle = menuElement.getAttribute("default-disabled-title-style"); 226 if (this.selectedMenuItemContextFieldName == null || menuElement.hasAttribute("selected-menuitem-context-field-name")) 227 this.selectedMenuItemContextFieldName = new FlexibleMapAccessor(menuElement.getAttribute("selected-menuitem-context-field-name")); 228 if (this.menuContainerStyleExdr == null || menuElement.hasAttribute("menu-container-style")) 229 this.setMenuContainerStyle(menuElement.getAttribute("menu-container-style")); 230 if (this.defaultAlign == null || menuElement.hasAttribute("default-align")) 231 this.defaultAlign = menuElement.getAttribute("default-align"); 232 if (this.defaultAlignStyle == null || menuElement.hasAttribute("default-align-style")) 233 this.defaultAlignStyle = menuElement.getAttribute("default-align-style"); 234 if (this.fillStyle == null || menuElement.hasAttribute("fill-style")) 235 this.fillStyle = menuElement.getAttribute("fill-style"); 236 237 Element actionsElement = UtilXml.firstChildElement(menuElement, "actions"); 239 if (actionsElement != null) { 240 this.actions = ModelMenuAction.readSubActions(this, actionsElement); 241 } 242 243 List itemElements = UtilXml.childElementList(menuElement, "menu-item"); 245 Iterator itemElementIter = itemElements.iterator(); 246 while (itemElementIter.hasNext()) { 247 Element itemElement = (Element ) itemElementIter.next(); 248 ModelMenuItem modelMenuItem = new ModelMenuItem(itemElement, this); 249 modelMenuItem = this.addUpdateMenuItem(modelMenuItem); 250 } 251 } 252 257 public ModelMenuItem addUpdateMenuItem(ModelMenuItem modelMenuItem) { 258 259 ModelMenuItem existingMenuItem = (ModelMenuItem) this.menuItemMap.get(modelMenuItem.getName()); 261 if (existingMenuItem != null) { 262 existingMenuItem.mergeOverrideModelMenuItem(modelMenuItem); 264 return existingMenuItem; 265 } else { 266 this.menuItemList.add(modelMenuItem); 268 this.menuItemMap.put(modelMenuItem.getName(), modelMenuItem); 269 return modelMenuItem; 270 } 271 } 272 273 public ModelMenuItem getModelMenuItemByName(String name) { 274 ModelMenuItem existingMenuItem = (ModelMenuItem) this.menuItemMap.get(name); 275 return existingMenuItem; 276 } 277 278 public ModelMenuItem getModelMenuItemByContentId(String contentId, Map context) { 279 280 ModelMenuItem existingMenuItem = null; 281 if (UtilValidate.isEmpty(contentId)) 282 return existingMenuItem; 283 Iterator iter = menuItemList.iterator(); 284 while (iter.hasNext()) { 285 ModelMenuItem mi = (ModelMenuItem) iter.next(); 286 String assocContentId = mi.getAssociatedContentId(context); 287 if (contentId.equals(assocContentId)) { 288 existingMenuItem = mi; 289 break; 290 } 291 } 292 return existingMenuItem; 293 } 294 295 310 public void renderMenuString(StringBuffer buffer, Map context, MenuStringRenderer menuStringRenderer) { 311 312 boolean passed = true; 313 314 if (passed) { 316 ModelMenuAction.runSubActions(this.actions, context); 317 if ("simple".equals(this.type)) { 318 this.renderSimpleMenuString(buffer, context, menuStringRenderer); 319 } else { 320 throw new IllegalArgumentException ("The type " + this.getType() + " is not supported for menu with name " + this.getName()); 321 } 322 } 323 } 325 326 public void renderSimpleMenuString(StringBuffer buffer, Map context, MenuStringRenderer menuStringRenderer) { 327 330 menuStringRenderer.renderMenuOpen(buffer, context, this); 332 333 menuStringRenderer.renderFormatSimpleWrapperOpen(buffer, context, this); 335 336 Iterator iter = menuItemList.iterator(); 340 while (iter.hasNext()) { 341 ModelMenuItem item = (ModelMenuItem)iter.next(); 342 item.renderMenuItemString(buffer, context, menuStringRenderer); 343 } 344 345 menuStringRenderer.renderFormatSimpleWrapperClose(buffer, context, this); 347 348 menuStringRenderer.renderMenuClose(buffer, context, this); 350 } 351 352 353 public LocalDispatcher getDispacher() { 354 return this.dispatcher; 355 } 356 357 public GenericDelegator getDelegator() { 358 return this.delegator; 359 } 360 361 364 public String getDefaultEntityName() { 365 return this.defaultEntityName; 366 } 367 368 371 public String getDefaultAlign() { 372 return this.defaultAlign; 373 } 374 375 378 public String getDefaultAlignStyle() { 379 return this.defaultAlignStyle; 380 } 381 382 383 386 public String getDefaultTitleStyle() { 387 return this.defaultTitleStyle; 388 } 389 390 393 public String getDefaultDisabledTitleStyle() { 394 return this.defaultDisabledTitleStyle; 395 } 396 397 400 public String getDefaultSelectedStyle() { 401 return this.defaultSelectedStyle; 402 } 403 404 407 public String getDefaultWidgetStyle() { 408 return this.defaultWidgetStyle; 409 } 410 411 414 public String getDefaultTooltipStyle() { 415 return this.defaultTooltipStyle; 416 } 417 418 421 public String getDefaultMenuItemName() { 422 return this.defaultMenuItemName; 423 } 424 425 428 public String getFillStyle() { 429 return this.fillStyle; 430 } 431 432 435 public String getSelectedMenuItemContextFieldName(Map context) { 436 return (String )this.selectedMenuItemContextFieldName.get(context); 437 } 438 439 442 public String getCurrentMenuItemName() { 443 if (UtilValidate.isNotEmpty(this.currentMenuItemName)) 444 return this.currentMenuItemName; 445 else 446 return this.defaultMenuItemName; 447 } 448 449 450 453 public String getName() { 454 return this.name; 455 } 456 457 public String getCurrentMenuName(Map context) { 458 return this.name; 459 } 460 461 462 465 public String getTitle() { 466 return this.title; 467 } 468 469 472 public String getTooltip() { 473 return this.tooltip; 474 } 475 476 479 public String getType() { 480 return this.type; 481 } 482 483 public Interpreter getBshInterpreter(Map context) throws EvalError { 484 Interpreter bsh = (Interpreter) context.get("bshInterpreter"); 485 if (bsh == null) { 486 bsh = BshUtil.makeInterpreter(context); 487 context.put("bshInterpreter", bsh); 488 } 489 return bsh; 490 } 491 492 495 public void setDefaultEntityName(String string) { 496 this.defaultEntityName = string; 497 } 498 499 500 503 public void setDefaultTitleStyle(String string) { 504 this.defaultTitleStyle = string; 505 } 506 507 510 public void setDefaultSelectedStyle(String string) { 511 this.defaultSelectedStyle = string; 512 } 513 514 517 public void setDefaultWidgetStyle(String string) { 518 this.defaultWidgetStyle = string; 519 } 520 521 524 public void setDefaultTooltipStyle(String string) { 525 this.defaultTooltipStyle = string; 526 } 527 528 531 public void setDefaultMenuItemName(String string) { 532 this.defaultMenuItemName = string; 533 } 534 535 538 public void setCurrentMenuItemName(String string) { 539 this.currentMenuItemName = string; 540 } 541 542 543 546 public void setName(String string) { 547 this.name = string; 548 } 549 550 553 public void setTarget(String string) { 554 this.target = string; 555 } 556 557 560 public void setTitle(String string) { 561 this.title = string; 562 } 563 564 567 public void setTooltip(String string) { 568 this.tooltip = string; 569 } 570 571 574 public void setType(String string) { 575 this.type = string; 576 } 577 578 581 public void setDefaultAssociatedContentId(String string) { 582 this.defaultAssociatedContentId = new FlexibleStringExpander(string); 583 } 584 585 588 public void setMenuContainerStyle(String string) { 589 this.menuContainerStyleExdr = new FlexibleStringExpander(string); 590 } 591 592 595 public String getDefaultAssociatedContentId(Map context) { 596 return defaultAssociatedContentId.expandString(context); 597 } 598 601 public String getMenuContainerStyle(Map context) { 602 return menuContainerStyleExdr.expandString(context); 603 } 604 605 608 public void setDefaultPermissionOperation(String string) { 609 this.defaultPermissionOperation = string; 610 } 611 612 615 public String getDefaultPermissionStatusId() { 616 return this.defaultPermissionStatusId; 617 } 618 619 622 public void setDefaultPermissionStatusId(String string) { 623 this.defaultPermissionStatusId = string; 624 } 625 626 629 public void setDefaultPrivilegeEnumId(String string) { 630 this.defaultPrivilegeEnumId = string; 631 } 632 633 636 public String getDefaultPrivilegeEnumId() { 637 return this.defaultPrivilegeEnumId; 638 } 639 640 643 public void setOrientation(String string) { 644 this.orientation = string; 645 } 646 647 650 public String getOrientation() { 651 return this.orientation; 652 } 653 654 657 public void setMenuWidth(String string) { 658 this.menuWidth = string; 659 } 660 661 664 public String getMenuWidth() { 665 return this.menuWidth; 666 } 667 668 671 public void setDefaultCellWidth(String string) { 672 this.defaultCellWidth = string; 673 } 674 675 678 public String getDefaultCellWidth() { 679 return this.defaultCellWidth; 680 } 681 682 685 public String getDefaultPermissionOperation() { 686 return this.defaultPermissionOperation; 687 } 688 689 692 public void setDefaultPermissionEntityAction(String string) { 693 this.defaultPermissionEntityAction = string; 694 } 695 696 699 public String getDefaultPermissionEntityAction() { 700 return this.defaultPermissionEntityAction; 701 } 702 703 706 public void setDefaultHideIfSelected(Boolean val) { 707 this.defaultHideIfSelected = val; 708 } 709 710 713 public Boolean getDefaultHideIfSelected() { 714 return this.defaultHideIfSelected; 715 } 716 717 public ModelMenuItem getCurrentMenuItem() { 718 719 ModelMenuItem currentMenuItem = (ModelMenuItem)menuItemMap.get(this.currentMenuItemName); 720 if (currentMenuItem == null) { 721 currentMenuItem = (ModelMenuItem)menuItemMap.get(this.defaultMenuItemName); 722 if (currentMenuItem == null && menuItemList.size() > 0) { 723 currentMenuItem = (ModelMenuItem)menuItemList.get(0); 724 } 725 } 726 return currentMenuItem; 727 } 728 729 public List getMenuItemList() { 730 return menuItemList; 731 } 732 733 public void dump(StringBuffer buffer ) { 734 buffer.append("ModelMenu:" 735 + "\n name=" + this.name 736 + "\n type=" + this.type 737 + "\n target=" + this.target 738 + "\n title=" + this.title 739 + "\n tooltip=" + this.tooltip 740 + "\n defaultEntityName=" + this.defaultEntityName 741 + "\n defaultTitleStyle=" + this.defaultTitleStyle 742 + "\n defaultWidgetStyle=" + this.defaultWidgetStyle 743 + "\n defaultTooltipStyle=" + this.defaultTooltipStyle 744 + "\n defaultSelectedStyle=" + this.defaultSelectedStyle 745 + "\n defaultMenuItemName=" + this.defaultMenuItemName 746 + "\n currentMenuItemName=" + this.currentMenuItemName 747 + "\n\n"); 748 749 Iterator iter = menuItemList.iterator(); 750 while (iter.hasNext()) { 751 ModelMenuItem menuItem = (ModelMenuItem)iter.next(); 752 menuItem.dump(buffer); 753 } 754 755 return; 756 } 757 758 } 759 | Popular Tags |