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

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.vaadin.client.ApplicationConnection;
  21. import com.vaadin.client.Paintable;
  22. import com.vaadin.client.Profiler;
  23. import com.vaadin.client.UIDL;
  24. import com.vaadin.client.communication.RpcProxy;
  25. import com.vaadin.client.communication.StateChangeEvent;
  26. import com.vaadin.client.ui.SimpleManagedLayout;
  27. import com.vaadin.shared.EventId;
  28. import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc;
  29. import com.vaadin.shared.ui.Connect;
  30. import com.vaadin.v7.client.ui.AbstractFieldConnector;
  31. import com.vaadin.v7.client.ui.VFilterSelect;
  32. import com.vaadin.v7.client.ui.VFilterSelect.DataReceivedHandler;
  33. import com.vaadin.v7.client.ui.VFilterSelect.FilterSelectSuggestion;
  34. import com.vaadin.v7.shared.ui.combobox.ComboBoxServerRpc;
  35. import com.vaadin.v7.shared.ui.combobox.ComboBoxState;
  36. import com.vaadin.v7.ui.ComboBox;
  37. @Connect(ComboBox.class)
  38. public class ComboBoxConnector extends AbstractFieldConnector
  39. implements Paintable, SimpleManagedLayout {
  40. protected ComboBoxServerRpc rpc = RpcProxy.create(ComboBoxServerRpc.class,
  41. this);
  42. protected FocusAndBlurServerRpc focusAndBlurRpc = RpcProxy
  43. .create(FocusAndBlurServerRpc.class, this);
  44. @Override
  45. protected void init() {
  46. super.init();
  47. getWidget().connector = this;
  48. }
  49. @Override
  50. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  51. super.onStateChanged(stateChangeEvent);
  52. Profiler.enter("ComboBoxConnector.onStateChanged update content");
  53. getWidget().readonly = isReadOnly();
  54. getWidget().updateReadOnly();
  55. getWidget().setTextInputEnabled(getState().textInputAllowed);
  56. if (getState().inputPrompt != null) {
  57. getWidget().inputPrompt = getState().inputPrompt;
  58. } else {
  59. getWidget().inputPrompt = "";
  60. }
  61. getWidget().pageLength = getState().pageLength;
  62. getWidget().filteringmode = getState().filteringMode;
  63. getWidget().suggestionPopupWidth = getState().suggestionPopupWidth;
  64. Profiler.leave("ComboBoxConnector.onStateChanged update content");
  65. }
  66. /*
  67. * (non-Javadoc)
  68. *
  69. * @see com.vaadin.client.Paintable#updateFromUIDL(com.vaadin.client.UIDL,
  70. * com.vaadin.client.ApplicationConnection)
  71. */
  72. @Override
  73. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  74. if (!isRealUpdate(uidl)) {
  75. return;
  76. }
  77. // not a FocusWidget -> needs own tabindex handling
  78. getWidget().tb.setTabIndex(getState().tabIndex);
  79. getWidget().nullSelectionAllowed = uidl.hasAttribute("nullselect");
  80. getWidget().nullSelectItem = uidl.hasAttribute("nullselectitem")
  81. && uidl.getBooleanAttribute("nullselectitem");
  82. getWidget().currentPage = uidl.getIntVariable("page");
  83. getWidget().suggestionPopup.updateStyleNames(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. String key = optionUidl.getStringAttribute("key");
  96. String caption = optionUidl.getStringAttribute("caption");
  97. String style = optionUidl.getStringAttribute("style");
  98. String untranslatedIconUri = null;
  99. if (optionUidl.hasAttribute("icon")) {
  100. untranslatedIconUri = optionUidl.getStringAttribute("icon");
  101. }
  102. final FilterSelectSuggestion suggestion = getWidget().new FilterSelectSuggestion(
  103. key, caption, style, untranslatedIconUri);
  104. newSuggestions.add(suggestion);
  105. }
  106. // only close the popup if the suggestions list has actually changed
  107. boolean suggestionsChanged = !getWidget().initDone
  108. || !newSuggestions.equals(getWidget().currentSuggestions);
  109. // An ItemSetChangeEvent on server side clears the current suggestion
  110. // popup. Popup needs to be repopulated with suggestions from UIDL.
  111. boolean popupOpenAndCleared = false;
  112. // oldSuggestionTextMatchTheOldSelection is used to detect when it's
  113. // safe to update textbox text by a changed item caption.
  114. boolean oldSuggestionTextMatchesTheOldSelection = false;
  115. if (suggestionsChanged) {
  116. oldSuggestionTextMatchesTheOldSelection = isWidgetsCurrentSelectionTextInTextBox();
  117. getWidget().currentSuggestions.clear();
  118. if (!getDataReceivedHandler().isWaitingForFilteringResponse()) {
  119. /*
  120. * Clear the current suggestions as the server response always
  121. * includes the new ones. Exception is when filtering, then we
  122. * need to retain the value if the user does not select any of
  123. * the options matching the filter.
  124. */
  125. getWidget().currentSuggestion = null;
  126. /*
  127. * Also ensure no old items in menu. Unless cleared the old
  128. * values may cause odd effects on blur events. Suggestions in
  129. * menu might not necessary exist in select at all anymore.
  130. */
  131. getWidget().suggestionPopup.menu.clearItems();
  132. popupOpenAndCleared = getWidget().suggestionPopup.isAttached();
  133. }
  134. for (FilterSelectSuggestion suggestion : newSuggestions) {
  135. getWidget().currentSuggestions.add(suggestion);
  136. }
  137. }
  138. // handle selection (null or a single value)
  139. if (uidl.hasVariable("selected")
  140. // In case we're switching page no need to update the selection as the
  141. // selection process didn't finish.
  142. // && getWidget().selectPopupItemWhenResponseIsReceived ==
  143. // VFilterSelect.Select.NONE
  144. //
  145. ) {
  146. // single selected key (can be empty string) or empty array for null
  147. // selection
  148. String[] selectedKeys = uidl.getStringArrayVariable("selected");
  149. String selectedKey = null;
  150. if (selectedKeys.length == 1) {
  151. selectedKey = selectedKeys[0];
  152. }
  153. // selected item caption in case it is not on the current page
  154. String selectedCaption = null;
  155. if (uidl.hasAttribute("selectedCaption")) {
  156. selectedCaption = uidl.getStringAttribute("selectedCaption");
  157. }
  158. getDataReceivedHandler().updateSelectionFromServer(selectedKey,
  159. selectedCaption, oldSuggestionTextMatchesTheOldSelection);
  160. }
  161. // TODO even this condition should probably be moved to the handler
  162. if ((getDataReceivedHandler().isWaitingForFilteringResponse()
  163. && getWidget().lastFilter.toLowerCase()
  164. .equals(uidl.getStringVariable("filter")))
  165. || popupOpenAndCleared) {
  166. getDataReceivedHandler().dataReceived();
  167. }
  168. // Calculate minimum textarea width
  169. getWidget().updateSuggestionPopupMinWidth();
  170. /*
  171. * if this is our first time we need to recalculate the root width.
  172. */
  173. if (!getWidget().initDone) {
  174. getWidget().updateRootWidth();
  175. }
  176. // Focus dependent style names are lost during the update, so we add
  177. // them here back again
  178. if (getWidget().focused) {
  179. getWidget().addStyleDependentName("focus");
  180. }
  181. getWidget().initDone = true;
  182. // TODO this should perhaps be moved to be a part of dataReceived()
  183. getDataReceivedHandler().serverReplyHandled();
  184. }
  185. private boolean isWidgetsCurrentSelectionTextInTextBox() {
  186. return getWidget().currentSuggestion != null
  187. && getWidget().currentSuggestion.getReplacementString()
  188. .equals(getWidget().tb.getText());
  189. }
  190. @Override
  191. public VFilterSelect getWidget() {
  192. return (VFilterSelect) super.getWidget();
  193. }
  194. private DataReceivedHandler getDataReceivedHandler() {
  195. return getWidget().getDataReceivedHandler();
  196. }
  197. @Override
  198. public ComboBoxState getState() {
  199. return (ComboBoxState) super.getState();
  200. }
  201. @Override
  202. public void layout() {
  203. VFilterSelect widget = getWidget();
  204. if (widget.initDone) {
  205. widget.updateRootWidth();
  206. }
  207. }
  208. @Override
  209. public void setWidgetEnabled(boolean widgetEnabled) {
  210. super.setWidgetEnabled(widgetEnabled);
  211. getWidget().enabled = widgetEnabled;
  212. getWidget().tb.setEnabled(widgetEnabled);
  213. }
  214. /*
  215. * These methods exist to move communications out of VFilterSelect, and may
  216. * be refactored/removed in the future
  217. */
  218. /**
  219. * Send a message about a newly created item to the server.
  220. *
  221. * This method is for internal use only and may be removed in future
  222. * versions.
  223. *
  224. * @since 8.0
  225. * @param itemValue
  226. * user entered string value for the new item
  227. */
  228. public void sendNewItem(String itemValue) {
  229. rpc.createNewItem(itemValue);
  230. afterSendRequestToServer();
  231. }
  232. /**
  233. * Send a message to the server to request the first page of items without
  234. * filtering or selection.
  235. *
  236. * This method is for internal use only and may be removed in future
  237. * versions.
  238. *
  239. * @since 8.0
  240. */
  241. public void requestFirstPage() {
  242. sendSelection(null);
  243. requestPage("", 0);
  244. }
  245. /**
  246. * Send a message to the server to request a page of items with a given
  247. * filter.
  248. *
  249. * This method is for internal use only and may be removed in future
  250. * versions.
  251. *
  252. * @since 8.0
  253. * @param filter
  254. * the current filter string
  255. * @param page
  256. * the page number to get
  257. */
  258. public void requestPage(String filter, int page) {
  259. rpc.requestPage(filter, page);
  260. afterSendRequestToServer();
  261. }
  262. /**
  263. * Send a message to the server updating the current selection.
  264. *
  265. * This method is for internal use only and may be removed in future
  266. * versions.
  267. *
  268. * @since 8.0
  269. * @param selection
  270. * the current selection
  271. */
  272. public void sendSelection(String selection) {
  273. rpc.setSelectedItem(selection);
  274. afterSendRequestToServer();
  275. }
  276. /**
  277. * Notify the server that the combo box received focus.
  278. *
  279. * For timing reasons, ConnectorFocusAndBlurHandler is not used at the
  280. * moment.
  281. *
  282. * This method is for internal use only and may be removed in future
  283. * versions.
  284. *
  285. * @since 8.0
  286. */
  287. public void sendFocusEvent() {
  288. boolean registeredListeners = hasEventListener(EventId.FOCUS);
  289. if (registeredListeners) {
  290. focusAndBlurRpc.focus();
  291. afterSendRequestToServer();
  292. }
  293. }
  294. /**
  295. * Notify the server that the combo box lost focus.
  296. *
  297. * For timing reasons, ConnectorFocusAndBlurHandler is not used at the
  298. * moment.
  299. *
  300. * This method is for internal use only and may be removed in future
  301. * versions.
  302. *
  303. * @since 8.0
  304. */
  305. public void sendBlurEvent() {
  306. boolean registeredListeners = hasEventListener(EventId.BLUR);
  307. if (registeredListeners) {
  308. focusAndBlurRpc.blur();
  309. afterSendRequestToServer();
  310. }
  311. }
  312. /*
  313. * Called after any request to server.
  314. */
  315. private void afterSendRequestToServer() {
  316. getDataReceivedHandler().anyRequestSentToServer();
  317. }
  318. }