Esempio n. 1
0
  /*
   * (non-Javadoc)
   *
   * @see com.vaadin.client.Paintable#updateFromUIDL(com.vaadin.client.UIDL,
   * com.vaadin.client.ApplicationConnection)
   */
  @Override
  public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
    // Save details
    getWidget().client = client;
    getWidget().paintableId = uidl.getId();

    getWidget().readonly = isReadOnly();
    getWidget().updateReadOnly();

    if (!isRealUpdate(uidl)) {
      return;
    }

    // Inverse logic here to make the default case (text input enabled)
    // work without additional UIDL messages
    boolean noTextInput =
        uidl.hasAttribute(ComboBoxConstants.ATTR_NO_TEXT_INPUT)
            && uidl.getBooleanAttribute(ComboBoxConstants.ATTR_NO_TEXT_INPUT);
    getWidget().setTextInputEnabled(!noTextInput);

    // not a FocusWidget -> needs own tabindex handling
    getWidget().tb.setTabIndex(getState().tabIndex);

    if (uidl.hasAttribute("filteringmode")) {
      getWidget().filteringmode = FilteringMode.valueOf(uidl.getStringAttribute("filteringmode"));
    }

    getWidget().immediate = getState().immediate;

    getWidget().nullSelectionAllowed = uidl.hasAttribute("nullselect");

    getWidget().nullSelectItem =
        uidl.hasAttribute("nullselectitem") && uidl.getBooleanAttribute("nullselectitem");

    getWidget().currentPage = uidl.getIntVariable("page");

    if (uidl.hasAttribute("pagelength")) {
      getWidget().pageLength = uidl.getIntAttribute("pagelength");
    }

    if (uidl.hasAttribute(ComboBoxConstants.ATTR_INPUTPROMPT)) {
      // input prompt changed from server
      getWidget().inputPrompt = uidl.getStringAttribute(ComboBoxConstants.ATTR_INPUTPROMPT);
    } else {
      getWidget().inputPrompt = "";
    }

    getWidget().suggestionPopup.updateStyleNames(uidl, getState());

    getWidget().allowNewItem = uidl.hasAttribute("allownewitem");
    getWidget().lastNewItemString = null;

    final UIDL options = uidl.getChildUIDL(0);
    if (uidl.hasAttribute("totalMatches")) {
      getWidget().totalMatches = uidl.getIntAttribute("totalMatches");
    } else {
      getWidget().totalMatches = 0;
    }

    List<FilterSelectSuggestion> newSuggestions = new ArrayList<FilterSelectSuggestion>();

    for (final Iterator<?> i = options.getChildIterator(); i.hasNext(); ) {
      final UIDL optionUidl = (UIDL) i.next();
      final FilterSelectSuggestion suggestion = getWidget().new FilterSelectSuggestion(optionUidl);
      newSuggestions.add(suggestion);
    }

    // only close the popup if the suggestions list has actually changed
    boolean suggestionsChanged =
        !getWidget().initDone || !newSuggestions.equals(getWidget().currentSuggestions);

    oldSuggestionTextMatchTheOldSelection = false;

    if (suggestionsChanged) {
      oldSuggestionTextMatchTheOldSelection = isWidgetsCurrentSelectionTextInTextBox();
      getWidget().currentSuggestions.clear();

      if (!getWidget().waitingForFilteringResponse) {
        /*
         * Clear the current suggestions as the server response always
         * includes the new ones. Exception is when filtering, then we
         * need to retain the value if the user does not select any of
         * the options matching the filter.
         */
        getWidget().currentSuggestion = null;
        /*
         * Also ensure no old items in menu. Unless cleared the old
         * values may cause odd effects on blur events. Suggestions in
         * menu might not necessary exist in select at all anymore.
         */
        getWidget().suggestionPopup.menu.clearItems();
      }

      for (FilterSelectSuggestion suggestion : newSuggestions) {
        getWidget().currentSuggestions.add(suggestion);
      }
    }

    // handle selection (null or a single value)
    if (uidl.hasVariable("selected")) {
      String[] selectedKeys = uidl.getStringArrayVariable("selected");
      if (selectedKeys.length > 0) {
        performSelection(selectedKeys[0]);
      } else {
        resetSelection();
      }
    }

    if (getWidget().waitingForFilteringResponse
        && getWidget().lastFilter.toLowerCase().equals(uidl.getStringVariable("filter"))) {
      getWidget()
          .suggestionPopup
          .showSuggestions(
              getWidget().currentSuggestions, getWidget().currentPage, getWidget().totalMatches);
      getWidget().waitingForFilteringResponse = false;
      if (!getWidget().popupOpenerClicked
          && getWidget().selectPopupItemWhenResponseIsReceived != VFilterSelect.Select.NONE) {
        // we're paging w/ arrows
        if (getWidget().selectPopupItemWhenResponseIsReceived == VFilterSelect.Select.LAST) {
          getWidget().suggestionPopup.menu.selectLastItem();
        } else {
          getWidget().suggestionPopup.menu.selectFirstItem();
        }

        // This is used for paging so we update the keyboard selection
        // variable as well.
        MenuItem activeMenuItem = getWidget().suggestionPopup.menu.getSelectedItem();
        getWidget().suggestionPopup.menu.setKeyboardSelectedItem(activeMenuItem);

        // Update text field to contain the correct text
        getWidget().setTextboxText(activeMenuItem.getText());
        getWidget()
            .tb
            .setSelectionRange(
                getWidget().lastFilter.length(),
                activeMenuItem.getText().length() - getWidget().lastFilter.length());

        getWidget().selectPopupItemWhenResponseIsReceived = VFilterSelect.Select.NONE; // reset
      }
      if (getWidget().updateSelectionWhenReponseIsReceived) {
        getWidget().suggestionPopup.menu.doPostFilterSelectedItemAction();
      }
    }

    // Calculate minimum textarea width
    getWidget().updateSuggestionPopupMinWidth();

    getWidget().popupOpenerClicked = false;

    if (!getWidget().initDone) {
      getWidget().updateRootWidth();
    }

    // Focus dependent style names are lost during the update, so we add
    // them here back again
    if (getWidget().focused) {
      getWidget().addStyleDependentName("focus");
    }

    getWidget().initDone = true;
  }
Esempio n. 2
0
  @Override
  public void updateFromUIDL(final UIDL uidl, ApplicationConnection client) {
    ConnectorMap paintableMap = ConnectorMap.get(getConnection());
    getWidget().rendering = true;
    getWidget().id = getConnectorId();
    boolean firstPaint = getWidget().connection == null;
    getWidget().connection = client;

    getWidget().immediate = getState().immediate;
    getWidget().resizeLazy = uidl.hasAttribute(UIConstants.RESIZE_LAZY);
    String newTheme = uidl.getStringAttribute("theme");
    if (getWidget().theme != null && !newTheme.equals(getWidget().theme)) {
      // Complete page refresh is needed due css can affect layout
      // calculations etc
      getWidget().reloadHostPage();
    } else {
      getWidget().theme = newTheme;
    }
    // this also implicitly removes old styles
    String styles = "";
    styles += getWidget().getStylePrimaryName() + " ";
    if (ComponentStateUtil.hasStyles(getState())) {
      for (String style : getState().styles) {
        styles += style + " ";
      }
    }
    if (!client.getConfiguration().isStandalone()) {
      styles += getWidget().getStylePrimaryName() + "-embedded";
    }
    getWidget().setStyleName(styles.trim());

    getWidget().makeScrollable();

    clickEventHandler.handleEventHandlerRegistration();

    // Process children
    int childIndex = 0;

    // Open URL:s
    boolean isClosed = false; // was this window closed?
    while (childIndex < uidl.getChildCount()
        && "open".equals(uidl.getChildUIDL(childIndex).getTag())) {
      final UIDL open = uidl.getChildUIDL(childIndex);
      final String url = client.translateVaadinUri(open.getStringAttribute("src"));
      final String target = open.getStringAttribute("name");
      if (target == null) {
        // source will be opened to this browser window, but we may have
        // to finish rendering this window in case this is a download
        // (and window stays open).
        Scheduler.get()
            .scheduleDeferred(
                new Command() {
                  @Override
                  public void execute() {
                    VUI.goTo(url);
                  }
                });
      } else if ("_self".equals(target)) {
        // This window is closing (for sure). Only other opens are
        // relevant in this change. See #3558, #2144
        isClosed = true;
        VUI.goTo(url);
      } else {
        String options;
        boolean alwaysAsPopup = true;
        if (open.hasAttribute("popup")) {
          alwaysAsPopup = open.getBooleanAttribute("popup");
        }
        if (alwaysAsPopup) {
          if (open.hasAttribute("border")) {
            if (open.getStringAttribute("border").equals("minimal")) {
              options = "menubar=yes,location=no,status=no";
            } else {
              options = "menubar=no,location=no,status=no";
            }

          } else {
            options =
                "resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes";
          }

          if (open.hasAttribute("width")) {
            int w = open.getIntAttribute("width");
            options += ",width=" + w;
          }
          if (open.hasAttribute("height")) {
            int h = open.getIntAttribute("height");
            options += ",height=" + h;
          }

          Window.open(url, target, options);
        } else {
          open(url, target);
        }
      }
      childIndex++;
    }
    if (isClosed) {
      // don't render the content, something else will be opened to this
      // browser view
      getWidget().rendering = false;
      return;
    }

    // Handle other UIDL children
    UIDL childUidl;
    while ((childUidl = uidl.getChildUIDL(childIndex++)) != null) {
      String tag = childUidl.getTag().intern();
      if (tag == "actions") {
        if (getWidget().actionHandler == null) {
          getWidget().actionHandler = new ShortcutActionHandler(getWidget().id, client);
        }
        getWidget().actionHandler.updateActionMap(childUidl);
      } else if (tag == "notifications") {
        for (final Iterator<?> it = childUidl.getChildIterator(); it.hasNext(); ) {
          final UIDL notification = (UIDL) it.next();
          VNotification.showNotification(client, notification);
        }
      }
    }

    if (uidl.hasAttribute("focused")) {
      // set focused component when render phase is finished
      Scheduler.get()
          .scheduleDeferred(
              new Command() {
                @Override
                public void execute() {
                  ComponentConnector paintable =
                      (ComponentConnector) uidl.getPaintableAttribute("focused", getConnection());

                  final Widget toBeFocused = paintable.getWidget();
                  /*
                   * Two types of Widgets can be focused, either implementing
                   * GWT HasFocus of a thinner Vaadin specific Focusable
                   * interface.
                   */
                  if (toBeFocused instanceof com.google.gwt.user.client.ui.Focusable) {
                    final com.google.gwt.user.client.ui.Focusable toBeFocusedWidget =
                        (com.google.gwt.user.client.ui.Focusable) toBeFocused;
                    toBeFocusedWidget.setFocus(true);
                  } else if (toBeFocused instanceof Focusable) {
                    ((Focusable) toBeFocused).focus();
                  } else {
                    VConsole.log("Could not focus component");
                  }
                }
              });
    }

    // Add window listeners on first paint, to prevent premature
    // variablechanges
    if (firstPaint) {
      Window.addWindowClosingHandler(getWidget());
      Window.addResizeHandler(getWidget());
    }

    if (uidl.hasAttribute("scrollTo")) {
      final ComponentConnector connector =
          (ComponentConnector) uidl.getPaintableAttribute("scrollTo", getConnection());
      scrollIntoView(connector);
    }

    if (uidl.hasAttribute(UIConstants.LOCATION_VARIABLE)) {
      String location = uidl.getStringAttribute(UIConstants.LOCATION_VARIABLE);
      int fragmentIndex = location.indexOf('#');
      if (fragmentIndex >= 0) {
        getWidget().currentFragment = location.substring(fragmentIndex + 1);
      }
      if (!getWidget().currentFragment.equals(History.getToken())) {
        History.newItem(getWidget().currentFragment, true);
      }
    }

    if (firstPaint) {
      // Queue the initial window size to be sent with the following
      // request.
      getWidget().sendClientResized();
    }
    getWidget().rendering = false;
  }