You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VFilterSelectPaintable.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Iterator;
  6. import com.google.gwt.core.client.GWT;
  7. import com.google.gwt.user.client.ui.Widget;
  8. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  9. import com.vaadin.terminal.gwt.client.UIDL;
  10. import com.vaadin.terminal.gwt.client.Util;
  11. import com.vaadin.terminal.gwt.client.ui.VFilterSelect.FilterSelectSuggestion;
  12. public class VFilterSelectPaintable extends AbstractComponentConnector implements
  13. SimpleManagedLayout {
  14. /*
  15. * (non-Javadoc)
  16. *
  17. * @see
  18. * com.vaadin.terminal.gwt.client.Paintable#updateFromUIDL(com.vaadin.terminal
  19. * .gwt.client.UIDL, com.vaadin.terminal.gwt.client.ApplicationConnection)
  20. */
  21. @Override
  22. @SuppressWarnings("deprecation")
  23. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  24. // Save details
  25. getWidget().client = client;
  26. getWidget().paintableId = uidl.getId();
  27. getWidget().readonly = getState().isReadOnly();
  28. getWidget().enabled = !getState().isDisabled();
  29. getWidget().tb.setEnabled(getWidget().enabled);
  30. getWidget().updateReadOnly();
  31. super.updateFromUIDL(uidl, client);
  32. if (!isRealUpdate(uidl)) {
  33. return;
  34. }
  35. // Inverse logic here to make the default case (text input enabled)
  36. // work without additional UIDL messages
  37. boolean noTextInput = uidl
  38. .hasAttribute(VFilterSelect.ATTR_NO_TEXT_INPUT)
  39. && uidl.getBooleanAttribute(VFilterSelect.ATTR_NO_TEXT_INPUT);
  40. getWidget().setTextInputEnabled(!noTextInput);
  41. // not a FocusWidget -> needs own tabindex handling
  42. if (uidl.hasAttribute("tabindex")) {
  43. getWidget().tb.setTabIndex(uidl
  44. .getIntAttribute("tabindex"));
  45. }
  46. if (uidl.hasAttribute("filteringmode")) {
  47. getWidget().filteringmode = uidl
  48. .getIntAttribute("filteringmode");
  49. }
  50. getWidget().immediate = getState().isImmediate();
  51. getWidget().nullSelectionAllowed = uidl
  52. .hasAttribute("nullselect");
  53. getWidget().nullSelectItem = uidl
  54. .hasAttribute("nullselectitem")
  55. && uidl.getBooleanAttribute("nullselectitem");
  56. getWidget().currentPage = uidl.getIntVariable("page");
  57. if (uidl.hasAttribute("pagelength")) {
  58. getWidget().pageLength = uidl
  59. .getIntAttribute("pagelength");
  60. }
  61. if (uidl.hasAttribute(VFilterSelect.ATTR_INPUTPROMPT)) {
  62. // input prompt changed from server
  63. getWidget().inputPrompt = uidl
  64. .getStringAttribute(VFilterSelect.ATTR_INPUTPROMPT);
  65. } else {
  66. getWidget().inputPrompt = "";
  67. }
  68. getWidget().suggestionPopup.updateStyleNames(uidl,
  69. getState());
  70. getWidget().allowNewItem = uidl
  71. .hasAttribute("allownewitem");
  72. getWidget().lastNewItemString = null;
  73. getWidget().currentSuggestions.clear();
  74. if (!getWidget().waitingForFilteringResponse) {
  75. /*
  76. * Clear the current suggestions as the server response always
  77. * includes the new ones. Exception is when filtering, then we need
  78. * to retain the value if the user does not select any of the
  79. * options matching the filter.
  80. */
  81. getWidget().currentSuggestion = null;
  82. /*
  83. * Also ensure no old items in menu. Unless cleared the old values
  84. * may cause odd effects on blur events. Suggestions in menu might
  85. * not necessary exist in select at all anymore.
  86. */
  87. getWidget().suggestionPopup.menu.clearItems();
  88. }
  89. final UIDL options = uidl.getChildUIDL(0);
  90. if (uidl.hasAttribute("totalMatches")) {
  91. getWidget().totalMatches = uidl
  92. .getIntAttribute("totalMatches");
  93. } else {
  94. getWidget().totalMatches = 0;
  95. }
  96. // used only to calculate minimum popup width
  97. String captions = Util.escapeHTML(getWidget().inputPrompt);
  98. for (final Iterator<?> i = options.getChildIterator(); i.hasNext();) {
  99. final UIDL optionUidl = (UIDL) i.next();
  100. final FilterSelectSuggestion suggestion = getWidget().new FilterSelectSuggestion(
  101. optionUidl);
  102. getWidget().currentSuggestions.add(suggestion);
  103. if (optionUidl.hasAttribute("selected")) {
  104. if (!getWidget().waitingForFilteringResponse
  105. || getWidget().popupOpenerClicked) {
  106. String newSelectedOptionKey = Integer.toString(suggestion
  107. .getOptionKey());
  108. if (!newSelectedOptionKey
  109. .equals(getWidget().selectedOptionKey)
  110. || suggestion.getReplacementString().equals(
  111. getWidget().tb.getText())) {
  112. // Update text field if we've got a new selection
  113. // Also update if we've got the same text to retain old
  114. // text selection behavior
  115. getWidget().setPromptingOff(
  116. suggestion.getReplacementString());
  117. getWidget().selectedOptionKey = newSelectedOptionKey;
  118. }
  119. }
  120. getWidget().currentSuggestion = suggestion;
  121. getWidget().setSelectedItemIcon(
  122. suggestion.getIconUri());
  123. }
  124. // Collect captions so we can calculate minimum width for textarea
  125. if (captions.length() > 0) {
  126. captions += "|";
  127. }
  128. captions += Util.escapeHTML(suggestion.getReplacementString());
  129. }
  130. if ((!getWidget().waitingForFilteringResponse || getWidget().popupOpenerClicked)
  131. && uidl.hasVariable("selected")
  132. && uidl.getStringArrayVariable("selected").length == 0) {
  133. // select nulled
  134. if (!getWidget().waitingForFilteringResponse
  135. || !getWidget().popupOpenerClicked) {
  136. if (!getWidget().focused) {
  137. /*
  138. * client.updateComponent overwrites all styles so we must
  139. * ALWAYS set the prompting style at this point, even though
  140. * we think it has been set already...
  141. */
  142. getWidget().prompting = false;
  143. getWidget().setPromptingOn();
  144. } else {
  145. // we have focus in field, prompting can't be set on,
  146. // instead just clear the input
  147. getWidget().tb.setValue("");
  148. }
  149. }
  150. getWidget().setSelectedItemIcon(null);
  151. getWidget().selectedOptionKey = null;
  152. }
  153. if (getWidget().waitingForFilteringResponse
  154. && getWidget().lastFilter.toLowerCase().equals(
  155. uidl.getStringVariable("filter"))) {
  156. getWidget().suggestionPopup.showSuggestions(
  157. getWidget().currentSuggestions,
  158. getWidget().currentPage,
  159. getWidget().totalMatches);
  160. getWidget().waitingForFilteringResponse = false;
  161. if (!getWidget().popupOpenerClicked
  162. && getWidget().selectPopupItemWhenResponseIsReceived != VFilterSelect.Select.NONE) {
  163. // we're paging w/ arrows
  164. if (getWidget().selectPopupItemWhenResponseIsReceived == VFilterSelect.Select.LAST) {
  165. getWidget().suggestionPopup.menu
  166. .selectLastItem();
  167. } else {
  168. getWidget().suggestionPopup.menu
  169. .selectFirstItem();
  170. }
  171. // This is used for paging so we update the keyboard selection
  172. // variable as well.
  173. MenuItem activeMenuItem = getWidget().suggestionPopup.menu
  174. .getSelectedItem();
  175. getWidget().suggestionPopup.menu
  176. .setKeyboardSelectedItem(activeMenuItem);
  177. // Update text field to contain the correct text
  178. getWidget()
  179. .setTextboxText(activeMenuItem.getText());
  180. getWidget().tb.setSelectionRange(
  181. getWidget().lastFilter.length(),
  182. activeMenuItem.getText().length()
  183. - getWidget().lastFilter.length());
  184. getWidget().selectPopupItemWhenResponseIsReceived = VFilterSelect.Select.NONE; // reset
  185. }
  186. if (getWidget().updateSelectionWhenReponseIsReceived) {
  187. getWidget().suggestionPopup.menu
  188. .doPostFilterSelectedItemAction();
  189. }
  190. }
  191. // Calculate minumum textarea width
  192. getWidget().suggestionPopupMinWidth = getWidget()
  193. .minWidth(captions);
  194. getWidget().popupOpenerClicked = false;
  195. if (!getWidget().initDone) {
  196. getWidget().updateRootWidth();
  197. }
  198. // Focus dependent style names are lost during the update, so we add
  199. // them here back again
  200. if (getWidget().focused) {
  201. getWidget().addStyleDependentName("focus");
  202. }
  203. getWidget().initDone = true;
  204. }
  205. @Override
  206. protected Widget createWidget() {
  207. return GWT.create(VFilterSelect.class);
  208. }
  209. @Override
  210. public VFilterSelect getWidget() {
  211. return (VFilterSelect) super.getWidget();
  212. }
  213. public void layout() {
  214. VFilterSelect widget = getWidget();
  215. if (widget.initDone) {
  216. widget.updateRootWidth();
  217. }
  218. }
  219. }