示例#1
0
  private void buildStateModel() {
    stateModel = new DynamicMenuModel();
    DefaultMenuItem homeItem = new DefaultMenuItem();
    homeItem.setAjax(false);
    homeItem.setValue("home");
    homeItem.setId(
        FacesContext.getCurrentInstance().getViewRoot().createUniqueId() + "_state_home");
    homeItem.setImmediate(true);
    homeItem.setCommand("#{stateController.clearStateAndGoHome}");
    stateModel.addElement(homeItem);
    for (StateItem stateItem : stateItens) {
      DefaultMenuItem item = new DefaultMenuItem();
      item.setAjax(stateItem.isAjax());
      item.setGlobal(stateItem.isGlobal());
      item.setResetValues(stateItem.isResetValues());
      item.setTitle(stateItem.getTitle());
      item.setImmediate(stateItem.isImmediate());
      item.setValue(getItemValue(stateItem.getValue()));
      if (stateItem.getOncomplete() != null && !"".equals(stateItem.getOncomplete())) {
        item.setOncomplete(stateItem.getOncomplete());
      }
      item.setId(FacesContext.getCurrentInstance().getViewRoot().createUniqueId() + "_state");
      if (stateItem.getOutcome() != null && !"".equals(stateItem.getOutcome())) {
        item.setIncludeViewParams(true);
        StringBuilder url = new StringBuilder(stateItem.getOutcome());
        if (stateItem.isAddEntityIdParam()) {
          url.append("?id=").append(((BaseEntity) stateItem.getEntity()).getId());
        }
        if (url.toString().contains("?")) {
          url.append("&pullState=true");
        } else {
          url.append("?pullState=true"); // tell statePusher to not call preRenderView event
        }
        url.append("&itemIndex=").append(stateItens.indexOf(stateItem));
        item.setUrl(url.toString());
      } else { // if has not outcome set command, note that they are muttually exclusive:
               // http://stackoverflow.com/questions/16437336/using-both-setactionexpression-and-seturl-on-menuitem-object-is-not-working
        item.setCommand("#{stateController.pullStateItem(" + stateItens.indexOf(stateItem) + ")}");
      }
      if (!"".equals(stateItem.getUpdate())) {
        item.setUpdate(stateItem.getUpdate());
      }

      if (stateItens.indexOf(stateItem) == stateItens.size() - 1) {
        item.setDisabled(true);
        item.setStyleClass("ui-state-disabled");
      }
      stateModel.addElement(item);
    }
  }
  @PostConstruct
  public void init() {
    model = new DefaultMenuModel();

    // First submenu
    DefaultSubMenu firstSubmenu = new DefaultSubMenu("Dynamic Submenu");

    DefaultMenuItem item = new DefaultMenuItem("External");
    item.setUrl("http://www.primefaces.org");
    item.setIcon("ui-icon-home");
    firstSubmenu.addElement(item);

    model.addElement(firstSubmenu);

    // Second submenu
    DefaultSubMenu secondSubmenu = new DefaultSubMenu("Dynamic Actions");

    item = new DefaultMenuItem("Save");
    item.setIcon("ui-icon-disk");
    item.setCommand("#{menuView.save}");
    item.setUpdate("messages");
    secondSubmenu.addElement(item);

    item = new DefaultMenuItem("Delete");
    item.setIcon("ui-icon-close");
    item.setCommand("#{menuView.delete}");
    item.setAjax(false);
    secondSubmenu.addElement(item);

    item = new DefaultMenuItem("Redirect");
    item.setIcon("ui-icon-search");
    item.setCommand("#{menuView.redirect}");
    secondSubmenu.addElement(item);

    model.addElement(secondSubmenu);
  }