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 276KB

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