Exemplo n.º 1
0
  /**
   * Handle Mouse Click
   *
   * @param tr
   */
  protected void onRowClicked(Element tr) {
    if (!isRowEnabled(tr) || tr == subPopupAnchor) {
      return;
    }

    int itemIndex = Integer.parseInt(tr.getAttribute("item-index"));
    Action menuItem = list.get(itemIndex);
    if (menuItem instanceof ActionGroup
        && (!((ActionGroup) menuItem).canBePerformed()
            && Utils.hasVisibleChildren(
                (ActionGroup) menuItem,
                presentationFactory,
                actionManager,
                managerProvider.get()))) {
      openSubPopup(tr);
    } else {
      if (actionSelectedHandler != null) {
        actionSelectedHandler.onActionSelected(menuItem);
      }
      ActionEvent e =
          new ActionEvent(
              presentationFactory.getPresentation(menuItem), actionManager, managerProvider.get());
      menuItem.actionPerformed(e);
    }
  }
Exemplo n.º 2
0
  /** @return true when at list one item from list of menu items has selected state. */
  private boolean hasCheckedItems() {
    for (int i = 0; i < list.size(); i++) {
      Action action = list.get(i);
      if (action instanceof ToggleAction) {

        ActionEvent e =
            new ActionEvent(
                presentationFactory.getPresentation(action), actionManager, managerProvider.get());
        if (((ToggleAction) action).isSelected(e)) {
          return true;
        }
      }
    }

    return false;
  }
Exemplo n.º 3
0
  private boolean isRowEnabled(Element tr) {
    if (tr == null) {
      return false;
    }

    String index = tr.getAttribute("item-index");
    if (index == null || "".equals(index)) {
      return false;
    }

    String enabled = tr.getAttribute("item-enabled");
    if (enabled == null || "".equals(enabled) || "false".equals(enabled)) {
      return false;
    }

    int itemIndex = Integer.parseInt(index);
    Action menuItem = list.get(itemIndex);
    return presentationFactory.getPresentation(menuItem).isEnabled();
  }
Exemplo n.º 4
0
  private void openSubPopup(final Element tableRowElement) {
    if (tableRowElement == null) {
      return;
    }

    if (openedSubPopup != null) {
      if (tableRowElement == subPopupAnchor) {
        return;
      }

      openedSubPopup.closePopup();
    }

    if (subPopupAnchor != null) {
      Element e = subPopupAnchor;
      subPopupAnchor = null;
      setStyleNormal(e);
    }

    subPopupAnchor = tableRowElement;
    setStyleHovered(subPopupAnchor);

    int itemIndex = Integer.parseInt(tableRowElement.getAttribute("item-index"));
    Action menuItem = list.get(itemIndex);

    String idPrefix = itemIdPrefix;
    if (idPrefix != null) {
      idPrefix += "/" + presentationFactory.getPresentation(menuItem).getText();
    }

    openedSubPopup =
        new PopupMenu(
            (ActionGroup) menuItem,
            actionManager,
            managerProvider,
            presentationFactory,
            lockLayer,
            actionSelectedHandler,
            keyBindingAgent,
            idPrefix);

    final int HORIZONTAL_OFFSET = 3;
    final int VERTICAL_OFFSET = 1;

    openedSubPopup.getElement().getStyle().setVisibility(Visibility.HIDDEN);
    lockLayer.add(openedSubPopup, 0, 0);

    Scheduler.get()
        .scheduleDeferred(
            new ScheduledCommand() {
              @Override
              public void execute() {
                int left = getAbsoluteLeft() + getOffsetWidth() - HORIZONTAL_OFFSET;
                int top =
                    tableRowElement.getAbsoluteTop() - lockLayer.getTopOffset() - VERTICAL_OFFSET;

                if (left + openedSubPopup.getOffsetWidth() > Window.getClientWidth()) {
                  if (left > openedSubPopup.getOffsetWidth()) {
                    left = getAbsoluteLeft() - openedSubPopup.getOffsetWidth() + HORIZONTAL_OFFSET;
                  } else {
                    int diff = left + openedSubPopup.getOffsetWidth() - Window.getClientWidth();
                    left -= diff;
                  }
                }

                if (top + openedSubPopup.getOffsetHeight() > Window.getClientHeight()) {
                  if (top > openedSubPopup.getOffsetHeight()) {
                    top =
                        tableRowElement.getAbsoluteTop()
                            - openedSubPopup.getOffsetHeight()
                            + VERTICAL_OFFSET;
                  } else {
                    int diff = top + openedSubPopup.getOffsetHeight() - Window.getClientHeight();
                    top -= diff;
                  }
                }

                openedSubPopup.getElement().getStyle().setLeft(left, Unit.PX);
                openedSubPopup.getElement().getStyle().setTop(top, Unit.PX);
                openedSubPopup.getElement().getStyle().setVisibility(Visibility.VISIBLE);
              }
            });
  }
Exemplo n.º 5
0
  /** Render Popup Menu component. */
  private void redraw() {
    String idPrefix = itemIdPrefix;
    if (idPrefix == null) {
      idPrefix = "";
    } else {
      idPrefix += "/";
    }

    table = new PopupMenuTable();
    table.setStyleName(POPUP_RESOURCES.popup().popupMenuTable());
    table.setCellPadding(0);
    table.setCellSpacing(0);

    for (int i = 0; i < list.size(); i++) {
      Action menuItem = list.get(i);

      if (menuItem instanceof Separator) {
        final String separatorText = ((Separator) menuItem).getText();
        if (separatorText == null) {
          table.getCellFormatter().setStyleName(i, 0, POPUP_RESOURCES.popup().popupMenuDelimiter());
        } else {
          table.setWidget(i, 0, new Label(separatorText));
          table
              .getCellFormatter()
              .setStyleName(i, 0, POPUP_RESOURCES.popup().popupMenuTextDelimiter());
        }
        table.getFlexCellFormatter().setColSpan(i, 0, hasCheckedItems ? 5 : 4);
      } else {
        Presentation presentation = presentationFactory.getPresentation(menuItem);

        if (presentation.getImageResource() != null) {
          Image image = new Image(presentation.getImageResource());
          table.setWidget(i, 0, image);

        } else if (presentation.getSVGResource() != null) {
          SVGImage image = new SVGImage(presentation.getSVGResource());
          table.setWidget(i, 0, image);
        } else if (presentation.getHTMLResource() != null) {
          table.setHTML(i, 0, presentation.getHTMLResource());
        }
        table
            .getCellFormatter()
            .setStyleName(
                i,
                0,
                presentation.isEnabled()
                    ? POPUP_RESOURCES.popup().popupMenuIconField()
                    : POPUP_RESOURCES.popup().popupMenuIconFieldDisabled());

        int work = 1;
        if (hasCheckedItems && menuItem instanceof ToggleAction) {
          ToggleAction toggleAction = (ToggleAction) menuItem;
          ActionEvent e =
              new ActionEvent(
                  presentationFactory.getPresentation(toggleAction),
                  actionManager,
                  managerProvider.get());

          if (toggleAction.isSelected(e)) {
            // Temporary solution
            table.setHTML(i, work, "<i class=\"fa fa-check\"></i>");
          }

          table
              .getCellFormatter()
              .setStyleName(
                  i,
                  work,
                  presentation.isEnabled()
                      ? POPUP_RESOURCES.popup().popupMenuCheckField()
                      : POPUP_RESOURCES.popup().popupMenuCheckFieldDisabled());
          work++;
        }

        table.setHTML(
            i,
            work,
            "<nobr id=\""
                + idPrefix
                + presentation.getText()
                + "\">"
                + presentation.getText()
                + "</nobr>");
        table
            .getCellFormatter()
            .setStyleName(
                i,
                work,
                presentation.isEnabled()
                    ? POPUP_RESOURCES.popup().popupMenuTitleField()
                    : POPUP_RESOURCES.popup().popupMenuTitleFieldDisabled());
        if (showTooltips) {
          Tooltip.create(
              (elemental.dom.Element) table.getCellFormatter().getElement(i, work),
              BOTTOM,
              MIDDLE,
              presentation.getText());
        }

        work++;
        String hotKey =
            KeyMapUtil.getShortcutText(
                keyBindingAgent.getKeyBinding(actionManager.getId(menuItem)));
        if (hotKey == null) {
          hotKey = "&nbsp;";
        } else {
          hotKey = "<nobr>&nbsp;" + hotKey + "&nbsp;</nobr>";
        }

        table.setHTML(i, work, hotKey);
        table
            .getCellFormatter()
            .setStyleName(
                i,
                work,
                presentation.isEnabled()
                    ? POPUP_RESOURCES.popup().popupMenuHotKeyField()
                    : POPUP_RESOURCES.popup().popupMenuHotKeyFieldDisabled());

        work++;

        if (menuItem instanceof ActionGroup
            && !(((ActionGroup) menuItem).canBePerformed()
                && !Utils.hasVisibleChildren(
                    (ActionGroup) menuItem,
                    presentationFactory,
                    actionManager,
                    managerProvider.get()))) {
          table.setWidget(i, work, new SVGImage(POPUP_RESOURCES.subMenu()));
          table
              .getCellFormatter()
              .setStyleName(
                  i,
                  work,
                  presentation.isEnabled()
                      ? POPUP_RESOURCES.popup().popupMenuSubMenuField()
                      : POPUP_RESOURCES.popup().popupMenuSubMenuFieldDisabled());
        } else {
          table
              .getCellFormatter()
              .setStyleName(
                  i,
                  work,
                  presentation.isEnabled()
                      ? POPUP_RESOURCES.popup().popupMenuSubMenuField()
                      : POPUP_RESOURCES.popup().popupMenuSubMenuFieldDisabled());
        }

        work++;

        table.getRowFormatter().getElement(i).setAttribute("item-index", Integer.toString(i));
        table
            .getRowFormatter()
            .getElement(i)
            .setAttribute("item-enabled", Boolean.toString(presentation.isEnabled()));

        String actionId = actionManager.getId(menuItem);
        String debugId;
        if (actionId == null) {
          debugId = idPrefix + menuItem.getTemplatePresentation().getText();
        } else {
          debugId = idPrefix + actionId;
        }
        UIObject.ensureDebugId(table.getRowFormatter().getElement(i), debugId);
      }
    }

    popupMenuPanel.add(table);
  }