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

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