Beispiel #1
0
  protected MenuItem selectMenu(List<MenuItem> m) {
    // Print Menu Items
    int i = 1;
    for (MenuItem menuItem : m) {
      System.out.print("[" + i + "] " + menuItem.getCommand());
      if (menuItem.getDescription() != null) {
        System.out.print(" - " + menuItem.getDescription());
      }
      System.out.println();

      i++;
    }

    Scanner input = new Scanner(System.in);
    String command = input.next();
    if (commandMap.containsKey(command.toLowerCase())) {
      return commandMap.get(command.toLowerCase());
    } else {
      System.out.println("I don't know what '" + command + "' means.");
      return null;
    }
  }
Beispiel #2
0
  /*
   * Performs the action associated with the given menu item. If the item has
   * a popup associated with it, the popup will be shown. If it has a command
   * associated with it, and 'fireCommand' is true, then the command will be
   * fired. Popups associated with other items will be hidden.
   *
   * @param item the item whose popup is to be shown. @param fireCommand
   * <code>true</code> if the item's command should be fired,
   * <code>false</code> otherwise.
   */
  protected void doItemAction(final MenuItem item, boolean fireCommand) {
    // If the given item is already showing its menu, we're done.
    if ((shownChildMenu != null) && (item.getSubMenu() == shownChildMenu)) {
      return;
    }

    // If another item is showing its menu, then hide it.
    if (shownChildMenu != null) {
      shownChildMenu.onHide();
      popup.hide();
    }

    // If the item has no popup, optionally fire its command.
    if (item.getSubMenu() == null) {
      if (fireCommand) {
        // Close this menu and all of its parents.
        closeAllParents();

        // Fire the item's command.
        final Command cmd = item.getCommand();
        if (cmd != null) {
          Scheduler.get().scheduleDeferred(cmd);
        }
      }
      return;
    }

    // Ensure that the item is selected.
    selectItem(item);

    // Create a new popup for this item, and position it next to
    // the item (below if this is a horizontal menu bar, to the
    // right if it's a vertical bar).
    popup =
        new VOverlay(true) {
          {
            setWidget(item.getSubMenu());
            item.getSubMenu().onShow();
            setOwner(MenuBar.this);
          }

          @Override
          public boolean onEventPreview(Event event) {
            // Hook the popup panel's event preview. We use this to keep it
            // from
            // auto-hiding when the parent menu is clicked.
            switch (DOM.eventGetType(event)) {
              case Event.ONCLICK:
                // If the event target is part of the parent menu, suppress
                // the
                // event altogether.
                final Element target = DOM.eventGetTarget(event);
                final Element parentMenuElement = item.getParentMenu().getElement();
                if (DOM.isOrHasChild(parentMenuElement, target)) {
                  return false;
                }
                break;
            }

            return super.onEventPreview(event);
          }
        };
    popup.addPopupListener(this);

    if (vertical) {
      popup.setPopupPosition(item.getAbsoluteLeft() + item.getOffsetWidth(), item.getAbsoluteTop());
    } else {
      popup.setPopupPosition(
          item.getAbsoluteLeft(), item.getAbsoluteTop() + item.getOffsetHeight());
    }

    shownChildMenu = item.getSubMenu();
    item.getSubMenu().parentMenu = this;

    // Show the popup, ensuring that the menubar's event preview remains on
    // top
    // of the popup's.
    popup.show();
  }