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.

Escalator.java 343KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.widgets;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.ListIterator;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import java.util.Optional;
  28. import java.util.TreeMap;
  29. import java.util.function.Consumer;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;
  32. import java.util.stream.Stream;
  33. import com.google.gwt.animation.client.Animation;
  34. import com.google.gwt.animation.client.AnimationScheduler;
  35. import com.google.gwt.animation.client.AnimationScheduler.AnimationCallback;
  36. import com.google.gwt.animation.client.AnimationScheduler.AnimationHandle;
  37. import com.google.gwt.core.client.Duration;
  38. import com.google.gwt.core.client.JavaScriptException;
  39. import com.google.gwt.core.client.JavaScriptObject;
  40. import com.google.gwt.core.client.JsArray;
  41. import com.google.gwt.core.client.Scheduler;
  42. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  43. import com.google.gwt.dom.client.DivElement;
  44. import com.google.gwt.dom.client.Document;
  45. import com.google.gwt.dom.client.Element;
  46. import com.google.gwt.dom.client.NativeEvent;
  47. import com.google.gwt.dom.client.Node;
  48. import com.google.gwt.dom.client.NodeList;
  49. import com.google.gwt.dom.client.Style;
  50. import com.google.gwt.dom.client.Style.Display;
  51. import com.google.gwt.dom.client.Style.Unit;
  52. import com.google.gwt.dom.client.TableCellElement;
  53. import com.google.gwt.dom.client.TableRowElement;
  54. import com.google.gwt.dom.client.TableSectionElement;
  55. import com.google.gwt.dom.client.Touch;
  56. import com.google.gwt.event.dom.client.KeyCodes;
  57. import com.google.gwt.event.shared.HandlerRegistration;
  58. import com.google.gwt.logging.client.LogConfiguration;
  59. import com.google.gwt.user.client.DOM;
  60. import com.google.gwt.user.client.Event;
  61. import com.google.gwt.user.client.Window;
  62. import com.google.gwt.user.client.ui.RequiresResize;
  63. import com.google.gwt.user.client.ui.RootPanel;
  64. import com.google.gwt.user.client.ui.UIObject;
  65. import com.google.gwt.user.client.ui.Widget;
  66. import com.vaadin.client.BrowserInfo;
  67. import com.vaadin.client.ComputedStyle;
  68. import com.vaadin.client.DeferredWorker;
  69. import com.vaadin.client.Profiler;
  70. import com.vaadin.client.WidgetUtil;
  71. import com.vaadin.client.ui.SubPartAware;
  72. import com.vaadin.client.widget.escalator.Cell;
  73. import com.vaadin.client.widget.escalator.ColumnConfiguration;
  74. import com.vaadin.client.widget.escalator.EscalatorUpdater;
  75. import com.vaadin.client.widget.escalator.FlyweightCell;
  76. import com.vaadin.client.widget.escalator.FlyweightRow;
  77. import com.vaadin.client.widget.escalator.PositionFunction;
  78. import com.vaadin.client.widget.escalator.PositionFunction.Translate3DPosition;
  79. import com.vaadin.client.widget.escalator.PositionFunction.TranslatePosition;
  80. import com.vaadin.client.widget.escalator.PositionFunction.WebkitTranslate3DPosition;
  81. import com.vaadin.client.widget.escalator.Row;
  82. import com.vaadin.client.widget.escalator.RowContainer;
  83. import com.vaadin.client.widget.escalator.RowContainer.BodyRowContainer;
  84. import com.vaadin.client.widget.escalator.RowVisibilityChangeEvent;
  85. import com.vaadin.client.widget.escalator.RowVisibilityChangeHandler;
  86. import com.vaadin.client.widget.escalator.ScrollbarBundle;
  87. import com.vaadin.client.widget.escalator.ScrollbarBundle.Direction;
  88. import com.vaadin.client.widget.escalator.ScrollbarBundle.HorizontalScrollbarBundle;
  89. import com.vaadin.client.widget.escalator.ScrollbarBundle.VerticalScrollbarBundle;
  90. import com.vaadin.client.widget.escalator.Spacer;
  91. import com.vaadin.client.widget.escalator.SpacerUpdater;
  92. import com.vaadin.client.widget.escalator.events.RowHeightChangedEvent;
  93. import com.vaadin.client.widget.escalator.events.SpacerIndexChangedEvent;
  94. import com.vaadin.client.widget.escalator.events.SpacerVisibilityChangedEvent;
  95. import com.vaadin.client.widget.grid.events.EscalatorSizeChangeHandler;
  96. import com.vaadin.client.widget.grid.events.EscalatorSizeChangeHandler.EscalatorSizeChangeEvent;
  97. import com.vaadin.client.widget.grid.events.ScrollEvent;
  98. import com.vaadin.client.widget.grid.events.ScrollHandler;
  99. import com.vaadin.client.widget.grid.events.VerticalScrollbarVisibilityChangeHandler;
  100. import com.vaadin.client.widget.grid.events.VerticalScrollbarVisibilityChangeHandler.VerticalScrollbarVisibilityChangeEvent;
  101. import com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle;
  102. import com.vaadin.shared.Range;
  103. import com.vaadin.shared.ui.grid.HeightMode;
  104. import com.vaadin.shared.ui.grid.ScrollDestination;
  105. import com.vaadin.shared.util.SharedUtil;
  106. /*-
  107. Maintenance Notes! Reading these might save your day.
  108. (note for editors: line width is 80 chars, including the
  109. one-space indentation)
  110. == Row Container Structure
  111. AbstractRowContainer
  112. |-- AbstractStaticRowContainer
  113. | |-- HeaderRowContainer
  114. | `-- FooterContainer
  115. `---- BodyRowContainerImpl
  116. AbstractRowContainer is intended to contain all common logic
  117. between RowContainers. It manages the bookkeeping of row
  118. count, makes sure that all individual cells are rendered
  119. the same way, and so on.
  120. AbstractStaticRowContainer has some special logic that is
  121. required by all RowContainers that don't scroll (hence the
  122. word "static"). HeaderRowContainer and FooterRowContainer
  123. are pretty thin special cases of a StaticRowContainer
  124. (mostly relating to positioning of the root element).
  125. BodyRowContainerImpl could also be split into an additional
  126. "AbstractScrollingRowContainer", but I felt that no more
  127. inner classes were needed. So it contains both logic
  128. required for making things scroll about, and equivalent
  129. special cases for layouting, as are found in
  130. Header/FooterRowContainers.
  131. == The Three Indices
  132. Each RowContainer can be thought to have three levels of
  133. indices for any given displayed row (but the distinction
  134. matters primarily for the BodyRowContainerImpl, because of
  135. the way it scrolls through data):
  136. - Logical index
  137. - Physical (or DOM) index
  138. - Visual index
  139. LOGICAL INDEX is the index that is linked to the data
  140. source. If you want your data source to represent a SQL
  141. database with 10 000 rows, the 7 000:th row in the SQL has a
  142. logical index of 6 999, since the index is 0-based (unless
  143. that data source does some funky logic).
  144. PHYSICAL INDEX is the index for a row that you see in a
  145. browser's DOM inspector. If your row is the second <tr>
  146. element within a <tbody> tag, it has a physical index of 1
  147. (because of 0-based indices). In Header and
  148. FooterRowContainers, you are safe to assume that the logical
  149. index is the same as the physical index. But because the
  150. BodyRowContainerImpl never displays large data sources
  151. entirely in the DOM, a physical index usually has no
  152. apparent direct relationship with its logical index.
  153. This is the sectionRowIndex in TableRowElements.
  154. RowIndex in TableRowElements displays the physical index
  155. of all row elements, headers and footers included.
  156. VISUAL INDEX is the index relating to the order that you
  157. see a row in, in the browser, as it is rendered. The
  158. topmost row is 0, the second is 1, and so on. The visual
  159. index is similar to the physical index in the sense that
  160. Header and FooterRowContainers can assume a 1:1
  161. relationship between visual index and logical index. And
  162. again, BodyRowContainerImpl has no such relationship. The
  163. body's visual index has additionally no apparent
  164. relationship with its physical index. Because the <tr> tags
  165. are reused in the body and visually repositioned with CSS
  166. as the user scrolls, the relationship between physical
  167. index and visual index is quickly broken. You can get an
  168. element's visual index via the field
  169. BodyRowContainerImpl.visualRowOrder.
  170. Currently, the physical and visual indices are kept in sync
  171. _most of the time_ by a deferred rearrangement of rows.
  172. They become desynced when scrolling. This is to help screen
  173. readers to read the contents from the DOM in a natural
  174. order. See BodyRowContainerImpl.DeferredDomSorter for more
  175. about that.
  176. It should be noted that the entire visual range is not
  177. necessarily in view at any given time, although it should be
  178. optimised to not exceed the maximum amount of rows that can
  179. theoretically fit within the viewport when their associated
  180. spacers have zero height, except by the two rows that are
  181. required for tab navigation to work.
  182. */
  183. /**
  184. * A workaround-class for GWT and JSNI.
  185. * <p>
  186. * GWT is unable to handle some method calls to Java methods in inner-classes
  187. * from within JSNI blocks. Having that inner class extend a non-inner-class (or
  188. * implement such an interface), makes it possible for JSNI to indirectly refer
  189. * to the inner class, by invoking methods and fields in the non-inner-class
  190. * API.
  191. *
  192. * @see Escalator.Scroller
  193. */
  194. abstract class JsniWorkaround {
  195. /**
  196. * A JavaScript function that handles the scroll DOM event, and passes it on
  197. * to Java code.
  198. *
  199. * @see #createScrollListenerFunction(Escalator)
  200. * @see Escalator.Scroller#onScroll()
  201. */
  202. protected final JavaScriptObject scrollListenerFunction;
  203. /**
  204. * A JavaScript function that handles the mousewheel DOM event, and passes
  205. * it on to Java code.
  206. *
  207. * @see #createMousewheelListenerFunction(Escalator)
  208. * @see Escalator.Scroller#onScroll()
  209. */
  210. protected final JavaScriptObject mousewheelListenerFunction;
  211. /**
  212. * A JavaScript function that handles the touch start DOM event, and passes
  213. * it on to Java code.
  214. *
  215. * @see TouchHandlerBundle#touchStart(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent)
  216. */
  217. protected JavaScriptObject touchStartFunction;
  218. /**
  219. * A JavaScript function that handles the touch move DOM event, and passes
  220. * it on to Java code.
  221. *
  222. * @see TouchHandlerBundle#touchMove(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent)
  223. */
  224. protected JavaScriptObject touchMoveFunction;
  225. /**
  226. * A JavaScript function that handles the touch end and cancel DOM events,
  227. * and passes them on to Java code.
  228. *
  229. * @see TouchHandlerBundle#touchEnd(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent)
  230. */
  231. protected JavaScriptObject touchEndFunction;
  232. protected TouchHandlerBundle touchHandlerBundle;
  233. protected JsniWorkaround(final Escalator escalator) {
  234. scrollListenerFunction = createScrollListenerFunction(escalator);
  235. mousewheelListenerFunction = createMousewheelListenerFunction(
  236. escalator);
  237. touchHandlerBundle = new TouchHandlerBundle(escalator);
  238. touchStartFunction = touchHandlerBundle.getTouchStartHandler();
  239. touchMoveFunction = touchHandlerBundle.getTouchMoveHandler();
  240. touchEndFunction = touchHandlerBundle.getTouchEndHandler();
  241. }
  242. /**
  243. * A method that constructs the JavaScript function that will be stored into
  244. * {@link #scrollListenerFunction}.
  245. *
  246. * @param esc
  247. * a reference to the current instance of {@link Escalator}
  248. * @see Escalator.Scroller#onScroll()
  249. */
  250. protected abstract JavaScriptObject createScrollListenerFunction(
  251. Escalator esc);
  252. /**
  253. * A method that constructs the JavaScript function that will be stored into
  254. * {@link #mousewheelListenerFunction}.
  255. *
  256. * @param esc
  257. * a reference to the current instance of {@link Escalator}
  258. * @see Escalator.Scroller#onScroll()
  259. */
  260. protected abstract JavaScriptObject createMousewheelListenerFunction(
  261. Escalator esc);
  262. }
  263. /**
  264. * A low-level table-like widget that features a scrolling virtual viewport and
  265. * lazily generated rows.
  266. *
  267. * @since 7.4
  268. * @author Vaadin Ltd
  269. */
  270. public class Escalator extends Widget
  271. implements RequiresResize, DeferredWorker, SubPartAware {
  272. // todo comments legend
  273. /*
  274. * [[optimize]]: There's an opportunity to rewrite the code in such a way
  275. * that it _might_ perform better (remember to measure, implement,
  276. * re-measure)
  277. */
  278. /*
  279. * [[mpixscroll]]: This code will require alterations that are relevant for
  280. * supporting the scrolling through more pixels than some browsers normally
  281. * would support. (i.e. when we support more than "a million" pixels in the
  282. * escalator DOM). NOTE: these bits can most often also be identified by
  283. * searching for code that call scrollElem.getScrollTop();.
  284. */
  285. /*
  286. * [[spacer]]: Code that is important to make spacers work.
  287. */
  288. /**
  289. * A utility class that contains utility methods that are usually called
  290. * from JSNI.
  291. * <p>
  292. * The methods are moved in this class to minimize the amount of JSNI code
  293. * as much as feasible.
  294. */
  295. static class JsniUtil {
  296. public static class TouchHandlerBundle {
  297. public static final String POINTER_EVENT_TYPE_TOUCH = "touch";
  298. public static final int SIGNIFICANT_MOVE_THRESHOLD = 3;
  299. /**
  300. * A <a href=
  301. * "http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsOverlay.html"
  302. * >JavaScriptObject overlay</a> for the
  303. * <a href="http://www.w3.org/TR/touch-events/">JavaScript
  304. * TouchEvent</a> object.
  305. * <p>
  306. * This needs to be used in the touch event handlers, since GWT's
  307. * {@link com.google.gwt.event.dom.client.TouchEvent TouchEvent}
  308. * can't be cast from the JSNI call, and the
  309. * {@link com.google.gwt.dom.client.NativeEvent NativeEvent} isn't
  310. * properly populated with the correct values.
  311. */
  312. private static final class CustomTouchEvent
  313. extends JavaScriptObject {
  314. protected CustomTouchEvent() {
  315. }
  316. public native NativeEvent getNativeEvent()
  317. /*-{
  318. return this;
  319. }-*/;
  320. public native int getPageX()
  321. /*-{
  322. return this.targetTouches[0].pageX;
  323. }-*/;
  324. public native int getPageY()
  325. /*-{
  326. return this.targetTouches[0].pageY;
  327. }-*/;
  328. public native String getPointerType()
  329. /*-{
  330. return this.pointerType;
  331. }-*/;
  332. }
  333. private final Escalator escalator;
  334. public TouchHandlerBundle(final Escalator escalator) {
  335. this.escalator = escalator;
  336. }
  337. public native JavaScriptObject getTouchStartHandler()
  338. /*-{
  339. // we need to store "this", since it won't be preserved on call.
  340. var self = this;
  341. return $entry(function (e) {
  342. self.@com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle::touchStart(*)(e);
  343. });
  344. }-*/;
  345. public native JavaScriptObject getTouchMoveHandler()
  346. /*-{
  347. // we need to store "this", since it won't be preserved on call.
  348. var self = this;
  349. return $entry(function (e) {
  350. self.@com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle::touchMove(*)(e);
  351. });
  352. }-*/;
  353. public native JavaScriptObject getTouchEndHandler()
  354. /*-{
  355. // we need to store "this", since it won't be preserved on call.
  356. var self = this;
  357. return $entry(function (e) {
  358. self.@com.vaadin.client.widgets.Escalator.JsniUtil.TouchHandlerBundle::touchEnd(*)(e);
  359. });
  360. }-*/;
  361. // Duration of the inertial scrolling simulation. Devices with
  362. // larger screens take longer durations.
  363. private static final int DURATION = Window.getClientHeight();
  364. // multiply scroll velocity with repeated touching
  365. private int acceleration = 1;
  366. private boolean touching = false;
  367. // Two movement objects for storing status and processing touches
  368. private Movement yMov, xMov;
  369. // true if moved significantly since touch start
  370. private boolean movedSignificantly = false;
  371. private double touchStartTime;
  372. final double MIN_VEL = 0.6, MAX_VEL = 4, F_VEL = 1500, F_ACC = 0.7,
  373. F_AXIS = 1;
  374. // The object to deal with one direction scrolling
  375. private class Movement {
  376. final List<Double> speeds = new ArrayList<>();
  377. final ScrollbarBundle scroll;
  378. double position, offset, velocity, prevPos, prevTime, delta;
  379. boolean run, vertical;
  380. public Movement(boolean vertical) {
  381. this.vertical = vertical;
  382. scroll = vertical ? escalator.verticalScrollbar
  383. : escalator.horizontalScrollbar;
  384. }
  385. public void startTouch(CustomTouchEvent event) {
  386. speeds.clear();
  387. prevPos = pagePosition(event);
  388. prevTime = Duration.currentTimeMillis();
  389. }
  390. public void moveTouch(CustomTouchEvent event) {
  391. double pagePosition = pagePosition(event);
  392. if (pagePosition > -1) {
  393. delta = prevPos - pagePosition;
  394. double now = Duration.currentTimeMillis();
  395. double ellapsed = now - prevTime;
  396. velocity = delta / ellapsed;
  397. // if last speed was so low, reset speeds and start
  398. // storing again
  399. if (!speeds.isEmpty() && !validSpeed(speeds.get(0))) {
  400. speeds.clear();
  401. run = true;
  402. }
  403. speeds.add(0, velocity);
  404. prevTime = now;
  405. prevPos = pagePosition;
  406. }
  407. }
  408. public void endTouch(CustomTouchEvent event) {
  409. // Compute average speed
  410. velocity = 0;
  411. for (double s : speeds) {
  412. velocity += s / speeds.size();
  413. }
  414. position = scroll.getScrollPos();
  415. // Compute offset, and adjust it with an easing curve so as
  416. // movement is smoother.
  417. offset = F_VEL * velocity * acceleration
  418. * easingInOutCos(velocity, MAX_VEL);
  419. // Enable or disable inertia movement in this axis
  420. run = validSpeed(velocity);
  421. if (run) {
  422. event.getNativeEvent().preventDefault();
  423. }
  424. }
  425. void validate(Movement other) {
  426. if (!run || other.velocity > 0
  427. && Math.abs(velocity / other.velocity) < F_AXIS) {
  428. delta = offset = 0;
  429. run = false;
  430. }
  431. }
  432. void stepAnimation(double progress) {
  433. scroll.setScrollPos(position + offset * progress);
  434. }
  435. int pagePosition(CustomTouchEvent event) {
  436. // Use native event's screen x and y for IE11 and Edge
  437. // since there is no touches for these browsers (#18737)
  438. if (isCurrentBrowserIE11OrEdge()) {
  439. return vertical
  440. ? event.getNativeEvent().getClientY()
  441. + Window.getScrollTop()
  442. : event.getNativeEvent().getClientX()
  443. + Window.getScrollLeft();
  444. }
  445. JsArray<Touch> a = event.getNativeEvent().getTouches();
  446. return vertical ? a.get(0).getPageY() : a.get(0).getPageX();
  447. }
  448. boolean validSpeed(double speed) {
  449. return Math.abs(speed) > MIN_VEL;
  450. }
  451. }
  452. // Using GWT animations which take care of native animation frames.
  453. private Animation animation = new Animation() {
  454. @Override
  455. public void onUpdate(double progress) {
  456. xMov.stepAnimation(progress);
  457. yMov.stepAnimation(progress);
  458. }
  459. @Override
  460. public double interpolate(double progress) {
  461. return easingOutCirc(progress);
  462. };
  463. @Override
  464. public void onComplete() {
  465. touching = false;
  466. escalator.body.domSorter.reschedule();
  467. };
  468. @Override
  469. public void run(int duration) {
  470. if (xMov.run || yMov.run) {
  471. super.run(duration);
  472. } else {
  473. onComplete();
  474. }
  475. };
  476. };
  477. public void touchStart(final CustomTouchEvent event) {
  478. if (allowTouch(event)) {
  479. if (yMov == null) {
  480. yMov = new Movement(true);
  481. xMov = new Movement(false);
  482. }
  483. if (animation.isRunning()) {
  484. acceleration += F_ACC;
  485. event.getNativeEvent().preventDefault();
  486. animation.cancel();
  487. } else {
  488. acceleration = 1;
  489. }
  490. xMov.startTouch(event);
  491. yMov.startTouch(event);
  492. touchStartTime = Duration.currentTimeMillis();
  493. touching = true;
  494. movedSignificantly = false;
  495. } else {
  496. touching = false;
  497. animation.cancel();
  498. acceleration = 1;
  499. }
  500. }
  501. public void touchMove(final CustomTouchEvent event) {
  502. if (touching) {
  503. if (!movedSignificantly) {
  504. double distanceSquared = Math.abs(xMov.delta)
  505. * Math.abs(xMov.delta)
  506. + Math.abs(yMov.delta) * Math.abs(yMov.delta);
  507. movedSignificantly = distanceSquared > SIGNIFICANT_MOVE_THRESHOLD
  508. * SIGNIFICANT_MOVE_THRESHOLD;
  509. }
  510. // allow handling long press differently, without triggering
  511. // scrolling
  512. if (escalator.getDelayToCancelTouchScroll() >= 0
  513. && !movedSignificantly
  514. && Duration.currentTimeMillis()
  515. - touchStartTime > escalator
  516. .getDelayToCancelTouchScroll()) {
  517. // cancel touch handling, don't prevent event
  518. touching = false;
  519. animation.cancel();
  520. acceleration = 1;
  521. return;
  522. }
  523. xMov.moveTouch(event);
  524. yMov.moveTouch(event);
  525. xMov.validate(yMov);
  526. yMov.validate(xMov);
  527. moveScrollFromEvent(escalator, xMov.delta, yMov.delta,
  528. event.getNativeEvent());
  529. }
  530. }
  531. public void touchEnd(final CustomTouchEvent event) {
  532. if (touching) {
  533. xMov.endTouch(event);
  534. yMov.endTouch(event);
  535. xMov.validate(yMov);
  536. yMov.validate(xMov);
  537. // Adjust duration so as longer movements take more duration
  538. boolean vert = !xMov.run || yMov.run
  539. && Math.abs(yMov.offset) > Math.abs(xMov.offset);
  540. double delta = Math.abs((vert ? yMov : xMov).offset);
  541. animation.run((int) (3 * DURATION * easingOutExp(delta)));
  542. }
  543. }
  544. // Allow touchStart for IE11 and Edge even though there is no touch
  545. // (#18737),
  546. // otherwise allow touch only if there is a single touch in the
  547. // event
  548. private boolean allowTouch(
  549. final TouchHandlerBundle.CustomTouchEvent event) {
  550. if (isCurrentBrowserIE11OrEdge()) {
  551. return (POINTER_EVENT_TYPE_TOUCH
  552. .equals(event.getPointerType()));
  553. } else {
  554. return (event.getNativeEvent().getTouches().length() == 1);
  555. }
  556. }
  557. private double easingInOutCos(double val, double max) {
  558. return 0.5 - 0.5 * Math.cos(Math.PI * Math.signum(val)
  559. * Math.min(Math.abs(val), max) / max);
  560. }
  561. private double easingOutExp(double delta) {
  562. return (1 - Math.pow(2, -delta / 1000));
  563. }
  564. private double easingOutCirc(double progress) {
  565. return Math.sqrt(1 - (progress - 1) * (progress - 1));
  566. }
  567. }
  568. public static void moveScrollFromEvent(final Escalator escalator,
  569. final double deltaX, final double deltaY,
  570. final NativeEvent event) {
  571. boolean scrollPosXChanged = false;
  572. boolean scrollPosYChanged = false;
  573. if (!Double.isNaN(deltaX)) {
  574. double oldScrollPosX = escalator.horizontalScrollbar
  575. .getScrollPos();
  576. escalator.horizontalScrollbar.setScrollPosByDelta(deltaX);
  577. if (oldScrollPosX != escalator.horizontalScrollbar
  578. .getScrollPos()) {
  579. scrollPosXChanged = true;
  580. }
  581. }
  582. if (!Double.isNaN(deltaY)) {
  583. double oldScrollPosY = escalator.verticalScrollbar
  584. .getScrollPos();
  585. escalator.verticalScrollbar.setScrollPosByDelta(deltaY);
  586. if (oldScrollPosY != escalator.verticalScrollbar
  587. .getScrollPos()) {
  588. scrollPosYChanged = true;
  589. }
  590. }
  591. /*
  592. * Only prevent if internal scrolling happened. If there's no more
  593. * room to scroll internally, allow the event to pass further.
  594. */
  595. final boolean warrantedYScroll = deltaY != 0 && scrollPosYChanged
  596. && escalator.verticalScrollbar.showsScrollHandle();
  597. final boolean warrantedXScroll = deltaX != 0 && scrollPosXChanged
  598. && escalator.horizontalScrollbar.showsScrollHandle();
  599. if (warrantedYScroll || warrantedXScroll) {
  600. event.preventDefault();
  601. }
  602. }
  603. }
  604. /**
  605. * ScrollDestination case-specific handling logic.
  606. */
  607. private static double getScrollPos(final ScrollDestination destination,
  608. final double targetStartPx, final double targetEndPx,
  609. final double viewportStartPx, final double viewportEndPx,
  610. final double padding) {
  611. final double viewportLength = viewportEndPx - viewportStartPx;
  612. switch (destination) {
  613. /*
  614. * Scroll as little as possible to show the target element. If the
  615. * element fits into view, this works as START or END depending on the
  616. * current scroll position. If the element does not fit into view, this
  617. * works as START.
  618. */
  619. case ANY: {
  620. final double startScrollPos = targetStartPx - padding;
  621. final double endScrollPos = targetEndPx + padding - viewportLength;
  622. if (startScrollPos < viewportStartPx) {
  623. return startScrollPos;
  624. } else if (targetEndPx + padding > viewportEndPx) {
  625. return endScrollPos;
  626. } else {
  627. // NOOP, it's already visible
  628. return viewportStartPx;
  629. }
  630. }
  631. /*
  632. * Scrolls so that the element is shown at the end of the viewport. The
  633. * viewport will, however, not scroll before its first element.
  634. */
  635. case END: {
  636. return targetEndPx + padding - viewportLength;
  637. }
  638. /*
  639. * Scrolls so that the element is shown in the middle of the viewport.
  640. * The viewport will, however, not scroll beyond its contents, given
  641. * more elements than what the viewport is able to show at once. Under
  642. * no circumstances will the viewport scroll before its first element.
  643. */
  644. case MIDDLE: {
  645. final double targetMiddle = targetStartPx
  646. + (targetEndPx - targetStartPx) / 2;
  647. return targetMiddle - viewportLength / 2;
  648. }
  649. /*
  650. * Scrolls so that the element is shown at the start of the viewport.
  651. * The viewport will, however, not scroll beyond its contents.
  652. */
  653. case START: {
  654. return targetStartPx - padding;
  655. }
  656. /*
  657. * Throw an error if we're here. This can only mean that
  658. * ScrollDestination has been carelessly amended..
  659. */
  660. default: {
  661. throw new IllegalArgumentException(
  662. "Internal: ScrollDestination has been modified, "
  663. + "but Escalator.getScrollPos has not been updated "
  664. + "to match new values.");
  665. }
  666. }
  667. }
  668. /** An inner class that handles all logic related to scrolling. */
  669. private class Scroller extends JsniWorkaround {
  670. private double lastScrollTop = 0;
  671. private double lastScrollLeft = 0;
  672. public Scroller() {
  673. super(Escalator.this);
  674. }
  675. @Override
  676. protected native JavaScriptObject createScrollListenerFunction(
  677. Escalator esc)
  678. /*-{
  679. var vScroll = esc.@com.vaadin.client.widgets.Escalator::verticalScrollbar;
  680. var vScrollElem = vScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::getElement()();
  681. var hScroll = esc.@com.vaadin.client.widgets.Escalator::horizontalScrollbar;
  682. var hScrollElem = hScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::getElement()();
  683. return $entry(function(e) {
  684. var target = e.target;
  685. // in case the scroll event was native (i.e. scrollbars were dragged, or
  686. // the scrollTop/Left was manually modified), the bundles have old cache
  687. // values. We need to make sure that the caches are kept up to date.
  688. if (target === vScrollElem) {
  689. vScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::updateScrollPosFromDom()();
  690. } else if (target === hScrollElem) {
  691. hScroll.@com.vaadin.client.widget.escalator.ScrollbarBundle::updateScrollPosFromDom()();
  692. } else {
  693. $wnd.console.error("unexpected scroll target: "+target);
  694. }
  695. });
  696. }-*/;
  697. @Override
  698. protected native JavaScriptObject createMousewheelListenerFunction(
  699. Escalator esc)
  700. /*-{
  701. return $entry(function(e) {
  702. var deltaX = e.deltaX ? e.deltaX : -0.5*e.wheelDeltaX;
  703. var deltaY = e.deltaY ? e.deltaY : -0.5*e.wheelDeltaY;
  704. // Delta mode 0 is in pixels; we don't need to do anything...
  705. // A delta mode of 1 means we're scrolling by lines instead of pixels
  706. // We need to scale the number of lines by the default line height
  707. if (e.deltaMode === 1) {
  708. var brc = esc.@com.vaadin.client.widgets.Escalator::body;
  709. deltaY *= brc.@com.vaadin.client.widgets.Escalator.AbstractRowContainer::getDefaultRowHeight()();
  710. }
  711. // Other delta modes aren't supported
  712. if ((e.deltaMode !== undefined) && (e.deltaMode >= 2 || e.deltaMode < 0)) {
  713. var msg = "Unsupported wheel delta mode \"" + e.deltaMode + "\"";
  714. // Print warning message
  715. esc.@com.vaadin.client.widgets.Escalator::logWarning(*)(msg);
  716. }
  717. // IE8 has only delta y
  718. if (isNaN(deltaY)) {
  719. deltaY = -0.5*e.wheelDelta;
  720. }
  721. @com.vaadin.client.widgets.Escalator.JsniUtil::moveScrollFromEvent(*)(esc, deltaX, deltaY, e);
  722. });
  723. }-*/;
  724. /**
  725. * Recalculates the virtual viewport represented by the scrollbars, so
  726. * that the sizes of the scroll handles appear correct in the browser
  727. */
  728. public void recalculateScrollbarsForVirtualViewport() {
  729. double scrollContentHeight = body.calculateTotalRowHeight()
  730. + body.spacerContainer.getSpacerHeightsSum();
  731. double scrollContentWidth = columnConfiguration.calculateRowWidth();
  732. double tableWrapperHeight = heightOfEscalator;
  733. double tableWrapperWidth = widthOfEscalator;
  734. boolean verticalScrollNeeded = scrollContentHeight > tableWrapperHeight
  735. + WidgetUtil.PIXEL_EPSILON - header.getHeightOfSection()
  736. - footer.getHeightOfSection();
  737. boolean horizontalScrollNeeded = scrollContentWidth > tableWrapperWidth
  738. + WidgetUtil.PIXEL_EPSILON;
  739. // One dimension got scrollbars, but not the other. Recheck time!
  740. if (verticalScrollNeeded != horizontalScrollNeeded) {
  741. if (!verticalScrollNeeded && horizontalScrollNeeded) {
  742. verticalScrollNeeded = scrollContentHeight > tableWrapperHeight
  743. + WidgetUtil.PIXEL_EPSILON
  744. - header.getHeightOfSection()
  745. - footer.getHeightOfSection()
  746. - horizontalScrollbar.getScrollbarThickness();
  747. } else {
  748. horizontalScrollNeeded = scrollContentWidth > tableWrapperWidth
  749. + WidgetUtil.PIXEL_EPSILON
  750. - verticalScrollbar.getScrollbarThickness();
  751. }
  752. }
  753. // let's fix the table wrapper size, since it's now stable.
  754. if (verticalScrollNeeded) {
  755. tableWrapperWidth -= verticalScrollbar.getScrollbarThickness();
  756. tableWrapperWidth = Math.max(0, tableWrapperWidth);
  757. }
  758. if (horizontalScrollNeeded) {
  759. tableWrapperHeight -= horizontalScrollbar
  760. .getScrollbarThickness();
  761. tableWrapperHeight = Math.max(0, tableWrapperHeight);
  762. }
  763. tableWrapper.getStyle().setHeight(tableWrapperHeight, Unit.PX);
  764. tableWrapper.getStyle().setWidth(tableWrapperWidth, Unit.PX);
  765. double footerHeight = footer.getHeightOfSection();
  766. double headerHeight = header.getHeightOfSection();
  767. double vScrollbarHeight = Math.max(0,
  768. tableWrapperHeight - footerHeight - headerHeight);
  769. verticalScrollbar.setOffsetSize(vScrollbarHeight);
  770. verticalScrollbar.setScrollSize(scrollContentHeight);
  771. /*
  772. * If decreasing the amount of frozen columns, and scrolled to the
  773. * right, the scroll position might reset. So we need to remember
  774. * the scroll position, and re-apply it once the scrollbar size has
  775. * been adjusted.
  776. */
  777. double prevScrollPos = horizontalScrollbar.getScrollPos();
  778. double unfrozenPixels = columnConfiguration
  779. .getCalculatedColumnsWidth(Range.between(
  780. columnConfiguration.getFrozenColumnCount(),
  781. columnConfiguration.getColumnCount()));
  782. double frozenPixels = scrollContentWidth - unfrozenPixels;
  783. double hScrollOffsetWidth = tableWrapperWidth - frozenPixels;
  784. horizontalScrollbar.setOffsetSize(hScrollOffsetWidth);
  785. horizontalScrollbar.setScrollSize(unfrozenPixels);
  786. horizontalScrollbar.getElement().getStyle().setLeft(frozenPixels,
  787. Unit.PX);
  788. horizontalScrollbar.setScrollPos(prevScrollPos);
  789. /*
  790. * only show the scrollbar wrapper if the scrollbar itself is
  791. * visible.
  792. */
  793. if (horizontalScrollbar.showsScrollHandle()) {
  794. horizontalScrollbarDeco.getStyle().clearDisplay();
  795. } else {
  796. horizontalScrollbarDeco.getStyle().setDisplay(Display.NONE);
  797. }
  798. /*
  799. * only show corner background divs if the vertical scrollbar is
  800. * visible.
  801. */
  802. Style hCornerStyle = headerDeco.getStyle();
  803. Style fCornerStyle = footerDeco.getStyle();
  804. if (verticalScrollbar.showsScrollHandle()) {
  805. hCornerStyle.clearDisplay();
  806. fCornerStyle.clearDisplay();
  807. if (horizontalScrollbar.showsScrollHandle()) {
  808. double offset = horizontalScrollbar.getScrollbarThickness();
  809. fCornerStyle.setBottom(offset, Unit.PX);
  810. } else {
  811. fCornerStyle.clearBottom();
  812. }
  813. } else {
  814. hCornerStyle.setDisplay(Display.NONE);
  815. fCornerStyle.setDisplay(Display.NONE);
  816. }
  817. }
  818. /**
  819. * Logical scrolling event handler for the entire widget.
  820. */
  821. public void onScroll() {
  822. final double scrollTop = verticalScrollbar.getScrollPos();
  823. final double scrollLeft = horizontalScrollbar.getScrollPos();
  824. if (lastScrollLeft != scrollLeft) {
  825. for (int i = 0; i < columnConfiguration.frozenColumns; i++) {
  826. header.updateFreezePosition(i, scrollLeft);
  827. body.updateFreezePosition(i, scrollLeft);
  828. footer.updateFreezePosition(i, scrollLeft);
  829. }
  830. position.set(headElem, -scrollLeft, 0);
  831. position.set(footElem, -scrollLeft, 0);
  832. lastScrollLeft = scrollLeft;
  833. }
  834. body.setBodyScrollPosition(scrollLeft, scrollTop);
  835. lastScrollTop = scrollTop;
  836. body.updateEscalatorRowsOnScroll();
  837. body.spacerContainer.updateSpacerDecosVisibility();
  838. /*
  839. * TODO [[optimize]]: Might avoid a reflow by first calculating new
  840. * scrolltop and scrolleft, then doing the escalator magic based on
  841. * those numbers and only updating the positions after that.
  842. */
  843. }
  844. public native void attachScrollListener(Element element)
  845. /*
  846. * Attaching events with JSNI instead of the GWT event mechanism because
  847. * GWT didn't provide enough details in events, or triggering the event
  848. * handlers with GWT bindings was unsuccessful. Maybe, with more time
  849. * and skill, it could be done with better success. JavaScript overlay
  850. * types might work. This might also get rid of the JsniWorkaround
  851. * class.
  852. */
  853. /*-{
  854. if (element.addEventListener) {
  855. element.addEventListener("scroll", this.@com.vaadin.client.widgets.JsniWorkaround::scrollListenerFunction);
  856. } else {
  857. element.attachEvent("onscroll", this.@com.vaadin.client.widgets.JsniWorkaround::scrollListenerFunction);
  858. }
  859. }-*/;
  860. public native void detachScrollListener(Element element)
  861. /*
  862. * Detaching events with JSNI instead of the GWT event mechanism because
  863. * GWT didn't provide enough details in events, or triggering the event
  864. * handlers with GWT bindings was unsuccessful. Maybe, with more time
  865. * and skill, it could be done with better success. JavaScript overlay
  866. * types might work. This might also get rid of the JsniWorkaround
  867. * class.
  868. */
  869. /*-{
  870. if (element.addEventListener) {
  871. element.removeEventListener("scroll", this.@com.vaadin.client.widgets.JsniWorkaround::scrollListenerFunction);
  872. } else {
  873. element.detachEvent("onscroll", this.@com.vaadin.client.widgets.JsniWorkaround::scrollListenerFunction);
  874. }
  875. }-*/;
  876. public native void attachMousewheelListener(Element element)
  877. /*
  878. * Attaching events with JSNI instead of the GWT event mechanism because
  879. * GWT didn't provide enough details in events, or triggering the event
  880. * handlers with GWT bindings was unsuccessful. Maybe, with more time
  881. * and skill, it could be done with better success. JavaScript overlay
  882. * types might work. This might also get rid of the JsniWorkaround
  883. * class.
  884. */
  885. /*-{
  886. // firefox likes "wheel", while others use "mousewheel"
  887. var eventName = 'onmousewheel' in element ? 'mousewheel' : 'wheel';
  888. element.addEventListener(eventName, this.@com.vaadin.client.widgets.JsniWorkaround::mousewheelListenerFunction);
  889. }-*/;
  890. public native void detachMousewheelListener(Element element)
  891. /*
  892. * Detaching events with JSNI instead of the GWT event mechanism because
  893. * GWT didn't provide enough details in events, or triggering the event
  894. * handlers with GWT bindings was unsuccessful. Maybe, with more time
  895. * and skill, it could be done with better success. JavaScript overlay
  896. * types might work. This might also get rid of the JsniWorkaround
  897. * class.
  898. */
  899. /*-{
  900. // firefox likes "wheel", while others use "mousewheel"
  901. var eventName = element.onwheel===undefined?"mousewheel":"wheel";
  902. element.removeEventListener(eventName, this.@com.vaadin.client.widgets.JsniWorkaround::mousewheelListenerFunction);
  903. }-*/;
  904. public native void attachTouchListeners(Element element)
  905. /*
  906. * Detaching events with JSNI instead of the GWT event mechanism because
  907. * GWT didn't provide enough details in events, or triggering the event
  908. * handlers with GWT bindings was unsuccessful. Maybe, with more time
  909. * and skill, it could be done with better success. JavaScript overlay
  910. * types might work. This might also get rid of the JsniWorkaround
  911. * class.
  912. */
  913. /*-{
  914. element.addEventListener("touchstart", this.@com.vaadin.client.widgets.JsniWorkaround::touchStartFunction);
  915. element.addEventListener("touchmove", this.@com.vaadin.client.widgets.JsniWorkaround::touchMoveFunction);
  916. element.addEventListener("touchend", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction);
  917. element.addEventListener("touchcancel", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction);
  918. }-*/;
  919. public native void detachTouchListeners(Element element)
  920. /*
  921. * Detaching events with JSNI instead of the GWT event mechanism because
  922. * GWT didn't provide enough details in events, or triggering the event
  923. * handlers with GWT bindings was unsuccessful. Maybe, with more time
  924. * and skill, it could be done with better success. JavaScript overlay
  925. * types might work. This might also get rid of the JsniWorkaround
  926. * class.
  927. */
  928. /*-{
  929. element.removeEventListener("touchstart", this.@com.vaadin.client.widgets.JsniWorkaround::touchStartFunction);
  930. element.removeEventListener("touchmove", this.@com.vaadin.client.widgets.JsniWorkaround::touchMoveFunction);
  931. element.removeEventListener("touchend", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction);
  932. element.removeEventListener("touchcancel", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction);
  933. }-*/;
  934. /**
  935. * Using pointerdown, pointermove, pointerup, and pointercancel for IE11
  936. * and Edge instead of touch* listeners (#18737)
  937. *
  938. * @param element
  939. */
  940. public native void attachPointerEventListeners(Element element)
  941. /*
  942. * Attaching events with JSNI instead of the GWT event mechanism because
  943. * GWT didn't provide enough details in events, or triggering the event
  944. * handlers with GWT bindings was unsuccessful. Maybe, with more time
  945. * and skill, it could be done with better success. JavaScript overlay
  946. * types might work. This might also get rid of the JsniWorkaround
  947. * class.
  948. */
  949. /*-{
  950. element.addEventListener("pointerdown", this.@com.vaadin.client.widgets.JsniWorkaround::touchStartFunction);
  951. element.addEventListener("pointermove", this.@com.vaadin.client.widgets.JsniWorkaround::touchMoveFunction);
  952. element.addEventListener("pointerup", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction);
  953. element.addEventListener("pointercancel", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction);
  954. }-*/;
  955. /**
  956. * Using pointerdown, pointermove, pointerup, and pointercancel for IE11
  957. * and Edge instead of touch* listeners (#18737)
  958. *
  959. * @param element
  960. */
  961. public native void detachPointerEventListeners(Element element)
  962. /*
  963. * Detaching events with JSNI instead of the GWT event mechanism because
  964. * GWT didn't provide enough details in events, or triggering the event
  965. * handlers with GWT bindings was unsuccessful. Maybe, with more time
  966. * and skill, it could be done with better success. JavaScript overlay
  967. * types might work. This might also get rid of the JsniWorkaround
  968. * class.
  969. */
  970. /*-{
  971. element.removeEventListener("pointerdown", this.@com.vaadin.client.widgets.JsniWorkaround::touchStartFunction);
  972. element.removeEventListener("pointermove", this.@com.vaadin.client.widgets.JsniWorkaround::touchMoveFunction);
  973. element.removeEventListener("pointerup", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction);
  974. element.removeEventListener("pointercancel", this.@com.vaadin.client.widgets.JsniWorkaround::touchEndFunction);
  975. }-*/;
  976. public void scrollToColumn(final int columnIndex,
  977. final ScrollDestination destination, final int padding) {
  978. assert columnIndex >= columnConfiguration.frozenColumns : "Can't scroll to a frozen column";
  979. /*
  980. * To cope with frozen columns, we just pretend those columns are
  981. * not there at all when calculating the position of the target
  982. * column and the boundaries of the viewport. The resulting
  983. * scrollLeft will be correct without compensation since the DOM
  984. * structure effectively means that scrollLeft also ignores the
  985. * frozen columns.
  986. */
  987. final double frozenPixels = columnConfiguration
  988. .getCalculatedColumnsWidth(Range.withLength(0,
  989. columnConfiguration.frozenColumns));
  990. final double targetStartPx = columnConfiguration
  991. .getCalculatedColumnsWidth(Range.withLength(0, columnIndex))
  992. - frozenPixels;
  993. final double targetEndPx = targetStartPx
  994. + columnConfiguration.getColumnWidthActual(columnIndex);
  995. final double viewportStartPx = getScrollLeft();
  996. double viewportEndPx = viewportStartPx
  997. + getBoundingWidth(getElement()) - frozenPixels;
  998. if (verticalScrollbar.showsScrollHandle()) {
  999. viewportEndPx -= WidgetUtil.getNativeScrollbarSize();
  1000. }
  1001. final double scrollLeft = getScrollPos(destination, targetStartPx,
  1002. targetEndPx, viewportStartPx, viewportEndPx, padding);
  1003. /*
  1004. * note that it doesn't matter if the scroll would go beyond the
  1005. * content, since the browser will adjust for that, and everything
  1006. * fall into line accordingly.
  1007. */
  1008. setScrollLeft(scrollLeft);
  1009. }
  1010. public void scrollToRow(final int rowIndex,
  1011. final ScrollDestination destination, final double padding) {
  1012. body.scrollToRowSpacerOrBoth(rowIndex, destination, padding,
  1013. ScrollType.ROW);
  1014. }
  1015. }
  1016. /**
  1017. * Helper class that helps to implement the WAI-ARIA functionality for the
  1018. * Grid and TreeGrid component.
  1019. * <p>
  1020. * The following WAI-ARIA attributes are added through this class:
  1021. *
  1022. * <ul>
  1023. * <li>aria-rowcount (since 8.2)</li>
  1024. * <li>roles provided by {@link AriaGridRole} (since 8.2)</li>
  1025. * </ul>
  1026. *
  1027. * @since 8.2
  1028. */
  1029. public class AriaGridHelper {
  1030. /**
  1031. * This field contains the total number of rows from the grid including
  1032. * rows from thead, tbody and tfoot.
  1033. *
  1034. * @since 8.2
  1035. */
  1036. private int allRows;
  1037. /**
  1038. * Adds the given numberOfRows to allRows and calls
  1039. * {@link #updateAriaRowCount()}.
  1040. *
  1041. * @param numberOfRows
  1042. * number of rows that were added to the grid
  1043. *
  1044. * @since 8.2
  1045. */
  1046. public void addRows(int numberOfRows) {
  1047. allRows += numberOfRows;
  1048. updateAriaRowCount();
  1049. }
  1050. /**
  1051. * Removes the given numberOfRows from allRows and calls
  1052. * {@link #updateAriaRowCount()}.
  1053. *
  1054. * @param numberOfRows
  1055. * number of rows that were removed from the grid
  1056. *
  1057. * @since 8.2
  1058. */
  1059. public void removeRows(int numberOfRows) {
  1060. allRows -= numberOfRows;
  1061. updateAriaRowCount();
  1062. }
  1063. /**
  1064. * Sets the aria-rowcount attribute with the current value of
  1065. * {@link AriaGridHelper#allRows} if the grid is attached and
  1066. * {@link AriaGridHelper#allRows} > 0.
  1067. *
  1068. * @since 8.2
  1069. */
  1070. public void updateAriaRowCount() {
  1071. if (!isAttached() || 0 > allRows) {
  1072. return;
  1073. }
  1074. getTable().setAttribute("aria-rowcount", String.valueOf(allRows));
  1075. }
  1076. /**
  1077. * Sets the {@code role} attribute to the given element.
  1078. *
  1079. * @param element
  1080. * element that should get the role attribute
  1081. * @param role
  1082. * role to be added
  1083. *
  1084. * @since 8.2
  1085. */
  1086. public void updateRole(final Element element, AriaGridRole role) {
  1087. element.setAttribute("role", role.getName());
  1088. }
  1089. }
  1090. /**
  1091. * Holds the currently used aria roles within the grid for rows and cells.
  1092. *
  1093. * @since 8.2
  1094. */
  1095. public enum AriaGridRole {
  1096. ROW("row"), ROWHEADER("rowheader"), ROWGROUP("rowgroup"), GRIDCELL(
  1097. "gridcell"), COLUMNHEADER("columnheader");
  1098. private final String name;
  1099. AriaGridRole(String name) {
  1100. this.name = name;
  1101. }
  1102. /**
  1103. * Return the name of the {@link AriaGridRole}.
  1104. *
  1105. * @return String name to be used as role attribute
  1106. */
  1107. public String getName() {
  1108. return name;
  1109. }
  1110. }
  1111. public abstract class AbstractRowContainer implements RowContainer {
  1112. private EscalatorUpdater updater = EscalatorUpdater.NULL;
  1113. private int rows;
  1114. /**
  1115. * The table section element ({@code <thead>}, {@code <tbody>} or
  1116. * {@code <tfoot>}) the rows (i.e. <code>&lt;tr&gt;</code> tags) are
  1117. * contained in.
  1118. */
  1119. protected final TableSectionElement root;
  1120. /**
  1121. * The primary style name of the escalator. Most commonly provided by
  1122. * Escalator as "v-escalator".
  1123. */
  1124. private String primaryStyleName = null;
  1125. private boolean defaultRowHeightShouldBeAutodetected = true;
  1126. private double defaultRowHeight = INITIAL_DEFAULT_ROW_HEIGHT;
  1127. private boolean initialColumnSizesCalculated = false;
  1128. private boolean autodetectingRowHeightLater = false;
  1129. public AbstractRowContainer(
  1130. final TableSectionElement rowContainerElement) {
  1131. root = rowContainerElement;
  1132. ariaGridHelper.updateRole(root, AriaGridRole.ROWGROUP);
  1133. }
  1134. @Override
  1135. public TableSectionElement getElement() {
  1136. return root;
  1137. }
  1138. /**
  1139. * Gets the tag name of an element to represent a cell in a row.
  1140. * <p>
  1141. * Usually {@code "th"} or {@code "td"}.
  1142. * <p>
  1143. * <em>Note:</em> To actually <em>create</em> such an element, use
  1144. * {@link #createCellElement(double)} instead.
  1145. *
  1146. * @return the tag name for the element to represent cells as
  1147. * @see #createCellElement(double)
  1148. */
  1149. protected abstract String getCellElementTagName();
  1150. /**
  1151. * Gets the role attribute of an element to represent a cell in a row.
  1152. * <p>
  1153. * Usually {@link AriaGridRole#GRIDCELL} except for a cell in the
  1154. * header.
  1155. *
  1156. * @return the role attribute for the element to represent cells
  1157. *
  1158. * @since 8.2
  1159. */
  1160. protected AriaGridRole getCellElementRole() {
  1161. return AriaGridRole.GRIDCELL;
  1162. }
  1163. /**
  1164. * Gets the role attribute of an element to represent a row in a grid.
  1165. * <p>
  1166. * Usually {@link AriaGridRole#ROW} except for a row in the header.
  1167. *
  1168. * @return the role attribute for the element to represent rows
  1169. *
  1170. * @since 8.2
  1171. */
  1172. protected AriaGridRole getRowElementRole() {
  1173. return AriaGridRole.ROW;
  1174. }
  1175. @Override
  1176. public EscalatorUpdater getEscalatorUpdater() {
  1177. return updater;
  1178. }
  1179. /**
  1180. * {@inheritDoc}
  1181. * <p>
  1182. * <em>Implementation detail:</em> This method does no DOM modifications
  1183. * (i.e. is very cheap to call) if there is no data for rows or columns
  1184. * when this method is called.
  1185. *
  1186. * @see #hasColumnAndRowData()
  1187. */
  1188. @Override
  1189. public void setEscalatorUpdater(
  1190. final EscalatorUpdater escalatorUpdater) {
  1191. if (escalatorUpdater == null) {
  1192. throw new IllegalArgumentException(
  1193. "escalator updater cannot be null");
  1194. }
  1195. updater = escalatorUpdater;
  1196. if (hasColumnAndRowData() && getRowCount() > 0) {
  1197. refreshRows(0, getRowCount());
  1198. }
  1199. }
  1200. /**
  1201. * {@inheritDoc}
  1202. * <p>
  1203. * <em>Implementation detail:</em> This method does no DOM modifications
  1204. * (i.e. is very cheap to call) if there are no rows in the DOM when
  1205. * this method is called.
  1206. *
  1207. * @see #hasSomethingInDom()
  1208. */
  1209. @Override
  1210. public void removeRows(final int index, final int numberOfRows) {
  1211. assertArgumentsAreValidAndWithinRange(index, numberOfRows);
  1212. rows -= numberOfRows;
  1213. ariaGridHelper.removeRows(numberOfRows);
  1214. if (!isAttached()) {
  1215. return;
  1216. }
  1217. if (hasSomethingInDom()) {
  1218. paintRemoveRows(index, numberOfRows);
  1219. }
  1220. }
  1221. /**
  1222. * Removes those row elements from the DOM that correspond to the given
  1223. * range of logical indices. This may be fewer than {@code numberOfRows}
  1224. * , even zero, if not all the removed rows are actually visible.
  1225. * <p>
  1226. * The implementation must call
  1227. * {@link #paintRemoveRow(TableRowElement, int)} for each row that is
  1228. * removed from the DOM.
  1229. *
  1230. * @param index
  1231. * the logical index of the first removed row
  1232. * @param numberOfRows
  1233. * number of logical rows to remove
  1234. */
  1235. protected abstract void paintRemoveRows(final int index,
  1236. final int numberOfRows);
  1237. /**
  1238. * Removes a row element from the DOM, invoking
  1239. * {@link #getEscalatorUpdater()}
  1240. * {@link EscalatorUpdater#preDetach(Row, Iterable) preDetach} and
  1241. * {@link EscalatorUpdater#postDetach(Row, Iterable) postDetach} before
  1242. * and after removing the row, respectively.
  1243. * <p>
  1244. * This method must be called for each removed DOM row by any
  1245. * {@link #paintRemoveRows(int, int)} implementation.
  1246. *
  1247. * @param tr
  1248. * the row element to remove.
  1249. * @param logicalRowIndex
  1250. * logical index of the row that is to be removed
  1251. */
  1252. protected void paintRemoveRow(final TableRowElement tr,
  1253. final int logicalRowIndex) {
  1254. flyweightRow.setup(tr, logicalRowIndex,
  1255. columnConfiguration.getCalculatedColumnWidths());
  1256. getEscalatorUpdater().preDetach(flyweightRow,
  1257. flyweightRow.getCells());
  1258. tr.removeFromParent();
  1259. getEscalatorUpdater().postDetach(flyweightRow,
  1260. flyweightRow.getCells());
  1261. /*
  1262. * the "assert" guarantees that this code is run only during
  1263. * development/debugging.
  1264. */
  1265. assert flyweightRow.teardown();
  1266. }
  1267. protected void assertArgumentsAreValidAndWithinRange(final int index,
  1268. final int numberOfRows)
  1269. throws IllegalArgumentException, IndexOutOfBoundsException {
  1270. if (numberOfRows < 1) {
  1271. throw new IllegalArgumentException(
  1272. "Number of rows must be 1 or greater (was "
  1273. + numberOfRows + ")");
  1274. }
  1275. if (index < 0 || index + numberOfRows > getRowCount()) {
  1276. throw new IndexOutOfBoundsException("The given " + "row range ("
  1277. + index + ".." + (index + numberOfRows)
  1278. + ") was outside of the current number of rows ("
  1279. + getRowCount() + ")");
  1280. }
  1281. }
  1282. @Override
  1283. public int getRowCount() {
  1284. return rows;
  1285. }
  1286. /**
  1287. * This method calculates the current row count directly from the DOM.
  1288. * <p>
  1289. * While Escalator is stable, this value should equal to
  1290. * {@link #getRowCount()}, but while row counts are being updated, these
  1291. * two values might differ for a short while.
  1292. * <p>
  1293. * Any extra content, such as spacers for the body, should not be
  1294. * included in this count.
  1295. *
  1296. * @since 7.5.0
  1297. *
  1298. * @return the actual DOM count of rows
  1299. */
  1300. public abstract int getDomRowCount();
  1301. /**
  1302. * {@inheritDoc}
  1303. * <p>
  1304. * <em>Implementation detail:</em> This method does no DOM modifications
  1305. * (i.e. is very cheap to call) if there is no data for columns when
  1306. * this method is called.
  1307. *
  1308. * @see #hasColumnAndRowData()
  1309. */
  1310. @Override
  1311. public void insertRows(final int index, final int numberOfRows) {
  1312. if (index < 0 || index > getRowCount()) {
  1313. throw new IndexOutOfBoundsException("The given index (" + index
  1314. + ") was outside of the current number of rows (0.."
  1315. + getRowCount() + ")");
  1316. }
  1317. if (numberOfRows < 1) {
  1318. throw new IllegalArgumentException(
  1319. "Number of rows must be 1 or greater (was "
  1320. + numberOfRows + ")");
  1321. }
  1322. rows += numberOfRows;
  1323. ariaGridHelper.addRows(numberOfRows);
  1324. /*
  1325. * only add items in the DOM if the widget itself is attached to the
  1326. * DOM. We can't calculate sizes otherwise.
  1327. */
  1328. if (isAttached()) {
  1329. paintInsertRows(index, numberOfRows);
  1330. /*
  1331. * We are inserting the first rows in this container. We
  1332. * potentially need to set the widths for the cells for the
  1333. * first time.
  1334. */
  1335. if (rows == numberOfRows) {
  1336. Scheduler.get().scheduleFinally(() -> {
  1337. if (initialColumnSizesCalculated) {
  1338. return;
  1339. }
  1340. initialColumnSizesCalculated = true;
  1341. Map<Integer, Double> colWidths = new HashMap<>();
  1342. for (int i = 0; i < getColumnConfiguration()
  1343. .getColumnCount(); i++) {
  1344. Double width = Double.valueOf(
  1345. getColumnConfiguration().getColumnWidth(i));
  1346. Integer col = Integer.valueOf(i);
  1347. colWidths.put(col, width);
  1348. }
  1349. getColumnConfiguration().setColumnWidths(colWidths);
  1350. });
  1351. }
  1352. }
  1353. }
  1354. /**
  1355. * Actually add rows into the DOM, now that everything can be
  1356. * calculated.
  1357. *
  1358. * @param visualIndex
  1359. * the DOM index to add rows into
  1360. * @param numberOfRows
  1361. * the number of rows to insert
  1362. */
  1363. protected abstract void paintInsertRows(final int visualIndex,
  1364. final int numberOfRows);
  1365. protected List<TableRowElement> paintInsertStaticRows(
  1366. final int visualIndex, final int numberOfRows) {
  1367. assert isAttached() : "Can't paint rows if Escalator is not attached";
  1368. final List<TableRowElement> addedRows = new ArrayList<>();
  1369. if (numberOfRows < 1) {
  1370. return addedRows;
  1371. }
  1372. Node referenceRow;
  1373. if (root.getChildCount() != 0 && visualIndex != 0) {
  1374. // get the row node we're inserting stuff after
  1375. referenceRow = root.getChild(visualIndex - 1);
  1376. } else {
  1377. // index is 0, so just prepend.
  1378. referenceRow = null;
  1379. }
  1380. for (int row = visualIndex; row < visualIndex
  1381. + numberOfRows; row++) {
  1382. final TableRowElement tr = TableRowElement.as(DOM.createTR());
  1383. addedRows.add(tr);
  1384. tr.addClassName(getStylePrimaryName() + "-row");
  1385. ariaGridHelper.updateRole(tr, getRowElementRole());
  1386. for (int col = 0; col < columnConfiguration
  1387. .getColumnCount(); col++) {
  1388. final double colWidth = columnConfiguration
  1389. .getColumnWidthActual(col);
  1390. final TableCellElement cellElem = createCellElement(
  1391. colWidth);
  1392. tr.appendChild(cellElem);
  1393. // Set stylename and position if new cell is frozen
  1394. if (col < columnConfiguration.frozenColumns) {
  1395. cellElem.addClassName("frozen");
  1396. position.set(cellElem, scroller.lastScrollLeft, 0);
  1397. }
  1398. if (columnConfiguration.frozenColumns > 0
  1399. && col == columnConfiguration.frozenColumns - 1) {
  1400. cellElem.addClassName("last-frozen");
  1401. }
  1402. }
  1403. referenceRow = paintInsertRow(referenceRow, tr, row);
  1404. }
  1405. reapplyRowWidths();
  1406. recalculateSectionHeight();
  1407. return addedRows;
  1408. }
  1409. /**
  1410. * Inserts a single row into the DOM, invoking
  1411. * {@link #getEscalatorUpdater()}
  1412. * {@link EscalatorUpdater#preAttach(Row, Iterable) preAttach} and
  1413. * {@link EscalatorUpdater#postAttach(Row, Iterable) postAttach} before
  1414. * and after inserting the row, respectively. The row should have its
  1415. * cells already inserted.
  1416. *
  1417. * @param referenceRow
  1418. * the row after which to insert or null if insert as first
  1419. * @param tr
  1420. * the row to be inserted
  1421. * @param logicalRowIndex
  1422. * the logical index of the inserted row
  1423. * @return the inserted row to be used as the new reference
  1424. */
  1425. protected Node paintInsertRow(Node referenceRow,
  1426. final TableRowElement tr, int logicalRowIndex) {
  1427. flyweightRow.setup(tr, logicalRowIndex,
  1428. columnConfiguration.getCalculatedColumnWidths());
  1429. getEscalatorUpdater().preAttach(flyweightRow,
  1430. flyweightRow.getCells());
  1431. referenceRow = insertAfterReferenceAndUpdateIt(root, tr,
  1432. referenceRow);
  1433. getEscalatorUpdater().postAttach(flyweightRow,
  1434. flyweightRow.getCells());
  1435. updater.update(flyweightRow, flyweightRow.getCells());
  1436. /*
  1437. * the "assert" guarantees that this code is run only during
  1438. * development/debugging.
  1439. */
  1440. assert flyweightRow.teardown();
  1441. return referenceRow;
  1442. }
  1443. private Node insertAfterReferenceAndUpdateIt(final Element parent,
  1444. final Element elem, final Node referenceNode) {
  1445. if (referenceNode != null) {
  1446. parent.insertAfter(elem, referenceNode);
  1447. } else {
  1448. /*
  1449. * referencenode being null means we have offset 0, i.e. make it
  1450. * the first row
  1451. */
  1452. /*
  1453. * TODO [[optimize]]: Is insertFirst or append faster for an
  1454. * empty root?
  1455. */
  1456. parent.insertFirst(elem);
  1457. }
  1458. return elem;
  1459. }
  1460. protected abstract void recalculateSectionHeight();
  1461. /**
  1462. * Returns the height of all rows in the row container.
  1463. */
  1464. protected double calculateTotalRowHeight() {
  1465. return getDefaultRowHeight() * getRowCount();
  1466. }
  1467. /**
  1468. * {@inheritDoc}
  1469. * <p>
  1470. * <em>Implementation detail:</em> This method does no DOM modifications
  1471. * (i.e. is very cheap to call) if there is no data for columns when
  1472. * this method is called.
  1473. *
  1474. * @see #hasColumnAndRowData()
  1475. */
  1476. @Override
  1477. // overridden because of JavaDoc
  1478. public void refreshRows(final int index, final int numberOfRows) {
  1479. Range rowRange = Range.withLength(index, numberOfRows);
  1480. Range colRange = Range.withLength(0,
  1481. getColumnConfiguration().getColumnCount());
  1482. refreshCells(rowRange, colRange);
  1483. }
  1484. protected abstract void refreshCells(Range logicalRowRange,
  1485. Range colRange);
  1486. void refreshRow(TableRowElement tr, int logicalRowIndex) {
  1487. refreshRow(tr, logicalRowIndex, Range.withLength(0,
  1488. getColumnConfiguration().getColumnCount()));
  1489. }
  1490. void refreshRow(final TableRowElement tr, final int logicalRowIndex,
  1491. Range colRange) {
  1492. flyweightRow.setup(tr, logicalRowIndex,
  1493. columnConfiguration.getCalculatedColumnWidths());
  1494. Iterable<FlyweightCell> cellsToUpdate = flyweightRow
  1495. .getCells(colRange.getStart(), colRange.length());
  1496. updater.update(flyweightRow, cellsToUpdate);
  1497. /*
  1498. * the "assert" guarantees that this code is run only during
  1499. * development/debugging.
  1500. */
  1501. assert flyweightRow.teardown();
  1502. }
  1503. /**
  1504. * Create and setup an empty cell element.
  1505. *
  1506. * @param width
  1507. * the width of the cell, in pixels
  1508. *
  1509. * @return a set-up empty cell element
  1510. */
  1511. public TableCellElement createCellElement(final double width) {
  1512. final TableCellElement cellElem = TableCellElement
  1513. .as(DOM.createElement(getCellElementTagName()));
  1514. final double height = getDefaultRowHeight();
  1515. assert height >= 0 : "defaultRowHeight was negative. There's a setter leak somewhere.";
  1516. cellElem.getStyle().setHeight(height, Unit.PX);
  1517. if (width >= 0) {
  1518. cellElem.getStyle().setWidth(width, Unit.PX);
  1519. }
  1520. cellElem.addClassName(getStylePrimaryName() + "-cell");
  1521. ariaGridHelper.updateRole(cellElem, getCellElementRole());
  1522. return cellElem;
  1523. }
  1524. @Override
  1525. public TableRowElement getRowElement(int index) {
  1526. return getTrByVisualIndex(index);
  1527. }
  1528. /**
  1529. * Gets the child element that is visually at a certain index.
  1530. *
  1531. * @param index
  1532. * the index of the element to retrieve
  1533. * @return the element at position {@code index}
  1534. * @throws IndexOutOfBoundsException
  1535. * if {@code index} is not valid within {@link #root}
  1536. */
  1537. protected abstract TableRowElement getTrByVisualIndex(int index)
  1538. throws IndexOutOfBoundsException;
  1539. protected void paintRemoveColumns(final int offset,
  1540. final int numberOfColumns) {
  1541. for (int i = 0; i < getDomRowCount(); i++) {
  1542. TableRowElement row = getTrByVisualIndex(i);
  1543. flyweightRow.setup(row, i,
  1544. columnConfiguration.getCalculatedColumnWidths());
  1545. Iterable<FlyweightCell> attachedCells = flyweightRow
  1546. .getCells(offset, numberOfColumns);
  1547. getEscalatorUpdater().preDetach(flyweightRow, attachedCells);
  1548. for (int j = 0; j < numberOfColumns; j++) {
  1549. row.getCells().getItem(offset).removeFromParent();
  1550. }
  1551. Iterable<FlyweightCell> detachedCells = flyweightRow
  1552. .getUnattachedCells(offset, numberOfColumns);
  1553. getEscalatorUpdater().postDetach(flyweightRow, detachedCells);
  1554. assert flyweightRow.teardown();
  1555. }
  1556. }
  1557. protected void paintInsertColumns(final int offset,
  1558. final int numberOfColumns, boolean frozen) {
  1559. for (int row = 0; row < getDomRowCount(); row++) {
  1560. final TableRowElement tr = getTrByVisualIndex(row);
  1561. int logicalRowIndex = getLogicalRowIndex(tr);
  1562. paintInsertCells(tr, logicalRowIndex, offset, numberOfColumns);
  1563. }
  1564. reapplyRowWidths();
  1565. if (frozen) {
  1566. for (int col = offset; col < offset + numberOfColumns; col++) {
  1567. setColumnFrozen(col, true);
  1568. }
  1569. }
  1570. }
  1571. /**
  1572. * Inserts new cell elements into a single row element, invoking
  1573. * {@link #getEscalatorUpdater()}
  1574. * {@link EscalatorUpdater#preAttach(Row, Iterable) preAttach} and
  1575. * {@link EscalatorUpdater#postAttach(Row, Iterable) postAttach} before
  1576. * and after inserting the cells, respectively.
  1577. * <p>
  1578. * Precondition: The row must be already attached to the DOM and the
  1579. * FlyweightCell instances corresponding to the new columns added to
  1580. * {@code flyweightRow}.
  1581. *
  1582. * @param tr
  1583. * the row in which to insert the cells
  1584. * @param logicalRowIndex
  1585. * the index of the row
  1586. * @param offset
  1587. * the index of the first cell
  1588. * @param numberOfCells
  1589. * the number of cells to insert
  1590. */
  1591. private void paintInsertCells(final TableRowElement tr,
  1592. int logicalRowIndex, final int offset,
  1593. final int numberOfCells) {
  1594. assert root.isOrHasChild(
  1595. tr) : "The row must be attached to the document";
  1596. flyweightRow.setup(tr, logicalRowIndex,
  1597. columnConfiguration.getCalculatedColumnWidths());
  1598. Iterable<FlyweightCell> cells = flyweightRow
  1599. .getUnattachedCells(offset, numberOfCells);
  1600. for (FlyweightCell cell : cells) {
  1601. final double colWidth = columnConfiguration
  1602. .getColumnWidthActual(cell.getColumn());
  1603. final TableCellElement cellElem = createCellElement(colWidth);
  1604. cell.setElement(cellElem);
  1605. }
  1606. getEscalatorUpdater().preAttach(flyweightRow, cells);
  1607. Node referenceCell;
  1608. if (offset != 0) {
  1609. referenceCell = tr.getChild(offset - 1);
  1610. } else {
  1611. referenceCell = null;
  1612. }
  1613. for (FlyweightCell cell : cells) {
  1614. referenceCell = insertAfterReferenceAndUpdateIt(tr,
  1615. cell.getElement(), referenceCell);
  1616. }
  1617. getEscalatorUpdater().postAttach(flyweightRow, cells);
  1618. getEscalatorUpdater().update(flyweightRow, cells);
  1619. assert flyweightRow.teardown();
  1620. }
  1621. public void setColumnFrozen(int column, boolean frozen) {
  1622. toggleFrozenColumnClass(column, frozen, "frozen");
  1623. if (frozen) {
  1624. updateFreezePosition(column, scroller.lastScrollLeft);
  1625. }
  1626. }
  1627. private void toggleFrozenColumnClass(int column, boolean frozen,
  1628. String className) {
  1629. final NodeList<TableRowElement> childRows = root.getRows();
  1630. for (int row = 0; row < childRows.getLength(); row++) {
  1631. final TableRowElement tr = childRows.getItem(row);
  1632. if (!rowCanBeFrozen(tr)) {
  1633. continue;
  1634. }
  1635. TableCellElement cell = tr.getCells().getItem(column);
  1636. if (frozen) {
  1637. cell.addClassName(className);
  1638. } else {
  1639. cell.removeClassName(className);
  1640. position.reset(cell);
  1641. }
  1642. }
  1643. }
  1644. public void setColumnLastFrozen(int column, boolean lastFrozen) {
  1645. toggleFrozenColumnClass(column, lastFrozen, "last-frozen");
  1646. }
  1647. public void updateFreezePosition(int column, double scrollLeft) {
  1648. final NodeList<TableRowElement> childRows = root.getRows();
  1649. for (int row = 0; row < childRows.getLength(); row++) {
  1650. final TableRowElement tr = childRows.getItem(row);
  1651. if (rowCanBeFrozen(tr)) {
  1652. TableCellElement cell = tr.getCells().getItem(column);
  1653. position.set(cell, scrollLeft, 0);
  1654. }
  1655. }
  1656. }
  1657. /**
  1658. * Checks whether a row is an element, or contains such elements, that
  1659. * can be frozen.
  1660. * <p>
  1661. * In practice, this applies for all header and footer rows. For body
  1662. * rows, it applies for all rows except spacer rows.
  1663. *
  1664. * @since 7.5.0
  1665. *
  1666. * @param tr
  1667. * the row element to check whether it, or any of its its
  1668. * descendants can be frozen
  1669. * @return <code>true</code> if the given element, or any of its
  1670. * descendants, can be frozen
  1671. */
  1672. protected abstract boolean rowCanBeFrozen(TableRowElement tr);
  1673. /**
  1674. * Iterates through all the cells in a column and returns the width of
  1675. * the widest element in this RowContainer.
  1676. *
  1677. * @param index
  1678. * the index of the column to inspect
  1679. * @return the pixel width of the widest element in the indicated column
  1680. */
  1681. public double calculateMaxColWidth(int index) {
  1682. TableRowElement row = TableRowElement
  1683. .as(root.getFirstChildElement());
  1684. double maxWidth = 0;
  1685. while (row != null) {
  1686. final TableCellElement cell = row.getCells().getItem(index);
  1687. final boolean isVisible = !cell.getStyle().getDisplay()
  1688. .equals(Display.NONE.getCssName());
  1689. if (isVisible) {
  1690. maxWidth = Math.max(maxWidth, getBoundingWidth(cell));
  1691. }
  1692. row = TableRowElement.as(row.getNextSiblingElement());
  1693. }
  1694. return maxWidth;
  1695. }
  1696. /**
  1697. * Reapplies all the cells' widths according to the calculated widths in
  1698. * the column configuration.
  1699. */
  1700. public void reapplyColumnWidths() {
  1701. Element row = root.getFirstChildElement();
  1702. while (row != null) {
  1703. // Only handle non-spacer rows
  1704. if (!body.spacerContainer.isSpacer(row)) {
  1705. Element cell = row.getFirstChildElement();
  1706. int columnIndex = 0;
  1707. while (cell != null) {
  1708. final double width = getCalculatedColumnWidthWithColspan(
  1709. cell, columnIndex);
  1710. /*
  1711. * TODO Should Escalator implement ProvidesResize at
  1712. * some point, this is where we need to do that.
  1713. */
  1714. cell.getStyle().setWidth(width, Unit.PX);
  1715. cell = cell.getNextSiblingElement();
  1716. columnIndex++;
  1717. }
  1718. }
  1719. row = row.getNextSiblingElement();
  1720. }
  1721. reapplyRowWidths();
  1722. }
  1723. private double getCalculatedColumnWidthWithColspan(final Element cell,
  1724. final int columnIndex) {
  1725. final int colspan = cell.getPropertyInt(FlyweightCell.COLSPAN_ATTR);
  1726. Range spannedColumns = Range.withLength(columnIndex, colspan);
  1727. /*
  1728. * Since browsers don't explode with overflowing colspans, escalator
  1729. * shouldn't either.
  1730. */
  1731. if (spannedColumns.getEnd() > columnConfiguration
  1732. .getColumnCount()) {
  1733. spannedColumns = Range.between(columnIndex,
  1734. columnConfiguration.getColumnCount());
  1735. }
  1736. return columnConfiguration
  1737. .getCalculatedColumnsWidth(spannedColumns);
  1738. }
  1739. /**
  1740. * Applies the total length of the columns to each row element.
  1741. * <p>
  1742. * <em>Note:</em> In contrast to {@link #reapplyColumnWidths()}, this
  1743. * method only modifies the width of the {@code
  1744. *
  1745. <tr>
  1746. * } element, not the cells within.
  1747. */
  1748. protected void reapplyRowWidths() {
  1749. double rowWidth = columnConfiguration.calculateRowWidth();
  1750. if (rowWidth < 0) {
  1751. return;
  1752. }
  1753. Element row = root.getFirstChildElement();
  1754. while (row != null) {
  1755. // IF there is a rounding error when summing the columns, we
  1756. // need to round the tr width up to ensure that columns fit and
  1757. // do not wrap
  1758. // E.g.122.95+123.25+103.75+209.25+83.52+88.57+263.45+131.21+126.85+113.13=1365.9299999999998
  1759. // For this we must set 1365.93 or the last column will wrap
  1760. row.getStyle().setWidth(WidgetUtil.roundSizeUp(rowWidth),
  1761. Unit.PX);
  1762. row = row.getNextSiblingElement();
  1763. }
  1764. }
  1765. /**
  1766. * The primary style name for the container.
  1767. *
  1768. * @param primaryStyleName
  1769. * the style name to use as prefix for all row and cell style
  1770. * names.
  1771. */
  1772. protected void setStylePrimaryName(String primaryStyleName) {
  1773. String oldStyle = getStylePrimaryName();
  1774. if (SharedUtil.equals(oldStyle, primaryStyleName)) {
  1775. return;
  1776. }
  1777. this.primaryStyleName = primaryStyleName;
  1778. // Update already rendered rows and cells
  1779. Element row = root.getRows().getItem(0);
  1780. while (row != null) {
  1781. UIObject.setStylePrimaryName(row, primaryStyleName + "-row");
  1782. Element cell = TableRowElement.as(row).getCells().getItem(0);
  1783. while (cell != null) {
  1784. assert TableCellElement.is(cell);
  1785. UIObject.setStylePrimaryName(cell,
  1786. primaryStyleName + "-cell");
  1787. cell = cell.getNextSiblingElement();
  1788. }
  1789. row = row.getNextSiblingElement();
  1790. }
  1791. }
  1792. /**
  1793. * Returns the primary style name of the container.
  1794. *
  1795. * @return The primary style name or <code>null</code> if not set.
  1796. */
  1797. protected String getStylePrimaryName() {
  1798. return primaryStyleName;
  1799. }
  1800. @Override
  1801. public void setDefaultRowHeight(double px)
  1802. throws IllegalArgumentException {
  1803. if (px < 1) {
  1804. throw new IllegalArgumentException(
  1805. "Height must be positive. " + px + " was given.");
  1806. }
  1807. defaultRowHeightShouldBeAutodetected = false;
  1808. defaultRowHeight = px;
  1809. reapplyDefaultRowHeights();
  1810. applyHeightByRows();
  1811. }
  1812. @Override
  1813. public double getDefaultRowHeight() {
  1814. return defaultRowHeight;
  1815. }
  1816. /**
  1817. * The default height of rows has (most probably) changed.
  1818. * <p>
  1819. * Make sure that the displayed rows with a default height are updated
  1820. * in height and top position.
  1821. * <p>
  1822. * <em>Note:</em>This implementation should not call
  1823. * {@link Escalator#recalculateElementSizes()} - it is done by the
  1824. * discretion of the caller of this method.
  1825. */
  1826. protected abstract void reapplyDefaultRowHeights();
  1827. protected void reapplyRowHeight(final TableRowElement tr,
  1828. final double heightPx) {
  1829. assert heightPx >= 0 : "Height must not be negative";
  1830. Element cellElem = tr.getFirstChildElement();
  1831. while (cellElem != null) {
  1832. cellElem.getStyle().setHeight(heightPx, Unit.PX);
  1833. cellElem = cellElem.getNextSiblingElement();
  1834. }
  1835. /*
  1836. * no need to apply height to tr-element, it'll be resized
  1837. * implicitly.
  1838. */
  1839. }
  1840. protected void setRowPosition(final TableRowElement tr, final int x,
  1841. final double y) {
  1842. positions.set(tr, x, y);
  1843. }
  1844. /**
  1845. * Returns <em>the assigned</em> top position for the given element.
  1846. * <p>
  1847. * <em>Note:</em> This method does not calculate what a row's top
  1848. * position should be. It just returns an assigned value, correct or
  1849. * not.
  1850. *
  1851. * @param tr
  1852. * the table row element to measure
  1853. * @return the current top position for {@code tr}
  1854. * @see BodyRowContainerImpl#getRowTop(int)
  1855. */
  1856. protected double getRowTop(final TableRowElement tr) {
  1857. return positions.getTop(tr);
  1858. }
  1859. protected void removeRowPosition(TableRowElement tr) {
  1860. positions.remove(tr);
  1861. }
  1862. /**
  1863. * Triggers delayed auto-detection of default row height if it hasn't
  1864. * been set by that point and the Escalator is both attached and
  1865. * displayed.
  1866. */
  1867. public void autodetectRowHeightLater() {
  1868. autodetectingRowHeightLater = true;
  1869. Scheduler.get().scheduleFinally(() -> {
  1870. if (defaultRowHeightShouldBeAutodetected && isAttached()
  1871. && WidgetUtil.isDisplayed(getElement())) {
  1872. autodetectRowHeightNow();
  1873. defaultRowHeightShouldBeAutodetected = false;
  1874. }
  1875. autodetectingRowHeightLater = false;
  1876. });
  1877. }
  1878. @Override
  1879. public boolean isAutodetectingRowHeightLater() {
  1880. return autodetectingRowHeightLater;
  1881. }
  1882. private void fireRowHeightChangedEventFinally() {
  1883. if (!rowHeightChangedEventFired) {
  1884. rowHeightChangedEventFired = true;
  1885. Scheduler.get().scheduleFinally(() -> {
  1886. fireEvent(new RowHeightChangedEvent());
  1887. rowHeightChangedEventFired = false;
  1888. });
  1889. }
  1890. }
  1891. /**
  1892. * Auto-detect row height immediately, if possible. If Escalator isn't
  1893. * attached and displayed yet, auto-detecting cannot be performed
  1894. * correctly. In such cases auto-detecting is left to wait for these
  1895. * conditions to change, and will be performed when they do.
  1896. */
  1897. public void autodetectRowHeightNow() {
  1898. if (!isAttached() || !WidgetUtil.isDisplayed(getElement())) {
  1899. // Run again when attached and displayed
  1900. defaultRowHeightShouldBeAutodetected = true;
  1901. return;
  1902. }
  1903. final double oldRowHeight = defaultRowHeight;
  1904. final Element detectionTr = DOM.createTR();
  1905. detectionTr.setClassName(getStylePrimaryName() + "-row");
  1906. final Element cellElem = DOM.createElement(getCellElementTagName());
  1907. cellElem.setClassName(getStylePrimaryName() + "-cell");
  1908. cellElem.setInnerText("Ij");
  1909. detectionTr.appendChild(cellElem);
  1910. root.appendChild(detectionTr);
  1911. double boundingHeight = getBoundingHeight(cellElem);
  1912. defaultRowHeight = Math.max(1.0d, boundingHeight);
  1913. root.removeChild(detectionTr);
  1914. if (root.hasChildNodes()) {
  1915. reapplyDefaultRowHeights();
  1916. applyHeightByRows();
  1917. }
  1918. if (oldRowHeight != defaultRowHeight) {
  1919. fireRowHeightChangedEventFinally();
  1920. }
  1921. }
  1922. @Override
  1923. public Cell getCell(final Element element) {
  1924. if (element == null) {
  1925. throw new IllegalArgumentException("Element cannot be null");
  1926. }
  1927. /*
  1928. * Ensure that element is not root nor the direct descendant of root
  1929. * (a row or spacer) and ensure the element is inside the dom
  1930. * hierarchy of the root element. If not, return null.
  1931. */
  1932. if (root == element || element.getParentElement() == root
  1933. || !root.isOrHasChild(element)) {
  1934. return null;
  1935. }
  1936. /*
  1937. * Ensure element is the cell element by iterating up the DOM
  1938. * hierarchy until reaching cell element.
  1939. */
  1940. Element cellElementCandidate = element;
  1941. while (cellElementCandidate.getParentElement()
  1942. .getParentElement() != root) {
  1943. cellElementCandidate = cellElementCandidate.getParentElement();
  1944. }
  1945. final TableCellElement cellElement = TableCellElement
  1946. .as(cellElementCandidate);
  1947. // Find dom column
  1948. int domColumnIndex = -1;
  1949. for (Element e = cellElement; e != null; e = e
  1950. .getPreviousSiblingElement()) {
  1951. domColumnIndex++;
  1952. }
  1953. // Find dom row
  1954. int domRowIndex = -1;
  1955. for (Element e = cellElement.getParentElement(); e != null; e = e
  1956. .getPreviousSiblingElement()) {
  1957. domRowIndex++;
  1958. }
  1959. return new Cell(domRowIndex, domColumnIndex, cellElement);
  1960. }
  1961. double measureCellWidth(TableCellElement cell, boolean withContent) {
  1962. /*
  1963. * To get the actual width of the contents, we need to get the cell
  1964. * content without any hardcoded height or width.
  1965. *
  1966. * But we don't want to modify the existing column, because that
  1967. * might trigger some unnecessary listeners and whatnot. So,
  1968. * instead, we make a deep clone of that cell, but without any
  1969. * explicit dimensions, and measure that instead.
  1970. */
  1971. TableCellElement cellClone = TableCellElement
  1972. .as((Element) cell.cloneNode(withContent));
  1973. if (!withContent || columnConfiguration
  1974. .getColumnWidth(cell.getCellIndex()) < 0) {
  1975. clearRelativeWidthContents(cellClone);
  1976. }
  1977. cellClone.getStyle().clearHeight();
  1978. cellClone.getStyle().clearWidth();
  1979. cell.getParentElement().insertBefore(cellClone, cell);
  1980. double requiredWidth = getBoundingWidth(cellClone);
  1981. if (requiredWidth > 0) {
  1982. // add one pixel to avoid subpixel issues
  1983. // (overflow, unnecessary ellipsis...)
  1984. requiredWidth += 1;
  1985. // round up to a fraction that the current browser can handle
  1986. requiredWidth = WidgetUtil.roundSizeUp(requiredWidth);
  1987. }
  1988. cellClone.removeFromParent();
  1989. return requiredWidth;
  1990. }
  1991. /**
  1992. * Contents of an element that is configured to have relative width
  1993. * shouldn't be taken into consideration when measuring minimum widths.
  1994. * Thus any such contents within the element hierarchy need to be
  1995. * cleared out for accurate results. The element itself should remain,
  1996. * however, in case it has styles that affect the end results.
  1997. *
  1998. * @param elem
  1999. * an element that might have unnecessary content that
  2000. * interferes with minimum width calculations
  2001. */
  2002. private void clearRelativeWidthContents(Element elem) {
  2003. try {
  2004. String width = elem.getStyle().getWidth();
  2005. if (width != null && width.endsWith("%")) {
  2006. if (elem.hasChildNodes()) {
  2007. elem.removeAllChildren();
  2008. // add a fake child so that :empty behavior doesn't
  2009. // change
  2010. elem.setInnerHTML("<a/>");
  2011. } else {
  2012. elem.setInnerHTML(null);
  2013. }
  2014. }
  2015. } catch (JavaScriptException e) {
  2016. // no width set, move on
  2017. }
  2018. for (int i = 0; i < elem.getChildCount(); ++i) {
  2019. Node node = elem.getChild(i);
  2020. if (node instanceof Element) {
  2021. clearRelativeWidthContents((Element) node);
  2022. }
  2023. }
  2024. }
  2025. /**
  2026. * Gets the minimum width needed to display the cell properly.
  2027. *
  2028. * @param colIndex
  2029. * index of column to measure
  2030. * @param withContent
  2031. * <code>true</code> if content is taken into account,
  2032. * <code>false</code> if not
  2033. * @return cell width needed for displaying correctly
  2034. */
  2035. double measureMinCellWidth(int colIndex, boolean withContent) {
  2036. assert isAttached() : "Can't measure max width of cell, since Escalator is not attached to the DOM.";
  2037. double minCellWidth = -1;
  2038. NodeList<TableRowElement> rows = root.getRows();
  2039. for (int row = 0; row < rows.getLength(); row++) {
  2040. TableCellElement cell = rows.getItem(row).getCells()
  2041. .getItem(colIndex);
  2042. if (cell != null && !cellIsPartOfSpan(cell)) {
  2043. double cellWidth = measureCellWidth(cell, withContent);
  2044. minCellWidth = Math.max(minCellWidth, cellWidth);
  2045. }
  2046. }
  2047. return minCellWidth;
  2048. }
  2049. private boolean cellIsPartOfSpan(TableCellElement cell) {
  2050. boolean cellHasColspan = cell.getColSpan() > 1;
  2051. boolean cellIsHidden = Display.NONE.getCssName()
  2052. .equals(cell.getStyle().getDisplay());
  2053. return cellHasColspan || cellIsHidden;
  2054. }
  2055. void refreshColumns(int index, int numberOfColumns) {
  2056. if (getRowCount() > 0) {
  2057. Range rowRange = Range.withLength(0, getRowCount());
  2058. Range colRange = Range.withLength(index, numberOfColumns);
  2059. refreshCells(rowRange, colRange);
  2060. }
  2061. }
  2062. /**
  2063. * The height of this table section.
  2064. * <p>
  2065. * Note that {@link Escalator#getBody() the body} will calculate its
  2066. * height, while the others will return a precomputed value.
  2067. *
  2068. * @since 7.5.0
  2069. *
  2070. * @return the height of this table section
  2071. */
  2072. protected abstract double getHeightOfSection();
  2073. /**
  2074. * Gets the logical row index for the given table row element.
  2075. *
  2076. * @param tr
  2077. * the table row element inside this container.
  2078. * @return the logical index of the given element
  2079. */
  2080. public int getLogicalRowIndex(final TableRowElement tr) {
  2081. // Note: BodyRowContainerImpl overrides this behaviour, since the
  2082. // physical index and logical index don't match there. For header
  2083. // and footer there is a match.
  2084. return tr.getSectionRowIndex();
  2085. };
  2086. }
  2087. private abstract class AbstractStaticRowContainer
  2088. extends AbstractRowContainer {
  2089. /** The height of the combined rows in the DOM. Never negative. */
  2090. private double heightOfSection = 0;
  2091. public AbstractStaticRowContainer(
  2092. final TableSectionElement headElement) {
  2093. super(headElement);
  2094. }
  2095. @Override
  2096. public int getDomRowCount() {
  2097. return root.getChildCount();
  2098. }
  2099. @Override
  2100. protected void paintRemoveRows(final int index,
  2101. final int numberOfRows) {
  2102. for (int i = index; i < index + numberOfRows; i++) {
  2103. final TableRowElement tr = root.getRows().getItem(index);
  2104. paintRemoveRow(tr, index);
  2105. }
  2106. recalculateSectionHeight();
  2107. }
  2108. @Override
  2109. protected TableRowElement getTrByVisualIndex(final int index)
  2110. throws IndexOutOfBoundsException {
  2111. if (index >= 0 && index < root.getChildCount()) {
  2112. return root.getRows().getItem(index);
  2113. } else {
  2114. throw new IndexOutOfBoundsException(
  2115. "No such visual index: " + index);
  2116. }
  2117. }
  2118. @Override
  2119. public void insertRows(int index, int numberOfRows) {
  2120. super.insertRows(index, numberOfRows);
  2121. recalculateElementSizes();
  2122. applyHeightByRows();
  2123. }
  2124. @Override
  2125. public void removeRows(int index, int numberOfRows) {
  2126. /*
  2127. * While the rows in a static section are removed, the scrollbar is
  2128. * temporarily shrunk and then re-expanded. This leads to the fact
  2129. * that the scroll position is scooted up a bit. This means that we
  2130. * need to reset the position here.
  2131. *
  2132. * If Escalator, at some point, gets a JIT evaluation functionality,
  2133. * this re-setting is a strong candidate for removal.
  2134. */
  2135. double oldScrollPos = verticalScrollbar.getScrollPos();
  2136. super.removeRows(index, numberOfRows);
  2137. recalculateElementSizes();
  2138. applyHeightByRows();
  2139. verticalScrollbar.setScrollPos(oldScrollPos);
  2140. }
  2141. @Override
  2142. protected void reapplyDefaultRowHeights() {
  2143. if (root.getChildCount() == 0) {
  2144. return;
  2145. }
  2146. Profiler.enter(
  2147. "Escalator.AbstractStaticRowContainer.reapplyDefaultRowHeights");
  2148. Element tr = root.getRows().getItem(0);
  2149. while (tr != null) {
  2150. reapplyRowHeight(TableRowElement.as(tr), getDefaultRowHeight());
  2151. tr = tr.getNextSiblingElement();
  2152. }
  2153. /*
  2154. * Because all rows are immediately displayed in the static row
  2155. * containers, the section's overall height has most probably
  2156. * changed.
  2157. */
  2158. recalculateSectionHeight();
  2159. Profiler.leave(
  2160. "Escalator.AbstractStaticRowContainer.reapplyDefaultRowHeights");
  2161. }
  2162. @Override
  2163. protected void recalculateSectionHeight() {
  2164. Profiler.enter(
  2165. "Escalator.AbstractStaticRowContainer.recalculateSectionHeight");
  2166. double newHeight = calculateTotalRowHeight();
  2167. if (newHeight != heightOfSection) {
  2168. heightOfSection = newHeight;
  2169. sectionHeightCalculated();
  2170. /*
  2171. * We need to update the scrollbar dimension at this point. If
  2172. * we are scrolled too far down and the static section shrinks,
  2173. * the body will try to render rows that don't exist during
  2174. * body.verifyEscalatorCount. This is because the logical row
  2175. * indices are calculated from the scrollbar position.
  2176. */
  2177. verticalScrollbar.setOffsetSize(
  2178. heightOfEscalator - header.getHeightOfSection()
  2179. - footer.getHeightOfSection());
  2180. body.verifyEscalatorCount();
  2181. body.spacerContainer.updateSpacerDecosVisibility();
  2182. }
  2183. Profiler.leave(
  2184. "Escalator.AbstractStaticRowContainer.recalculateSectionHeight");
  2185. }
  2186. /**
  2187. * Informs the row container that the height of its respective table
  2188. * section has changed.
  2189. * <p>
  2190. * These calculations might affect some layouting logic, such as the
  2191. * body is being offset by the footer, the footer needs to be readjusted
  2192. * according to its height, and so on.
  2193. * <p>
  2194. * A table section is either header, body or footer.
  2195. */
  2196. protected abstract void sectionHeightCalculated();
  2197. @Override
  2198. protected void refreshCells(Range logicalRowRange, Range colRange) {
  2199. assertArgumentsAreValidAndWithinRange(logicalRowRange.getStart(),
  2200. logicalRowRange.length());
  2201. if (!isAttached()) {
  2202. return;
  2203. }
  2204. Profiler.enter("Escalator.AbstractStaticRowContainer.refreshCells");
  2205. if (hasColumnAndRowData()) {
  2206. for (int row = logicalRowRange.getStart(); row < logicalRowRange
  2207. .getEnd(); row++) {
  2208. final TableRowElement tr = getTrByVisualIndex(row);
  2209. refreshRow(tr, row, colRange);
  2210. }
  2211. }
  2212. Profiler.leave("Escalator.AbstractStaticRowContainer.refreshCells");
  2213. }
  2214. @Override
  2215. protected void paintInsertRows(int visualIndex, int numberOfRows) {
  2216. paintInsertStaticRows(visualIndex, numberOfRows);
  2217. }
  2218. @Override
  2219. protected boolean rowCanBeFrozen(TableRowElement tr) {
  2220. assert root.isOrHasChild(
  2221. tr) : "Row does not belong to this table section";
  2222. return true;
  2223. }
  2224. @Override
  2225. protected double getHeightOfSection() {
  2226. return Math.max(0, heightOfSection);
  2227. }
  2228. }
  2229. private class HeaderRowContainer extends AbstractStaticRowContainer {
  2230. public HeaderRowContainer(final TableSectionElement headElement) {
  2231. super(headElement);
  2232. }
  2233. @Override
  2234. protected void sectionHeightCalculated() {
  2235. double heightOfSection = getHeightOfSection();
  2236. bodyElem.getStyle().setMarginTop(heightOfSection, Unit.PX);
  2237. spacerDecoContainer.getStyle().setMarginTop(heightOfSection,
  2238. Unit.PX);
  2239. verticalScrollbar.getElement().getStyle().setTop(heightOfSection,
  2240. Unit.PX);
  2241. headerDeco.getStyle().setHeight(heightOfSection, Unit.PX);
  2242. }
  2243. @Override
  2244. protected String getCellElementTagName() {
  2245. return "th";
  2246. }
  2247. @Override
  2248. protected AriaGridRole getRowElementRole() {
  2249. return AriaGridRole.ROWHEADER;
  2250. }
  2251. @Override
  2252. protected AriaGridRole getCellElementRole() {
  2253. return AriaGridRole.COLUMNHEADER;
  2254. }
  2255. @Override
  2256. public void setStylePrimaryName(String primaryStyleName) {
  2257. super.setStylePrimaryName(primaryStyleName);
  2258. UIObject.setStylePrimaryName(root, primaryStyleName + "-header");
  2259. }
  2260. }
  2261. private class FooterRowContainer extends AbstractStaticRowContainer {
  2262. public FooterRowContainer(final TableSectionElement footElement) {
  2263. super(footElement);
  2264. }
  2265. @Override
  2266. public void setStylePrimaryName(String primaryStyleName) {
  2267. super.setStylePrimaryName(primaryStyleName);
  2268. UIObject.setStylePrimaryName(root, primaryStyleName + "-footer");
  2269. }
  2270. @Override
  2271. protected String getCellElementTagName() {
  2272. return "td";
  2273. }
  2274. @Override
  2275. protected void sectionHeightCalculated() {
  2276. double headerHeight = header.getHeightOfSection();
  2277. double footerHeight = footer.getHeightOfSection();
  2278. int vscrollHeight = (int) Math
  2279. .floor(heightOfEscalator - headerHeight - footerHeight);
  2280. final boolean horizontalScrollbarNeeded = columnConfiguration
  2281. .calculateRowWidth() > widthOfEscalator;
  2282. if (horizontalScrollbarNeeded) {
  2283. vscrollHeight -= horizontalScrollbar.getScrollbarThickness();
  2284. }
  2285. footerDeco.getStyle().setHeight(footer.getHeightOfSection(),
  2286. Unit.PX);
  2287. verticalScrollbar.setOffsetSize(vscrollHeight);
  2288. }
  2289. }
  2290. private class BodyRowContainerImpl extends AbstractRowContainer
  2291. implements BodyRowContainer {
  2292. /*
  2293. * TODO [[optimize]]: check whether a native JsArray might be faster
  2294. * than LinkedList
  2295. */
  2296. /**
  2297. * The order in which row elements are rendered visually in the browser,
  2298. * with the help of CSS tricks. Usually has nothing to do with the DOM
  2299. * order.
  2300. *
  2301. * @see #sortDomElements()
  2302. */
  2303. private final LinkedList<TableRowElement> visualRowOrder = new LinkedList<>();
  2304. /**
  2305. * The logical index of the topmost row.
  2306. *
  2307. * @deprecated Use the accessors {@link #setTopRowLogicalIndex(int)},
  2308. * {@link #updateTopRowLogicalIndex(int)} and
  2309. * {@link #getTopRowLogicalIndex()} instead
  2310. */
  2311. @Deprecated
  2312. private int topRowLogicalIndex = 0;
  2313. /**
  2314. * A callback function to be executed after new rows are added to the
  2315. * escalator.
  2316. */
  2317. private Consumer<List<TableRowElement>> newEscalatorRowCallback;
  2318. /**
  2319. * Set the logical index of the first dom row in visual order.
  2320. * <p>
  2321. * NOTE: this is not necessarily the first dom row in the dom tree, just
  2322. * the one positioned to the top with CSS. See maintenance notes at the
  2323. * top of this class for further information.
  2324. *
  2325. * @param topRowLogicalIndex
  2326. * logical index of the first dom row in visual order, might
  2327. * not match the dom tree order
  2328. */
  2329. private void setTopRowLogicalIndex(int topRowLogicalIndex) {
  2330. if (LogConfiguration.loggingIsEnabled(Level.INFO)) {
  2331. Logger.getLogger("Escalator.BodyRowContainer")
  2332. .fine("topRowLogicalIndex: " + this.topRowLogicalIndex
  2333. + " -> " + topRowLogicalIndex);
  2334. }
  2335. assert topRowLogicalIndex >= 0 : "topRowLogicalIndex became negative (top left cell contents: "
  2336. + visualRowOrder.getFirst().getCells().getItem(0)
  2337. .getInnerText()
  2338. + ") ";
  2339. /*
  2340. * if there's a smart way of evaluating and asserting the max index,
  2341. * this would be a nice place to put it. I haven't found out an
  2342. * effective and generic solution.
  2343. */
  2344. this.topRowLogicalIndex = topRowLogicalIndex;
  2345. }
  2346. /**
  2347. * Returns the logical index of the first dom row in visual order. This
  2348. * also gives the offset between the logical and visual indexes.
  2349. * <p>
  2350. * NOTE: this is not necessarily the first dom row in the dom tree, just
  2351. * the one positioned to the top with CSS. See maintenance notes at the
  2352. * top of this class for further information.
  2353. *
  2354. * @return logical index of the first dom row in visual order, might not
  2355. * match the dom tree order
  2356. */
  2357. public int getTopRowLogicalIndex() {
  2358. return topRowLogicalIndex;
  2359. }
  2360. /**
  2361. * Updates the logical index of the first dom row in visual order with
  2362. * the given difference.
  2363. * <p>
  2364. * NOTE: this is not necessarily the first dom row in the dom tree, just
  2365. * the one positioned to the top with CSS. See maintenance notes at the
  2366. * top of this class for further information.
  2367. *
  2368. * @param diff
  2369. * the amount to increase or decrease the logical index of
  2370. * the first dom row in visual order
  2371. */
  2372. private void updateTopRowLogicalIndex(int diff) {
  2373. setTopRowLogicalIndex(topRowLogicalIndex + diff);
  2374. }
  2375. private class DeferredDomSorter {
  2376. private static final int SORT_DELAY_MILLIS = 50;
  2377. // as it happens, 3 frames = 50ms @ 60fps.
  2378. private static final int REQUIRED_FRAMES_PASSED = 3;
  2379. private final AnimationCallback frameCounter = new AnimationCallback() {
  2380. @Override
  2381. public void execute(double timestamp) {
  2382. framesPassed++;
  2383. boolean domWasSorted = sortIfConditionsMet();
  2384. if (!domWasSorted) {
  2385. animationHandle = AnimationScheduler.get()
  2386. .requestAnimationFrame(this);
  2387. } else {
  2388. waiting = false;
  2389. }
  2390. }
  2391. };
  2392. private int framesPassed;
  2393. private double startTime;
  2394. private AnimationHandle animationHandle;
  2395. /** <code>true</code> if a sort is scheduled */
  2396. public boolean waiting = false;
  2397. public void reschedule() {
  2398. waiting = true;
  2399. resetConditions();
  2400. animationHandle = AnimationScheduler.get()
  2401. .requestAnimationFrame(frameCounter);
  2402. }
  2403. private boolean sortIfConditionsMet() {
  2404. boolean enoughFramesHavePassed = framesPassed >= REQUIRED_FRAMES_PASSED;
  2405. boolean enoughTimeHasPassed = (Duration.currentTimeMillis()
  2406. - startTime) >= SORT_DELAY_MILLIS;
  2407. boolean notTouchActivity = !scroller.touchHandlerBundle.touching;
  2408. boolean conditionsMet = enoughFramesHavePassed
  2409. && enoughTimeHasPassed && notTouchActivity;
  2410. if (conditionsMet) {
  2411. resetConditions();
  2412. sortDomElements();
  2413. }
  2414. return conditionsMet;
  2415. }
  2416. private void resetConditions() {
  2417. if (animationHandle != null) {
  2418. animationHandle.cancel();
  2419. animationHandle = null;
  2420. }
  2421. startTime = Duration.currentTimeMillis();
  2422. framesPassed = 0;
  2423. }
  2424. }
  2425. private DeferredDomSorter domSorter = new DeferredDomSorter();
  2426. private final SpacerContainer spacerContainer = new SpacerContainer();
  2427. private boolean insertingOrRemoving = false;
  2428. public BodyRowContainerImpl(final TableSectionElement bodyElement) {
  2429. super(bodyElement);
  2430. }
  2431. @Override
  2432. public void setStylePrimaryName(String primaryStyleName) {
  2433. super.setStylePrimaryName(primaryStyleName);
  2434. UIObject.setStylePrimaryName(root, primaryStyleName + "-body");
  2435. spacerContainer.setStylePrimaryName(primaryStyleName);
  2436. }
  2437. public void updateEscalatorRowsOnScroll() {
  2438. if (visualRowOrder.isEmpty()) {
  2439. return;
  2440. }
  2441. boolean rowsWereMoved = false;
  2442. final double topElementPosition;
  2443. final double nextRowBottomOffset;
  2444. SpacerContainer.SpacerImpl topSpacer = spacerContainer
  2445. .getSpacer(getTopRowLogicalIndex() - 1);
  2446. if (topSpacer != null) {
  2447. topElementPosition = topSpacer.getTop();
  2448. nextRowBottomOffset = topSpacer.getHeight()
  2449. + getDefaultRowHeight();
  2450. } else {
  2451. topElementPosition = getRowTop(visualRowOrder.getFirst());
  2452. nextRowBottomOffset = getDefaultRowHeight();
  2453. }
  2454. // TODO [[mpixscroll]]
  2455. final double scrollTop = tBodyScrollTop;
  2456. final double sectionHeight = getHeightOfSection();
  2457. /*
  2458. * Calculate how the visual range is situated in relation to the
  2459. * viewport. Negative value means part of visual range is hidden
  2460. * above or below the viewport, positive value means there is a gap
  2461. * at the top or the bottom of the viewport, zero means exact match.
  2462. * If there is a gap, some rows that are out of view may need to be
  2463. * recycled from the opposite end.
  2464. */
  2465. final double viewportOffsetTop = topElementPosition - scrollTop;
  2466. final double viewportOffsetBottom = scrollTop + sectionHeight
  2467. - getRowTop(
  2468. getTopRowLogicalIndex() + visualRowOrder.size());
  2469. /*
  2470. * You can only scroll far enough to leave a gap if visualRowOrder
  2471. * contains a maximal amount of rows and there is at least one more
  2472. * outside of the visual range. Consequently there can only be a gap
  2473. * in one end of the viewport at a time.
  2474. */
  2475. if (viewportOffsetTop > 0 || (viewportOffsetTop == 0
  2476. && getTopRowLogicalIndex() > 0)) {
  2477. /*
  2478. * Scrolling up. Either there's empty room on top, or there
  2479. * should be a buffer row for tab navigation on top, but there
  2480. * isn't.
  2481. */
  2482. recycleRowsUpOnScroll(viewportOffsetTop);
  2483. rowsWereMoved = true;
  2484. } else if ((viewportOffsetBottom > 0
  2485. && (viewportOffsetTop + nextRowBottomOffset <= 0))
  2486. || (viewportOffsetBottom == 0 && (getTopRowLogicalIndex()
  2487. + visualRowOrder.size() < getRowCount() - 2))) {
  2488. /*
  2489. * Scrolling down. Either there's empty room at the bottom and
  2490. * the viewport has been scrolled more than the topmost visual
  2491. * row, or there should be a buffer row at the bottom to ensure
  2492. * tab navigation works, but there isn't.
  2493. */
  2494. recycleRowsDownOnScroll(topElementPosition, scrollTop);
  2495. // Moving rows may have removed more spacers and created another
  2496. // gap, this time the scroll position needs adjusting. The last
  2497. // row within visual range should be just below the viewport as
  2498. // a buffer for helping with tab navigation, unless it's the
  2499. // last row altogether.
  2500. int lastRowInVisualRange = getTopRowLogicalIndex()
  2501. + visualRowOrder.size() - 1;
  2502. double expectedBottom = getRowTop(lastRowInVisualRange);
  2503. if (lastRowInVisualRange == getRowCount() - 1) {
  2504. expectedBottom += getDefaultRowHeight() + spacerContainer
  2505. .getSpacerHeight(lastRowInVisualRange);
  2506. }
  2507. if (expectedBottom < scrollTop + sectionHeight) {
  2508. double expectedTop = Math.max(0,
  2509. expectedBottom - sectionHeight);
  2510. setBodyScrollPosition(tBodyScrollLeft, expectedTop);
  2511. setScrollTop(expectedTop);
  2512. }
  2513. rowsWereMoved = true;
  2514. }
  2515. if (rowsWereMoved) {
  2516. fireRowVisibilityChangeEvent();
  2517. // schedule updating of the physical indexes
  2518. domSorter.reschedule();
  2519. }
  2520. }
  2521. /**
  2522. * Recycling rows up for {@link #updateEscalatorRowsOnScroll()}.
  2523. * <p>
  2524. * NOTE: This method should not be called directly from anywhere else.
  2525. *
  2526. * @param viewportOffsetTop
  2527. */
  2528. private void recycleRowsUpOnScroll(double viewportOffsetTop) {
  2529. /*
  2530. * We can ignore spacers here, because we keep enough rows within
  2531. * the visual range to fill the viewport completely whether or not
  2532. * any spacers are shown. There is a small tradeoff of having some
  2533. * rows rendered even if they are outside of the viewport, but this
  2534. * simplifies the handling significantly (we can't know what height
  2535. * any individual spacer has before it has been rendered, which
  2536. * happens with a delay) and keeps the visual range size stable
  2537. * while scrolling. Consequently, even if there are spacers within
  2538. * the current visual range, repositioning this many rows won't
  2539. * cause us to run out of rows at the bottom.
  2540. *
  2541. * The viewportOffsetTop is positive and we round up, and
  2542. * visualRowOrder can't be empty since we are scrolling, so there is
  2543. * always going to be at least one row to move. There should also be
  2544. * one buffer row that actually falls outside of the viewport, in
  2545. * order to ensure that tabulator navigation works if the rows have
  2546. * components in them. The buffer row is only needed if filling the
  2547. * gap doesn't bring us to the top row already.
  2548. */
  2549. int rowsToFillTheGap = (int) Math
  2550. .ceil(viewportOffsetTop / getDefaultRowHeight());
  2551. // ensure we don't try to move more rows than are available
  2552. // above
  2553. rowsToFillTheGap = Math.min(rowsToFillTheGap,
  2554. getTopRowLogicalIndex());
  2555. // add the buffer row if there is room for it
  2556. if (rowsToFillTheGap < getTopRowLogicalIndex()) {
  2557. ++rowsToFillTheGap;
  2558. }
  2559. // we may have scrolled up past all the rows and beyond, can
  2560. // only recycle as many rows as we have
  2561. int rowsToRecycle = Math.min(rowsToFillTheGap,
  2562. visualRowOrder.size());
  2563. // select the rows to recycle from the end of the visual range
  2564. int end = visualRowOrder.size();
  2565. int start = end - rowsToRecycle;
  2566. /*
  2567. * Calculate the logical index for insertion point based on how many
  2568. * rows would be needed to fill the gap. Because we are recycling
  2569. * rows to the top the insertion index will also be the new top row
  2570. * logical index.
  2571. */
  2572. int newTopRowLogicalIndex = getTopRowLogicalIndex()
  2573. - rowsToFillTheGap;
  2574. // recycle the rows and move them to their new positions
  2575. moveAndUpdateEscalatorRows(Range.between(start, end), 0,
  2576. newTopRowLogicalIndex);
  2577. setTopRowLogicalIndex(newTopRowLogicalIndex);
  2578. }
  2579. /**
  2580. * Recycling rows down for {@link #updateEscalatorRowsOnScroll()}.
  2581. * <p>
  2582. * NOTE: This method should not be called directly from anywhere else.
  2583. *
  2584. * @param topElementPosition
  2585. * @param scrollTop
  2586. */
  2587. private void recycleRowsDownOnScroll(double topElementPosition,
  2588. double scrollTop) {
  2589. /*
  2590. * It's better to have any extra rows below than above, so move as
  2591. * many of them as possible regardless of how many are needed to
  2592. * fill the gap, as long as one buffer row remains at the top. It
  2593. * should not be possible to scroll down enough to create a gap
  2594. * without it being possible to recycle rows to fill the gap, so
  2595. * viewport itself doesn't need adjusting no matter what.
  2596. */
  2597. // we already have the rows and spacers here and we don't want
  2598. // to recycle rows that are going to stay visible, so the
  2599. // spacers have to be taken into account
  2600. double extraRowPxAbove = getRowHeightsSumBetweenPxExcludingSpacers(
  2601. topElementPosition, scrollTop);
  2602. // how many rows fit within that extra space and can be
  2603. // recycled, rounded towards zero to avoid moving any partially
  2604. // visible rows
  2605. int rowsToCoverTheExtra = (int) Math
  2606. .floor(extraRowPxAbove / getDefaultRowHeight());
  2607. // leave one to ensure there is a buffer row to help with tab
  2608. // navigation
  2609. if (rowsToCoverTheExtra > 0) {
  2610. --rowsToCoverTheExtra;
  2611. }
  2612. /*
  2613. * Don't move more rows than there are to move, but also don't move
  2614. * more rows than should exist at the bottom. However, it's not
  2615. * possible to scroll down beyond available rows, so there is always
  2616. * at least one row to recycle.
  2617. */
  2618. int rowsToRecycle = Math.min(
  2619. Math.min(rowsToCoverTheExtra, visualRowOrder.size()),
  2620. getRowCount() - getTopRowLogicalIndex()
  2621. - visualRowOrder.size());
  2622. // are only some of the rows getting recycled instead of all
  2623. // of them
  2624. boolean partialMove = rowsToRecycle < visualRowOrder.size();
  2625. // calculate the logical index where the rows should be moved
  2626. int logicalTargetIndex;
  2627. if (partialMove) {
  2628. /*
  2629. * We scroll so little that we can just keep adding the rows
  2630. * immediately below the current escalator.
  2631. */
  2632. logicalTargetIndex = getTopRowLogicalIndex()
  2633. + visualRowOrder.size();
  2634. } else {
  2635. /*
  2636. * Since all escalator rows are getting recycled all spacers are
  2637. * going to get removed and the calculations have to ignore the
  2638. * spacers again in order to figure out which rows are to be
  2639. * displayed. In practice we may end up scrolling further down
  2640. * than the scroll position indicated initially as the spacers
  2641. * that get removed give room for more rows than expected.
  2642. *
  2643. * We can rely on calculations here because there won't be any
  2644. * old rows left to end up mismatched with.
  2645. */
  2646. logicalTargetIndex = (int) Math
  2647. .floor(scrollTop / getDefaultRowHeight());
  2648. /*
  2649. * Make sure we don't try to move rows below the actual row
  2650. * count, even if some of the rows end up hidden at the top as a
  2651. * result. This won't leave us with any old rows in any case,
  2652. * because we already checked earlier that there is room to
  2653. * recycle all the rows. It's only a question of how the new
  2654. * visual range gets positioned in relation to the viewport.
  2655. */
  2656. if (logicalTargetIndex
  2657. + visualRowOrder.size() > getRowCount()) {
  2658. logicalTargetIndex = getRowCount() - visualRowOrder.size();
  2659. }
  2660. }
  2661. /*
  2662. * Recycle the rows and move them to their new positions. Since we
  2663. * are moving the viewport downwards, the visual target index is
  2664. * always at the bottom and matches the length of the visual range.
  2665. * Note: Due to how moveAndUpdateEscalatorRows works, this will work
  2666. * out even if we move all the rows, and try to place them
  2667. * "at the end".
  2668. */
  2669. moveAndUpdateEscalatorRows(Range.between(0, rowsToRecycle),
  2670. visualRowOrder.size(), logicalTargetIndex);
  2671. // top row logical index needs to be updated differently
  2672. // depending on which update strategy was used, since the rows
  2673. // are being moved down
  2674. if (partialMove) {
  2675. // move down by the amount of recycled rows
  2676. updateTopRowLogicalIndex(rowsToRecycle);
  2677. } else {
  2678. // the insertion index is the new top row logical index
  2679. setTopRowLogicalIndex(logicalTargetIndex);
  2680. }
  2681. }
  2682. /**
  2683. * Calculates how much of the given range contains only rows with
  2684. * spacers excluded.
  2685. *
  2686. * @param y1
  2687. * start position
  2688. * @param y2
  2689. * end position
  2690. * @return position difference excluding any space taken up by spacers
  2691. */
  2692. private double getRowHeightsSumBetweenPxExcludingSpacers(double y1,
  2693. double y2) {
  2694. assert y1 < y2 : "y1 must be smaller than y2";
  2695. double viewportPx = y2 - y1;
  2696. double spacerPx = spacerContainer.getSpacerHeightsSumBetweenPx(y1,
  2697. SpacerInclusionStrategy.PARTIAL, y2,
  2698. SpacerInclusionStrategy.PARTIAL);
  2699. return viewportPx - spacerPx;
  2700. }
  2701. @Override
  2702. public void insertRows(int index, int numberOfRows) {
  2703. insertingOrRemoving = true;
  2704. super.insertRows(index, numberOfRows);
  2705. insertingOrRemoving = false;
  2706. if (heightMode == HeightMode.UNDEFINED) {
  2707. setHeightByRows(getRowCount());
  2708. }
  2709. }
  2710. @Override
  2711. public void removeRows(int index, int numberOfRows) {
  2712. insertingOrRemoving = true;
  2713. super.removeRows(index, numberOfRows);
  2714. insertingOrRemoving = false;
  2715. if (heightMode == HeightMode.UNDEFINED) {
  2716. setHeightByRows(getRowCount());
  2717. }
  2718. }
  2719. @Override
  2720. protected void paintInsertRows(final int index,
  2721. final int numberOfRows) {
  2722. assert index >= 0
  2723. && index < getRowCount() : "Attempting to insert a row "
  2724. + "outside of the available range.";
  2725. assert numberOfRows > 0 : "Attempting to insert a non-positive "
  2726. + "amount of rows, something must be wrong.";
  2727. if (numberOfRows <= 0) {
  2728. return;
  2729. }
  2730. /*
  2731. * NOTE: this method handles and manipulates logical, visual, and
  2732. * physical indexes a lot. If you don't remember what those mean and
  2733. * how they relate to each other, see the top of this class for
  2734. * Maintenance Notes.
  2735. *
  2736. * At the beginning of this method the logical index of the data
  2737. * provider has already been updated to include the new rows, but
  2738. * visual and physical indexes have not, nor has the spacer indexing
  2739. * been updated, and the topRowLogicalIndex may be out of date as
  2740. * well.
  2741. */
  2742. // top of visible area before any rows are actually added
  2743. double scrollTop = getScrollTop();
  2744. // logical index of the first row within the visual range before any
  2745. // rows are actually added
  2746. int oldTopRowLogicalIndex = getTopRowLogicalIndex();
  2747. // length of the visual range before any rows are actually added
  2748. int oldVisualRangeLength = visualRowOrder.size();
  2749. /*
  2750. * If there is room for more dom rows within the maximum visual
  2751. * range, add them. Calling this method repositions all the rows and
  2752. * spacers below the insertion point and updates the spacer indexes
  2753. * accordingly.
  2754. *
  2755. * TODO: Details rows should be added and populated here, since they
  2756. * have variable heights and affect the position calculations.
  2757. * Currently that's left to be triggered at the end and with a
  2758. * delay. If any new spacers exist, everything below them is going
  2759. * to be repositioned again for every spacer addition.
  2760. */
  2761. final List<TableRowElement> addedRows = fillAndPopulateEscalatorRowsIfNeeded(
  2762. index - oldTopRowLogicalIndex, index, numberOfRows);
  2763. // is the insertion point for new rows below visual range (viewport
  2764. // is irrelevant)
  2765. final boolean newRowsInsertedBelowVisualRange = index >= oldVisualRangeLength
  2766. + oldTopRowLogicalIndex;
  2767. // is the insertion point for new rows above initial visual range
  2768. final boolean newRowsInsertedAboveVisualRange = index <= oldTopRowLogicalIndex;
  2769. // is the insertion point for new rows above viewport
  2770. final boolean newRowsInsertedAboveCurrentViewport = getRowTop(
  2771. index) < scrollTop;
  2772. if (newRowsInsertedBelowVisualRange) {
  2773. /*
  2774. * There is no change to scroll position, and all other changes
  2775. * to positioning and indexing are out of visual range or
  2776. * already done (if addedRows is not empty).
  2777. */
  2778. } else if (newRowsInsertedAboveVisualRange && addedRows.isEmpty()
  2779. && newRowsInsertedAboveCurrentViewport) {
  2780. /*
  2781. * This section can only be reached if the insertion point is
  2782. * above the visual range, the visual range already covers a
  2783. * maximal amount of rows, and we are scrolled down enough that
  2784. * the top row is either partially or completely hidden. The
  2785. * last two points happen by default if the first row of the
  2786. * visual range has any other logical index than zero. Any other
  2787. * use cases involving the top row within the visual range need
  2788. * different handling.
  2789. */
  2790. paintInsertRowsAboveViewPort(index, numberOfRows,
  2791. oldTopRowLogicalIndex);
  2792. } else if (newRowsInsertedAboveCurrentViewport) {
  2793. /*
  2794. * Rows were inserted within the visual range but above the
  2795. * viewport. This includes the use case where the insertion
  2796. * point is just above the visual range and we are scrolled down
  2797. * a bit but the visual range doesn't have maximal amount of
  2798. * rows yet (can only happen with spacers in play), so more rows
  2799. * were added to the visual range but no rows need to be
  2800. * recycled.
  2801. */
  2802. paintInsertRowsWithinVisualRangeButAboveViewport(index,
  2803. numberOfRows, oldTopRowLogicalIndex, addedRows.size());
  2804. } else {
  2805. /*
  2806. * Rows added within visual range and either within or below the
  2807. * viewport. Recycled rows come from the END of the visual
  2808. * range.
  2809. */
  2810. paintInsertRowsWithinVisualRangeAndWithinOrBelowViewport(index,
  2811. numberOfRows, oldTopRowLogicalIndex, addedRows.size());
  2812. }
  2813. /*
  2814. * Calling insertRows will always change the number of rows - update
  2815. * the scrollbar sizes. This calculation isn't affected by actual
  2816. * dom rows amount or contents except for spacer heights. Spacers
  2817. * that don't fit the visual range are considered to have no height
  2818. * and might affect scrollbar calculations aversely, but that can't
  2819. * be avoided since they have unknown and variable heights.
  2820. */
  2821. scroller.recalculateScrollbarsForVirtualViewport();
  2822. }
  2823. /**
  2824. * Row insertion handling for {@link #paintInsertRows(int, int)} when
  2825. * the range will be inserted above the visual range.
  2826. * <p>
  2827. * NOTE: This method should not be called directly from anywhere else.
  2828. *
  2829. * @param index
  2830. * @param numberOfRows
  2831. * @param oldTopRowLogicalIndex
  2832. */
  2833. private void paintInsertRowsAboveViewPort(int index, int numberOfRows,
  2834. int oldTopRowLogicalIndex) {
  2835. /*
  2836. * Because there is no need to expand the visual range, no row or
  2837. * spacer contents get updated. All rows, spacers, and scroll
  2838. * position simply need to be shifted down accordingly and the
  2839. * spacer indexes need updating.
  2840. */
  2841. spacerContainer.updateSpacerIndexesForRowAndAfter(index,
  2842. oldTopRowLogicalIndex + visualRowOrder.size(),
  2843. numberOfRows);
  2844. // height of a single row
  2845. double defaultRowHeight = getDefaultRowHeight();
  2846. // height of new rows, out of visual range so spacers assumed to
  2847. // have no height
  2848. double newRowsHeight = numberOfRows * defaultRowHeight;
  2849. // update the positions
  2850. moveViewportAndContent(index, newRowsHeight, newRowsHeight,
  2851. newRowsHeight);
  2852. // top row logical index moves down by the number of new rows
  2853. updateTopRowLogicalIndex(numberOfRows);
  2854. }
  2855. /**
  2856. * Row insertion handling for {@link #paintInsertRows(int, int)} when
  2857. * the range will be inserted within the visual range above the
  2858. * viewport.
  2859. * <p>
  2860. * NOTE: This method should not be called directly from anywhere else.
  2861. *
  2862. * @param index
  2863. * @param numberOfRows
  2864. * @param oldTopRowLogicalIndex
  2865. * @param addedRowCount
  2866. */
  2867. private void paintInsertRowsWithinVisualRangeButAboveViewport(int index,
  2868. int numberOfRows, int oldTopRowLogicalIndex,
  2869. int addedRowCount) {
  2870. /*
  2871. * Unless we are scrolled all the way to the top the visual range is
  2872. * always out of view because we need a buffer row for tabulator
  2873. * navigation. Depending on the scroll position and spacers there
  2874. * might even be several rendered rows above the viewport,
  2875. * especially when we are scrolled all the way to the bottom.
  2876. *
  2877. * Even though the new rows will be initially out of view they still
  2878. * need to be correctly populated and positioned. Their contents
  2879. * won't be refreshed if they become visible later on (e.g. when a
  2880. * spacer gets hidden, which causes more rows to fit within the
  2881. * viewport) because they are expected to be already up to date.
  2882. *
  2883. * Note that it's not possible to insert content so that it's
  2884. * partially visible at the top. A partially visible row at top will
  2885. * still be the exact same partially visible row after the
  2886. * insertion, no matter which side of that row the new content gets
  2887. * inserted to. This section handles the use case where the new
  2888. * content is inserted above the partially visible row.
  2889. *
  2890. * Because the insertion point is out of view above the viewport,
  2891. * the only thing that should change for the end user visually is
  2892. * the scroll handle, which gets a new position and possibly turns a
  2893. * bit smaller if a lot of rows got inserted.
  2894. *
  2895. * From a technical point of view this also means that any rows that
  2896. * might need to get recycled should be taken from the BEGINNING of
  2897. * the visual range, above the insertion point. There might still be
  2898. * some "extra" rows below the viewport as well, but those should be
  2899. * left alone. They are going to be needed where they are if any
  2900. * spacers get closed or reduced in size.
  2901. *
  2902. * On a practical level we need to tweak the virtual viewport --
  2903. * scroll handle positions, row and spacer positions, and ensure the
  2904. * scroll area height is calculated correctly. Viewport should
  2905. * remain in a fixed position in relation to the existing rows and
  2906. * display no new rows. If any rows get recycled and have spacers
  2907. * either before or after the update the height of those spacers
  2908. * affects the position calculations.
  2909. *
  2910. * Insertion point can be anywhere from just before the previous
  2911. * first row of the visual range to just before the first actually
  2912. * visible row. The insertion shifts down the content below
  2913. * insertion point, which excludes any dom rows that remain above
  2914. * the insertion point after recycling is finished. After the rows
  2915. * below insertion point have been moved the viewport needs to be
  2916. * shifted down a similar amount to regain its old relative position
  2917. * again.
  2918. *
  2919. * The visual range only ever contains at most as many rows as would
  2920. * fit within the viewport without any spacers with one extra row on
  2921. * both at the top and at the bottom as buffer rows, so the amount
  2922. * of rows that needs to be checked is always reasonably limited.
  2923. */
  2924. // insertion index within the visual range
  2925. int visualTargetIndex = index - oldTopRowLogicalIndex;
  2926. // how many dom rows before insertion point versus how many new
  2927. // rows didn't get their own dom rows -- smaller amount
  2928. // determines how many rows can and need to be recycled
  2929. int rowsToUpdate = Math.min(visualTargetIndex,
  2930. numberOfRows - addedRowCount);
  2931. // height of a single row
  2932. double defaultRowHeight = getDefaultRowHeight();
  2933. boolean rowVisibilityChanged = false;
  2934. if (rowsToUpdate > 0) {
  2935. // recycle the rows and update the positions, adjust
  2936. // logical index for inserted rows that won't fit within
  2937. // visual range
  2938. int logicalIndex = index + numberOfRows - rowsToUpdate;
  2939. if (visualTargetIndex > 0) {
  2940. // move after any added dom rows
  2941. moveAndUpdateEscalatorRows(Range.between(0, rowsToUpdate),
  2942. visualTargetIndex + addedRowCount, logicalIndex);
  2943. } else {
  2944. // move before any added dom rows
  2945. moveAndUpdateEscalatorRows(Range.between(0, rowsToUpdate),
  2946. visualTargetIndex, logicalIndex);
  2947. }
  2948. // adjust viewport down to maintain the initial position
  2949. double newRowsHeight = numberOfRows * defaultRowHeight;
  2950. double newSpacerHeights = spacerContainer
  2951. .getSpacerHeightsSumUntilIndex(
  2952. logicalIndex + rowsToUpdate)
  2953. - spacerContainer.getSpacerHeightsSumUntilIndex(index);
  2954. /*
  2955. * FIXME: spacers haven't been added yet and they can cause
  2956. * escalator contents to shift after the fact in a way that
  2957. * can't be countered for here.
  2958. *
  2959. * FIXME: verticalScrollbar internal state causes this update to
  2960. * fail partially and the next attempt at scrolling causes
  2961. * things to jump.
  2962. *
  2963. * Couldn't find a quick fix to either problem and this use case
  2964. * is somewhat marginal so left them here for now.
  2965. */
  2966. moveViewportAndContent(null, 0, 0,
  2967. newSpacerHeights + newRowsHeight);
  2968. rowVisibilityChanged = true;
  2969. } else {
  2970. // no rows to recycle but update the spacer indexes
  2971. spacerContainer.updateSpacerIndexesForRowAndAfter(index,
  2972. index + numberOfRows - addedRowCount,
  2973. numberOfRows - addedRowCount);
  2974. double newRowsHeight = numberOfRows * defaultRowHeight;
  2975. if (addedRowCount > 0) {
  2976. // update the viewport, rows and spacers were
  2977. // repositioned already by the method for adding dom
  2978. // rows
  2979. moveViewportAndContent(null, 0, 0, newRowsHeight);
  2980. rowVisibilityChanged = true;
  2981. } else {
  2982. // all changes are actually above the viewport after
  2983. // all, update all positions
  2984. moveViewportAndContent(index, newRowsHeight, newRowsHeight,
  2985. newRowsHeight);
  2986. }
  2987. }
  2988. if (numberOfRows > addedRowCount) {
  2989. /*
  2990. * If there are more new rows than how many new dom rows got
  2991. * added, the top row logical index necessarily gets shifted
  2992. * down by that difference because recycling doesn't replace any
  2993. * logical rows, just shifts them off the visual range, and the
  2994. * inserted rows that don't fit to the visual range also push
  2995. * the other rows down. If every new row got new dom rows as
  2996. * well the top row logical index doesn't change, because the
  2997. * insertion point was within the visual range.
  2998. */
  2999. updateTopRowLogicalIndex(numberOfRows - addedRowCount);
  3000. }
  3001. if (rowVisibilityChanged) {
  3002. fireRowVisibilityChangeEvent();
  3003. }
  3004. if (rowsToUpdate > 0) {
  3005. // update the physical index
  3006. sortDomElements();
  3007. }
  3008. }
  3009. /**
  3010. * Row insertion handling for {@link #paintInsertRows(int, int)} when
  3011. * the range will be inserted within the visual range either within or
  3012. * below the viewport.
  3013. * <p>
  3014. * NOTE: This method should not be called directly from anywhere else.
  3015. *
  3016. * @param index
  3017. * @param numberOfRows
  3018. * @param oldTopRowLogicalIndex
  3019. * @param addedRowCount
  3020. */
  3021. private void paintInsertRowsWithinVisualRangeAndWithinOrBelowViewport(
  3022. int index, int numberOfRows, int oldTopRowLogicalIndex,
  3023. int addedRowCount) {
  3024. // insertion index within the visual range
  3025. int visualIndex = index - oldTopRowLogicalIndex;
  3026. // how many dom rows after insertion point versus how many new
  3027. // rows to add -- smaller amount determines how many rows can or
  3028. // need to be recycled, excluding the rows that already got new
  3029. // dom rows
  3030. int rowsToUpdate = Math.max(
  3031. Math.min(visualRowOrder.size() - visualIndex, numberOfRows)
  3032. - addedRowCount,
  3033. 0);
  3034. if (rowsToUpdate > 0) {
  3035. moveAndUpdateEscalatorRows(
  3036. Range.between(visualRowOrder.size() - rowsToUpdate,
  3037. visualRowOrder.size()),
  3038. visualIndex + addedRowCount, index + addedRowCount);
  3039. fireRowVisibilityChangeEvent();
  3040. // update the physical index
  3041. sortDomElements();
  3042. }
  3043. }
  3044. /**
  3045. * Move escalator rows around, and make sure everything gets
  3046. * appropriately repositioned and repainted. In the case of insertion or
  3047. * removal, following spacer indexes get updated as well.
  3048. *
  3049. * @param visualSourceRange
  3050. * the range of rows to move to a new place
  3051. * @param visualTargetIndex
  3052. * the visual index where the rows will be placed to
  3053. * @param logicalTargetIndex
  3054. * the logical index to be assigned to the first moved row
  3055. */
  3056. private void moveAndUpdateEscalatorRows(final Range visualSourceRange,
  3057. final int visualTargetIndex, final int logicalTargetIndex)
  3058. throws IllegalArgumentException {
  3059. if (visualSourceRange.isEmpty()) {
  3060. return;
  3061. }
  3062. int sourceRangeLength = visualSourceRange.length();
  3063. int domRowCount = getDomRowCount();
  3064. int rowCount = getRowCount();
  3065. assert visualSourceRange.getStart() >= 0 : "Visual source start "
  3066. + "must be 0 or greater (was "
  3067. + visualSourceRange.getStart() + ")";
  3068. assert logicalTargetIndex >= 0 : "Logical target must be 0 or "
  3069. + "greater (was " + logicalTargetIndex + ")";
  3070. assert visualTargetIndex >= 0 : "Visual target must be 0 or greater (was "
  3071. + visualTargetIndex + ")";
  3072. assert visualTargetIndex <= domRowCount : "Visual target "
  3073. + "must not be greater than the number of escalator rows (was "
  3074. + visualTargetIndex + ", escalator rows " + domRowCount
  3075. + ")";
  3076. assert logicalTargetIndex
  3077. + sourceRangeLength <= rowCount : "Logical "
  3078. + "target leads to rows outside of the data range ("
  3079. + Range.withLength(logicalTargetIndex,
  3080. sourceRangeLength)
  3081. + " goes beyond " + Range.withLength(0, rowCount)
  3082. + ")";
  3083. /*
  3084. * Since we move a range into another range, the indices might move
  3085. * about. Having 10 rows, if we move 0..1 to index 10 (to the end of
  3086. * the collection), the target range will end up being 8..9, instead
  3087. * of 10..11.
  3088. *
  3089. * This applies only if we move elements forward in the collection,
  3090. * not backward.
  3091. */
  3092. final int adjustedVisualTargetIndex;
  3093. if (visualSourceRange.getStart() < visualTargetIndex) {
  3094. adjustedVisualTargetIndex = visualTargetIndex
  3095. - sourceRangeLength;
  3096. } else {
  3097. adjustedVisualTargetIndex = visualTargetIndex;
  3098. }
  3099. int oldTopRowLogicalIndex = getTopRowLogicalIndex();
  3100. // first moved row's logical index before move
  3101. int oldSourceRangeLogicalStart = oldTopRowLogicalIndex
  3102. + visualSourceRange.getStart();
  3103. // new top row logical index
  3104. int newTopRowLogicalIndex = logicalTargetIndex
  3105. - adjustedVisualTargetIndex;
  3106. // variables for update types that require special handling
  3107. boolean recycledToTop = logicalTargetIndex < oldTopRowLogicalIndex;
  3108. boolean recycledFromTop = visualSourceRange.getStart() == 0;
  3109. boolean scrollingUp = recycledToTop
  3110. && visualSourceRange.getEnd() == visualRowOrder.size();
  3111. boolean scrollingDown = recycledFromTop
  3112. && logicalTargetIndex >= oldTopRowLogicalIndex
  3113. + visualRowOrder.size();
  3114. if (visualSourceRange.getStart() != adjustedVisualTargetIndex) {
  3115. /*
  3116. * Reorder the rows to their correct places within
  3117. * visualRowOrder (unless rows are moved back to their original
  3118. * places)
  3119. */
  3120. /*
  3121. * TODO [[optimize]]: move whichever set is smaller: the ones
  3122. * explicitly moved, or the others. So, with 10 escalator rows,
  3123. * if we are asked to move idx[0..8] to the end of the list,
  3124. * it's faster to just move idx[9] to the beginning.
  3125. */
  3126. final List<TableRowElement> removedRows = new ArrayList<>(
  3127. sourceRangeLength);
  3128. for (int i = 0; i < sourceRangeLength; i++) {
  3129. final TableRowElement tr = visualRowOrder
  3130. .remove(visualSourceRange.getStart());
  3131. removedRows.add(tr);
  3132. }
  3133. visualRowOrder.addAll(adjustedVisualTargetIndex, removedRows);
  3134. }
  3135. // refresh contents of rows to be recycled, returns the combined
  3136. // height of the spacers that got removed from visual range
  3137. double spacerHeightsOfRecycledRowsBefore = refreshRecycledRowContents(
  3138. logicalTargetIndex, adjustedVisualTargetIndex,
  3139. sourceRangeLength, oldSourceRangeLogicalStart);
  3140. boolean movedDown = adjustedVisualTargetIndex != visualTargetIndex;
  3141. boolean recycledToOrFromTop = recycledToTop || recycledFromTop;
  3142. // update spacer indexes unless we are scrolling -- with scrolling
  3143. // the remaining spacers are where they belong, the recycled ones
  3144. // were already removed, and new ones will be added with delay
  3145. if (!(scrollingUp || scrollingDown)) {
  3146. if (recycledToOrFromTop) {
  3147. updateSpacerIndexesForMoveWhenRecycledToOrFromTop(
  3148. oldSourceRangeLogicalStart, sourceRangeLength,
  3149. oldTopRowLogicalIndex, newTopRowLogicalIndex,
  3150. recycledFromTop);
  3151. } else {
  3152. updateSpacerIndexesForMoveWhenNotRecycledToOrFromTop(
  3153. logicalTargetIndex, oldSourceRangeLogicalStart,
  3154. sourceRangeLength, movedDown);
  3155. }
  3156. }
  3157. // Would be useful if new spacer heights could be determined
  3158. // here already but their contents are populated with delay.
  3159. // If the heights ever become available immediately, the
  3160. // handling that follows needs to be updated to take the new
  3161. // spacer heights into account.
  3162. repositionMovedRows(adjustedVisualTargetIndex, sourceRangeLength,
  3163. newTopRowLogicalIndex);
  3164. // variables for reducing the amount of necessary parameters
  3165. boolean scrollingDownAndNoSpacersRemoved = scrollingDown
  3166. && spacerHeightsOfRecycledRowsBefore <= 0d;
  3167. boolean spacerHeightsChanged = spacerHeightsOfRecycledRowsBefore > 0d;
  3168. repositionRowsShiftedByTheMove(visualSourceRange, visualTargetIndex,
  3169. adjustedVisualTargetIndex, newTopRowLogicalIndex,
  3170. scrollingDownAndNoSpacersRemoved, scrollingUp,
  3171. recycledToTop);
  3172. repositionRowsBelowMovedAndShiftedIfNeeded(visualSourceRange,
  3173. visualTargetIndex, adjustedVisualTargetIndex,
  3174. newTopRowLogicalIndex, (scrollingUp || scrollingDown),
  3175. recycledToOrFromTop, spacerHeightsChanged);
  3176. }
  3177. /**
  3178. * Refresh the contents of the affected rows for
  3179. * {@link #moveAndUpdateEscalatorRows(Range, int, int)}
  3180. * <p>
  3181. * NOTE: This method should not be called directly from anywhere else.
  3182. *
  3183. * @param logicalTargetIndex
  3184. * @param adjustedVisualTargetIndex
  3185. * @param sourceRangeLength
  3186. * @param spacerHeightsBeforeMoveTotal
  3187. * @param oldSourceRangeLogicalStart
  3188. * @return the combined height of any removed spacers
  3189. */
  3190. private double refreshRecycledRowContents(int logicalTargetIndex,
  3191. int adjustedVisualTargetIndex, int sourceRangeLength,
  3192. int oldSourceRangeLogicalStart) {
  3193. final ListIterator<TableRowElement> iter = visualRowOrder
  3194. .listIterator(adjustedVisualTargetIndex);
  3195. double removedSpacerHeights = 0d;
  3196. for (int i = 0; i < sourceRangeLength; ++i) {
  3197. final TableRowElement tr = iter.next();
  3198. int logicalIndex = logicalTargetIndex + i;
  3199. // clear old spacer
  3200. SpacerContainer.SpacerImpl spacer = spacerContainer
  3201. .getSpacer(oldSourceRangeLogicalStart + i);
  3202. if (spacer != null) {
  3203. double spacerHeight = spacer.getHeight();
  3204. removedSpacerHeights += spacerHeight;
  3205. spacerContainer
  3206. .removeSpacer(oldSourceRangeLogicalStart + i);
  3207. }
  3208. refreshRow(tr, logicalIndex);
  3209. }
  3210. return removedSpacerHeights;
  3211. }
  3212. /**
  3213. * Update the spacer indexes to correspond with logical indexes for
  3214. * {@link #moveAndUpdateEscalatorRows(Range, int, int)} when the move
  3215. * recycles rows to or from top
  3216. * <p>
  3217. * NOTE: This method should not be called directly from anywhere else.
  3218. *
  3219. * @param oldSourceRangeLogicalStart
  3220. * @param sourceRangeLength
  3221. * @param oldTopRowLogicalIndex
  3222. * @param newTopRowLogicalIndex
  3223. * @param recycledFromTop
  3224. */
  3225. private void updateSpacerIndexesForMoveWhenRecycledToOrFromTop(
  3226. int oldSourceRangeLogicalStart, int sourceRangeLength,
  3227. int oldTopRowLogicalIndex, int newTopRowLogicalIndex,
  3228. boolean recycledFromTop) {
  3229. if (recycledFromTop) {
  3230. // first rows are getting recycled thanks to insertion or
  3231. // removal, all the indexes below need to be updated
  3232. // accordingly
  3233. int indexesToShift;
  3234. if (newTopRowLogicalIndex != oldTopRowLogicalIndex) {
  3235. indexesToShift = newTopRowLogicalIndex
  3236. - oldTopRowLogicalIndex;
  3237. } else {
  3238. indexesToShift = -sourceRangeLength;
  3239. }
  3240. spacerContainer.updateSpacerIndexesForRowAndAfter(
  3241. oldSourceRangeLogicalStart + sourceRangeLength,
  3242. oldTopRowLogicalIndex + visualRowOrder.size(),
  3243. indexesToShift);
  3244. } else {
  3245. // rows recycled to the top, move the remaining spacer
  3246. // indexes up
  3247. spacerContainer.updateSpacerIndexesForRowAndAfter(
  3248. oldSourceRangeLogicalStart + sourceRangeLength,
  3249. getRowCount() + sourceRangeLength, -sourceRangeLength);
  3250. }
  3251. }
  3252. /**
  3253. * Update the spacer indexes to correspond with logical indexes for
  3254. * {@link #moveAndUpdateEscalatorRows(Range, int, int)} when the move
  3255. * does not recycle rows to or from top
  3256. * <p>
  3257. * NOTE: This method should not be called directly from anywhere else.
  3258. *
  3259. * @param logicalTargetIndex
  3260. * @param oldSourceRangeLogicalStart
  3261. * @param sourceRangeLength
  3262. * @param movedDown
  3263. */
  3264. private void updateSpacerIndexesForMoveWhenNotRecycledToOrFromTop(
  3265. int logicalTargetIndex, int oldSourceRangeLogicalStart,
  3266. int sourceRangeLength, boolean movedDown) {
  3267. if (movedDown) {
  3268. // move the shifted spacer indexes up to fill the freed
  3269. // space
  3270. spacerContainer.updateSpacerIndexesForRowAndAfter(
  3271. oldSourceRangeLogicalStart + sourceRangeLength,
  3272. logicalTargetIndex + sourceRangeLength,
  3273. -sourceRangeLength);
  3274. } else {
  3275. // move the shifted spacer indexes down to fill the freed
  3276. // space
  3277. spacerContainer.updateSpacerIndexesForRowAndAfter(
  3278. logicalTargetIndex, oldSourceRangeLogicalStart,
  3279. sourceRangeLength);
  3280. }
  3281. }
  3282. /**
  3283. * Reposition the rows that were moved for
  3284. * {@link #moveAndUpdateEscalatorRows(Range, int, int)}
  3285. * <p>
  3286. * NOTE: This method should not be called directly from anywhere else.
  3287. *
  3288. * @param adjustedVisualTargetIndex
  3289. * @param sourceRangeLength
  3290. * @param newTopRowLogicalIndex
  3291. */
  3292. private void repositionMovedRows(int adjustedVisualTargetIndex,
  3293. int sourceRangeLength, int newTopRowLogicalIndex) {
  3294. int start = adjustedVisualTargetIndex;
  3295. updateRowPositions(newTopRowLogicalIndex + start, start,
  3296. sourceRangeLength);
  3297. }
  3298. /**
  3299. * Reposition the rows that were shifted by the move for
  3300. * {@link #moveAndUpdateEscalatorRows(Range, int, int)}
  3301. * <p>
  3302. * NOTE: This method should not be called directly from anywhere else.
  3303. *
  3304. * @param visualSourceRange
  3305. * @param visualTargetIndex
  3306. * @param adjustedVisualTargetIndex
  3307. * @param newTopRowLogicalIndex
  3308. * @param scrollingDownAndNoSpacersRemoved
  3309. * @param scrollingUp
  3310. * @param recycledToTop
  3311. */
  3312. private void repositionRowsShiftedByTheMove(Range visualSourceRange,
  3313. int visualTargetIndex, int adjustedVisualTargetIndex,
  3314. int newTopRowLogicalIndex,
  3315. boolean scrollingDownAndNoSpacersRemoved, boolean scrollingUp,
  3316. boolean recycledToTop) {
  3317. if (visualSourceRange.length() == visualRowOrder.size()) {
  3318. // all rows got updated and were repositioned already
  3319. return;
  3320. }
  3321. if (scrollingDownAndNoSpacersRemoved || scrollingUp) {
  3322. // scrolling, no spacers got removed from or added above any
  3323. // remaining rows so everything is where it belongs already
  3324. // (there is no check for added spacers because adding happens
  3325. // with delay, whether any spacers are coming or not they don't
  3326. // exist yet and thus can't be taken into account here)
  3327. return;
  3328. }
  3329. if (adjustedVisualTargetIndex != visualTargetIndex) {
  3330. // rows moved down, shifted rows need to be moved up
  3331. int start = visualSourceRange.getStart();
  3332. updateRowPositions(newTopRowLogicalIndex + start, start,
  3333. adjustedVisualTargetIndex - start);
  3334. } else {
  3335. // rows moved up, shifted rows need to be repositioned
  3336. // unless it's just a recycling and no spacer heights
  3337. // above got updated
  3338. if (recycledToTop) {
  3339. // rows below the shifted ones need to be moved up (which is
  3340. // done in the next helper method) but the shifted rows
  3341. // themselves are already where they belong
  3342. // (this should only be done if no spacers were added, but
  3343. // we can't know that yet so we'll have to adjust for them
  3344. // afterwards if any do appear)
  3345. return;
  3346. }
  3347. int start = adjustedVisualTargetIndex
  3348. + visualSourceRange.length();
  3349. updateRowPositions(newTopRowLogicalIndex + start, start,
  3350. visualSourceRange.getEnd() - start);
  3351. }
  3352. }
  3353. /**
  3354. * If necessary, reposition the rows that are below those rows that got
  3355. * moved or shifted for
  3356. * {@link #moveAndUpdateEscalatorRows(Range, int, int)}
  3357. * <p>
  3358. * NOTE: This method should not be called directly from anywhere else.
  3359. *
  3360. * @param visualSourceRange
  3361. * @param visualTargetIndex
  3362. * @param adjustedVisualTargetIndex
  3363. * @param newTopRowLogicalIndex
  3364. * @param scrolling
  3365. * @param recycledToOrFromTop
  3366. * @param spacerHeightsChanged
  3367. */
  3368. private void repositionRowsBelowMovedAndShiftedIfNeeded(
  3369. Range visualSourceRange, int visualTargetIndex,
  3370. int adjustedVisualTargetIndex, int newTopRowLogicalIndex,
  3371. boolean scrolling, boolean recycledToOrFromTop,
  3372. boolean spacerHeightsChanged) {
  3373. /*
  3374. * There is no need to check if any rows preceding the source and
  3375. * target range need their positions adjusted, but rows below both
  3376. * may very well need it if spacer heights changed or rows got
  3377. * inserted or removed instead of just moved around.
  3378. *
  3379. * When scrolling to either direction all the rows already got
  3380. * processed by earlier stages, there are no unprocessed rows left
  3381. * either above or below.
  3382. */
  3383. if (!scrolling && (recycledToOrFromTop || spacerHeightsChanged)) {
  3384. int firstBelow;
  3385. if (adjustedVisualTargetIndex != visualTargetIndex) {
  3386. // rows moved down
  3387. firstBelow = adjustedVisualTargetIndex
  3388. + visualSourceRange.length();
  3389. } else {
  3390. // rows moved up
  3391. firstBelow = visualSourceRange.getEnd();
  3392. }
  3393. updateRowPositions(newTopRowLogicalIndex + firstBelow,
  3394. firstBelow, visualRowOrder.size() - firstBelow);
  3395. }
  3396. }
  3397. @Override
  3398. public void updateRowPositions(int index, int numberOfRows) {
  3399. Range visibleRowRange = getVisibleRowRange();
  3400. Range rangeToUpdate = Range.withLength(index, numberOfRows);
  3401. Range intersectingRange = visibleRowRange
  3402. .partitionWith(rangeToUpdate)[1];
  3403. if (intersectingRange.isEmpty()) {
  3404. // no overlap with the visual range, ignore the positioning
  3405. return;
  3406. }
  3407. int adjustedIndex = intersectingRange.getStart();
  3408. int adjustedVisualIndex = adjustedIndex - getTopRowLogicalIndex();
  3409. updateRowPositions(adjustedIndex, adjustedVisualIndex,
  3410. intersectingRange.length());
  3411. // make sure there is no unnecessary gap
  3412. adjustScrollPositionIfNeeded();
  3413. scroller.recalculateScrollbarsForVirtualViewport();
  3414. }
  3415. /**
  3416. * Re-calculates and updates the positions of rows and spacers within
  3417. * the given range. Doesn't touch the scroll positions.
  3418. *
  3419. * @param logicalIndex
  3420. * logical index of the first row to reposition
  3421. * @param visualIndex
  3422. * visual index of the first row to reposition
  3423. * @param numberOfRows
  3424. * the number of rows to reposition
  3425. */
  3426. private void updateRowPositions(int logicalIndex, int visualIndex,
  3427. int numberOfRows) {
  3428. double newRowTop = getRowTop(logicalIndex);
  3429. for (int i = 0; i < numberOfRows; ++i) {
  3430. TableRowElement tr = visualRowOrder.get(visualIndex + i);
  3431. setRowPosition(tr, 0, newRowTop);
  3432. newRowTop += getDefaultRowHeight();
  3433. SpacerContainer.SpacerImpl spacer = spacerContainer
  3434. .getSpacer(logicalIndex + i);
  3435. if (spacer != null) {
  3436. spacer.setPosition(0, newRowTop);
  3437. newRowTop += spacer.getHeight();
  3438. }
  3439. }
  3440. }
  3441. /**
  3442. * Checks whether there is an unexpected gap below the visible rows and
  3443. * adjusts the viewport if necessary.
  3444. */
  3445. private void adjustScrollPositionIfNeeded() {
  3446. double scrollTop = getScrollTop();
  3447. int firstBelowVisualRange = getTopRowLogicalIndex()
  3448. + visualRowOrder.size();
  3449. double gapBelow = scrollTop + getHeightOfSection()
  3450. - getRowTop(firstBelowVisualRange);
  3451. boolean bufferRowNeeded = gapBelow == 0
  3452. && firstBelowVisualRange < getRowCount();
  3453. if (scrollTop > 0 && (gapBelow > 0 || bufferRowNeeded)) {
  3454. /*
  3455. * This situation can be reached e.g. by removing a spacer.
  3456. * Scroll position must be adjusted accordingly but no more than
  3457. * there is room to scroll up. If a buffer row is needed make
  3458. * sure the last row ends up at least slightly below the
  3459. * viewport.
  3460. */
  3461. double adjustedGap = Math.max(gapBelow,
  3462. bufferRowNeeded ? 1 : 0);
  3463. double yDeltaScroll = Math.min(adjustedGap, scrollTop);
  3464. moveViewportAndContent(null, 0, 0, -yDeltaScroll);
  3465. }
  3466. }
  3467. /**
  3468. * Adjust the scroll position and move the contained rows.
  3469. * <p>
  3470. * The difference between using this method and simply scrolling is that
  3471. * this method "takes the rows and spacers with it" and renders them
  3472. * appropriately. The viewport may be scrolled any arbitrary amount, and
  3473. * the contents are moved appropriately, but always snapped into a
  3474. * plausible place.
  3475. * <p>
  3476. * <dl>
  3477. * <dt>Example 1</dt>
  3478. * <dd>An Escalator with default row height 20px. Adjusting the scroll
  3479. * position with 7.5px will move the viewport 7.5px down, but leave the
  3480. * row where it is.</dd>
  3481. * <dt>Example 2</dt>
  3482. * <dd>An Escalator with default row height 20px. Adjusting the scroll
  3483. * position with 27.5px will move the viewport 27.5px down, and place
  3484. * the row at 20px.</dd>
  3485. * </dl>
  3486. *
  3487. * @deprecated This method isn't used by Escalator anymore since Vaadin
  3488. * 8.9 and the general row handling logic has been
  3489. * rewritten, so attempting to call this method may lead to
  3490. * unexpected consequences. This method is likely to get
  3491. * removed soon.
  3492. * @param yDelta
  3493. * the delta of pixels by which to move the viewport and
  3494. * content. A positive value moves everything downwards,
  3495. * while a negative value moves everything upwards
  3496. */
  3497. @Deprecated
  3498. public void moveViewportAndContent(final double yDelta) {
  3499. if (yDelta == 0) {
  3500. return;
  3501. }
  3502. double newTop = tBodyScrollTop + yDelta;
  3503. verticalScrollbar.setScrollPos(newTop);
  3504. final double defaultRowHeight = getDefaultRowHeight();
  3505. double rowPxDelta = yDelta - (yDelta % defaultRowHeight);
  3506. int rowIndexDelta = (int) (yDelta / defaultRowHeight);
  3507. if (!WidgetUtil.pixelValuesEqual(rowPxDelta, 0)) {
  3508. Collection<SpacerContainer.SpacerImpl> spacers = spacerContainer
  3509. .getSpacersAfterPx(tBodyScrollTop,
  3510. SpacerInclusionStrategy.PARTIAL);
  3511. for (SpacerContainer.SpacerImpl spacer : spacers) {
  3512. spacer.setPositionDiff(0, rowPxDelta);
  3513. spacer.setRowIndex(spacer.getRow() + rowIndexDelta);
  3514. }
  3515. for (TableRowElement tr : visualRowOrder) {
  3516. setRowPosition(tr, 0, getRowTop(tr) + rowPxDelta);
  3517. }
  3518. }
  3519. setBodyScrollPosition(tBodyScrollLeft, newTop);
  3520. }
  3521. /**
  3522. * Move rows, spacers, and/or viewport up or down. For rows and spacers
  3523. * either everything within visual range is affected (index
  3524. * {@code null}) or only those from the given row index forward.
  3525. * <p>
  3526. * This method does not update spacer indexes.
  3527. *
  3528. * @param index
  3529. * the logical index from which forward the rows and spacers
  3530. * should be updated, or {@code null} if all of them
  3531. * @param yDeltaRows
  3532. * how much rows should be shifted in pixels
  3533. * @param yDeltaSpacers
  3534. * how much spacers should be shifted in pixels
  3535. * @param yDeltaScroll
  3536. * how much scroll position should be shifted in pixels
  3537. */
  3538. private void moveViewportAndContent(Integer index,
  3539. final double yDeltaRows, final double yDeltaSpacers,
  3540. final double yDeltaScroll) {
  3541. if (!WidgetUtil.pixelValuesEqual(yDeltaScroll, 0d)) {
  3542. double newTop = tBodyScrollTop + yDeltaScroll;
  3543. verticalScrollbar.setScrollPos(newTop);
  3544. setBodyScrollPosition(tBodyScrollLeft, newTop);
  3545. }
  3546. if (!WidgetUtil.pixelValuesEqual(yDeltaSpacers, 0d)) {
  3547. Collection<SpacerContainer.SpacerImpl> spacers;
  3548. if (index == null) {
  3549. spacers = spacerContainer.getSpacersAfterPx(tBodyScrollTop,
  3550. SpacerInclusionStrategy.PARTIAL);
  3551. } else {
  3552. spacers = spacerContainer.getSpacersForRowAndAfter(index);
  3553. }
  3554. for (SpacerContainer.SpacerImpl spacer : spacers) {
  3555. spacer.setPositionDiff(0, yDeltaSpacers);
  3556. }
  3557. }
  3558. if (!WidgetUtil.pixelValuesEqual(yDeltaRows, 0d)) {
  3559. if (index == null) {
  3560. // move all visible rows to the desired direction
  3561. for (TableRowElement tr : visualRowOrder) {
  3562. setRowPosition(tr, 0, getRowTop(tr) + yDeltaRows);
  3563. }
  3564. } else {
  3565. // move all visible rows, including the index row, to the
  3566. // desired direction
  3567. shiftRowPositions(index - 1, yDeltaRows);
  3568. }
  3569. }
  3570. }
  3571. /**
  3572. * Adds new physical escalator rows to the DOM at the given visual index
  3573. * if there's still a need for more escalator rows.
  3574. * <p>
  3575. * If Escalator already is at (or beyond) max capacity, this method does
  3576. * nothing to the DOM.
  3577. * <p>
  3578. * Calling this method repositions all the rows and spacers below the
  3579. * insertion point.
  3580. *
  3581. * @param visualIndex
  3582. * the index at which to add new escalator rows to DOM
  3583. * @param logicalIndex
  3584. * the logical index that corresponds with the first new
  3585. * escalator row, should usually be the same as visual index
  3586. * because there is still need for new rows, but this is not
  3587. * always the case e.g. if row height is changed
  3588. * @param numberOfRows
  3589. * the number of rows to add at <code>index</code>
  3590. * @return a list of the added rows
  3591. */
  3592. private List<TableRowElement> fillAndPopulateEscalatorRowsIfNeeded(
  3593. final int visualIndex, final int logicalIndex,
  3594. final int numberOfRows) {
  3595. /*
  3596. * We want to maintain enough rows to fill the entire viewport even
  3597. * if their spacers have no height. If their spacers do have height
  3598. * some of these rows may end up outside of the viewport, but that's
  3599. * ok.
  3600. */
  3601. final int escalatorRowsStillFit = getMaxVisibleRowCount()
  3602. - getDomRowCount();
  3603. final int escalatorRowsNeeded = Math.min(numberOfRows,
  3604. escalatorRowsStillFit);
  3605. if (escalatorRowsNeeded > 0) {
  3606. int rowsBeforeAddition = visualRowOrder.size();
  3607. // this is AbstractRowContainer method and not easily overridden
  3608. // to consider logical indexes separately from visual indexes,
  3609. // so as a workaround we create the rows as if those two were
  3610. // the same and then update the contents if needed
  3611. final List<TableRowElement> addedRows = paintInsertStaticRows(
  3612. visualIndex, escalatorRowsNeeded);
  3613. visualRowOrder.addAll(visualIndex, addedRows);
  3614. if (visualIndex != logicalIndex) {
  3615. // row got populated with wrong contents, need to update
  3616. int adjustedLogicalIndex = 0;
  3617. if (visualIndex == 0) {
  3618. // added to the beginning of visual range, use the
  3619. // end of insertion range because the beginning might
  3620. // not fit completely
  3621. adjustedLogicalIndex = logicalIndex + numberOfRows
  3622. - addedRows.size();
  3623. } else {
  3624. // added anywhere else, use the beginning of
  3625. // insertion range and the rest of the rows get
  3626. // recycled below if there is room for them
  3627. adjustedLogicalIndex = logicalIndex;
  3628. }
  3629. for (int i = 0; i < addedRows.size(); ++i) {
  3630. TableRowElement tr = addedRows.get(i);
  3631. refreshRow(tr, adjustedLogicalIndex + i);
  3632. }
  3633. }
  3634. // if something is getting inserted instead of just being
  3635. // brought to visual range, the rows below the insertion point
  3636. // need to have their spacer indexes updated accordingly
  3637. if (logicalIndex >= getTopRowLogicalIndex()
  3638. && visualIndex < rowsBeforeAddition) {
  3639. spacerContainer.updateSpacerIndexesForRowAndAfter(
  3640. logicalIndex, getRowCount(), addedRows.size());
  3641. }
  3642. // update the positions of the added rows and the rows below
  3643. // them
  3644. // TODO: this can lead to moving things around twice in case
  3645. // some rows didn't get new dom rows (e.g. when expanding a
  3646. // TreeGrid node with more children than can fit within the max
  3647. // visual range size), consider moving this update elsewhere
  3648. double rowTop = getRowTop(logicalIndex);
  3649. for (int i = visualIndex; i < visualRowOrder.size(); i++) {
  3650. final TableRowElement tr = visualRowOrder.get(i);
  3651. setRowPosition(tr, 0, rowTop);
  3652. rowTop += getDefaultRowHeight();
  3653. SpacerContainer.SpacerImpl spacer = spacerContainer
  3654. .getSpacer(logicalIndex - visualIndex + i);
  3655. if (spacer != null) {
  3656. spacer.setPosition(0, rowTop);
  3657. rowTop += spacer.getHeight();
  3658. }
  3659. }
  3660. // Execute the registered callback function for newly created
  3661. // rows
  3662. Optional.ofNullable(newEscalatorRowCallback)
  3663. .ifPresent(callback -> callback.accept(addedRows));
  3664. return addedRows;
  3665. } else {
  3666. return Collections.emptyList();
  3667. }
  3668. }
  3669. private int getMaxVisibleRowCount() {
  3670. double heightOfSection = getHeightOfSection();
  3671. // By including the possibly shown scrollbar height, we get a
  3672. // consistent count and do not add/remove rows whenever a scrollbar
  3673. // is shown. Make sure that two extra rows are included for
  3674. // assisting with tab navigation on both sides of the viewport.
  3675. heightOfSection += horizontalScrollbarDeco.getOffsetHeight();
  3676. double defaultRowHeight = getDefaultRowHeight();
  3677. final int maxVisibleRowCount = (int) Math
  3678. .ceil(heightOfSection / defaultRowHeight) + 2;
  3679. /*
  3680. * maxVisibleRowCount can become negative if the headers and footers
  3681. * start to overlap. This is a crazy situation, but Vaadin blinks
  3682. * the components a lot, so it's feasible.
  3683. */
  3684. return Math.max(0, maxVisibleRowCount);
  3685. }
  3686. @Override
  3687. protected void paintRemoveRows(final int index,
  3688. final int numberOfRows) {
  3689. if (numberOfRows == 0) {
  3690. return;
  3691. }
  3692. /*
  3693. * NOTE: this method handles and manipulates logical, visual, and
  3694. * physical indexes a lot. If you don't remember what those mean and
  3695. * how they relate to each other, see the top of this class for
  3696. * Maintenance Notes.
  3697. *
  3698. * At the beginning of this method the logical index of the data
  3699. * provider has already been updated to include the new rows, but
  3700. * visual and physical indexes have not, nor has the spacer indexing
  3701. * been updated, and the topRowLogicalIndex may be out of date as
  3702. * well.
  3703. */
  3704. // logical index of the first old row, also the difference between
  3705. // logical index and visual index before any rows have been removed
  3706. final int oldTopRowLogicalIndex = getTopRowLogicalIndex();
  3707. // length of the visual range before anything gets removed
  3708. final int oldVisualRangeLength = visualRowOrder.size();
  3709. // logical range of the removed rows
  3710. final Range removedRowsLogicalRange = Range.withLength(index,
  3711. numberOfRows);
  3712. // check which parts of the removed range fall within or beyond the
  3713. // visual range
  3714. final Range[] partitions = removedRowsLogicalRange
  3715. .partitionWith(Range.withLength(oldTopRowLogicalIndex,
  3716. oldVisualRangeLength));
  3717. final Range removedLogicalAbove = partitions[0];
  3718. final Range removedLogicalBelow = partitions[2];
  3719. final Range removedLogicalWithin = partitions[1];
  3720. if (removedLogicalBelow.length() == numberOfRows) {
  3721. /*
  3722. * Rows were removed entirely from below the visual range. No
  3723. * rows to recycle or scroll position to adjust, just need to
  3724. * recalculate scrollbar height. No need to touch the spacer
  3725. * indexing or the physical index.
  3726. */
  3727. scroller.recalculateScrollbarsForVirtualViewport();
  3728. // Visual range contents remain the same, no need to fire a
  3729. // RowVisibilityChangeEvent.
  3730. } else if (removedLogicalAbove.length() == numberOfRows) {
  3731. /*
  3732. * Rows were removed entirely from above the visual range. No
  3733. * rows to recycle, just need to update the spacer indexing and
  3734. * the content positions. No need to touch the physical index.
  3735. */
  3736. // update the logical indexes of remaining spacers
  3737. spacerContainer.updateSpacerIndexesForRowAndAfter(
  3738. oldTopRowLogicalIndex,
  3739. oldTopRowLogicalIndex + oldVisualRangeLength,
  3740. -numberOfRows);
  3741. // default height of a single row
  3742. final double defaultRowHeight = getDefaultRowHeight();
  3743. // how much viewport, rows, and spacers should be shifted based
  3744. // on the removed rows, assume there were no spacers to remove
  3745. final double yDelta = numberOfRows * defaultRowHeight;
  3746. // shift everything up
  3747. moveViewportAndContent(null, -yDelta, -yDelta, -yDelta);
  3748. // update the top row logical index according to any removed
  3749. // rows
  3750. updateTopRowLogicalIndex(-numberOfRows);
  3751. // update scrollbar
  3752. scroller.recalculateScrollbarsForVirtualViewport();
  3753. // Visual range contents remain the same, no need to fire a
  3754. // RowVisibilityChangeEvent.
  3755. } else {
  3756. /*
  3757. * Rows are being removed at least partially from within the
  3758. * visual range. This is where things get tricky. We might have
  3759. * to scroll up or down or nowhere at all, depending on the
  3760. * situation.
  3761. */
  3762. // Visual range contents changed, RowVisibilityChangeEvent will
  3763. // be triggered within this method
  3764. paintRemoveRowsWithinVisualRange(index, numberOfRows,
  3765. oldTopRowLogicalIndex, oldVisualRangeLength,
  3766. removedLogicalAbove.length(), removedLogicalWithin);
  3767. }
  3768. }
  3769. /**
  3770. * Row removal handling for {@link #paintRemoveRows(int, int)} when the
  3771. * removed range intersects the visual range at least partially.
  3772. * <p>
  3773. * NOTE: This method should not be called directly from anywhere else.
  3774. *
  3775. * @param index
  3776. * @param numberOfRows
  3777. * @param oldTopRowLogicalIndex
  3778. * @param oldVisualRangeLength
  3779. * @param removedAboveLength
  3780. * @param removedLogicalWithin
  3781. */
  3782. private void paintRemoveRowsWithinVisualRange(int index,
  3783. int numberOfRows, int oldTopRowLogicalIndex,
  3784. int oldVisualRangeLength, int removedAboveLength,
  3785. Range removedLogicalWithin) {
  3786. /*
  3787. * Calculating where the visual range should start after the
  3788. * removals is not entirely trivial.
  3789. *
  3790. * Initially, any rows removed from within the visual range won't
  3791. * affect the top index, even if they are removed from the
  3792. * beginning, as the rows are also removed from the logical index.
  3793. * Likewise we don't need to care about rows removed from below the
  3794. * visual range. On the other hand, any rows removed from above the
  3795. * visual range do shift the index down.
  3796. *
  3797. * However, in all of these cases, if there aren't enough rows below
  3798. * the visual range to replace the content removed from within the
  3799. * visual range, more rows need to be brought in from above the old
  3800. * visual range in turn. This shifts the index down even further.
  3801. */
  3802. // scroll position before any rows or spacers are removed
  3803. double scrollTop = getScrollTop();
  3804. Range removedVisualWithin = convertToVisual(removedLogicalWithin);
  3805. int remainingVisualRangeRowCount = visualRowOrder.size()
  3806. - removedVisualWithin.length();
  3807. int newTopRowLogicalIndex = oldTopRowLogicalIndex
  3808. - removedAboveLength;
  3809. int rowsToIncludeFromBelow = Math.min(
  3810. getRowCount() - newTopRowLogicalIndex
  3811. - remainingVisualRangeRowCount,
  3812. removedLogicalWithin.length());
  3813. int rowsToIncludeFromAbove = removedLogicalWithin.length()
  3814. - rowsToIncludeFromBelow;
  3815. int rowsToRemoveFromDom = 0;
  3816. if (rowsToIncludeFromAbove > 0) {
  3817. // don't try to bring in more rows than exist, it's possible
  3818. // to remove enough rows that visual range won't be full
  3819. // anymore
  3820. rowsToRemoveFromDom = Math
  3821. .max(rowsToIncludeFromAbove - newTopRowLogicalIndex, 0);
  3822. rowsToIncludeFromAbove -= rowsToRemoveFromDom;
  3823. newTopRowLogicalIndex -= rowsToIncludeFromAbove;
  3824. }
  3825. int visualIndexToRemove = Math.max(index - oldTopRowLogicalIndex,
  3826. 0);
  3827. // remove extra dom rows and their spacers if any
  3828. double removedFromDomSpacerHeights = 0d;
  3829. if (rowsToRemoveFromDom > 0) {
  3830. for (int i = 0; i < rowsToRemoveFromDom; ++i) {
  3831. TableRowElement tr = visualRowOrder
  3832. .remove(visualIndexToRemove);
  3833. // logical index of this row before anything got removed
  3834. int logicalRowIndex = oldTopRowLogicalIndex
  3835. + visualIndexToRemove + i;
  3836. double spacerHeight = spacerContainer
  3837. .getSpacerHeight(logicalRowIndex);
  3838. removedFromDomSpacerHeights += spacerHeight;
  3839. spacerContainer.removeSpacer(logicalRowIndex);
  3840. paintRemoveRow(tr, removedVisualWithin.getStart());
  3841. removeRowPosition(tr);
  3842. }
  3843. // update the associated row indexes for remaining spacers,
  3844. // even for those rows that are going to get recycled
  3845. spacerContainer.updateSpacerIndexesForRowAndAfter(
  3846. oldTopRowLogicalIndex + visualIndexToRemove
  3847. + rowsToRemoveFromDom,
  3848. oldTopRowLogicalIndex + oldVisualRangeLength,
  3849. -rowsToRemoveFromDom);
  3850. }
  3851. // add new content from below visual range, if there is any
  3852. if (rowsToIncludeFromBelow > 0) {
  3853. // removed rows are recycled to just below the old visual
  3854. // range, calculate the logical index of the insertion
  3855. // point that is just below the existing rows, taking into
  3856. // account that the indexing has changed with the removal
  3857. int firstBelow = newTopRowLogicalIndex + rowsToIncludeFromAbove
  3858. + remainingVisualRangeRowCount;
  3859. moveAndUpdateEscalatorRows(
  3860. Range.withLength(visualIndexToRemove,
  3861. rowsToIncludeFromBelow),
  3862. visualRowOrder.size(), firstBelow);
  3863. }
  3864. // add new content from above visual range, if there is any
  3865. // -- this is left last because most of the time it isn't even
  3866. // needed
  3867. if (rowsToIncludeFromAbove > 0) {
  3868. moveAndUpdateEscalatorRows(
  3869. Range.withLength(visualIndexToRemove,
  3870. rowsToIncludeFromAbove),
  3871. 0, newTopRowLogicalIndex);
  3872. }
  3873. // recycling updates all relevant row and spacer positions but
  3874. // if we only removed DOM rows and didn't recycle any we still
  3875. // need to shift up the rows below the removal point
  3876. if (rowsToIncludeFromAbove <= 0 && rowsToIncludeFromBelow <= 0) {
  3877. // update the positions for the rows and spacers below the
  3878. // removed ones, assume there is no need to update scroll
  3879. // position since the final check adjusts that if needed
  3880. double yDelta = numberOfRows * getDefaultRowHeight()
  3881. + removedFromDomSpacerHeights;
  3882. moveViewportAndContent(
  3883. newTopRowLogicalIndex + visualIndexToRemove, -yDelta,
  3884. -yDelta, 0);
  3885. }
  3886. setTopRowLogicalIndex(newTopRowLogicalIndex);
  3887. scroller.recalculateScrollbarsForVirtualViewport();
  3888. // calling this method also triggers adding new spacers to the
  3889. // recycled rows, if any are needed
  3890. fireRowVisibilityChangeEvent();
  3891. // populating the spacers might take a while, delay calculations
  3892. // or the viewport might get adjusted too high
  3893. Scheduler.get().scheduleFinally(() -> {
  3894. // make sure there isn't a gap at the bottom after removal
  3895. // and adjust the viewport if there is
  3896. // FIXME: this should be doable with
  3897. // adjustScrollPositionIfNeeded() but it uses current
  3898. // scrollTop, which may have ended in wrong position and
  3899. // results in assuming too big gap and consequently
  3900. // scrolling up too much
  3901. double extraSpaceAtBottom = scrollTop + getHeightOfSection()
  3902. - getRowTop(getTopRowLogicalIndex()
  3903. + visualRowOrder.size());
  3904. if (extraSpaceAtBottom > 0 && scrollTop > 0) {
  3905. // we need to move the viewport up to adjust, while the
  3906. // rows and spacers can remain where they are
  3907. double yDeltaScroll = Math.min(extraSpaceAtBottom,
  3908. scrollTop);
  3909. moveViewportAndContent(null, 0, 0, -yDeltaScroll);
  3910. }
  3911. });
  3912. // update physical index
  3913. sortDomElements();
  3914. }
  3915. @Override
  3916. public int getLogicalRowIndex(final TableRowElement tr) {
  3917. assert tr
  3918. .getParentNode() == root : "The given element isn't a row element in the body";
  3919. int internalIndex = visualRowOrder.indexOf(tr);
  3920. return getTopRowLogicalIndex() + internalIndex;
  3921. }
  3922. @Override
  3923. protected void recalculateSectionHeight() {
  3924. // NOOP for body, since it doesn't make any sense.
  3925. }
  3926. /**
  3927. * Adjusts the row index and number to be relevant for the current
  3928. * virtual viewport.
  3929. * <p>
  3930. * It converts a logical range of rows index to the matching visual
  3931. * range, truncating the resulting range with the viewport.
  3932. * <p>
  3933. * <ul>
  3934. * <li>Escalator contains logical rows 0..100
  3935. * <li>Current viewport showing logical rows 20..29
  3936. * <li>convertToVisual([20..29]) &rarr; [0..9]
  3937. * <li>convertToVisual([15..24]) &rarr; [0..4]
  3938. * <li>convertToVisual([25..29]) &rarr; [5..9]
  3939. * <li>convertToVisual([26..39]) &rarr; [6..9]
  3940. * <li>convertToVisual([0..5]) &rarr; [0..-1] <em>(empty)</em>
  3941. * <li>convertToVisual([35..1]) &rarr; [0..-1] <em>(empty)</em>
  3942. * <li>convertToVisual([0..100]) &rarr; [0..9]
  3943. * </ul>
  3944. *
  3945. * @return a logical range converted to a visual range, truncated to the
  3946. * current viewport. The first visual row has the index 0.
  3947. */
  3948. private Range convertToVisual(final Range logicalRange) {
  3949. if (logicalRange.isEmpty()) {
  3950. return logicalRange;
  3951. } else if (visualRowOrder.isEmpty()) {
  3952. // empty range
  3953. return Range.withLength(0, 0);
  3954. }
  3955. final int currentTopRowIndex = getTopRowLogicalIndex();
  3956. final Range[] partitions = logicalRange
  3957. .partitionWith(getVisibleRowRange());
  3958. final Range insideRange = partitions[1];
  3959. return insideRange.offsetBy(-currentTopRowIndex);
  3960. }
  3961. @Override
  3962. protected String getCellElementTagName() {
  3963. return "td";
  3964. }
  3965. @Override
  3966. protected double getHeightOfSection() {
  3967. final int tableHeight = tableWrapper.getOffsetHeight();
  3968. final double footerHeight = footer.getHeightOfSection();
  3969. final double headerHeight = header.getHeightOfSection();
  3970. double heightOfSection = tableHeight - footerHeight - headerHeight;
  3971. return Math.max(0, heightOfSection);
  3972. }
  3973. @Override
  3974. protected void refreshCells(Range logicalRowRange, Range colRange) {
  3975. Profiler.enter("Escalator.BodyRowContainer.refreshRows");
  3976. final Range visualRange = convertToVisual(logicalRowRange);
  3977. if (!visualRange.isEmpty()) {
  3978. final int firstLogicalRowIndex = getLogicalRowIndex(
  3979. visualRowOrder.getFirst());
  3980. for (int rowNumber = visualRange
  3981. .getStart(); rowNumber < visualRange
  3982. .getEnd(); rowNumber++) {
  3983. refreshRow(visualRowOrder.get(rowNumber),
  3984. firstLogicalRowIndex + rowNumber, colRange);
  3985. }
  3986. }
  3987. Profiler.leave("Escalator.BodyRowContainer.refreshRows");
  3988. }
  3989. @Override
  3990. protected TableRowElement getTrByVisualIndex(final int index)
  3991. throws IndexOutOfBoundsException {
  3992. if (index >= 0 && index < visualRowOrder.size()) {
  3993. return visualRowOrder.get(index);
  3994. } else {
  3995. throw new IndexOutOfBoundsException(
  3996. "No such visual index: " + index);
  3997. }
  3998. }
  3999. @Override
  4000. public TableRowElement getRowElement(int index) {
  4001. if (index < 0 || index >= getRowCount()) {
  4002. throw new IndexOutOfBoundsException(
  4003. "No such logical index: " + index);
  4004. }
  4005. int visualIndex = index
  4006. - getLogicalRowIndex(visualRowOrder.getFirst());
  4007. if (visualIndex >= 0 && visualIndex < visualRowOrder.size()) {
  4008. return super.getRowElement(visualIndex);
  4009. } else {
  4010. throw new IllegalStateException("Row with logical index "
  4011. + index + " is currently not available in the DOM");
  4012. }
  4013. }
  4014. private void setBodyScrollPosition(final double scrollLeft,
  4015. final double scrollTop) {
  4016. tBodyScrollLeft = scrollLeft;
  4017. tBodyScrollTop = scrollTop;
  4018. position.set(bodyElem, -tBodyScrollLeft, -tBodyScrollTop);
  4019. position.set(spacerDecoContainer, 0, -tBodyScrollTop);
  4020. }
  4021. /**
  4022. * Make sure that there is a correct amount of escalator rows: Add more
  4023. * if needed, or remove any superfluous ones.
  4024. * <p>
  4025. * This method should be called when e.g. the height of the Escalator
  4026. * changes.
  4027. * <p>
  4028. * <em>Note:</em> This method will make sure that the escalator rows are
  4029. * placed in the proper places. By default new rows are added below, but
  4030. * if the content is scrolled down, the rows are populated on top
  4031. * instead.
  4032. */
  4033. public void verifyEscalatorCount() {
  4034. /*
  4035. * This method indeed has a smell very similar to paintRemoveRows
  4036. * and paintInsertRows.
  4037. *
  4038. * Unfortunately, the code of those can't trivially be shared, since
  4039. * there are some slight differences in the respective
  4040. * responsibilities. The "paint" methods fake the addition and
  4041. * removal of rows, and make sure to either push existing data out
  4042. * of view, or draw new data into view. Only in some special cases
  4043. * will the DOM element count change.
  4044. *
  4045. * This method, however, has the explicit responsibility to verify
  4046. * that when "something" happens, we still have the correct amount
  4047. * of escalator rows in the DOM, and if not, we make sure to modify
  4048. * that count. Only in some special cases do we need to take into
  4049. * account other things than simply modifying the DOM element count.
  4050. */
  4051. Profiler.enter("Escalator.BodyRowContainer.verifyEscalatorCount");
  4052. if (!isAttached()) {
  4053. return;
  4054. }
  4055. int oldTopRowLogicalIndex = getTopRowLogicalIndex();
  4056. int oldVisualRangeLength = visualRowOrder.size();
  4057. final int maxVisibleRowCount = getMaxVisibleRowCount();
  4058. final int neededEscalatorRows = Math.min(maxVisibleRowCount,
  4059. body.getRowCount());
  4060. final int rowDiff = neededEscalatorRows - oldVisualRangeLength;
  4061. if (rowDiff > 0) {
  4062. // more rows are needed
  4063. // calculate the indexes for adding rows below the last row of
  4064. // the visual range
  4065. final int visualTargetIndex = oldVisualRangeLength;
  4066. final int logicalTargetIndex;
  4067. if (!visualRowOrder.isEmpty()) {
  4068. logicalTargetIndex = oldTopRowLogicalIndex
  4069. + visualTargetIndex;
  4070. } else {
  4071. logicalTargetIndex = 0;
  4072. }
  4073. // prioritise adding to the bottom so that there's less chance
  4074. // for a gap if a details row is later closed (e.g. by user)
  4075. final int addToBottom = Math.min(rowDiff,
  4076. getRowCount() - logicalTargetIndex);
  4077. final int addToTop = Math.max(rowDiff - addToBottom, 0);
  4078. if (addToTop > 0) {
  4079. fillAndPopulateEscalatorRowsIfNeeded(0,
  4080. oldTopRowLogicalIndex - addToTop, addToTop);
  4081. updateTopRowLogicalIndex(-addToTop);
  4082. }
  4083. if (addToBottom > 0) {
  4084. // take into account that rows may have got added to top as
  4085. // well, affects visual but not logical indexing
  4086. fillAndPopulateEscalatorRowsIfNeeded(
  4087. visualTargetIndex + addToTop, logicalTargetIndex,
  4088. addToBottom);
  4089. // adding new rows due to resizing may have created a gap in
  4090. // the middle, check whether the existing rows need moving
  4091. double rowTop = getRowTop(oldTopRowLogicalIndex);
  4092. if (rowTop > getRowTop(visualRowOrder.get(addToTop))) {
  4093. for (int i = addToTop; i < visualTargetIndex; i++) {
  4094. final TableRowElement tr = visualRowOrder.get(i);
  4095. setRowPosition(tr, 0, rowTop);
  4096. rowTop += getDefaultRowHeight();
  4097. SpacerContainer.SpacerImpl spacer = spacerContainer
  4098. .getSpacer(oldTopRowLogicalIndex + i);
  4099. if (spacer != null) {
  4100. spacer.setPosition(0, rowTop);
  4101. rowTop += spacer.getHeight();
  4102. }
  4103. }
  4104. }
  4105. }
  4106. } else if (rowDiff < 0) {
  4107. // rows need to be removed
  4108. // prioritise removing rows from above the viewport as they are
  4109. // less likely to be needed in a hurry -- the rows below are
  4110. // more likely to slide into view when spacer contents are
  4111. // updated
  4112. // top of visible area before any rows are actually added
  4113. double scrollTop = getScrollTop();
  4114. // visual index of the first actually visible row, including
  4115. // spacer
  4116. int oldFirstVisibleVisualIndex = -1;
  4117. ListIterator<TableRowElement> iter = visualRowOrder
  4118. .listIterator(0);
  4119. for (int i = 0; i < visualRowOrder.size(); ++i) {
  4120. if (positions.getTop(iter.next()) > scrollTop) {
  4121. break;
  4122. }
  4123. oldFirstVisibleVisualIndex = i;
  4124. }
  4125. int rowsToRemoveFromAbove = Math.max(0, Math
  4126. .min(Math.abs(rowDiff), oldFirstVisibleVisualIndex));
  4127. boolean spacersRemovedFromAbove = false;
  4128. if (rowsToRemoveFromAbove > 0) {
  4129. double initialSpacerHeightSum = spacerContainer
  4130. .getSpacerHeightsSum();
  4131. iter = visualRowOrder.listIterator(0);
  4132. for (int i = 0; i < rowsToRemoveFromAbove; ++i) {
  4133. final Element first = iter.next();
  4134. first.removeFromParent();
  4135. iter.remove();
  4136. spacerContainer.removeSpacer(oldTopRowLogicalIndex + i);
  4137. }
  4138. spacersRemovedFromAbove = initialSpacerHeightSum != spacerContainer
  4139. .getSpacerHeightsSum();
  4140. }
  4141. // if there weren't enough rows above, remove the rest from
  4142. // below
  4143. int rowsToRemoveFromBelow = Math.abs(rowDiff)
  4144. - rowsToRemoveFromAbove;
  4145. if (rowsToRemoveFromBelow > 0) {
  4146. iter = visualRowOrder.listIterator(visualRowOrder.size());
  4147. for (int i = 1; i <= rowsToRemoveFromBelow; ++i) {
  4148. final Element last = iter.previous();
  4149. last.removeFromParent();
  4150. iter.remove();
  4151. spacerContainer.removeSpacer(oldTopRowLogicalIndex
  4152. + oldVisualRangeLength - i);
  4153. }
  4154. }
  4155. updateTopRowLogicalIndex(rowsToRemoveFromAbove);
  4156. if (spacersRemovedFromAbove) {
  4157. updateRowPositions(oldTopRowLogicalIndex, 0,
  4158. visualRowOrder.size());
  4159. }
  4160. // removing rows might cause a gap at the bottom
  4161. adjustScrollPositionIfNeeded();
  4162. }
  4163. if (rowDiff != 0) {
  4164. scroller.recalculateScrollbarsForVirtualViewport();
  4165. fireRowVisibilityChangeEvent();
  4166. }
  4167. Profiler.leave("Escalator.BodyRowContainer.verifyEscalatorCount");
  4168. }
  4169. @Override
  4170. protected void reapplyDefaultRowHeights() {
  4171. if (visualRowOrder.isEmpty()) {
  4172. return;
  4173. }
  4174. Profiler.enter(
  4175. "Escalator.BodyRowContainer.reapplyDefaultRowHeights");
  4176. double spacerHeightsAboveViewport = spacerContainer
  4177. .getSpacerHeightsSumUntilPx(
  4178. verticalScrollbar.getScrollPos());
  4179. double allSpacerHeights = spacerContainer.getSpacerHeightsSum();
  4180. /* step 1: resize and reposition rows */
  4181. // there should be no spacers above the visual range
  4182. double spacerHeights = 0;
  4183. for (int i = 0; i < visualRowOrder.size(); i++) {
  4184. TableRowElement tr = visualRowOrder.get(i);
  4185. reapplyRowHeight(tr, getDefaultRowHeight());
  4186. final int logicalIndex = getTopRowLogicalIndex() + i;
  4187. double y = logicalIndex * getDefaultRowHeight() + spacerHeights;
  4188. setRowPosition(tr, 0, y);
  4189. SpacerContainer.SpacerImpl spacer = spacerContainer
  4190. .getSpacer(logicalIndex);
  4191. if (spacer != null) {
  4192. spacer.setPosition(0, y + getDefaultRowHeight());
  4193. spacerHeights += spacer.getHeight();
  4194. }
  4195. }
  4196. /*
  4197. * step 2: move scrollbar so that it corresponds to its previous
  4198. * place
  4199. */
  4200. // scrollRatio has to be calculated without spacers for it to be
  4201. // comparable between different row heights
  4202. double scrollRatio = (verticalScrollbar.getScrollPos()
  4203. - spacerHeightsAboveViewport)
  4204. / (verticalScrollbar.getScrollSize() - allSpacerHeights);
  4205. scroller.recalculateScrollbarsForVirtualViewport();
  4206. // spacer heights have to be added back for setting new scrollPos
  4207. verticalScrollbar.setScrollPos(
  4208. (int) ((getDefaultRowHeight() * getRowCount() * scrollRatio)
  4209. + spacerHeightsAboveViewport));
  4210. setBodyScrollPosition(horizontalScrollbar.getScrollPos(),
  4211. verticalScrollbar.getScrollPos());
  4212. scroller.onScroll();
  4213. /*
  4214. * step 3: make sure we have the correct amount of escalator rows.
  4215. */
  4216. verifyEscalatorCount();
  4217. Profiler.leave(
  4218. "Escalator.BodyRowContainer.reapplyDefaultRowHeights");
  4219. }
  4220. /**
  4221. * Sorts the rows in the DOM to correspond to the visual order.
  4222. *
  4223. * @see #visualRowOrder
  4224. */
  4225. private void sortDomElements() {
  4226. final String profilingName = "Escalator.BodyRowContainer.sortDomElements";
  4227. Profiler.enter(profilingName);
  4228. /*
  4229. * Focus is lost from an element if that DOM element is (or any of
  4230. * its parents are) removed from the document. Therefore, we sort
  4231. * everything around that row instead.
  4232. */
  4233. final TableRowElement focusedRow = getRowWithFocus();
  4234. if (focusedRow != null) {
  4235. assert focusedRow
  4236. .getParentElement() == root : "Trying to sort around a row that doesn't exist in body";
  4237. assert visualRowOrder.contains(focusedRow)
  4238. || body.spacerContainer.isSpacer(
  4239. focusedRow) : "Trying to sort around a row that doesn't exist in visualRowOrder or is not a spacer.";
  4240. }
  4241. /*
  4242. * Two cases handled simultaneously:
  4243. *
  4244. * 1) No focus on rows. We iterate visualRowOrder backwards, and
  4245. * take the respective element in the DOM, and place it as the first
  4246. * child in the body element. Then we take the next-to-last from
  4247. * visualRowOrder, and put that first, pushing the previous row as
  4248. * the second child. And so on...
  4249. *
  4250. * 2) Focus on some row within Escalator body. Again, we iterate
  4251. * visualRowOrder backwards. This time, we use the focused row as a
  4252. * pivot: Instead of placing rows from the bottom of visualRowOrder
  4253. * and placing it first, we place it underneath the focused row.
  4254. * Once we hit the focused row, we don't move it (to not reset
  4255. * focus) but change sorting mode. After that, we place all rows as
  4256. * the first child.
  4257. */
  4258. List<TableRowElement> orderedBodyRows = new ArrayList<>(
  4259. visualRowOrder);
  4260. Map<Integer, SpacerContainer.SpacerImpl> spacers = body.spacerContainer
  4261. .getSpacers();
  4262. /*
  4263. * Start at -1 to include a spacer that is rendered above the
  4264. * viewport, but its parent row is still not shown
  4265. */
  4266. for (int i = -1; i < visualRowOrder.size(); i++) {
  4267. SpacerContainer.SpacerImpl spacer = spacers
  4268. .remove(Integer.valueOf(getTopRowLogicalIndex() + i));
  4269. if (spacer != null) {
  4270. orderedBodyRows.add(i + 1, spacer.getRootElement());
  4271. spacer.show();
  4272. }
  4273. }
  4274. /*
  4275. * At this point, invisible spacers aren't reordered, so their
  4276. * position in the DOM will remain undefined.
  4277. */
  4278. // If a spacer was not reordered, it means that it's out of visual
  4279. // range. This should never happen with default Grid implementations
  4280. // but it's possible on an extended Escalator.
  4281. for (SpacerContainer.SpacerImpl unmovedSpacer : spacers.values()) {
  4282. unmovedSpacer.hide();
  4283. }
  4284. /*
  4285. * If we have a focused row, start in the mode where we put
  4286. * everything underneath that row. Otherwise, all rows are placed as
  4287. * first child.
  4288. */
  4289. boolean insertFirst = (focusedRow == null);
  4290. final ListIterator<TableRowElement> i = orderedBodyRows
  4291. .listIterator(orderedBodyRows.size());
  4292. while (i.hasPrevious()) {
  4293. TableRowElement tr = i.previous();
  4294. if (tr == focusedRow) {
  4295. insertFirst = true;
  4296. } else if (insertFirst) {
  4297. // remove row explicitly to work around an IE11 bug (#9850)
  4298. if (BrowserInfo.get().isIE11()
  4299. && tr.equals(root.getFirstChildElement())) {
  4300. root.removeChild(tr);
  4301. }
  4302. root.insertFirst(tr);
  4303. } else {
  4304. root.insertAfter(tr, focusedRow);
  4305. }
  4306. }
  4307. Profiler.leave(profilingName);
  4308. }
  4309. /**
  4310. * Get the {@literal <tbody>} row that contains (or has) focus.
  4311. *
  4312. * @return The {@literal <tbody>} row that contains a focused DOM
  4313. * element, or <code>null</code> if focus is outside of a body
  4314. * row.
  4315. */
  4316. private TableRowElement getRowWithFocus() {
  4317. TableRowElement rowContainingFocus = null;
  4318. final Element focusedElement = WidgetUtil.getFocusedElement();
  4319. if (focusedElement != null && root.isOrHasChild(focusedElement)) {
  4320. Element e = focusedElement;
  4321. while (e != null && e != root) {
  4322. /*
  4323. * You never know if there's several tables embedded in a
  4324. * cell... We'll take the deepest one.
  4325. */
  4326. if (TableRowElement.is(e)) {
  4327. rowContainingFocus = TableRowElement.as(e);
  4328. }
  4329. e = e.getParentElement();
  4330. }
  4331. }
  4332. return rowContainingFocus;
  4333. }
  4334. /**
  4335. * Returns the cell object which contains information about the cell or
  4336. * spacer the element is in. As an implementation detail each spacer is
  4337. * a row with one cell, but they are stored in their own container and
  4338. * share the indexing with the regular rows.
  4339. *
  4340. * @param element
  4341. * The element to get the cell for. If element is not present
  4342. * in row or spacer container then <code>null</code> is
  4343. * returned.
  4344. *
  4345. * @return the cell reference of the element, or <code>null</code> if
  4346. * element is not present in the {@link RowContainer} or the
  4347. * {@link SpacerContainer}.
  4348. */
  4349. @Override
  4350. public Cell getCell(Element element) {
  4351. Cell cell = super.getCell(element);
  4352. if (cell == null) {
  4353. return null;
  4354. }
  4355. // Convert DOM coordinates to logical coordinates for rows
  4356. TableRowElement rowElement = (TableRowElement) cell.getElement()
  4357. .getParentElement();
  4358. if (!visualRowOrder.contains(rowElement)) {
  4359. for (Entry<Integer, SpacerContainer.SpacerImpl> entry : spacerContainer
  4360. .getSpacers().entrySet()) {
  4361. if (rowElement.equals(entry.getValue().getRootElement())) {
  4362. return new Cell(entry.getKey(), cell.getColumn(),
  4363. cell.getElement());
  4364. }
  4365. }
  4366. return null;
  4367. }
  4368. return new Cell(getLogicalRowIndex(rowElement), cell.getColumn(),
  4369. cell.getElement());
  4370. }
  4371. @Override
  4372. public void setSpacer(int rowIndex, double height)
  4373. throws IllegalArgumentException {
  4374. spacerContainer.setSpacer(rowIndex, height);
  4375. }
  4376. @Override
  4377. public boolean spacerExists(int rowIndex) {
  4378. return spacerContainer.spacerExists(rowIndex);
  4379. }
  4380. @Override
  4381. public void setSpacerUpdater(SpacerUpdater spacerUpdater)
  4382. throws IllegalArgumentException {
  4383. spacerContainer.setSpacerUpdater(spacerUpdater);
  4384. }
  4385. @Override
  4386. public SpacerUpdater getSpacerUpdater() {
  4387. return spacerContainer.getSpacerUpdater();
  4388. }
  4389. /**
  4390. * <em>Calculates</em> the expected top position of a row at a logical
  4391. * index, regardless if there is one there currently or not.
  4392. * <p>
  4393. * This method relies on fixed row height (by
  4394. * {@link #getDefaultRowHeight()}) and can only take into account
  4395. * spacers that are within visual range. Any scrolling might invalidate
  4396. * these results, so this method shouldn't be used to estimate scroll
  4397. * positions.
  4398. *
  4399. * @param logicalIndex
  4400. * the logical index of the row for which to calculate the
  4401. * top position
  4402. * @return the position where the row should currently be, were it to
  4403. * exist
  4404. * @see #getRowTop(TableRowElement)
  4405. */
  4406. private double getRowTop(int logicalIndex) {
  4407. double top = spacerContainer
  4408. .getSpacerHeightsSumUntilIndex(logicalIndex);
  4409. return top + (logicalIndex * getDefaultRowHeight());
  4410. }
  4411. public void shiftRowPositions(int row, double diff) {
  4412. for (TableRowElement tr : getVisibleRowsAfter(row)) {
  4413. setRowPosition(tr, 0, getRowTop(tr) + diff);
  4414. }
  4415. }
  4416. private List<TableRowElement> getVisibleRowsAfter(int logicalRow) {
  4417. Range visibleRowLogicalRange = getVisibleRowRange();
  4418. boolean allRowsAreInView = logicalRow < visibleRowLogicalRange
  4419. .getStart();
  4420. boolean noRowsAreInView = logicalRow >= visibleRowLogicalRange
  4421. .getEnd() - 1;
  4422. if (allRowsAreInView) {
  4423. return Collections.unmodifiableList(visualRowOrder);
  4424. } else if (noRowsAreInView) {
  4425. return Collections.emptyList();
  4426. } else {
  4427. int fromIndex = (logicalRow - visibleRowLogicalRange.getStart())
  4428. + 1;
  4429. int toIndex = visibleRowLogicalRange.length();
  4430. List<TableRowElement> sublist = visualRowOrder
  4431. .subList(fromIndex, toIndex);
  4432. return Collections.unmodifiableList(sublist);
  4433. }
  4434. }
  4435. @Override
  4436. public int getDomRowCount() {
  4437. return root.getChildCount()
  4438. - spacerContainer.getSpacersInDom().size();
  4439. }
  4440. @Override
  4441. protected boolean rowCanBeFrozen(TableRowElement tr) {
  4442. return visualRowOrder.contains(tr);
  4443. }
  4444. void reapplySpacerWidths() {
  4445. spacerContainer.reapplySpacerWidths();
  4446. }
  4447. void scrollToRowSpacerOrBoth(int targetRowIndex,
  4448. ScrollDestination destination, double padding,
  4449. ScrollType scrollType) {
  4450. if (!ensureScrollingAllowed()) {
  4451. return;
  4452. }
  4453. validateScrollDestination(destination, (int) padding);
  4454. // ignore the special case of -1 index spacer from the row index
  4455. // validation
  4456. if (!(targetRowIndex == -1 && !ScrollType.ROW.equals(scrollType))) {
  4457. // throws an IndexOutOfBoundsException if not valid
  4458. verifyValidRowIndex(targetRowIndex);
  4459. }
  4460. int oldTopRowLogicalIndex = getTopRowLogicalIndex();
  4461. int visualRangeLength = visualRowOrder.size();
  4462. int paddingInRows = 0;
  4463. if (!WidgetUtil.pixelValuesEqual(padding, 0d)) {
  4464. paddingInRows = (int) Math
  4465. .ceil(Double.valueOf(padding) / getDefaultRowHeight());
  4466. }
  4467. // calculate the largest index necessary to include at least
  4468. // partially below the top of the viewport and the smallest index
  4469. // necessary to include at least partially above the bottom of the
  4470. // viewport (target row itself might not be if padding is negative)
  4471. int firstVisibleIndexIfScrollingUp = targetRowIndex - paddingInRows;
  4472. int lastVisibleIndexIfScrollingDown = targetRowIndex
  4473. + paddingInRows;
  4474. int oldFirstBelowIndex = oldTopRowLogicalIndex + visualRangeLength;
  4475. int newTopRowLogicalIndex;
  4476. int logicalTargetIndex;
  4477. switch (destination) {
  4478. case ANY:
  4479. // scroll as little as possible, take into account that there
  4480. // needs to be a buffer row at both ends if there is room for
  4481. // one
  4482. boolean newRowsNeededAbove = (firstVisibleIndexIfScrollingUp < oldTopRowLogicalIndex)
  4483. || (firstVisibleIndexIfScrollingUp == oldTopRowLogicalIndex
  4484. && targetRowIndex > 0);
  4485. boolean rowsNeededBelow = (lastVisibleIndexIfScrollingDown >= oldFirstBelowIndex)
  4486. || ((lastVisibleIndexIfScrollingDown == oldFirstBelowIndex
  4487. - 1) && (oldFirstBelowIndex < getRowCount()));
  4488. if (newRowsNeededAbove) {
  4489. // scroll up, add buffer row if it fits
  4490. logicalTargetIndex = Math
  4491. .max(firstVisibleIndexIfScrollingUp - 1, 0);
  4492. newTopRowLogicalIndex = logicalTargetIndex;
  4493. } else if (rowsNeededBelow) {
  4494. // scroll down, add buffer row if it fits
  4495. newTopRowLogicalIndex = Math.min(
  4496. lastVisibleIndexIfScrollingDown + 1,
  4497. getRowCount() - 1) - visualRangeLength + 1;
  4498. if (newTopRowLogicalIndex
  4499. - oldTopRowLogicalIndex < visualRangeLength) {
  4500. // partial recycling, target index at the end of
  4501. // current range
  4502. logicalTargetIndex = oldFirstBelowIndex;
  4503. } else {
  4504. // full recycling, target index the same as the new
  4505. // top row index
  4506. logicalTargetIndex = newTopRowLogicalIndex;
  4507. }
  4508. } else {
  4509. // no need to recycle rows but viewport might need
  4510. // adjusting regardless
  4511. logicalTargetIndex = -1;
  4512. newTopRowLogicalIndex = oldTopRowLogicalIndex;
  4513. }
  4514. break;
  4515. case END:
  4516. // target row at the bottom of the viewport
  4517. newTopRowLogicalIndex = lastVisibleIndexIfScrollingDown + 1
  4518. - visualRangeLength + 1;
  4519. newTopRowLogicalIndex = ensureTopRowLogicalIndexSanity(
  4520. newTopRowLogicalIndex);
  4521. if ((newTopRowLogicalIndex > oldTopRowLogicalIndex)
  4522. && (newTopRowLogicalIndex
  4523. - oldTopRowLogicalIndex < visualRangeLength)) {
  4524. // partial recycling, target index at the end of
  4525. // current range
  4526. logicalTargetIndex = oldFirstBelowIndex;
  4527. } else {
  4528. // full recycling, target index the same as the new
  4529. // top row index
  4530. logicalTargetIndex = newTopRowLogicalIndex;
  4531. }
  4532. break;
  4533. case MIDDLE:
  4534. // target row at the middle of the viewport, padding has to be
  4535. // zero or we never would have reached this far
  4536. newTopRowLogicalIndex = targetRowIndex - visualRangeLength / 2;
  4537. newTopRowLogicalIndex = ensureTopRowLogicalIndexSanity(
  4538. newTopRowLogicalIndex);
  4539. if (newTopRowLogicalIndex < oldTopRowLogicalIndex) {
  4540. logicalTargetIndex = newTopRowLogicalIndex;
  4541. } else if (newTopRowLogicalIndex > oldTopRowLogicalIndex) {
  4542. if (newTopRowLogicalIndex
  4543. - oldTopRowLogicalIndex < visualRangeLength) {
  4544. // partial recycling, target index at the end of
  4545. // current range
  4546. logicalTargetIndex = oldFirstBelowIndex;
  4547. } else {
  4548. // full recycling, target index the same as the new
  4549. // top row index
  4550. logicalTargetIndex = newTopRowLogicalIndex;
  4551. }
  4552. } else {
  4553. logicalTargetIndex = -1;
  4554. }
  4555. break;
  4556. case START:
  4557. // target row at the top of the viewport, include buffer
  4558. // row if there is room for one
  4559. newTopRowLogicalIndex = firstVisibleIndexIfScrollingUp - 1;
  4560. newTopRowLogicalIndex = ensureTopRowLogicalIndexSanity(
  4561. newTopRowLogicalIndex);
  4562. if (getVisibleRowRange().contains(newTopRowLogicalIndex)) {
  4563. logicalTargetIndex = oldTopRowLogicalIndex
  4564. + visualRangeLength;
  4565. } else {
  4566. logicalTargetIndex = newTopRowLogicalIndex;
  4567. }
  4568. break;
  4569. default:
  4570. String msg = "Internal: Unsupported ScrollDestination: ";
  4571. throw new IllegalArgumentException(msg + destination.name());
  4572. }
  4573. // adjust visual range if necessary
  4574. if (newTopRowLogicalIndex < oldTopRowLogicalIndex) {
  4575. adjustVisualRangeUpForScrollToRowSpacerOrBoth(
  4576. oldTopRowLogicalIndex, visualRangeLength,
  4577. logicalTargetIndex);
  4578. } else if (newTopRowLogicalIndex > oldTopRowLogicalIndex) {
  4579. adjustVisualRangeDownForScrollToRowSpacerOrBoth(
  4580. oldTopRowLogicalIndex, visualRangeLength,
  4581. newTopRowLogicalIndex, logicalTargetIndex);
  4582. }
  4583. boolean rowsWereMoved = newTopRowLogicalIndex != oldTopRowLogicalIndex;
  4584. // update scroll position if necessary
  4585. adjustScrollPositionForScrollToRowSpacerOrBoth(targetRowIndex,
  4586. destination, padding, scrollType);
  4587. if (rowsWereMoved) {
  4588. fireRowVisibilityChangeEvent();
  4589. // schedule updating of the physical indexes
  4590. domSorter.reschedule();
  4591. }
  4592. }
  4593. /**
  4594. * Modifies the proposed top row logical index to fit within the logical
  4595. * range and to not leave gaps if it is avoidable.
  4596. *
  4597. * @param proposedTopRowLogicalIndex
  4598. * @return an adjusted index, or the original if no changes were
  4599. * necessary
  4600. */
  4601. private int ensureTopRowLogicalIndexSanity(
  4602. int proposedTopRowLogicalIndex) {
  4603. int newTopRowLogicalIndex = Math.max(proposedTopRowLogicalIndex, 0);
  4604. int visualRangeLength = visualRowOrder.size();
  4605. if (newTopRowLogicalIndex + visualRangeLength > getRowCount()) {
  4606. newTopRowLogicalIndex = getRowCount() - visualRangeLength;
  4607. }
  4608. return newTopRowLogicalIndex;
  4609. }
  4610. /**
  4611. * Checks that scrolling is allowed and resets the scroll position if
  4612. * it's not.
  4613. *
  4614. * @return {@code true} if scrolling is allowed, {@code false} otherwise
  4615. */
  4616. private boolean ensureScrollingAllowed() {
  4617. if (isScrollLocked(Direction.VERTICAL)) {
  4618. // no scrolling can happen
  4619. if (getScrollTop() != tBodyScrollTop) {
  4620. setBodyScrollPosition(tBodyScrollLeft, getScrollTop());
  4621. }
  4622. return false;
  4623. }
  4624. return true;
  4625. }
  4626. /**
  4627. * Adjusts visual range up for
  4628. * {@link #scrollToRowSpacerOrBoth(int, ScrollDestination, double, boolean, boolean)},
  4629. * reuse at your own peril.
  4630. *
  4631. * @param oldTopRowLogicalIndex
  4632. * @param visualRangeLength
  4633. * @param logicalTargetIndex
  4634. */
  4635. private void adjustVisualRangeUpForScrollToRowSpacerOrBoth(
  4636. int oldTopRowLogicalIndex, int visualRangeLength,
  4637. int logicalTargetIndex) {
  4638. // recycle at most the visual range's worth of rows to fill
  4639. // the gap between the new visualTargetIndex and the existing
  4640. // rows
  4641. int rowsToRecycle = Math.min(
  4642. oldTopRowLogicalIndex - logicalTargetIndex,
  4643. visualRangeLength);
  4644. // recycle from the end to the beginning
  4645. moveAndUpdateEscalatorRows(
  4646. Range.withLength(visualRangeLength - rowsToRecycle,
  4647. rowsToRecycle),
  4648. 0, logicalTargetIndex);
  4649. // update the index
  4650. setTopRowLogicalIndex(logicalTargetIndex);
  4651. }
  4652. /**
  4653. * Adjusts visual range down for
  4654. * {@link #scrollToRowSpacerOrBoth(int, ScrollDestination, double, boolean, boolean)},
  4655. * reuse at your own peril.
  4656. *
  4657. * @param oldTopRowLogicalIndex
  4658. * @param visualRangeLength
  4659. * @param newTopRowLogicalIndex
  4660. * @param logicalTargetIndex
  4661. */
  4662. private void adjustVisualRangeDownForScrollToRowSpacerOrBoth(
  4663. int oldTopRowLogicalIndex, int visualRangeLength,
  4664. int newTopRowLogicalIndex, int logicalTargetIndex) {
  4665. // recycle at most the visual range's worth of rows to fill
  4666. // the gap between the new visualTargetIndex and the existing
  4667. // rows
  4668. int rowsToRecycle;
  4669. if (newTopRowLogicalIndex
  4670. - oldTopRowLogicalIndex >= visualRangeLength) {
  4671. // full recycling
  4672. rowsToRecycle = visualRangeLength;
  4673. } else {
  4674. // partial recycling
  4675. rowsToRecycle = newTopRowLogicalIndex - oldTopRowLogicalIndex;
  4676. }
  4677. // recycle from the beginning to the end
  4678. moveAndUpdateEscalatorRows(Range.withLength(0, rowsToRecycle),
  4679. visualRangeLength, logicalTargetIndex);
  4680. // update the index
  4681. setTopRowLogicalIndex(newTopRowLogicalIndex);
  4682. }
  4683. /**
  4684. * Adjusts scroll position for
  4685. * {@link #scrollToRowSpacerOrBoth(int, ScrollDestination, double, boolean, boolean)},
  4686. * reuse at your own peril.
  4687. *
  4688. * @param targetRowIndex
  4689. * @param destination
  4690. * @param padding
  4691. * @param scrollType
  4692. */
  4693. private void adjustScrollPositionForScrollToRowSpacerOrBoth(
  4694. int targetRowIndex, ScrollDestination destination,
  4695. double padding, ScrollType scrollType) {
  4696. /*
  4697. * attempting to scroll above first row or below last row would get
  4698. * automatically corrected later but that causes unnecessary
  4699. * calculations, so try not to overshoot
  4700. */
  4701. double sectionHeight = getHeightOfSection();
  4702. double rowTop = getRowTop(targetRowIndex);
  4703. double spacerHeight = spacerContainer
  4704. .getSpacerHeight(targetRowIndex);
  4705. double scrollTop;
  4706. switch (destination) {
  4707. case ANY:
  4708. if (!ScrollType.SPACER.equals(scrollType)
  4709. && Math.max(rowTop - padding, 0) < getScrollTop()) {
  4710. // within visual range but row top above the viewport or not
  4711. // enough padding, shift a little
  4712. scrollTop = Math.max(rowTop - padding, 0);
  4713. } else if (ScrollType.SPACER.equals(scrollType)
  4714. && Math.max(rowTop + getDefaultRowHeight() - padding,
  4715. 0) < getScrollTop()) {
  4716. // within visual range but spacer top above the viewport or
  4717. // not enough padding, shift a little
  4718. scrollTop = Math
  4719. .max(rowTop + getDefaultRowHeight() - padding, 0);
  4720. } else if (ScrollType.ROW.equals(scrollType)
  4721. && rowTop + getDefaultRowHeight()
  4722. + padding > getScrollTop() + sectionHeight) {
  4723. // within visual range but end of row below the viewport
  4724. // or not enough padding, shift a little
  4725. scrollTop = rowTop + getDefaultRowHeight() - sectionHeight
  4726. + padding;
  4727. // ensure that we don't overshoot beyond bottom
  4728. scrollTop = Math.min(scrollTop,
  4729. getRowTop(getRowCount() - 1) + getDefaultRowHeight()
  4730. + spacerContainer
  4731. .getSpacerHeight(getRowCount() - 1)
  4732. - sectionHeight);
  4733. // if padding is set we want to overshoot or undershoot,
  4734. // otherwise make sure the top of the row is in view
  4735. if (padding == 0) {
  4736. scrollTop = Math.min(scrollTop, rowTop);
  4737. }
  4738. } else if (rowTop + getDefaultRowHeight() + spacerHeight
  4739. + padding > getScrollTop() + sectionHeight) {
  4740. // within visual range but end of spacer below the viewport
  4741. // or not enough padding, shift a little
  4742. scrollTop = rowTop + getDefaultRowHeight() + spacerHeight
  4743. - sectionHeight + padding;
  4744. // ensure that we don't overshoot beyond bottom
  4745. scrollTop = Math.min(scrollTop,
  4746. getRowTop(getRowCount()) - sectionHeight);
  4747. // if padding is set we want to overshoot or undershoot,
  4748. // otherwise make sure the top of the row or spacer is
  4749. // in view
  4750. if (padding == 0) {
  4751. if (ScrollType.SPACER.equals(scrollType)) {
  4752. scrollTop = Math.min(scrollTop,
  4753. rowTop + getDefaultRowHeight());
  4754. } else {
  4755. scrollTop = Math.min(scrollTop, rowTop);
  4756. }
  4757. }
  4758. } else {
  4759. // we are fine where we are
  4760. scrollTop = getScrollTop();
  4761. }
  4762. break;
  4763. case END:
  4764. if (ScrollType.ROW.equals(scrollType)
  4765. && rowTop + getDefaultRowHeight()
  4766. + padding != getScrollTop() + sectionHeight) {
  4767. // row should be at the bottom of the viewport
  4768. scrollTop = rowTop + getDefaultRowHeight() - sectionHeight
  4769. + padding;
  4770. } else if (rowTop + getDefaultRowHeight() + spacerHeight
  4771. + padding != getScrollTop() + sectionHeight) {
  4772. // spacer should be at the bottom of the viewport
  4773. scrollTop = rowTop + getDefaultRowHeight() + spacerHeight
  4774. - sectionHeight + padding;
  4775. } else {
  4776. // we are fine where we are
  4777. scrollTop = getScrollTop();
  4778. }
  4779. break;
  4780. case MIDDLE:
  4781. double center;
  4782. if (ScrollType.ROW.equals(scrollType)) {
  4783. // center the row itself
  4784. center = rowTop + (getDefaultRowHeight() / 2.0);
  4785. } else if (ScrollType.ROW_AND_SPACER.equals(scrollType)) {
  4786. // center both
  4787. center = rowTop
  4788. + ((getDefaultRowHeight() + spacerHeight) / 2.0);
  4789. } else {
  4790. // center the spacer
  4791. center = rowTop + getDefaultRowHeight()
  4792. + (spacerHeight / 2.0);
  4793. }
  4794. scrollTop = center - Math.ceil(sectionHeight / 2.0);
  4795. break;
  4796. case START:
  4797. if (!ScrollType.SPACER.equals(scrollType)
  4798. && Math.max(rowTop - padding, 0) != getScrollTop()) {
  4799. // row should be at the top of the viewport
  4800. scrollTop = Math.max(rowTop - padding, 0);
  4801. } else if (ScrollType.SPACER.equals(scrollType)
  4802. && Math.max(rowTop + getDefaultRowHeight() - padding,
  4803. 0) != getScrollTop()) {
  4804. // spacer should be at the top of the viewport
  4805. scrollTop = Math
  4806. .max(rowTop + getDefaultRowHeight() - padding, 0);
  4807. } else {
  4808. scrollTop = getScrollTop();
  4809. }
  4810. break;
  4811. default:
  4812. scrollTop = getScrollTop();
  4813. }
  4814. // ensure that we don't overshoot beyond bottom
  4815. scrollTop = Math.min(scrollTop,
  4816. getRowTop(getRowCount()) - sectionHeight);
  4817. // ensure that we don't overshoot beyond top
  4818. scrollTop = Math.max(0, scrollTop);
  4819. if (scrollTop != getScrollTop()) {
  4820. setScrollTop(scrollTop);
  4821. setBodyScrollPosition(tBodyScrollLeft, scrollTop);
  4822. }
  4823. }
  4824. @Override
  4825. public void setNewRowCallback(
  4826. Consumer<List<TableRowElement>> callback) {
  4827. newEscalatorRowCallback = callback;
  4828. }
  4829. }
  4830. private class ColumnConfigurationImpl implements ColumnConfiguration {
  4831. public class Column {
  4832. public static final double DEFAULT_COLUMN_WIDTH_PX = 100;
  4833. private double definedWidth = -1;
  4834. private double calculatedWidth = DEFAULT_COLUMN_WIDTH_PX;
  4835. private boolean measuringRequested = false;
  4836. public void setWidth(double px) {
  4837. Profiler.enter(
  4838. "Escalator.ColumnConfigurationImpl.Column.setWidth");
  4839. definedWidth = px;
  4840. if (px < 0) {
  4841. if (isAttached()) {
  4842. calculateWidth();
  4843. } else {
  4844. /*
  4845. * the column's width is calculated at Escalator.onLoad
  4846. * via measureAndSetWidthIfNeeded!
  4847. */
  4848. measuringRequested = true;
  4849. }
  4850. } else {
  4851. calculatedWidth = px;
  4852. }
  4853. Profiler.leave(
  4854. "Escalator.ColumnConfigurationImpl.Column.setWidth");
  4855. }
  4856. public double getDefinedWidth() {
  4857. return definedWidth;
  4858. }
  4859. /**
  4860. * Returns the actual width in the DOM.
  4861. *
  4862. * @return the width in pixels in the DOM. Returns -1 if the column
  4863. * needs measuring, but has not been yet measured
  4864. */
  4865. public double getCalculatedWidth() {
  4866. /*
  4867. * This might return an untrue value (e.g. during init/onload),
  4868. * since we haven't had a proper chance to actually calculate
  4869. * widths yet.
  4870. *
  4871. * This is fixed during Escalator.onLoad, by the call to
  4872. * "measureAndSetWidthIfNeeded", which fixes "everything".
  4873. */
  4874. if (!measuringRequested) {
  4875. return calculatedWidth;
  4876. } else {
  4877. return -1;
  4878. }
  4879. }
  4880. /**
  4881. * Checks if the column needs measuring, and then measures it.
  4882. * <p>
  4883. * Called by {@link Escalator#onLoad()}.
  4884. */
  4885. public boolean measureAndSetWidthIfNeeded() {
  4886. assert isAttached() : "Column.measureAndSetWidthIfNeeded() was called even though Escalator was not attached!";
  4887. if (measuringRequested) {
  4888. measuringRequested = false;
  4889. setWidth(definedWidth);
  4890. return true;
  4891. }
  4892. return false;
  4893. }
  4894. private void calculateWidth() {
  4895. calculatedWidth = getMaxCellWidth(columns.indexOf(this));
  4896. }
  4897. }
  4898. private final List<Column> columns = new ArrayList<>();
  4899. private int frozenColumns = 0;
  4900. /*
  4901. * TODO: this is a bit of a duplicate functionality with the
  4902. * Column.calculatedWidth caching. Probably should use one or the other,
  4903. * not both
  4904. */
  4905. /**
  4906. * A cached array of all the calculated column widths.
  4907. *
  4908. * @see #getCalculatedColumnWidths()
  4909. */
  4910. private double[] widthsArray = null;
  4911. /**
  4912. * {@inheritDoc}
  4913. * <p>
  4914. * <em>Implementation detail:</em> This method does no DOM modifications
  4915. * (i.e. is very cheap to call) if there are no rows in the DOM when
  4916. * this method is called.
  4917. *
  4918. * @see #hasSomethingInDom()
  4919. */
  4920. @Override
  4921. public void removeColumns(final int index, final int numberOfColumns) {
  4922. if (numberOfColumns == 0) {
  4923. return;
  4924. }
  4925. // Validate
  4926. assertArgumentsAreValidAndWithinRange(index, numberOfColumns);
  4927. // Move the horizontal scrollbar to the left, if removed columns are
  4928. // to the left of the viewport
  4929. removeColumnsAdjustScrollbar(index, numberOfColumns);
  4930. // Remove from DOM
  4931. header.paintRemoveColumns(index, numberOfColumns);
  4932. body.paintRemoveColumns(index, numberOfColumns);
  4933. footer.paintRemoveColumns(index, numberOfColumns);
  4934. // Remove from bookkeeping
  4935. flyweightRow.removeCells(index, numberOfColumns);
  4936. columns.subList(index, index + numberOfColumns).clear();
  4937. // Adjust frozen columns
  4938. if (index < getFrozenColumnCount()) {
  4939. if (index + numberOfColumns < frozenColumns) {
  4940. /*
  4941. * Last removed column was frozen, meaning that all removed
  4942. * columns were frozen. Just decrement the number of frozen
  4943. * columns accordingly.
  4944. */
  4945. frozenColumns -= numberOfColumns;
  4946. } else {
  4947. /*
  4948. * If last removed column was not frozen, we have removed
  4949. * columns beyond the frozen range, so all remaining frozen
  4950. * columns are to the left of the removed columns.
  4951. */
  4952. frozenColumns = index;
  4953. }
  4954. }
  4955. scroller.recalculateScrollbarsForVirtualViewport();
  4956. body.verifyEscalatorCount();
  4957. if (getColumnConfiguration().getColumnCount() > 0) {
  4958. reapplyRowWidths(header);
  4959. reapplyRowWidths(body);
  4960. reapplyRowWidths(footer);
  4961. }
  4962. /*
  4963. * Colspans make any kind of automatic clever content re-rendering
  4964. * impossible: As soon as anything has colspans, removing one might
  4965. * reveal further colspans, modifying the DOM structure once again,
  4966. * ending in a cascade of updates. Because we don't know how the
  4967. * data is updated.
  4968. *
  4969. * So, instead, we don't do anything. The client code is responsible
  4970. * for re-rendering the content (if so desired). Everything Just
  4971. * Works (TM) if colspans aren't used.
  4972. */
  4973. }
  4974. private void reapplyRowWidths(AbstractRowContainer container) {
  4975. if (container.getRowCount() > 0) {
  4976. container.reapplyRowWidths();
  4977. }
  4978. }
  4979. private void removeColumnsAdjustScrollbar(int index,
  4980. int numberOfColumns) {
  4981. if (horizontalScrollbar.getOffsetSize() >= horizontalScrollbar
  4982. .getScrollSize()) {
  4983. return;
  4984. }
  4985. double leftPosOfFirstColumnToRemove = getCalculatedColumnsWidth(
  4986. Range.between(0, index));
  4987. double widthOfColumnsToRemove = getCalculatedColumnsWidth(
  4988. Range.withLength(index, numberOfColumns));
  4989. double scrollLeft = horizontalScrollbar.getScrollPos();
  4990. if (scrollLeft <= leftPosOfFirstColumnToRemove) {
  4991. /*
  4992. * viewport is scrolled to the left of the first removed column,
  4993. * so there's no need to adjust anything
  4994. */
  4995. return;
  4996. }
  4997. double adjustedScrollLeft = Math.max(leftPosOfFirstColumnToRemove,
  4998. scrollLeft - widthOfColumnsToRemove);
  4999. horizontalScrollbar.setScrollPos(adjustedScrollLeft);
  5000. }
  5001. /**
  5002. * Calculate the width of a row, as the sum of columns' widths.
  5003. *
  5004. * @return the width of a row, in pixels
  5005. */
  5006. public double calculateRowWidth() {
  5007. return getCalculatedColumnsWidth(
  5008. Range.between(0, getColumnCount()));
  5009. }
  5010. private void assertArgumentsAreValidAndWithinRange(final int index,
  5011. final int numberOfColumns) {
  5012. if (numberOfColumns < 1) {
  5013. throw new IllegalArgumentException(
  5014. "Number of columns can't be less than 1 (was "
  5015. + numberOfColumns + ")");
  5016. }
  5017. if (index < 0 || index + numberOfColumns > getColumnCount()) {
  5018. throw new IndexOutOfBoundsException("The given "
  5019. + "column range (" + index + ".."
  5020. + (index + numberOfColumns)
  5021. + ") was outside of the current "
  5022. + "number of columns (" + getColumnCount() + ")");
  5023. }
  5024. }
  5025. /**
  5026. * {@inheritDoc}
  5027. * <p>
  5028. * <em>Implementation detail:</em> This method does no DOM modifications
  5029. * (i.e. is very cheap to call) if there is no data for rows when this
  5030. * method is called.
  5031. *
  5032. * @see #hasColumnAndRowData()
  5033. */
  5034. @Override
  5035. public void insertColumns(final int index, final int numberOfColumns) {
  5036. if (numberOfColumns == 0) {
  5037. return;
  5038. }
  5039. // Validate
  5040. if (index < 0 || index > getColumnCount()) {
  5041. throw new IndexOutOfBoundsException("The given index(" + index
  5042. + ") was outside of the current number of columns (0.."
  5043. + getColumnCount() + ")");
  5044. }
  5045. if (numberOfColumns < 1) {
  5046. throw new IllegalArgumentException(
  5047. "Number of columns must be 1 or greater (was "
  5048. + numberOfColumns);
  5049. }
  5050. // Add to bookkeeping
  5051. flyweightRow.addCells(index, numberOfColumns);
  5052. for (int i = 0; i < numberOfColumns; i++) {
  5053. columns.add(index, new Column());
  5054. }
  5055. // Adjust frozen columns
  5056. boolean frozen = index < frozenColumns;
  5057. if (frozen) {
  5058. frozenColumns += numberOfColumns;
  5059. }
  5060. // Add to DOM
  5061. header.paintInsertColumns(index, numberOfColumns, frozen);
  5062. body.paintInsertColumns(index, numberOfColumns, frozen);
  5063. footer.paintInsertColumns(index, numberOfColumns, frozen);
  5064. // this needs to be before the scrollbar adjustment.
  5065. boolean scrollbarWasNeeded = horizontalScrollbar
  5066. .getOffsetSize() < horizontalScrollbar.getScrollSize();
  5067. scroller.recalculateScrollbarsForVirtualViewport();
  5068. boolean scrollbarIsNowNeeded = horizontalScrollbar
  5069. .getOffsetSize() < horizontalScrollbar.getScrollSize();
  5070. if (!scrollbarWasNeeded && scrollbarIsNowNeeded) {
  5071. // This might as a side effect move rows around (when scrolled
  5072. // all the way down) and require the DOM to be up to date, i.e.
  5073. // the column to be added
  5074. body.verifyEscalatorCount();
  5075. }
  5076. // fix initial width
  5077. if (header.getRowCount() > 0 || body.getRowCount() > 0
  5078. || footer.getRowCount() > 0) {
  5079. Map<Integer, Double> colWidths = new HashMap<>();
  5080. Double width = Double.valueOf(Column.DEFAULT_COLUMN_WIDTH_PX);
  5081. for (int i = index; i < index + numberOfColumns; i++) {
  5082. Integer col = Integer.valueOf(i);
  5083. colWidths.put(col, width);
  5084. }
  5085. getColumnConfiguration().setColumnWidths(colWidths);
  5086. }
  5087. // Adjust scrollbar
  5088. double pixelsToInsertedColumn = columnConfiguration
  5089. .getCalculatedColumnsWidth(Range.withLength(0, index));
  5090. final boolean columnsWereAddedToTheLeftOfViewport = scroller.lastScrollLeft > pixelsToInsertedColumn;
  5091. if (columnsWereAddedToTheLeftOfViewport) {
  5092. double insertedColumnsWidth = columnConfiguration
  5093. .getCalculatedColumnsWidth(
  5094. Range.withLength(index, numberOfColumns));
  5095. horizontalScrollbar.setScrollPos(
  5096. scroller.lastScrollLeft + insertedColumnsWidth);
  5097. }
  5098. /*
  5099. * Colspans make any kind of automatic clever content re-rendering
  5100. * impossible: As soon as anything has colspans, adding one might
  5101. * affect surrounding colspans, modifying the DOM structure once
  5102. * again, ending in a cascade of updates. Because we don't know how
  5103. * the data is updated.
  5104. *
  5105. * So, instead, we don't do anything. The client code is responsible
  5106. * for re-rendering the content (if so desired). Everything Just
  5107. * Works (TM) if colspans aren't used.
  5108. */
  5109. }
  5110. @Override
  5111. public int getColumnCount() {
  5112. return columns.size();
  5113. }
  5114. @Override
  5115. public void setFrozenColumnCount(int count)
  5116. throws IllegalArgumentException {
  5117. if (count < 0 || count > getColumnCount()) {
  5118. throw new IllegalArgumentException(
  5119. "count must be between 0 and the current number of columns ("
  5120. + getColumnCount() + ")");
  5121. }
  5122. int oldCount = frozenColumns;
  5123. if (count == oldCount) {
  5124. return;
  5125. }
  5126. frozenColumns = count;
  5127. if (hasSomethingInDom()) {
  5128. // Are we freezing or unfreezing?
  5129. boolean frozen = count > oldCount;
  5130. int firstAffectedCol;
  5131. int firstUnaffectedCol;
  5132. if (frozen) {
  5133. firstAffectedCol = oldCount;
  5134. firstUnaffectedCol = count;
  5135. } else {
  5136. firstAffectedCol = count;
  5137. firstUnaffectedCol = oldCount;
  5138. }
  5139. if (oldCount > 0) {
  5140. header.setColumnLastFrozen(oldCount - 1, false);
  5141. body.setColumnLastFrozen(oldCount - 1, false);
  5142. footer.setColumnLastFrozen(oldCount - 1, false);
  5143. }
  5144. if (count > 0) {
  5145. header.setColumnLastFrozen(count - 1, true);
  5146. body.setColumnLastFrozen(count - 1, true);
  5147. footer.setColumnLastFrozen(count - 1, true);
  5148. }
  5149. for (int col = firstAffectedCol; col < firstUnaffectedCol; col++) {
  5150. header.setColumnFrozen(col, frozen);
  5151. body.setColumnFrozen(col, frozen);
  5152. footer.setColumnFrozen(col, frozen);
  5153. }
  5154. }
  5155. scroller.recalculateScrollbarsForVirtualViewport();
  5156. }
  5157. @Override
  5158. public int getFrozenColumnCount() {
  5159. return frozenColumns;
  5160. }
  5161. @Override
  5162. public void setColumnWidth(int index, double px)
  5163. throws IllegalArgumentException {
  5164. setColumnWidths(Collections.singletonMap(Integer.valueOf(index),
  5165. Double.valueOf(px)));
  5166. }
  5167. @Override
  5168. public void setColumnWidths(Map<Integer, Double> indexWidthMap)
  5169. throws IllegalArgumentException {
  5170. if (indexWidthMap == null) {
  5171. throw new IllegalArgumentException("indexWidthMap was null");
  5172. }
  5173. if (indexWidthMap.isEmpty()) {
  5174. return;
  5175. }
  5176. Profiler.enter("Escalator.ColumnConfigurationImpl.setColumnWidths");
  5177. try {
  5178. for (Entry<Integer, Double> entry : indexWidthMap.entrySet()) {
  5179. int index = entry.getKey().intValue();
  5180. double width = entry.getValue().doubleValue();
  5181. checkValidColumnIndex(index);
  5182. // Not all browsers will accept any fractional size..
  5183. width = WidgetUtil.roundSizeDown(width);
  5184. columns.get(index).setWidth(width);
  5185. }
  5186. widthsArray = null;
  5187. header.reapplyColumnWidths();
  5188. body.reapplyColumnWidths();
  5189. footer.reapplyColumnWidths();
  5190. recalculateElementSizes();
  5191. } finally {
  5192. Profiler.leave(
  5193. "Escalator.ColumnConfigurationImpl.setColumnWidths");
  5194. }
  5195. }
  5196. private void checkValidColumnIndex(int index)
  5197. throws IllegalArgumentException {
  5198. if (!Range.withLength(0, getColumnCount()).contains(index)) {
  5199. throw new IllegalArgumentException("The given column index ("
  5200. + index + ") does not exist");
  5201. }
  5202. }
  5203. @Override
  5204. public double getColumnWidth(int index)
  5205. throws IllegalArgumentException {
  5206. checkValidColumnIndex(index);
  5207. return columns.get(index).getDefinedWidth();
  5208. }
  5209. @Override
  5210. public double getColumnWidthActual(int index) {
  5211. return columns.get(index).getCalculatedWidth();
  5212. }
  5213. private double getMaxCellWidth(int colIndex)
  5214. throws IllegalArgumentException {
  5215. double headerWidth = header.measureMinCellWidth(colIndex, true);
  5216. double bodyWidth = body.measureMinCellWidth(colIndex, true);
  5217. double footerWidth = footer.measureMinCellWidth(colIndex, true);
  5218. double maxWidth = Math.max(headerWidth,
  5219. Math.max(bodyWidth, footerWidth));
  5220. if (maxWidth < 0 && header.getRowCount() == 0
  5221. && body.getRowCount() == 0 && footer.getRowCount() == 0) {
  5222. maxWidth = 0;
  5223. }
  5224. assert maxWidth >= 0 : "Got a negative max width for a column, which should be impossible.";
  5225. return maxWidth;
  5226. }
  5227. private double getMinCellWidth(int colIndex)
  5228. throws IllegalArgumentException {
  5229. double headerWidth = header.measureMinCellWidth(colIndex, false);
  5230. double bodyWidth = body.measureMinCellWidth(colIndex, false);
  5231. double footerWidth = footer.measureMinCellWidth(colIndex, false);
  5232. double minWidth = Math.max(headerWidth,
  5233. Math.max(bodyWidth, footerWidth));
  5234. if (minWidth < 0 && header.getRowCount() == 0
  5235. && body.getRowCount() == 0 && footer.getRowCount() == 0) {
  5236. minWidth = 0;
  5237. }
  5238. assert minWidth >= 0 : "Got a negative min width for a column, which should be impossible.";
  5239. return minWidth;
  5240. }
  5241. /**
  5242. * Calculates the width of the columns in a given range.
  5243. *
  5244. * @param columns
  5245. * the columns to calculate
  5246. * @return the total width of the columns in the given
  5247. * <code>columns</code>
  5248. */
  5249. double getCalculatedColumnsWidth(final Range columns) {
  5250. /*
  5251. * This is an assert instead of an exception, since this is an
  5252. * internal method.
  5253. */
  5254. assert columns
  5255. .isSubsetOf(Range.between(0, getColumnCount())) : "Range "
  5256. + "was outside of current column range (i.e.: "
  5257. + Range.between(0, getColumnCount())
  5258. + ", but was given :" + columns;
  5259. double sum = 0;
  5260. for (int i = columns.getStart(); i < columns.getEnd(); i++) {
  5261. double columnWidthActual = getColumnWidthActual(i);
  5262. sum += columnWidthActual;
  5263. }
  5264. return sum;
  5265. }
  5266. double[] getCalculatedColumnWidths() {
  5267. if (widthsArray == null || widthsArray.length != getColumnCount()) {
  5268. widthsArray = new double[getColumnCount()];
  5269. for (int i = 0; i < columns.size(); i++) {
  5270. widthsArray[i] = columns.get(i).getCalculatedWidth();
  5271. }
  5272. }
  5273. return widthsArray;
  5274. }
  5275. @Override
  5276. public void refreshColumns(int index, int numberOfColumns)
  5277. throws IndexOutOfBoundsException, IllegalArgumentException {
  5278. if (numberOfColumns < 1) {
  5279. throw new IllegalArgumentException(
  5280. "Number of columns must be 1 or greater (was "
  5281. + numberOfColumns + ")");
  5282. }
  5283. if (index < 0 || index + numberOfColumns > getColumnCount()) {
  5284. throw new IndexOutOfBoundsException("The given "
  5285. + "column range (" + index + ".."
  5286. + (index + numberOfColumns)
  5287. + ") was outside of the current number of columns ("
  5288. + getColumnCount() + ")");
  5289. }
  5290. header.refreshColumns(index, numberOfColumns);
  5291. body.refreshColumns(index, numberOfColumns);
  5292. footer.refreshColumns(index, numberOfColumns);
  5293. }
  5294. }
  5295. /**
  5296. * A decision on how to measure a spacer when it is partially within a
  5297. * designated range.
  5298. * <p>
  5299. * The meaning of each value may differ depending on the context it is being
  5300. * used in. Check that particular method's JavaDoc.
  5301. */
  5302. private enum SpacerInclusionStrategy {
  5303. /** A representation of "the entire spacer". */
  5304. COMPLETE,
  5305. /** A representation of "a partial spacer". */
  5306. PARTIAL,
  5307. /** A representation of "no spacer at all". */
  5308. NONE
  5309. }
  5310. private class SpacerContainer {
  5311. /** This is used mainly for testing purposes */
  5312. private static final String SPACER_LOGICAL_ROW_PROPERTY = "vLogicalRow";
  5313. private final class SpacerImpl implements Spacer {
  5314. private TableCellElement spacerElement;
  5315. private TableRowElement root;
  5316. private DivElement deco;
  5317. private int rowIndex;
  5318. private double height = -1;
  5319. private boolean domHasBeenSetup = false;
  5320. private double decoHeight;
  5321. private double defaultCellBorderBottomSize = -1;
  5322. public SpacerImpl(int rowIndex) {
  5323. this.rowIndex = rowIndex;
  5324. root = TableRowElement.as(DOM.createTR());
  5325. spacerElement = TableCellElement.as(DOM.createTD());
  5326. root.appendChild(spacerElement);
  5327. root.setPropertyInt(SPACER_LOGICAL_ROW_PROPERTY, rowIndex);
  5328. deco = DivElement.as(DOM.createDiv());
  5329. }
  5330. public void setPositionDiff(double x, double y) {
  5331. setPosition(getLeft() + x, getTop() + y);
  5332. }
  5333. public void setupDom(double height) {
  5334. assert !domHasBeenSetup : "DOM can't be set up twice.";
  5335. assert RootPanel.get().getElement().isOrHasChild(
  5336. root) : "Root element should've been attached to the DOM by now.";
  5337. domHasBeenSetup = true;
  5338. getRootElement().getStyle().setWidth(getInnerWidth(), Unit.PX);
  5339. setHeight(height);
  5340. spacerElement
  5341. .setColSpan(getColumnConfiguration().getColumnCount());
  5342. setStylePrimaryName(getStylePrimaryName());
  5343. }
  5344. public TableRowElement getRootElement() {
  5345. return root;
  5346. }
  5347. @Override
  5348. public Element getDecoElement() {
  5349. return deco;
  5350. }
  5351. public void setPosition(double x, double y) {
  5352. positions.set(getRootElement(), x, y);
  5353. positions.set(getDecoElement(), 0,
  5354. y - getSpacerDecoTopOffset());
  5355. }
  5356. private double getSpacerDecoTopOffset() {
  5357. return getBody().getDefaultRowHeight();
  5358. }
  5359. public void setStylePrimaryName(String style) {
  5360. UIObject.setStylePrimaryName(root, style + "-spacer");
  5361. UIObject.setStylePrimaryName(deco, style + "-spacer-deco");
  5362. }
  5363. /**
  5364. * Clear spacer height without moving other contents.
  5365. *
  5366. * @see #setHeight(double)
  5367. */
  5368. private void clearHeight() {
  5369. height = 0;
  5370. root.getStyle().setHeight(0, Unit.PX);
  5371. updateDecoratorGeometry(0);
  5372. }
  5373. public void setHeight(double height) {
  5374. assert height >= 0 : "Height must be more >= 0 (was " + height
  5375. + ")";
  5376. final double heightDiff = height - Math.max(0, this.height);
  5377. final double oldHeight = this.height;
  5378. this.height = height;
  5379. // since the spacer might be rendered on top of the previous
  5380. // rows border (done with css), need to increase height the
  5381. // amount of the border thickness
  5382. if (defaultCellBorderBottomSize < 0) {
  5383. defaultCellBorderBottomSize = WidgetUtil
  5384. .getBorderBottomThickness(body
  5385. .getRowElement(
  5386. getVisibleRowRange().getStart())
  5387. .getFirstChildElement());
  5388. }
  5389. root.getStyle().setHeight(height + defaultCellBorderBottomSize,
  5390. Unit.PX);
  5391. // move the visible spacers getRow row onwards.
  5392. shiftSpacerPositionsAfterRow(getRow(), heightDiff);
  5393. /*
  5394. * If we're growing, we'll adjust the scroll size first, then
  5395. * adjust scrolling. If we're shrinking, we do it after the
  5396. * second if-clause.
  5397. */
  5398. boolean spacerIsGrowing = heightDiff > 0;
  5399. if (spacerIsGrowing) {
  5400. verticalScrollbar.setScrollSize(
  5401. verticalScrollbar.getScrollSize() + heightDiff);
  5402. }
  5403. /*
  5404. * Don't modify the scrollbars if we're expanding the -1 spacer
  5405. * while we're scrolled to the top.
  5406. */
  5407. boolean minusOneSpacerException = spacerIsGrowing
  5408. && getRow() == -1 && body.getTopRowLogicalIndex() == 0;
  5409. boolean viewportNeedsScrolling = getRow() < body
  5410. .getTopRowLogicalIndex() && !minusOneSpacerException;
  5411. if (viewportNeedsScrolling) {
  5412. /*
  5413. * We can't use adjustScrollPos here, probably because of a
  5414. * bookkeeping-related race condition.
  5415. *
  5416. * This particular situation is easier, however, since we
  5417. * know exactly how many pixels we need to move (heightDiff)
  5418. * and all elements below the spacer always need to move
  5419. * that pixel amount.
  5420. */
  5421. for (TableRowElement row : body.visualRowOrder) {
  5422. body.setRowPosition(row, 0,
  5423. body.getRowTop(row) + heightDiff);
  5424. }
  5425. double top = getTop();
  5426. double bottom = top + oldHeight;
  5427. double scrollTop = verticalScrollbar.getScrollPos();
  5428. boolean viewportTopIsAtMidSpacer = top < scrollTop
  5429. && scrollTop < bottom;
  5430. final double moveDiff;
  5431. if (viewportTopIsAtMidSpacer && !spacerIsGrowing) {
  5432. /*
  5433. * If the scroll top is in the middle of the modified
  5434. * spacer, we want to scroll the viewport up as usual,
  5435. * but we don't want to scroll past the top of it.
  5436. *
  5437. * Math.max ensures this (remember: the result is going
  5438. * to be negative).
  5439. */
  5440. moveDiff = Math.max(heightDiff, top - scrollTop);
  5441. } else {
  5442. moveDiff = heightDiff;
  5443. }
  5444. body.setBodyScrollPosition(tBodyScrollLeft,
  5445. tBodyScrollTop + moveDiff);
  5446. verticalScrollbar.setScrollPosByDelta(moveDiff);
  5447. } else {
  5448. body.shiftRowPositions(getRow(), heightDiff);
  5449. }
  5450. if (!spacerIsGrowing) {
  5451. verticalScrollbar.setScrollSize(
  5452. verticalScrollbar.getScrollSize() + heightDiff);
  5453. }
  5454. updateDecoratorGeometry(height);
  5455. }
  5456. /** Resizes and places the decorator. */
  5457. private void updateDecoratorGeometry(double detailsHeight) {
  5458. Style style = deco.getStyle();
  5459. decoHeight = detailsHeight + getBody().getDefaultRowHeight();
  5460. style.setHeight(decoHeight, Unit.PX);
  5461. }
  5462. @Override
  5463. public Element getElement() {
  5464. return spacerElement;
  5465. }
  5466. @Override
  5467. public int getRow() {
  5468. return rowIndex;
  5469. }
  5470. public double getHeight() {
  5471. assert height >= 0 : "Height was not previously set by setHeight.";
  5472. return height;
  5473. }
  5474. public double getTop() {
  5475. return positions.getTop(getRootElement());
  5476. }
  5477. public double getLeft() {
  5478. return positions.getLeft(getRootElement());
  5479. }
  5480. /**
  5481. * Sets a new row index for this spacer. Also updates the
  5482. * bookkeeping at {@link SpacerContainer#rowIndexToSpacer}.
  5483. */
  5484. @SuppressWarnings("boxing")
  5485. public void setRowIndex(int rowIndex) {
  5486. SpacerImpl spacer = rowIndexToSpacer.remove(this.rowIndex);
  5487. assert this == spacer : "trying to move an unexpected spacer.";
  5488. int oldIndex = this.rowIndex;
  5489. this.rowIndex = rowIndex;
  5490. root.setPropertyInt(SPACER_LOGICAL_ROW_PROPERTY, rowIndex);
  5491. rowIndexToSpacer.put(this.rowIndex, this);
  5492. fireEvent(new SpacerIndexChangedEvent(oldIndex, this.rowIndex));
  5493. }
  5494. /**
  5495. * Updates the spacer's visibility parameters, based on whether it
  5496. * is being currently visible or not.
  5497. *
  5498. * @deprecated Escalator no longer uses this logic at initialisation
  5499. * as there can only be a limited number of spacers and
  5500. * hidden spacers within visual range interfere with
  5501. * position calculations.
  5502. */
  5503. @Deprecated
  5504. public void updateVisibility() {
  5505. if (isInViewport()) {
  5506. show();
  5507. } else {
  5508. hide();
  5509. }
  5510. }
  5511. private boolean isInViewport() {
  5512. int top = (int) Math.ceil(getTop());
  5513. int height = (int) Math.floor(getHeight());
  5514. Range location = Range.withLength(top, height);
  5515. return getViewportPixels().intersects(location);
  5516. }
  5517. public void show() {
  5518. getRootElement().getStyle().clearDisplay();
  5519. getDecoElement().getStyle().clearDisplay();
  5520. fireEvent(new SpacerVisibilityChangedEvent(getRow(), true));
  5521. }
  5522. public void hide() {
  5523. getRootElement().getStyle().setDisplay(Display.NONE);
  5524. getDecoElement().getStyle().setDisplay(Display.NONE);
  5525. fireEvent(new SpacerVisibilityChangedEvent(getRow(), false));
  5526. }
  5527. /**
  5528. * Crop the decorator element so that it doesn't overlap the header
  5529. * and footer sections.
  5530. *
  5531. * @param bodyTop
  5532. * the top cordinate of the escalator body
  5533. * @param bodyBottom
  5534. * the bottom cordinate of the escalator body
  5535. * @param decoWidth
  5536. * width of the deco
  5537. */
  5538. private void updateDecoClip(final double bodyTop,
  5539. final double bodyBottom, final double decoWidth) {
  5540. final int top = deco.getAbsoluteTop();
  5541. final int bottom = deco.getAbsoluteBottom();
  5542. /*
  5543. * FIXME
  5544. *
  5545. * Height and its use is a workaround for the issue where
  5546. * coordinates of the deco are not calculated yet. This will
  5547. * prevent a deco from being displayed when it's added to DOM
  5548. */
  5549. final int height = bottom - top;
  5550. if (top < bodyTop || bottom > bodyBottom) {
  5551. final double topClip = Math.max(0.0D, bodyTop - top);
  5552. final double bottomClip = height
  5553. - Math.max(0.0D, bottom - bodyBottom);
  5554. // TODO [optimize] not sure how GWT compiles this
  5555. final String clip = new StringBuilder("rect(")
  5556. .append(topClip).append("px,").append(decoWidth)
  5557. .append("px,").append(bottomClip).append("px,0)")
  5558. .toString();
  5559. deco.getStyle().setProperty("clip", clip);
  5560. } else {
  5561. deco.getStyle().setProperty("clip", "auto");
  5562. }
  5563. }
  5564. }
  5565. private final TreeMap<Integer, SpacerImpl> rowIndexToSpacer = new TreeMap<>();
  5566. private SpacerUpdater spacerUpdater = SpacerUpdater.NULL;
  5567. private final ScrollHandler spacerScroller = new ScrollHandler() {
  5568. private double prevScrollX = 0;
  5569. @Override
  5570. public void onScroll(ScrollEvent event) {
  5571. if (WidgetUtil.pixelValuesEqual(getScrollLeft(), prevScrollX)) {
  5572. return;
  5573. }
  5574. prevScrollX = getScrollLeft();
  5575. for (SpacerImpl spacer : rowIndexToSpacer.values()) {
  5576. spacer.setPosition(prevScrollX, spacer.getTop());
  5577. }
  5578. }
  5579. };
  5580. private HandlerRegistration spacerScrollerRegistration;
  5581. /** Width of the spacers' decos. Calculated once then cached. */
  5582. private double spacerDecoWidth = 0.0D;
  5583. public void setSpacer(int rowIndex, double height)
  5584. throws IllegalArgumentException {
  5585. if (rowIndex < -1 || rowIndex >= getBody().getRowCount()) {
  5586. throw new IllegalArgumentException("invalid row index: "
  5587. + rowIndex + ", while the body only has "
  5588. + getBody().getRowCount() + " rows.");
  5589. }
  5590. if (height >= 0) {
  5591. if (!spacerExists(rowIndex)) {
  5592. insertNewSpacer(rowIndex, height);
  5593. } else {
  5594. updateExistingSpacer(rowIndex, height);
  5595. }
  5596. } else if (spacerExists(rowIndex)) {
  5597. removeSpacer(rowIndex);
  5598. }
  5599. updateSpacerDecosVisibility();
  5600. }
  5601. /** Checks if a given element is a spacer element */
  5602. public boolean isSpacer(Element row) {
  5603. /*
  5604. * If this needs optimization, we could do a more heuristic check
  5605. * based on stylenames and stuff, instead of iterating through the
  5606. * map.
  5607. */
  5608. for (SpacerImpl spacer : rowIndexToSpacer.values()) {
  5609. if (spacer.getRootElement().equals(row)) {
  5610. return true;
  5611. }
  5612. }
  5613. return false;
  5614. }
  5615. @SuppressWarnings("boxing")
  5616. void scrollToSpacer(int spacerIndex, ScrollDestination destination,
  5617. int padding) {
  5618. assert !destination.equals(ScrollDestination.MIDDLE)
  5619. || padding != 0 : "destination/padding check should be done before this method";
  5620. body.scrollToRowSpacerOrBoth(spacerIndex, destination, padding,
  5621. ScrollType.SPACER);
  5622. }
  5623. public void reapplySpacerWidths() {
  5624. // FIXME #16266 , spacers get couple pixels too much because borders
  5625. final double width = getInnerWidth() - spacerDecoWidth;
  5626. for (SpacerImpl spacer : rowIndexToSpacer.values()) {
  5627. spacer.getRootElement().getStyle().setWidth(width, Unit.PX);
  5628. }
  5629. }
  5630. /**
  5631. * @deprecated This method is no longer used by Escalator and is likely
  5632. * to be removed soon.
  5633. *
  5634. * @param removedRowsRange
  5635. */
  5636. @Deprecated
  5637. public void paintRemoveSpacers(Range removedRowsRange) {
  5638. removeSpacers(removedRowsRange);
  5639. shiftSpacersByRows(removedRowsRange.getStart(),
  5640. -removedRowsRange.length());
  5641. }
  5642. /**
  5643. * Removes spacers of the given range without moving other contents.
  5644. * <p>
  5645. * NOTE: Changed functionality since 8.9. Previous incarnation of this
  5646. * method updated the positions of all the contents below the first
  5647. * removed spacer.
  5648. *
  5649. * @param removedRange
  5650. * logical range of spacers to remove
  5651. */
  5652. @SuppressWarnings("boxing")
  5653. public void removeSpacers(Range removedRange) {
  5654. Map<Integer, SpacerImpl> removedSpacers = rowIndexToSpacer.subMap(
  5655. removedRange.getStart(), true, removedRange.getEnd(),
  5656. false);
  5657. if (removedSpacers.isEmpty()) {
  5658. return;
  5659. }
  5660. double specialSpacerHeight = removedRange.contains(-1)
  5661. ? getSpacerHeight(-1)
  5662. : 0;
  5663. for (Entry<Integer, SpacerImpl> entry : removedSpacers.entrySet()) {
  5664. SpacerImpl spacer = entry.getValue();
  5665. rowIndexToSpacer.remove(entry.getKey());
  5666. destroySpacerContent(spacer);
  5667. spacer.clearHeight();
  5668. spacer.getRootElement().removeFromParent();
  5669. spacer.getDecoElement().removeFromParent();
  5670. }
  5671. removedSpacers.clear();
  5672. if (rowIndexToSpacer.isEmpty()) {
  5673. assert spacerScrollerRegistration != null : "Spacer scroller registration was null";
  5674. spacerScrollerRegistration.removeHandler();
  5675. spacerScrollerRegistration = null;
  5676. }
  5677. // if a rowless spacer at the top got removed, all rows and spacers
  5678. // need to be moved up accordingly
  5679. if (!WidgetUtil.pixelValuesEqual(specialSpacerHeight, 0)) {
  5680. double scrollDiff = Math.min(specialSpacerHeight,
  5681. getScrollTop());
  5682. body.moveViewportAndContent(null, -specialSpacerHeight,
  5683. -specialSpacerHeight, -scrollDiff);
  5684. }
  5685. }
  5686. public Map<Integer, SpacerImpl> getSpacers() {
  5687. return new HashMap<>(rowIndexToSpacer);
  5688. }
  5689. /**
  5690. * Calculates the sum of all spacers.
  5691. *
  5692. * @return sum of all spacers, or 0 if no spacers present
  5693. */
  5694. public double getSpacerHeightsSum() {
  5695. return getHeights(rowIndexToSpacer.values());
  5696. }
  5697. /**
  5698. * Calculates the sum of all spacers from one row index onwards.
  5699. *
  5700. * @param logicalRowIndex
  5701. * the spacer to include as the first calculated spacer
  5702. * @return the sum of all spacers from {@code logicalRowIndex} and
  5703. * onwards, or 0 if no suitable spacers were found
  5704. */
  5705. @SuppressWarnings("boxing")
  5706. public Collection<SpacerImpl> getSpacersForRowAndAfter(
  5707. int logicalRowIndex) {
  5708. return new ArrayList<>(
  5709. rowIndexToSpacer.tailMap(logicalRowIndex, true).values());
  5710. }
  5711. /**
  5712. * Get all spacers from one pixel point onwards.
  5713. * <p>
  5714. *
  5715. * In this method, the {@link SpacerInclusionStrategy} has the following
  5716. * meaning when a spacer lies in the middle of either pixel argument:
  5717. * <dl>
  5718. * <dt>{@link SpacerInclusionStrategy#COMPLETE COMPLETE}
  5719. * <dd>include the spacer
  5720. * <dt>{@link SpacerInclusionStrategy#PARTIAL PARTIAL}
  5721. * <dd>include the spacer
  5722. * <dt>{@link SpacerInclusionStrategy#NONE NONE}
  5723. * <dd>ignore the spacer
  5724. * </dl>
  5725. *
  5726. * @param px
  5727. * the pixel point after which to return all spacers
  5728. * @param strategy
  5729. * the inclusion strategy regarding the {@code px}
  5730. * @return a collection of the spacers that exist after {@code px}
  5731. */
  5732. public Collection<SpacerImpl> getSpacersAfterPx(final double px,
  5733. final SpacerInclusionStrategy strategy) {
  5734. List<SpacerImpl> spacers = new ArrayList<>(
  5735. rowIndexToSpacer.values());
  5736. for (int i = 0; i < spacers.size(); i++) {
  5737. SpacerImpl spacer = spacers.get(i);
  5738. double top = spacer.getTop();
  5739. double bottom = top + spacer.getHeight();
  5740. if (top > px) {
  5741. return spacers.subList(i, spacers.size());
  5742. } else if (bottom > px) {
  5743. if (strategy == SpacerInclusionStrategy.NONE) {
  5744. return spacers.subList(i + 1, spacers.size());
  5745. } else {
  5746. return spacers.subList(i, spacers.size());
  5747. }
  5748. }
  5749. }
  5750. return Collections.emptySet();
  5751. }
  5752. /**
  5753. * Gets the spacers currently rendered in the DOM.
  5754. *
  5755. * @return an unmodifiable (but live) collection of the spacers
  5756. * currently in the DOM
  5757. */
  5758. public Collection<SpacerImpl> getSpacersInDom() {
  5759. return Collections
  5760. .unmodifiableCollection(rowIndexToSpacer.values());
  5761. }
  5762. /**
  5763. * Gets the amount of pixels occupied by spacers between two pixel
  5764. * points.
  5765. * <p>
  5766. * In this method, the {@link SpacerInclusionStrategy} has the following
  5767. * meaning when a spacer lies in the middle of either pixel argument:
  5768. * <dl>
  5769. * <dt>{@link SpacerInclusionStrategy#COMPLETE COMPLETE}
  5770. * <dd>take the entire spacer into account
  5771. * <dt>{@link SpacerInclusionStrategy#PARTIAL PARTIAL}
  5772. * <dd>take only the visible area into account
  5773. * <dt>{@link SpacerInclusionStrategy#NONE NONE}
  5774. * <dd>ignore that spacer
  5775. * </dl>
  5776. *
  5777. * @param rangeTop
  5778. * the top pixel point
  5779. * @param topInclusion
  5780. * the inclusion strategy regarding {@code rangeTop}.
  5781. * @param rangeBottom
  5782. * the bottom pixel point
  5783. * @param bottomInclusion
  5784. * the inclusion strategy regarding {@code rangeBottom}.
  5785. * @return the pixels occupied by spacers between {@code rangeTop} and
  5786. * {@code rangeBottom}
  5787. */
  5788. public double getSpacerHeightsSumBetweenPx(double rangeTop,
  5789. SpacerInclusionStrategy topInclusion, double rangeBottom,
  5790. SpacerInclusionStrategy bottomInclusion) {
  5791. assert rangeTop <= rangeBottom : "rangeTop must be less than rangeBottom";
  5792. double heights = 0;
  5793. /*
  5794. * TODO [[optimize]]: this might be somewhat inefficient (due to
  5795. * iterator-based scanning, instead of using the treemap's search
  5796. * functionalities). But it should be easy to write, read, verify
  5797. * and maintain.
  5798. */
  5799. for (SpacerImpl spacer : rowIndexToSpacer.values()) {
  5800. double top = spacer.getTop();
  5801. double height = spacer.getHeight();
  5802. double bottom = top + height;
  5803. /*
  5804. * If we happen to implement a DoubleRange (in addition to the
  5805. * int-based Range) at some point, the following logic should
  5806. * probably be converted into using the
  5807. * Range.partitionWith-equivalent.
  5808. */
  5809. boolean topIsAboveRange = top < rangeTop;
  5810. boolean topIsInRange = rangeTop <= top && top <= rangeBottom;
  5811. boolean topIsBelowRange = rangeBottom < top;
  5812. boolean bottomIsAboveRange = bottom < rangeTop;
  5813. boolean bottomIsInRange = rangeTop <= bottom
  5814. && bottom <= rangeBottom;
  5815. boolean bottomIsBelowRange = rangeBottom < bottom;
  5816. assert topIsAboveRange ^ topIsBelowRange
  5817. ^ topIsInRange : "Bad top logic";
  5818. assert bottomIsAboveRange ^ bottomIsBelowRange
  5819. ^ bottomIsInRange : "Bad bottom logic";
  5820. if (bottomIsAboveRange) {
  5821. continue;
  5822. } else if (topIsBelowRange) {
  5823. return heights;
  5824. } else if (topIsAboveRange && bottomIsInRange) {
  5825. switch (topInclusion) {
  5826. case PARTIAL:
  5827. heights += bottom - rangeTop;
  5828. break;
  5829. case COMPLETE:
  5830. heights += height;
  5831. break;
  5832. default:
  5833. break;
  5834. }
  5835. } else if (topIsAboveRange && bottomIsBelowRange) {
  5836. /*
  5837. * Here we arbitrarily decide that the top inclusion will
  5838. * have the honor of overriding the bottom inclusion if
  5839. * happens to be a conflict of interests.
  5840. */
  5841. switch (topInclusion) {
  5842. case NONE:
  5843. return 0;
  5844. case COMPLETE:
  5845. return height;
  5846. case PARTIAL:
  5847. return rangeBottom - rangeTop;
  5848. default:
  5849. throw new IllegalArgumentException(
  5850. "Unexpected inclusion state :" + topInclusion);
  5851. }
  5852. } else if (topIsInRange && bottomIsInRange) {
  5853. heights += height;
  5854. } else if (topIsInRange && bottomIsBelowRange) {
  5855. switch (bottomInclusion) {
  5856. case PARTIAL:
  5857. heights += rangeBottom - top;
  5858. break;
  5859. case COMPLETE:
  5860. heights += height;
  5861. break;
  5862. default:
  5863. break;
  5864. }
  5865. return heights;
  5866. } else {
  5867. assert false : "Unnaccounted-for situation";
  5868. }
  5869. }
  5870. return heights;
  5871. }
  5872. /**
  5873. * Gets the amount of pixels occupied by spacers from the top until a
  5874. * certain spot from the top of the body.
  5875. *
  5876. * @param px
  5877. * pixels counted from the top
  5878. * @return the pixels occupied by spacers up until {@code px}
  5879. */
  5880. public double getSpacerHeightsSumUntilPx(double px) {
  5881. return getSpacerHeightsSumBetweenPx(0,
  5882. SpacerInclusionStrategy.PARTIAL, px,
  5883. SpacerInclusionStrategy.PARTIAL);
  5884. }
  5885. /**
  5886. * Gets the amount of pixels occupied by spacers until a logical row
  5887. * index. The spacer of the row corresponding with the given index isn't
  5888. * included.
  5889. *
  5890. * @param logicalIndex
  5891. * a logical row index
  5892. * @return the pixels occupied by spacers up until {@code logicalIndex}
  5893. */
  5894. @SuppressWarnings("boxing")
  5895. public double getSpacerHeightsSumUntilIndex(int logicalIndex) {
  5896. return getHeights(
  5897. rowIndexToSpacer.headMap(logicalIndex, false).values());
  5898. }
  5899. private double getHeights(Collection<SpacerImpl> spacers) {
  5900. double heights = 0;
  5901. for (SpacerImpl spacer : spacers) {
  5902. heights += spacer.getHeight();
  5903. }
  5904. return heights;
  5905. }
  5906. /**
  5907. * Gets the height of the spacer for a row index.
  5908. *
  5909. * @param rowIndex
  5910. * the index of the row where the spacer should be
  5911. * @return the height of the spacer at index {@code rowIndex}, or 0 if
  5912. * there is no spacer there
  5913. */
  5914. public double getSpacerHeight(int rowIndex) {
  5915. SpacerImpl spacer = getSpacer(rowIndex);
  5916. if (spacer != null) {
  5917. return spacer.getHeight();
  5918. } else {
  5919. return 0;
  5920. }
  5921. }
  5922. private boolean spacerExists(int rowIndex) {
  5923. return rowIndexToSpacer.containsKey(Integer.valueOf(rowIndex));
  5924. }
  5925. @SuppressWarnings("boxing")
  5926. private void insertNewSpacer(int rowIndex, double height) {
  5927. if (spacerScrollerRegistration == null) {
  5928. spacerScrollerRegistration = addScrollHandler(spacerScroller);
  5929. }
  5930. final SpacerImpl spacer = new SpacerImpl(rowIndex);
  5931. rowIndexToSpacer.put(rowIndex, spacer);
  5932. // set the position before adding it to DOM
  5933. positions.set(spacer.getRootElement(), getScrollLeft(),
  5934. calculateSpacerTop(rowIndex));
  5935. TableRowElement spacerRoot = spacer.getRootElement();
  5936. spacerRoot.getStyle()
  5937. .setWidth(columnConfiguration.calculateRowWidth(), Unit.PX);
  5938. body.getElement().appendChild(spacerRoot);
  5939. spacer.setupDom(height);
  5940. // set the deco position, requires that spacer is in the DOM
  5941. positions.set(spacer.getDecoElement(), 0,
  5942. spacer.getTop() - spacer.getSpacerDecoTopOffset());
  5943. spacerDecoContainer.appendChild(spacer.getDecoElement());
  5944. if (spacerDecoContainer.getParentElement() == null) {
  5945. getElement().appendChild(spacerDecoContainer);
  5946. // calculate the spacer deco width, it won't change
  5947. spacerDecoWidth = getBoundingWidth(spacer.getDecoElement());
  5948. }
  5949. initSpacerContent(spacer);
  5950. // schedule updating of the physical indexes
  5951. body.domSorter.reschedule();
  5952. }
  5953. private void updateExistingSpacer(int rowIndex, double newHeight) {
  5954. getSpacer(rowIndex).setHeight(newHeight);
  5955. }
  5956. public SpacerImpl getSpacer(int rowIndex) {
  5957. return rowIndexToSpacer.get(Integer.valueOf(rowIndex));
  5958. }
  5959. private void removeSpacer(int rowIndex) {
  5960. removeSpacers(Range.withOnly(rowIndex));
  5961. }
  5962. public void setStylePrimaryName(String style) {
  5963. for (SpacerImpl spacer : rowIndexToSpacer.values()) {
  5964. spacer.setStylePrimaryName(style);
  5965. }
  5966. }
  5967. public void setSpacerUpdater(SpacerUpdater spacerUpdater)
  5968. throws IllegalArgumentException {
  5969. if (spacerUpdater == null) {
  5970. throw new IllegalArgumentException(
  5971. "spacer updater cannot be null");
  5972. }
  5973. destroySpacerContent(rowIndexToSpacer.values());
  5974. this.spacerUpdater = spacerUpdater;
  5975. initSpacerContent(rowIndexToSpacer.values());
  5976. }
  5977. public SpacerUpdater getSpacerUpdater() {
  5978. return spacerUpdater;
  5979. }
  5980. private void destroySpacerContent(Iterable<SpacerImpl> spacers) {
  5981. for (SpacerImpl spacer : spacers) {
  5982. destroySpacerContent(spacer);
  5983. }
  5984. }
  5985. private void destroySpacerContent(SpacerImpl spacer) {
  5986. assert getElement().isOrHasChild(spacer
  5987. .getRootElement()) : "Spacer's root element somehow got detached from Escalator before detaching";
  5988. assert getElement().isOrHasChild(spacer
  5989. .getElement()) : "Spacer element somehow got detached from Escalator before detaching";
  5990. spacerUpdater.destroy(spacer);
  5991. assert getElement().isOrHasChild(spacer
  5992. .getRootElement()) : "Spacer's root element somehow got detached from Escalator before detaching";
  5993. assert getElement().isOrHasChild(spacer
  5994. .getElement()) : "Spacer element somehow got detached from Escalator before detaching";
  5995. }
  5996. private void initSpacerContent(Iterable<SpacerImpl> spacers) {
  5997. for (SpacerImpl spacer : spacers) {
  5998. initSpacerContent(spacer);
  5999. }
  6000. }
  6001. private void initSpacerContent(SpacerImpl spacer) {
  6002. assert getElement().isOrHasChild(spacer
  6003. .getRootElement()) : "Spacer's root element somehow got detached from Escalator before attaching";
  6004. assert getElement().isOrHasChild(spacer
  6005. .getElement()) : "Spacer element somehow got detached from Escalator before attaching";
  6006. spacerUpdater.init(spacer);
  6007. assert getElement().isOrHasChild(spacer
  6008. .getRootElement()) : "Spacer's root element somehow got detached from Escalator during attaching";
  6009. assert getElement().isOrHasChild(spacer
  6010. .getElement()) : "Spacer element somehow got detached from Escalator during attaching";
  6011. spacer.show();
  6012. }
  6013. public String getSubPartName(Element subElement) {
  6014. for (SpacerImpl spacer : rowIndexToSpacer.values()) {
  6015. if (spacer.getRootElement().isOrHasChild(subElement)) {
  6016. return "spacer[" + spacer.getRow() + "]";
  6017. }
  6018. }
  6019. return null;
  6020. }
  6021. public Element getSubPartElement(int index) {
  6022. SpacerImpl spacer = rowIndexToSpacer.get(Integer.valueOf(index));
  6023. if (spacer != null) {
  6024. return spacer.getElement();
  6025. } else {
  6026. return null;
  6027. }
  6028. }
  6029. private double calculateSpacerTop(int logicalIndex) {
  6030. return body.getRowTop(logicalIndex) + body.getDefaultRowHeight();
  6031. }
  6032. @SuppressWarnings("boxing")
  6033. private void shiftSpacerPositionsAfterRow(int changedRowIndex,
  6034. double diffPx) {
  6035. for (SpacerImpl spacer : rowIndexToSpacer
  6036. .tailMap(changedRowIndex, false).values()) {
  6037. spacer.setPositionDiff(0, diffPx);
  6038. }
  6039. }
  6040. /**
  6041. * Shifts spacers at and after a specific row by an amount of rows that
  6042. * don't contain spacers of their own.
  6043. * <p>
  6044. * This moves both their associated logical row index and also their
  6045. * visual placement.
  6046. * <p>
  6047. * <em>Note:</em> This method does not check for the validity of any
  6048. * arguments.
  6049. *
  6050. * @param index
  6051. * the index of first row to move
  6052. * @param numberOfRows
  6053. * the number of rows to shift the spacers with. A positive
  6054. * value is downwards, a negative value is upwards.
  6055. */
  6056. public void shiftSpacersByRows(int index, int numberOfRows) {
  6057. final double pxDiff = numberOfRows * body.getDefaultRowHeight();
  6058. List<SpacerContainer.SpacerImpl> spacers = new ArrayList<>(
  6059. getSpacersForRowAndAfter(index));
  6060. if (numberOfRows < 0) {
  6061. for (SpacerContainer.SpacerImpl spacer : spacers) {
  6062. spacer.setPositionDiff(0, pxDiff);
  6063. spacer.setRowIndex(spacer.getRow() + numberOfRows);
  6064. }
  6065. } else {
  6066. for (int i = spacers.size() - 1; i >= 0; --i) {
  6067. SpacerContainer.SpacerImpl spacer = spacers.get(i);
  6068. spacer.setPositionDiff(0, pxDiff);
  6069. spacer.setRowIndex(spacer.getRow() + numberOfRows);
  6070. }
  6071. }
  6072. }
  6073. /**
  6074. * Update the associated logical row indexes for spacers without moving
  6075. * their actual positions.
  6076. * <p>
  6077. * <em>Note:</em> This method does not check for the validity of any
  6078. * arguments.
  6079. *
  6080. * @param startIndex
  6081. * the previous logical index of first row to update
  6082. * @param endIndex
  6083. * the previous logical index of first row that doesn't need
  6084. * updating anymore
  6085. * @param numberOfRows
  6086. * the number of rows to shift the associated logical index
  6087. * with. A positive value is downwards, a negative value is
  6088. * upwards.
  6089. */
  6090. private void updateSpacerIndexesForRowAndAfter(int startIndex,
  6091. int endIndex, int numberOfRows) {
  6092. List<SpacerContainer.SpacerImpl> spacers = new ArrayList<>(
  6093. getSpacersForRowAndAfter(startIndex));
  6094. spacers.removeAll(getSpacersForRowAndAfter(endIndex));
  6095. if (numberOfRows < 0) {
  6096. for (SpacerContainer.SpacerImpl spacer : spacers) {
  6097. spacer.setRowIndex(spacer.getRow() + numberOfRows);
  6098. }
  6099. } else {
  6100. for (int i = spacers.size() - 1; i >= 0; --i) {
  6101. SpacerContainer.SpacerImpl spacer = spacers.get(i);
  6102. spacer.setRowIndex(spacer.getRow() + numberOfRows);
  6103. }
  6104. }
  6105. }
  6106. private void updateSpacerDecosVisibility() {
  6107. final Range visibleRowRange = getVisibleRowRange();
  6108. Collection<SpacerImpl> visibleSpacers = rowIndexToSpacer
  6109. .subMap(visibleRowRange.getStart() - 1,
  6110. visibleRowRange.getEnd() + 1)
  6111. .values();
  6112. if (!visibleSpacers.isEmpty()) {
  6113. final double top = tableWrapper.getAbsoluteTop()
  6114. + header.getHeightOfSection();
  6115. final double bottom = tableWrapper.getAbsoluteBottom()
  6116. - footer.getHeightOfSection();
  6117. for (SpacerImpl spacer : visibleSpacers) {
  6118. spacer.updateDecoClip(top, bottom, spacerDecoWidth);
  6119. }
  6120. }
  6121. }
  6122. }
  6123. private class ElementPositionBookkeeper {
  6124. /**
  6125. * A map containing cached values of an element's current top position.
  6126. */
  6127. private final Map<Element, Double> elementTopPositionMap = new HashMap<>();
  6128. private final Map<Element, Double> elementLeftPositionMap = new HashMap<>();
  6129. public void set(final Element e, final double x, final double y) {
  6130. assert e != null : "Element was null";
  6131. position.set(e, x, y);
  6132. elementTopPositionMap.put(e, Double.valueOf(y));
  6133. elementLeftPositionMap.put(e, Double.valueOf(x));
  6134. }
  6135. public double getTop(final Element e) {
  6136. Double top = elementTopPositionMap.get(e);
  6137. if (top == null) {
  6138. throw new IllegalArgumentException("Element " + e
  6139. + " was not found in the position bookkeeping");
  6140. }
  6141. return top.doubleValue();
  6142. }
  6143. public double getLeft(final Element e) {
  6144. Double left = elementLeftPositionMap.get(e);
  6145. if (left == null) {
  6146. throw new IllegalArgumentException("Element " + e
  6147. + " was not found in the position bookkeeping");
  6148. }
  6149. return left.doubleValue();
  6150. }
  6151. public void remove(Element e) {
  6152. elementTopPositionMap.remove(e);
  6153. elementLeftPositionMap.remove(e);
  6154. }
  6155. }
  6156. /**
  6157. * Utility class for parsing and storing SubPart request string attributes
  6158. * for Grid and Escalator.
  6159. *
  6160. * @since 7.5.0
  6161. */
  6162. public static class SubPartArguments {
  6163. private String type;
  6164. private int[] indices;
  6165. private SubPartArguments(String type, int[] indices) {
  6166. /*
  6167. * The constructor is private so that no third party would by
  6168. * mistake start using this parsing scheme, since it's not official
  6169. * by TestBench (yet?).
  6170. */
  6171. this.type = type;
  6172. this.indices = indices;
  6173. }
  6174. public String getType() {
  6175. return type;
  6176. }
  6177. public int getIndicesLength() {
  6178. return indices.length;
  6179. }
  6180. public int getIndex(int i) {
  6181. return indices[i];
  6182. }
  6183. public int[] getIndices() {
  6184. return Arrays.copyOf(indices, indices.length);
  6185. }
  6186. static SubPartArguments create(String subPart) {
  6187. String[] splitArgs = subPart.split("\\[");
  6188. String type = splitArgs[0];
  6189. int[] indices = new int[splitArgs.length - 1];
  6190. for (int i = 0; i < indices.length; ++i) {
  6191. String tmp = splitArgs[i + 1];
  6192. indices[i] = Integer
  6193. .parseInt(tmp.substring(0, tmp.indexOf("]", 1)));
  6194. }
  6195. return new SubPartArguments(type, indices);
  6196. }
  6197. }
  6198. enum ScrollType {
  6199. ROW, SPACER, ROW_AND_SPACER
  6200. }
  6201. // abs(atan(y/x))*(180/PI) = n deg, x = 1, solve y
  6202. /**
  6203. * The solution to
  6204. * <code>|tan<sup>-1</sup>(<i>x</i>)|&times;(180/&pi;)&nbsp;=&nbsp;30</code>
  6205. * .
  6206. * <p>
  6207. * This constant is placed in the Escalator class, instead of an inner
  6208. * class, since even mathematical expressions aren't allowed in non-static
  6209. * inner classes for constants.
  6210. */
  6211. private static final double RATIO_OF_30_DEGREES = 1 / Math.sqrt(3);
  6212. /**
  6213. * The solution to
  6214. * <code>|tan<sup>-1</sup>(<i>x</i>)|&times;(180/&pi;)&nbsp;=&nbsp;40</code>
  6215. * .
  6216. * <p>
  6217. * This constant is placed in the Escalator class, instead of an inner
  6218. * class, since even mathematical expressions aren't allowed in non-static
  6219. * inner classes for constants.
  6220. */
  6221. private static final double RATIO_OF_40_DEGREES = Math.tan(2 * Math.PI / 9);
  6222. private static final String DEFAULT_WIDTH = "500.0px";
  6223. private static final String DEFAULT_HEIGHT = "400.0px";
  6224. private FlyweightRow flyweightRow = new FlyweightRow();
  6225. /** The {@code <thead/>} tag. */
  6226. private final TableSectionElement headElem = TableSectionElement
  6227. .as(DOM.createTHead());
  6228. /** The {@code <tbody/>} tag. */
  6229. private final TableSectionElement bodyElem = TableSectionElement
  6230. .as(DOM.createTBody());
  6231. /** The {@code <tfoot/>} tag. */
  6232. private final TableSectionElement footElem = TableSectionElement
  6233. .as(DOM.createTFoot());
  6234. /**
  6235. * TODO: investigate whether this field is now unnecessary, as
  6236. * {@link ScrollbarBundle} now caches its values.
  6237. *
  6238. * @deprecated maybe...
  6239. */
  6240. @Deprecated
  6241. private double tBodyScrollTop = 0;
  6242. /**
  6243. * TODO: investigate whether this field is now unnecessary, as
  6244. * {@link ScrollbarBundle} now caches its values.
  6245. *
  6246. * @deprecated maybe...
  6247. */
  6248. @Deprecated
  6249. private double tBodyScrollLeft = 0;
  6250. private final VerticalScrollbarBundle verticalScrollbar = new VerticalScrollbarBundle();
  6251. private final HorizontalScrollbarBundle horizontalScrollbar = new HorizontalScrollbarBundle();
  6252. private final AriaGridHelper ariaGridHelper = new AriaGridHelper();
  6253. private final HeaderRowContainer header = new HeaderRowContainer(headElem);
  6254. private final BodyRowContainerImpl body = new BodyRowContainerImpl(
  6255. bodyElem);
  6256. private final FooterRowContainer footer = new FooterRowContainer(footElem);
  6257. /**
  6258. * Flag for keeping track of {@link RowHeightChangedEvent}s
  6259. */
  6260. private boolean rowHeightChangedEventFired = false;
  6261. private final Scroller scroller = new Scroller();
  6262. private final ColumnConfigurationImpl columnConfiguration = new ColumnConfigurationImpl();
  6263. private final DivElement tableWrapper;
  6264. private final Element table;
  6265. private final DivElement horizontalScrollbarDeco = DivElement
  6266. .as(DOM.createDiv());
  6267. private final DivElement headerDeco = DivElement.as(DOM.createDiv());
  6268. private final DivElement footerDeco = DivElement.as(DOM.createDiv());
  6269. private final DivElement spacerDecoContainer = DivElement
  6270. .as(DOM.createDiv());
  6271. private PositionFunction position;
  6272. /** The cached width of the escalator, in pixels. */
  6273. private double widthOfEscalator = 0;
  6274. /** The cached height of the escalator, in pixels. */
  6275. private double heightOfEscalator = 0;
  6276. /** The height of Escalator in terms of body rows. */
  6277. private double heightByRows = 10.0d;
  6278. /** The height of Escalator, as defined by {@link #setHeight(String)} */
  6279. private String heightByCss = "";
  6280. private HeightMode heightMode = HeightMode.CSS;
  6281. private double delayToCancelTouchScroll = -1;
  6282. private boolean layoutIsScheduled = false;
  6283. private ScheduledCommand layoutCommand = () -> {
  6284. // ensure that row heights have been set or auto-detected if
  6285. // auto-detection is already possible, because visibility changes might
  6286. // not trigger the default check that happens in onLoad()
  6287. header.autodetectRowHeightLater();
  6288. body.autodetectRowHeightLater();
  6289. footer.autodetectRowHeightLater();
  6290. recalculateElementSizes();
  6291. layoutIsScheduled = false;
  6292. };
  6293. private final ElementPositionBookkeeper positions = new ElementPositionBookkeeper();
  6294. /**
  6295. * Creates a new Escalator widget instance.
  6296. */
  6297. public Escalator() {
  6298. detectAndApplyPositionFunction();
  6299. getLogger().info("Using " + position.getClass().getSimpleName()
  6300. + " for position");
  6301. final Element root = DOM.createDiv();
  6302. setElement(root);
  6303. setupScrollbars(root);
  6304. tableWrapper = DivElement.as(DOM.createDiv());
  6305. Event.sinkEvents(tableWrapper, Event.ONSCROLL | Event.KEYEVENTS);
  6306. Event.setEventListener(tableWrapper, event -> {
  6307. if (event.getKeyCode() != KeyCodes.KEY_TAB) {
  6308. return;
  6309. }
  6310. boolean browserScroll = tableWrapper.getScrollLeft() != 0
  6311. || tableWrapper.getScrollTop() != 0;
  6312. boolean keyEvent = event.getType().startsWith("key");
  6313. if (browserScroll || keyEvent) {
  6314. // Browser is scrolling our div automatically, reset
  6315. tableWrapper.setScrollLeft(0);
  6316. tableWrapper.setScrollTop(0);
  6317. Element focused = WidgetUtil.getFocusedElement();
  6318. Stream.of(header, body, footer).forEach(container -> {
  6319. Cell cell = container.getCell(focused);
  6320. if (cell == null) {
  6321. return;
  6322. }
  6323. scrollToColumn(cell.getColumn(), ScrollDestination.ANY, 0);
  6324. if (container == body) {
  6325. scrollToRow(cell.getRow(), ScrollDestination.ANY, 0);
  6326. }
  6327. });
  6328. }
  6329. });
  6330. root.appendChild(tableWrapper);
  6331. table = DOM.createTable();
  6332. tableWrapper.appendChild(table);
  6333. table.appendChild(headElem);
  6334. table.appendChild(bodyElem);
  6335. table.appendChild(footElem);
  6336. Style hCornerStyle = headerDeco.getStyle();
  6337. hCornerStyle.setWidth(verticalScrollbar.getScrollbarThickness(),
  6338. Unit.PX);
  6339. hCornerStyle.setDisplay(Display.NONE);
  6340. root.appendChild(headerDeco);
  6341. Style fCornerStyle = footerDeco.getStyle();
  6342. fCornerStyle.setWidth(verticalScrollbar.getScrollbarThickness(),
  6343. Unit.PX);
  6344. fCornerStyle.setDisplay(Display.NONE);
  6345. root.appendChild(footerDeco);
  6346. Style hWrapperStyle = horizontalScrollbarDeco.getStyle();
  6347. hWrapperStyle.setDisplay(Display.NONE);
  6348. hWrapperStyle.setHeight(horizontalScrollbar.getScrollbarThickness(),
  6349. Unit.PX);
  6350. root.appendChild(horizontalScrollbarDeco);
  6351. setStylePrimaryName("v-escalator");
  6352. spacerDecoContainer.setAttribute("aria-hidden", "true");
  6353. // init default dimensions
  6354. setHeight(null);
  6355. setWidth(null);
  6356. publishJSHelpers(root);
  6357. }
  6358. private double getBoundingWidth(Element element) {
  6359. // Gets the current width, including border and padding, for the element
  6360. // while ignoring any transforms applied to the element (e.g. scale)
  6361. return new ComputedStyle(element).getWidthIncludingBorderPadding();
  6362. }
  6363. private double getBoundingHeight(Element element) {
  6364. // Gets the current height, including border and padding, for the
  6365. // element while ignoring any transforms applied to the element (e.g.
  6366. // scale)
  6367. return new ComputedStyle(element).getHeightIncludingBorderPadding();
  6368. }
  6369. private int getBodyRowCount() {
  6370. return getBody().getRowCount();
  6371. }
  6372. private native void publishJSHelpers(Element root)
  6373. /*-{
  6374. var self = this;
  6375. root.getBodyRowCount = $entry(function () {
  6376. return self.@Escalator::getBodyRowCount()();
  6377. });
  6378. }-*/;
  6379. private void setupScrollbars(final Element root) {
  6380. ScrollHandler scrollHandler = event -> {
  6381. scroller.onScroll();
  6382. fireEvent(new ScrollEvent());
  6383. };
  6384. int scrollbarThickness = WidgetUtil.getNativeScrollbarSize();
  6385. if (BrowserInfo.get().isIE()) {
  6386. /*
  6387. * IE refuses to scroll properly if the DIV isn't at least one pixel
  6388. * larger than the scrollbar controls themselves.
  6389. */
  6390. scrollbarThickness += 1;
  6391. }
  6392. root.appendChild(verticalScrollbar.getElement());
  6393. verticalScrollbar.addScrollHandler(scrollHandler);
  6394. verticalScrollbar.setScrollbarThickness(scrollbarThickness);
  6395. verticalScrollbar
  6396. .addVisibilityHandler(new ScrollbarBundle.VisibilityHandler() {
  6397. private boolean queued = false;
  6398. @Override
  6399. public void visibilityChanged(
  6400. ScrollbarBundle.VisibilityChangeEvent event) {
  6401. if (queued) {
  6402. return;
  6403. }
  6404. queued = true;
  6405. /*
  6406. * We either lost or gained a scrollbar. In either case,
  6407. * we may need to update the column widths.
  6408. */
  6409. Scheduler.get().scheduleFinally(() -> {
  6410. fireVerticalScrollbarVisibilityChangeEvent();
  6411. queued = false;
  6412. });
  6413. }
  6414. });
  6415. root.appendChild(horizontalScrollbar.getElement());
  6416. horizontalScrollbar.addScrollHandler(scrollHandler);
  6417. horizontalScrollbar.setScrollbarThickness(scrollbarThickness);
  6418. horizontalScrollbar
  6419. .addVisibilityHandler(new ScrollbarBundle.VisibilityHandler() {
  6420. private boolean queued = false;
  6421. @Override
  6422. public void visibilityChanged(
  6423. ScrollbarBundle.VisibilityChangeEvent event) {
  6424. if (queued) {
  6425. return;
  6426. }
  6427. queued = true;
  6428. /*
  6429. * We either lost or gained a scrollbar. In any case, we
  6430. * need to change the height, if it's defined by rows.
  6431. */
  6432. Scheduler.get().scheduleFinally(() -> {
  6433. applyHeightByRows();
  6434. queued = false;
  6435. });
  6436. }
  6437. });
  6438. /*
  6439. * Because of all the IE hacks we've done above, we now have scrollbars
  6440. * hiding underneath a lot of DOM elements.
  6441. *
  6442. * This leads to problems with OSX (and many touch-only devices) when
  6443. * scrollbars are only shown when scrolling, as the scrollbar elements
  6444. * are hidden underneath everything. We trust that the scrollbars behave
  6445. * properly in these situations and simply pop them out with a bit of
  6446. * z-indexing.
  6447. */
  6448. if (WidgetUtil.getNativeScrollbarSize() == 0) {
  6449. verticalScrollbar.getElement().getStyle().setZIndex(90);
  6450. horizontalScrollbar.getElement().getStyle().setZIndex(90);
  6451. }
  6452. }
  6453. @Override
  6454. protected void onLoad() {
  6455. super.onLoad();
  6456. // ensure that row heights have been set or auto-detected if
  6457. // auto-detection is already possible, if not the check will be
  6458. // performed again in layoutCommand
  6459. header.autodetectRowHeightLater();
  6460. body.autodetectRowHeightLater();
  6461. footer.autodetectRowHeightLater();
  6462. header.paintInsertRows(0, header.getRowCount());
  6463. footer.paintInsertRows(0, footer.getRowCount());
  6464. boolean columnsChanged = false;
  6465. for (ColumnConfigurationImpl.Column column : columnConfiguration.columns) {
  6466. boolean columnChanged = column.measureAndSetWidthIfNeeded();
  6467. if (columnChanged) {
  6468. columnsChanged = true;
  6469. }
  6470. }
  6471. if (columnsChanged) {
  6472. header.reapplyColumnWidths();
  6473. body.reapplyColumnWidths();
  6474. footer.reapplyColumnWidths();
  6475. }
  6476. verticalScrollbar.onLoad();
  6477. horizontalScrollbar.onLoad();
  6478. scroller.attachScrollListener(verticalScrollbar.getElement());
  6479. scroller.attachScrollListener(horizontalScrollbar.getElement());
  6480. scroller.attachMousewheelListener(getElement());
  6481. if (isCurrentBrowserIE11OrEdge()) {
  6482. // Touch listeners doesn't work for IE11 and Edge (#18737)
  6483. scroller.attachPointerEventListeners(getElement());
  6484. } else {
  6485. scroller.attachTouchListeners(getElement());
  6486. }
  6487. /*
  6488. * Note: There's no need to explicitly insert rows into the body.
  6489. *
  6490. * recalculateElementSizes will recalculate the height of the body. This
  6491. * has the side-effect that as the body's size grows bigger (i.e. from 0
  6492. * to its actual height), more escalator rows are populated. Those
  6493. * escalator rows are then immediately rendered. This, in effect, is the
  6494. * same thing as inserting those rows.
  6495. *
  6496. * In fact, having an extra paintInsertRows here would lead to duplicate
  6497. * rows.
  6498. */
  6499. recalculateElementSizes();
  6500. }
  6501. @Override
  6502. protected void onUnload() {
  6503. scroller.detachScrollListener(verticalScrollbar.getElement());
  6504. scroller.detachScrollListener(horizontalScrollbar.getElement());
  6505. scroller.detachMousewheelListener(getElement());
  6506. if (isCurrentBrowserIE11OrEdge()) {
  6507. // Touch listeners doesn't work for IE11 and Edge (#18737)
  6508. scroller.detachPointerEventListeners(getElement());
  6509. } else {
  6510. scroller.detachTouchListeners(getElement());
  6511. }
  6512. /*
  6513. * We can call paintRemoveRows here, because static ranges are simple to
  6514. * remove.
  6515. */
  6516. header.paintRemoveRows(0, header.getRowCount());
  6517. footer.paintRemoveRows(0, footer.getRowCount());
  6518. /*
  6519. * We can't call body.paintRemoveRows since it relies on rowCount to be
  6520. * updated correctly. Since it isn't, we'll simply and brutally rip out
  6521. * the DOM elements (in an elegant way, of course).
  6522. */
  6523. int rowsToRemove = body.getDomRowCount();
  6524. for (int i = 0; i < rowsToRemove; i++) {
  6525. int index = rowsToRemove - i - 1;
  6526. TableRowElement tr = bodyElem.getRows().getItem(index);
  6527. body.paintRemoveRow(tr, index);
  6528. positions.remove(tr);
  6529. }
  6530. body.visualRowOrder.clear();
  6531. body.setTopRowLogicalIndex(0);
  6532. super.onUnload();
  6533. }
  6534. private void detectAndApplyPositionFunction() {
  6535. final Style docStyle = Document.get().getBody().getStyle();
  6536. if (hasProperty(docStyle, "transform")) {
  6537. if (hasProperty(docStyle, "transformStyle")) {
  6538. position = new Translate3DPosition();
  6539. } else {
  6540. position = new TranslatePosition();
  6541. }
  6542. } else if (hasProperty(docStyle, "webkitTransform")) {
  6543. position = new WebkitTranslate3DPosition();
  6544. }
  6545. }
  6546. private Logger getLogger() {
  6547. return Logger.getLogger(getClass().getName());
  6548. }
  6549. private static native boolean hasProperty(Style style, String name)
  6550. /*-{
  6551. return style[name] !== undefined;
  6552. }-*/;
  6553. /**
  6554. * Check whether there are both columns and any row data (for either
  6555. * headers, body or footer).
  6556. *
  6557. * @return <code>true</code> if header, body or footer has rows and there
  6558. * are columns
  6559. */
  6560. private boolean hasColumnAndRowData() {
  6561. return (header.getRowCount() > 0 || body.getRowCount() > 0
  6562. || footer.getRowCount() > 0)
  6563. && columnConfiguration.getColumnCount() > 0;
  6564. }
  6565. /**
  6566. * Check whether there are any cells in the DOM.
  6567. *
  6568. * @return <code>true</code> if header, body or footer has any child
  6569. * elements
  6570. */
  6571. private boolean hasSomethingInDom() {
  6572. return headElem.hasChildNodes() || bodyElem.hasChildNodes()
  6573. || footElem.hasChildNodes();
  6574. }
  6575. /**
  6576. * Returns the row container for the header in this Escalator.
  6577. *
  6578. * @return the header. Never <code>null</code>
  6579. */
  6580. public RowContainer getHeader() {
  6581. return header;
  6582. }
  6583. /**
  6584. * Returns the row container for the body in this Escalator.
  6585. *
  6586. * @return the body. Never <code>null</code>
  6587. */
  6588. public BodyRowContainer getBody() {
  6589. return body;
  6590. }
  6591. /**
  6592. * Returns the row container for the footer in this Escalator.
  6593. *
  6594. * @return the footer. Never <code>null</code>
  6595. */
  6596. public RowContainer getFooter() {
  6597. return footer;
  6598. }
  6599. /**
  6600. * Returns the configuration object for the columns in this Escalator.
  6601. *
  6602. * @return the configuration object for the columns in this Escalator. Never
  6603. * <code>null</code>
  6604. */
  6605. public ColumnConfiguration getColumnConfiguration() {
  6606. return columnConfiguration;
  6607. }
  6608. @Override
  6609. public void setWidth(final String width) {
  6610. String oldWidth = getElement().getStyle().getProperty("width");
  6611. if (width != null && !width.isEmpty()) {
  6612. super.setWidth(width);
  6613. if (!width.equals(oldWidth)) {
  6614. fireEscalatorSizeChangeEvent();
  6615. }
  6616. } else {
  6617. super.setWidth(DEFAULT_WIDTH);
  6618. if (!DEFAULT_WIDTH.equals(oldWidth)) {
  6619. fireEscalatorSizeChangeEvent();
  6620. }
  6621. }
  6622. recalculateElementSizes();
  6623. }
  6624. /**
  6625. * {@inheritDoc}
  6626. * <p>
  6627. * If Escalator is currently not in {@link HeightMode#CSS}, the given value
  6628. * is remembered, and applied once the mode is applied.
  6629. *
  6630. * @see #setHeightMode(HeightMode)
  6631. */
  6632. @Override
  6633. public void setHeight(String height) {
  6634. /*
  6635. * TODO remove method once RequiresResize and the Vaadin layoutmanager
  6636. * listening mechanisms are implemented
  6637. */
  6638. if (height != null && !height.isEmpty()) {
  6639. heightByCss = height;
  6640. } else {
  6641. if (getHeightMode() == HeightMode.UNDEFINED) {
  6642. heightByRows = body.getRowCount();
  6643. applyHeightByRows();
  6644. return;
  6645. } else {
  6646. heightByCss = DEFAULT_HEIGHT;
  6647. }
  6648. }
  6649. if (getHeightMode() == HeightMode.CSS) {
  6650. setHeightInternal(height);
  6651. }
  6652. }
  6653. private void setHeightInternal(final String height) {
  6654. final int escalatorRowsBefore = body.visualRowOrder.size();
  6655. if (height != null && !height.isEmpty()) {
  6656. String oldHeight = getElement().getStyle().getProperty("height");
  6657. super.setHeight(height);
  6658. if (!height.equals(oldHeight)) {
  6659. fireEscalatorSizeChangeEvent();
  6660. }
  6661. } else {
  6662. if (getHeightMode() == HeightMode.UNDEFINED) {
  6663. int newHeightByRows = body.getRowCount();
  6664. if (heightByRows != newHeightByRows) {
  6665. heightByRows = newHeightByRows;
  6666. applyHeightByRows();
  6667. }
  6668. return;
  6669. } else {
  6670. String oldHeight = getElement().getStyle()
  6671. .getProperty("height");
  6672. super.setHeight(DEFAULT_HEIGHT);
  6673. if (!DEFAULT_HEIGHT.equals(oldHeight)) {
  6674. fireEscalatorSizeChangeEvent();
  6675. }
  6676. }
  6677. }
  6678. recalculateElementSizes();
  6679. if (escalatorRowsBefore != body.visualRowOrder.size()) {
  6680. fireRowVisibilityChangeEvent();
  6681. }
  6682. }
  6683. /**
  6684. * Returns the vertical scroll offset. Note that this is not necessarily the
  6685. * same as the {@code scrollTop} attribute in the DOM.
  6686. *
  6687. * @return the logical vertical scroll offset
  6688. */
  6689. public double getScrollTop() {
  6690. return verticalScrollbar.getScrollPos();
  6691. }
  6692. /**
  6693. * Sets the vertical scroll offset. Note that this will not necessarily
  6694. * become the same as the {@code scrollTop} attribute in the DOM.
  6695. *
  6696. * @param scrollTop
  6697. * the number of pixels to scroll vertically
  6698. */
  6699. public void setScrollTop(final double scrollTop) {
  6700. verticalScrollbar.setScrollPos(scrollTop);
  6701. }
  6702. /**
  6703. * Returns the logical horizontal scroll offset. Note that this is not
  6704. * necessarily the same as the {@code scrollLeft} attribute in the DOM.
  6705. *
  6706. * @return the logical horizontal scroll offset
  6707. */
  6708. public double getScrollLeft() {
  6709. return horizontalScrollbar.getScrollPos();
  6710. }
  6711. /**
  6712. * Sets the logical horizontal scroll offset. Note that will not necessarily
  6713. * become the same as the {@code scrollLeft} attribute in the DOM.
  6714. *
  6715. * @param scrollLeft
  6716. * the number of pixels to scroll horizontally
  6717. */
  6718. public void setScrollLeft(final double scrollLeft) {
  6719. horizontalScrollbar.setScrollPos(scrollLeft);
  6720. }
  6721. /**
  6722. * Returns the scroll width for the escalator. Note that this is not
  6723. * necessary the same as {@code Element.scrollWidth} in the DOM.
  6724. *
  6725. * @since 7.5.0
  6726. * @return the scroll width in pixels
  6727. */
  6728. public double getScrollWidth() {
  6729. return horizontalScrollbar.getScrollSize();
  6730. }
  6731. /**
  6732. * Returns the scroll height for the escalator. Note that this is not
  6733. * necessary the same as {@code Element.scrollHeight} in the DOM.
  6734. *
  6735. * @since 7.5.0
  6736. * @return the scroll height in pixels
  6737. */
  6738. public double getScrollHeight() {
  6739. return verticalScrollbar.getScrollSize();
  6740. }
  6741. /**
  6742. * Scrolls the body horizontally so that the column at the given index is
  6743. * visible and there is at least {@code padding} pixels in the direction of
  6744. * the given scroll destination.
  6745. *
  6746. * @param columnIndex
  6747. * the index of the column to scroll to
  6748. * @param destination
  6749. * where the column should be aligned visually after scrolling
  6750. * @param padding
  6751. * the number pixels to place between the scrolled-to column and
  6752. * the viewport edge.
  6753. * @throws IndexOutOfBoundsException
  6754. * if {@code columnIndex} is not a valid index for an existing
  6755. * column
  6756. * @throws IllegalArgumentException
  6757. * if {@code destination} is {@link ScrollDestination#MIDDLE}
  6758. * and padding is nonzero; or if the indicated column is frozen;
  6759. * or if {@code destination == null}
  6760. */
  6761. public void scrollToColumn(final int columnIndex,
  6762. final ScrollDestination destination, final int padding)
  6763. throws IndexOutOfBoundsException, IllegalArgumentException {
  6764. validateScrollDestination(destination, padding);
  6765. verifyValidColumnIndex(columnIndex);
  6766. if (columnIndex < columnConfiguration.frozenColumns) {
  6767. throw new IllegalArgumentException(
  6768. "The given column index " + columnIndex + " is frozen.");
  6769. }
  6770. scroller.scrollToColumn(columnIndex, destination, padding);
  6771. }
  6772. private void verifyValidColumnIndex(final int columnIndex)
  6773. throws IndexOutOfBoundsException {
  6774. if (columnIndex < 0
  6775. || columnIndex >= columnConfiguration.getColumnCount()) {
  6776. throw new IndexOutOfBoundsException("The given column index "
  6777. + columnIndex + " does not exist.");
  6778. }
  6779. }
  6780. /**
  6781. * Scrolls the body vertically so that the row at the given index is visible
  6782. * and there is at least {@literal padding} pixels to the given scroll
  6783. * destination.
  6784. *
  6785. * @param rowIndex
  6786. * the index of the logical row to scroll to
  6787. * @param destination
  6788. * where the row should be aligned visually after scrolling
  6789. * @param padding
  6790. * the number pixels to place between the scrolled-to row and the
  6791. * viewport edge.
  6792. * @throws IndexOutOfBoundsException
  6793. * if {@code rowIndex} is not a valid index for an existing row
  6794. * @throws IllegalArgumentException
  6795. * if {@code destination} is {@link ScrollDestination#MIDDLE}
  6796. * and padding is nonzero; or if {@code destination == null}
  6797. * @see #scrollToRowAndSpacer(int, ScrollDestination, int)
  6798. * @see #scrollToSpacer(int, ScrollDestination, int)
  6799. */
  6800. public void scrollToRow(final int rowIndex,
  6801. final ScrollDestination destination, final int padding)
  6802. throws IndexOutOfBoundsException, IllegalArgumentException {
  6803. verifyValidRowIndex(rowIndex);
  6804. body.scrollToRowSpacerOrBoth(rowIndex, destination, padding,
  6805. ScrollType.ROW);
  6806. }
  6807. private void verifyValidRowIndex(final int rowIndex) {
  6808. if (rowIndex < 0 || rowIndex >= body.getRowCount()) {
  6809. throw new IndexOutOfBoundsException(
  6810. "The given row index " + rowIndex + " does not exist.");
  6811. }
  6812. }
  6813. /**
  6814. * Scrolls the body vertically so that the spacer at the given row index is
  6815. * visible and there is at least {@literal padding} pixels to the given
  6816. * scroll destination.
  6817. *
  6818. * @since 7.5.0
  6819. * @param spacerIndex
  6820. * the row index of the spacer to scroll to
  6821. * @param destination
  6822. * where the spacer should be aligned visually after scrolling
  6823. * @param padding
  6824. * the number of pixels to place between the scrolled-to spacer
  6825. * and the viewport edge
  6826. * @throws IllegalArgumentException
  6827. * if {@code spacerIndex} is not an opened spacer; or if
  6828. * {@code destination} is {@link ScrollDestination#MIDDLE} and
  6829. * padding is nonzero; or if {@code destination == null}
  6830. * @see #scrollToRow(int, ScrollDestination, int)
  6831. * @see #scrollToRowAndSpacer(int, ScrollDestination, int)
  6832. */
  6833. public void scrollToSpacer(final int spacerIndex,
  6834. ScrollDestination destination, final int padding)
  6835. throws IllegalArgumentException {
  6836. body.scrollToRowSpacerOrBoth(spacerIndex, destination, padding,
  6837. ScrollType.SPACER);
  6838. }
  6839. /**
  6840. * Scrolls vertically to a row and the spacer below it.
  6841. * <p>
  6842. * If a spacer is not open at that index, this method behaves like
  6843. * {@link #scrollToRow(int, ScrollDestination, int)}
  6844. *
  6845. * @since 7.5.0
  6846. * @param rowIndex
  6847. * the index of the logical row to scroll to. -1 takes the
  6848. * topmost spacer into account as well.
  6849. * @param destination
  6850. * where the row should be aligned visually after scrolling
  6851. * @param padding
  6852. * the number pixels to place between the scrolled-to row and the
  6853. * viewport edge.
  6854. * @see #scrollToRow(int, ScrollDestination, int)
  6855. * @see #scrollToSpacer(int, ScrollDestination, int)
  6856. * @throws IllegalArgumentException
  6857. * if {@code destination} is {@link ScrollDestination#MIDDLE}
  6858. * and {@code padding} is not zero; or if {@code rowIndex} is
  6859. * not a valid row index, or -1; or if
  6860. * {@code destination == null}; or if {@code rowIndex == -1} and
  6861. * there is no spacer open at that index.
  6862. */
  6863. public void scrollToRowAndSpacer(final int rowIndex,
  6864. final ScrollDestination destination, final int padding)
  6865. throws IllegalArgumentException {
  6866. if (rowIndex != -1) {
  6867. verifyValidRowIndex(rowIndex);
  6868. }
  6869. body.scrollToRowSpacerOrBoth(rowIndex, destination, padding,
  6870. ScrollType.ROW_AND_SPACER);
  6871. }
  6872. private static void validateScrollDestination(
  6873. final ScrollDestination destination, final int padding) {
  6874. if (destination == null) {
  6875. throw new IllegalArgumentException("Destination cannot be null");
  6876. }
  6877. if (destination == ScrollDestination.MIDDLE && padding != 0) {
  6878. throw new IllegalArgumentException(
  6879. "You cannot have a padding with a MIDDLE destination");
  6880. }
  6881. }
  6882. /**
  6883. * Recalculates the dimensions for all elements that require manual
  6884. * calculations. Also updates the dimension caches.
  6885. * <p>
  6886. * <em>Note:</em> This method has the <strong>side-effect</strong>
  6887. * automatically makes sure that an appropriate amount of escalator rows are
  6888. * present. So, if the body area grows, more <strong>escalator rows might be
  6889. * inserted</strong>. Conversely, if the body area shrinks,
  6890. * <strong>escalator rows might be removed</strong>.
  6891. */
  6892. private void recalculateElementSizes() {
  6893. if (!isAttached()) {
  6894. return;
  6895. }
  6896. Profiler.enter("Escalator.recalculateElementSizes");
  6897. widthOfEscalator = Math.max(0, getBoundingWidth(getElement()));
  6898. heightOfEscalator = Math.max(0, getBoundingHeight(getElement()));
  6899. header.recalculateSectionHeight();
  6900. body.recalculateSectionHeight();
  6901. footer.recalculateSectionHeight();
  6902. scroller.recalculateScrollbarsForVirtualViewport();
  6903. body.verifyEscalatorCount();
  6904. body.reapplySpacerWidths();
  6905. Profiler.leave("Escalator.recalculateElementSizes");
  6906. }
  6907. /**
  6908. * Snap deltas of x and y to the major four axes (up, down, left, right)
  6909. * with a threshold of a number of degrees from those axes.
  6910. *
  6911. * @param deltaX
  6912. * the delta in the x axis
  6913. * @param deltaY
  6914. * the delta in the y axis
  6915. * @param thresholdRatio
  6916. * the threshold in ratio (0..1) between x and y for when to snap
  6917. * @return a two-element array: <code>[snappedX, snappedY]</code>
  6918. */
  6919. private static double[] snapDeltas(final double deltaX, final double deltaY,
  6920. final double thresholdRatio) {
  6921. final double[] array = new double[2];
  6922. if (deltaX != 0 && deltaY != 0) {
  6923. final double aDeltaX = Math.abs(deltaX);
  6924. final double aDeltaY = Math.abs(deltaY);
  6925. final double yRatio = aDeltaY / aDeltaX;
  6926. final double xRatio = aDeltaX / aDeltaY;
  6927. array[0] = (xRatio < thresholdRatio) ? 0 : deltaX;
  6928. array[1] = (yRatio < thresholdRatio) ? 0 : deltaY;
  6929. } else {
  6930. array[0] = deltaX;
  6931. array[1] = deltaY;
  6932. }
  6933. return array;
  6934. }
  6935. /**
  6936. * FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
  6937. * <p>
  6938. * Adds an event handler that gets notified when the visibility of the
  6939. * vertical scrollbar changes.
  6940. *
  6941. * @param verticalScrollbarVisibilityChangeHandler
  6942. * the event handler
  6943. * @return a handler registration for the added handler
  6944. */
  6945. public HandlerRegistration addVerticalScrollbarVisibilityChangeHandler(
  6946. VerticalScrollbarVisibilityChangeHandler verticalScrollbarVisibilityChangeHandler) {
  6947. return addHandler(verticalScrollbarVisibilityChangeHandler,
  6948. VerticalScrollbarVisibilityChangeEvent.TYPE);
  6949. }
  6950. private void fireVerticalScrollbarVisibilityChangeEvent() {
  6951. fireEvent(new VerticalScrollbarVisibilityChangeEvent());
  6952. }
  6953. /**
  6954. * FOR INTERNAL USE ONLY, MAY GET REMOVED OR MODIFIED AT ANY TIME!
  6955. * <p>
  6956. * Adds an event handler that gets notified when the Escalator size changes.
  6957. *
  6958. * @param escalatorSizeChangeHandler
  6959. * the event handler
  6960. * @return a handler registration for the added handler
  6961. */
  6962. public HandlerRegistration addEscalatorSizeChangeHandler(
  6963. EscalatorSizeChangeHandler escalatorSizeChangeHandler) {
  6964. return addHandler(escalatorSizeChangeHandler,
  6965. EscalatorSizeChangeEvent.TYPE);
  6966. }
  6967. private void fireEscalatorSizeChangeEvent() {
  6968. fireEvent(new EscalatorSizeChangeEvent());
  6969. }
  6970. /**
  6971. * Adds an event handler that gets notified when the range of visible rows
  6972. * changes e.g. because of scrolling, row resizing or spacers
  6973. * appearing/disappearing.
  6974. *
  6975. * @param rowVisibilityChangeHandler
  6976. * the event handler
  6977. * @return a handler registration for the added handler
  6978. */
  6979. public HandlerRegistration addRowVisibilityChangeHandler(
  6980. RowVisibilityChangeHandler rowVisibilityChangeHandler) {
  6981. return addHandler(rowVisibilityChangeHandler,
  6982. RowVisibilityChangeEvent.TYPE);
  6983. }
  6984. private void fireRowVisibilityChangeEvent() {
  6985. if (!body.visualRowOrder.isEmpty()) {
  6986. int visibleRangeStart = body.getTopRowLogicalIndex();
  6987. int visibleRowCount = body.visualRowOrder.size();
  6988. fireEvent(new RowVisibilityChangeEvent(visibleRangeStart,
  6989. visibleRowCount));
  6990. } else {
  6991. fireEvent(new RowVisibilityChangeEvent(0, 0));
  6992. }
  6993. }
  6994. /**
  6995. * Gets the logical index range of currently visible rows.
  6996. *
  6997. * @return logical index range of visible rows
  6998. */
  6999. public Range getVisibleRowRange() {
  7000. if (!body.visualRowOrder.isEmpty()) {
  7001. return Range.withLength(body.getTopRowLogicalIndex(),
  7002. body.visualRowOrder.size());
  7003. } else {
  7004. return Range.withLength(0, 0);
  7005. }
  7006. }
  7007. /**
  7008. * Returns the widget from a cell node or <code>null</code> if there is no
  7009. * widget in the cell
  7010. *
  7011. * @param cellNode
  7012. * The cell node
  7013. */
  7014. static Widget getWidgetFromCell(Node cellNode) {
  7015. Node possibleWidgetNode = cellNode.getFirstChild();
  7016. if (possibleWidgetNode != null
  7017. && possibleWidgetNode.getNodeType() == Node.ELEMENT_NODE) {
  7018. @SuppressWarnings("deprecation")
  7019. com.google.gwt.user.client.Element castElement = (com.google.gwt.user.client.Element) possibleWidgetNode
  7020. .cast();
  7021. Widget w = WidgetUtil.findWidget(castElement);
  7022. // Ensure findWidget did not traverse past the cell element in the
  7023. // DOM hierarchy
  7024. if (cellNode.isOrHasChild(w.getElement())) {
  7025. return w;
  7026. }
  7027. }
  7028. return null;
  7029. }
  7030. @Override
  7031. public void setStylePrimaryName(String style) {
  7032. super.setStylePrimaryName(style);
  7033. verticalScrollbar.setStylePrimaryName(style);
  7034. horizontalScrollbar.setStylePrimaryName(style);
  7035. UIObject.setStylePrimaryName(tableWrapper, style + "-tablewrapper");
  7036. UIObject.setStylePrimaryName(headerDeco, style + "-header-deco");
  7037. UIObject.setStylePrimaryName(footerDeco, style + "-footer-deco");
  7038. UIObject.setStylePrimaryName(horizontalScrollbarDeco,
  7039. style + "-horizontal-scrollbar-deco");
  7040. UIObject.setStylePrimaryName(spacerDecoContainer,
  7041. style + "-spacer-deco-container");
  7042. header.setStylePrimaryName(style);
  7043. body.setStylePrimaryName(style);
  7044. footer.setStylePrimaryName(style);
  7045. }
  7046. /**
  7047. * Sets the number of rows that should be visible in Escalator's body, while
  7048. * {@link #getHeightMode()} is {@link HeightMode#ROW}.
  7049. * <p>
  7050. * If Escalator is currently not in {@link HeightMode#ROW}, the given value
  7051. * is remembered, and applied once the mode is applied.
  7052. *
  7053. * @param rows
  7054. * the number of rows that should be visible in Escalator's body
  7055. * @throws IllegalArgumentException
  7056. * if {@code rows} is &leq; 0, {@link Double#isInfinite(double)
  7057. * infinite} or {@link Double#isNaN(double) NaN}.
  7058. * @see #setHeightMode(HeightMode)
  7059. */
  7060. public void setHeightByRows(double rows) throws IllegalArgumentException {
  7061. if (heightMode == HeightMode.UNDEFINED && body.insertingOrRemoving) {
  7062. // this will be called again once the operation is finished, ignore
  7063. // for now
  7064. return;
  7065. }
  7066. if (rows < 0) {
  7067. throw new IllegalArgumentException(
  7068. "The number of rows must be a positive number.");
  7069. } else if (Double.isInfinite(rows)) {
  7070. throw new IllegalArgumentException(
  7071. "The number of rows must be finite.");
  7072. } else if (Double.isNaN(rows)) {
  7073. throw new IllegalArgumentException("The number must not be NaN.");
  7074. }
  7075. heightByRows = rows;
  7076. applyHeightByRows();
  7077. }
  7078. /**
  7079. * Gets the amount of rows in Escalator's body that are shown, while
  7080. * {@link #getHeightMode()} is {@link HeightMode#ROW}.
  7081. * <p>
  7082. * By default, it is 10.
  7083. *
  7084. * @return the amount of rows that are being shown in Escalator's body
  7085. * @see #setHeightByRows(double)
  7086. */
  7087. public double getHeightByRows() {
  7088. return heightByRows;
  7089. }
  7090. /**
  7091. * Reapplies the row-based height of the Grid, if Grid currently should
  7092. * define its height that way.
  7093. */
  7094. private void applyHeightByRows() {
  7095. if (heightMode != HeightMode.ROW
  7096. && heightMode != HeightMode.UNDEFINED) {
  7097. return;
  7098. }
  7099. double headerHeight = header.getHeightOfSection();
  7100. double footerHeight = footer.getHeightOfSection();
  7101. double bodyHeight = body.getDefaultRowHeight() * heightByRows;
  7102. double scrollbar = horizontalScrollbar.showsScrollHandle()
  7103. ? horizontalScrollbar.getScrollbarThickness()
  7104. : 0;
  7105. double spacerHeight = 0; // ignored if HeightMode.ROW
  7106. if (heightMode == HeightMode.UNDEFINED) {
  7107. spacerHeight = body.spacerContainer.getSpacerHeightsSum();
  7108. }
  7109. double totalHeight = headerHeight + bodyHeight + spacerHeight
  7110. + scrollbar + footerHeight;
  7111. setHeightInternal(totalHeight + "px");
  7112. }
  7113. /**
  7114. * Defines the mode in which the Escalator widget's height is calculated.
  7115. * <p>
  7116. * If {@link HeightMode#CSS} is given, Escalator will respect the values
  7117. * given via {@link #setHeight(String)}, and behave as a traditional Widget.
  7118. * <p>
  7119. * If {@link HeightMode#ROW} is given, Escalator will make sure that the
  7120. * {@link #getBody() body} will display as many rows as
  7121. * {@link #getHeightByRows()} defines. <em>Note:</em> If headers/footers are
  7122. * inserted or removed, the widget will resize itself to still display the
  7123. * required amount of rows in its body. It also takes the horizontal
  7124. * scrollbar into account.
  7125. *
  7126. * @param heightMode
  7127. * the mode in to which Escalator should be set
  7128. */
  7129. public void setHeightMode(HeightMode heightMode) {
  7130. /*
  7131. * This method is a workaround for the fact that Vaadin re-applies
  7132. * widget dimensions (height/width) on each state change event. The
  7133. * original design was to have setHeight an setHeightByRow be equals,
  7134. * and whichever was called the latest was considered in effect.
  7135. *
  7136. * But, because of Vaadin always calling setHeight on the widget, this
  7137. * approach doesn't work.
  7138. */
  7139. if (heightMode != this.heightMode) {
  7140. this.heightMode = heightMode;
  7141. switch (this.heightMode) {
  7142. case CSS:
  7143. setHeight(heightByCss);
  7144. break;
  7145. case ROW:
  7146. setHeightByRows(heightByRows);
  7147. break;
  7148. case UNDEFINED:
  7149. setHeightByRows(body.getRowCount());
  7150. break;
  7151. default:
  7152. throw new IllegalStateException("Unimplemented feature "
  7153. + "- unknown HeightMode: " + this.heightMode);
  7154. }
  7155. }
  7156. }
  7157. /**
  7158. * Returns the current {@link HeightMode} the Escalator is in.
  7159. * <p>
  7160. * Defaults to {@link HeightMode#CSS}.
  7161. *
  7162. * @return the current HeightMode
  7163. */
  7164. public HeightMode getHeightMode() {
  7165. return heightMode;
  7166. }
  7167. /**
  7168. * Returns the {@link RowContainer} which contains the element.
  7169. *
  7170. * @param element
  7171. * the element to check for
  7172. * @return the container the element is in or <code>null</code> if element
  7173. * is not present in any container.
  7174. */
  7175. public RowContainer findRowContainer(Element element) {
  7176. if (getHeader().getElement() != element
  7177. && getHeader().getElement().isOrHasChild(element)) {
  7178. return getHeader();
  7179. } else if (getBody().getElement() != element
  7180. && getBody().getElement().isOrHasChild(element)) {
  7181. return getBody();
  7182. } else if (getFooter().getElement() != element
  7183. && getFooter().getElement().isOrHasChild(element)) {
  7184. return getFooter();
  7185. }
  7186. return null;
  7187. }
  7188. /**
  7189. * Sets whether a scroll direction is locked or not.
  7190. * <p>
  7191. * If a direction is locked, the escalator will refuse to scroll in that
  7192. * direction.
  7193. *
  7194. * @param direction
  7195. * the orientation of the scroll to set the lock status
  7196. * @param locked
  7197. * <code>true</code> to lock, <code>false</code> to unlock
  7198. */
  7199. public void setScrollLocked(ScrollbarBundle.Direction direction,
  7200. boolean locked) {
  7201. switch (direction) {
  7202. case HORIZONTAL:
  7203. horizontalScrollbar.setLocked(locked);
  7204. break;
  7205. case VERTICAL:
  7206. verticalScrollbar.setLocked(locked);
  7207. break;
  7208. default:
  7209. throw new UnsupportedOperationException(
  7210. "Unexpected value: " + direction);
  7211. }
  7212. }
  7213. /**
  7214. * Checks whether or not an direction is locked for scrolling.
  7215. *
  7216. * @param direction
  7217. * the direction of the scroll of which to check the lock status
  7218. * @return <code>true</code> if the direction is locked
  7219. */
  7220. public boolean isScrollLocked(ScrollbarBundle.Direction direction) {
  7221. switch (direction) {
  7222. case HORIZONTAL:
  7223. return horizontalScrollbar.isLocked();
  7224. case VERTICAL:
  7225. return verticalScrollbar.isLocked();
  7226. default:
  7227. throw new UnsupportedOperationException(
  7228. "Unexpected value: " + direction);
  7229. }
  7230. }
  7231. /**
  7232. * Adds a scroll handler to this escalator.
  7233. *
  7234. * @param handler
  7235. * the scroll handler to add
  7236. * @return a handler registration for the registered scroll handler
  7237. */
  7238. public HandlerRegistration addScrollHandler(ScrollHandler handler) {
  7239. return addHandler(handler, ScrollEvent.TYPE);
  7240. }
  7241. /**
  7242. * Returns true if the Escalator is currently scrolling by touch, or has not
  7243. * made the decision yet whether to accept touch actions as scrolling or
  7244. * not.
  7245. *
  7246. * @see #setDelayToCancelTouchScroll(double)
  7247. *
  7248. * @return true when the component is touch scrolling at the moment
  7249. * @since 8.1
  7250. */
  7251. public boolean isTouchScrolling() {
  7252. return scroller.touchHandlerBundle.touching;
  7253. }
  7254. /**
  7255. * Returns the time after which to not consider a touch event a scroll event
  7256. * if the user has not moved the touch. This can be used to differentiate
  7257. * between quick touch move (scrolling) and long tap (e.g. context menu or
  7258. * drag and drop operation).
  7259. *
  7260. * @return delay in milliseconds after which to cancel touch scrolling if
  7261. * there is no movement, -1 means scrolling is always allowed
  7262. * @since 8.1
  7263. */
  7264. public double getDelayToCancelTouchScroll() {
  7265. return delayToCancelTouchScroll;
  7266. }
  7267. /**
  7268. * Sets the time after which to not consider a touch event a scroll event if
  7269. * the user has not moved the touch. This can be used to differentiate
  7270. * between quick touch move (scrolling) and long tap (e.g. context menu or
  7271. * drag and drop operation).
  7272. *
  7273. * @param delayToCancelTouchScroll
  7274. * delay in milliseconds after which to cancel touch scrolling if
  7275. * there is no movement, -1 to always allow scrolling
  7276. * @since 8.1
  7277. */
  7278. public void setDelayToCancelTouchScroll(double delayToCancelTouchScroll) {
  7279. this.delayToCancelTouchScroll = delayToCancelTouchScroll;
  7280. }
  7281. @Override
  7282. public boolean isWorkPending() {
  7283. return body.domSorter.waiting || verticalScrollbar.isWorkPending()
  7284. || horizontalScrollbar.isWorkPending() || layoutIsScheduled;
  7285. }
  7286. @Override
  7287. public void onResize() {
  7288. if (isAttached() && !layoutIsScheduled) {
  7289. layoutIsScheduled = true;
  7290. Scheduler.get().scheduleFinally(layoutCommand);
  7291. }
  7292. }
  7293. /**
  7294. * Gets the maximum number of body rows that can be visible on the screen at
  7295. * once.
  7296. *
  7297. * @return the maximum capacity
  7298. */
  7299. public int getMaxVisibleRowCount() {
  7300. return body.getMaxVisibleRowCount();
  7301. }
  7302. /**
  7303. * Gets the escalator's inner width. This is the entire width in pixels,
  7304. * without the vertical scrollbar.
  7305. *
  7306. * @return escalator's inner width
  7307. */
  7308. public double getInnerWidth() {
  7309. return getBoundingWidth(tableWrapper);
  7310. }
  7311. /**
  7312. * Resets all cached pixel sizes and reads new values from the DOM. This
  7313. * methods should be used e.g. when styles affecting the dimensions of
  7314. * elements in this escalator have been changed.
  7315. */
  7316. public void resetSizesFromDom() {
  7317. header.autodetectRowHeightNow();
  7318. body.autodetectRowHeightNow();
  7319. footer.autodetectRowHeightNow();
  7320. for (int i = 0; i < columnConfiguration.getColumnCount(); i++) {
  7321. columnConfiguration.setColumnWidth(i,
  7322. columnConfiguration.getColumnWidth(i));
  7323. }
  7324. }
  7325. private Range getViewportPixels() {
  7326. int from = (int) Math.floor(verticalScrollbar.getScrollPos());
  7327. int to = (int) body.getHeightOfSection();
  7328. return Range.withLength(from, to);
  7329. }
  7330. @Override
  7331. @SuppressWarnings("deprecation")
  7332. public com.google.gwt.user.client.Element getSubPartElement(
  7333. String subPart) {
  7334. SubPartArguments args = SubPartArguments.create(subPart);
  7335. Element tableStructureElement = getSubPartElementTableStructure(args);
  7336. if (tableStructureElement != null) {
  7337. return DOM.asOld(tableStructureElement);
  7338. }
  7339. Element spacerElement = getSubPartElementSpacer(args);
  7340. if (spacerElement != null) {
  7341. return DOM.asOld(spacerElement);
  7342. }
  7343. return null;
  7344. }
  7345. /**
  7346. * Returns the {@code <div class="{primary-stylename}-tablewrapper" />}
  7347. * element which has the table inside it. {primary-stylename} is .e.g
  7348. * {@code v-grid}.
  7349. * <p>
  7350. * <em>NOTE: you should not do any modifications to the returned element.
  7351. * This API is only available for querying data from the element.</em>
  7352. *
  7353. * @return the table wrapper element
  7354. * @since 8.1
  7355. */
  7356. public Element getTableWrapper() {
  7357. return tableWrapper;
  7358. }
  7359. /**
  7360. * Returns the <code>&lt;table&gt;</code> element of the grid.
  7361. *
  7362. * @return the table element
  7363. * @since 8.2
  7364. */
  7365. public Element getTable() {
  7366. return table;
  7367. }
  7368. private Element getSubPartElementTableStructure(SubPartArguments args) {
  7369. String type = args.getType();
  7370. int[] indices = args.getIndices();
  7371. // Get correct RowContainer for type from Escalator
  7372. RowContainer container = null;
  7373. if (type.equalsIgnoreCase("header")) {
  7374. container = getHeader();
  7375. } else if (type.equalsIgnoreCase("cell")) {
  7376. if (indices.length > 0) {
  7377. // If wanted row is not visible, we need to scroll there.
  7378. // Scrolling might be a no-op if row is already in the viewport.
  7379. scrollToRow(indices[0], ScrollDestination.ANY, 0);
  7380. }
  7381. container = getBody();
  7382. } else if (type.equalsIgnoreCase("footer")) {
  7383. container = getFooter();
  7384. }
  7385. if (null != container) {
  7386. if (indices.length == 0) {
  7387. // No indexing. Just return the wanted container element
  7388. return container.getElement();
  7389. } else {
  7390. try {
  7391. return getSubPart(container, indices);
  7392. } catch (Exception e) {
  7393. getLogger().log(Level.SEVERE, e.getMessage());
  7394. }
  7395. }
  7396. }
  7397. return null;
  7398. }
  7399. private Element getSubPart(RowContainer container, int[] indices) {
  7400. Element targetElement = container.getRowElement(indices[0]);
  7401. // Scroll wanted column to view if able
  7402. if (indices.length > 1 && targetElement != null) {
  7403. if (getColumnConfiguration().getFrozenColumnCount() <= indices[1]) {
  7404. scrollToColumn(indices[1], ScrollDestination.ANY, 0);
  7405. }
  7406. targetElement = getCellFromRow(TableRowElement.as(targetElement),
  7407. indices[1]);
  7408. for (int i = 2; i < indices.length && targetElement != null; ++i) {
  7409. targetElement = (Element) targetElement.getChild(indices[i]);
  7410. }
  7411. }
  7412. return targetElement;
  7413. }
  7414. private static Element getCellFromRow(TableRowElement rowElement,
  7415. int index) {
  7416. int childCount = rowElement.getCells().getLength();
  7417. if (index < 0 || index >= childCount) {
  7418. return null;
  7419. }
  7420. TableCellElement currentCell = null;
  7421. boolean indexInColspan = false;
  7422. int i = 0;
  7423. while (!indexInColspan) {
  7424. currentCell = rowElement.getCells().getItem(i);
  7425. // Calculate if this is the cell we are looking for
  7426. int colSpan = currentCell.getColSpan();
  7427. indexInColspan = index < colSpan + i;
  7428. // Increment by colspan to skip over hidden cells
  7429. i += colSpan;
  7430. }
  7431. return currentCell;
  7432. }
  7433. private Element getSubPartElementSpacer(SubPartArguments args) {
  7434. if ("spacer".equals(args.getType()) && args.getIndicesLength() == 1) {
  7435. // If spacer's row is not visible, we need to scroll there.
  7436. // Scrolling might be a no-op if row is already in the viewport.
  7437. scrollToSpacer(args.getIndex(0), ScrollDestination.ANY, 0);
  7438. return body.spacerContainer.getSubPartElement(args.getIndex(0));
  7439. } else {
  7440. return null;
  7441. }
  7442. }
  7443. @Override
  7444. @SuppressWarnings("deprecation")
  7445. public String getSubPartName(
  7446. com.google.gwt.user.client.Element subElement) {
  7447. /*
  7448. * The spacer check needs to be before table structure check, because
  7449. * (for now) the table structure will take spacer elements into account
  7450. * as well, when it shouldn't.
  7451. */
  7452. String spacer = getSubPartNameSpacer(subElement);
  7453. if (spacer != null) {
  7454. return spacer;
  7455. }
  7456. String tableStructure = getSubPartNameTableStructure(subElement);
  7457. if (tableStructure != null) {
  7458. return tableStructure;
  7459. }
  7460. return null;
  7461. }
  7462. private String getSubPartNameTableStructure(Element subElement) {
  7463. List<RowContainer> containers = Arrays.asList(getHeader(), getBody(),
  7464. getFooter());
  7465. List<String> containerType = Arrays.asList("header", "cell", "footer");
  7466. for (int i = 0; i < containers.size(); ++i) {
  7467. RowContainer container = containers.get(i);
  7468. boolean containerRow = (subElement.getTagName()
  7469. .equalsIgnoreCase("tr")
  7470. && subElement.getParentElement() == container.getElement());
  7471. if (containerRow) {
  7472. /*
  7473. * Wanted SubPart is row that is a child of containers root to
  7474. * get indices, we use a cell that is a child of this row
  7475. */
  7476. subElement = subElement.getFirstChildElement();
  7477. }
  7478. Cell cell = container.getCell(subElement);
  7479. if (cell != null) {
  7480. // Skip the column index if subElement was a child of root
  7481. return containerType.get(i) + "[" + cell.getRow()
  7482. + (containerRow ? "]" : "][" + cell.getColumn() + "]");
  7483. }
  7484. }
  7485. return null;
  7486. }
  7487. private String getSubPartNameSpacer(Element subElement) {
  7488. return body.spacerContainer.getSubPartName(subElement);
  7489. }
  7490. private void logWarning(String message) {
  7491. getLogger().warning(message);
  7492. }
  7493. /**
  7494. * This is an internal method for calculating minimum width for Column
  7495. * resize.
  7496. *
  7497. * @return minimum width for column
  7498. */
  7499. double getMinCellWidth(int colIndex) {
  7500. return columnConfiguration.getMinCellWidth(colIndex);
  7501. }
  7502. /**
  7503. * Internal method for checking whether the browser is IE11 or Edge
  7504. *
  7505. * @return true only if the current browser is IE11, or Edge
  7506. */
  7507. private static boolean isCurrentBrowserIE11OrEdge() {
  7508. return BrowserInfo.get().isIE11() || BrowserInfo.get().isEdge();
  7509. }
  7510. }