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.

VFilterSelect.java 92KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  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;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Date;
  20. import java.util.HashSet;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Set;
  24. import com.google.gwt.aria.client.Roles;
  25. import com.google.gwt.core.client.JavaScriptObject;
  26. import com.google.gwt.core.client.Scheduler;
  27. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  28. import com.google.gwt.dom.client.Document;
  29. import com.google.gwt.dom.client.Element;
  30. import com.google.gwt.dom.client.NativeEvent;
  31. import com.google.gwt.dom.client.Node;
  32. import com.google.gwt.dom.client.Style;
  33. import com.google.gwt.dom.client.Style.Display;
  34. import com.google.gwt.dom.client.Style.Unit;
  35. import com.google.gwt.dom.client.Style.Visibility;
  36. import com.google.gwt.event.dom.client.BlurEvent;
  37. import com.google.gwt.event.dom.client.BlurHandler;
  38. import com.google.gwt.event.dom.client.ClickEvent;
  39. import com.google.gwt.event.dom.client.ClickHandler;
  40. import com.google.gwt.event.dom.client.FocusEvent;
  41. import com.google.gwt.event.dom.client.FocusHandler;
  42. import com.google.gwt.event.dom.client.KeyCodes;
  43. import com.google.gwt.event.dom.client.KeyDownEvent;
  44. import com.google.gwt.event.dom.client.KeyDownHandler;
  45. import com.google.gwt.event.dom.client.KeyUpEvent;
  46. import com.google.gwt.event.dom.client.KeyUpHandler;
  47. import com.google.gwt.event.dom.client.LoadEvent;
  48. import com.google.gwt.event.dom.client.LoadHandler;
  49. import com.google.gwt.event.logical.shared.CloseEvent;
  50. import com.google.gwt.event.logical.shared.CloseHandler;
  51. import com.google.gwt.i18n.client.HasDirection.Direction;
  52. import com.google.gwt.user.client.Command;
  53. import com.google.gwt.user.client.DOM;
  54. import com.google.gwt.user.client.Event;
  55. import com.google.gwt.user.client.Timer;
  56. import com.google.gwt.user.client.Window;
  57. import com.google.gwt.user.client.ui.Composite;
  58. import com.google.gwt.user.client.ui.FlowPanel;
  59. import com.google.gwt.user.client.ui.HTML;
  60. import com.google.gwt.user.client.ui.PopupPanel;
  61. import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
  62. import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
  63. import com.google.gwt.user.client.ui.TextBox;
  64. import com.google.gwt.user.client.ui.Widget;
  65. import com.vaadin.client.ApplicationConnection;
  66. import com.vaadin.client.BrowserInfo;
  67. import com.vaadin.client.ComponentConnector;
  68. import com.vaadin.client.ComputedStyle;
  69. import com.vaadin.client.ConnectorMap;
  70. import com.vaadin.client.DeferredWorker;
  71. import com.vaadin.client.Focusable;
  72. import com.vaadin.client.UIDL;
  73. import com.vaadin.client.VConsole;
  74. import com.vaadin.client.WidgetUtil;
  75. import com.vaadin.client.ui.Field;
  76. import com.vaadin.client.ui.Icon;
  77. import com.vaadin.client.ui.SubPartAware;
  78. import com.vaadin.client.ui.VLazyExecutor;
  79. import com.vaadin.client.ui.VOverlay;
  80. import com.vaadin.client.ui.aria.AriaHelper;
  81. import com.vaadin.client.ui.aria.HandlesAriaCaption;
  82. import com.vaadin.client.ui.aria.HandlesAriaInvalid;
  83. import com.vaadin.client.ui.aria.HandlesAriaRequired;
  84. import com.vaadin.client.ui.menubar.MenuBar;
  85. import com.vaadin.client.ui.menubar.MenuItem;
  86. import com.vaadin.shared.AbstractComponentState;
  87. import com.vaadin.shared.EventId;
  88. import com.vaadin.shared.ui.ComponentStateUtil;
  89. import com.vaadin.shared.util.SharedUtil;
  90. import com.vaadin.v7.shared.ui.combobox.FilteringMode;
  91. /**
  92. * Client side implementation of the Select component.
  93. *
  94. * TODO needs major refactoring (to be extensible etc)
  95. */
  96. @SuppressWarnings("deprecation")
  97. public class VFilterSelect extends Composite
  98. implements Field, KeyDownHandler, KeyUpHandler, ClickHandler,
  99. FocusHandler, BlurHandler, Focusable, SubPartAware, HandlesAriaCaption,
  100. HandlesAriaInvalid, HandlesAriaRequired, DeferredWorker {
  101. /**
  102. * Represents a suggestion in the suggestion popup box
  103. */
  104. public class FilterSelectSuggestion implements Suggestion, Command {
  105. private final String key;
  106. private final String caption;
  107. private String untranslatedIconUri;
  108. private String style;
  109. /**
  110. * Constructor
  111. *
  112. * @param uidl
  113. * The UIDL received from the server
  114. */
  115. public FilterSelectSuggestion(UIDL uidl) {
  116. key = uidl.getStringAttribute("key");
  117. caption = uidl.getStringAttribute("caption");
  118. style = uidl.getStringAttribute("style");
  119. if (uidl.hasAttribute("icon")) {
  120. untranslatedIconUri = uidl.getStringAttribute("icon");
  121. }
  122. }
  123. /**
  124. * Gets the visible row in the popup as a HTML string. The string
  125. * contains an image tag with the rows icon (if an icon has been
  126. * specified) and the caption of the item
  127. */
  128. @Override
  129. public String getDisplayString() {
  130. final StringBuffer sb = new StringBuffer();
  131. final Icon icon = client
  132. .getIcon(client.translateVaadinUri(untranslatedIconUri));
  133. if (icon != null) {
  134. sb.append(icon.getElement().getString());
  135. }
  136. String content;
  137. if ("".equals(caption)) {
  138. // Ensure that empty options use the same height as other
  139. // options and are not collapsed (#7506)
  140. content = " ";
  141. } else {
  142. content = WidgetUtil.escapeHTML(caption);
  143. }
  144. sb.append("<span>" + content + "</span>");
  145. return sb.toString();
  146. }
  147. /**
  148. * Get a string that represents this item. This is used in the text box.
  149. */
  150. @Override
  151. public String getReplacementString() {
  152. return caption;
  153. }
  154. /**
  155. * Get the option key which represents the item on the server side.
  156. *
  157. * @return The key of the item
  158. */
  159. public String getOptionKey() {
  160. return key;
  161. }
  162. /**
  163. * Get the URI of the icon. Used when constructing the displayed option.
  164. *
  165. * @return
  166. */
  167. public String getIconUri() {
  168. return client.translateVaadinUri(untranslatedIconUri);
  169. }
  170. /**
  171. * Gets the style set for this suggestion item. Styles are typically set
  172. * by a server-side {@link com.vaadin.ui.ComboBox.ItemStyleGenerator}.
  173. * The returned style is prefixed by <code>v-filterselect-item-</code>.
  174. *
  175. * @since 7.5.6
  176. * @return the style name to use, or <code>null</code> to not apply any
  177. * custom style.
  178. */
  179. public String getStyle() {
  180. return style;
  181. }
  182. /**
  183. * Executes a selection of this item.
  184. */
  185. @Override
  186. public void execute() {
  187. onSuggestionSelected(this);
  188. }
  189. @Override
  190. public boolean equals(Object obj) {
  191. if (!(obj instanceof FilterSelectSuggestion)) {
  192. return false;
  193. }
  194. FilterSelectSuggestion other = (FilterSelectSuggestion) obj;
  195. if ((key == null && other.key != null)
  196. || (key != null && !key.equals(other.key))) {
  197. return false;
  198. }
  199. if ((caption == null && other.caption != null)
  200. || (caption != null && !caption.equals(other.caption))) {
  201. return false;
  202. }
  203. if (!SharedUtil.equals(untranslatedIconUri,
  204. other.untranslatedIconUri)) {
  205. return false;
  206. }
  207. if (!SharedUtil.equals(style, other.style)) {
  208. return false;
  209. }
  210. return true;
  211. }
  212. @Override
  213. public int hashCode() {
  214. final int prime = 31;
  215. int result = 1;
  216. result = prime * result + VFilterSelect.this.hashCode();
  217. result = prime * result + ((key == null) ? 0 : key.hashCode());
  218. result = prime * result
  219. + ((caption == null) ? 0 : caption.hashCode());
  220. result = prime * result + ((untranslatedIconUri == null) ? 0
  221. : untranslatedIconUri.hashCode());
  222. result = prime * result + ((style == null) ? 0 : style.hashCode());
  223. return result;
  224. }
  225. }
  226. /** An inner class that handles all logic related to mouse wheel. */
  227. private class MouseWheeler extends JsniMousewheelHandler {
  228. public MouseWheeler() {
  229. super(VFilterSelect.this);
  230. }
  231. @Override
  232. protected native JavaScriptObject createMousewheelListenerFunction(
  233. Widget widget)
  234. /*-{
  235. return $entry(function(e) {
  236. var deltaX = e.deltaX ? e.deltaX : -0.5*e.wheelDeltaX;
  237. var deltaY = e.deltaY ? e.deltaY : -0.5*e.wheelDeltaY;
  238. // IE8 has only delta y
  239. if (isNaN(deltaY)) {
  240. deltaY = -0.5*e.wheelDelta;
  241. }
  242. @com.vaadin.v7.client.ui.VFilterSelect.JsniUtil::moveScrollFromEvent(*)(widget, deltaX, deltaY, e, e.deltaMode);
  243. });
  244. }-*/;
  245. }
  246. /**
  247. * A utility class that contains utility methods that are usually called
  248. * from JSNI.
  249. * <p>
  250. * The methods are moved in this class to minimize the amount of JSNI code
  251. * as much as feasible.
  252. */
  253. static class JsniUtil {
  254. private static final int DOM_DELTA_PIXEL = 0;
  255. private static final int DOM_DELTA_LINE = 1;
  256. private static final int DOM_DELTA_PAGE = 2;
  257. // Rough estimation of item height
  258. private static final int SCROLL_UNIT_PX = 25;
  259. private static double deltaSum = 0;
  260. public static void moveScrollFromEvent(final Widget widget,
  261. final double deltaX, final double deltaY,
  262. final NativeEvent event, final int deltaMode) {
  263. if (!Double.isNaN(deltaY)) {
  264. VFilterSelect filterSelect = (VFilterSelect) widget;
  265. switch (deltaMode) {
  266. case DOM_DELTA_LINE:
  267. if (deltaY >= 0) {
  268. filterSelect.suggestionPopup.selectNextItem();
  269. } else {
  270. filterSelect.suggestionPopup.selectPrevItem();
  271. }
  272. break;
  273. case DOM_DELTA_PAGE:
  274. if (deltaY >= 0) {
  275. filterSelect.selectNextPage();
  276. } else {
  277. filterSelect.selectPrevPage();
  278. }
  279. break;
  280. case DOM_DELTA_PIXEL:
  281. default:
  282. // Accumulate dampened deltas
  283. deltaSum += Math.pow(Math.abs(deltaY), 0.7)
  284. * Math.signum(deltaY);
  285. // "Scroll" if change exceeds item height
  286. while (Math.abs(deltaSum) >= SCROLL_UNIT_PX) {
  287. if (!filterSelect.waitingForFilteringResponse) {
  288. // Move selection if page flip is not in progress
  289. if (deltaSum < 0) {
  290. filterSelect.suggestionPopup.selectPrevItem();
  291. } else {
  292. filterSelect.suggestionPopup.selectNextItem();
  293. }
  294. }
  295. deltaSum -= SCROLL_UNIT_PX * Math.signum(deltaSum);
  296. }
  297. break;
  298. }
  299. }
  300. }
  301. }
  302. /**
  303. * Represents the popup box with the selection options. Wraps a suggestion
  304. * menu.
  305. */
  306. public class SuggestionPopup extends VOverlay
  307. implements PositionCallback, CloseHandler<PopupPanel> {
  308. private static final int Z_INDEX = 30000;
  309. /** For internal use only. May be removed or replaced in the future. */
  310. public final SuggestionMenu menu;
  311. private final Element up = DOM.createDiv();
  312. private final Element down = DOM.createDiv();
  313. private final Element status = DOM.createDiv();
  314. private boolean isPagingEnabled = true;
  315. private long lastAutoClosed;
  316. private int popupOuterPadding = -1;
  317. private int topPosition;
  318. private final MouseWheeler mouseWheeler = new MouseWheeler();
  319. /**
  320. * Default constructor
  321. */
  322. SuggestionPopup() {
  323. super(true, false);
  324. debug("VFS.SP: constructor()");
  325. setOwner(VFilterSelect.this);
  326. menu = new SuggestionMenu();
  327. setWidget(menu);
  328. getElement().getStyle().setZIndex(Z_INDEX);
  329. final Element root = getContainerElement();
  330. up.setInnerHTML("<span>Prev</span>");
  331. DOM.sinkEvents(up, Event.ONCLICK);
  332. down.setInnerHTML("<span>Next</span>");
  333. DOM.sinkEvents(down, Event.ONCLICK);
  334. root.insertFirst(up);
  335. root.appendChild(down);
  336. root.appendChild(status);
  337. DOM.sinkEvents(root, Event.ONMOUSEDOWN | Event.ONMOUSEWHEEL);
  338. addCloseHandler(this);
  339. Roles.getListRole().set(getElement());
  340. setPreviewingAllNativeEvents(true);
  341. }
  342. @Override
  343. protected void onLoad() {
  344. super.onLoad();
  345. // Register mousewheel listener on paged select
  346. if (pageLength > 0) {
  347. mouseWheeler.attachMousewheelListener(getElement());
  348. }
  349. }
  350. @Override
  351. protected void onUnload() {
  352. mouseWheeler.detachMousewheelListener(getElement());
  353. super.onUnload();
  354. }
  355. /**
  356. * Shows the popup where the user can see the filtered options
  357. *
  358. * @param currentSuggestions
  359. * The filtered suggestions
  360. * @param currentPage
  361. * The current page number
  362. * @param totalSuggestions
  363. * The total amount of suggestions
  364. */
  365. public void showSuggestions(
  366. final Collection<FilterSelectSuggestion> currentSuggestions,
  367. final int currentPage, final int totalSuggestions) {
  368. debug("VFS.SP: showSuggestions(" + currentSuggestions + ", "
  369. + currentPage + ", " + totalSuggestions + ")");
  370. /*
  371. * We need to defer the opening of the popup so that the parent DOM
  372. * has stabilized so we can calculate an absolute top and left
  373. * correctly. This issue manifests when a Combobox is placed in
  374. * another popupView which also needs to calculate the absoluteTop()
  375. * to position itself. #9768
  376. *
  377. * After deferring the showSuggestions method, a problem with
  378. * navigating in the combo box occurs. Because of that the method
  379. * navigateItemAfterPageChange in ComboBoxConnector class, which
  380. * navigates to the exact item after page was changed also was
  381. * marked as deferred. #11333
  382. */
  383. final SuggestionPopup popup = this;
  384. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  385. @Override
  386. public void execute() {
  387. // Add TT anchor point
  388. getElement().setId("VAADIN_COMBOBOX_OPTIONLIST");
  389. menu.setSuggestions(currentSuggestions);
  390. final int x = VFilterSelect.this.getAbsoluteLeft();
  391. topPosition = tb.getAbsoluteTop();
  392. topPosition += tb.getOffsetHeight();
  393. setPopupPosition(x, topPosition);
  394. int nullOffset = (nullSelectionAllowed
  395. && "".equals(lastFilter) ? 1 : 0);
  396. boolean firstPage = (currentPage == 0);
  397. final int first = currentPage * pageLength + 1
  398. - (firstPage ? 0 : nullOffset);
  399. final int last = first + currentSuggestions.size() - 1
  400. - (firstPage && "".equals(lastFilter) ? nullOffset
  401. : 0);
  402. final int matches = totalSuggestions - nullOffset;
  403. if (last > 0) {
  404. // nullsel not counted, as requested by user
  405. status.setInnerText((matches == 0 ? 0 : first) + "-"
  406. + last + "/" + matches);
  407. } else {
  408. status.setInnerText("");
  409. }
  410. // We don't need to show arrows or statusbar if there is
  411. // only one page
  412. if (totalSuggestions <= pageLength || pageLength == 0) {
  413. setPagingEnabled(false);
  414. } else {
  415. setPagingEnabled(true);
  416. }
  417. setPrevButtonActive(first > 1);
  418. setNextButtonActive(last < matches);
  419. // clear previously fixed width
  420. menu.setWidth("");
  421. menu.getElement().getFirstChildElement().getStyle()
  422. .clearWidth();
  423. setPopupPositionAndShow(popup);
  424. // Fix for #14173
  425. // IE9 and IE10 have a bug, when resize an a element with
  426. // box-shadow.
  427. // IE9 and IE10 need explicit update to remove extra
  428. // box-shadows
  429. if (BrowserInfo.get().isIE9()
  430. || BrowserInfo.get().isIE10()) {
  431. forceReflow();
  432. }
  433. }
  434. });
  435. }
  436. /**
  437. * Should the next page button be visible to the user?
  438. *
  439. * @param active
  440. */
  441. private void setNextButtonActive(boolean active) {
  442. if (enableDebug) {
  443. debug("VFS.SP: setNextButtonActive(" + active + ")");
  444. }
  445. if (active) {
  446. DOM.sinkEvents(down, Event.ONCLICK);
  447. down.setClassName(
  448. VFilterSelect.this.getStylePrimaryName() + "-nextpage");
  449. } else {
  450. DOM.sinkEvents(down, 0);
  451. down.setClassName(VFilterSelect.this.getStylePrimaryName()
  452. + "-nextpage-off");
  453. }
  454. }
  455. /**
  456. * Should the previous page button be visible to the user
  457. *
  458. * @param active
  459. */
  460. private void setPrevButtonActive(boolean active) {
  461. if (enableDebug) {
  462. debug("VFS.SP: setPrevButtonActive(" + active + ")");
  463. }
  464. if (active) {
  465. DOM.sinkEvents(up, Event.ONCLICK);
  466. up.setClassName(
  467. VFilterSelect.this.getStylePrimaryName() + "-prevpage");
  468. } else {
  469. DOM.sinkEvents(up, 0);
  470. up.setClassName(VFilterSelect.this.getStylePrimaryName()
  471. + "-prevpage-off");
  472. }
  473. }
  474. /**
  475. * Selects the next item in the filtered selections
  476. */
  477. public void selectNextItem() {
  478. debug("VFS.SP: selectNextItem()");
  479. final int index = menu.getSelectedIndex() + 1;
  480. if (menu.getItems().size() > index) {
  481. selectItem(menu.getItems().get(index));
  482. } else {
  483. selectNextPage();
  484. }
  485. }
  486. /**
  487. * Selects the previous item in the filtered selections
  488. */
  489. public void selectPrevItem() {
  490. debug("VFS.SP: selectPrevItem()");
  491. final int index = menu.getSelectedIndex() - 1;
  492. if (index > -1) {
  493. selectItem(menu.getItems().get(index));
  494. } else if (index == -1) {
  495. selectPrevPage();
  496. } else {
  497. if (!menu.getItems().isEmpty()) {
  498. selectLastItem();
  499. }
  500. }
  501. }
  502. /**
  503. * Select the first item of the suggestions list popup.
  504. *
  505. * @since 7.2.6
  506. */
  507. public void selectFirstItem() {
  508. debug("VFS.SP: selectFirstItem()");
  509. selectItem(menu.getFirstItem());
  510. }
  511. /**
  512. * Select the last item of the suggestions list popup.
  513. *
  514. * @since 7.2.6
  515. */
  516. public void selectLastItem() {
  517. debug("VFS.SP: selectLastItem()");
  518. selectItem(menu.getLastItem());
  519. }
  520. /*
  521. * Sets the selected item in the popup menu.
  522. */
  523. private void selectItem(final MenuItem newSelectedItem) {
  524. menu.selectItem(newSelectedItem);
  525. // Set the icon.
  526. FilterSelectSuggestion suggestion = (FilterSelectSuggestion) newSelectedItem
  527. .getCommand();
  528. setSelectedItemIcon(suggestion.getIconUri());
  529. // Set the text.
  530. setText(suggestion.getReplacementString());
  531. }
  532. /*
  533. * Using a timer to scroll up or down the pages so when we receive lots
  534. * of consecutive mouse wheel events the pages does not flicker.
  535. */
  536. private LazyPageScroller lazyPageScroller = new LazyPageScroller();
  537. private class LazyPageScroller extends Timer {
  538. private int pagesToScroll = 0;
  539. @Override
  540. public void run() {
  541. debug("VFS.SP.LPS: run()");
  542. if (pagesToScroll != 0) {
  543. if (!waitingForFilteringResponse) {
  544. /*
  545. * Avoid scrolling while we are waiting for a response
  546. * because otherwise the waiting flag will be reset in
  547. * the first response and the second response will be
  548. * ignored, causing an empty popup...
  549. *
  550. * As long as the scrolling delay is suitable
  551. * double/triple clicks will work by scrolling two or
  552. * three pages at a time and this should not be a
  553. * problem.
  554. */
  555. filterOptions(currentPage + pagesToScroll, lastFilter);
  556. }
  557. pagesToScroll = 0;
  558. }
  559. }
  560. public void scrollUp() {
  561. debug("VFS.SP.LPS: scrollUp()");
  562. if (pageLength > 0 && currentPage + pagesToScroll > 0) {
  563. pagesToScroll--;
  564. cancel();
  565. schedule(200);
  566. }
  567. }
  568. public void scrollDown() {
  569. debug("VFS.SP.LPS: scrollDown()");
  570. if (pageLength > 0
  571. && totalMatches > (currentPage + pagesToScroll + 1)
  572. * pageLength) {
  573. pagesToScroll++;
  574. cancel();
  575. schedule(200);
  576. }
  577. }
  578. }
  579. @Override
  580. public void onBrowserEvent(Event event) {
  581. debug("VFS.SP: onBrowserEvent()");
  582. if (event.getTypeInt() == Event.ONCLICK) {
  583. final Element target = DOM.eventGetTarget(event);
  584. if (target == up || target == DOM.getChild(up, 0)) {
  585. lazyPageScroller.scrollUp();
  586. } else if (target == down || target == DOM.getChild(down, 0)) {
  587. lazyPageScroller.scrollDown();
  588. }
  589. }
  590. /*
  591. * Prevent the keyboard focus from leaving the textfield by
  592. * preventing the default behavior of the browser. Fixes #4285.
  593. */
  594. handleMouseDownEvent(event);
  595. }
  596. /**
  597. * Should paging be enabled. If paging is enabled then only a certain
  598. * amount of items are visible at a time and a scrollbar or buttons are
  599. * visible to change page. If paging is turned of then all options are
  600. * rendered into the popup menu.
  601. *
  602. * @param paging
  603. * Should the paging be turned on?
  604. */
  605. public void setPagingEnabled(boolean paging) {
  606. debug("VFS.SP: setPagingEnabled(" + paging + ")");
  607. if (isPagingEnabled == paging) {
  608. return;
  609. }
  610. if (paging) {
  611. down.getStyle().clearDisplay();
  612. up.getStyle().clearDisplay();
  613. status.getStyle().clearDisplay();
  614. } else {
  615. down.getStyle().setDisplay(Display.NONE);
  616. up.getStyle().setDisplay(Display.NONE);
  617. status.getStyle().setDisplay(Display.NONE);
  618. }
  619. isPagingEnabled = paging;
  620. }
  621. @Override
  622. public void setPosition(int offsetWidth, int offsetHeight) {
  623. debug("VFS.SP: setPosition(" + offsetWidth + ", " + offsetHeight
  624. + ")");
  625. int top = topPosition;
  626. int left = getPopupLeft();
  627. // reset menu size and retrieve its "natural" size
  628. menu.setHeight("");
  629. if (currentPage > 0 && !hasNextPage()) {
  630. // fix height to avoid height change when getting to last page
  631. menu.fixHeightTo(pageLength);
  632. }
  633. final int desiredHeight = offsetHeight = getOffsetHeight();
  634. final int desiredWidth = getMainWidth();
  635. debug("VFS.SP: desired[" + desiredWidth + ", " + desiredHeight
  636. + "]");
  637. Element menuFirstChild = menu.getElement().getFirstChildElement();
  638. int naturalMenuWidth;
  639. if (BrowserInfo.get().isIE()
  640. && BrowserInfo.get().getBrowserMajorVersion() < 10) {
  641. // On IE 8 & 9 visibility is set to hidden and measuring
  642. // elements while they are hidden yields incorrect results
  643. String before = menu.getElement().getParentElement().getStyle()
  644. .getVisibility();
  645. menu.getElement().getParentElement().getStyle()
  646. .setVisibility(Visibility.VISIBLE);
  647. naturalMenuWidth = WidgetUtil.getRequiredWidth(menuFirstChild);
  648. menu.getElement().getParentElement().getStyle()
  649. .setProperty("visibility", before);
  650. } else {
  651. naturalMenuWidth = WidgetUtil.getRequiredWidth(menuFirstChild);
  652. }
  653. if (popupOuterPadding == -1) {
  654. popupOuterPadding = WidgetUtil
  655. .measureHorizontalPaddingAndBorder(menu.getElement(), 2)
  656. + WidgetUtil.measureHorizontalPaddingAndBorder(
  657. suggestionPopup.getElement(), 0);
  658. }
  659. updateMenuWidth(desiredWidth, naturalMenuWidth);
  660. if (BrowserInfo.get().isIE()
  661. && BrowserInfo.get().getBrowserMajorVersion() < 11) {
  662. // Must take margin,border,padding manually into account for
  663. // menu element as we measure the element child and set width to
  664. // the element parent
  665. double naturalMenuOuterWidth;
  666. if (BrowserInfo.get().getBrowserMajorVersion() < 10) {
  667. // On IE 8 & 9 visibility is set to hidden and measuring
  668. // elements while they are hidden yields incorrect results
  669. String before = menu.getElement().getParentElement()
  670. .getStyle().getVisibility();
  671. menu.getElement().getParentElement().getStyle()
  672. .setVisibility(Visibility.VISIBLE);
  673. naturalMenuOuterWidth = WidgetUtil
  674. .getRequiredWidthDouble(menuFirstChild)
  675. + getMarginBorderPaddingWidth(menu.getElement());
  676. menu.getElement().getParentElement().getStyle()
  677. .setProperty("visibility", before);
  678. } else {
  679. naturalMenuOuterWidth = WidgetUtil
  680. .getRequiredWidthDouble(menuFirstChild)
  681. + getMarginBorderPaddingWidth(menu.getElement());
  682. }
  683. /*
  684. * IE requires us to specify the width for the container
  685. * element. Otherwise it will be 100% wide
  686. */
  687. double rootWidth = Math.max(desiredWidth - popupOuterPadding,
  688. naturalMenuOuterWidth);
  689. getContainerElement().getStyle().setWidth(rootWidth, Unit.PX);
  690. }
  691. final int textInputHeight = VFilterSelect.this.getOffsetHeight();
  692. final int textInputTopOnPage = tb.getAbsoluteTop();
  693. final int viewportOffset = Document.get().getScrollTop();
  694. final int textInputTopInViewport = textInputTopOnPage
  695. - viewportOffset;
  696. final int textInputBottomInViewport = textInputTopInViewport
  697. + textInputHeight;
  698. final int spaceAboveInViewport = textInputTopInViewport;
  699. final int spaceBelowInViewport = Window.getClientHeight()
  700. - textInputBottomInViewport;
  701. if (spaceBelowInViewport < offsetHeight
  702. && spaceBelowInViewport < spaceAboveInViewport) {
  703. // popup on top of input instead
  704. if (offsetHeight > spaceAboveInViewport) {
  705. // Shrink popup height to fit above
  706. offsetHeight = spaceAboveInViewport;
  707. }
  708. top = textInputTopOnPage - offsetHeight;
  709. } else {
  710. // Show below, position calculated in showSuggestions for some
  711. // strange reason
  712. top = topPosition;
  713. offsetHeight = Math.min(offsetHeight, spaceBelowInViewport);
  714. }
  715. // fetch real width (mac FF bugs here due GWT popups overflow:auto )
  716. offsetWidth = menuFirstChild.getOffsetWidth();
  717. if (offsetHeight < desiredHeight) {
  718. int menuHeight = offsetHeight;
  719. if (isPagingEnabled) {
  720. menuHeight -= up.getOffsetHeight() + down.getOffsetHeight()
  721. + status.getOffsetHeight();
  722. } else {
  723. final ComputedStyle s = new ComputedStyle(
  724. menu.getElement());
  725. menuHeight -= s.getIntProperty("marginBottom")
  726. + s.getIntProperty("marginTop");
  727. }
  728. // If the available page height is really tiny then this will be
  729. // negative and an exception will be thrown on setHeight.
  730. int menuElementHeight = menu.getItemOffsetHeight();
  731. if (menuHeight < menuElementHeight) {
  732. menuHeight = menuElementHeight;
  733. }
  734. menu.setHeight(menuHeight + "px");
  735. if (suggestionPopupWidth == null) {
  736. final int naturalMenuWidthPlusScrollBar = naturalMenuWidth
  737. + WidgetUtil.getNativeScrollbarSize();
  738. if (offsetWidth < naturalMenuWidthPlusScrollBar) {
  739. menu.setWidth(naturalMenuWidthPlusScrollBar + "px");
  740. }
  741. }
  742. }
  743. if (offsetWidth + left > Window.getClientWidth()) {
  744. left = VFilterSelect.this.getAbsoluteLeft()
  745. + VFilterSelect.this.getOffsetWidth() - offsetWidth;
  746. if (left < 0) {
  747. left = 0;
  748. menu.setWidth(Window.getClientWidth() + "px");
  749. }
  750. if (BrowserInfo.get().isIE()
  751. && BrowserInfo.get().getBrowserMajorVersion() < 10) {
  752. setTdWidth(menu.getElement(), Window.getClientWidth() - 8);
  753. }
  754. }
  755. setPopupPosition(left, top);
  756. menu.scrollSelectionIntoView();
  757. }
  758. /**
  759. * Adds in-line CSS rules to the DOM according to the
  760. * suggestionPopupWidth field
  761. *
  762. * @param desiredWidth
  763. * @param naturalMenuWidth
  764. */
  765. private void updateMenuWidth(final int desiredWidth,
  766. int naturalMenuWidth) {
  767. /**
  768. * Three different width modes for the suggestion pop-up:
  769. *
  770. * 1. Legacy "null"-mode: width is determined by the longest item
  771. * caption for each page while still maintaining minimum width of
  772. * (desiredWidth - popupOuterPadding)
  773. *
  774. * 2. relative to the component itself
  775. *
  776. * 3. fixed width
  777. */
  778. String width = "auto";
  779. if (suggestionPopupWidth == null) {
  780. if (naturalMenuWidth < desiredWidth) {
  781. naturalMenuWidth = desiredWidth - popupOuterPadding;
  782. width = (desiredWidth - popupOuterPadding) + "px";
  783. }
  784. } else if (isrelativeUnits(suggestionPopupWidth)) {
  785. float mainComponentWidth = desiredWidth - popupOuterPadding;
  786. // convert percentage value to fraction
  787. int widthInPx = Math.round(
  788. mainComponentWidth * asFraction(suggestionPopupWidth));
  789. width = widthInPx + "px";
  790. } else {
  791. // use as fixed width CSS definition
  792. width = WidgetUtil.escapeAttribute(suggestionPopupWidth);
  793. }
  794. menu.setWidth(width);
  795. // IE8 or 9?
  796. if (BrowserInfo.get().isIE()
  797. && BrowserInfo.get().getBrowserMajorVersion() < 10) {
  798. // using legacy mode?
  799. if (suggestionPopupWidth == null) {
  800. // set the TD widths manually as these browsers do not
  801. // respect display: block; width:100% rules
  802. setTdWidth(menu.getElement(), naturalMenuWidth);
  803. } else {
  804. int compensation = WidgetUtil
  805. .measureHorizontalPaddingAndBorder(
  806. menu.getElement(), 4);
  807. setTdWidth(menu.getElement(),
  808. menu.getOffsetWidth() - compensation);
  809. }
  810. }
  811. }
  812. /**
  813. * Descends to child elements until finds TD elements and sets their
  814. * width in pixels. Can be used to workaround IE8 & 9 TD element
  815. * display: block issues
  816. *
  817. * @param parent
  818. * @param width
  819. */
  820. private void setTdWidth(Node parent, int width) {
  821. for (int i = 0; i < parent.getChildCount(); i++) {
  822. Node child = parent.getChild(i);
  823. if ("td".equals(child.getNodeName().toLowerCase())) {
  824. ((Element) child).getStyle().setWidth(width, Unit.PX);
  825. } else {
  826. setTdWidth(child, width);
  827. }
  828. }
  829. }
  830. /**
  831. * Returns the percentage value as a fraction, e.g. 42% -> 0.42
  832. *
  833. * @param percentage
  834. */
  835. private float asFraction(String percentage) {
  836. String trimmed = percentage.trim();
  837. String withoutPercentSign = trimmed.substring(0,
  838. trimmed.length() - 1);
  839. float asFraction = Float.parseFloat(withoutPercentSign) / 100;
  840. return asFraction;
  841. }
  842. /**
  843. * @since 7.7
  844. * @param suggestionPopupWidth
  845. * @return
  846. */
  847. private boolean isrelativeUnits(String suggestionPopupWidth) {
  848. return suggestionPopupWidth.trim().endsWith("%");
  849. }
  850. /**
  851. * Was the popup just closed?
  852. *
  853. * @return true if popup was just closed
  854. */
  855. public boolean isJustClosed() {
  856. debug("VFS.SP: justClosed()");
  857. final long now = (new Date()).getTime();
  858. return (lastAutoClosed > 0 && (now - lastAutoClosed) < 200);
  859. }
  860. /*
  861. * (non-Javadoc)
  862. *
  863. * @see
  864. * com.google.gwt.event.logical.shared.CloseHandler#onClose(com.google
  865. * .gwt.event.logical.shared.CloseEvent)
  866. */
  867. @Override
  868. public void onClose(CloseEvent<PopupPanel> event) {
  869. if (enableDebug) {
  870. debug("VFS.SP: onClose(" + event.isAutoClosed() + ")");
  871. }
  872. if (event.isAutoClosed()) {
  873. lastAutoClosed = (new Date()).getTime();
  874. }
  875. }
  876. /**
  877. * Updates style names in suggestion popup to help theme building.
  878. *
  879. * @param uidl
  880. * UIDL for the whole combo box
  881. * @param componentState
  882. * shared state of the combo box
  883. */
  884. public void updateStyleNames(UIDL uidl,
  885. AbstractComponentState componentState) {
  886. debug("VFS.SP: updateStyleNames()");
  887. setStyleName(
  888. VFilterSelect.this.getStylePrimaryName() + "-suggestpopup");
  889. menu.setStyleName(
  890. VFilterSelect.this.getStylePrimaryName() + "-suggestmenu");
  891. status.setClassName(
  892. VFilterSelect.this.getStylePrimaryName() + "-status");
  893. if (ComponentStateUtil.hasStyles(componentState)) {
  894. for (String style : componentState.styles) {
  895. if (!"".equals(style)) {
  896. addStyleDependentName(style);
  897. }
  898. }
  899. }
  900. }
  901. }
  902. /**
  903. * The menu where the suggestions are rendered
  904. */
  905. public class SuggestionMenu extends MenuBar
  906. implements SubPartAware, LoadHandler {
  907. private VLazyExecutor delayedImageLoadExecutioner = new VLazyExecutor(
  908. 100, new ScheduledCommand() {
  909. @Override
  910. public void execute() {
  911. debug("VFS.SM: delayedImageLoadExecutioner()");
  912. if (suggestionPopup.isVisible()
  913. && suggestionPopup.isAttached()) {
  914. setWidth("");
  915. getElement().getFirstChildElement().getStyle()
  916. .clearWidth();
  917. suggestionPopup
  918. .setPopupPositionAndShow(suggestionPopup);
  919. }
  920. }
  921. });
  922. /**
  923. * Default constructor
  924. */
  925. SuggestionMenu() {
  926. super(true);
  927. debug("VFS.SM: constructor()");
  928. addDomHandler(this, LoadEvent.getType());
  929. setScrollEnabled(true);
  930. }
  931. /**
  932. * Fixes menus height to use same space as full page would use. Needed
  933. * to avoid height changes when quickly "scrolling" to last page.
  934. */
  935. public void fixHeightTo(int pageItemsCount) {
  936. setHeight(getPreferredHeight(pageItemsCount));
  937. }
  938. /*
  939. * Gets the preferred height of the menu including pageItemsCount items.
  940. */
  941. String getPreferredHeight(int pageItemsCount) {
  942. if (currentSuggestions.size() > 0) {
  943. final int pixels = (getPreferredHeight()
  944. / currentSuggestions.size()) * pageItemsCount;
  945. return pixels + "px";
  946. } else {
  947. return "";
  948. }
  949. }
  950. /**
  951. * Sets the suggestions rendered in the menu
  952. *
  953. * @param suggestions
  954. * The suggestions to be rendered in the menu
  955. */
  956. public void setSuggestions(
  957. Collection<FilterSelectSuggestion> suggestions) {
  958. if (enableDebug) {
  959. debug("VFS.SM: setSuggestions(" + suggestions + ")");
  960. }
  961. clearItems();
  962. final Iterator<FilterSelectSuggestion> it = suggestions.iterator();
  963. boolean isFirstIteration = true;
  964. while (it.hasNext()) {
  965. final FilterSelectSuggestion s = it.next();
  966. final MenuItem mi = new MenuItem(s.getDisplayString(), true, s);
  967. String style = s.getStyle();
  968. if (style != null) {
  969. mi.addStyleName("v-filterselect-item-" + style);
  970. }
  971. Roles.getListitemRole().set(mi.getElement());
  972. WidgetUtil.sinkOnloadForImages(mi.getElement());
  973. this.addItem(mi);
  974. // By default, first item on the list is always highlighted,
  975. // unless adding new items is allowed.
  976. if (isFirstIteration && !allowNewItem) {
  977. selectItem(mi);
  978. }
  979. // If the filter matches the current selection, highlight that
  980. // instead of the first item.
  981. if (tb.getText().equals(s.getReplacementString())
  982. && s == currentSuggestion) {
  983. selectItem(mi);
  984. }
  985. isFirstIteration = false;
  986. }
  987. if (suggestionPopupWidth != null && BrowserInfo.get().isIE()
  988. && BrowserInfo.get().getBrowserMajorVersion() < 10) {
  989. // set TD width to a low value so that they won't mandate the
  990. // suggestion pop-up width
  991. suggestionPopup.setTdWidth(suggestionPopup.menu.getElement(),
  992. 1);
  993. }
  994. }
  995. /**
  996. * Send the current selection to the server. Triggered when a selection
  997. * is made or on a blur event.
  998. */
  999. public void doSelectedItemAction() {
  1000. debug("VFS.SM: doSelectedItemAction()");
  1001. // do not send a value change event if null was and stays selected
  1002. final String enteredItemValue = tb.getText();
  1003. if (nullSelectionAllowed && "".equals(enteredItemValue)
  1004. && selectedOptionKey != null
  1005. && !"".equals(selectedOptionKey)) {
  1006. if (nullSelectItem) {
  1007. reset();
  1008. return;
  1009. }
  1010. // null is not visible on pages != 0, and not visible when
  1011. // filtering: handle separately
  1012. client.updateVariable(paintableId, "filter", "", false);
  1013. client.updateVariable(paintableId, "page", 0, false);
  1014. client.updateVariable(paintableId, "selected", new String[] {},
  1015. immediate);
  1016. afterUpdateClientVariables();
  1017. suggestionPopup.hide();
  1018. return;
  1019. }
  1020. updateSelectionWhenReponseIsReceived = waitingForFilteringResponse;
  1021. if (!waitingForFilteringResponse) {
  1022. doPostFilterSelectedItemAction();
  1023. }
  1024. }
  1025. /**
  1026. * Triggered after a selection has been made
  1027. */
  1028. public void doPostFilterSelectedItemAction() {
  1029. debug("VFS.SM: doPostFilterSelectedItemAction()");
  1030. final MenuItem item = getSelectedItem();
  1031. final String enteredItemValue = tb.getText();
  1032. updateSelectionWhenReponseIsReceived = false;
  1033. // check for exact match in menu
  1034. int p = getItems().size();
  1035. if (p > 0) {
  1036. for (int i = 0; i < p; i++) {
  1037. final MenuItem potentialExactMatch = getItems().get(i);
  1038. if (potentialExactMatch.getText()
  1039. .equals(enteredItemValue)) {
  1040. selectItem(potentialExactMatch);
  1041. // do not send a value change event if null was and
  1042. // stays selected
  1043. if (!"".equals(enteredItemValue)
  1044. || (selectedOptionKey != null
  1045. && !"".equals(selectedOptionKey))) {
  1046. doItemAction(potentialExactMatch, true);
  1047. }
  1048. suggestionPopup.hide();
  1049. return;
  1050. }
  1051. }
  1052. }
  1053. if (allowNewItem) {
  1054. if (!prompting && !enteredItemValue.equals(lastNewItemString)) {
  1055. /*
  1056. * Store last sent new item string to avoid double sends
  1057. */
  1058. lastNewItemString = enteredItemValue;
  1059. client.updateVariable(paintableId, "newitem",
  1060. enteredItemValue, immediate);
  1061. afterUpdateClientVariables();
  1062. }
  1063. } else if (item != null && !"".equals(lastFilter)
  1064. && (filteringmode == FilteringMode.CONTAINS
  1065. ? item.getText().toLowerCase()
  1066. .contains(lastFilter.toLowerCase())
  1067. : item.getText().toLowerCase()
  1068. .startsWith(lastFilter.toLowerCase()))) {
  1069. doItemAction(item, true);
  1070. } else {
  1071. // currentSuggestion has key="" for nullselection
  1072. if (currentSuggestion != null
  1073. && !currentSuggestion.key.equals("")) {
  1074. // An item (not null) selected
  1075. String text = currentSuggestion.getReplacementString();
  1076. setText(text);
  1077. selectedOptionKey = currentSuggestion.key;
  1078. } else {
  1079. // Null selected
  1080. setText("");
  1081. selectedOptionKey = null;
  1082. }
  1083. }
  1084. suggestionPopup.hide();
  1085. }
  1086. private static final String SUBPART_PREFIX = "item";
  1087. @Override
  1088. public com.google.gwt.user.client.Element getSubPartElement(
  1089. String subPart) {
  1090. int index = Integer
  1091. .parseInt(subPart.substring(SUBPART_PREFIX.length()));
  1092. MenuItem item = getItems().get(index);
  1093. return item.getElement();
  1094. }
  1095. @Override
  1096. public String getSubPartName(
  1097. com.google.gwt.user.client.Element subElement) {
  1098. if (!getElement().isOrHasChild(subElement)) {
  1099. return null;
  1100. }
  1101. Element menuItemRoot = subElement;
  1102. while (menuItemRoot != null
  1103. && !menuItemRoot.getTagName().equalsIgnoreCase("td")) {
  1104. menuItemRoot = menuItemRoot.getParentElement().cast();
  1105. }
  1106. // "menuItemRoot" is now the root of the menu item
  1107. final int itemCount = getItems().size();
  1108. for (int i = 0; i < itemCount; i++) {
  1109. if (getItems().get(i).getElement() == menuItemRoot) {
  1110. String name = SUBPART_PREFIX + i;
  1111. return name;
  1112. }
  1113. }
  1114. return null;
  1115. }
  1116. @Override
  1117. public void onLoad(LoadEvent event) {
  1118. debug("VFS.SM: onLoad()");
  1119. // Handle icon onload events to ensure shadow is resized
  1120. // correctly
  1121. delayedImageLoadExecutioner.trigger();
  1122. }
  1123. /**
  1124. * @deprecated use {@link SuggestionPopup#selectFirstItem()} instead.
  1125. */
  1126. @Deprecated
  1127. public void selectFirstItem() {
  1128. debug("VFS.SM: selectFirstItem()");
  1129. MenuItem firstItem = getItems().get(0);
  1130. selectItem(firstItem);
  1131. }
  1132. /**
  1133. * @deprecated use {@link SuggestionPopup#selectLastItem()} instead.
  1134. */
  1135. @Deprecated
  1136. public void selectLastItem() {
  1137. debug("VFS.SM: selectLastItem()");
  1138. List<MenuItem> items = getItems();
  1139. MenuItem lastItem = items.get(items.size() - 1);
  1140. selectItem(lastItem);
  1141. }
  1142. /*
  1143. * Gets the height of one menu item.
  1144. */
  1145. int getItemOffsetHeight() {
  1146. List<MenuItem> items = getItems();
  1147. return items != null && items.size() > 0
  1148. ? items.get(0).getOffsetHeight()
  1149. : 0;
  1150. }
  1151. /*
  1152. * Gets the width of one menu item.
  1153. */
  1154. int getItemOffsetWidth() {
  1155. List<MenuItem> items = getItems();
  1156. return items != null && items.size() > 0
  1157. ? items.get(0).getOffsetWidth()
  1158. : 0;
  1159. }
  1160. /**
  1161. * Returns true if the scroll is active on the menu element or if the
  1162. * menu currently displays the last page with less items then the
  1163. * maximum visibility (in which case the scroll is not active, but the
  1164. * scroll is active for any other page in general).
  1165. *
  1166. * @since 7.2.6
  1167. */
  1168. @Override
  1169. public boolean isScrollActive() {
  1170. String height = getElement().getStyle().getHeight();
  1171. String preferredHeight = getPreferredHeight(pageLength);
  1172. return !(height == null || height.isEmpty()
  1173. || height.equals(preferredHeight));
  1174. }
  1175. }
  1176. /**
  1177. * TextBox variant used as input element for filter selects, which prevents
  1178. * selecting text when disabled.
  1179. *
  1180. * @since 7.1.5
  1181. */
  1182. public class FilterSelectTextBox extends TextBox {
  1183. /**
  1184. * Creates a new filter select text box.
  1185. *
  1186. * @since 7.6.4
  1187. */
  1188. public FilterSelectTextBox() {
  1189. /*-
  1190. * Stop the browser from showing its own suggestion popup.
  1191. *
  1192. * Using an invalid value instead of "off" as suggested by
  1193. * https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
  1194. *
  1195. * Leaving the non-standard Safari options autocapitalize and
  1196. * autocorrect untouched since those do not interfere in the same
  1197. * way, and they might be useful in a combo box where new items are
  1198. * allowed.
  1199. */
  1200. getElement().setAttribute("autocomplete", "nope");
  1201. }
  1202. /**
  1203. * Overridden to avoid selecting text when text input is disabled
  1204. */
  1205. @Override
  1206. public void setSelectionRange(int pos, int length) {
  1207. if (textInputEnabled) {
  1208. /*
  1209. * set selection range with a backwards direction: anchor at the
  1210. * back, focus at the front. This means that items that are too
  1211. * long to display will display from the start and not the end
  1212. * even on Firefox.
  1213. *
  1214. * We need the JSNI function to set selection range so that we
  1215. * can use the optional direction attribute to set the anchor to
  1216. * the end and the focus to the start. This makes Firefox work
  1217. * the same way as other browsers (#13477)
  1218. */
  1219. WidgetUtil.setSelectionRange(getElement(), pos, length,
  1220. "backward");
  1221. } else {
  1222. /*
  1223. * Setting the selectionrange for an uneditable textbox leads to
  1224. * unwanted behavior when the width of the textbox is narrower
  1225. * than the width of the entry: the end of the entry is shown
  1226. * instead of the beginning. (see #13477)
  1227. *
  1228. * To avoid this, we set the caret to the beginning of the line.
  1229. */
  1230. super.setSelectionRange(0, 0);
  1231. }
  1232. }
  1233. }
  1234. @Deprecated
  1235. public static final FilteringMode FILTERINGMODE_OFF = FilteringMode.OFF;
  1236. @Deprecated
  1237. public static final FilteringMode FILTERINGMODE_STARTSWITH = FilteringMode.STARTSWITH;
  1238. @Deprecated
  1239. public static final FilteringMode FILTERINGMODE_CONTAINS = FilteringMode.CONTAINS;
  1240. public static final String CLASSNAME = "v-filterselect";
  1241. private static final String STYLE_NO_INPUT = "no-input";
  1242. /** For internal use only. May be removed or replaced in the future. */
  1243. public int pageLength = 10;
  1244. private boolean enableDebug = false;
  1245. private final FlowPanel panel = new FlowPanel();
  1246. /**
  1247. * The text box where the filter is written
  1248. * <p>
  1249. * For internal use only. May be removed or replaced in the future.
  1250. */
  1251. public final TextBox tb;
  1252. /** For internal use only. May be removed or replaced in the future. */
  1253. public final SuggestionPopup suggestionPopup;
  1254. /**
  1255. * Used when measuring the width of the popup
  1256. */
  1257. private final HTML popupOpener = new HTML("") {
  1258. /*
  1259. * (non-Javadoc)
  1260. *
  1261. * @see
  1262. * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt
  1263. * .user.client.Event)
  1264. */
  1265. @Override
  1266. public void onBrowserEvent(Event event) {
  1267. super.onBrowserEvent(event);
  1268. /*
  1269. * Prevent the keyboard focus from leaving the textfield by
  1270. * preventing the default behavior of the browser. Fixes #4285.
  1271. */
  1272. handleMouseDownEvent(event);
  1273. }
  1274. };
  1275. private class IconWidget extends Widget {
  1276. private Icon icon;
  1277. IconWidget(Icon icon) {
  1278. this.icon = icon;
  1279. setElement(icon.getElement());
  1280. addDomHandler(VFilterSelect.this, ClickEvent.getType());
  1281. }
  1282. }
  1283. private IconWidget selectedItemIcon;
  1284. /** For internal use only. May be removed or replaced in the future. */
  1285. public ApplicationConnection client;
  1286. /** For internal use only. May be removed or replaced in the future. */
  1287. public String paintableId;
  1288. /** For internal use only. May be removed or replaced in the future. */
  1289. public int currentPage;
  1290. /**
  1291. * A collection of available suggestions (options) as received from the
  1292. * server.
  1293. * <p>
  1294. * For internal use only. May be removed or replaced in the future.
  1295. */
  1296. public final List<FilterSelectSuggestion> currentSuggestions = new ArrayList<FilterSelectSuggestion>();
  1297. /** For internal use only. May be removed or replaced in the future. */
  1298. public boolean immediate;
  1299. /** For internal use only. May be removed or replaced in the future. */
  1300. public String selectedOptionKey;
  1301. /** For internal use only. May be removed or replaced in the future. */
  1302. public boolean waitingForFilteringResponse = false;
  1303. /** For internal use only. May be removed or replaced in the future. */
  1304. public boolean updateSelectionWhenReponseIsReceived = false;
  1305. /** For internal use only. May be removed or replaced in the future. */
  1306. public boolean initDone = false;
  1307. /** For internal use only. May be removed or replaced in the future. */
  1308. public String lastFilter = "";
  1309. /** For internal use only. May be removed or replaced in the future. */
  1310. public enum Select {
  1311. NONE, FIRST, LAST
  1312. }
  1313. /** For internal use only. May be removed or replaced in the future. */
  1314. public Select selectPopupItemWhenResponseIsReceived = Select.NONE;
  1315. /**
  1316. * The current suggestion selected from the dropdown. This is one of the
  1317. * values in currentSuggestions except when filtering, in this case
  1318. * currentSuggestion might not be in currentSuggestions.
  1319. * <p>
  1320. * For internal use only. May be removed or replaced in the future.
  1321. */
  1322. public FilterSelectSuggestion currentSuggestion;
  1323. /** For internal use only. May be removed or replaced in the future. */
  1324. public boolean allowNewItem;
  1325. /** For internal use only. May be removed or replaced in the future. */
  1326. public int totalMatches;
  1327. /** For internal use only. May be removed or replaced in the future. */
  1328. public boolean nullSelectionAllowed;
  1329. /** For internal use only. May be removed or replaced in the future. */
  1330. public boolean nullSelectItem;
  1331. /** For internal use only. May be removed or replaced in the future. */
  1332. public boolean enabled;
  1333. /** For internal use only. May be removed or replaced in the future. */
  1334. public boolean readonly;
  1335. /** For internal use only. May be removed or replaced in the future. */
  1336. public FilteringMode filteringmode = FilteringMode.OFF;
  1337. // shown in unfocused empty field, disappears on focus (e.g "Search here")
  1338. private static final String CLASSNAME_PROMPT = "prompt";
  1339. /** For internal use only. May be removed or replaced in the future. */
  1340. public String inputPrompt = "";
  1341. /** For internal use only. May be removed or replaced in the future. */
  1342. public boolean prompting = false;
  1343. /**
  1344. * Set true when popupopened has been clicked. Cleared on each UIDL-update.
  1345. * This handles the special case where are not filtering yet and the
  1346. * selected value has changed on the server-side. See #2119
  1347. * <p>
  1348. * For internal use only. May be removed or replaced in the future.
  1349. */
  1350. public boolean popupOpenerClicked;
  1351. /** For internal use only. May be removed or replaced in the future. */
  1352. public int suggestionPopupMinWidth = 0;
  1353. public String suggestionPopupWidth = null;
  1354. private int popupWidth = -1;
  1355. /**
  1356. * Stores the last new item string to avoid double submissions. Cleared on
  1357. * uidl updates.
  1358. * <p>
  1359. * For internal use only. May be removed or replaced in the future.
  1360. */
  1361. public String lastNewItemString;
  1362. /** For internal use only. May be removed or replaced in the future. */
  1363. public boolean focused = false;
  1364. /**
  1365. * If set to false, the component should not allow entering text to the
  1366. * field even for filtering.
  1367. */
  1368. private boolean textInputEnabled = true;
  1369. /**
  1370. * Default constructor.
  1371. */
  1372. public VFilterSelect() {
  1373. tb = createTextBox();
  1374. suggestionPopup = createSuggestionPopup();
  1375. popupOpener.sinkEvents(Event.ONMOUSEDOWN);
  1376. Roles.getButtonRole().setAriaHiddenState(popupOpener.getElement(),
  1377. true);
  1378. Roles.getButtonRole().set(popupOpener.getElement());
  1379. panel.add(tb);
  1380. panel.add(popupOpener);
  1381. initWidget(panel);
  1382. Roles.getComboboxRole().set(panel.getElement());
  1383. tb.addKeyDownHandler(this);
  1384. tb.addKeyUpHandler(this);
  1385. tb.addFocusHandler(this);
  1386. tb.addBlurHandler(this);
  1387. tb.addClickHandler(this);
  1388. popupOpener.addClickHandler(this);
  1389. setStyleName(CLASSNAME);
  1390. sinkEvents(Event.ONPASTE);
  1391. }
  1392. private static double getMarginBorderPaddingWidth(Element element) {
  1393. final ComputedStyle s = new ComputedStyle(element);
  1394. return s.getMarginWidth() + s.getBorderWidth() + s.getPaddingWidth();
  1395. }
  1396. /*
  1397. * (non-Javadoc)
  1398. *
  1399. * @see
  1400. * com.google.gwt.user.client.ui.Composite#onBrowserEvent(com.google.gwt
  1401. * .user.client.Event)
  1402. */
  1403. @Override
  1404. public void onBrowserEvent(Event event) {
  1405. super.onBrowserEvent(event);
  1406. if (event.getTypeInt() == Event.ONPASTE) {
  1407. if (textInputEnabled) {
  1408. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  1409. @Override
  1410. public void execute() {
  1411. filterOptions(currentPage);
  1412. }
  1413. });
  1414. }
  1415. }
  1416. }
  1417. /**
  1418. * This method will create the TextBox used by the VFilterSelect instance.
  1419. * It is invoked during the Constructor and should only be overridden if a
  1420. * custom TextBox shall be used. The overriding method cannot use any
  1421. * instance variables.
  1422. *
  1423. * @since 7.1.5
  1424. * @return TextBox instance used by this VFilterSelect
  1425. */
  1426. protected TextBox createTextBox() {
  1427. return new FilterSelectTextBox();
  1428. }
  1429. /**
  1430. * This method will create the SuggestionPopup used by the VFilterSelect
  1431. * instance. It is invoked during the Constructor and should only be
  1432. * overridden if a custom SuggestionPopup shall be used. The overriding
  1433. * method cannot use any instance variables.
  1434. *
  1435. * @since 7.1.5
  1436. * @return SuggestionPopup instance used by this VFilterSelect
  1437. */
  1438. protected SuggestionPopup createSuggestionPopup() {
  1439. return new SuggestionPopup();
  1440. }
  1441. @Override
  1442. public void setStyleName(String style) {
  1443. super.setStyleName(style);
  1444. updateStyleNames();
  1445. }
  1446. @Override
  1447. public void setStylePrimaryName(String style) {
  1448. super.setStylePrimaryName(style);
  1449. updateStyleNames();
  1450. }
  1451. protected void updateStyleNames() {
  1452. tb.setStyleName(getStylePrimaryName() + "-input");
  1453. popupOpener.setStyleName(getStylePrimaryName() + "-button");
  1454. suggestionPopup.setStyleName(getStylePrimaryName() + "-suggestpopup");
  1455. }
  1456. /**
  1457. * Does the Select have more pages?
  1458. *
  1459. * @return true if a next page exists, else false if the current page is the
  1460. * last page
  1461. */
  1462. public boolean hasNextPage() {
  1463. if (pageLength > 0 && totalMatches > (currentPage + 1) * pageLength) {
  1464. return true;
  1465. } else {
  1466. return false;
  1467. }
  1468. }
  1469. /**
  1470. * Filters the options at a certain page. Uses the text box input as a
  1471. * filter
  1472. *
  1473. * @param page
  1474. * The page which items are to be filtered
  1475. */
  1476. public void filterOptions(int page) {
  1477. filterOptions(page, tb.getText());
  1478. }
  1479. /**
  1480. * Filters the options at certain page using the given filter
  1481. *
  1482. * @param page
  1483. * The page to filter
  1484. * @param filter
  1485. * The filter to apply to the components
  1486. */
  1487. public void filterOptions(int page, String filter) {
  1488. filterOptions(page, filter, true);
  1489. }
  1490. /**
  1491. * Filters the options at certain page using the given filter
  1492. *
  1493. * @param page
  1494. * The page to filter
  1495. * @param filter
  1496. * The filter to apply to the options
  1497. * @param immediate
  1498. * Whether to send the options request immediately
  1499. */
  1500. private void filterOptions(int page, String filter, boolean immediate) {
  1501. debug("VFS: filterOptions(" + page + ", " + filter + ", " + immediate
  1502. + ")");
  1503. if (filter.equals(lastFilter) && currentPage == page) {
  1504. if (!suggestionPopup.isAttached()) {
  1505. suggestionPopup.showSuggestions(currentSuggestions, currentPage,
  1506. totalMatches);
  1507. }
  1508. return;
  1509. }
  1510. if (!filter.equals(lastFilter)) {
  1511. // when filtering, let the server decide the page unless we've
  1512. // set the filter to empty and explicitly said that we want to see
  1513. // the results starting from page 0.
  1514. if ("".equals(filter) && page != 0) {
  1515. // let server decide
  1516. page = -1;
  1517. } else {
  1518. page = 0;
  1519. }
  1520. }
  1521. waitingForFilteringResponse = true;
  1522. client.updateVariable(paintableId, "filter", filter, false);
  1523. client.updateVariable(paintableId, "page", page, immediate);
  1524. afterUpdateClientVariables();
  1525. lastFilter = filter;
  1526. currentPage = page;
  1527. }
  1528. /** For internal use only. May be removed or replaced in the future. */
  1529. public void updateReadOnly() {
  1530. debug("VFS: updateReadOnly()");
  1531. tb.setReadOnly(readonly || !textInputEnabled);
  1532. }
  1533. public void setTextInputEnabled(boolean textInputEnabled) {
  1534. debug("VFS: setTextInputEnabled()");
  1535. // Always update styles as they might have been overwritten
  1536. if (textInputEnabled) {
  1537. removeStyleDependentName(STYLE_NO_INPUT);
  1538. Roles.getTextboxRole().removeAriaReadonlyProperty(tb.getElement());
  1539. } else {
  1540. addStyleDependentName(STYLE_NO_INPUT);
  1541. Roles.getTextboxRole().setAriaReadonlyProperty(tb.getElement(),
  1542. true);
  1543. }
  1544. if (this.textInputEnabled == textInputEnabled) {
  1545. return;
  1546. }
  1547. this.textInputEnabled = textInputEnabled;
  1548. updateReadOnly();
  1549. }
  1550. /**
  1551. * Sets the text in the text box.
  1552. *
  1553. * @param text
  1554. * the text to set in the text box
  1555. */
  1556. public void setTextboxText(final String text) {
  1557. if (enableDebug) {
  1558. debug("VFS: setTextboxText(" + text + ")");
  1559. }
  1560. setText(text);
  1561. }
  1562. private void setText(final String text) {
  1563. /**
  1564. * To leave caret in the beginning of the line. SetSelectionRange
  1565. * wouldn't work on IE (see #13477)
  1566. */
  1567. Direction previousDirection = tb.getDirection();
  1568. tb.setDirection(Direction.RTL);
  1569. tb.setText(text);
  1570. tb.setDirection(previousDirection);
  1571. }
  1572. /**
  1573. * Turns prompting on. When prompting is turned on a command prompt is shown
  1574. * in the text box if nothing has been entered.
  1575. */
  1576. public void setPromptingOn() {
  1577. debug("VFS: setPromptingOn()");
  1578. if (!prompting) {
  1579. prompting = true;
  1580. addStyleDependentName(CLASSNAME_PROMPT);
  1581. }
  1582. setTextboxText(inputPrompt);
  1583. }
  1584. /**
  1585. * Turns prompting off. When prompting is turned on a command prompt is
  1586. * shown in the text box if nothing has been entered.
  1587. * <p>
  1588. * For internal use only. May be removed or replaced in the future.
  1589. *
  1590. * @param text
  1591. * The text the text box should contain.
  1592. */
  1593. public void setPromptingOff(String text) {
  1594. debug("VFS: setPromptingOff()");
  1595. setTextboxText(text);
  1596. if (prompting) {
  1597. prompting = false;
  1598. removeStyleDependentName(CLASSNAME_PROMPT);
  1599. }
  1600. }
  1601. /**
  1602. * Triggered when a suggestion is selected
  1603. *
  1604. * @param suggestion
  1605. * The suggestion that just got selected.
  1606. */
  1607. public void onSuggestionSelected(FilterSelectSuggestion suggestion) {
  1608. if (enableDebug) {
  1609. debug("VFS: onSuggestionSelected(" + suggestion.caption + ": "
  1610. + suggestion.key + ")");
  1611. }
  1612. updateSelectionWhenReponseIsReceived = false;
  1613. currentSuggestion = suggestion;
  1614. String newKey;
  1615. if (suggestion.key.equals("")) {
  1616. // "nullselection"
  1617. newKey = "";
  1618. } else {
  1619. // normal selection
  1620. newKey = suggestion.getOptionKey();
  1621. }
  1622. String text = suggestion.getReplacementString();
  1623. if ("".equals(newKey) && !focused) {
  1624. setPromptingOn();
  1625. } else {
  1626. setPromptingOff(text);
  1627. }
  1628. setSelectedItemIcon(suggestion.getIconUri());
  1629. if (!(newKey.equals(selectedOptionKey)
  1630. || ("".equals(newKey) && selectedOptionKey == null))) {
  1631. selectedOptionKey = newKey;
  1632. client.updateVariable(paintableId, "selected",
  1633. new String[] { selectedOptionKey }, immediate);
  1634. afterUpdateClientVariables();
  1635. // currentPage = -1; // forget the page
  1636. }
  1637. if (getSelectedCaption() != null && newKey.equals("")) {
  1638. // In scrollToPage(false) mode selecting null seems to be broken
  1639. // if current selection is not on first page. The above clause is so
  1640. // hard to interpret that new clause added here :-(
  1641. selectedOptionKey = newKey;
  1642. explicitSelectedCaption = null;
  1643. client.updateVariable(paintableId, "selected",
  1644. new String[] { selectedOptionKey }, immediate);
  1645. afterUpdateClientVariables();
  1646. }
  1647. suggestionPopup.hide();
  1648. }
  1649. /**
  1650. * Sets the icon URI of the selected item. The icon is shown on the left
  1651. * side of the item caption text. Set the URI to null to remove the icon.
  1652. *
  1653. * @param iconUri
  1654. * The URI of the icon
  1655. */
  1656. public void setSelectedItemIcon(String iconUri) {
  1657. if (iconUri == null || iconUri.isEmpty()) {
  1658. if (selectedItemIcon != null) {
  1659. panel.remove(selectedItemIcon);
  1660. selectedItemIcon = null;
  1661. afterSelectedItemIconChange();
  1662. }
  1663. } else {
  1664. IconWidget newIcon = new IconWidget(client.getIcon(iconUri));
  1665. if (iconEquals(newIcon, selectedItemIcon)) {
  1666. /*
  1667. * Do not update the icon if nothing has changed. Otherwise we
  1668. * can cause problems such as not being able to click in the
  1669. * icon to open the popup (blur might occur and call this
  1670. * method, icon is replaced and the click event is not delivered
  1671. * to the new icon)
  1672. */
  1673. return;
  1674. }
  1675. if (selectedItemIcon != null) {
  1676. panel.remove(selectedItemIcon);
  1677. }
  1678. // Older IE versions don't scale icon correctly if DOM
  1679. // contains height and width attributes.
  1680. newIcon.getElement().removeAttribute("height");
  1681. newIcon.getElement().removeAttribute("width");
  1682. newIcon.addDomHandler(new LoadHandler() {
  1683. @Override
  1684. public void onLoad(LoadEvent event) {
  1685. afterSelectedItemIconChange();
  1686. }
  1687. }, LoadEvent.getType());
  1688. panel.insert(newIcon, 0);
  1689. selectedItemIcon = newIcon;
  1690. afterSelectedItemIconChange();
  1691. }
  1692. }
  1693. /**
  1694. * Checks if the icon widgets show the same icon.
  1695. *
  1696. * @param icon1
  1697. * the first widget
  1698. * @param icon2
  1699. * the second widget
  1700. * @return <code>true</code> if they show the same icon, <code>false</code>
  1701. * otherwise
  1702. */
  1703. private static boolean iconEquals(IconWidget icon1, IconWidget icon2) {
  1704. if (icon1 == null) {
  1705. return icon2 == null;
  1706. } else if (icon2 == null) {
  1707. return false;
  1708. } else {
  1709. return icon1.icon.getUri().equals(icon2.icon.getUri());
  1710. }
  1711. }
  1712. private void afterSelectedItemIconChange() {
  1713. if (BrowserInfo.get().isWebkit() || BrowserInfo.get().isIE8()) {
  1714. // Some browsers need a nudge to reposition the text field
  1715. forceReflow();
  1716. }
  1717. updateRootWidth();
  1718. if (selectedItemIcon != null) {
  1719. updateSelectedIconPosition();
  1720. }
  1721. }
  1722. private void forceReflow() {
  1723. WidgetUtil.setStyleTemporarily(tb.getElement(), "zoom", "1");
  1724. }
  1725. /**
  1726. * Positions the icon vertically in the middle. Should be called after the
  1727. * icon has loaded
  1728. */
  1729. private void updateSelectedIconPosition() {
  1730. // Position icon vertically to middle
  1731. int availableHeight = 0;
  1732. availableHeight = getOffsetHeight();
  1733. int iconHeight = WidgetUtil.getRequiredHeight(selectedItemIcon);
  1734. int marginTop = (availableHeight - iconHeight) / 2;
  1735. selectedItemIcon.getElement().getStyle().setMarginTop(marginTop,
  1736. Unit.PX);
  1737. }
  1738. private static Set<Integer> navigationKeyCodes = new HashSet<Integer>();
  1739. static {
  1740. navigationKeyCodes.add(KeyCodes.KEY_DOWN);
  1741. navigationKeyCodes.add(KeyCodes.KEY_UP);
  1742. navigationKeyCodes.add(KeyCodes.KEY_PAGEDOWN);
  1743. navigationKeyCodes.add(KeyCodes.KEY_PAGEUP);
  1744. navigationKeyCodes.add(KeyCodes.KEY_ENTER);
  1745. }
  1746. /*
  1747. * (non-Javadoc)
  1748. *
  1749. * @see
  1750. * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
  1751. * .event.dom.client.KeyDownEvent)
  1752. */
  1753. @Override
  1754. public void onKeyDown(KeyDownEvent event) {
  1755. if (enabled && !readonly) {
  1756. int keyCode = event.getNativeKeyCode();
  1757. if (enableDebug) {
  1758. debug("VFS: key down: " + keyCode);
  1759. }
  1760. if (waitingForFilteringResponse
  1761. && navigationKeyCodes.contains(keyCode)) {
  1762. /*
  1763. * Keyboard navigation events should not be handled while we are
  1764. * waiting for a response. This avoids flickering, disappearing
  1765. * items, wrongly interpreted responses and more.
  1766. */
  1767. if (enableDebug) {
  1768. debug("Ignoring " + keyCode
  1769. + " because we are waiting for a filtering response");
  1770. }
  1771. DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
  1772. event.stopPropagation();
  1773. return;
  1774. }
  1775. if (suggestionPopup.isAttached()) {
  1776. if (enableDebug) {
  1777. debug("Keycode " + keyCode + " target is popup");
  1778. }
  1779. popupKeyDown(event);
  1780. } else {
  1781. if (enableDebug) {
  1782. debug("Keycode " + keyCode + " target is text field");
  1783. }
  1784. inputFieldKeyDown(event);
  1785. }
  1786. }
  1787. }
  1788. private void debug(String string) {
  1789. if (enableDebug) {
  1790. VConsole.error(string);
  1791. }
  1792. }
  1793. /**
  1794. * Triggered when a key is pressed in the text box
  1795. *
  1796. * @param event
  1797. * The KeyDownEvent
  1798. */
  1799. private void inputFieldKeyDown(KeyDownEvent event) {
  1800. if (enableDebug) {
  1801. debug("VFS: inputFieldKeyDown(" + event.getNativeKeyCode() + ")");
  1802. }
  1803. switch (event.getNativeKeyCode()) {
  1804. case KeyCodes.KEY_DOWN:
  1805. case KeyCodes.KEY_UP:
  1806. case KeyCodes.KEY_PAGEDOWN:
  1807. case KeyCodes.KEY_PAGEUP:
  1808. // open popup as from gadget
  1809. filterOptions(-1, "");
  1810. lastFilter = "";
  1811. tb.selectAll();
  1812. break;
  1813. case KeyCodes.KEY_ENTER:
  1814. /*
  1815. * This only handles the case when new items is allowed, a text is
  1816. * entered, the popup opener button is clicked to close the popup
  1817. * and enter is then pressed (see #7560).
  1818. */
  1819. if (!allowNewItem) {
  1820. return;
  1821. }
  1822. if (currentSuggestion != null && tb.getText()
  1823. .equals(currentSuggestion.getReplacementString())) {
  1824. // Retain behavior from #6686 by returning without stopping
  1825. // propagation if there's nothing to do
  1826. return;
  1827. }
  1828. suggestionPopup.menu.doSelectedItemAction();
  1829. event.stopPropagation();
  1830. break;
  1831. }
  1832. }
  1833. /**
  1834. * Triggered when a key was pressed in the suggestion popup.
  1835. *
  1836. * @param event
  1837. * The KeyDownEvent of the key
  1838. */
  1839. private void popupKeyDown(KeyDownEvent event) {
  1840. if (enableDebug) {
  1841. debug("VFS: popupKeyDown(" + event.getNativeKeyCode() + ")");
  1842. }
  1843. // Propagation of handled events is stopped so other handlers such as
  1844. // shortcut key handlers do not also handle the same events.
  1845. switch (event.getNativeKeyCode()) {
  1846. case KeyCodes.KEY_DOWN:
  1847. suggestionPopup.selectNextItem();
  1848. DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
  1849. event.stopPropagation();
  1850. break;
  1851. case KeyCodes.KEY_UP:
  1852. suggestionPopup.selectPrevItem();
  1853. DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
  1854. event.stopPropagation();
  1855. break;
  1856. case KeyCodes.KEY_PAGEDOWN:
  1857. selectNextPage();
  1858. event.stopPropagation();
  1859. break;
  1860. case KeyCodes.KEY_PAGEUP:
  1861. selectPrevPage();
  1862. event.stopPropagation();
  1863. break;
  1864. case KeyCodes.KEY_ESCAPE:
  1865. reset();
  1866. DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
  1867. event.stopPropagation();
  1868. break;
  1869. case KeyCodes.KEY_TAB:
  1870. case KeyCodes.KEY_ENTER:
  1871. if (!allowNewItem) {
  1872. int selected = suggestionPopup.menu.getSelectedIndex();
  1873. if (selected != -1) {
  1874. onSuggestionSelected(currentSuggestions.get(selected));
  1875. } else {
  1876. // The way VFilterSelect is done, it handles enter and tab
  1877. // in exactly the same way so we close the popup in both
  1878. // cases even though we could leave it open when pressing
  1879. // enter
  1880. suggestionPopup.hide();
  1881. }
  1882. } else {
  1883. // Handle addition of new items.
  1884. suggestionPopup.menu.doSelectedItemAction();
  1885. }
  1886. event.stopPropagation();
  1887. break;
  1888. }
  1889. }
  1890. /*
  1891. * Show the prev page.
  1892. */
  1893. private void selectPrevPage() {
  1894. if (currentPage > 0) {
  1895. filterOptions(currentPage - 1, lastFilter);
  1896. selectPopupItemWhenResponseIsReceived = Select.LAST;
  1897. }
  1898. }
  1899. /*
  1900. * Show the next page.
  1901. */
  1902. private void selectNextPage() {
  1903. if (hasNextPage()) {
  1904. filterOptions(currentPage + 1, lastFilter);
  1905. selectPopupItemWhenResponseIsReceived = Select.FIRST;
  1906. }
  1907. }
  1908. /**
  1909. * Triggered when a key was depressed
  1910. *
  1911. * @param event
  1912. * The KeyUpEvent of the key depressed
  1913. */
  1914. @Override
  1915. public void onKeyUp(KeyUpEvent event) {
  1916. if (enableDebug) {
  1917. debug("VFS: onKeyUp(" + event.getNativeKeyCode() + ")");
  1918. }
  1919. if (enabled && !readonly) {
  1920. switch (event.getNativeKeyCode()) {
  1921. case KeyCodes.KEY_ENTER:
  1922. case KeyCodes.KEY_TAB:
  1923. case KeyCodes.KEY_SHIFT:
  1924. case KeyCodes.KEY_CTRL:
  1925. case KeyCodes.KEY_ALT:
  1926. case KeyCodes.KEY_DOWN:
  1927. case KeyCodes.KEY_UP:
  1928. case KeyCodes.KEY_PAGEDOWN:
  1929. case KeyCodes.KEY_PAGEUP:
  1930. case KeyCodes.KEY_ESCAPE:
  1931. // NOP
  1932. break;
  1933. default:
  1934. if (textInputEnabled) {
  1935. // when filtering, we always want to see the results on the
  1936. // first page first.
  1937. filterOptions(0);
  1938. }
  1939. break;
  1940. }
  1941. }
  1942. }
  1943. /**
  1944. * Resets the Select to its initial state
  1945. */
  1946. private void reset() {
  1947. debug("VFS: reset()");
  1948. if (currentSuggestion != null) {
  1949. String text = currentSuggestion.getReplacementString();
  1950. setPromptingOff(text);
  1951. setSelectedItemIcon(currentSuggestion.getIconUri());
  1952. selectedOptionKey = currentSuggestion.key;
  1953. } else {
  1954. if (focused || readonly || !enabled) {
  1955. setPromptingOff("");
  1956. } else {
  1957. setPromptingOn();
  1958. }
  1959. setSelectedItemIcon(null);
  1960. selectedOptionKey = null;
  1961. }
  1962. lastFilter = "";
  1963. suggestionPopup.hide();
  1964. }
  1965. /**
  1966. * Listener for popupopener
  1967. */
  1968. @Override
  1969. public void onClick(ClickEvent event) {
  1970. debug("VFS: onClick()");
  1971. if (textInputEnabled && event.getNativeEvent().getEventTarget()
  1972. .cast() == tb.getElement()) {
  1973. // Don't process clicks on the text field if text input is enabled
  1974. return;
  1975. }
  1976. if (enabled && !readonly) {
  1977. // ask suggestionPopup if it was just closed, we are using GWT
  1978. // Popup's auto close feature
  1979. if (!suggestionPopup.isJustClosed()) {
  1980. // If a focus event is not going to be sent, send the options
  1981. // request immediately; otherwise queue in the same burst as the
  1982. // focus event. Fixes #8321.
  1983. boolean immediate = focused
  1984. || !client.hasEventListeners(this, EventId.FOCUS);
  1985. filterOptions(-1, "", immediate);
  1986. popupOpenerClicked = true;
  1987. lastFilter = "";
  1988. }
  1989. DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
  1990. focus();
  1991. tb.selectAll();
  1992. }
  1993. }
  1994. /**
  1995. * Update minimum width for FilterSelect textarea based on input prompt and
  1996. * suggestions.
  1997. * <p>
  1998. * For internal use only. May be removed or replaced in the future.
  1999. */
  2000. public void updateSuggestionPopupMinWidth() {
  2001. // used only to calculate minimum width
  2002. String captions = WidgetUtil.escapeHTML(inputPrompt);
  2003. for (FilterSelectSuggestion suggestion : currentSuggestions) {
  2004. // Collect captions so we can calculate minimum width for
  2005. // textarea
  2006. if (!captions.isEmpty()) {
  2007. captions += "|";
  2008. }
  2009. captions += WidgetUtil
  2010. .escapeHTML(suggestion.getReplacementString());
  2011. }
  2012. // Calculate minimum textarea width
  2013. suggestionPopupMinWidth = minWidth(captions);
  2014. }
  2015. /**
  2016. * Calculate minimum width for FilterSelect textarea.
  2017. * <p>
  2018. * For internal use only. May be removed or replaced in the future.
  2019. */
  2020. public native int minWidth(String captions)
  2021. /*-{
  2022. if(!captions || captions.length <= 0)
  2023. return 0;
  2024. captions = captions.split("|");
  2025. var d = $wnd.document.createElement("div");
  2026. var html = "";
  2027. for(var i=0; i < captions.length; i++) {
  2028. html += "<div>" + captions[i] + "</div>";
  2029. // TODO apply same CSS classname as in suggestionmenu
  2030. }
  2031. d.style.position = "absolute";
  2032. d.style.top = "0";
  2033. d.style.left = "0";
  2034. d.style.visibility = "hidden";
  2035. d.innerHTML = html;
  2036. $wnd.document.body.appendChild(d);
  2037. var w = d.offsetWidth;
  2038. $wnd.document.body.removeChild(d);
  2039. return w;
  2040. }-*/;
  2041. /**
  2042. * A flag which prevents a focus event from taking place
  2043. */
  2044. boolean iePreventNextFocus = false;
  2045. /*
  2046. * (non-Javadoc)
  2047. *
  2048. * @see
  2049. * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
  2050. * .dom.client.FocusEvent)
  2051. */
  2052. @Override
  2053. public void onFocus(FocusEvent event) {
  2054. debug("VFS: onFocus()");
  2055. /*
  2056. * When we disable a blur event in ie we need to refocus the textfield.
  2057. * This will cause a focus event we do not want to process, so in that
  2058. * case we just ignore it.
  2059. */
  2060. if (BrowserInfo.get().isIE() && iePreventNextFocus) {
  2061. iePreventNextFocus = false;
  2062. return;
  2063. }
  2064. focused = true;
  2065. if (prompting && !readonly) {
  2066. setPromptingOff("");
  2067. }
  2068. addStyleDependentName("focus");
  2069. if (client.hasEventListeners(this, EventId.FOCUS)) {
  2070. client.updateVariable(paintableId, EventId.FOCUS, "", true);
  2071. afterUpdateClientVariables();
  2072. }
  2073. ComponentConnector connector = ConnectorMap.get(client)
  2074. .getConnector(this);
  2075. client.getVTooltip()
  2076. .showAssistive(connector.getTooltipInfo(getElement()));
  2077. }
  2078. /**
  2079. * A flag which cancels the blur event and sets the focus back to the
  2080. * textfield if the Browser is IE
  2081. */
  2082. boolean preventNextBlurEventInIE = false;
  2083. private String explicitSelectedCaption;
  2084. /*
  2085. * (non-Javadoc)
  2086. *
  2087. * @see
  2088. * com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event
  2089. * .dom.client.BlurEvent)
  2090. */
  2091. @Override
  2092. public void onBlur(BlurEvent event) {
  2093. debug("VFS: onBlur()");
  2094. if (BrowserInfo.get().isIE() && preventNextBlurEventInIE) {
  2095. /*
  2096. * Clicking in the suggestion popup or on the popup button in IE
  2097. * causes a blur event to be sent for the field. In other browsers
  2098. * this is prevented by canceling/preventing default behavior for
  2099. * the focus event, in IE we handle it here by refocusing the text
  2100. * field and ignoring the resulting focus event for the textfield
  2101. * (in onFocus).
  2102. */
  2103. preventNextBlurEventInIE = false;
  2104. Element focusedElement = WidgetUtil.getFocusedElement();
  2105. if (getElement().isOrHasChild(focusedElement) || suggestionPopup
  2106. .getElement().isOrHasChild(focusedElement)) {
  2107. // IF the suggestion popup or another part of the VFilterSelect
  2108. // was focused, move the focus back to the textfield and prevent
  2109. // the triggered focus event (in onFocus).
  2110. iePreventNextFocus = true;
  2111. tb.setFocus(true);
  2112. return;
  2113. }
  2114. }
  2115. focused = false;
  2116. if (!readonly) {
  2117. if (textInputEnabled && allowNewItem) {
  2118. suggestionPopup.menu.doSelectedItemAction();
  2119. }
  2120. if (selectedOptionKey == null) {
  2121. if (explicitSelectedCaption != null) {
  2122. setPromptingOff(explicitSelectedCaption);
  2123. } else {
  2124. setPromptingOn();
  2125. }
  2126. } else if (currentSuggestion != null) {
  2127. setPromptingOff(currentSuggestion.caption);
  2128. setSelectedItemIcon(currentSuggestion.getIconUri());
  2129. }
  2130. }
  2131. removeStyleDependentName("focus");
  2132. if (client.hasEventListeners(this, EventId.BLUR)) {
  2133. client.updateVariable(paintableId, EventId.BLUR, "", true);
  2134. afterUpdateClientVariables();
  2135. }
  2136. }
  2137. /*
  2138. * (non-Javadoc)
  2139. *
  2140. * @see com.vaadin.client.Focusable#focus()
  2141. */
  2142. @Override
  2143. public void focus() {
  2144. debug("VFS: focus()");
  2145. focused = true;
  2146. if (prompting && !readonly) {
  2147. setPromptingOff("");
  2148. }
  2149. tb.setFocus(true);
  2150. }
  2151. /**
  2152. * Calculates the width of the select if the select has undefined width.
  2153. * Should be called when the width changes or when the icon changes.
  2154. * <p>
  2155. * For internal use only. May be removed or replaced in the future.
  2156. */
  2157. public void updateRootWidth() {
  2158. ComponentConnector paintable = ConnectorMap.get(client)
  2159. .getConnector(this);
  2160. if (paintable.isUndefinedWidth()) {
  2161. /*
  2162. * When the select has a undefined with we need to check that we are
  2163. * only setting the text box width relative to the first page width
  2164. * of the items. If this is not done the text box width will change
  2165. * when the popup is used to view longer items than the text box is
  2166. * wide.
  2167. */
  2168. int w = WidgetUtil.getRequiredWidth(this);
  2169. if ((!initDone || currentPage + 1 < 0)
  2170. && suggestionPopupMinWidth > w) {
  2171. /*
  2172. * We want to compensate for the paddings just to preserve the
  2173. * exact size as in Vaadin 6.x, but we get here before
  2174. * MeasuredSize has been initialized.
  2175. * Util.measureHorizontalPaddingAndBorder does not work with
  2176. * border-box, so we must do this the hard way.
  2177. */
  2178. Style style = getElement().getStyle();
  2179. String originalPadding = style.getPadding();
  2180. String originalBorder = style.getBorderWidth();
  2181. style.setPaddingLeft(0, Unit.PX);
  2182. style.setBorderWidth(0, Unit.PX);
  2183. style.setProperty("padding", originalPadding);
  2184. style.setProperty("borderWidth", originalBorder);
  2185. // Use util.getRequiredWidth instead of getOffsetWidth here
  2186. int iconWidth = selectedItemIcon == null ? 0
  2187. : WidgetUtil.getRequiredWidth(selectedItemIcon);
  2188. int buttonWidth = popupOpener == null ? 0
  2189. : WidgetUtil.getRequiredWidth(popupOpener);
  2190. /*
  2191. * Instead of setting the width of the wrapper, set the width of
  2192. * the combobox. Subtract the width of the icon and the
  2193. * popupopener
  2194. */
  2195. tb.setWidth((suggestionPopupMinWidth - iconWidth - buttonWidth)
  2196. + "px");
  2197. }
  2198. /*
  2199. * Lock the textbox width to its current value if it's not already
  2200. * locked
  2201. */
  2202. if (!tb.getElement().getStyle().getWidth().endsWith("px")) {
  2203. int iconWidth = selectedItemIcon == null ? 0
  2204. : selectedItemIcon.getOffsetWidth();
  2205. tb.setWidth((tb.getOffsetWidth() - iconWidth) + "px");
  2206. }
  2207. }
  2208. }
  2209. /**
  2210. * Get the width of the select in pixels where the text area and icon has
  2211. * been included.
  2212. *
  2213. * @return The width in pixels
  2214. */
  2215. private int getMainWidth() {
  2216. return getOffsetWidth();
  2217. }
  2218. @Override
  2219. public void setWidth(String width) {
  2220. super.setWidth(width);
  2221. if (!width.isEmpty()) {
  2222. tb.setWidth("100%");
  2223. }
  2224. }
  2225. /**
  2226. * Handles special behavior of the mouse down event
  2227. *
  2228. * @param event
  2229. */
  2230. private void handleMouseDownEvent(Event event) {
  2231. /*
  2232. * Prevent the keyboard focus from leaving the textfield by preventing
  2233. * the default behavior of the browser. Fixes #4285.
  2234. */
  2235. if (event.getTypeInt() == Event.ONMOUSEDOWN) {
  2236. event.preventDefault();
  2237. event.stopPropagation();
  2238. /*
  2239. * In IE the above wont work, the blur event will still trigger. So,
  2240. * we set a flag here to prevent the next blur event from happening.
  2241. * This is not needed if do not already have focus, in that case
  2242. * there will not be any blur event and we should not cancel the
  2243. * next blur.
  2244. */
  2245. if (BrowserInfo.get().isIE() && focused) {
  2246. preventNextBlurEventInIE = true;
  2247. debug("VFS: Going to prevent next blur event on IE");
  2248. }
  2249. }
  2250. }
  2251. @Override
  2252. protected void onDetach() {
  2253. super.onDetach();
  2254. suggestionPopup.hide();
  2255. }
  2256. @Override
  2257. public com.google.gwt.user.client.Element getSubPartElement(
  2258. String subPart) {
  2259. String[] parts = subPart.split("/");
  2260. if ("textbox".equals(parts[0])) {
  2261. return tb.getElement();
  2262. } else if ("button".equals(parts[0])) {
  2263. return popupOpener.getElement();
  2264. } else if ("popup".equals(parts[0]) && suggestionPopup.isAttached()) {
  2265. if (parts.length == 2) {
  2266. return suggestionPopup.menu.getSubPartElement(parts[1]);
  2267. }
  2268. return suggestionPopup.getElement();
  2269. }
  2270. return null;
  2271. }
  2272. @Override
  2273. public String getSubPartName(
  2274. com.google.gwt.user.client.Element subElement) {
  2275. if (tb.getElement().isOrHasChild(subElement)) {
  2276. return "textbox";
  2277. } else if (popupOpener.getElement().isOrHasChild(subElement)) {
  2278. return "button";
  2279. } else if (suggestionPopup.getElement().isOrHasChild(subElement)) {
  2280. return "popup";
  2281. }
  2282. return null;
  2283. }
  2284. @Override
  2285. public void setAriaRequired(boolean required) {
  2286. AriaHelper.handleInputRequired(tb, required);
  2287. }
  2288. @Override
  2289. public void setAriaInvalid(boolean invalid) {
  2290. AriaHelper.handleInputInvalid(tb, invalid);
  2291. }
  2292. @Override
  2293. public void bindAriaCaption(
  2294. com.google.gwt.user.client.Element captionElement) {
  2295. AriaHelper.bindCaption(tb, captionElement);
  2296. }
  2297. /*
  2298. * Anything that should be set after the client updates the server.
  2299. */
  2300. private void afterUpdateClientVariables() {
  2301. // We need this here to be consistent with the all the calls.
  2302. // Then set your specific selection type only after
  2303. // client.updateVariable() method call.
  2304. selectPopupItemWhenResponseIsReceived = Select.NONE;
  2305. }
  2306. @Override
  2307. public boolean isWorkPending() {
  2308. return waitingForFilteringResponse
  2309. || suggestionPopup.lazyPageScroller.isRunning();
  2310. }
  2311. /**
  2312. * Sets the caption of selected item, if "scroll to page" is disabled. This
  2313. * method is meant for internal use and may change in future versions.
  2314. *
  2315. * @since 7.7
  2316. * @param selectedCaption
  2317. * the caption of selected item
  2318. */
  2319. public void setSelectedCaption(String selectedCaption) {
  2320. explicitSelectedCaption = selectedCaption;
  2321. if (selectedCaption != null) {
  2322. setPromptingOff(selectedCaption);
  2323. }
  2324. }
  2325. /**
  2326. * This method is meant for internal use and may change in future versions.
  2327. *
  2328. * @since 7.7
  2329. * @return the caption of selected item, if "scroll to page" is disabled
  2330. */
  2331. public String getSelectedCaption() {
  2332. return explicitSelectedCaption;
  2333. }
  2334. }