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.

ComboBoxConnector.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.combobox;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import com.google.gwt.core.client.Scheduler;
  21. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  22. import com.vaadin.client.ApplicationConnection;
  23. import com.vaadin.client.Paintable;
  24. import com.vaadin.client.UIDL;
  25. import com.vaadin.client.ui.AbstractFieldConnector;
  26. import com.vaadin.client.ui.SimpleManagedLayout;
  27. import com.vaadin.client.ui.VFilterSelect;
  28. import com.vaadin.client.ui.VFilterSelect.FilterSelectSuggestion;
  29. import com.vaadin.shared.ui.Connect;
  30. import com.vaadin.shared.ui.combobox.ComboBoxConstants;
  31. import com.vaadin.shared.ui.combobox.ComboBoxState;
  32. import com.vaadin.shared.ui.combobox.FilteringMode;
  33. import com.vaadin.ui.ComboBox;
  34. @Connect(ComboBox.class)
  35. public class ComboBoxConnector extends AbstractFieldConnector implements
  36. Paintable, SimpleManagedLayout {
  37. // oldSuggestionTextMatchTheOldSelection is used to detect when it's safe to
  38. // update textbox text by a changed item caption.
  39. private boolean oldSuggestionTextMatchTheOldSelection;
  40. /*
  41. * (non-Javadoc)
  42. *
  43. * @see com.vaadin.client.Paintable#updateFromUIDL(com.vaadin.client.UIDL,
  44. * com.vaadin.client.ApplicationConnection)
  45. */
  46. @Override
  47. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  48. // Save details
  49. getWidget().client = client;
  50. getWidget().paintableId = uidl.getId();
  51. getWidget().readonly = isReadOnly();
  52. getWidget().updateReadOnly();
  53. if (!isRealUpdate(uidl)) {
  54. return;
  55. }
  56. // Inverse logic here to make the default case (text input enabled)
  57. // work without additional UIDL messages
  58. boolean noTextInput = uidl
  59. .hasAttribute(ComboBoxConstants.ATTR_NO_TEXT_INPUT)
  60. && uidl.getBooleanAttribute(ComboBoxConstants.ATTR_NO_TEXT_INPUT);
  61. getWidget().setTextInputEnabled(!noTextInput);
  62. // not a FocusWidget -> needs own tabindex handling
  63. getWidget().tb.setTabIndex(getState().tabIndex);
  64. if (uidl.hasAttribute("filteringmode")) {
  65. getWidget().filteringmode = FilteringMode.valueOf(uidl
  66. .getStringAttribute("filteringmode"));
  67. }
  68. getWidget().immediate = getState().immediate;
  69. getWidget().nullSelectionAllowed = uidl.hasAttribute("nullselect");
  70. getWidget().nullSelectItem = uidl.hasAttribute("nullselectitem")
  71. && uidl.getBooleanAttribute("nullselectitem");
  72. getWidget().currentPage = uidl.getIntVariable("page");
  73. if (uidl.hasAttribute("pagelength")) {
  74. getWidget().pageLength = uidl.getIntAttribute("pagelength");
  75. }
  76. if (uidl.hasAttribute(ComboBoxConstants.ATTR_INPUTPROMPT)) {
  77. // input prompt changed from server
  78. getWidget().inputPrompt = uidl
  79. .getStringAttribute(ComboBoxConstants.ATTR_INPUTPROMPT);
  80. } else {
  81. getWidget().inputPrompt = "";
  82. }
  83. getWidget().suggestionPopup.updateStyleNames(uidl, getState());
  84. getWidget().allowNewItem = uidl.hasAttribute("allownewitem");
  85. getWidget().lastNewItemString = null;
  86. final UIDL options = uidl.getChildUIDL(0);
  87. if (uidl.hasAttribute("totalMatches")) {
  88. getWidget().totalMatches = uidl.getIntAttribute("totalMatches");
  89. } else {
  90. getWidget().totalMatches = 0;
  91. }
  92. List<FilterSelectSuggestion> newSuggestions = new ArrayList<FilterSelectSuggestion>();
  93. for (final Iterator<?> i = options.getChildIterator(); i.hasNext();) {
  94. final UIDL optionUidl = (UIDL) i.next();
  95. final FilterSelectSuggestion suggestion = getWidget().new FilterSelectSuggestion(
  96. optionUidl);
  97. newSuggestions.add(suggestion);
  98. }
  99. // only close the popup if the suggestions list has actually changed
  100. boolean suggestionsChanged = !getWidget().initDone
  101. || !newSuggestions.equals(getWidget().currentSuggestions);
  102. // An ItemSetChangeEvent on server side clears the current suggestion
  103. // popup. Popup needs to be repopulated with suggestions from UIDL.
  104. boolean popupOpenAndCleared = false;
  105. oldSuggestionTextMatchTheOldSelection = false;
  106. if (suggestionsChanged) {
  107. oldSuggestionTextMatchTheOldSelection = isWidgetsCurrentSelectionTextInTextBox();
  108. getWidget().currentSuggestions.clear();
  109. if (!getWidget().waitingForFilteringResponse) {
  110. /*
  111. * Clear the current suggestions as the server response always
  112. * includes the new ones. Exception is when filtering, then we
  113. * need to retain the value if the user does not select any of
  114. * the options matching the filter.
  115. */
  116. getWidget().currentSuggestion = null;
  117. /*
  118. * Also ensure no old items in menu. Unless cleared the old
  119. * values may cause odd effects on blur events. Suggestions in
  120. * menu might not necessary exist in select at all anymore.
  121. */
  122. getWidget().suggestionPopup.menu.clearItems();
  123. popupOpenAndCleared = getWidget().suggestionPopup.isAttached();
  124. }
  125. for (FilterSelectSuggestion suggestion : newSuggestions) {
  126. getWidget().currentSuggestions.add(suggestion);
  127. }
  128. }
  129. // handle selection (null or a single value)
  130. if (uidl.hasVariable("selected")
  131. // In case we're switching page no need to update the selection as the
  132. // selection process didn't finish.
  133. // && getWidget().selectPopupItemWhenResponseIsReceived ==
  134. // VFilterSelect.Select.NONE
  135. //
  136. ) {
  137. String[] selectedKeys = uidl.getStringArrayVariable("selected");
  138. if (selectedKeys.length > 0) {
  139. performSelection(selectedKeys[0]);
  140. } else {
  141. resetSelection();
  142. }
  143. }
  144. if ((getWidget().waitingForFilteringResponse && getWidget().lastFilter
  145. .toLowerCase().equals(uidl.getStringVariable("filter")))
  146. || popupOpenAndCleared) {
  147. getWidget().suggestionPopup.showSuggestions(
  148. getWidget().currentSuggestions, getWidget().currentPage,
  149. getWidget().totalMatches);
  150. getWidget().waitingForFilteringResponse = false;
  151. if (!getWidget().popupOpenerClicked
  152. && getWidget().selectPopupItemWhenResponseIsReceived != VFilterSelect.Select.NONE) {
  153. // we're paging w/ arrows
  154. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  155. @Override
  156. public void execute() {
  157. navigateItemAfterPageChange();
  158. }
  159. });
  160. }
  161. if (getWidget().updateSelectionWhenReponseIsReceived) {
  162. getWidget().suggestionPopup.menu
  163. .doPostFilterSelectedItemAction();
  164. }
  165. }
  166. // Calculate minimum textarea width
  167. getWidget().updateSuggestionPopupMinWidth();
  168. getWidget().popupOpenerClicked = false;
  169. /*
  170. * if this is our first time we need to recalculate the root width.
  171. */
  172. if (!getWidget().initDone) {
  173. getWidget().updateRootWidth();
  174. }
  175. // Focus dependent style names are lost during the update, so we add
  176. // them here back again
  177. if (getWidget().focused) {
  178. getWidget().addStyleDependentName("focus");
  179. }
  180. getWidget().initDone = true;
  181. }
  182. /*
  183. * This method navigates to the proper item in the combobox page. This
  184. * should be executed after setSuggestions() method which is called from
  185. * vFilterSelect.showSuggestions(). ShowSuggestions() method builds the page
  186. * content. As far as setSuggestions() method is called as deferred,
  187. * navigateItemAfterPageChange method should be also be called as deferred.
  188. * #11333
  189. */
  190. private void navigateItemAfterPageChange() {
  191. if (getWidget().selectPopupItemWhenResponseIsReceived == VFilterSelect.Select.LAST) {
  192. getWidget().suggestionPopup.selectLastItem();
  193. } else {
  194. getWidget().suggestionPopup.selectFirstItem();
  195. }
  196. // If you're in between 2 requests both changing the page back and
  197. // forth, you don't want this here, instead you need it before any
  198. // other request.
  199. // getWidget().selectPopupItemWhenResponseIsReceived =
  200. // VFilterSelect.Select.NONE; // reset
  201. }
  202. private void performSelection(String selectedKey) {
  203. // some item selected
  204. for (FilterSelectSuggestion suggestion : getWidget().currentSuggestions) {
  205. String suggestionKey = suggestion.getOptionKey();
  206. if (!suggestionKey.equals(selectedKey)) {
  207. continue;
  208. }
  209. if (!getWidget().waitingForFilteringResponse
  210. || getWidget().popupOpenerClicked) {
  211. if (!suggestionKey.equals(getWidget().selectedOptionKey)
  212. || suggestion.getReplacementString().equals(
  213. getWidget().tb.getText())
  214. || oldSuggestionTextMatchTheOldSelection) {
  215. // Update text field if we've got a new
  216. // selection
  217. // Also update if we've got the same text to
  218. // retain old text selection behavior
  219. // OR if selected item caption is changed.
  220. getWidget().setPromptingOff(
  221. suggestion.getReplacementString());
  222. getWidget().selectedOptionKey = suggestionKey;
  223. }
  224. }
  225. getWidget().currentSuggestion = suggestion;
  226. getWidget().setSelectedItemIcon(suggestion.getIconUri());
  227. // only a single item can be selected
  228. break;
  229. }
  230. }
  231. private boolean isWidgetsCurrentSelectionTextInTextBox() {
  232. return getWidget().currentSuggestion != null
  233. && getWidget().currentSuggestion.getReplacementString().equals(
  234. getWidget().tb.getText());
  235. }
  236. private void resetSelection() {
  237. if (!getWidget().waitingForFilteringResponse
  238. || getWidget().popupOpenerClicked) {
  239. // select nulled
  240. if (!getWidget().focused) {
  241. /*
  242. * client.updateComponent overwrites all styles so we must
  243. * ALWAYS set the prompting style at this point, even though we
  244. * think it has been set already...
  245. */
  246. getWidget().setPromptingOff("");
  247. if (getWidget().enabled && !getWidget().readonly) {
  248. getWidget().setPromptingOn();
  249. }
  250. } else {
  251. // we have focus in field, prompting can't be set on, instead
  252. // just clear the input if the value has changed from something
  253. // else to null
  254. if (getWidget().selectedOptionKey != null
  255. || (getWidget().allowNewItem && !getWidget().tb
  256. .getValue().isEmpty())) {
  257. getWidget().tb.setValue("");
  258. }
  259. }
  260. getWidget().setSelectedItemIcon(null);
  261. getWidget().selectedOptionKey = null;
  262. }
  263. }
  264. @Override
  265. public VFilterSelect getWidget() {
  266. return (VFilterSelect) super.getWidget();
  267. }
  268. @Override
  269. public ComboBoxState getState() {
  270. return (ComboBoxState) super.getState();
  271. }
  272. @Override
  273. public void layout() {
  274. VFilterSelect widget = getWidget();
  275. if (widget.initDone) {
  276. widget.updateRootWidth();
  277. }
  278. }
  279. @Override
  280. public void setWidgetEnabled(boolean widgetEnabled) {
  281. super.setWidgetEnabled(widgetEnabled);
  282. getWidget().enabled = widgetEnabled;
  283. getWidget().tb.setEnabled(widgetEnabled);
  284. }
  285. }