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

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