public Menu addSubMenu(Menu parentMenu, String subMenuName, String menuItemNames) {
    // initialise the new sub menu
    Menu menu = new Menu();
    menu.setShowShadow(true);
    menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);

    MenuItem menuItem = new MenuItem(subMenuName);

    // create an array of menu item names
    String[] menuItems = process(menuItemNames);

    for (int i = 0; i < menuItems.length; i++) {
      // remove any whitespace
      String menuItemName = menuItems[i].replaceAll("\\W", "");

      if (menuItemName.contentEquals(SEPARATOR)) {
        MenuItemSeparator separator = new MenuItemSeparator();
        menu.addItem(separator);
        continue;
      }

      menuItem = new MenuItem(menuItems[i], getIcon(menuItems[i]));
      // menuItem.addClickHandler(clickHandler);
      menu.addItem(menuItem);
    }

    // add the sub menu to the menu item
    menuItem = new MenuItem(subMenuName);
    parentMenu.addItem(menuItem);
    menuItem.setSubmenu(menu);

    return menu;
  }
  public Menu addMenu(String menuName, int width, String menuItemNames, ClickHandler clickHandler) {
    // initialise the new menu
    Menu menu = new Menu();
    menu.setTitle(menuName);
    menu.setShowShadow(true);
    menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);
    menu.setWidth(width);

    // create an array of menu item names
    String[] menuItems = process(menuItemNames);

    for (int i = 0; i < menuItems.length; i++) {
      // remove any whitespace
      String menuItemName = menuItems[i].replaceAll("\\W", "");

      if (menuItemName.contentEquals(SEPARATOR)) {
        MenuItemSeparator separator = new MenuItemSeparator();
        menu.addItem(separator);
        continue;
      }

      MenuItem menuItem = new MenuItem(menuItems[i], getIcon(menuItems[i]));
      menuItem.addClickHandler(clickHandler);
      menu.addItem(menuItem);
    }

    Menu[] menus = new Menu[1];
    menus[0] = menu;
    menuBar.addMenus(menus, menuPosition);
    menuPosition++;

    return menus[0];
  }
  public Menu addMenu(String menuName, int width) {
    // initialise the new menu
    Menu menu = new Menu();
    menu.setTitle(menuName);
    menu.setShowShadow(true);
    menu.setShadowDepth(DEFAULT_SHADOW_DEPTH);
    menu.setWidth(width);

    Menu[] menus = new Menu[1];
    menus[0] = menu;
    menuBar.addMenus(menus, menuPosition);
    menuPosition++;

    return menu;
  }
  private Canvas buildUserInfo() {
    HLayout userFields = new HLayout();
    userFields.setAlign(Alignment.RIGHT);
    userFields.addMember(buildUserImage());
    LayoutSpacer sp1 = new LayoutSpacer();
    sp1.setWidth(8);
    userFields.addMember(sp1);

    // Label userLabel = new Label(SecurityManager.USER.getUserName());
    //  userLabel.setBaseStyle("userText");
    //  userLabel.setWidth(1);
    //   userLabel.setOverflow(Overflow.VISIBLE);
    //   userFields.addMember(userLabel);

    //  userFields.addMember(buildLogoutImage());

    Menu menu = new Menu();
    menu.setShowShadow(true);
    menu.setShadowDepth(10);
    menu.setShowIcons(false);

    MenuItem logout = new MenuItem("Logout");
    MenuItem edit = new MenuItem("Edit ...");
    MenuItem changePassword = new MenuItem("Change Password ...");

    menu.setItems(edit, changePassword, logout);

    changePassword.addClickHandler(
        new com.smartgwt.client.widgets.menu.events.ClickHandler() {
          @Override
          public void onClick(MenuItemClickEvent event) {
            redirect(GWT.getHostPageBaseURL() + "blcadmin/changePassword");
          }
        });
    //        changePassword.addClickHandler(new
    // com.smartgwt.client.widgets.menu.events.ClickHandler() {
    //                    @Override
    //                    public void onClick(MenuItemClickEvent event) {
    //                        final DynamicEntityDataSource userDS = new
    // DynamicEntityDataSource(CeilingEntities.ADMIN_USER);
    //                        userDS.buildFields(null, false, new AsyncCallbackAdapter() {
    //                            public void onSetupSuccess(DataSource ds) {
    //                                //strip out password validation
    //                                userDS.getField("password").setValidators();
    //
    //                                AdminUser currentUser = SecurityManager.USER;
    //                                Record userRecord = new Record();
    //                                userRecord.setAttribute("id", currentUser.getId());
    //                                userRecord.setAttribute("login", currentUser.getUserName());
    //                                userRecord.setAttribute("_type", new
    // String[]{EntityImplementations.ADMIN_USER});
    //
    //                                EntityEditDialog ed = new EntityEditDialog();
    //
    //                                ed.editRecord("Change Password", userDS, userRecord, new
    // ItemEditedHandler() {
    //                                    public void onItemEdited(ItemEdited event) {
    //                                        String currentPage =
    // BLCLaunch.getSelectedPage(History.getToken());
    //                                        if ("User Management".equals(currentPage)) {
    //                                            buildHistoryNewItem(currentPage,
    // BLCLaunch.getSelectedModule(History.getToken()), event.getRecord().getAttribute("id"));
    //                                        }
    //                                    }
    //                                }, new String[]{"password"}, new String[]{}, false);
    //                            }
    //                        });
    //
    //                    }
    //                });

    edit.addClickHandler(
        new com.smartgwt.client.widgets.menu.events.ClickHandler() {
          @Override
          public void onClick(MenuItemClickEvent event) {
            final DynamicEntityDataSource userDS =
                new DynamicEntityDataSource(CeilingEntities.ADMIN_USER);
            userDS.buildFields(
                null,
                false,
                new AsyncCallbackAdapter() {
                  public void onSetupSuccess(DataSource ds) {
                    AdminUser currentUser = SecurityManager.USER;
                    Record userRecord = new Record();
                    userRecord.setAttribute("id", currentUser.getId());
                    userRecord.setAttribute("name", currentUser.getName());
                    userRecord.setAttribute("email", currentUser.getEmail());
                    userRecord.setAttribute("phoneNumber", currentUser.getPhoneNumber());
                    userRecord.setAttribute("login", currentUser.getUserName());
                    userRecord.setAttribute(
                        "_type", new String[] {EntityImplementations.ADMIN_USER});

                    EntityEditDialog ed = new EntityEditDialog();

                    ed.editRecord(
                        "Edit User Information",
                        userDS,
                        userRecord,
                        new ItemEditedHandler() {
                          public void onItemEdited(ItemEdited event) {
                            SecurityManager.USER.setPhoneNumber(
                                event.getRecord().getAttribute("phoneNumber"));
                            SecurityManager.USER.setName(event.getRecord().getAttribute("name"));
                            SecurityManager.USER.setEmail(event.getRecord().getAttribute("email"));
                            String currentPage = BLCLaunch.getSelectedPage(History.getToken());
                            // If we are on the user module, reload the page with the specifically
                            // edited item.
                            if ("User Management".equals(currentPage)) {
                              buildHistoryNewItem(
                                  currentPage,
                                  BLCLaunch.getSelectedModule(History.getToken()),
                                  event.getRecord().getAttribute("id"));
                            }
                          }
                        },
                        null,
                        new String[] {"login", "activeStatusFlag", "password"},
                        false);
                  }
                });
          }
        });

    logout.addClickHandler(
        new com.smartgwt.client.widgets.menu.events.ClickHandler() {
          @Override
          public void onClick(MenuItemClickEvent event) {
            UrlBuilder builder = Window.Location.createUrlBuilder();
            builder.setPath(BLCMain.webAppContext + "/adminLogout.htm");
            builder.setHash(null);
            builder.setParameter("time", String.valueOf(System.currentTimeMillis()));
            Window.open(builder.buildString(), "_self", null);
          }
        });

    IMenuButton menuButton = new IMenuButton(SecurityManager.USER.getUserName(), menu);
    menuButton.setPadding(5);
    menuButton.setChildrenSnapResizeToGrid(true);
    menuButton.setOverflow(Overflow.VISIBLE);
    userFields.addMember(menuButton);

    LayoutSpacer sp2 = new LayoutSpacer();
    sp2.setWidth(200);
    userFields.addMember(sp2);

    return userFields;
  }
  /**
   * Instantiates a new side nav input tree.
   *
   * @param dispatcher the dispatcher
   * @param lang
   */
  private InputQueueTree(
      final DispatchAsync dispatcher, final LangConstants lang, final EventBus eventBus) {
    this.lang = lang;
    setWidth100();
    setHeight100();
    setCustomIconProperty("icon");
    setAnimateFolderTime(100);
    setAnimateFolders(true);
    setAnimateFolderSpeed(1000);
    setNodeIcon("silk/application_view_list.png");
    setFolderIcon("silk/folder.png");
    setShowOpenIcons(true);
    setShowDropIcons(false);
    setShowSortArrow(SortArrow.CORNER);
    setShowConnectors(true);
    setShowAllRecords(true);
    setLoadDataOnDemand(true);
    setCanSort(true);
    setAutoFetchData(true);
    setShowRoot(false);
    setSelectionType(SelectionStyle.SINGLE);
    setShowRollOverCanvas(true);

    MenuItem showItem = new MenuItem(lang.show(), "icons/16/structure.png");
    final Menu showMenu = new Menu();
    showMenu.setShowShadow(true);
    showMenu.setShadowDepth(10);
    showMenu.setItems(showItem);

    createItem = new MenuItem(lang.create(), "icons/16/structure_into.png");

    final Menu editMenu = new Menu();
    editMenu.setShowShadow(true);
    editMenu.setShadowDepth(10);
    setContextMenu(editMenu);

    dispatcher.execute(
        new HasUserRightsAction(new EDITOR_RIGHTS[] {EDITOR_RIGHTS.LONG_RUNNING_PROCESS}),
        new DispatchCallback<HasUserRightsResult>() {

          @Override
          public void callback(HasUserRightsResult result) {
            canLongProcess = result.getOk()[0];
          }
        });

    addCellContextClickHandler(
        new CellContextClickHandler() {

          @Override
          public void onCellContextClick(CellContextClickEvent event) {

            ListGridRecord record = event.getRecord();
            final String path = record.getAttribute(Constants.ATTR_ID);
            if (path != null && path.length() > 1 && path.substring(1).contains("/")) {
              // String model = path.substring(1, path.substring(1).indexOf("/") + 1);
              String id = path.substring(path.substring(1).indexOf("/") + 2);
              if (id.contains("/")) {
                id = id.substring(0, id.indexOf("/"));
              }

              MenuItem quartz = new MenuItem(lang.addToScheduler(), "icons/16/clock.png");
              quartz.addClickHandler(
                  new ClickHandler() {

                    @Override
                    public void onClick(MenuItemClickEvent event) {

                      String msg = event.getMenu().getEmptyMessage(); // duplicate code
                      String model = msg.substring(0, msg.indexOf("/"));
                      String path = msg.substring(msg.indexOf("/") + 1);

                      QuartzConvertImagesAction action = new QuartzConvertImagesAction(model, path);

                      DispatchCallback<QuartzConvertImagesResult> quartzConvertCallback =
                          new DispatchCallback<QuartzConvertImagesResult>() {

                            @Override
                            public void callback(QuartzConvertImagesResult result) {
                              if (result.getNumberOfImages() != null) {
                                SC.say(
                                    result.getNumberOfImages().toString()
                                        + " images will be converted");
                              } else {
                                SC.say("No images found");
                              }
                            }
                          };

                      dispatcher.execute(action, quartzConvertCallback);
                    }
                  });

              if (record.getAttributeAsBoolean(Constants.ATTR_INGEST_INFO)) {
                MenuItem ingestInfo = new MenuItem(lang.ingestInfo(), "icons/16/export1.png");
                ingestInfo.addClickHandler(
                    new ClickHandler() {

                      @Override
                      public void onClick(MenuItemClickEvent event) {
                        getIngestInfo(path, dispatcher, eventBus);
                      }
                    });
                if (canLongProcess) {
                  editMenu.setItems(createItem, ingestInfo, quartz);
                } else {
                  editMenu.setItems(createItem, ingestInfo);
                }
              } else {
                if (canLongProcess) {
                  editMenu.setItems(createItem, quartz);
                } else {
                  editMenu.setItems(createItem);
                }
              }

              editMenu.setEmptyMessage(path.substring(1, path.length()));
              editMenu.showContextMenu();
            } else {
              showMenu.showContextMenu();
            }
          }
        });
    addShowContextMenuHandler(
        new ShowContextMenuHandler() {

          @Override
          public void onShowContextMenu(ShowContextMenuEvent event) {
            event.cancel();
          }
        });

    eventBus.addHandler(
        RefreshTreeEvent.getType(),
        new RefreshTreeEvent.RefreshTreeHandler() {

          @Override
          public void onRefreshTree(RefreshTreeEvent event) {
            if (event.getTree() == NAME_OF_TREE.INPUT_QUEUE) refreshTree();
          }
        });

    TreeGridField field1 = new TreeGridField();
    field1.setCanFilter(true);
    field1.setName(Constants.ATTR_BARCODE);
    field1.setTitle("ID");
    field1.setCellFormatter(
        new CellFormatter() {

          @Override
          public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
            boolean ingestInfo = record.getAttributeAsBoolean(Constants.ATTR_INGEST_INFO);
            if (ingestInfo) {
              return record.getAttribute(Constants.ATTR_BARCODE) + HTML_TICK_CODE;

            } else {
              return record.getAttribute(Constants.ATTR_BARCODE);
            }
          }
        });
    ListGridField nameField = new ListGridField();
    nameField.setCanFilter(true);
    nameField.setName(Constants.ATTR_NAME);
    nameField.setTitle(lang.name());

    setFields(field1, nameField);
    setDataSource(new InputTreeGwtRPCDS(dispatcher, lang));
  }