@SuppressWarnings("unchecked")
  @Override
  protected void showSuggestions(
      final SuggestBox suggestBox,
      Collection<? extends Suggestion> suggestions,
      boolean isDisplayStringHTML,
      boolean isAutoSelectEnabled,
      final SuggestionCallback callback) {

    // Hide popup if not needed
    boolean anySuggestions = suggestions != null && suggestions.size() > 0;
    if (!anySuggestions) {
      hideSuggestions();
      return;
    }

    // Create suggestion popup and suggestions table widget
    if (suggestionPopup == null) {
      suggestionPopup = createPopup();
    }

    // Hide the popup before we manipulate the menu within it.
    if (suggestionPopup.isAttached()) {
      suggestionPopup.hide();
    }

    // Link the popup autoHide to the TextBox.
    // If the suggest box has changed, free the old one first.
    if (suggestBox != null) {
      suggestionPopup.removeAutoHidePartner(suggestBox.getElement());
      suggestionPopup.addAutoHidePartner(suggestBox.getElement());
    }

    // Create suggestions table widget
    suggestionsTable =
        getSuggestionCellList(
            (Collection<SearchSuggestion>) suggestions, suggestBox, suggestionPopup);

    // Add table to popup
    suggestionPopup.setWidget(suggestionsTable);

    // Show the popup under the TextBox.
    suggestionPopup.showRelativeTo(searchBoxPanel);

    int searchBoxWidth = searchBoxPanel.getElement().getClientWidth();
    Element table =
        (Element)
            suggestionPopup.getElement().getElementsByTagName("table").getItem(0); // $NON-NLS-1$

    suggestionPopup.getElement().getStyle().setWidth(searchBoxWidth, Unit.PX);
    table.getStyle().setWidth(searchBoxWidth, Unit.PX);
  }
Esempio n. 2
0
  private void showSuggestions(Collection<? extends Suggestion> suggestions) {
    if (suggestions.size() > 0) {

      // Hide the popup before we manipulate the menu within it. If we do not
      // do this, some browsers will redraw the popup as items are removed
      // and added to the menu.
      boolean isAnimationEnabled = suggestionPopup.isAnimationEnabled();
      if (suggestionPopup.isAttached()) {
        suggestionPopup.hide();
      }

      suggestionMenu.clearItems();

      for (Suggestion curSuggestion : suggestions) {
        final SuggestionMenuItem menuItem =
            new SuggestionMenuItem(
                curSuggestion,
                oracle.isDisplayStringHTML(),
                new SuggestionCompletionCommand(curSuggestion));

        suggestionMenu.addItem(menuItem);
      }

      class TextBoxSkewWrapper extends TextBox {
        private TextBoxBase wrapped;
        private int skewWidth;
        private int skewHeight;

        public TextBoxSkewWrapper(TextBoxBase textBoxBase, int skewWidth, int skewHeight) {
          this.wrapped = textBoxBase;
          this.skewWidth = skewWidth;
          this.skewHeight = skewHeight;
        }

        @Override
        public int getOffsetWidth() {
          return wrapped.getOffsetWidth();
        }

        @Override
        public int getOffsetHeight() {
          return wrapped.getOffsetHeight();
        }

        @Override
        public int getAbsoluteLeft() {
          return wrapped.getAbsoluteLeft() + skewWidth;
        }

        @Override
        public int getAbsoluteTop() {
          return wrapped.getAbsoluteTop() + skewHeight;
        }
      }

      suggestionPopup.showRelativeTo(new TextBoxSkewWrapper(getTextBox(), 0, 5));
      suggestionPopup.setAnimationEnabled(isAnimationEnabled);
    } else {
      suggestionPopup.hide();
    }

    setFocus(true);
  }