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

ComboBoxConnector.java 13KB

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