123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727 |
- /*
- * Copyright 2000-2018 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
- package com.vaadin.v7.client.ui;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Locale;
- import java.util.Set;
- import java.util.logging.Logger;
-
- import com.google.gwt.animation.client.AnimationScheduler;
- import com.google.gwt.animation.client.AnimationScheduler.AnimationCallback;
- import com.google.gwt.aria.client.Roles;
- import com.google.gwt.core.client.JavaScriptObject;
- import com.google.gwt.core.client.Scheduler;
- import com.google.gwt.core.client.Scheduler.ScheduledCommand;
- import com.google.gwt.dom.client.Document;
- import com.google.gwt.dom.client.Element;
- import com.google.gwt.dom.client.NativeEvent;
- import com.google.gwt.dom.client.Node;
- import com.google.gwt.dom.client.Style;
- import com.google.gwt.dom.client.Style.Display;
- import com.google.gwt.dom.client.Style.Unit;
- import com.google.gwt.dom.client.Style.Visibility;
- import com.google.gwt.event.dom.client.BlurEvent;
- import com.google.gwt.event.dom.client.BlurHandler;
- import com.google.gwt.event.dom.client.ClickEvent;
- import com.google.gwt.event.dom.client.ClickHandler;
- import com.google.gwt.event.dom.client.FocusEvent;
- import com.google.gwt.event.dom.client.FocusHandler;
- import com.google.gwt.event.dom.client.KeyCodes;
- import com.google.gwt.event.dom.client.KeyDownEvent;
- import com.google.gwt.event.dom.client.KeyDownHandler;
- import com.google.gwt.event.dom.client.KeyUpEvent;
- import com.google.gwt.event.dom.client.KeyUpHandler;
- import com.google.gwt.event.dom.client.LoadEvent;
- import com.google.gwt.event.dom.client.LoadHandler;
- import com.google.gwt.event.logical.shared.CloseEvent;
- import com.google.gwt.event.logical.shared.CloseHandler;
- import com.google.gwt.i18n.client.HasDirection.Direction;
- import com.google.gwt.user.client.Command;
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Event;
- import com.google.gwt.user.client.Event.NativePreviewEvent;
- import com.google.gwt.user.client.Timer;
- import com.google.gwt.user.client.Window;
- import com.google.gwt.user.client.ui.Composite;
- import com.google.gwt.user.client.ui.FlowPanel;
- import com.google.gwt.user.client.ui.HTML;
- import com.google.gwt.user.client.ui.PopupPanel;
- import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;
- import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
- import com.google.gwt.user.client.ui.TextBox;
- import com.google.gwt.user.client.ui.Widget;
- import com.vaadin.client.ApplicationConnection;
- import com.vaadin.client.BrowserInfo;
- import com.vaadin.client.ComponentConnector;
- import com.vaadin.client.ComputedStyle;
- import com.vaadin.client.ConnectorMap;
- import com.vaadin.client.DeferredWorker;
- import com.vaadin.client.Focusable;
- import com.vaadin.client.UIDL;
- import com.vaadin.client.WidgetUtil;
- import com.vaadin.client.ui.Field;
- import com.vaadin.client.ui.Icon;
- import com.vaadin.client.ui.SubPartAware;
- import com.vaadin.client.ui.VLazyExecutor;
- import com.vaadin.client.ui.VOverlay;
- import com.vaadin.client.ui.aria.AriaHelper;
- import com.vaadin.client.ui.aria.HandlesAriaCaption;
- import com.vaadin.client.ui.aria.HandlesAriaInvalid;
- import com.vaadin.client.ui.aria.HandlesAriaRequired;
- import com.vaadin.client.ui.menubar.MenuBar;
- import com.vaadin.client.ui.menubar.MenuItem;
- import com.vaadin.shared.AbstractComponentState;
- import com.vaadin.shared.EventId;
- import com.vaadin.shared.ui.ComponentStateUtil;
- import com.vaadin.shared.util.SharedUtil;
- import com.vaadin.v7.shared.ui.combobox.FilteringMode;
-
- /**
- * Client side implementation of the Select component.
- *
- * TODO needs major refactoring (to be extensible etc)
- */
- @SuppressWarnings("deprecation")
- public class VFilterSelect extends Composite
- implements Field, KeyDownHandler, KeyUpHandler, ClickHandler,
- FocusHandler, BlurHandler, Focusable, SubPartAware, HandlesAriaCaption,
- HandlesAriaInvalid, HandlesAriaRequired, DeferredWorker {
-
- /**
- * Represents a suggestion in the suggestion popup box.
- */
- public class FilterSelectSuggestion implements Suggestion, Command {
-
- private final String key;
- private final String caption;
- private String untranslatedIconUri;
- private String style;
-
- /**
- * Constructor.
- *
- * @param uidl
- * The UIDL received from the server
- */
- public FilterSelectSuggestion(UIDL uidl) {
- key = uidl.getStringAttribute("key");
- caption = uidl.getStringAttribute("caption");
- style = uidl.getStringAttribute("style");
-
- if (uidl.hasAttribute("icon")) {
- untranslatedIconUri = uidl.getStringAttribute("icon");
- }
- }
-
- /**
- * Gets the visible row in the popup as a HTML string. The string
- * contains an image tag with the rows icon (if an icon has been
- * specified) and the caption of the item
- */
-
- @Override
- public String getDisplayString() {
- final StringBuilder sb = new StringBuilder();
- final Icon icon = client
- .getIcon(client.translateVaadinUri(untranslatedIconUri));
- if (icon != null) {
- sb.append(icon.getElement().getString());
- }
- String content;
- if ("".equals(caption)) {
- // Ensure that empty options use the same height as other
- // options and are not collapsed (#7506)
- content = " ";
- } else {
- content = WidgetUtil.escapeHTML(caption);
- }
- sb.append("<span>" + content + "</span>");
- return sb.toString();
- }
-
- /**
- * Get a string that represents this item. This is used in the text box.
- */
-
- @Override
- public String getReplacementString() {
- return caption;
- }
-
- /**
- * Get the option key which represents the item on the server side.
- *
- * @return The key of the item
- */
- public String getOptionKey() {
- return key;
- }
-
- /**
- * Get the URI of the icon. Used when constructing the displayed option.
- *
- * @return
- */
- public String getIconUri() {
- return client.translateVaadinUri(untranslatedIconUri);
- }
-
- /**
- * Gets the style set for this suggestion item. Styles are typically set
- * by a server-side {@link com.vaadin.ui.ComboBox.ItemStyleGenerator}.
- * The returned style is prefixed by <code>v-filterselect-item-</code>.
- *
- * @since 7.5.6
- * @return the style name to use, or <code>null</code> to not apply any
- * custom style.
- */
- public String getStyle() {
- return style;
- }
-
- /**
- * Executes a selection of this item.
- */
-
- @Override
- public void execute() {
- onSuggestionSelected(this);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof FilterSelectSuggestion)) {
- return false;
- }
- FilterSelectSuggestion other = (FilterSelectSuggestion) obj;
- if ((key == null && other.key != null)
- || (key != null && !key.equals(other.key))) {
- return false;
- }
- if ((caption == null && other.caption != null)
- || (caption != null && !caption.equals(other.caption))) {
- return false;
- }
- if (!SharedUtil.equals(untranslatedIconUri,
- other.untranslatedIconUri)) {
- return false;
- }
- if (!SharedUtil.equals(style, other.style)) {
- return false;
- }
- return true;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + VFilterSelect.this.hashCode();
- result = prime * result + ((key == null) ? 0 : key.hashCode());
- result = prime * result
- + ((caption == null) ? 0 : caption.hashCode());
- result = prime * result + ((untranslatedIconUri == null) ? 0
- : untranslatedIconUri.hashCode());
- result = prime * result + ((style == null) ? 0 : style.hashCode());
- return result;
- }
- }
-
- /** An inner class that handles all logic related to mouse wheel. */
- private class MouseWheeler extends JsniMousewheelHandler {
-
- public MouseWheeler() {
- super(VFilterSelect.this);
- }
-
- @Override
- protected native JavaScriptObject createMousewheelListenerFunction(
- Widget widget)
- /*-{
- return $entry(function(e) {
- var deltaX = e.deltaX ? e.deltaX : -0.5*e.wheelDeltaX;
- var deltaY = e.deltaY ? e.deltaY : -0.5*e.wheelDeltaY;
-
- // IE8 has only delta y
- if (isNaN(deltaY)) {
- deltaY = -0.5*e.wheelDelta;
- }
-
- @com.vaadin.v7.client.ui.VFilterSelect.JsniUtil::moveScrollFromEvent(*)(widget, deltaX, deltaY, e, e.deltaMode);
- });
- }-*/;
-
- }
-
- /**
- * A utility class that contains utility methods that are usually called
- * from JSNI.
- * <p>
- * The methods are moved in this class to minimize the amount of JSNI code
- * as much as feasible.
- */
- static class JsniUtil {
- private static final int DOM_DELTA_PIXEL = 0;
- private static final int DOM_DELTA_LINE = 1;
- private static final int DOM_DELTA_PAGE = 2;
-
- // Rough estimation of item height
- private static final int SCROLL_UNIT_PX = 25;
-
- private static double deltaSum = 0;
-
- public static void moveScrollFromEvent(final Widget widget,
- final double deltaX, final double deltaY,
- final NativeEvent event, final int deltaMode) {
-
- if (!Double.isNaN(deltaY)) {
- VFilterSelect filterSelect = (VFilterSelect) widget;
-
- switch (deltaMode) {
- case DOM_DELTA_LINE:
- if (deltaY >= 0) {
- filterSelect.suggestionPopup.selectNextItem();
- } else {
- filterSelect.suggestionPopup.selectPrevItem();
- }
- break;
- case DOM_DELTA_PAGE:
- if (deltaY >= 0) {
- filterSelect.selectNextPage();
- } else {
- filterSelect.selectPrevPage();
- }
- break;
- case DOM_DELTA_PIXEL:
- default:
- // Accumulate dampened deltas
- deltaSum += Math.pow(Math.abs(deltaY), 0.7)
- * Math.signum(deltaY);
-
- // "Scroll" if change exceeds item height
- while (Math.abs(deltaSum) >= SCROLL_UNIT_PX) {
- if (!filterSelect.waitingForFilteringResponse) {
- // Move selection if page flip is not in progress
- if (deltaSum < 0) {
- filterSelect.suggestionPopup.selectPrevItem();
- } else {
- filterSelect.suggestionPopup.selectNextItem();
- }
- }
- deltaSum -= SCROLL_UNIT_PX * Math.signum(deltaSum);
- }
- break;
- }
- }
- }
- }
-
- /**
- * Represents the popup box with the selection options. Wraps a suggestion
- * menu.
- */
- public class SuggestionPopup extends VOverlay
- implements PositionCallback, CloseHandler<PopupPanel> {
-
- private static final int Z_INDEX = 30000;
-
- /** For internal use only. May be removed or replaced in the future. */
- public final SuggestionMenu menu;
-
- private final Element up = DOM.createDiv();
- private final Element down = DOM.createDiv();
- private final Element status = DOM.createDiv();
-
- private boolean isPagingEnabled = true;
-
- private long lastAutoClosed;
-
- private int popupOuterPadding = -1;
-
- private int topPosition;
- private int leftPosition;
-
- private final MouseWheeler mouseWheeler = new MouseWheeler();
- private boolean scrollPending = false;
-
- /**
- * Default constructor
- */
- SuggestionPopup() {
- super(true, false);
- debug("VFS.SP: constructor()");
- setOwner(VFilterSelect.this);
- menu = new SuggestionMenu();
- setWidget(menu);
-
- getElement().getStyle().setZIndex(Z_INDEX);
-
- final Element root = getContainerElement();
-
- up.setInnerHTML("<span>Prev</span>");
- DOM.sinkEvents(up, Event.ONCLICK);
-
- down.setInnerHTML("<span>Next</span>");
- DOM.sinkEvents(down, Event.ONCLICK);
-
- root.insertFirst(up);
- root.appendChild(down);
- root.appendChild(status);
-
- DOM.sinkEvents(root, Event.ONMOUSEDOWN | Event.ONMOUSEWHEEL);
- addCloseHandler(this);
-
- Roles.getListRole().set(getElement());
-
- setPreviewingAllNativeEvents(true);
- }
-
- @Override
- protected void onLoad() {
- super.onLoad();
-
- // Register mousewheel listener on paged select
- if (pageLength > 0) {
- mouseWheeler.attachMousewheelListener(getElement());
- }
- }
-
- @Override
- protected void onUnload() {
- mouseWheeler.detachMousewheelListener(getElement());
- super.onUnload();
- }
-
- /**
- * Shows the popup where the user can see the filtered options.
- *
- * @param currentSuggestions
- * The filtered suggestions
- * @param currentPage
- * The current page number
- * @param totalSuggestions
- * The total amount of suggestions
- */
- public void showSuggestions(
- final Collection<FilterSelectSuggestion> currentSuggestions,
- final int currentPage, final int totalSuggestions) {
-
- debug("VFS.SP: showSuggestions(" + currentSuggestions + ", "
- + currentPage + ", " + totalSuggestions + ")");
-
- /*
- * We need to defer the opening of the popup so that the parent DOM
- * has stabilized so we can calculate an absolute top and left
- * correctly. This issue manifests when a Combobox is placed in
- * another popupView which also needs to calculate the absoluteTop()
- * to position itself. #9768
- *
- * After deferring the showSuggestions method, a problem with
- * navigating in the combo box occurs. Because of that the method
- * navigateItemAfterPageChange in ComboBoxConnector class, which
- * navigates to the exact item after page was changed also was
- * marked as deferred. #11333
- */
- final SuggestionPopup popup = this;
- Scheduler.get().scheduleDeferred(new ScheduledCommand() {
- @Override
- public void execute() {
- // Add TT anchor point
- getElement().setId("VAADIN_COMBOBOX_OPTIONLIST");
-
- menu.setSuggestions(currentSuggestions);
- leftPosition = getDesiredLeftPosition();
-
- topPosition = getDesiredTopPosition();
-
- setPopupPosition(leftPosition, topPosition);
-
- int nullOffset = (nullSelectionAllowed
- && "".equals(lastFilter) ? 1 : 0);
- boolean firstPage = (currentPage == 0);
- final int first = currentPage * pageLength + 1
- - (firstPage ? 0 : nullOffset);
- final int last = first + currentSuggestions.size() - 1
- - (firstPage && "".equals(lastFilter) ? nullOffset
- : 0);
- final int matches = totalSuggestions - nullOffset;
- if (last > 0) {
- // nullsel not counted, as requested by user
- status.setInnerText((matches == 0 ? 0 : first) + "-"
- + last + "/" + matches);
- } else {
- status.setInnerText("");
- }
- // We don't need to show arrows or statusbar if there is
- // only one page
- if (totalSuggestions <= pageLength || pageLength == 0) {
- setPagingEnabled(false);
- } else {
- setPagingEnabled(true);
- }
- setPrevButtonActive(first > 1);
- setNextButtonActive(last < matches);
-
- // clear previously fixed width
- menu.setWidth("");
- menu.getElement().getFirstChildElement().getStyle()
- .clearWidth();
-
- setPopupPositionAndShow(popup);
- // Fix for #14173
- // IE9 and IE10 have a bug, when resize an a element with
- // box-shadow.
- // IE9 and IE10 need explicit update to remove extra
- // box-shadows
- if (BrowserInfo.get().isIE9()
- || BrowserInfo.get().isIE10()) {
- forceReflow();
- }
- }
- });
- }
-
- private native int toInt32(double val)
- /*-{
- return val | 0;
- }-*/;
-
- private int getDesiredTopPosition() {
- return toInt32(WidgetUtil.getBoundingClientRect(tb.getElement())
- .getBottom()) + Window.getScrollTop();
- }
-
- private int getDesiredLeftPosition() {
- return toInt32(WidgetUtil
- .getBoundingClientRect(VFilterSelect.this.getElement())
- .getLeft());
- }
-
- /**
- * Should the next page button be visible to the user?
- *
- * @param active
- */
- private void setNextButtonActive(boolean active) {
- if (enableDebug) {
- debug("VFS.SP: setNextButtonActive(" + active + ")");
- }
- if (active) {
- DOM.sinkEvents(down, Event.ONCLICK);
- down.setClassName(
- VFilterSelect.this.getStylePrimaryName() + "-nextpage");
- } else {
- DOM.sinkEvents(down, 0);
- down.setClassName(VFilterSelect.this.getStylePrimaryName()
- + "-nextpage-off");
- }
- }
-
- /**
- * Should the previous page button be visible to the user
- *
- * @param active
- */
- private void setPrevButtonActive(boolean active) {
- if (enableDebug) {
- debug("VFS.SP: setPrevButtonActive(" + active + ")");
- }
-
- if (active) {
- DOM.sinkEvents(up, Event.ONCLICK);
- up.setClassName(
- VFilterSelect.this.getStylePrimaryName() + "-prevpage");
- } else {
- DOM.sinkEvents(up, 0);
- up.setClassName(VFilterSelect.this.getStylePrimaryName()
- + "-prevpage-off");
- }
-
- }
-
- /**
- * Selects the next item in the filtered selections.
- */
- public void selectNextItem() {
- debug("VFS.SP: selectNextItem()");
-
- final int index = menu.getSelectedIndex() + 1;
- if (menu.getItems().size() > index) {
- selectItem(menu.getItems().get(index));
-
- } else {
- selectNextPage();
- }
- }
-
- /**
- * Selects the previous item in the filtered selections.
- */
- public void selectPrevItem() {
- debug("VFS.SP: selectPrevItem()");
-
- final int index = menu.getSelectedIndex() - 1;
- if (index > -1) {
- selectItem(menu.getItems().get(index));
-
- } else if (index == -1) {
- selectPrevPage();
-
- } else {
- if (!menu.getItems().isEmpty()) {
- selectLastItem();
- }
- }
- }
-
- /**
- * Select the first item of the suggestions list popup.
- *
- * @since 7.2.6
- */
- public void selectFirstItem() {
- debug("VFS.SP: selectFirstItem()");
- selectItem(menu.getFirstItem());
- }
-
- /**
- * Select the last item of the suggestions list popup.
- *
- * @since 7.2.6
- */
- public void selectLastItem() {
- debug("VFS.SP: selectLastItem()");
- selectItem(menu.getLastItem());
- }
-
- /*
- * Sets the selected item in the popup menu.
- */
- private void selectItem(final MenuItem newSelectedItem) {
- menu.selectItem(newSelectedItem);
-
- // Set the icon.
- FilterSelectSuggestion suggestion = (FilterSelectSuggestion) newSelectedItem
- .getCommand();
- setSelectedItemIcon(suggestion.getIconUri());
-
- // Set the text.
- setText(suggestion.getReplacementString());
-
- }
-
- /*
- * Using a timer to scroll up or down the pages so when we receive lots
- * of consecutive mouse wheel events the pages does not flicker.
- */
- private LazyPageScroller lazyPageScroller = new LazyPageScroller();
-
- private class LazyPageScroller extends Timer {
- private int pagesToScroll = 0;
-
- @Override
- public void run() {
- debug("VFS.SP.LPS: run()");
- if (pagesToScroll != 0) {
- if (!waitingForFilteringResponse) {
- /*
- * Avoid scrolling while we are waiting for a response
- * because otherwise the waiting flag will be reset in
- * the first response and the second response will be
- * ignored, causing an empty popup...
- *
- * As long as the scrolling delay is suitable
- * double/triple clicks will work by scrolling two or
- * three pages at a time and this should not be a
- * problem.
- */
- filterOptions(currentPage + pagesToScroll, lastFilter);
- }
- pagesToScroll = 0;
- }
- }
-
- public void scrollUp() {
- debug("VFS.SP.LPS: scrollUp()");
- if (pageLength > 0 && currentPage + pagesToScroll > 0) {
- pagesToScroll--;
- cancel();
- schedule(200);
- }
- }
-
- public void scrollDown() {
- debug("VFS.SP.LPS: scrollDown()");
- if (pageLength > 0
- && totalMatches > (currentPage + pagesToScroll + 1)
- * pageLength) {
- pagesToScroll++;
- cancel();
- schedule(200);
- }
- }
- }
-
- @Override
- public void onBrowserEvent(Event event) {
- debug("VFS.SP: onBrowserEvent()");
-
- if (event.getTypeInt() == Event.ONCLICK) {
- final Element target = DOM.eventGetTarget(event);
- if (target == up || target == DOM.getChild(up, 0)) {
- lazyPageScroller.scrollUp();
- } else if (target == down || target == DOM.getChild(down, 0)) {
- lazyPageScroller.scrollDown();
- }
-
- }
-
- /*
- * Prevent the keyboard focus from leaving the textfield by
- * preventing the default behavior of the browser. Fixes #4285.
- */
- handleMouseDownEvent(event);
- }
-
- @Override
- protected void onPreviewNativeEvent(NativePreviewEvent event) {
- // Check all events outside the combobox to see if they scroll the
- // page. We cannot use e.g. Window.addScrollListener() because the
- // scrolled element can be at any level on the page.
-
- // Normally this is only called when the popup is showing, but make
- // sure we don't accidentally process all events when not showing.
- if (!scrollPending && isShowing() && !DOM.isOrHasChild(
- SuggestionPopup.this.getElement(),
- Element.as(event.getNativeEvent().getEventTarget()))) {
- if (getDesiredLeftPosition() != leftPosition
- || getDesiredTopPosition() != topPosition) {
- updatePopupPositionOnScroll();
- }
- }
-
- super.onPreviewNativeEvent(event);
- }
-
- /**
- * Make the popup follow the position of the ComboBox when the page is
- * scrolled.
- */
- private void updatePopupPositionOnScroll() {
- if (!scrollPending) {
- AnimationScheduler.get()
- .requestAnimationFrame(new AnimationCallback() {
- public void execute(double timestamp) {
- if (isShowing()) {
- leftPosition = getDesiredLeftPosition();
- topPosition = getDesiredTopPosition();
- setPopupPosition(leftPosition, topPosition);
- }
- scrollPending = false;
- }
- });
- scrollPending = true;
- }
- }
-
- /**
- * Should paging be enabled. If paging is enabled then only a certain
- * amount of items are visible at a time and a scrollbar or buttons are
- * visible to change page. If paging is turned of then all options are
- * rendered into the popup menu.
- *
- * @param paging
- * Should the paging be turned on?
- */
- public void setPagingEnabled(boolean paging) {
- debug("VFS.SP: setPagingEnabled(" + paging + ")");
- if (isPagingEnabled == paging) {
- return;
- }
- if (paging) {
- down.getStyle().clearDisplay();
- up.getStyle().clearDisplay();
- status.getStyle().clearDisplay();
- } else {
- down.getStyle().setDisplay(Display.NONE);
- up.getStyle().setDisplay(Display.NONE);
- status.getStyle().setDisplay(Display.NONE);
- }
- isPagingEnabled = paging;
- }
-
- @Override
- public void setPosition(int offsetWidth, int offsetHeight) {
- debug("VFS.SP: setPosition(" + offsetWidth + ", " + offsetHeight
- + ")");
-
- int top = topPosition;
- int left = getPopupLeft();
-
- // reset menu size and retrieve its "natural" size
- menu.setHeight("");
- if (currentPage > 0 && !hasNextPage()) {
- // fix height to avoid height change when getting to last page
- menu.fixHeightTo(pageLength);
- }
-
- final int desiredHeight = offsetHeight = getOffsetHeight();
- final int desiredWidth = getMainWidth();
-
- debug("VFS.SP: desired[" + desiredWidth + ", " + desiredHeight
- + "]");
-
- Element menuFirstChild = menu.getElement().getFirstChildElement();
- int naturalMenuWidth;
- if (BrowserInfo.get().isIE()
- && BrowserInfo.get().getBrowserMajorVersion() < 10) {
- // On IE 8 & 9 visibility is set to hidden and measuring
- // elements while they are hidden yields incorrect results
- String before = menu.getElement().getParentElement().getStyle()
- .getVisibility();
- menu.getElement().getParentElement().getStyle()
- .setVisibility(Visibility.VISIBLE);
- naturalMenuWidth = WidgetUtil.getRequiredWidth(menuFirstChild);
- menu.getElement().getParentElement().getStyle()
- .setProperty("visibility", before);
- } else {
- naturalMenuWidth = WidgetUtil.getRequiredWidth(menuFirstChild);
- }
-
- if (popupOuterPadding == -1) {
- popupOuterPadding = WidgetUtil
- .measureHorizontalPaddingAndBorder(menu.getElement(), 2)
- + WidgetUtil.measureHorizontalPaddingAndBorder(
- suggestionPopup.getElement(), 0);
- }
-
- updateMenuWidth(desiredWidth, naturalMenuWidth);
-
- if (BrowserInfo.get().isIE()
- && BrowserInfo.get().getBrowserMajorVersion() < 11) {
- // Must take margin,border,padding manually into account for
- // menu element as we measure the element child and set width to
- // the element parent
-
- double naturalMenuOuterWidth;
- if (BrowserInfo.get().getBrowserMajorVersion() < 10) {
- // On IE 8 & 9 visibility is set to hidden and measuring
- // elements while they are hidden yields incorrect results
- String before = menu.getElement().getParentElement()
- .getStyle().getVisibility();
- menu.getElement().getParentElement().getStyle()
- .setVisibility(Visibility.VISIBLE);
- naturalMenuOuterWidth = WidgetUtil
- .getRequiredWidthDouble(menuFirstChild)
- + getMarginBorderPaddingWidth(menu.getElement());
- menu.getElement().getParentElement().getStyle()
- .setProperty("visibility", before);
- } else {
- naturalMenuOuterWidth = WidgetUtil
- .getRequiredWidthDouble(menuFirstChild)
- + getMarginBorderPaddingWidth(menu.getElement());
- }
-
- /*
- * IE requires us to specify the width for the container
- * element. Otherwise it will be 100% wide
- */
- double rootWidth = Math.max(desiredWidth - popupOuterPadding,
- naturalMenuOuterWidth);
- getContainerElement().getStyle().setWidth(rootWidth, Unit.PX);
- }
-
- final int textInputHeight = VFilterSelect.this.getOffsetHeight();
- final int textInputTopOnPage = tb.getAbsoluteTop();
- final int viewportOffset = Document.get().getScrollTop();
- final int textInputTopInViewport = textInputTopOnPage
- - viewportOffset;
- final int textInputBottomInViewport = textInputTopInViewport
- + textInputHeight;
-
- final int spaceAboveInViewport = textInputTopInViewport;
- final int spaceBelowInViewport = Window.getClientHeight()
- - textInputBottomInViewport;
-
- if (spaceBelowInViewport < offsetHeight
- && spaceBelowInViewport < spaceAboveInViewport) {
- // popup on top of input instead
- if (offsetHeight > spaceAboveInViewport) {
- // Shrink popup height to fit above
- offsetHeight = spaceAboveInViewport;
- }
- top = textInputTopOnPage - offsetHeight;
- } else {
- // Show below, position calculated in showSuggestions for some
- // strange reason
- top = topPosition;
- offsetHeight = Math.min(offsetHeight, spaceBelowInViewport);
- }
-
- // fetch real width (mac FF bugs here due GWT popups overflow:auto )
- offsetWidth = menuFirstChild.getOffsetWidth();
-
- if (offsetHeight < desiredHeight) {
- int menuHeight = offsetHeight;
- if (isPagingEnabled) {
- menuHeight -= up.getOffsetHeight() + down.getOffsetHeight()
- + status.getOffsetHeight();
- } else {
- final ComputedStyle s = new ComputedStyle(
- menu.getElement());
- menuHeight -= s.getIntProperty("marginBottom")
- + s.getIntProperty("marginTop");
- }
-
- // If the available page height is really tiny then this will be
- // negative and an exception will be thrown on setHeight.
- int menuElementHeight = menu.getItemOffsetHeight();
- if (menuHeight < menuElementHeight) {
- menuHeight = menuElementHeight;
- }
-
- menu.setHeight(menuHeight + "px");
-
- if (suggestionPopupWidth == null) {
- final int naturalMenuWidthPlusScrollBar = naturalMenuWidth
- + WidgetUtil.getNativeScrollbarSize();
- if (offsetWidth < naturalMenuWidthPlusScrollBar) {
- menu.setWidth(naturalMenuWidthPlusScrollBar + "px");
- }
- }
- }
-
- if (offsetWidth + left > Window.getClientWidth()) {
- left = VFilterSelect.this.getAbsoluteLeft()
- + VFilterSelect.this.getOffsetWidth() - offsetWidth;
- if (left < 0) {
- left = 0;
- menu.setWidth(Window.getClientWidth() + "px");
-
- }
- if (BrowserInfo.get().isIE()
- && BrowserInfo.get().getBrowserMajorVersion() < 10) {
- setTdWidth(menu.getElement(), Window.getClientWidth() - 8);
- }
- }
-
- setPopupPosition(left, top);
- menu.scrollSelectionIntoView();
- }
-
- /**
- * Adds in-line CSS rules to the DOM according to the
- * suggestionPopupWidth field
- *
- * @param desiredWidth
- * @param naturalMenuWidth
- */
- private void updateMenuWidth(final int desiredWidth,
- int naturalMenuWidth) {
- /**
- * Three different width modes for the suggestion pop-up:
- *
- * 1. Legacy "null"-mode: width is determined by the longest item
- * caption for each page while still maintaining minimum width of
- * (desiredWidth - popupOuterPadding)
- *
- * 2. relative to the component itself
- *
- * 3. fixed width
- */
- String width = "auto";
- if (suggestionPopupWidth == null) {
- if (naturalMenuWidth < desiredWidth) {
- naturalMenuWidth = desiredWidth - popupOuterPadding;
- width = (desiredWidth - popupOuterPadding) + "px";
- }
- } else if (isrelativeUnits(suggestionPopupWidth)) {
- float mainComponentWidth = desiredWidth - popupOuterPadding;
- // convert percentage value to fraction
- int widthInPx = Math.round(
- mainComponentWidth * asFraction(suggestionPopupWidth));
- width = widthInPx + "px";
- } else {
- // use as fixed width CSS definition
- width = WidgetUtil.escapeAttribute(suggestionPopupWidth);
- }
- menu.setWidth(width);
-
- // IE8 or 9?
- if (BrowserInfo.get().isIE()
- && BrowserInfo.get().getBrowserMajorVersion() < 10) {
- // using legacy mode?
- if (suggestionPopupWidth == null) {
- // set the TD widths manually as these browsers do not
- // respect display: block; width:100% rules
- setTdWidth(menu.getElement(), naturalMenuWidth);
- } else {
- int compensation = WidgetUtil
- .measureHorizontalPaddingAndBorder(
- menu.getElement(), 4);
- setTdWidth(menu.getElement(),
- menu.getOffsetWidth() - compensation);
- }
-
- }
- }
-
- /**
- * Descends to child elements until finds TD elements and sets their
- * width in pixels. Can be used to workaround IE8 & 9 TD element
- * display: block issues
- *
- * @param parent
- * @param width
- */
- private void setTdWidth(Node parent, int width) {
- for (int i = 0; i < parent.getChildCount(); i++) {
- Node child = parent.getChild(i);
- if ("td".equals(child.getNodeName().toLowerCase(Locale.ROOT))) {
- ((Element) child).getStyle().setWidth(width, Unit.PX);
- } else {
- setTdWidth(child, width);
- }
-
- }
- }
-
- /**
- * Returns the percentage value as a fraction, e.g. 42% -> 0.42
- *
- * @param percentage
- */
- private float asFraction(String percentage) {
- String trimmed = percentage.trim();
- String withoutPercentSign = trimmed.substring(0,
- trimmed.length() - 1);
- float asFraction = Float.parseFloat(withoutPercentSign) / 100;
- return asFraction;
- }
-
- /**
- * @since 7.7
- * @param suggestionPopupWidth
- * @return
- */
- private boolean isrelativeUnits(String suggestionPopupWidth) {
- return suggestionPopupWidth.trim().endsWith("%");
- }
-
- /**
- * Was the popup just closed?
- *
- * @return true if popup was just closed
- */
- public boolean isJustClosed() {
- debug("VFS.SP: justClosed()");
- final long now = (new Date()).getTime();
- return (lastAutoClosed > 0 && (now - lastAutoClosed) < 200);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.logical.shared.CloseHandler#onClose(com.google
- * .gwt.event.logical.shared.CloseEvent)
- */
-
- @Override
- public void onClose(CloseEvent<PopupPanel> event) {
- if (enableDebug) {
- debug("VFS.SP: onClose(" + event.isAutoClosed() + ")");
- }
- if (event.isAutoClosed()) {
- lastAutoClosed = (new Date()).getTime();
- }
- }
-
- /**
- * Updates style names in suggestion popup to help theme building.
- *
- * @param uidl
- * UIDL for the whole combo box
- * @param componentState
- * shared state of the combo box
- */
- public void updateStyleNames(UIDL uidl,
- AbstractComponentState componentState) {
- debug("VFS.SP: updateStyleNames()");
- setStyleName(
- VFilterSelect.this.getStylePrimaryName() + "-suggestpopup");
- menu.setStyleName(
- VFilterSelect.this.getStylePrimaryName() + "-suggestmenu");
- status.setClassName(
- VFilterSelect.this.getStylePrimaryName() + "-status");
- if (ComponentStateUtil.hasStyles(componentState)) {
- for (String style : componentState.styles) {
- if (!"".equals(style)) {
- addStyleDependentName(style);
- }
- }
- }
- }
-
- }
-
- /**
- * The menu where the suggestions are rendered.
- */
- public class SuggestionMenu extends MenuBar
- implements SubPartAware, LoadHandler {
-
- private VLazyExecutor delayedImageLoadExecutioner = new VLazyExecutor(
- 100, new ScheduledCommand() {
-
- @Override
- public void execute() {
- debug("VFS.SM: delayedImageLoadExecutioner()");
- if (suggestionPopup.isVisible()
- && suggestionPopup.isAttached()) {
- setWidth("");
- getElement().getFirstChildElement().getStyle()
- .clearWidth();
- suggestionPopup
- .setPopupPositionAndShow(suggestionPopup);
- }
-
- }
- });
-
- /**
- * Default constructor
- */
- SuggestionMenu() {
- super(true);
- debug("VFS.SM: constructor()");
- addDomHandler(this, LoadEvent.getType());
-
- setScrollEnabled(true);
- }
-
- /**
- * Fixes menus height to use same space as full page would use. Needed
- * to avoid height changes when quickly "scrolling" to last page.
- */
- public void fixHeightTo(int pageItemsCount) {
- setHeight(getPreferredHeight(pageItemsCount));
- }
-
- /*
- * Gets the preferred height of the menu including pageItemsCount items.
- */
- String getPreferredHeight(int pageItemsCount) {
- if (!currentSuggestions.isEmpty()) {
- final int pixels = (getPreferredHeight()
- / currentSuggestions.size()) * pageItemsCount;
- return pixels + "px";
- } else {
- return "";
- }
- }
-
- /**
- * Sets the suggestions rendered in the menu.
- *
- * @param suggestions
- * The suggestions to be rendered in the menu
- */
- public void setSuggestions(
- Collection<FilterSelectSuggestion> suggestions) {
- if (enableDebug) {
- debug("VFS.SM: setSuggestions(" + suggestions + ")");
- }
-
- clearItems();
- boolean isFirstIteration = true;
- for (final FilterSelectSuggestion s : suggestions) {
- final MenuItem mi = new MenuItem(s.getDisplayString(), true, s);
- String style = s.getStyle();
- if (style != null) {
- mi.addStyleName("v-filterselect-item-" + style);
- }
- Roles.getListitemRole().set(mi.getElement());
-
- WidgetUtil.sinkOnloadForImages(mi.getElement());
-
- this.addItem(mi);
-
- // By default, first item on the list is always highlighted,
- // unless adding new items is allowed.
- if (isFirstIteration && !allowNewItem) {
- selectItem(mi);
- }
-
- // If the filter matches the current selection, highlight that
- // instead of the first item.
- if (tb.getText().equals(s.getReplacementString())
- && s == currentSuggestion) {
- selectItem(mi);
- }
-
- isFirstIteration = false;
- }
- if (suggestionPopupWidth != null && BrowserInfo.get().isIE()
- && BrowserInfo.get().getBrowserMajorVersion() < 10) {
- // set TD width to a low value so that they won't mandate the
- // suggestion pop-up width
- suggestionPopup.setTdWidth(suggestionPopup.menu.getElement(),
- 1);
- }
- }
-
- /**
- * Send the current selection to the server. Triggered when a selection
- * is made or on a blur event.
- */
- public void doSelectedItemAction() {
- debug("VFS.SM: doSelectedItemAction()");
- // do not send a value change event if null was and stays selected
- final String enteredItemValue = tb.getText();
- if (nullSelectionAllowed && "".equals(enteredItemValue)
- && selectedOptionKey != null
- && !"".equals(selectedOptionKey)) {
- if (nullSelectItem) {
- reset();
- return;
- }
- // null is not visible on pages != 0, and not visible when
- // filtering: handle separately
- client.updateVariable(paintableId, "filter", "", false);
- client.updateVariable(paintableId, "page", 0, false);
- client.updateVariable(paintableId, "selected", new String[] {},
- immediate);
- afterUpdateClientVariables();
-
- suggestionPopup.hide();
- return;
- }
-
- updateSelectionWhenReponseIsReceived = waitingForFilteringResponse;
- if (!waitingForFilteringResponse) {
- doPostFilterSelectedItemAction();
- }
- }
-
- /**
- * Triggered after a selection has been made.
- */
- public void doPostFilterSelectedItemAction() {
- debug("VFS.SM: doPostFilterSelectedItemAction()");
- final MenuItem item = getSelectedItem();
- final String enteredItemValue = tb.getText();
-
- updateSelectionWhenReponseIsReceived = false;
-
- // check for exact match in menu
- int p = getItems().size();
- if (p > 0) {
- for (int i = 0; i < p; i++) {
- final MenuItem potentialExactMatch = getItems().get(i);
- if (potentialExactMatch.getText()
- .equals(enteredItemValue)) {
- selectItem(potentialExactMatch);
- // do not send a value change event if null was and
- // stays selected
- if (!"".equals(enteredItemValue)
- || (selectedOptionKey != null
- && !"".equals(selectedOptionKey))) {
- doItemAction(potentialExactMatch, true);
- }
- suggestionPopup.hide();
- return;
- }
- }
- }
- if (allowNewItem) {
-
- if (!prompting && !enteredItemValue.equals(lastNewItemString)) {
- /*
- * Store last sent new item string to avoid double sends
- */
- lastNewItemString = enteredItemValue;
- client.updateVariable(paintableId, "newitem",
- enteredItemValue, immediate);
- afterUpdateClientVariables();
- }
- } else if (item != null && !"".equals(lastFilter)
- && (filteringmode == FilteringMode.CONTAINS
- ? item.getText().toLowerCase(Locale.ROOT).contains(
- lastFilter.toLowerCase(Locale.ROOT))
- : item.getText().toLowerCase(Locale.ROOT)
- .startsWith(lastFilter
- .toLowerCase(Locale.ROOT)))) {
- doItemAction(item, true);
- } else {
- // currentSuggestion has key="" for nullselection
- if (currentSuggestion != null
- && !currentSuggestion.key.equals("")) {
- // An item (not null) selected
- String text = currentSuggestion.getReplacementString();
- setText(text);
- selectedOptionKey = currentSuggestion.key;
- } else {
- // Null selected
- setText("");
- selectedOptionKey = null;
- }
- }
- suggestionPopup.hide();
- }
-
- private static final String SUBPART_PREFIX = "item";
-
- @Override
- public com.google.gwt.user.client.Element getSubPartElement(
- String subPart) {
- int index = Integer
- .parseInt(subPart.substring(SUBPART_PREFIX.length()));
-
- MenuItem item = getItems().get(index);
-
- return item.getElement();
- }
-
- @Override
- public String getSubPartName(
- com.google.gwt.user.client.Element subElement) {
- if (!getElement().isOrHasChild(subElement)) {
- return null;
- }
-
- Element menuItemRoot = subElement;
- while (menuItemRoot != null
- && !menuItemRoot.getTagName().equalsIgnoreCase("td")) {
- menuItemRoot = menuItemRoot.getParentElement().cast();
- }
- // "menuItemRoot" is now the root of the menu item
-
- final int itemCount = getItems().size();
- for (int i = 0; i < itemCount; i++) {
- if (getItems().get(i).getElement() == menuItemRoot) {
- String name = SUBPART_PREFIX + i;
- return name;
- }
- }
- return null;
- }
-
- @Override
- public void onLoad(LoadEvent event) {
- debug("VFS.SM: onLoad()");
- // Handle icon onload events to ensure shadow is resized
- // correctly
- delayedImageLoadExecutioner.trigger();
-
- }
-
- /**
- * @deprecated use {@link SuggestionPopup#selectFirstItem()} instead.
- */
- @Deprecated
- public void selectFirstItem() {
- debug("VFS.SM: selectFirstItem()");
- MenuItem firstItem = getItems().get(0);
- selectItem(firstItem);
- }
-
- /**
- * @deprecated use {@link SuggestionPopup#selectLastItem()} instead.
- */
- @Deprecated
- public void selectLastItem() {
- debug("VFS.SM: selectLastItem()");
- List<MenuItem> items = getItems();
- MenuItem lastItem = items.get(items.size() - 1);
- selectItem(lastItem);
- }
-
- /*
- * Gets the height of one menu item.
- */
- int getItemOffsetHeight() {
- List<MenuItem> items = getItems();
- return items != null && !items.isEmpty()
- ? items.get(0).getOffsetHeight()
- : 0;
- }
-
- /*
- * Gets the width of one menu item.
- */
- int getItemOffsetWidth() {
- List<MenuItem> items = getItems();
- return items != null && !items.isEmpty()
- ? items.get(0).getOffsetWidth()
- : 0;
- }
-
- /**
- * Returns true if the scroll is active on the menu element or if the
- * menu currently displays the last page with less items then the
- * maximum visibility (in which case the scroll is not active, but the
- * scroll is active for any other page in general).
- *
- * @since 7.2.6
- */
- @Override
- public boolean isScrollActive() {
- String height = getElement().getStyle().getHeight();
- String preferredHeight = getPreferredHeight(pageLength);
-
- return !(height == null || height.isEmpty()
- || height.equals(preferredHeight));
- }
-
- }
-
- /**
- * TextBox variant used as input element for filter selects, which prevents
- * selecting text when disabled.
- *
- * @since 7.1.5
- */
- public class FilterSelectTextBox extends TextBox {
-
- /**
- * Creates a new filter select text box.
- *
- * @since 7.6.4
- */
- public FilterSelectTextBox() {
- /*-
- * Stop the browser from showing its own suggestion popup.
- *
- * Using an invalid value instead of "off" as suggested by
- * https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
- *
- * Leaving the non-standard Safari options autocapitalize and
- * autocorrect untouched since those do not interfere in the same
- * way, and they might be useful in a combo box where new items are
- * allowed.
- */
- getElement().setAttribute("autocomplete", Math.random() + "");
- }
-
- /**
- * Overridden to avoid selecting text when text input is disabled.
- */
- @Override
- public void setSelectionRange(int pos, int length) {
- if (textInputEnabled) {
- /*
- * set selection range with a backwards direction: anchor at the
- * back, focus at the front. This means that items that are too
- * long to display will display from the start and not the end
- * even on Firefox.
- *
- * We need the JSNI function to set selection range so that we
- * can use the optional direction attribute to set the anchor to
- * the end and the focus to the start. This makes Firefox work
- * the same way as other browsers (#13477)
- */
- WidgetUtil.setSelectionRange(getElement(), pos, length,
- "backward");
-
- } else {
- /*
- * Setting the selectionrange for an uneditable textbox leads to
- * unwanted behavior when the width of the textbox is narrower
- * than the width of the entry: the end of the entry is shown
- * instead of the beginning. (see #13477)
- *
- * To avoid this, we set the caret to the beginning of the line.
- */
-
- super.setSelectionRange(0, 0);
- }
- }
-
- }
-
- @Deprecated
- public static final FilteringMode FILTERINGMODE_OFF = FilteringMode.OFF;
- @Deprecated
- public static final FilteringMode FILTERINGMODE_STARTSWITH = FilteringMode.STARTSWITH;
- @Deprecated
- public static final FilteringMode FILTERINGMODE_CONTAINS = FilteringMode.CONTAINS;
-
- public static final String CLASSNAME = "v-filterselect";
- private static final String STYLE_NO_INPUT = "no-input";
-
- /** For internal use only. May be removed or replaced in the future. */
- public int pageLength = 10;
-
- private boolean enableDebug = false;
-
- private final FlowPanel panel = new FlowPanel();
-
- /**
- * The text box where the filter is written
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public final TextBox tb;
-
- /** For internal use only. May be removed or replaced in the future. */
- public final SuggestionPopup suggestionPopup;
-
- /**
- * Used when measuring the width of the popup
- */
- private final HTML popupOpener = new HTML("") {
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt
- * .user.client.Event)
- */
-
- @Override
- public void onBrowserEvent(Event event) {
- super.onBrowserEvent(event);
-
- /*
- * Prevent the keyboard focus from leaving the textfield by
- * preventing the default behavior of the browser. Fixes #4285.
- */
- handleMouseDownEvent(event);
- }
- };
-
- private class IconWidget extends Widget {
- private Icon icon;
-
- IconWidget(Icon icon) {
- this.icon = icon;
- setElement(icon.getElement());
- addDomHandler(VFilterSelect.this, ClickEvent.getType());
- }
- }
-
- private IconWidget selectedItemIcon;
-
- /** For internal use only. May be removed or replaced in the future. */
- public ApplicationConnection client;
-
- /** For internal use only. May be removed or replaced in the future. */
- public String paintableId;
-
- /** For internal use only. May be removed or replaced in the future. */
- public int currentPage;
-
- /**
- * A collection of available suggestions (options) as received from the
- * server.
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public final List<FilterSelectSuggestion> currentSuggestions = new ArrayList<FilterSelectSuggestion>();
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean immediate;
-
- /** For internal use only. May be removed or replaced in the future. */
- public String selectedOptionKey;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean waitingForFilteringResponse = false;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean updateSelectionWhenReponseIsReceived = false;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean initDone = false;
-
- /** For internal use only. May be removed or replaced in the future. */
- public String lastFilter = "";
-
- /** For internal use only. May be removed or replaced in the future. */
- public enum Select {
- NONE, FIRST, LAST
- }
-
- /** For internal use only. May be removed or replaced in the future. */
- public Select selectPopupItemWhenResponseIsReceived = Select.NONE;
-
- /**
- * The current suggestion selected from the dropdown. This is one of the
- * values in currentSuggestions except when filtering, in this case
- * currentSuggestion might not be in currentSuggestions.
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public FilterSelectSuggestion currentSuggestion;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean allowNewItem;
-
- /** For internal use only. May be removed or replaced in the future. */
- public int totalMatches;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean nullSelectionAllowed;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean nullSelectItem;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean enabled;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean readonly;
-
- /** For internal use only. May be removed or replaced in the future. */
- public FilteringMode filteringmode = FilteringMode.OFF;
-
- // shown in unfocused empty field, disappears on focus (e.g "Search here")
- private static final String CLASSNAME_PROMPT = "prompt";
-
- /** For internal use only. May be removed or replaced in the future. */
- public String inputPrompt = "";
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean prompting = false;
-
- /**
- * Set true when popupopened has been clicked. Cleared on each UIDL-update.
- * This handles the special case where are not filtering yet and the
- * selected value has changed on the server-side. See #2119
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public boolean popupOpenerClicked;
-
- /** For internal use only. May be removed or replaced in the future. */
- public int suggestionPopupMinWidth = 0;
-
- public String suggestionPopupWidth = null;
-
- private int popupWidth = -1;
- /**
- * Stores the last new item string to avoid double submissions. Cleared on
- * uidl updates.
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public String lastNewItemString;
-
- /** For internal use only. May be removed or replaced in the future. */
- public boolean focused = false;
-
- /**
- * If set to false, the component should not allow entering text to the
- * field even for filtering.
- */
- private boolean textInputEnabled = true;
-
- /**
- * Default constructor.
- */
- public VFilterSelect() {
- tb = createTextBox();
- suggestionPopup = createSuggestionPopup();
-
- popupOpener.sinkEvents(Event.ONMOUSEDOWN);
- Roles.getButtonRole().setAriaHiddenState(popupOpener.getElement(),
- true);
- Roles.getButtonRole().set(popupOpener.getElement());
-
- panel.add(tb);
- panel.add(popupOpener);
- initWidget(panel);
- Roles.getComboboxRole().set(panel.getElement());
-
- tb.addKeyDownHandler(this);
- tb.addKeyUpHandler(this);
-
- tb.addFocusHandler(this);
- tb.addBlurHandler(this);
- tb.addClickHandler(this);
-
- popupOpener.addClickHandler(this);
-
- setStyleName(CLASSNAME);
-
- sinkEvents(Event.ONPASTE);
- }
-
- private static double getMarginBorderPaddingWidth(Element element) {
- final ComputedStyle s = new ComputedStyle(element);
- return s.getMarginWidth() + s.getBorderWidth() + s.getPaddingWidth();
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.user.client.ui.Composite#onBrowserEvent(com.google.gwt
- * .user.client.Event)
- */
- @Override
- public void onBrowserEvent(Event event) {
- super.onBrowserEvent(event);
-
- if (event.getTypeInt() == Event.ONPASTE) {
- if (textInputEnabled) {
- Scheduler.get().scheduleDeferred(new ScheduledCommand() {
-
- @Override
- public void execute() {
- filterOptions(currentPage);
- }
- });
- }
- }
- }
-
- /**
- * This method will create the TextBox used by the VFilterSelect instance.
- * It is invoked during the Constructor and should only be overridden if a
- * custom TextBox shall be used. The overriding method cannot use any
- * instance variables.
- *
- * @since 7.1.5
- * @return TextBox instance used by this VFilterSelect
- */
- protected TextBox createTextBox() {
- return new FilterSelectTextBox();
- }
-
- /**
- * This method will create the SuggestionPopup used by the VFilterSelect
- * instance. It is invoked during the Constructor and should only be
- * overridden if a custom SuggestionPopup shall be used. The overriding
- * method cannot use any instance variables.
- *
- * @since 7.1.5
- * @return SuggestionPopup instance used by this VFilterSelect
- */
- protected SuggestionPopup createSuggestionPopup() {
- return new SuggestionPopup();
- }
-
- @Override
- public void setStyleName(String style) {
- super.setStyleName(style);
- updateStyleNames();
- }
-
- @Override
- public void setStylePrimaryName(String style) {
- super.setStylePrimaryName(style);
- updateStyleNames();
- }
-
- protected void updateStyleNames() {
- tb.setStyleName(getStylePrimaryName() + "-input");
- popupOpener.setStyleName(getStylePrimaryName() + "-button");
- suggestionPopup.setStyleName(getStylePrimaryName() + "-suggestpopup");
- }
-
- /**
- * Does the Select have more pages?
- *
- * @return true if a next page exists, else false if the current page is the
- * last page
- */
- public boolean hasNextPage() {
- return pageLength > 0 && totalMatches > (currentPage + 1) * pageLength;
- }
-
- /**
- * Filters the options at a certain page. Uses the text box input as a
- * filter
- *
- * @param page
- * The page which items are to be filtered
- */
- public void filterOptions(int page) {
- filterOptions(page, tb.getText());
- }
-
- /**
- * Filters the options at certain page using the given filter.
- *
- * @param page
- * The page to filter
- * @param filter
- * The filter to apply to the components
- */
- public void filterOptions(int page, String filter) {
- filterOptions(page, filter, true);
- }
-
- /**
- * Filters the options at certain page using the given filter
- *
- * @param page
- * The page to filter
- * @param filter
- * The filter to apply to the options
- * @param immediate
- * Whether to send the options request immediately
- */
- private void filterOptions(int page, String filter, boolean immediate) {
- debug("VFS: filterOptions(" + page + ", " + filter + ", " + immediate
- + ")");
-
- if (filter.equals(lastFilter) && currentPage == page) {
- if (!suggestionPopup.isAttached()) {
- suggestionPopup.showSuggestions(currentSuggestions, currentPage,
- totalMatches);
- }
- return;
- }
- if (!filter.equals(lastFilter)) {
- // when filtering, let the server decide the page unless we've
- // set the filter to empty and explicitly said that we want to see
- // the results starting from page 0.
- if ("".equals(filter) && page != 0) {
- // let server decide
- page = -1;
- } else {
- page = 0;
- }
- }
-
- waitingForFilteringResponse = true;
- client.updateVariable(paintableId, "filter", filter, false);
- client.updateVariable(paintableId, "page", page, immediate);
- afterUpdateClientVariables();
-
- lastFilter = filter;
- currentPage = page;
- }
-
- /** For internal use only. May be removed or replaced in the future. */
- public void updateReadOnly() {
- debug("VFS: updateReadOnly()");
- tb.setReadOnly(readonly || !textInputEnabled);
- }
-
- public void setTextInputEnabled(boolean textInputEnabled) {
- debug("VFS: setTextInputEnabled()");
- // Always update styles as they might have been overwritten
- if (textInputEnabled) {
- removeStyleDependentName(STYLE_NO_INPUT);
- Roles.getTextboxRole().removeAriaReadonlyProperty(tb.getElement());
- } else {
- addStyleDependentName(STYLE_NO_INPUT);
- Roles.getTextboxRole().setAriaReadonlyProperty(tb.getElement(),
- true);
- }
-
- if (this.textInputEnabled == textInputEnabled) {
- return;
- }
-
- this.textInputEnabled = textInputEnabled;
- updateReadOnly();
- }
-
- /**
- * Sets the text in the text box.
- *
- * @param text
- * the text to set in the text box
- */
- public void setTextboxText(final String text) {
- if (enableDebug) {
- debug("VFS: setTextboxText(" + text + ")");
- }
- setText(text);
- }
-
- private void setText(final String text) {
- /**
- * To leave caret in the beginning of the line. SetSelectionRange
- * wouldn't work on IE (see #13477)
- */
- Direction previousDirection = tb.getDirection();
- tb.setDirection(Direction.RTL);
- tb.setText(text);
- tb.setDirection(previousDirection);
- }
-
- /**
- * Turns prompting on. When prompting is turned on a command prompt is shown
- * in the text box if nothing has been entered.
- */
- public void setPromptingOn() {
- debug("VFS: setPromptingOn()");
- if (!prompting) {
- prompting = true;
- addStyleDependentName(CLASSNAME_PROMPT);
- }
- setTextboxText(inputPrompt);
- }
-
- /**
- * Turns prompting off. When prompting is turned on a command prompt is
- * shown in the text box if nothing has been entered.
- * <p>
- * For internal use only. May be removed or replaced in the future.
- *
- * @param text
- * The text the text box should contain.
- */
- public void setPromptingOff(String text) {
- debug("VFS: setPromptingOff()");
- setTextboxText(text);
- if (prompting) {
- prompting = false;
- removeStyleDependentName(CLASSNAME_PROMPT);
- }
- }
-
- /**
- * Triggered when a suggestion is selected.
- *
- * @param suggestion
- * The suggestion that just got selected.
- */
- public void onSuggestionSelected(FilterSelectSuggestion suggestion) {
- if (enableDebug) {
- debug("VFS: onSuggestionSelected(" + suggestion.caption + ": "
- + suggestion.key + ")");
- }
- updateSelectionWhenReponseIsReceived = false;
-
- currentSuggestion = suggestion;
- String newKey;
- if (suggestion.key.equals("")) {
- // "nullselection"
- newKey = "";
- } else {
- // normal selection
- newKey = suggestion.getOptionKey();
- }
-
- String text = suggestion.getReplacementString();
- if ("".equals(newKey) && !focused) {
- setPromptingOn();
- } else {
- setPromptingOff(text);
- }
- setSelectedItemIcon(suggestion.getIconUri());
-
- if (!(newKey.equals(selectedOptionKey)
- || ("".equals(newKey) && selectedOptionKey == null))) {
- selectedOptionKey = newKey;
- client.updateVariable(paintableId, "selected",
- new String[] { selectedOptionKey }, immediate);
- afterUpdateClientVariables();
-
- // currentPage = -1; // forget the page
- }
-
- if (getSelectedCaption() != null && newKey.equals("")) {
- // In scrollToPage(false) mode selecting null seems to be broken
- // if current selection is not on first page. The above clause is so
- // hard to interpret that new clause added here :-(
- selectedOptionKey = newKey;
- explicitSelectedCaption = null;
- client.updateVariable(paintableId, "selected",
- new String[] { selectedOptionKey }, immediate);
- afterUpdateClientVariables();
- }
-
- suggestionPopup.hide();
- }
-
- /**
- * Sets the icon URI of the selected item. The icon is shown on the left
- * side of the item caption text. Set the URI to null to remove the icon.
- *
- * @param iconUri
- * The URI of the icon
- */
- public void setSelectedItemIcon(String iconUri) {
-
- if (iconUri == null || iconUri.isEmpty()) {
- if (selectedItemIcon != null) {
- panel.remove(selectedItemIcon);
- selectedItemIcon = null;
- afterSelectedItemIconChange();
- }
- } else {
- IconWidget newIcon = new IconWidget(client.getIcon(iconUri));
- if (iconEquals(newIcon, selectedItemIcon)) {
- /*
- * Do not update the icon if nothing has changed. Otherwise we
- * can cause problems such as not being able to click in the
- * icon to open the popup (blur might occur and call this
- * method, icon is replaced and the click event is not delivered
- * to the new icon)
- */
- return;
- }
-
- if (selectedItemIcon != null) {
- panel.remove(selectedItemIcon);
- }
-
- // Older IE versions don't scale icon correctly if DOM
- // contains height and width attributes.
- newIcon.getElement().removeAttribute("height");
- newIcon.getElement().removeAttribute("width");
- newIcon.addDomHandler(new LoadHandler() {
- @Override
- public void onLoad(LoadEvent event) {
- afterSelectedItemIconChange();
- }
- }, LoadEvent.getType());
- panel.insert(newIcon, 0);
- selectedItemIcon = newIcon;
- afterSelectedItemIconChange();
- }
- }
-
- /**
- * Checks if the icon widgets show the same icon.
- *
- * @param icon1
- * the first widget
- * @param icon2
- * the second widget
- * @return <code>true</code> if they show the same icon, <code>false</code>
- * otherwise
- */
- private static boolean iconEquals(IconWidget icon1, IconWidget icon2) {
- if (icon1 == null) {
- return icon2 == null;
- } else if (icon2 == null) {
- return false;
- } else {
- return icon1.icon.getUri().equals(icon2.icon.getUri());
- }
-
- }
-
- private void afterSelectedItemIconChange() {
- if (BrowserInfo.get().isWebkit() || BrowserInfo.get().isIE8()) {
- // Some browsers need a nudge to reposition the text field
- forceReflow();
- }
- updateRootWidth();
- if (selectedItemIcon != null) {
- updateSelectedIconPosition();
- }
- }
-
- private void forceReflow() {
- WidgetUtil.setStyleTemporarily(tb.getElement(), "zoom", "1");
- }
-
- /**
- * Positions the icon vertically in the middle. Should be called after the
- * icon has loaded
- */
- private void updateSelectedIconPosition() {
- // Position icon vertically to middle
- int availableHeight = 0;
- availableHeight = getOffsetHeight();
-
- int iconHeight = WidgetUtil.getRequiredHeight(selectedItemIcon);
- int marginTop = (availableHeight - iconHeight) / 2;
- selectedItemIcon.getElement().getStyle().setMarginTop(marginTop,
- Unit.PX);
- }
-
- private static Set<Integer> navigationKeyCodes = new HashSet<Integer>();
- static {
- navigationKeyCodes.add(KeyCodes.KEY_DOWN);
- navigationKeyCodes.add(KeyCodes.KEY_UP);
- navigationKeyCodes.add(KeyCodes.KEY_PAGEDOWN);
- navigationKeyCodes.add(KeyCodes.KEY_PAGEUP);
- navigationKeyCodes.add(KeyCodes.KEY_ENTER);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
- * .event.dom.client.KeyDownEvent)
- */
-
- @Override
- public void onKeyDown(KeyDownEvent event) {
- if (enabled && !readonly) {
- int keyCode = event.getNativeKeyCode();
-
- if (enableDebug) {
- debug("VFS: key down: " + keyCode);
- }
- if (waitingForFilteringResponse
- && navigationKeyCodes.contains(keyCode)) {
- /*
- * Keyboard navigation events should not be handled while we are
- * waiting for a response. This avoids flickering, disappearing
- * items, wrongly interpreted responses and more.
- */
- if (enableDebug) {
- debug("Ignoring " + keyCode
- + " because we are waiting for a filtering response");
- }
- DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
- event.stopPropagation();
- return;
- }
-
- if (suggestionPopup.isAttached()) {
- if (enableDebug) {
- debug("Keycode " + keyCode + " target is popup");
- }
- popupKeyDown(event);
- } else {
- if (enableDebug) {
- debug("Keycode " + keyCode + " target is text field");
- }
- inputFieldKeyDown(event);
- }
- }
- }
-
- private void debug(String string) {
- if (enableDebug) {
- getLogger().severe(string);
- }
- }
-
- /**
- * Triggered when a key is pressed in the text box
- *
- * @param event
- * The KeyDownEvent
- */
- private void inputFieldKeyDown(KeyDownEvent event) {
- if (enableDebug) {
- debug("VFS: inputFieldKeyDown(" + event.getNativeKeyCode() + ")");
- }
- switch (event.getNativeKeyCode()) {
- case KeyCodes.KEY_DOWN:
- case KeyCodes.KEY_UP:
- case KeyCodes.KEY_PAGEDOWN:
- case KeyCodes.KEY_PAGEUP:
- // open popup as from gadget
- filterOptions(-1, "");
- lastFilter = "";
- tb.selectAll();
- break;
- case KeyCodes.KEY_ENTER:
- /*
- * This only handles the case when new items is allowed, a text is
- * entered, the popup opener button is clicked to close the popup
- * and enter is then pressed (see #7560).
- */
- if (!allowNewItem) {
- return;
- }
-
- if (currentSuggestion != null && tb.getText()
- .equals(currentSuggestion.getReplacementString())) {
- // Retain behavior from #6686 by returning without stopping
- // propagation if there's nothing to do
- return;
- }
- suggestionPopup.menu.doSelectedItemAction();
-
- event.stopPropagation();
- break;
- }
-
- }
-
- /**
- * Triggered when a key was pressed in the suggestion popup.
- *
- * @param event
- * The KeyDownEvent of the key
- */
- private void popupKeyDown(KeyDownEvent event) {
- if (enableDebug) {
- debug("VFS: popupKeyDown(" + event.getNativeKeyCode() + ")");
- }
- // Propagation of handled events is stopped so other handlers such as
- // shortcut key handlers do not also handle the same events.
- switch (event.getNativeKeyCode()) {
- case KeyCodes.KEY_DOWN:
- suggestionPopup.selectNextItem();
-
- DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
- event.stopPropagation();
- break;
- case KeyCodes.KEY_UP:
- suggestionPopup.selectPrevItem();
-
- DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
- event.stopPropagation();
- break;
- case KeyCodes.KEY_PAGEDOWN:
- selectNextPage();
- event.stopPropagation();
- break;
- case KeyCodes.KEY_PAGEUP:
- selectPrevPage();
- event.stopPropagation();
- break;
- case KeyCodes.KEY_ESCAPE:
- reset();
- DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
- event.stopPropagation();
- break;
- case KeyCodes.KEY_TAB:
- case KeyCodes.KEY_ENTER:
-
- if (!allowNewItem) {
- int selected = suggestionPopup.menu.getSelectedIndex();
- if (selected != -1) {
- onSuggestionSelected(currentSuggestions.get(selected));
- } else {
- // The way VFilterSelect is done, it handles enter and tab
- // in exactly the same way so we close the popup in both
- // cases even though we could leave it open when pressing
- // enter
- suggestionPopup.hide();
- }
- } else {
- // Handle addition of new items.
- suggestionPopup.menu.doSelectedItemAction();
- }
-
- event.stopPropagation();
- break;
- }
-
- }
-
- /*
- * Show the prev page.
- */
- private void selectPrevPage() {
- if (currentPage > 0) {
- filterOptions(currentPage - 1, lastFilter);
- selectPopupItemWhenResponseIsReceived = Select.LAST;
- }
- }
-
- /*
- * Show the next page.
- */
- private void selectNextPage() {
- if (hasNextPage()) {
- filterOptions(currentPage + 1, lastFilter);
- selectPopupItemWhenResponseIsReceived = Select.FIRST;
- }
- }
-
- /**
- * Triggered when a key was depressed.
- *
- * @param event
- * The KeyUpEvent of the key depressed
- */
-
- @Override
- public void onKeyUp(KeyUpEvent event) {
- if (enableDebug) {
- debug("VFS: onKeyUp(" + event.getNativeKeyCode() + ")");
- }
- if (enabled && !readonly) {
- switch (event.getNativeKeyCode()) {
- case KeyCodes.KEY_ENTER:
- case KeyCodes.KEY_TAB:
- case KeyCodes.KEY_SHIFT:
- case KeyCodes.KEY_CTRL:
- case KeyCodes.KEY_ALT:
- case KeyCodes.KEY_DOWN:
- case KeyCodes.KEY_UP:
- case KeyCodes.KEY_PAGEDOWN:
- case KeyCodes.KEY_PAGEUP:
- case KeyCodes.KEY_ESCAPE:
- // NOP
- break;
- default:
- if (textInputEnabled) {
- // when filtering, we always want to see the results on the
- // first page first.
- filterOptions(0);
- }
- break;
- }
- }
- }
-
- /**
- * Resets the Select to its initial state
- */
- private void reset() {
- debug("VFS: reset()");
- if (currentSuggestion != null) {
- String text = currentSuggestion.getReplacementString();
- setPromptingOff(text);
- setSelectedItemIcon(currentSuggestion.getIconUri());
-
- selectedOptionKey = currentSuggestion.key;
-
- } else {
- if (focused || readonly || !enabled) {
- setPromptingOff("");
- } else {
- setPromptingOn();
- }
- setSelectedItemIcon(null);
-
- selectedOptionKey = null;
- }
-
- lastFilter = "";
- suggestionPopup.hide();
- }
-
- /**
- * Listener for popupopener.
- */
-
- @Override
- public void onClick(ClickEvent event) {
- debug("VFS: onClick()");
- if (textInputEnabled && event.getNativeEvent().getEventTarget()
- .cast() == tb.getElement()) {
- // Don't process clicks on the text field if text input is enabled
- return;
- }
- if (enabled && !readonly) {
- // ask suggestionPopup if it was just closed, we are using GWT
- // Popup's auto close feature
- if (!suggestionPopup.isJustClosed()) {
- // If a focus event is not going to be sent, send the options
- // request immediately; otherwise queue in the same burst as the
- // focus event. Fixes #8321.
- boolean immediate = focused
- || !client.hasEventListeners(this, EventId.FOCUS);
- filterOptions(-1, "", immediate);
- popupOpenerClicked = true;
- lastFilter = "";
- }
- DOM.eventPreventDefault(DOM.eventGetCurrentEvent());
- focus();
- tb.selectAll();
- }
- }
-
- /**
- * Update minimum width for FilterSelect textarea based on input prompt and
- * suggestions.
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public void updateSuggestionPopupMinWidth() {
- // used only to calculate minimum width
- String captions = WidgetUtil.escapeHTML(inputPrompt);
-
- for (FilterSelectSuggestion suggestion : currentSuggestions) {
- // Collect captions so we can calculate minimum width for
- // textarea
- if (!captions.isEmpty()) {
- captions += "|";
- }
- captions += WidgetUtil
- .escapeHTML(suggestion.getReplacementString());
- }
-
- // Calculate minimum textarea width
- suggestionPopupMinWidth = minWidth(captions);
- }
-
- /**
- * Calculate minimum width for FilterSelect textarea.
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public native int minWidth(String captions)
- /*-{
- if (!captions || captions.length <= 0)
- return 0;
- captions = captions.split("|");
- var d = $wnd.document.createElement("div");
- var html = "";
- for (var i=0; i < captions.length; i++) {
- html += "<div>" + captions[i] + "</div>";
- // TODO apply same CSS classname as in suggestionmenu
- }
- d.style.position = "absolute";
- d.style.top = "0";
- d.style.left = "0";
- d.style.visibility = "hidden";
- d.innerHTML = html;
- $wnd.document.body.appendChild(d);
- var w = d.offsetWidth;
- $wnd.document.body.removeChild(d);
- return w;
- }-*/;
-
- /**
- * A flag which prevents a focus event from taking place
- */
- boolean iePreventNextFocus = false;
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
- * .dom.client.FocusEvent)
- */
-
- @Override
- public void onFocus(FocusEvent event) {
- debug("VFS: onFocus()");
-
- /*
- * When we disable a blur event in ie we need to refocus the textfield.
- * This will cause a focus event we do not want to process, so in that
- * case we just ignore it.
- */
- if (BrowserInfo.get().isIE() && iePreventNextFocus) {
- iePreventNextFocus = false;
- return;
- }
-
- focused = true;
- if (prompting && !readonly) {
- setPromptingOff("");
- }
- addStyleDependentName("focus");
-
- if (client.hasEventListeners(this, EventId.FOCUS)) {
- client.updateVariable(paintableId, EventId.FOCUS, "", true);
- afterUpdateClientVariables();
- }
-
- ComponentConnector connector = ConnectorMap.get(client)
- .getConnector(this);
- client.getVTooltip()
- .showAssistive(connector.getTooltipInfo(getElement()));
- }
-
- /**
- * A flag which cancels the blur event and sets the focus back to the
- * textfield if the Browser is IE
- */
- boolean preventNextBlurEventInIE = false;
-
- private String explicitSelectedCaption;
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event
- * .dom.client.BlurEvent)
- */
-
- @Override
- public void onBlur(BlurEvent event) {
- debug("VFS: onBlur()");
-
- if (BrowserInfo.get().isIE() && preventNextBlurEventInIE) {
- /*
- * Clicking in the suggestion popup or on the popup button in IE
- * causes a blur event to be sent for the field. In other browsers
- * this is prevented by canceling/preventing default behavior for
- * the focus event, in IE we handle it here by refocusing the text
- * field and ignoring the resulting focus event for the textfield
- * (in onFocus).
- */
- preventNextBlurEventInIE = false;
-
- Element focusedElement = WidgetUtil.getFocusedElement();
- if (getElement().isOrHasChild(focusedElement) || suggestionPopup
- .getElement().isOrHasChild(focusedElement)) {
-
- // IF the suggestion popup or another part of the VFilterSelect
- // was focused, move the focus back to the textfield and prevent
- // the triggered focus event (in onFocus).
- iePreventNextFocus = true;
- tb.setFocus(true);
- return;
- }
- }
-
- focused = false;
- if (!readonly) {
- if (textInputEnabled && allowNewItem) {
- suggestionPopup.menu.doSelectedItemAction();
- }
- if (selectedOptionKey == null) {
- if (explicitSelectedCaption != null) {
- setPromptingOff(explicitSelectedCaption);
- } else {
- setPromptingOn();
- }
- } else if (currentSuggestion != null) {
- setPromptingOff(currentSuggestion.caption);
- setSelectedItemIcon(currentSuggestion.getIconUri());
- }
- }
- removeStyleDependentName("focus");
-
- if (client.hasEventListeners(this, EventId.BLUR)) {
- client.updateVariable(paintableId, EventId.BLUR, "", true);
- afterUpdateClientVariables();
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.client.Focusable#focus()
- */
-
- @Override
- public void focus() {
- debug("VFS: focus()");
- focused = true;
- if (prompting && !readonly) {
- setPromptingOff("");
- }
- tb.setFocus(true);
- }
-
- /**
- * Calculates the width of the select if the select has undefined width.
- * Should be called when the width changes or when the icon changes.
- * <p>
- * For internal use only. May be removed or replaced in the future.
- */
- public void updateRootWidth() {
- ComponentConnector paintable = ConnectorMap.get(client)
- .getConnector(this);
-
- if (paintable.isUndefinedWidth()) {
-
- /*
- * When the select has a undefined with we need to check that we are
- * only setting the text box width relative to the first page width
- * of the items. If this is not done the text box width will change
- * when the popup is used to view longer items than the text box is
- * wide.
- */
- int w = WidgetUtil.getRequiredWidth(this);
-
- if ((!initDone || currentPage + 1 < 0)
- && suggestionPopupMinWidth > w) {
- /*
- * We want to compensate for the paddings just to preserve the
- * exact size as in Vaadin 6.x, but we get here before
- * MeasuredSize has been initialized.
- * Util.measureHorizontalPaddingAndBorder does not work with
- * border-box, so we must do this the hard way.
- */
- Style style = getElement().getStyle();
- String originalPadding = style.getPadding();
- String originalBorder = style.getBorderWidth();
- style.setPaddingLeft(0, Unit.PX);
- style.setBorderWidth(0, Unit.PX);
- style.setProperty("padding", originalPadding);
- style.setProperty("borderWidth", originalBorder);
-
- // Use util.getRequiredWidth instead of getOffsetWidth here
-
- int iconWidth = selectedItemIcon == null ? 0
- : WidgetUtil.getRequiredWidth(selectedItemIcon);
- int buttonWidth = popupOpener == null ? 0
- : WidgetUtil.getRequiredWidth(popupOpener);
-
- /*
- * Instead of setting the width of the wrapper, set the width of
- * the combobox. Subtract the width of the icon and the
- * popupopener
- */
-
- tb.setWidth((suggestionPopupMinWidth - iconWidth - buttonWidth)
- + "px");
-
- }
-
- /*
- * Lock the textbox width to its current value if it's not already
- * locked
- */
- if (!tb.getElement().getStyle().getWidth().endsWith("px")) {
- int iconWidth = selectedItemIcon == null ? 0
- : selectedItemIcon.getOffsetWidth();
- tb.setWidth((tb.getOffsetWidth() - iconWidth) + "px");
- }
- }
- }
-
- /**
- * Get the width of the select in pixels where the text area and icon has
- * been included.
- *
- * @return The width in pixels
- */
- private int getMainWidth() {
- return getOffsetWidth();
- }
-
- @Override
- public void setWidth(String width) {
- super.setWidth(width);
- if (!width.isEmpty()) {
- tb.setWidth("100%");
- }
- }
-
- /**
- * Handles special behavior of the mouse down event
- *
- * @param event
- */
- private void handleMouseDownEvent(Event event) {
- /*
- * Prevent the keyboard focus from leaving the textfield by preventing
- * the default behavior of the browser. Fixes #4285.
- */
- if (event.getTypeInt() == Event.ONMOUSEDOWN) {
- event.preventDefault();
- event.stopPropagation();
-
- /*
- * In IE the above wont work, the blur event will still trigger. So,
- * we set a flag here to prevent the next blur event from happening.
- * This is not needed if do not already have focus, in that case
- * there will not be any blur event and we should not cancel the
- * next blur.
- */
- if (BrowserInfo.get().isIE() && focused) {
- preventNextBlurEventInIE = true;
- debug("VFS: Going to prevent next blur event on IE");
- }
- }
- }
-
- @Override
- protected void onDetach() {
- super.onDetach();
- suggestionPopup.hide();
- }
-
- @Override
- public com.google.gwt.user.client.Element getSubPartElement(
- String subPart) {
- String[] parts = subPart.split("/");
- if ("textbox".equals(parts[0])) {
- return tb.getElement();
- } else if ("button".equals(parts[0])) {
- return popupOpener.getElement();
- } else if ("popup".equals(parts[0]) && suggestionPopup.isAttached()) {
- if (parts.length == 2) {
- return suggestionPopup.menu.getSubPartElement(parts[1]);
- }
- return suggestionPopup.getElement();
- }
- return null;
- }
-
- @Override
- public String getSubPartName(
- com.google.gwt.user.client.Element subElement) {
- if (tb.getElement().isOrHasChild(subElement)) {
- return "textbox";
- } else if (popupOpener.getElement().isOrHasChild(subElement)) {
- return "button";
- } else if (suggestionPopup.getElement().isOrHasChild(subElement)) {
- return "popup";
- }
- return null;
- }
-
- @Override
- public void setAriaRequired(boolean required) {
- AriaHelper.handleInputRequired(tb, required);
- }
-
- @Override
- public void setAriaInvalid(boolean invalid) {
- AriaHelper.handleInputInvalid(tb, invalid);
- }
-
- @Override
- public void bindAriaCaption(
- com.google.gwt.user.client.Element captionElement) {
- AriaHelper.bindCaption(tb, captionElement);
- }
-
- /*
- * Anything that should be set after the client updates the server.
- */
- private void afterUpdateClientVariables() {
- // We need this here to be consistent with the all the calls.
- // Then set your specific selection type only after
- // client.updateVariable() method call.
- selectPopupItemWhenResponseIsReceived = Select.NONE;
- }
-
- @Override
- public boolean isWorkPending() {
- return waitingForFilteringResponse
- || suggestionPopup.lazyPageScroller.isRunning();
- }
-
- /**
- * Sets the caption of selected item, if "scroll to page" is disabled. This
- * method is meant for internal use and may change in future versions.
- *
- * @since 7.7
- * @param selectedCaption
- * the caption of selected item
- */
- public void setSelectedCaption(String selectedCaption) {
- explicitSelectedCaption = selectedCaption;
- if (selectedCaption != null) {
- setPromptingOff(selectedCaption);
- }
- }
-
- /**
- * This method is meant for internal use and may change in future versions.
- *
- * @since 7.7
- * @return the caption of selected item, if "scroll to page" is disabled
- */
- public String getSelectedCaption() {
- return explicitSelectedCaption;
- }
-
- private static Logger getLogger() {
- return Logger.getLogger(VFilterSelect.class.getName());
- }
- }
|