Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ComboBoxConnector.java 15KB

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