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

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