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.

VScrollTable.java 308KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334
  1. /*
  2. * Copyright 2000-2014 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.ui;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.util.Iterator;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.google.gwt.core.client.JavaScriptObject;
  29. import com.google.gwt.core.client.Scheduler;
  30. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  31. import com.google.gwt.dom.client.Document;
  32. import com.google.gwt.dom.client.Element;
  33. import com.google.gwt.dom.client.NativeEvent;
  34. import com.google.gwt.dom.client.Node;
  35. import com.google.gwt.dom.client.NodeList;
  36. import com.google.gwt.dom.client.Style;
  37. import com.google.gwt.dom.client.Style.Display;
  38. import com.google.gwt.dom.client.Style.Overflow;
  39. import com.google.gwt.dom.client.Style.Position;
  40. import com.google.gwt.dom.client.Style.TextAlign;
  41. import com.google.gwt.dom.client.Style.Unit;
  42. import com.google.gwt.dom.client.Style.Visibility;
  43. import com.google.gwt.dom.client.TableCellElement;
  44. import com.google.gwt.dom.client.TableRowElement;
  45. import com.google.gwt.dom.client.TableSectionElement;
  46. import com.google.gwt.dom.client.Touch;
  47. import com.google.gwt.event.dom.client.BlurEvent;
  48. import com.google.gwt.event.dom.client.BlurHandler;
  49. import com.google.gwt.event.dom.client.FocusEvent;
  50. import com.google.gwt.event.dom.client.FocusHandler;
  51. import com.google.gwt.event.dom.client.KeyCodes;
  52. import com.google.gwt.event.dom.client.KeyDownEvent;
  53. import com.google.gwt.event.dom.client.KeyDownHandler;
  54. import com.google.gwt.event.dom.client.KeyPressEvent;
  55. import com.google.gwt.event.dom.client.KeyPressHandler;
  56. import com.google.gwt.event.dom.client.KeyUpEvent;
  57. import com.google.gwt.event.dom.client.KeyUpHandler;
  58. import com.google.gwt.event.dom.client.ScrollEvent;
  59. import com.google.gwt.event.dom.client.ScrollHandler;
  60. import com.google.gwt.event.logical.shared.CloseEvent;
  61. import com.google.gwt.event.logical.shared.CloseHandler;
  62. import com.google.gwt.event.shared.HandlerRegistration;
  63. import com.google.gwt.regexp.shared.MatchResult;
  64. import com.google.gwt.regexp.shared.RegExp;
  65. import com.google.gwt.user.client.Command;
  66. import com.google.gwt.user.client.DOM;
  67. import com.google.gwt.user.client.Event;
  68. import com.google.gwt.user.client.Event.NativePreviewEvent;
  69. import com.google.gwt.user.client.Event.NativePreviewHandler;
  70. import com.google.gwt.user.client.Timer;
  71. import com.google.gwt.user.client.Window;
  72. import com.google.gwt.user.client.ui.FlowPanel;
  73. import com.google.gwt.user.client.ui.HasWidgets;
  74. import com.google.gwt.user.client.ui.Panel;
  75. import com.google.gwt.user.client.ui.PopupPanel;
  76. import com.google.gwt.user.client.ui.UIObject;
  77. import com.google.gwt.user.client.ui.Widget;
  78. import com.vaadin.client.ApplicationConnection;
  79. import com.vaadin.client.BrowserInfo;
  80. import com.vaadin.client.ComponentConnector;
  81. import com.vaadin.client.ConnectorMap;
  82. import com.vaadin.client.DeferredWorker;
  83. import com.vaadin.client.Focusable;
  84. import com.vaadin.client.MouseEventDetailsBuilder;
  85. import com.vaadin.client.StyleConstants;
  86. import com.vaadin.client.TooltipInfo;
  87. import com.vaadin.client.UIDL;
  88. import com.vaadin.client.Util;
  89. import com.vaadin.client.VConsole;
  90. import com.vaadin.client.VTooltip;
  91. import com.vaadin.client.WidgetUtil;
  92. import com.vaadin.client.ui.VScrollTable.VScrollTableBody.VScrollTableRow;
  93. import com.vaadin.client.ui.dd.DDUtil;
  94. import com.vaadin.client.ui.dd.VAbstractDropHandler;
  95. import com.vaadin.client.ui.dd.VAcceptCallback;
  96. import com.vaadin.client.ui.dd.VDragAndDropManager;
  97. import com.vaadin.client.ui.dd.VDragEvent;
  98. import com.vaadin.client.ui.dd.VHasDropHandler;
  99. import com.vaadin.client.ui.dd.VTransferable;
  100. import com.vaadin.shared.AbstractComponentState;
  101. import com.vaadin.shared.MouseEventDetails;
  102. import com.vaadin.shared.ui.dd.VerticalDropLocation;
  103. import com.vaadin.shared.ui.table.TableConstants;
  104. /**
  105. * VScrollTable
  106. *
  107. * VScrollTable is a FlowPanel having two widgets in it: * TableHead component *
  108. * ScrollPanel
  109. *
  110. * TableHead contains table's header and widgets + logic for resizing,
  111. * reordering and hiding columns.
  112. *
  113. * ScrollPanel contains VScrollTableBody object which handles content. To save
  114. * some bandwidth and to improve clients responsiveness with loads of data, in
  115. * VScrollTableBody all rows are not necessary rendered. There are "spacers" in
  116. * VScrollTableBody to use the exact same space as non-rendered rows would use.
  117. * This way we can use seamlessly traditional scrollbars and scrolling to fetch
  118. * more rows instead of "paging".
  119. *
  120. * In VScrollTable we listen to scroll events. On horizontal scrolling we also
  121. * update TableHeads scroll position which has its scrollbars hidden. On
  122. * vertical scroll events we will check if we are reaching the end of area where
  123. * we have rows rendered and
  124. *
  125. * TODO implement unregistering for child components in Cells
  126. */
  127. public class VScrollTable extends FlowPanel implements HasWidgets,
  128. ScrollHandler, VHasDropHandler, FocusHandler, BlurHandler, Focusable,
  129. ActionOwner, SubPartAware, DeferredWorker {
  130. /**
  131. * Simple interface for parts of the table capable of owning a context menu.
  132. *
  133. * @since 7.2
  134. * @author Vaadin Ltd
  135. */
  136. private interface ContextMenuOwner {
  137. public void showContextMenu(Event event);
  138. }
  139. /**
  140. * Handles showing context menu on "long press" from a touch screen.
  141. *
  142. * @since 7.2
  143. * @author Vaadin Ltd
  144. */
  145. private class TouchContextProvider {
  146. private static final int TOUCH_CONTEXT_MENU_TIMEOUT = 500;
  147. private Timer contextTouchTimeout;
  148. private Event touchStart;
  149. private int touchStartY;
  150. private int touchStartX;
  151. private ContextMenuOwner target;
  152. /**
  153. * Initializes a handler for a certain context menu owner.
  154. *
  155. * @param target
  156. * the owner of the context menu
  157. */
  158. public TouchContextProvider(ContextMenuOwner target) {
  159. this.target = target;
  160. }
  161. /**
  162. * Cancels the current context touch timeout.
  163. */
  164. public void cancel() {
  165. if (contextTouchTimeout != null) {
  166. contextTouchTimeout.cancel();
  167. contextTouchTimeout = null;
  168. }
  169. touchStart = null;
  170. }
  171. /**
  172. * A function to handle touch context events in a table.
  173. *
  174. * @param event
  175. * browser event to handle
  176. */
  177. public void handleTouchEvent(final Event event) {
  178. int type = event.getTypeInt();
  179. switch (type) {
  180. case Event.ONCONTEXTMENU:
  181. target.showContextMenu(event);
  182. break;
  183. case Event.ONTOUCHSTART:
  184. // save position to fields, touches in events are same
  185. // instance during the operation.
  186. touchStart = event;
  187. Touch touch = event.getChangedTouches().get(0);
  188. touchStartX = touch.getClientX();
  189. touchStartY = touch.getClientY();
  190. if (contextTouchTimeout == null) {
  191. contextTouchTimeout = new Timer() {
  192. @Override
  193. public void run() {
  194. if (touchStart != null) {
  195. // Open the context menu if finger
  196. // is held in place long enough.
  197. target.showContextMenu(touchStart);
  198. event.preventDefault();
  199. touchStart = null;
  200. }
  201. }
  202. };
  203. }
  204. contextTouchTimeout.schedule(TOUCH_CONTEXT_MENU_TIMEOUT);
  205. break;
  206. case Event.ONTOUCHCANCEL:
  207. case Event.ONTOUCHEND:
  208. cancel();
  209. break;
  210. case Event.ONTOUCHMOVE:
  211. if (isSignificantMove(event)) {
  212. // Moved finger before the context menu timer
  213. // expired, so let the browser handle the event.
  214. cancel();
  215. }
  216. }
  217. }
  218. /**
  219. * Calculates how many pixels away the user's finger has traveled. This
  220. * reduces the chance of small non-intentional movements from canceling
  221. * the long press detection.
  222. *
  223. * @param event
  224. * the Event for which to check the move distance
  225. * @return true if this is considered an intentional move by the user
  226. */
  227. protected boolean isSignificantMove(Event event) {
  228. if (touchStart == null) {
  229. // no touch start
  230. return false;
  231. }
  232. // Calculate the distance between touch start and the current touch
  233. // position
  234. Touch touch = event.getChangedTouches().get(0);
  235. int deltaX = touch.getClientX() - touchStartX;
  236. int deltaY = touch.getClientY() - touchStartY;
  237. int delta = deltaX * deltaX + deltaY * deltaY;
  238. // Compare to the square of the significant move threshold to remove
  239. // the need for a square root
  240. if (delta > TouchScrollDelegate.SIGNIFICANT_MOVE_THRESHOLD
  241. * TouchScrollDelegate.SIGNIFICANT_MOVE_THRESHOLD) {
  242. return true;
  243. }
  244. return false;
  245. }
  246. }
  247. public static final String STYLENAME = "v-table";
  248. public enum SelectMode {
  249. NONE(0), SINGLE(1), MULTI(2);
  250. private int id;
  251. private SelectMode(int id) {
  252. this.id = id;
  253. }
  254. public int getId() {
  255. return id;
  256. }
  257. }
  258. private static final String ROW_HEADER_COLUMN_KEY = "0";
  259. private static final double CACHE_RATE_DEFAULT = 2;
  260. /**
  261. * The default multi select mode where simple left clicks only selects one
  262. * item, CTRL+left click selects multiple items and SHIFT-left click selects
  263. * a range of items.
  264. */
  265. private static final int MULTISELECT_MODE_DEFAULT = 0;
  266. /**
  267. * The simple multiselect mode is what the table used to have before
  268. * ctrl/shift selections were added. That is that when this is set clicking
  269. * on an item selects/deselects the item and no ctrl/shift selections are
  270. * available.
  271. */
  272. private static final int MULTISELECT_MODE_SIMPLE = 1;
  273. /**
  274. * multiple of pagelength which component will cache when requesting more
  275. * rows
  276. */
  277. private double cache_rate = CACHE_RATE_DEFAULT;
  278. /**
  279. * fraction of pageLength which can be scrolled without making new request
  280. */
  281. private double cache_react_rate = 0.75 * cache_rate;
  282. public static final char ALIGN_CENTER = 'c';
  283. public static final char ALIGN_LEFT = 'b';
  284. public static final char ALIGN_RIGHT = 'e';
  285. private static final int CHARCODE_SPACE = 32;
  286. private int firstRowInViewPort = 0;
  287. private int pageLength = 15;
  288. private int lastRequestedFirstvisible = 0; // to detect "serverside scroll"
  289. private int firstvisibleOnLastPage = -1; // To detect if the first visible
  290. // is on the last page
  291. /** For internal use only. May be removed or replaced in the future. */
  292. public boolean showRowHeaders = false;
  293. private String[] columnOrder;
  294. protected ApplicationConnection client;
  295. /** For internal use only. May be removed or replaced in the future. */
  296. public String paintableId;
  297. /** For internal use only. May be removed or replaced in the future. */
  298. public boolean immediate;
  299. private boolean updatedReqRows = true;
  300. private boolean nullSelectionAllowed = true;
  301. private SelectMode selectMode = SelectMode.NONE;
  302. public final HashSet<String> selectedRowKeys = new HashSet<String>();
  303. /*
  304. * When scrolling and selecting at the same time, the selections are not in
  305. * sync with the server while retrieving new rows (until key is released).
  306. */
  307. private HashSet<Object> unSyncedselectionsBeforeRowFetch;
  308. /*
  309. * These are used when jumping between pages when pressing Home and End
  310. */
  311. /** For internal use only. May be removed or replaced in the future. */
  312. public boolean selectLastItemInNextRender = false;
  313. /** For internal use only. May be removed or replaced in the future. */
  314. public boolean selectFirstItemInNextRender = false;
  315. /** For internal use only. May be removed or replaced in the future. */
  316. public boolean focusFirstItemInNextRender = false;
  317. /** For internal use only. May be removed or replaced in the future. */
  318. public boolean focusLastItemInNextRender = false;
  319. /**
  320. * The currently focused row.
  321. * <p>
  322. * For internal use only. May be removed or replaced in the future.
  323. */
  324. public VScrollTableRow focusedRow;
  325. /**
  326. * Helper to store selection range start in when using the keyboard
  327. * <p>
  328. * For internal use only. May be removed or replaced in the future.
  329. */
  330. public VScrollTableRow selectionRangeStart;
  331. /**
  332. * Flag for notifying when the selection has changed and should be sent to
  333. * the server
  334. * <p>
  335. * For internal use only. May be removed or replaced in the future.
  336. */
  337. public boolean selectionChanged = false;
  338. /*
  339. * The speed (in pixels) which the scrolling scrolls vertically/horizontally
  340. */
  341. private int scrollingVelocity = 10;
  342. private Timer scrollingVelocityTimer = null;
  343. /** For internal use only. May be removed or replaced in the future. */
  344. public String[] bodyActionKeys;
  345. private boolean enableDebug = false;
  346. private static final boolean hasNativeTouchScrolling = BrowserInfo.get()
  347. .isTouchDevice()
  348. && !BrowserInfo.get().requiresTouchScrollDelegate();
  349. private Set<String> noncollapsibleColumns;
  350. /**
  351. * The last known row height used to preserve the height of a table with
  352. * custom row heights and a fixed page length after removing the last row
  353. * from the table.
  354. *
  355. * A new VScrollTableBody instance is created every time the number of rows
  356. * changes causing {@link VScrollTableBody#rowHeight} to be discarded and
  357. * the height recalculated by {@link VScrollTableBody#getRowHeight(boolean)}
  358. * to avoid some rounding problems, e.g. round(2 * 19.8) / 2 = 20 but
  359. * round(3 * 19.8) / 3 = 19.66.
  360. */
  361. private double lastKnownRowHeight = Double.NaN;
  362. /**
  363. * Remember scroll position when getting detached to properly scroll back to
  364. * the location that there is data for if getting attached again.
  365. */
  366. private int detachedScrollPosition = 0;
  367. /**
  368. * Represents a select range of rows
  369. */
  370. private class SelectionRange {
  371. private VScrollTableRow startRow;
  372. private final int length;
  373. /**
  374. * Constuctor.
  375. */
  376. public SelectionRange(VScrollTableRow row1, VScrollTableRow row2) {
  377. VScrollTableRow endRow;
  378. if (row2.isBefore(row1)) {
  379. startRow = row2;
  380. endRow = row1;
  381. } else {
  382. startRow = row1;
  383. endRow = row2;
  384. }
  385. length = endRow.getIndex() - startRow.getIndex() + 1;
  386. }
  387. public SelectionRange(VScrollTableRow row, int length) {
  388. startRow = row;
  389. this.length = length;
  390. }
  391. /*
  392. * (non-Javadoc)
  393. *
  394. * @see java.lang.Object#toString()
  395. */
  396. @Override
  397. public String toString() {
  398. return startRow.getKey() + "-" + length;
  399. }
  400. private boolean inRange(VScrollTableRow row) {
  401. return row.getIndex() >= startRow.getIndex()
  402. && row.getIndex() < startRow.getIndex() + length;
  403. }
  404. public Collection<SelectionRange> split(VScrollTableRow row) {
  405. assert row.isAttached();
  406. ArrayList<SelectionRange> ranges = new ArrayList<SelectionRange>(2);
  407. int endOfFirstRange = row.getIndex() - 1;
  408. if (endOfFirstRange >= startRow.getIndex()) {
  409. // create range of first part unless its length is < 1
  410. ranges.add(new SelectionRange(startRow, endOfFirstRange
  411. - startRow.getIndex() + 1));
  412. }
  413. int startOfSecondRange = row.getIndex() + 1;
  414. if (getEndIndex() >= startOfSecondRange) {
  415. // create range of second part unless its length is < 1
  416. VScrollTableRow startOfRange = scrollBody
  417. .getRowByRowIndex(startOfSecondRange);
  418. if (startOfRange != null) {
  419. ranges.add(new SelectionRange(startOfRange, getEndIndex()
  420. - startOfSecondRange + 1));
  421. }
  422. }
  423. return ranges;
  424. }
  425. private int getEndIndex() {
  426. return startRow.getIndex() + length - 1;
  427. }
  428. }
  429. private final HashSet<SelectionRange> selectedRowRanges = new HashSet<SelectionRange>();
  430. /** For internal use only. May be removed or replaced in the future. */
  431. public boolean initializedAndAttached = false;
  432. /**
  433. * Flag to indicate if a column width recalculation is needed due update.
  434. * <p>
  435. * For internal use only. May be removed or replaced in the future.
  436. */
  437. public boolean headerChangedDuringUpdate = false;
  438. /** For internal use only. May be removed or replaced in the future. */
  439. public final TableHead tHead = new TableHead();
  440. /** For internal use only. May be removed or replaced in the future. */
  441. public final TableFooter tFoot = new TableFooter();
  442. /** Handles context menu for table body */
  443. private ContextMenuOwner contextMenuOwner = new ContextMenuOwner() {
  444. @Override
  445. public void showContextMenu(Event event) {
  446. int left = WidgetUtil.getTouchOrMouseClientX(event);
  447. int top = WidgetUtil.getTouchOrMouseClientY(event);
  448. boolean menuShown = handleBodyContextMenu(left, top);
  449. if (menuShown) {
  450. event.stopPropagation();
  451. event.preventDefault();
  452. }
  453. }
  454. };
  455. /** Handles touch events to display a context menu for table body */
  456. private TouchContextProvider touchContextProvider = new TouchContextProvider(
  457. contextMenuOwner);
  458. /**
  459. * For internal use only. May be removed or replaced in the future.
  460. *
  461. * Overwrites onBrowserEvent function on FocusableScrollPanel to give event
  462. * access to touchContextProvider. Has to be public to give TableConnector
  463. * access to the scrollBodyPanel field.
  464. *
  465. * @since 7.2
  466. * @author Vaadin Ltd
  467. */
  468. public class FocusableScrollContextPanel extends FocusableScrollPanel {
  469. @Override
  470. public void onBrowserEvent(Event event) {
  471. super.onBrowserEvent(event);
  472. touchContextProvider.handleTouchEvent(event);
  473. };
  474. public FocusableScrollContextPanel(boolean useFakeFocusElement) {
  475. super(useFakeFocusElement);
  476. }
  477. }
  478. /** For internal use only. May be removed or replaced in the future. */
  479. public final FocusableScrollContextPanel scrollBodyPanel = new FocusableScrollContextPanel(
  480. true);
  481. private KeyPressHandler navKeyPressHandler = new KeyPressHandler() {
  482. @Override
  483. public void onKeyPress(KeyPressEvent keyPressEvent) {
  484. // This is used for Firefox only, since Firefox auto-repeat
  485. // works correctly only if we use a key press handler, other
  486. // browsers handle it correctly when using a key down handler
  487. if (!BrowserInfo.get().isGecko()) {
  488. return;
  489. }
  490. NativeEvent event = keyPressEvent.getNativeEvent();
  491. if (!enabled) {
  492. // Cancel default keyboard events on a disabled Table
  493. // (prevents scrolling)
  494. event.preventDefault();
  495. } else if (hasFocus) {
  496. // Key code in Firefox/onKeyPress is present only for
  497. // special keys, otherwise 0 is returned
  498. int keyCode = event.getKeyCode();
  499. if (keyCode == 0 && event.getCharCode() == ' ') {
  500. // Provide a keyCode for space to be compatible with
  501. // FireFox keypress event
  502. keyCode = CHARCODE_SPACE;
  503. }
  504. if (handleNavigation(keyCode,
  505. event.getCtrlKey() || event.getMetaKey(),
  506. event.getShiftKey())) {
  507. event.preventDefault();
  508. }
  509. startScrollingVelocityTimer();
  510. }
  511. }
  512. };
  513. private KeyUpHandler navKeyUpHandler = new KeyUpHandler() {
  514. @Override
  515. public void onKeyUp(KeyUpEvent keyUpEvent) {
  516. NativeEvent event = keyUpEvent.getNativeEvent();
  517. int keyCode = event.getKeyCode();
  518. if (!isFocusable()) {
  519. cancelScrollingVelocityTimer();
  520. } else if (isNavigationKey(keyCode)) {
  521. if (keyCode == getNavigationDownKey()
  522. || keyCode == getNavigationUpKey()) {
  523. /*
  524. * in multiselect mode the server may still have value from
  525. * previous page. Clear it unless doing multiselection or
  526. * just moving focus.
  527. */
  528. if (!event.getShiftKey() && !event.getCtrlKey()) {
  529. instructServerToForgetPreviousSelections();
  530. }
  531. sendSelectedRows();
  532. }
  533. cancelScrollingVelocityTimer();
  534. navKeyDown = false;
  535. }
  536. }
  537. };
  538. private KeyDownHandler navKeyDownHandler = new KeyDownHandler() {
  539. @Override
  540. public void onKeyDown(KeyDownEvent keyDownEvent) {
  541. NativeEvent event = keyDownEvent.getNativeEvent();
  542. // This is not used for Firefox
  543. if (BrowserInfo.get().isGecko()) {
  544. return;
  545. }
  546. if (!enabled) {
  547. // Cancel default keyboard events on a disabled Table
  548. // (prevents scrolling)
  549. event.preventDefault();
  550. } else if (hasFocus) {
  551. if (handleNavigation(event.getKeyCode(), event.getCtrlKey()
  552. || event.getMetaKey(), event.getShiftKey())) {
  553. navKeyDown = true;
  554. event.preventDefault();
  555. }
  556. startScrollingVelocityTimer();
  557. }
  558. }
  559. };
  560. /** For internal use only. May be removed or replaced in the future. */
  561. public int totalRows;
  562. private Set<String> collapsedColumns;
  563. /** For internal use only. May be removed or replaced in the future. */
  564. public final RowRequestHandler rowRequestHandler;
  565. /** For internal use only. May be removed or replaced in the future. */
  566. public VScrollTableBody scrollBody;
  567. private int firstvisible = 0;
  568. private boolean sortAscending;
  569. private String sortColumn;
  570. private String oldSortColumn;
  571. private boolean columnReordering;
  572. /**
  573. * This map contains captions and icon urls for actions like: * "33_c" ->
  574. * "Edit" * "33_i" -> "http://dom.com/edit.png"
  575. */
  576. private final HashMap<Object, String> actionMap = new HashMap<Object, String>();
  577. private String[] visibleColOrder;
  578. private boolean initialContentReceived = false;
  579. private Element scrollPositionElement;
  580. /** For internal use only. May be removed or replaced in the future. */
  581. public boolean enabled;
  582. /** For internal use only. May be removed or replaced in the future. */
  583. public boolean showColHeaders;
  584. /** For internal use only. May be removed or replaced in the future. */
  585. public boolean showColFooters;
  586. /** flag to indicate that table body has changed */
  587. private boolean isNewBody = true;
  588. /**
  589. * Read from the "recalcWidths" -attribute. When it is true, the table will
  590. * recalculate the widths for columns - desirable in some cases. For #1983,
  591. * marked experimental. See also variable <code>refreshContentWidths</code>
  592. * in method {@link TableHead#updateCellsFromUIDL(UIDL)}.
  593. * <p>
  594. * For internal use only. May be removed or replaced in the future.
  595. */
  596. public boolean recalcWidths = false;
  597. /** For internal use only. May be removed or replaced in the future. */
  598. public boolean rendering = false;
  599. private boolean hasFocus = false;
  600. private int dragmode;
  601. private int multiselectmode;
  602. /** For internal use only. May be removed or replaced in the future. */
  603. public int tabIndex;
  604. private TouchScrollDelegate touchScrollDelegate;
  605. /** For internal use only. May be removed or replaced in the future. */
  606. public int lastRenderedHeight;
  607. /**
  608. * Values (serverCacheFirst+serverCacheLast) sent by server that tells which
  609. * rows (indexes) are in the server side cache (page buffer). -1 means
  610. * unknown. The server side cache row MUST MATCH the client side cache rows.
  611. *
  612. * If the client side cache contains additional rows with e.g. buttons, it
  613. * will cause out of sync when such a button is pressed.
  614. *
  615. * If the server side cache contains additional rows with e.g. buttons,
  616. * scrolling in the client will cause empty buttons to be rendered
  617. * (cached=true request for non-existing components)
  618. *
  619. * For internal use only. May be removed or replaced in the future.
  620. */
  621. public int serverCacheFirst = -1;
  622. public int serverCacheLast = -1;
  623. /**
  624. * In several cases TreeTable depends on the scrollBody.lastRendered being
  625. * 'out of sync' while the update is being done. In those cases the sanity
  626. * check must be performed afterwards.
  627. */
  628. public boolean postponeSanityCheckForLastRendered;
  629. /** For internal use only. May be removed or replaced in the future. */
  630. public boolean sizeNeedsInit = true;
  631. /**
  632. * Used to recall the position of an open context menu if we need to close
  633. * and reopen it during a row update.
  634. * <p>
  635. * For internal use only. May be removed or replaced in the future.
  636. */
  637. public class ContextMenuDetails implements CloseHandler<PopupPanel> {
  638. public String rowKey;
  639. public int left;
  640. public int top;
  641. HandlerRegistration closeRegistration;
  642. public ContextMenuDetails(VContextMenu menu, String rowKey, int left,
  643. int top) {
  644. this.rowKey = rowKey;
  645. this.left = left;
  646. this.top = top;
  647. closeRegistration = menu.addCloseHandler(this);
  648. }
  649. @Override
  650. public void onClose(CloseEvent<PopupPanel> event) {
  651. contextMenu = null;
  652. closeRegistration.removeHandler();
  653. }
  654. }
  655. /** For internal use only. May be removed or replaced in the future. */
  656. public ContextMenuDetails contextMenu = null;
  657. private boolean hadScrollBars = false;
  658. private HandlerRegistration addCloseHandler;
  659. /**
  660. * Changes to manage mouseDown and mouseUp
  661. */
  662. /**
  663. * The element where the last mouse down event was registered.
  664. */
  665. private Element lastMouseDownTarget;
  666. /**
  667. * Set to true by {@link #mouseUpPreviewHandler} if it gets a mouseup at the
  668. * same element as {@link #lastMouseDownTarget}.
  669. */
  670. private boolean mouseUpPreviewMatched = false;
  671. private HandlerRegistration mouseUpEventPreviewRegistration;
  672. /**
  673. * Previews events after a mousedown to detect where the following mouseup
  674. * hits.
  675. */
  676. private final NativePreviewHandler mouseUpPreviewHandler = new NativePreviewHandler() {
  677. @Override
  678. public void onPreviewNativeEvent(NativePreviewEvent event) {
  679. if (event.getTypeInt() == Event.ONMOUSEUP) {
  680. mouseUpEventPreviewRegistration.removeHandler();
  681. // Event's reported target not always correct if event
  682. // capture is in use
  683. Element elementUnderMouse = WidgetUtil
  684. .getElementUnderMouse(event.getNativeEvent());
  685. if (lastMouseDownTarget != null
  686. && lastMouseDownTarget.isOrHasChild(elementUnderMouse)) {
  687. mouseUpPreviewMatched = true;
  688. } else {
  689. getLogger().log(
  690. Level.FINEST,
  691. "Ignoring mouseup from " + elementUnderMouse
  692. + " when mousedown was on "
  693. + lastMouseDownTarget);
  694. }
  695. }
  696. }
  697. };
  698. public VScrollTable() {
  699. setMultiSelectMode(MULTISELECT_MODE_DEFAULT);
  700. scrollBodyPanel.addFocusHandler(this);
  701. scrollBodyPanel.addBlurHandler(this);
  702. scrollBodyPanel.addScrollHandler(this);
  703. /*
  704. * Firefox auto-repeat works correctly only if we use a key press
  705. * handler, other browsers handle it correctly when using a key down
  706. * handler
  707. */
  708. if (BrowserInfo.get().isGecko()) {
  709. scrollBodyPanel.addKeyPressHandler(navKeyPressHandler);
  710. } else {
  711. scrollBodyPanel.addKeyDownHandler(navKeyDownHandler);
  712. }
  713. scrollBodyPanel.addKeyUpHandler(navKeyUpHandler);
  714. scrollBodyPanel.sinkEvents(Event.TOUCHEVENTS | Event.ONCONTEXTMENU);
  715. setStyleName(STYLENAME);
  716. add(tHead);
  717. add(scrollBodyPanel);
  718. add(tFoot);
  719. rowRequestHandler = new RowRequestHandler();
  720. }
  721. @Override
  722. public void setStyleName(String style) {
  723. updateStyleNames(style, false);
  724. }
  725. @Override
  726. public void setStylePrimaryName(String style) {
  727. updateStyleNames(style, true);
  728. }
  729. private void updateStyleNames(String newStyle, boolean isPrimary) {
  730. scrollBodyPanel
  731. .removeStyleName(getStylePrimaryName() + "-body-wrapper");
  732. scrollBodyPanel.removeStyleName(getStylePrimaryName() + "-body");
  733. if (scrollBody != null) {
  734. scrollBody.removeStyleName(getStylePrimaryName()
  735. + "-body-noselection");
  736. }
  737. if (isPrimary) {
  738. super.setStylePrimaryName(newStyle);
  739. } else {
  740. super.setStyleName(newStyle);
  741. }
  742. scrollBodyPanel.addStyleName(getStylePrimaryName() + "-body-wrapper");
  743. scrollBodyPanel.addStyleName(getStylePrimaryName() + "-body");
  744. tHead.updateStyleNames(getStylePrimaryName());
  745. tFoot.updateStyleNames(getStylePrimaryName());
  746. if (scrollBody != null) {
  747. scrollBody.updateStyleNames(getStylePrimaryName());
  748. }
  749. }
  750. public void init(ApplicationConnection client) {
  751. this.client = client;
  752. // Add a handler to clear saved context menu details when the menu
  753. // closes. See #8526.
  754. addCloseHandler = client.getContextMenu().addCloseHandler(
  755. new CloseHandler<PopupPanel>() {
  756. @Override
  757. public void onClose(CloseEvent<PopupPanel> event) {
  758. contextMenu = null;
  759. }
  760. });
  761. }
  762. /**
  763. * Handles a context menu event on table body.
  764. *
  765. * @param left
  766. * left position of the context menu
  767. * @param top
  768. * top position of the context menu
  769. * @return true if a context menu was shown, otherwise false
  770. */
  771. private boolean handleBodyContextMenu(int left, int top) {
  772. if (enabled && bodyActionKeys != null) {
  773. top += Window.getScrollTop();
  774. left += Window.getScrollLeft();
  775. client.getContextMenu().showAt(this, left, top);
  776. return true;
  777. }
  778. return false;
  779. }
  780. /**
  781. * Fires a column resize event which sends the resize information to the
  782. * server.
  783. *
  784. * @param columnId
  785. * The columnId of the column which was resized
  786. * @param originalWidth
  787. * The width in pixels of the column before the resize event
  788. * @param newWidth
  789. * The width in pixels of the column after the resize event
  790. */
  791. private void fireColumnResizeEvent(String columnId, int originalWidth,
  792. int newWidth) {
  793. client.updateVariable(paintableId, "columnResizeEventColumn", columnId,
  794. false);
  795. client.updateVariable(paintableId, "columnResizeEventPrev",
  796. originalWidth, false);
  797. client.updateVariable(paintableId, "columnResizeEventCurr", newWidth,
  798. immediate);
  799. }
  800. /**
  801. * Non-immediate variable update of column widths for a collection of
  802. * columns.
  803. *
  804. * @param columns
  805. * the columns to trigger the events for.
  806. */
  807. private void sendColumnWidthUpdates(Collection<HeaderCell> columns) {
  808. String[] newSizes = new String[columns.size()];
  809. int ix = 0;
  810. for (HeaderCell cell : columns) {
  811. newSizes[ix++] = cell.getColKey() + ":" + cell.getWidth();
  812. }
  813. client.updateVariable(paintableId, "columnWidthUpdates", newSizes,
  814. false);
  815. }
  816. /**
  817. * Moves the focus one step down
  818. *
  819. * @return Returns true if succeeded
  820. */
  821. private boolean moveFocusDown() {
  822. return moveFocusDown(0);
  823. }
  824. /**
  825. * Moves the focus down by 1+offset rows
  826. *
  827. * @return Returns true if succeeded, else false if the selection could not
  828. * be move downwards
  829. */
  830. private boolean moveFocusDown(int offset) {
  831. if (isSelectable()) {
  832. if (focusedRow == null && scrollBody.iterator().hasNext()) {
  833. // FIXME should focus first visible from top, not first rendered
  834. // ??
  835. return setRowFocus((VScrollTableRow) scrollBody.iterator()
  836. .next());
  837. } else {
  838. VScrollTableRow next = getNextRow(focusedRow, offset);
  839. if (next != null) {
  840. return setRowFocus(next);
  841. }
  842. }
  843. }
  844. return false;
  845. }
  846. /**
  847. * Moves the selection one step up
  848. *
  849. * @return Returns true if succeeded
  850. */
  851. private boolean moveFocusUp() {
  852. return moveFocusUp(0);
  853. }
  854. /**
  855. * Moves the focus row upwards
  856. *
  857. * @return Returns true if succeeded, else false if the selection could not
  858. * be move upwards
  859. *
  860. */
  861. private boolean moveFocusUp(int offset) {
  862. if (isSelectable()) {
  863. if (focusedRow == null && scrollBody.iterator().hasNext()) {
  864. // FIXME logic is exactly the same as in moveFocusDown, should
  865. // be the opposite??
  866. return setRowFocus((VScrollTableRow) scrollBody.iterator()
  867. .next());
  868. } else {
  869. VScrollTableRow prev = getPreviousRow(focusedRow, offset);
  870. if (prev != null) {
  871. return setRowFocus(prev);
  872. } else {
  873. VConsole.log("no previous available");
  874. }
  875. }
  876. }
  877. return false;
  878. }
  879. /**
  880. * Selects a row where the current selection head is
  881. *
  882. * @param ctrlSelect
  883. * Is the selection a ctrl+selection
  884. * @param shiftSelect
  885. * Is the selection a shift+selection
  886. * @return Returns truw
  887. */
  888. private void selectFocusedRow(boolean ctrlSelect, boolean shiftSelect) {
  889. if (focusedRow != null) {
  890. // Arrows moves the selection and clears previous selections
  891. if (isSelectable() && !ctrlSelect && !shiftSelect) {
  892. deselectAll();
  893. focusedRow.toggleSelection();
  894. selectionRangeStart = focusedRow;
  895. } else if (isSelectable() && ctrlSelect && !shiftSelect) {
  896. // Ctrl+arrows moves selection head
  897. selectionRangeStart = focusedRow;
  898. // No selection, only selection head is moved
  899. } else if (isMultiSelectModeAny() && !ctrlSelect && shiftSelect) {
  900. // Shift+arrows selection selects a range
  901. focusedRow.toggleShiftSelection(shiftSelect);
  902. }
  903. }
  904. }
  905. /**
  906. * Sends the selection to the server if changed since the last update/visit.
  907. */
  908. protected void sendSelectedRows() {
  909. sendSelectedRows(immediate);
  910. }
  911. private void updateFirstVisibleAndSendSelectedRows() {
  912. updateFirstVisibleRow();
  913. sendSelectedRows(immediate);
  914. }
  915. /**
  916. * Sends the selection to the server if it has been changed since the last
  917. * update/visit.
  918. *
  919. * @param immediately
  920. * set to true to immediately send the rows
  921. */
  922. protected void sendSelectedRows(boolean immediately) {
  923. // Don't send anything if selection has not changed
  924. if (!selectionChanged) {
  925. return;
  926. }
  927. // Reset selection changed flag
  928. selectionChanged = false;
  929. // Note: changing the immediateness of this might require changes to
  930. // "clickEvent" immediateness also.
  931. if (isMultiSelectModeDefault()) {
  932. // Convert ranges to a set of strings
  933. Set<String> ranges = new HashSet<String>();
  934. for (SelectionRange range : selectedRowRanges) {
  935. ranges.add(range.toString());
  936. }
  937. // Send the selected row ranges
  938. client.updateVariable(paintableId, "selectedRanges",
  939. ranges.toArray(new String[selectedRowRanges.size()]), false);
  940. selectedRowRanges.clear();
  941. // clean selectedRowKeys so that they don't contain excess values
  942. for (Iterator<String> iterator = selectedRowKeys.iterator(); iterator
  943. .hasNext();) {
  944. String key = iterator.next();
  945. VScrollTableRow renderedRowByKey = getRenderedRowByKey(key);
  946. if (renderedRowByKey != null) {
  947. for (SelectionRange range : selectedRowRanges) {
  948. if (range.inRange(renderedRowByKey)) {
  949. iterator.remove();
  950. }
  951. }
  952. } else {
  953. // orphaned selected key, must be in a range, ignore
  954. iterator.remove();
  955. }
  956. }
  957. }
  958. // Send the selected rows
  959. client.updateVariable(paintableId, "selected",
  960. selectedRowKeys.toArray(new String[selectedRowKeys.size()]),
  961. immediately);
  962. }
  963. /**
  964. * Get the key that moves the selection head upwards. By default it is the
  965. * up arrow key but by overriding this you can change the key to whatever
  966. * you want.
  967. *
  968. * @return The keycode of the key
  969. */
  970. protected int getNavigationUpKey() {
  971. return KeyCodes.KEY_UP;
  972. }
  973. /**
  974. * Get the key that moves the selection head downwards. By default it is the
  975. * down arrow key but by overriding this you can change the key to whatever
  976. * you want.
  977. *
  978. * @return The keycode of the key
  979. */
  980. protected int getNavigationDownKey() {
  981. return KeyCodes.KEY_DOWN;
  982. }
  983. /**
  984. * Get the key that scrolls to the left in the table. By default it is the
  985. * left arrow key but by overriding this you can change the key to whatever
  986. * you want.
  987. *
  988. * @return The keycode of the key
  989. */
  990. protected int getNavigationLeftKey() {
  991. return KeyCodes.KEY_LEFT;
  992. }
  993. /**
  994. * Get the key that scroll to the right on the table. By default it is the
  995. * right arrow key but by overriding this you can change the key to whatever
  996. * you want.
  997. *
  998. * @return The keycode of the key
  999. */
  1000. protected int getNavigationRightKey() {
  1001. return KeyCodes.KEY_RIGHT;
  1002. }
  1003. /**
  1004. * Get the key that selects an item in the table. By default it is the space
  1005. * bar key but by overriding this you can change the key to whatever you
  1006. * want.
  1007. *
  1008. * @return
  1009. */
  1010. protected int getNavigationSelectKey() {
  1011. return CHARCODE_SPACE;
  1012. }
  1013. /**
  1014. * Get the key the moves the selection one page up in the table. By default
  1015. * this is the Page Up key but by overriding this you can change the key to
  1016. * whatever you want.
  1017. *
  1018. * @return
  1019. */
  1020. protected int getNavigationPageUpKey() {
  1021. return KeyCodes.KEY_PAGEUP;
  1022. }
  1023. /**
  1024. * Get the key the moves the selection one page down in the table. By
  1025. * default this is the Page Down key but by overriding this you can change
  1026. * the key to whatever you want.
  1027. *
  1028. * @return
  1029. */
  1030. protected int getNavigationPageDownKey() {
  1031. return KeyCodes.KEY_PAGEDOWN;
  1032. }
  1033. /**
  1034. * Get the key the moves the selection to the beginning of the table. By
  1035. * default this is the Home key but by overriding this you can change the
  1036. * key to whatever you want.
  1037. *
  1038. * @return
  1039. */
  1040. protected int getNavigationStartKey() {
  1041. return KeyCodes.KEY_HOME;
  1042. }
  1043. /**
  1044. * Get the key the moves the selection to the end of the table. By default
  1045. * this is the End key but by overriding this you can change the key to
  1046. * whatever you want.
  1047. *
  1048. * @return
  1049. */
  1050. protected int getNavigationEndKey() {
  1051. return KeyCodes.KEY_END;
  1052. }
  1053. /** For internal use only. May be removed or replaced in the future. */
  1054. public void initializeRows(UIDL uidl, UIDL rowData) {
  1055. if (scrollBody != null) {
  1056. scrollBody.removeFromParent();
  1057. }
  1058. // Without this call the scroll position is messed up in IE even after
  1059. // the lazy scroller has set the scroll position to the first visible
  1060. // item
  1061. int pos = scrollBodyPanel.getScrollPosition();
  1062. // Reset first row in view port so client requests correct last row.
  1063. if (pos == 0) {
  1064. firstRowInViewPort = 0;
  1065. }
  1066. scrollBody = createScrollBody();
  1067. scrollBody.renderInitialRows(rowData, uidl.getIntAttribute("firstrow"),
  1068. uidl.getIntAttribute("rows"));
  1069. scrollBodyPanel.add(scrollBody);
  1070. // New body starts scrolled to the left, make sure the header and footer
  1071. // are also scrolled to the left
  1072. tHead.setHorizontalScrollPosition(0);
  1073. tFoot.setHorizontalScrollPosition(0);
  1074. initialContentReceived = true;
  1075. sizeNeedsInit = true;
  1076. scrollBody.restoreRowVisibility();
  1077. }
  1078. /** For internal use only. May be removed or replaced in the future. */
  1079. public void updateColumnProperties(UIDL uidl) {
  1080. updateColumnOrder(uidl);
  1081. updateCollapsedColumns(uidl);
  1082. UIDL vc = uidl.getChildByTagName("visiblecolumns");
  1083. if (vc != null) {
  1084. tHead.updateCellsFromUIDL(vc);
  1085. tFoot.updateCellsFromUIDL(vc);
  1086. }
  1087. updateHeader(uidl.getStringArrayAttribute("vcolorder"));
  1088. updateFooter(uidl.getStringArrayAttribute("vcolorder"));
  1089. if (uidl.hasVariable("noncollapsiblecolumns")) {
  1090. noncollapsibleColumns = uidl
  1091. .getStringArrayVariableAsSet("noncollapsiblecolumns");
  1092. }
  1093. }
  1094. private void updateCollapsedColumns(UIDL uidl) {
  1095. if (uidl.hasVariable("collapsedcolumns")) {
  1096. tHead.setColumnCollapsingAllowed(true);
  1097. collapsedColumns = uidl
  1098. .getStringArrayVariableAsSet("collapsedcolumns");
  1099. } else {
  1100. tHead.setColumnCollapsingAllowed(false);
  1101. }
  1102. }
  1103. private void updateColumnOrder(UIDL uidl) {
  1104. if (uidl.hasVariable("columnorder")) {
  1105. columnReordering = true;
  1106. columnOrder = uidl.getStringArrayVariable("columnorder");
  1107. } else {
  1108. columnReordering = false;
  1109. columnOrder = null;
  1110. }
  1111. }
  1112. /** For internal use only. May be removed or replaced in the future. */
  1113. public boolean selectSelectedRows(UIDL uidl) {
  1114. boolean keyboardSelectionOverRowFetchInProgress = false;
  1115. if (uidl.hasVariable("selected")) {
  1116. final Set<String> selectedKeys = uidl
  1117. .getStringArrayVariableAsSet("selected");
  1118. // Do not update focus if there is a single selected row
  1119. // that is the same as the previous selection. This prevents
  1120. // unwanted scrolling (#18247).
  1121. boolean rowsUnSelected = removeUnselectedRowKeys(selectedKeys);
  1122. boolean updateFocus = rowsUnSelected || selectedRowKeys.size() == 0
  1123. || focusedRow == null;
  1124. if (scrollBody != null) {
  1125. Iterator<Widget> iterator = scrollBody.iterator();
  1126. while (iterator.hasNext()) {
  1127. /*
  1128. * Make the focus reflect to the server side state unless we
  1129. * are currently selecting multiple rows with keyboard.
  1130. */
  1131. VScrollTableRow row = (VScrollTableRow) iterator.next();
  1132. boolean selected = selectedKeys.contains(row.getKey());
  1133. if (!selected
  1134. && unSyncedselectionsBeforeRowFetch != null
  1135. && unSyncedselectionsBeforeRowFetch.contains(row
  1136. .getKey())) {
  1137. selected = true;
  1138. keyboardSelectionOverRowFetchInProgress = true;
  1139. }
  1140. if (selected && selectedKeys.size() == 1 && updateFocus) {
  1141. /*
  1142. * If a single item is selected, move focus to the
  1143. * selected row. (#10522)
  1144. */
  1145. setRowFocus(row);
  1146. }
  1147. if (selected != row.isSelected()) {
  1148. row.toggleSelection();
  1149. if (!isSingleSelectMode() && !selected) {
  1150. // Update selection range in case a row is
  1151. // unselected from the middle of a range - #8076
  1152. removeRowFromUnsentSelectionRanges(row);
  1153. }
  1154. }
  1155. }
  1156. }
  1157. }
  1158. unSyncedselectionsBeforeRowFetch = null;
  1159. return keyboardSelectionOverRowFetchInProgress;
  1160. }
  1161. private boolean removeUnselectedRowKeys(final Set<String> selectedKeys) {
  1162. List<String> unselectedKeys = new ArrayList<String>(0);
  1163. for (String key : selectedRowKeys) {
  1164. if (!selectedKeys.contains(key)) {
  1165. unselectedKeys.add(key);
  1166. }
  1167. }
  1168. return selectedRowKeys.removeAll(unselectedKeys);
  1169. }
  1170. /** For internal use only. May be removed or replaced in the future. */
  1171. public void updateSortingProperties(UIDL uidl) {
  1172. oldSortColumn = sortColumn;
  1173. if (uidl.hasVariable("sortascending")) {
  1174. sortAscending = uidl.getBooleanVariable("sortascending");
  1175. sortColumn = uidl.getStringVariable("sortcolumn");
  1176. }
  1177. }
  1178. /** For internal use only. May be removed or replaced in the future. */
  1179. public void resizeSortedColumnForSortIndicator() {
  1180. // Force recalculation of the captionContainer element inside the header
  1181. // cell to accomodate for the size of the sort arrow.
  1182. HeaderCell sortedHeader = tHead.getHeaderCell(sortColumn);
  1183. if (sortedHeader != null) {
  1184. // Mark header as sorted now. Any earlier marking would lead to
  1185. // columns with wrong sizes
  1186. sortedHeader.setSorted(true);
  1187. tHead.resizeCaptionContainer(sortedHeader);
  1188. }
  1189. // Also recalculate the width of the captionContainer element in the
  1190. // previously sorted header, since this now has more room.
  1191. HeaderCell oldSortedHeader = tHead.getHeaderCell(oldSortColumn);
  1192. if (oldSortedHeader != null) {
  1193. tHead.resizeCaptionContainer(oldSortedHeader);
  1194. }
  1195. }
  1196. private boolean lazyScrollerIsActive;
  1197. private void disableLazyScroller() {
  1198. lazyScrollerIsActive = false;
  1199. scrollBodyPanel.getElement().getStyle().clearOverflowX();
  1200. scrollBodyPanel.getElement().getStyle().clearOverflowY();
  1201. }
  1202. private void enableLazyScroller() {
  1203. Scheduler.get().scheduleDeferred(lazyScroller);
  1204. lazyScrollerIsActive = true;
  1205. // prevent scrolling to jump in IE11
  1206. scrollBodyPanel.getElement().getStyle().setOverflowX(Overflow.HIDDEN);
  1207. scrollBodyPanel.getElement().getStyle().setOverflowY(Overflow.HIDDEN);
  1208. }
  1209. private boolean isLazyScrollerActive() {
  1210. return lazyScrollerIsActive;
  1211. }
  1212. private ScheduledCommand lazyScroller = new ScheduledCommand() {
  1213. @Override
  1214. public void execute() {
  1215. if (firstvisible >= 0) {
  1216. firstRowInViewPort = firstvisible;
  1217. if (firstvisibleOnLastPage > -1) {
  1218. scrollBodyPanel
  1219. .setScrollPosition(measureRowHeightOffset(firstvisibleOnLastPage));
  1220. } else {
  1221. scrollBodyPanel
  1222. .setScrollPosition(measureRowHeightOffset(firstvisible));
  1223. }
  1224. }
  1225. disableLazyScroller();
  1226. }
  1227. };
  1228. /** For internal use only. May be removed or replaced in the future. */
  1229. public void updateFirstVisibleAndScrollIfNeeded(UIDL uidl) {
  1230. firstvisible = uidl.hasVariable("firstvisible") ? uidl
  1231. .getIntVariable("firstvisible") : 0;
  1232. firstvisibleOnLastPage = uidl.hasVariable("firstvisibleonlastpage") ? uidl
  1233. .getIntVariable("firstvisibleonlastpage") : -1;
  1234. if (firstvisible != lastRequestedFirstvisible && scrollBody != null) {
  1235. // Update lastRequestedFirstvisible right away here
  1236. // (don't rely on update in the timer which could be cancelled).
  1237. lastRequestedFirstvisible = firstRowInViewPort;
  1238. // Only scroll if the first visible changes from the server side.
  1239. // Else we might unintentionally scroll even when the scroll
  1240. // position has not changed.
  1241. enableLazyScroller();
  1242. }
  1243. }
  1244. protected int measureRowHeightOffset(int rowIx) {
  1245. return (int) (rowIx * scrollBody.getRowHeight());
  1246. }
  1247. /** For internal use only. May be removed or replaced in the future. */
  1248. public void updatePageLength(UIDL uidl) {
  1249. int oldPageLength = pageLength;
  1250. if (uidl.hasAttribute("pagelength")) {
  1251. pageLength = uidl.getIntAttribute("pagelength");
  1252. } else {
  1253. // pagelength is "0" meaning scrolling is turned off
  1254. pageLength = totalRows;
  1255. }
  1256. if (oldPageLength != pageLength && initializedAndAttached) {
  1257. // page length changed, need to update size
  1258. sizeNeedsInit = true;
  1259. }
  1260. }
  1261. /** For internal use only. May be removed or replaced in the future. */
  1262. public void updateSelectionProperties(UIDL uidl,
  1263. AbstractComponentState state, boolean readOnly) {
  1264. setMultiSelectMode(uidl.hasAttribute("multiselectmode") ? uidl
  1265. .getIntAttribute("multiselectmode") : MULTISELECT_MODE_DEFAULT);
  1266. nullSelectionAllowed = uidl.hasAttribute("nsa") ? uidl
  1267. .getBooleanAttribute("nsa") : true;
  1268. if (uidl.hasAttribute("selectmode")) {
  1269. if (readOnly) {
  1270. selectMode = SelectMode.NONE;
  1271. } else if (uidl.getStringAttribute("selectmode").equals("multi")) {
  1272. selectMode = SelectMode.MULTI;
  1273. } else if (uidl.getStringAttribute("selectmode").equals("single")) {
  1274. selectMode = SelectMode.SINGLE;
  1275. } else {
  1276. selectMode = SelectMode.NONE;
  1277. }
  1278. }
  1279. }
  1280. /** For internal use only. May be removed or replaced in the future. */
  1281. public void updateDragMode(UIDL uidl) {
  1282. dragmode = uidl.hasAttribute("dragmode") ? uidl
  1283. .getIntAttribute("dragmode") : 0;
  1284. if (BrowserInfo.get().isIE()) {
  1285. if (dragmode > 0) {
  1286. getElement().setPropertyJSO("onselectstart",
  1287. getPreventTextSelectionIEHack());
  1288. } else {
  1289. getElement().setPropertyJSO("onselectstart", null);
  1290. }
  1291. }
  1292. }
  1293. /** For internal use only. May be removed or replaced in the future. */
  1294. public void updateTotalRows(UIDL uidl) {
  1295. int newTotalRows = uidl.getIntAttribute("totalrows");
  1296. if (newTotalRows != getTotalRows()) {
  1297. if (scrollBody != null) {
  1298. if (getTotalRows() == 0) {
  1299. tHead.clear();
  1300. tFoot.clear();
  1301. }
  1302. initializedAndAttached = false;
  1303. initialContentReceived = false;
  1304. isNewBody = true;
  1305. }
  1306. setTotalRows(newTotalRows);
  1307. }
  1308. }
  1309. protected void setTotalRows(int newTotalRows) {
  1310. totalRows = newTotalRows;
  1311. }
  1312. public int getTotalRows() {
  1313. return totalRows;
  1314. }
  1315. /**
  1316. * Returns the extra space that is given to the header column when column
  1317. * width is determined by header text.
  1318. *
  1319. * @return extra space in pixels
  1320. */
  1321. private int getHeaderPadding() {
  1322. return scrollBody.getCellExtraWidth();
  1323. }
  1324. /**
  1325. * This method exists for the needs of {@link VTreeTable} only. Not part of
  1326. * the official API, <b>extend at your own risk</b>. May be removed or
  1327. * replaced in the future.
  1328. *
  1329. * @return index of TreeTable's hierarchy column, or -1 if not applicable
  1330. */
  1331. protected int getHierarchyColumnIndex() {
  1332. return -1;
  1333. }
  1334. /**
  1335. * For internal use only. May be removed or replaced in the future.
  1336. */
  1337. public void updateMaxIndent() {
  1338. int oldIndent = scrollBody.getMaxIndent();
  1339. scrollBody.calculateMaxIndent();
  1340. if (oldIndent != scrollBody.getMaxIndent()) {
  1341. // indent updated, headers might need adjusting
  1342. triggerLazyColumnAdjustment(true);
  1343. }
  1344. }
  1345. /** For internal use only. May be removed or replaced in the future. */
  1346. public void focusRowFromBody() {
  1347. if (selectedRowKeys.size() == 1) {
  1348. // try to focus a row currently selected and in viewport
  1349. String selectedRowKey = selectedRowKeys.iterator().next();
  1350. if (selectedRowKey != null) {
  1351. VScrollTableRow renderedRow = getRenderedRowByKey(selectedRowKey);
  1352. if (renderedRow == null || !renderedRow.isInViewPort()) {
  1353. setRowFocus(scrollBody.getRowByRowIndex(firstRowInViewPort));
  1354. } else {
  1355. setRowFocus(renderedRow);
  1356. }
  1357. }
  1358. } else {
  1359. // multiselect mode
  1360. setRowFocus(scrollBody.getRowByRowIndex(firstRowInViewPort));
  1361. }
  1362. }
  1363. protected VScrollTableBody createScrollBody() {
  1364. return new VScrollTableBody();
  1365. }
  1366. /**
  1367. * Selects the last row visible in the table
  1368. * <p>
  1369. * For internal use only. May be removed or replaced in the future.
  1370. *
  1371. * @param focusOnly
  1372. * Should the focus only be moved to the last row
  1373. */
  1374. public void selectLastRenderedRowInViewPort(boolean focusOnly) {
  1375. int index = firstRowInViewPort + getFullyVisibleRowCount();
  1376. VScrollTableRow lastRowInViewport = scrollBody.getRowByRowIndex(index);
  1377. if (lastRowInViewport == null) {
  1378. // this should not happen in normal situations (white space at the
  1379. // end of viewport). Select the last rendered as a fallback.
  1380. lastRowInViewport = scrollBody.getRowByRowIndex(scrollBody
  1381. .getLastRendered());
  1382. if (lastRowInViewport == null) {
  1383. return; // empty table
  1384. }
  1385. }
  1386. setRowFocus(lastRowInViewport);
  1387. if (!focusOnly) {
  1388. selectFocusedRow(false, multiselectPending);
  1389. sendSelectedRows();
  1390. }
  1391. }
  1392. /**
  1393. * Selects the first row visible in the table
  1394. * <p>
  1395. * For internal use only. May be removed or replaced in the future.
  1396. *
  1397. * @param focusOnly
  1398. * Should the focus only be moved to the first row
  1399. */
  1400. public void selectFirstRenderedRowInViewPort(boolean focusOnly) {
  1401. int index = firstRowInViewPort;
  1402. VScrollTableRow firstInViewport = scrollBody.getRowByRowIndex(index);
  1403. if (firstInViewport == null) {
  1404. // this should not happen in normal situations
  1405. return;
  1406. }
  1407. setRowFocus(firstInViewport);
  1408. if (!focusOnly) {
  1409. selectFocusedRow(false, multiselectPending);
  1410. sendSelectedRows();
  1411. }
  1412. }
  1413. /** For internal use only. May be removed or replaced in the future. */
  1414. public void setCacheRateFromUIDL(UIDL uidl) {
  1415. setCacheRate(uidl.hasAttribute("cr") ? uidl.getDoubleAttribute("cr")
  1416. : CACHE_RATE_DEFAULT);
  1417. }
  1418. private void setCacheRate(double d) {
  1419. if (cache_rate != d) {
  1420. cache_rate = d;
  1421. cache_react_rate = 0.75 * d;
  1422. }
  1423. }
  1424. /** For internal use only. May be removed or replaced in the future. */
  1425. public void updateActionMap(UIDL mainUidl) {
  1426. UIDL actionsUidl = mainUidl.getChildByTagName("actions");
  1427. if (actionsUidl == null) {
  1428. return;
  1429. }
  1430. final Iterator<?> it = actionsUidl.getChildIterator();
  1431. while (it.hasNext()) {
  1432. final UIDL action = (UIDL) it.next();
  1433. final String key = action.getStringAttribute("key");
  1434. final String caption = action.getStringAttribute("caption");
  1435. actionMap.put(key + "_c", caption);
  1436. if (action.hasAttribute("icon")) {
  1437. // TODO need some uri handling ??
  1438. actionMap.put(key + "_i", action.getStringAttribute("icon"));
  1439. } else {
  1440. actionMap.remove(key + "_i");
  1441. }
  1442. }
  1443. }
  1444. public String getActionCaption(String actionKey) {
  1445. return actionMap.get(actionKey + "_c");
  1446. }
  1447. public String getActionIcon(String actionKey) {
  1448. return client.translateVaadinUri(actionMap.get(actionKey + "_i"));
  1449. }
  1450. private void updateHeader(String[] strings) {
  1451. if (strings == null) {
  1452. return;
  1453. }
  1454. int visibleCols = strings.length;
  1455. int colIndex = 0;
  1456. if (showRowHeaders) {
  1457. tHead.enableColumn(ROW_HEADER_COLUMN_KEY, colIndex);
  1458. visibleCols++;
  1459. visibleColOrder = new String[visibleCols];
  1460. visibleColOrder[colIndex] = ROW_HEADER_COLUMN_KEY;
  1461. colIndex++;
  1462. } else {
  1463. visibleColOrder = new String[visibleCols];
  1464. tHead.removeCell(ROW_HEADER_COLUMN_KEY);
  1465. }
  1466. int i;
  1467. for (i = 0; i < strings.length; i++) {
  1468. final String cid = strings[i];
  1469. visibleColOrder[colIndex] = cid;
  1470. tHead.enableColumn(cid, colIndex);
  1471. colIndex++;
  1472. }
  1473. tHead.setVisible(showColHeaders);
  1474. setContainerHeight();
  1475. }
  1476. /**
  1477. * Updates footers.
  1478. * <p>
  1479. * Update headers whould be called before this method is called!
  1480. * </p>
  1481. *
  1482. * @param strings
  1483. */
  1484. private void updateFooter(String[] strings) {
  1485. if (strings == null) {
  1486. return;
  1487. }
  1488. // Add dummy column if row headers are present
  1489. int colIndex = 0;
  1490. if (showRowHeaders) {
  1491. tFoot.enableColumn(ROW_HEADER_COLUMN_KEY, colIndex);
  1492. colIndex++;
  1493. } else {
  1494. tFoot.removeCell(ROW_HEADER_COLUMN_KEY);
  1495. }
  1496. int i;
  1497. for (i = 0; i < strings.length; i++) {
  1498. final String cid = strings[i];
  1499. tFoot.enableColumn(cid, colIndex);
  1500. colIndex++;
  1501. }
  1502. tFoot.setVisible(showColFooters);
  1503. }
  1504. /**
  1505. * For internal use only. May be removed or replaced in the future.
  1506. *
  1507. * @param uidl
  1508. * which contains row data
  1509. * @param firstRow
  1510. * first row in data set
  1511. * @param reqRows
  1512. * amount of rows in data set
  1513. */
  1514. public void updateBody(UIDL uidl, int firstRow, int reqRows) {
  1515. int oldIndent = scrollBody.getMaxIndent();
  1516. if (uidl == null || reqRows < 1) {
  1517. // container is empty, remove possibly existing rows
  1518. if (firstRow <= 0) {
  1519. postponeSanityCheckForLastRendered = true;
  1520. while (scrollBody.getLastRendered() > scrollBody
  1521. .getFirstRendered()) {
  1522. scrollBody.unlinkRow(false);
  1523. }
  1524. postponeSanityCheckForLastRendered = false;
  1525. scrollBody.unlinkRow(false);
  1526. }
  1527. return;
  1528. }
  1529. scrollBody.renderRows(uidl, firstRow, reqRows);
  1530. discardRowsOutsideCacheWindow();
  1531. scrollBody.calculateMaxIndent();
  1532. if (oldIndent != scrollBody.getMaxIndent()) {
  1533. // indent updated, headers might need adjusting
  1534. headerChangedDuringUpdate = true;
  1535. }
  1536. }
  1537. /** For internal use only. May be removed or replaced in the future. */
  1538. public void updateRowsInBody(UIDL partialRowUpdates) {
  1539. if (partialRowUpdates == null) {
  1540. return;
  1541. }
  1542. int firstRowIx = partialRowUpdates.getIntAttribute("firsturowix");
  1543. int count = partialRowUpdates.getIntAttribute("numurows");
  1544. scrollBody.unlinkRows(firstRowIx, count);
  1545. scrollBody.insertRows(partialRowUpdates, firstRowIx, count);
  1546. }
  1547. /**
  1548. * Updates the internal cache by unlinking rows that fall outside of the
  1549. * caching window.
  1550. */
  1551. protected void discardRowsOutsideCacheWindow() {
  1552. int firstRowToKeep = (int) (firstRowInViewPort - pageLength
  1553. * cache_rate);
  1554. int lastRowToKeep = (int) (firstRowInViewPort + pageLength + pageLength
  1555. * cache_rate);
  1556. // sanity checks:
  1557. if (firstRowToKeep < 0) {
  1558. firstRowToKeep = 0;
  1559. }
  1560. if (lastRowToKeep > totalRows) {
  1561. lastRowToKeep = totalRows - 1;
  1562. }
  1563. debug("Client side calculated cache rows to keep: " + firstRowToKeep
  1564. + "-" + lastRowToKeep);
  1565. if (serverCacheFirst != -1) {
  1566. firstRowToKeep = serverCacheFirst;
  1567. lastRowToKeep = serverCacheLast;
  1568. debug("Server cache rows that override: " + serverCacheFirst + "-"
  1569. + serverCacheLast);
  1570. if (firstRowToKeep < scrollBody.getFirstRendered()
  1571. || lastRowToKeep > scrollBody.getLastRendered()) {
  1572. debug("*** Server wants us to keep " + serverCacheFirst + "-"
  1573. + serverCacheLast + " but we only have rows "
  1574. + scrollBody.getFirstRendered() + "-"
  1575. + scrollBody.getLastRendered() + " rendered!");
  1576. }
  1577. }
  1578. discardRowsOutsideOf(firstRowToKeep, lastRowToKeep);
  1579. scrollBody.fixSpacers();
  1580. scrollBody.restoreRowVisibility();
  1581. }
  1582. private void discardRowsOutsideOf(int optimalFirstRow, int optimalLastRow) {
  1583. /*
  1584. * firstDiscarded and lastDiscarded are only calculated for debug
  1585. * purposes
  1586. */
  1587. int firstDiscarded = -1, lastDiscarded = -1;
  1588. boolean cont = true;
  1589. while (cont && scrollBody.getLastRendered() > optimalFirstRow
  1590. && scrollBody.getFirstRendered() < optimalFirstRow) {
  1591. if (firstDiscarded == -1) {
  1592. firstDiscarded = scrollBody.getFirstRendered();
  1593. }
  1594. // removing row from start
  1595. cont = scrollBody.unlinkRow(true);
  1596. }
  1597. if (firstDiscarded != -1) {
  1598. lastDiscarded = scrollBody.getFirstRendered() - 1;
  1599. debug("Discarded rows " + firstDiscarded + "-" + lastDiscarded);
  1600. }
  1601. firstDiscarded = lastDiscarded = -1;
  1602. cont = true;
  1603. while (cont && scrollBody.getLastRendered() > optimalLastRow) {
  1604. if (lastDiscarded == -1) {
  1605. lastDiscarded = scrollBody.getLastRendered();
  1606. }
  1607. // removing row from the end
  1608. cont = scrollBody.unlinkRow(false);
  1609. }
  1610. if (lastDiscarded != -1) {
  1611. firstDiscarded = scrollBody.getLastRendered() + 1;
  1612. debug("Discarded rows " + firstDiscarded + "-" + lastDiscarded);
  1613. }
  1614. debug("Now in cache: " + scrollBody.getFirstRendered() + "-"
  1615. + scrollBody.getLastRendered());
  1616. }
  1617. /**
  1618. * Inserts rows in the table body or removes them from the table body based
  1619. * on the commands in the UIDL.
  1620. * <p>
  1621. * For internal use only. May be removed or replaced in the future.
  1622. *
  1623. * @param partialRowAdditions
  1624. * the UIDL containing row updates.
  1625. */
  1626. public void addAndRemoveRows(UIDL partialRowAdditions) {
  1627. if (partialRowAdditions == null) {
  1628. return;
  1629. }
  1630. if (partialRowAdditions.hasAttribute("hide")) {
  1631. scrollBody.unlinkAndReindexRows(
  1632. partialRowAdditions.getIntAttribute("firstprowix"),
  1633. partialRowAdditions.getIntAttribute("numprows"));
  1634. scrollBody.ensureCacheFilled();
  1635. } else {
  1636. if (partialRowAdditions.hasAttribute("delbelow")) {
  1637. scrollBody.insertRowsDeleteBelow(partialRowAdditions,
  1638. partialRowAdditions.getIntAttribute("firstprowix"),
  1639. partialRowAdditions.getIntAttribute("numprows"));
  1640. } else {
  1641. scrollBody.insertAndReindexRows(partialRowAdditions,
  1642. partialRowAdditions.getIntAttribute("firstprowix"),
  1643. partialRowAdditions.getIntAttribute("numprows"));
  1644. }
  1645. }
  1646. discardRowsOutsideCacheWindow();
  1647. }
  1648. /**
  1649. * Gives correct column index for given column key ("cid" in UIDL).
  1650. *
  1651. * @param colKey
  1652. * @return column index of visible columns, -1 if column not visible
  1653. */
  1654. private int getColIndexByKey(String colKey) {
  1655. // return 0 if asked for rowHeaders
  1656. if (ROW_HEADER_COLUMN_KEY.equals(colKey)) {
  1657. return 0;
  1658. }
  1659. for (int i = 0; i < visibleColOrder.length; i++) {
  1660. if (visibleColOrder[i].equals(colKey)) {
  1661. return i;
  1662. }
  1663. }
  1664. return -1;
  1665. }
  1666. private boolean isMultiSelectModeSimple() {
  1667. return selectMode == SelectMode.MULTI
  1668. && multiselectmode == MULTISELECT_MODE_SIMPLE;
  1669. }
  1670. private boolean isSingleSelectMode() {
  1671. return selectMode == SelectMode.SINGLE;
  1672. }
  1673. private boolean isMultiSelectModeAny() {
  1674. return selectMode == SelectMode.MULTI;
  1675. }
  1676. private boolean isMultiSelectModeDefault() {
  1677. return selectMode == SelectMode.MULTI
  1678. && multiselectmode == MULTISELECT_MODE_DEFAULT;
  1679. }
  1680. private void setMultiSelectMode(int multiselectmode) {
  1681. if (BrowserInfo.get().isTouchDevice()) {
  1682. // Always use the simple mode for touch devices that do not have
  1683. // shift/ctrl keys
  1684. this.multiselectmode = MULTISELECT_MODE_SIMPLE;
  1685. } else {
  1686. this.multiselectmode = multiselectmode;
  1687. }
  1688. }
  1689. /** For internal use only. May be removed or replaced in the future. */
  1690. public boolean isSelectable() {
  1691. return selectMode.getId() > SelectMode.NONE.getId();
  1692. }
  1693. private boolean isCollapsedColumn(String colKey) {
  1694. if (collapsedColumns == null) {
  1695. return false;
  1696. }
  1697. if (collapsedColumns.contains(colKey)) {
  1698. return true;
  1699. }
  1700. return false;
  1701. }
  1702. private String getColKeyByIndex(int index) {
  1703. return tHead.getHeaderCell(index).getColKey();
  1704. }
  1705. /**
  1706. * Note: not part of the official API, extend at your own risk. May be
  1707. * removed or replaced in the future.
  1708. *
  1709. * Sets the indicated column's width for headers and scrollBody alike.
  1710. *
  1711. * @param colIndex
  1712. * index of the modified column
  1713. * @param w
  1714. * new width (may be subject to modifications if doesn't meet
  1715. * minimum requirements)
  1716. * @param isDefinedWidth
  1717. * disables expand ratio if set true
  1718. */
  1719. protected void setColWidth(int colIndex, int w, boolean isDefinedWidth) {
  1720. final HeaderCell hcell = tHead.getHeaderCell(colIndex);
  1721. // Make sure that the column grows to accommodate the sort indicator if
  1722. // necessary.
  1723. // get min width with no indent or padding
  1724. int minWidth = hcell.getMinWidth(false, false);
  1725. if (w < minWidth) {
  1726. w = minWidth;
  1727. }
  1728. // Set header column width WITHOUT INDENT
  1729. hcell.setWidth(w, isDefinedWidth);
  1730. // Set footer column width likewise
  1731. FooterCell fcell = tFoot.getFooterCell(colIndex);
  1732. fcell.setWidth(w, isDefinedWidth);
  1733. // Ensure indicators have been taken into account
  1734. tHead.resizeCaptionContainer(hcell);
  1735. // Make sure that the body column grows to accommodate the indent if
  1736. // necessary.
  1737. // get min width with indent, no padding
  1738. minWidth = hcell.getMinWidth(true, false);
  1739. if (w < minWidth) {
  1740. w = minWidth;
  1741. }
  1742. // Set body column width
  1743. scrollBody.setColWidth(colIndex, w);
  1744. }
  1745. private int getColWidth(String colKey) {
  1746. return tHead.getHeaderCell(colKey).getWidthWithIndent();
  1747. }
  1748. /**
  1749. * Get a rendered row by its key
  1750. *
  1751. * @param key
  1752. * The key to search with
  1753. * @return
  1754. */
  1755. public VScrollTableRow getRenderedRowByKey(String key) {
  1756. if (scrollBody != null) {
  1757. final Iterator<Widget> it = scrollBody.iterator();
  1758. VScrollTableRow r = null;
  1759. while (it.hasNext()) {
  1760. r = (VScrollTableRow) it.next();
  1761. if (r.getKey().equals(key)) {
  1762. return r;
  1763. }
  1764. }
  1765. }
  1766. return null;
  1767. }
  1768. /**
  1769. * Returns the next row to the given row
  1770. *
  1771. * @param row
  1772. * The row to calculate from
  1773. *
  1774. * @return The next row or null if no row exists
  1775. */
  1776. private VScrollTableRow getNextRow(VScrollTableRow row, int offset) {
  1777. final Iterator<Widget> it = scrollBody.iterator();
  1778. VScrollTableRow r = null;
  1779. while (it.hasNext()) {
  1780. r = (VScrollTableRow) it.next();
  1781. if (r == row) {
  1782. r = null;
  1783. while (offset >= 0 && it.hasNext()) {
  1784. r = (VScrollTableRow) it.next();
  1785. offset--;
  1786. }
  1787. return r;
  1788. }
  1789. }
  1790. return null;
  1791. }
  1792. /**
  1793. * Returns the previous row from the given row
  1794. *
  1795. * @param row
  1796. * The row to calculate from
  1797. * @return The previous row or null if no row exists
  1798. */
  1799. private VScrollTableRow getPreviousRow(VScrollTableRow row, int offset) {
  1800. final Iterator<Widget> it = scrollBody.iterator();
  1801. final Iterator<Widget> offsetIt = scrollBody.iterator();
  1802. VScrollTableRow r = null;
  1803. VScrollTableRow prev = null;
  1804. while (it.hasNext()) {
  1805. r = (VScrollTableRow) it.next();
  1806. if (offset < 0) {
  1807. prev = (VScrollTableRow) offsetIt.next();
  1808. }
  1809. if (r == row) {
  1810. return prev;
  1811. }
  1812. offset--;
  1813. }
  1814. return null;
  1815. }
  1816. protected void reOrderColumn(String columnKey, int newIndex) {
  1817. final int oldIndex = getColIndexByKey(columnKey);
  1818. // Change header order
  1819. tHead.moveCell(oldIndex, newIndex);
  1820. // Change body order
  1821. scrollBody.moveCol(oldIndex, newIndex);
  1822. // Change footer order
  1823. tFoot.moveCell(oldIndex, newIndex);
  1824. /*
  1825. * Build new columnOrder and update it to server Note that columnOrder
  1826. * also contains collapsed columns so we cannot directly build it from
  1827. * cells vector Loop the old columnOrder and append in order to new
  1828. * array unless on moved columnKey. On new index also put the moved key
  1829. * i == index on columnOrder, j == index on newOrder
  1830. */
  1831. final String oldKeyOnNewIndex = visibleColOrder[newIndex];
  1832. if (showRowHeaders) {
  1833. newIndex--; // columnOrder don't have rowHeader
  1834. }
  1835. // add back hidden rows,
  1836. for (int i = 0; i < columnOrder.length; i++) {
  1837. if (columnOrder[i].equals(oldKeyOnNewIndex)) {
  1838. break; // break loop at target
  1839. }
  1840. if (isCollapsedColumn(columnOrder[i])) {
  1841. newIndex++;
  1842. }
  1843. }
  1844. // finally we can build the new columnOrder for server
  1845. final String[] newOrder = new String[columnOrder.length];
  1846. for (int i = 0, j = 0; j < newOrder.length; i++) {
  1847. if (j == newIndex) {
  1848. newOrder[j] = columnKey;
  1849. j++;
  1850. }
  1851. if (i == columnOrder.length) {
  1852. break;
  1853. }
  1854. if (columnOrder[i].equals(columnKey)) {
  1855. continue;
  1856. }
  1857. newOrder[j] = columnOrder[i];
  1858. j++;
  1859. }
  1860. columnOrder = newOrder;
  1861. // also update visibleColumnOrder
  1862. int i = showRowHeaders ? 1 : 0;
  1863. for (int j = 0; j < newOrder.length; j++) {
  1864. final String cid = newOrder[j];
  1865. if (!isCollapsedColumn(cid)) {
  1866. visibleColOrder[i++] = cid;
  1867. }
  1868. }
  1869. client.updateVariable(paintableId, "columnorder", columnOrder, false);
  1870. if (client.hasEventListeners(this,
  1871. TableConstants.COLUMN_REORDER_EVENT_ID)) {
  1872. client.sendPendingVariableChanges();
  1873. }
  1874. }
  1875. @Override
  1876. protected void onDetach() {
  1877. detachedScrollPosition = scrollBodyPanel.getScrollPosition();
  1878. rowRequestHandler.cancel();
  1879. super.onDetach();
  1880. // ensure that scrollPosElement will be detached
  1881. if (scrollPositionElement != null) {
  1882. final Element parent = DOM.getParent(scrollPositionElement);
  1883. if (parent != null) {
  1884. DOM.removeChild(parent, scrollPositionElement);
  1885. }
  1886. }
  1887. }
  1888. @Override
  1889. public void onAttach() {
  1890. super.onAttach();
  1891. scrollBodyPanel.setScrollPosition(detachedScrollPosition);
  1892. }
  1893. /**
  1894. * Run only once when component is attached and received its initial
  1895. * content. This function:
  1896. *
  1897. * * Syncs headers and bodys "natural widths and saves the values.
  1898. *
  1899. * * Sets proper width and height
  1900. *
  1901. * * Makes deferred request to get some cache rows
  1902. *
  1903. * For internal use only. May be removed or replaced in the future.
  1904. */
  1905. public void sizeInit() {
  1906. sizeNeedsInit = false;
  1907. scrollBody.setContainerHeight();
  1908. /*
  1909. * We will use browsers table rendering algorithm to find proper column
  1910. * widths. If content and header take less space than available, we will
  1911. * divide extra space relatively to each column which has not width set.
  1912. *
  1913. * Overflow pixels are added to last column.
  1914. */
  1915. Iterator<Widget> headCells = tHead.iterator();
  1916. Iterator<Widget> footCells = tFoot.iterator();
  1917. int i = 0;
  1918. int totalExplicitColumnsWidths = 0;
  1919. int total = 0;
  1920. float expandRatioDivider = 0;
  1921. final int[] widths = new int[tHead.visibleCells.size()];
  1922. tHead.enableBrowserIntelligence();
  1923. tFoot.enableBrowserIntelligence();
  1924. int hierarchyColumnIndent = scrollBody != null ? scrollBody
  1925. .getMaxIndent() : 0;
  1926. HeaderCell hierarchyHeaderWithExpandRatio = null;
  1927. // first loop: collect natural widths
  1928. while (headCells.hasNext()) {
  1929. final HeaderCell hCell = (HeaderCell) headCells.next();
  1930. final FooterCell fCell = (FooterCell) footCells.next();
  1931. boolean needsIndent = hierarchyColumnIndent > 0
  1932. && hCell.isHierarchyColumn();
  1933. hCell.saveNaturalColumnWidthIfNotSaved(i);
  1934. fCell.saveNaturalColumnWidthIfNotSaved(i);
  1935. int w = hCell.getWidth();
  1936. if (hCell.isDefinedWidth()) {
  1937. // server has defined column width explicitly
  1938. if (needsIndent && w < hierarchyColumnIndent) {
  1939. // hierarchy indent overrides explicitly set width
  1940. w = hierarchyColumnIndent;
  1941. }
  1942. totalExplicitColumnsWidths += w;
  1943. } else {
  1944. if (hCell.getExpandRatio() > 0) {
  1945. expandRatioDivider += hCell.getExpandRatio();
  1946. w = 0;
  1947. if (needsIndent && w < hierarchyColumnIndent) {
  1948. hierarchyHeaderWithExpandRatio = hCell;
  1949. // don't add to widths here, because will be included in
  1950. // the expand ratio space if there's enough of it
  1951. }
  1952. } else {
  1953. // get and store greater of header width and column width,
  1954. // and store it as a minimum natural column width (these
  1955. // already contain the indent if any)
  1956. int headerWidth = hCell.getNaturalColumnWidth(i);
  1957. int footerWidth = fCell.getNaturalColumnWidth(i);
  1958. w = headerWidth > footerWidth ? headerWidth : footerWidth;
  1959. }
  1960. if (w != 0) {
  1961. hCell.setNaturalMinimumColumnWidth(w);
  1962. fCell.setNaturalMinimumColumnWidth(w);
  1963. }
  1964. }
  1965. widths[i] = w;
  1966. total += w;
  1967. i++;
  1968. }
  1969. if (hierarchyHeaderWithExpandRatio != null) {
  1970. total += hierarchyColumnIndent;
  1971. }
  1972. tHead.disableBrowserIntelligence();
  1973. tFoot.disableBrowserIntelligence();
  1974. boolean willHaveScrollbarz = willHaveScrollbars();
  1975. // fix "natural" width if width not set
  1976. if (isDynamicWidth()) {
  1977. int w = total;
  1978. w += scrollBody.getCellExtraWidth() * visibleColOrder.length;
  1979. if (willHaveScrollbarz) {
  1980. w += WidgetUtil.getNativeScrollbarSize();
  1981. }
  1982. setContentWidth(w);
  1983. }
  1984. int availW = scrollBody.getAvailableWidth();
  1985. if (BrowserInfo.get().isIE()) {
  1986. // Hey IE, are you really sure about this?
  1987. availW = scrollBody.getAvailableWidth();
  1988. }
  1989. availW -= scrollBody.getCellExtraWidth() * visibleColOrder.length;
  1990. if (willHaveScrollbarz) {
  1991. availW -= WidgetUtil.getNativeScrollbarSize();
  1992. }
  1993. // TODO refactor this code to be the same as in resize timer
  1994. if (availW > total) {
  1995. // natural size is smaller than available space
  1996. int extraSpace = availW - total;
  1997. if (hierarchyHeaderWithExpandRatio != null) {
  1998. /*
  1999. * add the indent's space back to ensure each column gets an
  2000. * even share according to the expand ratios (note: if the
  2001. * allocated space isn't enough for the hierarchy column it
  2002. * shall be treated like a defined width column and the indent
  2003. * space gets removed from the extra space again)
  2004. */
  2005. extraSpace += hierarchyColumnIndent;
  2006. }
  2007. final int totalWidthR = total - totalExplicitColumnsWidths;
  2008. int checksum = 0;
  2009. if (extraSpace == 1) {
  2010. // We cannot divide one single pixel so we give it the first
  2011. // undefined column
  2012. // no need to worry about indent here
  2013. headCells = tHead.iterator();
  2014. i = 0;
  2015. checksum = availW;
  2016. while (headCells.hasNext()) {
  2017. HeaderCell hc = (HeaderCell) headCells.next();
  2018. if (!hc.isDefinedWidth()) {
  2019. widths[i]++;
  2020. break;
  2021. }
  2022. i++;
  2023. }
  2024. } else if (expandRatioDivider > 0) {
  2025. boolean setIndentToHierarchyHeader = false;
  2026. if (hierarchyHeaderWithExpandRatio != null) {
  2027. // ensure first that the hierarchyColumn gets at least the
  2028. // space allocated for indent
  2029. final int newSpace = Math
  2030. .round((extraSpace * (hierarchyHeaderWithExpandRatio
  2031. .getExpandRatio() / expandRatioDivider)));
  2032. if (newSpace < hierarchyColumnIndent) {
  2033. // not enough space for indent, remove indent from the
  2034. // extraSpace again and handle hierarchy column's header
  2035. // separately
  2036. setIndentToHierarchyHeader = true;
  2037. extraSpace -= hierarchyColumnIndent;
  2038. }
  2039. }
  2040. // visible columns have some active expand ratios, excess
  2041. // space is divided according to them
  2042. headCells = tHead.iterator();
  2043. i = 0;
  2044. while (headCells.hasNext()) {
  2045. HeaderCell hCell = (HeaderCell) headCells.next();
  2046. if (hCell.getExpandRatio() > 0) {
  2047. int w = widths[i];
  2048. if (setIndentToHierarchyHeader
  2049. && hierarchyHeaderWithExpandRatio.equals(hCell)) {
  2050. // hierarchy column's header is no longer part of
  2051. // the expansion divide and only gets indent
  2052. w += hierarchyColumnIndent;
  2053. } else {
  2054. final int newSpace = Math
  2055. .round((extraSpace * (hCell
  2056. .getExpandRatio() / expandRatioDivider)));
  2057. w += newSpace;
  2058. }
  2059. widths[i] = w;
  2060. }
  2061. checksum += widths[i];
  2062. i++;
  2063. }
  2064. } else if (totalWidthR > 0) {
  2065. // no expand ratios defined, we will share extra space
  2066. // relatively to "natural widths" among those without
  2067. // explicit width
  2068. // no need to worry about indent here, it's already included
  2069. headCells = tHead.iterator();
  2070. i = 0;
  2071. while (headCells.hasNext()) {
  2072. HeaderCell hCell = (HeaderCell) headCells.next();
  2073. if (!hCell.isDefinedWidth()) {
  2074. int w = widths[i];
  2075. final int newSpace = Math.round((float) extraSpace
  2076. * (float) w / totalWidthR);
  2077. w += newSpace;
  2078. widths[i] = w;
  2079. }
  2080. checksum += widths[i];
  2081. i++;
  2082. }
  2083. }
  2084. if (extraSpace > 0 && checksum != availW) {
  2085. /*
  2086. * There might be in some cases a rounding error of 1px when
  2087. * extra space is divided so if there is one then we give the
  2088. * first undefined column 1 more pixel
  2089. */
  2090. headCells = tHead.iterator();
  2091. i = 0;
  2092. while (headCells.hasNext()) {
  2093. HeaderCell hc = (HeaderCell) headCells.next();
  2094. if (!hc.isDefinedWidth()) {
  2095. widths[i] += availW - checksum;
  2096. break;
  2097. }
  2098. i++;
  2099. }
  2100. }
  2101. } else {
  2102. // body's size will be more than available and scrollbar will appear
  2103. }
  2104. // last loop: set possibly modified values or reset if new tBody
  2105. i = 0;
  2106. headCells = tHead.iterator();
  2107. while (headCells.hasNext()) {
  2108. final HeaderCell hCell = (HeaderCell) headCells.next();
  2109. if (isNewBody || hCell.getWidth() == -1) {
  2110. final int w = widths[i];
  2111. setColWidth(i, w, false);
  2112. }
  2113. i++;
  2114. }
  2115. initializedAndAttached = true;
  2116. updatePageLength();
  2117. /*
  2118. * Fix "natural" height if height is not set. This must be after width
  2119. * fixing so the components' widths have been adjusted.
  2120. */
  2121. if (isDynamicHeight()) {
  2122. /*
  2123. * We must force an update of the row height as this point as it
  2124. * might have been (incorrectly) calculated earlier
  2125. */
  2126. /*
  2127. * TreeTable updates stuff in a funky order, so we must set the
  2128. * height as zero here before doing the real update to make it
  2129. * realize that there is no content,
  2130. */
  2131. if (pageLength == totalRows && pageLength == 0) {
  2132. scrollBody.setHeight("0px");
  2133. }
  2134. int bodyHeight;
  2135. if (pageLength == totalRows) {
  2136. /*
  2137. * A hack to support variable height rows when paging is off.
  2138. * Generally this is not supported by scrolltable. We want to
  2139. * show all rows so the bodyHeight should be equal to the table
  2140. * height.
  2141. */
  2142. // int bodyHeight = scrollBody.getOffsetHeight();
  2143. bodyHeight = scrollBody.getRequiredHeight();
  2144. } else {
  2145. bodyHeight = (int) Math.round(scrollBody.getRowHeight(true)
  2146. * pageLength);
  2147. }
  2148. boolean needsSpaceForHorizontalSrollbar = (total > availW);
  2149. if (needsSpaceForHorizontalSrollbar) {
  2150. bodyHeight += WidgetUtil.getNativeScrollbarSize();
  2151. }
  2152. scrollBodyPanel.setHeight(bodyHeight + "px");
  2153. WidgetUtil.runWebkitOverflowAutoFix(scrollBodyPanel.getElement());
  2154. }
  2155. isNewBody = false;
  2156. if (firstvisible > 0) {
  2157. enableLazyScroller();
  2158. }
  2159. if (enabled) {
  2160. // Do we need cache rows
  2161. if (scrollBody.getLastRendered() + 1 < firstRowInViewPort
  2162. + pageLength + (int) cache_react_rate * pageLength) {
  2163. if (totalRows - 1 > scrollBody.getLastRendered()) {
  2164. // fetch cache rows
  2165. int firstInNewSet = scrollBody.getLastRendered() + 1;
  2166. int lastInNewSet = (int) (firstRowInViewPort + pageLength + cache_rate
  2167. * pageLength);
  2168. if (lastInNewSet > totalRows - 1) {
  2169. lastInNewSet = totalRows - 1;
  2170. }
  2171. rowRequestHandler.triggerRowFetch(firstInNewSet,
  2172. lastInNewSet - firstInNewSet + 1, 1);
  2173. }
  2174. }
  2175. }
  2176. /*
  2177. * Ensures the column alignments are correct at initial loading. <br/>
  2178. * (child components widths are correct)
  2179. */
  2180. WidgetUtil.runWebkitOverflowAutoFixDeferred(scrollBodyPanel
  2181. .getElement());
  2182. hadScrollBars = willHaveScrollbarz;
  2183. }
  2184. /**
  2185. * Note: this method is not part of official API although declared as
  2186. * protected. Extend at your own risk.
  2187. *
  2188. * @return true if content area will have scrollbars visible.
  2189. */
  2190. protected boolean willHaveScrollbars() {
  2191. if (isDynamicHeight()) {
  2192. if (pageLength < totalRows) {
  2193. return true;
  2194. }
  2195. } else {
  2196. int fakeheight = (int) Math.round(scrollBody.getRowHeight()
  2197. * totalRows);
  2198. int availableHeight = scrollBodyPanel.getElement().getPropertyInt(
  2199. "clientHeight");
  2200. if (fakeheight > availableHeight) {
  2201. return true;
  2202. }
  2203. }
  2204. return false;
  2205. }
  2206. private void announceScrollPosition() {
  2207. if (scrollPositionElement == null) {
  2208. scrollPositionElement = DOM.createDiv();
  2209. scrollPositionElement.setClassName(getStylePrimaryName()
  2210. + "-scrollposition");
  2211. scrollPositionElement.getStyle().setPosition(Position.ABSOLUTE);
  2212. scrollPositionElement.getStyle().setDisplay(Display.NONE);
  2213. getElement().appendChild(scrollPositionElement);
  2214. }
  2215. Style style = scrollPositionElement.getStyle();
  2216. style.setMarginLeft(getElement().getOffsetWidth() / 2 - 80, Unit.PX);
  2217. style.setMarginTop(-scrollBodyPanel.getOffsetHeight(), Unit.PX);
  2218. // indexes go from 1-totalRows, as rowheaders in index-mode indicate
  2219. int last = (firstRowInViewPort + pageLength);
  2220. if (last > totalRows) {
  2221. last = totalRows;
  2222. }
  2223. scrollPositionElement.setInnerHTML("<span>" + (firstRowInViewPort + 1)
  2224. + " &ndash; " + (last) + "..." + "</span>");
  2225. style.setDisplay(Display.BLOCK);
  2226. }
  2227. /** For internal use only. May be removed or replaced in the future. */
  2228. public void hideScrollPositionAnnotation() {
  2229. if (scrollPositionElement != null) {
  2230. scrollPositionElement.getStyle().setDisplay(Display.NONE);
  2231. }
  2232. }
  2233. /** For internal use only. May be removed or replaced in the future. */
  2234. public boolean isScrollPositionVisible() {
  2235. return scrollPositionElement != null
  2236. && !scrollPositionElement.getStyle().getDisplay()
  2237. .equals(Display.NONE.toString());
  2238. }
  2239. /** For internal use only. May be removed or replaced in the future. */
  2240. public class RowRequestHandler extends Timer {
  2241. private int reqFirstRow = 0;
  2242. private int reqRows = 0;
  2243. private boolean isRequestHandlerRunning = false;
  2244. public void triggerRowFetch(int first, int rows) {
  2245. setReqFirstRow(first);
  2246. setReqRows(rows);
  2247. deferRowFetch();
  2248. }
  2249. public void triggerRowFetch(int first, int rows, int delay) {
  2250. setReqFirstRow(first);
  2251. setReqRows(rows);
  2252. deferRowFetch(delay);
  2253. }
  2254. public void deferRowFetch() {
  2255. deferRowFetch(250);
  2256. }
  2257. public boolean isRequestHandlerRunning() {
  2258. return isRequestHandlerRunning;
  2259. }
  2260. public void deferRowFetch(int msec) {
  2261. isRequestHandlerRunning = true;
  2262. if (reqRows > 0 && reqFirstRow < totalRows) {
  2263. schedule(msec);
  2264. // tell scroll position to user if currently "visible" rows are
  2265. // not rendered
  2266. if (totalRows > pageLength
  2267. && ((firstRowInViewPort + pageLength > scrollBody
  2268. .getLastRendered()) || (firstRowInViewPort < scrollBody
  2269. .getFirstRendered()))) {
  2270. announceScrollPosition();
  2271. } else {
  2272. hideScrollPositionAnnotation();
  2273. }
  2274. }
  2275. }
  2276. public int getReqFirstRow() {
  2277. return reqFirstRow;
  2278. }
  2279. public void setReqFirstRow(int reqFirstRow) {
  2280. if (reqFirstRow < 0) {
  2281. this.reqFirstRow = 0;
  2282. } else if (reqFirstRow >= totalRows) {
  2283. this.reqFirstRow = totalRows - 1;
  2284. } else {
  2285. this.reqFirstRow = reqFirstRow;
  2286. }
  2287. }
  2288. public void setReqRows(int reqRows) {
  2289. if (reqRows < 0) {
  2290. this.reqRows = 0;
  2291. } else if (reqFirstRow + reqRows > totalRows) {
  2292. this.reqRows = totalRows - reqFirstRow;
  2293. } else {
  2294. this.reqRows = reqRows;
  2295. }
  2296. }
  2297. @Override
  2298. public void run() {
  2299. if (client.hasActiveRequest() || navKeyDown) {
  2300. // if client connection is busy, don't bother loading it more
  2301. VConsole.log("Postponed rowfetch");
  2302. schedule(250);
  2303. } else if (allRenderedRowsAreNew() && !updatedReqRows) {
  2304. /*
  2305. * If all rows are new, there might have been a server-side call
  2306. * to Table.setCurrentPageFirstItemIndex(int) In this case,
  2307. * scrolling event takes way too late, and all the rows from
  2308. * previous viewport to this one were requested.
  2309. *
  2310. * This should prevent requesting unneeded rows by updating
  2311. * reqFirstRow and reqRows before needing them. See (#14135)
  2312. */
  2313. setReqFirstRow((firstRowInViewPort - (int) (pageLength * cache_rate)));
  2314. int last = firstRowInViewPort + (int) (cache_rate * pageLength)
  2315. + pageLength - 1;
  2316. if (last >= totalRows) {
  2317. last = totalRows - 1;
  2318. }
  2319. setReqRows(last - getReqFirstRow() + 1);
  2320. updatedReqRows = true;
  2321. schedule(250);
  2322. } else {
  2323. int firstRendered = scrollBody.getFirstRendered();
  2324. int lastRendered = scrollBody.getLastRendered();
  2325. if (lastRendered > totalRows) {
  2326. lastRendered = totalRows - 1;
  2327. }
  2328. boolean rendered = firstRendered >= 0 && lastRendered >= 0;
  2329. int firstToBeRendered = firstRendered;
  2330. if (reqFirstRow < firstToBeRendered) {
  2331. firstToBeRendered = reqFirstRow;
  2332. } else if (firstRowInViewPort - (int) (cache_rate * pageLength) > firstToBeRendered) {
  2333. firstToBeRendered = firstRowInViewPort
  2334. - (int) (cache_rate * pageLength);
  2335. if (firstToBeRendered < 0) {
  2336. firstToBeRendered = 0;
  2337. }
  2338. } else if (rendered && firstRendered + 1 < reqFirstRow
  2339. && lastRendered + 1 < reqFirstRow) {
  2340. // requested rows must fall within the requested rendering
  2341. // area
  2342. firstToBeRendered = reqFirstRow;
  2343. }
  2344. if (firstToBeRendered + reqRows < firstRendered) {
  2345. // must increase the required row count accordingly,
  2346. // otherwise may leave a gap and the rows beyond will get
  2347. // removed
  2348. setReqRows(firstRendered - firstToBeRendered);
  2349. }
  2350. int lastToBeRendered = lastRendered;
  2351. int lastReqRow = reqFirstRow + reqRows - 1;
  2352. if (lastReqRow > lastToBeRendered) {
  2353. lastToBeRendered = lastReqRow;
  2354. } else if (firstRowInViewPort + pageLength + pageLength
  2355. * cache_rate < lastToBeRendered) {
  2356. lastToBeRendered = (firstRowInViewPort + pageLength + (int) (pageLength * cache_rate));
  2357. if (lastToBeRendered >= totalRows) {
  2358. lastToBeRendered = totalRows - 1;
  2359. }
  2360. // due Safari 3.1 bug (see #2607), verify reqrows, original
  2361. // problem unknown, but this should catch the issue
  2362. if (lastReqRow > lastToBeRendered) {
  2363. setReqRows(lastToBeRendered - reqFirstRow);
  2364. }
  2365. } else if (rendered && lastRendered - 1 > lastReqRow
  2366. && firstRendered - 1 > lastReqRow) {
  2367. // requested rows must fall within the requested rendering
  2368. // area
  2369. lastToBeRendered = lastReqRow;
  2370. }
  2371. if (lastToBeRendered > totalRows) {
  2372. lastToBeRendered = totalRows - 1;
  2373. }
  2374. if (reqFirstRow < firstToBeRendered
  2375. || (reqFirstRow > firstToBeRendered && (reqFirstRow < firstRendered || reqFirstRow > lastRendered + 1))) {
  2376. setReqFirstRow(firstToBeRendered);
  2377. }
  2378. if (lastRendered < lastToBeRendered
  2379. && lastRendered + reqRows < lastToBeRendered) {
  2380. // must increase the required row count accordingly,
  2381. // otherwise may leave a gap and the rows after will get
  2382. // removed
  2383. setReqRows(lastToBeRendered - lastRendered);
  2384. } else if (lastToBeRendered >= firstRendered
  2385. && reqFirstRow + reqRows < firstRendered) {
  2386. setReqRows(lastToBeRendered - lastRendered);
  2387. }
  2388. client.updateVariable(paintableId, "firstToBeRendered",
  2389. firstToBeRendered, false);
  2390. client.updateVariable(paintableId, "lastToBeRendered",
  2391. lastToBeRendered, false);
  2392. // don't request server to update page first index in case it
  2393. // has not been changed
  2394. if (firstRowInViewPort != firstvisible) {
  2395. // remember which firstvisible we requested, in case the
  2396. // server has a differing opinion
  2397. lastRequestedFirstvisible = firstRowInViewPort;
  2398. client.updateVariable(paintableId, "firstvisible",
  2399. firstRowInViewPort, false);
  2400. }
  2401. client.updateVariable(paintableId, "reqfirstrow", reqFirstRow,
  2402. false);
  2403. client.updateVariable(paintableId, "reqrows", reqRows, true);
  2404. if (selectionChanged) {
  2405. unSyncedselectionsBeforeRowFetch = new HashSet<Object>(
  2406. selectedRowKeys);
  2407. }
  2408. isRequestHandlerRunning = false;
  2409. }
  2410. }
  2411. /**
  2412. * Sends request to refresh content at this position.
  2413. */
  2414. public void refreshContent() {
  2415. isRequestHandlerRunning = true;
  2416. int first = (int) (firstRowInViewPort - pageLength * cache_rate);
  2417. int reqRows = (int) (2 * pageLength * cache_rate + pageLength);
  2418. if (first < 0) {
  2419. reqRows = reqRows + first;
  2420. first = 0;
  2421. }
  2422. setReqFirstRow(first);
  2423. setReqRows(reqRows);
  2424. run();
  2425. }
  2426. }
  2427. public class HeaderCell extends Widget {
  2428. Element td = DOM.createTD();
  2429. Element captionContainer = DOM.createDiv();
  2430. Element sortIndicator = DOM.createDiv();
  2431. Element colResizeWidget = DOM.createDiv();
  2432. Element floatingCopyOfHeaderCell;
  2433. private boolean sortable = false;
  2434. private final String cid;
  2435. private boolean dragging;
  2436. private Integer currentDragX = null; // is used to resolve #14796
  2437. private int dragStartX;
  2438. private int colIndex;
  2439. private int originalWidth;
  2440. private boolean isResizing;
  2441. private int headerX;
  2442. private boolean moved;
  2443. private int closestSlot;
  2444. private int width = -1;
  2445. private int naturalWidth = -1;
  2446. private char align = ALIGN_LEFT;
  2447. boolean definedWidth = false;
  2448. private float expandRatio = 0;
  2449. private boolean sorted;
  2450. public void setSortable(boolean b) {
  2451. sortable = b;
  2452. }
  2453. /**
  2454. * Makes room for the sorting indicator in case the column that the
  2455. * header cell belongs to is sorted. This is done by resizing the width
  2456. * of the caption container element by the correct amount
  2457. */
  2458. public void resizeCaptionContainer(int rightSpacing) {
  2459. int captionContainerWidth = width
  2460. - colResizeWidget.getOffsetWidth() - rightSpacing;
  2461. if (td.getClassName().contains("-asc")
  2462. || td.getClassName().contains("-desc")) {
  2463. // Leave room for the sort indicator
  2464. captionContainerWidth -= sortIndicator.getOffsetWidth();
  2465. }
  2466. if (captionContainerWidth < 0) {
  2467. rightSpacing += captionContainerWidth;
  2468. captionContainerWidth = 0;
  2469. }
  2470. captionContainer.getStyle().setPropertyPx("width",
  2471. captionContainerWidth);
  2472. // Apply/Remove spacing if defined
  2473. if (rightSpacing > 0) {
  2474. colResizeWidget.getStyle().setMarginLeft(rightSpacing, Unit.PX);
  2475. } else {
  2476. colResizeWidget.getStyle().clearMarginLeft();
  2477. }
  2478. }
  2479. public void setNaturalMinimumColumnWidth(int w) {
  2480. naturalWidth = w;
  2481. }
  2482. public HeaderCell(String colId, String headerText) {
  2483. cid = colId;
  2484. setText(headerText);
  2485. td.appendChild(colResizeWidget);
  2486. // ensure no clipping initially (problem on column additions)
  2487. captionContainer.getStyle().setOverflow(Overflow.VISIBLE);
  2488. td.appendChild(sortIndicator);
  2489. td.appendChild(captionContainer);
  2490. DOM.sinkEvents(td, Event.MOUSEEVENTS | Event.ONDBLCLICK
  2491. | Event.ONCONTEXTMENU | Event.TOUCHEVENTS);
  2492. setElement(td);
  2493. setAlign(ALIGN_LEFT);
  2494. }
  2495. protected void updateStyleNames(String primaryStyleName) {
  2496. colResizeWidget.setClassName(primaryStyleName + "-resizer");
  2497. sortIndicator.setClassName(primaryStyleName + "-sort-indicator");
  2498. captionContainer.setClassName(primaryStyleName
  2499. + "-caption-container");
  2500. if (sorted) {
  2501. if (sortAscending) {
  2502. setStyleName(primaryStyleName + "-header-cell-asc");
  2503. } else {
  2504. setStyleName(primaryStyleName + "-header-cell-desc");
  2505. }
  2506. } else {
  2507. setStyleName(primaryStyleName + "-header-cell");
  2508. }
  2509. final String ALIGN_PREFIX = primaryStyleName
  2510. + "-caption-container-align-";
  2511. switch (align) {
  2512. case ALIGN_CENTER:
  2513. captionContainer.addClassName(ALIGN_PREFIX + "center");
  2514. break;
  2515. case ALIGN_RIGHT:
  2516. captionContainer.addClassName(ALIGN_PREFIX + "right");
  2517. break;
  2518. default:
  2519. captionContainer.addClassName(ALIGN_PREFIX + "left");
  2520. break;
  2521. }
  2522. }
  2523. public void disableAutoWidthCalculation() {
  2524. definedWidth = true;
  2525. expandRatio = 0;
  2526. }
  2527. /**
  2528. * Sets width to the header cell. This width should not include any
  2529. * possible indent modifications that are present in
  2530. * {@link VScrollTableBody#getMaxIndent()}.
  2531. *
  2532. * @param w
  2533. * required width of the cell sans indentations
  2534. * @param ensureDefinedWidth
  2535. * disables expand ratio if required
  2536. */
  2537. public void setWidth(int w, boolean ensureDefinedWidth) {
  2538. if (ensureDefinedWidth) {
  2539. definedWidth = true;
  2540. // on column resize expand ratio becomes zero
  2541. expandRatio = 0;
  2542. }
  2543. if (width == -1) {
  2544. // go to default mode, clip content if necessary
  2545. captionContainer.getStyle().clearOverflow();
  2546. }
  2547. width = w;
  2548. if (w == -1) {
  2549. captionContainer.getStyle().clearWidth();
  2550. setWidth("");
  2551. } else {
  2552. tHead.resizeCaptionContainer(this);
  2553. /*
  2554. * if we already have tBody, set the header width properly, if
  2555. * not defer it. IE will fail with complex float in table header
  2556. * unless TD width is not explicitly set.
  2557. */
  2558. if (scrollBody != null) {
  2559. int maxIndent = scrollBody.getMaxIndent();
  2560. if (w < maxIndent && isHierarchyColumn()) {
  2561. w = maxIndent;
  2562. }
  2563. int tdWidth = w + scrollBody.getCellExtraWidth();
  2564. setWidth(tdWidth + "px");
  2565. } else {
  2566. Scheduler.get().scheduleDeferred(new Command() {
  2567. @Override
  2568. public void execute() {
  2569. int maxIndent = scrollBody.getMaxIndent();
  2570. int tdWidth = width;
  2571. if (tdWidth < maxIndent && isHierarchyColumn()) {
  2572. tdWidth = maxIndent;
  2573. }
  2574. tdWidth += scrollBody.getCellExtraWidth();
  2575. setWidth(tdWidth + "px");
  2576. }
  2577. });
  2578. }
  2579. }
  2580. }
  2581. public void setUndefinedWidth() {
  2582. definedWidth = false;
  2583. if (!isResizing) {
  2584. setWidth(-1, false);
  2585. }
  2586. }
  2587. private void setUndefinedWidthFlagOnly() {
  2588. definedWidth = false;
  2589. }
  2590. /**
  2591. * Detects if width is fixed by developer on server side or resized to
  2592. * current width by user.
  2593. *
  2594. * @return true if defined, false if "natural" width
  2595. */
  2596. public boolean isDefinedWidth() {
  2597. return definedWidth && width >= 0;
  2598. }
  2599. /**
  2600. * This method exists for the needs of {@link VTreeTable} only.
  2601. *
  2602. * Returns the pixels width of the header cell. This includes the
  2603. * indent, if applicable.
  2604. *
  2605. * @return The width in pixels
  2606. */
  2607. protected int getWidthWithIndent() {
  2608. if (scrollBody != null && isHierarchyColumn()) {
  2609. int maxIndent = scrollBody.getMaxIndent();
  2610. if (maxIndent > width) {
  2611. return maxIndent;
  2612. }
  2613. }
  2614. return width;
  2615. }
  2616. /**
  2617. * Returns the pixels width of the header cell.
  2618. *
  2619. * @return The width in pixels
  2620. */
  2621. public int getWidth() {
  2622. return width;
  2623. }
  2624. /**
  2625. * This method exists for the needs of {@link VTreeTable} only.
  2626. *
  2627. * @return <code>true</code> if this is hierarcyColumn's header cell,
  2628. * <code>false</code> otherwise
  2629. */
  2630. private boolean isHierarchyColumn() {
  2631. int hierarchyColumnIndex = getHierarchyColumnIndex();
  2632. return hierarchyColumnIndex >= 0
  2633. && tHead.visibleCells.indexOf(this) == hierarchyColumnIndex;
  2634. }
  2635. public void setText(String headerText) {
  2636. DOM.setInnerHTML(captionContainer, headerText);
  2637. }
  2638. public String getColKey() {
  2639. return cid;
  2640. }
  2641. private void setSorted(boolean sorted) {
  2642. this.sorted = sorted;
  2643. updateStyleNames(VScrollTable.this.getStylePrimaryName());
  2644. }
  2645. /**
  2646. * Handle column reordering.
  2647. */
  2648. @Override
  2649. public void onBrowserEvent(Event event) {
  2650. if (enabled && event != null) {
  2651. if (isResizing
  2652. || event.getEventTarget().cast() == colResizeWidget) {
  2653. if (dragging
  2654. && (event.getTypeInt() == Event.ONMOUSEUP || event
  2655. .getTypeInt() == Event.ONTOUCHEND)) {
  2656. // Handle releasing column header on spacer #5318
  2657. handleCaptionEvent(event);
  2658. } else {
  2659. onResizeEvent(event);
  2660. }
  2661. } else {
  2662. /*
  2663. * Ensure focus before handling caption event. Otherwise
  2664. * variables changed from caption event may be before
  2665. * variables from other components that fire variables when
  2666. * they lose focus.
  2667. */
  2668. if (event.getTypeInt() == Event.ONMOUSEDOWN
  2669. || event.getTypeInt() == Event.ONTOUCHSTART) {
  2670. scrollBodyPanel.setFocus(true);
  2671. }
  2672. handleCaptionEvent(event);
  2673. boolean stopPropagation = true;
  2674. if (event.getTypeInt() == Event.ONCONTEXTMENU
  2675. && !client.hasEventListeners(VScrollTable.this,
  2676. TableConstants.HEADER_CLICK_EVENT_ID)) {
  2677. // Prevent showing the browser's context menu only when
  2678. // there is a header click listener.
  2679. stopPropagation = false;
  2680. }
  2681. if (stopPropagation) {
  2682. event.stopPropagation();
  2683. event.preventDefault();
  2684. }
  2685. }
  2686. }
  2687. }
  2688. private void createFloatingCopy() {
  2689. floatingCopyOfHeaderCell = DOM.createDiv();
  2690. DOM.setInnerHTML(floatingCopyOfHeaderCell, DOM.getInnerHTML(td));
  2691. floatingCopyOfHeaderCell = DOM
  2692. .getChild(floatingCopyOfHeaderCell, 2);
  2693. // #12714 the shown "ghost element" should be inside
  2694. // v-overlay-container, and it should contain the same styles as the
  2695. // table to enable theming (except v-table & v-widget).
  2696. String stylePrimaryName = VScrollTable.this.getStylePrimaryName();
  2697. StringBuilder sb = new StringBuilder();
  2698. for (String s : VScrollTable.this.getStyleName().split(" ")) {
  2699. if (!s.equals(StyleConstants.UI_WIDGET)) {
  2700. sb.append(s);
  2701. if (s.equals(stylePrimaryName)) {
  2702. sb.append("-header-drag ");
  2703. } else {
  2704. sb.append(" ");
  2705. }
  2706. }
  2707. }
  2708. floatingCopyOfHeaderCell.setClassName(sb.toString().trim());
  2709. // otherwise might wrap or be cut if narrow column
  2710. floatingCopyOfHeaderCell.getStyle().setProperty("width", "auto");
  2711. updateFloatingCopysPosition(DOM.getAbsoluteLeft(td),
  2712. DOM.getAbsoluteTop(td));
  2713. DOM.appendChild(VOverlay.getOverlayContainer(client),
  2714. floatingCopyOfHeaderCell);
  2715. }
  2716. private void updateFloatingCopysPosition(int x, int y) {
  2717. x -= DOM.getElementPropertyInt(floatingCopyOfHeaderCell,
  2718. "offsetWidth") / 2;
  2719. floatingCopyOfHeaderCell.getStyle().setLeft(x, Unit.PX);
  2720. if (y > 0) {
  2721. floatingCopyOfHeaderCell.getStyle().setTop(y + 7, Unit.PX);
  2722. }
  2723. }
  2724. private void hideFloatingCopy() {
  2725. floatingCopyOfHeaderCell.removeFromParent();
  2726. floatingCopyOfHeaderCell = null;
  2727. }
  2728. /**
  2729. * Fires a header click event after the user has clicked a column header
  2730. * cell
  2731. *
  2732. * @param event
  2733. * The click event
  2734. */
  2735. private void fireHeaderClickedEvent(Event event) {
  2736. if (client.hasEventListeners(VScrollTable.this,
  2737. TableConstants.HEADER_CLICK_EVENT_ID)) {
  2738. MouseEventDetails details = MouseEventDetailsBuilder
  2739. .buildMouseEventDetails(event);
  2740. client.updateVariable(paintableId, "headerClickEvent",
  2741. details.toString(), false);
  2742. client.updateVariable(paintableId, "headerClickCID", cid, true);
  2743. }
  2744. }
  2745. protected void handleCaptionEvent(Event event) {
  2746. switch (DOM.eventGetType(event)) {
  2747. case Event.ONTOUCHSTART:
  2748. case Event.ONMOUSEDOWN:
  2749. if (columnReordering
  2750. && WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  2751. if (event.getTypeInt() == Event.ONTOUCHSTART) {
  2752. /*
  2753. * prevent using this event in e.g. scrolling
  2754. */
  2755. event.stopPropagation();
  2756. }
  2757. dragging = true;
  2758. currentDragX = WidgetUtil.getTouchOrMouseClientX(event);
  2759. moved = false;
  2760. colIndex = getColIndexByKey(cid);
  2761. DOM.setCapture(getElement());
  2762. headerX = tHead.getAbsoluteLeft();
  2763. event.preventDefault(); // prevent selecting text &&
  2764. // generated touch events
  2765. }
  2766. break;
  2767. case Event.ONMOUSEUP:
  2768. case Event.ONTOUCHEND:
  2769. case Event.ONTOUCHCANCEL:
  2770. if (columnReordering
  2771. && WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  2772. dragging = false;
  2773. currentDragX = null;
  2774. DOM.releaseCapture(getElement());
  2775. if (WidgetUtil.isTouchEvent(event)) {
  2776. /*
  2777. * Prevent using in e.g. scrolling and prevent generated
  2778. * events.
  2779. */
  2780. event.preventDefault();
  2781. event.stopPropagation();
  2782. }
  2783. if (moved) {
  2784. hideFloatingCopy();
  2785. tHead.removeSlotFocus();
  2786. if (closestSlot != colIndex
  2787. && closestSlot != (colIndex + 1)) {
  2788. if (closestSlot > colIndex) {
  2789. reOrderColumn(cid, closestSlot - 1);
  2790. } else {
  2791. reOrderColumn(cid, closestSlot);
  2792. }
  2793. }
  2794. moved = false;
  2795. break;
  2796. }
  2797. }
  2798. if (!moved) {
  2799. // mouse event was a click to header -> sort column
  2800. if (sortable
  2801. && WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  2802. if (sortColumn.equals(cid)) {
  2803. // just toggle order
  2804. client.updateVariable(paintableId, "sortascending",
  2805. !sortAscending, false);
  2806. } else {
  2807. // set table sorted by this column
  2808. client.updateVariable(paintableId, "sortcolumn",
  2809. cid, false);
  2810. }
  2811. // get also cache columns at the same request
  2812. scrollBodyPanel.setScrollPosition(0);
  2813. firstvisible = 0;
  2814. rowRequestHandler.setReqFirstRow(0);
  2815. rowRequestHandler.setReqRows((int) (2 * pageLength
  2816. * cache_rate + pageLength));
  2817. rowRequestHandler.deferRowFetch(); // some validation +
  2818. // defer 250ms
  2819. rowRequestHandler.cancel(); // instead of waiting
  2820. rowRequestHandler.run(); // run immediately
  2821. }
  2822. fireHeaderClickedEvent(event);
  2823. if (WidgetUtil.isTouchEvent(event)) {
  2824. /*
  2825. * Prevent using in e.g. scrolling and prevent generated
  2826. * events.
  2827. */
  2828. event.preventDefault();
  2829. event.stopPropagation();
  2830. }
  2831. break;
  2832. }
  2833. break;
  2834. case Event.ONDBLCLICK:
  2835. fireHeaderClickedEvent(event);
  2836. break;
  2837. case Event.ONTOUCHMOVE:
  2838. case Event.ONMOUSEMOVE:
  2839. // only start the drag if the mouse / touch has moved a minimum
  2840. // distance in x-axis (the same idea as in #13381)
  2841. int currentX = WidgetUtil.getTouchOrMouseClientX(event);
  2842. if (currentDragX == null
  2843. || Math.abs(currentDragX - currentX) > VDragAndDropManager.MINIMUM_DISTANCE_TO_START_DRAG) {
  2844. if (dragging
  2845. && WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  2846. if (event.getTypeInt() == Event.ONTOUCHMOVE) {
  2847. /*
  2848. * prevent using this event in e.g. scrolling
  2849. */
  2850. event.stopPropagation();
  2851. }
  2852. if (!moved) {
  2853. createFloatingCopy();
  2854. moved = true;
  2855. }
  2856. final int clientX = WidgetUtil
  2857. .getTouchOrMouseClientX(event);
  2858. final int x = clientX
  2859. + tHead.hTableWrapper.getScrollLeft();
  2860. int slotX = headerX;
  2861. closestSlot = colIndex;
  2862. int closestDistance = -1;
  2863. int start = 0;
  2864. if (showRowHeaders) {
  2865. start++;
  2866. }
  2867. final int visibleCellCount = tHead
  2868. .getVisibleCellCount();
  2869. for (int i = start; i <= visibleCellCount; i++) {
  2870. if (i > 0) {
  2871. final String colKey = getColKeyByIndex(i - 1);
  2872. // getColWidth only returns the internal width
  2873. // without padding, not the offset width of the
  2874. // whole td (#10890)
  2875. slotX += getColWidth(colKey)
  2876. + scrollBody.getCellExtraWidth();
  2877. }
  2878. final int dist = Math.abs(x - slotX);
  2879. if (closestDistance == -1 || dist < closestDistance) {
  2880. closestDistance = dist;
  2881. closestSlot = i;
  2882. }
  2883. }
  2884. tHead.focusSlot(closestSlot);
  2885. updateFloatingCopysPosition(clientX, -1);
  2886. }
  2887. }
  2888. break;
  2889. default:
  2890. break;
  2891. }
  2892. }
  2893. private void onResizeEvent(Event event) {
  2894. switch (DOM.eventGetType(event)) {
  2895. case Event.ONMOUSEDOWN:
  2896. if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  2897. return;
  2898. }
  2899. isResizing = true;
  2900. DOM.setCapture(getElement());
  2901. dragStartX = DOM.eventGetClientX(event);
  2902. colIndex = getColIndexByKey(cid);
  2903. originalWidth = getWidthWithIndent();
  2904. DOM.eventPreventDefault(event);
  2905. break;
  2906. case Event.ONMOUSEUP:
  2907. if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  2908. return;
  2909. }
  2910. isResizing = false;
  2911. DOM.releaseCapture(getElement());
  2912. tHead.disableAutoColumnWidthCalculation(this);
  2913. // Ensure last header cell is taking into account possible
  2914. // column selector
  2915. HeaderCell lastCell = tHead.getHeaderCell(tHead
  2916. .getVisibleCellCount() - 1);
  2917. tHead.resizeCaptionContainer(lastCell);
  2918. triggerLazyColumnAdjustment(true);
  2919. fireColumnResizeEvent(cid, originalWidth, getColWidth(cid));
  2920. break;
  2921. case Event.ONMOUSEMOVE:
  2922. if (!WidgetUtil.isTouchEventOrLeftMouseButton(event)) {
  2923. return;
  2924. }
  2925. if (isResizing) {
  2926. final int deltaX = DOM.eventGetClientX(event) - dragStartX;
  2927. if (deltaX == 0) {
  2928. return;
  2929. }
  2930. tHead.disableAutoColumnWidthCalculation(this);
  2931. int newWidth = originalWidth + deltaX;
  2932. // get min width with indent, no padding
  2933. int minWidth = getMinWidth(true, false);
  2934. if (newWidth < minWidth) {
  2935. // already includes indent if any
  2936. newWidth = minWidth;
  2937. }
  2938. setColWidth(colIndex, newWidth, true);
  2939. triggerLazyColumnAdjustment(false);
  2940. forceRealignColumnHeaders();
  2941. }
  2942. break;
  2943. default:
  2944. break;
  2945. }
  2946. }
  2947. /**
  2948. * Returns the smallest possible cell width in pixels.
  2949. *
  2950. * @param includeIndent
  2951. * - width should include hierarchy column indent if
  2952. * applicable (VTreeTable only)
  2953. * @param includeCellExtraWidth
  2954. * - width should include paddings etc.
  2955. * @return
  2956. */
  2957. private int getMinWidth(boolean includeIndent,
  2958. boolean includeCellExtraWidth) {
  2959. int minWidth = sortIndicator.getOffsetWidth();
  2960. if (scrollBody != null) {
  2961. // check the need for indent before adding paddings etc.
  2962. if (includeIndent && isHierarchyColumn()) {
  2963. int maxIndent = scrollBody.getMaxIndent();
  2964. if (minWidth < maxIndent) {
  2965. minWidth = maxIndent;
  2966. }
  2967. }
  2968. if (includeCellExtraWidth) {
  2969. minWidth += scrollBody.getCellExtraWidth();
  2970. }
  2971. }
  2972. return minWidth;
  2973. }
  2974. public int getMinWidth() {
  2975. // get min width with padding, no indent
  2976. return getMinWidth(false, true);
  2977. }
  2978. public String getCaption() {
  2979. return DOM.getInnerText(captionContainer);
  2980. }
  2981. public boolean isEnabled() {
  2982. return getParent() != null;
  2983. }
  2984. public void setAlign(char c) {
  2985. align = c;
  2986. updateStyleNames(VScrollTable.this.getStylePrimaryName());
  2987. }
  2988. public char getAlign() {
  2989. return align;
  2990. }
  2991. /**
  2992. * Saves natural column width if it hasn't been saved already.
  2993. *
  2994. * @param columnIndex
  2995. * @since 7.3.9
  2996. */
  2997. protected void saveNaturalColumnWidthIfNotSaved(int columnIndex) {
  2998. if (naturalWidth < 0) {
  2999. // This is recently revealed column. Try to detect a proper
  3000. // value (greater of header and data columns)
  3001. int hw = captionContainer.getOffsetWidth() + getHeaderPadding();
  3002. if (BrowserInfo.get().isGecko()) {
  3003. hw += sortIndicator.getOffsetWidth();
  3004. }
  3005. if (columnIndex < 0) {
  3006. columnIndex = 0;
  3007. for (Iterator<Widget> it = tHead.iterator(); it.hasNext(); columnIndex++) {
  3008. if (it.next() == this) {
  3009. break;
  3010. }
  3011. }
  3012. }
  3013. final int cw = scrollBody.getColWidth(columnIndex);
  3014. naturalWidth = (hw > cw ? hw : cw);
  3015. }
  3016. }
  3017. /**
  3018. * Detects the natural minimum width for the column of this header cell.
  3019. * If column is resized by user or the width is defined by server the
  3020. * actual width is returned. Else the natural min width is returned.
  3021. *
  3022. * @param columnIndex
  3023. * column index hint, if -1 (unknown) it will be detected
  3024. *
  3025. * @return
  3026. */
  3027. public int getNaturalColumnWidth(int columnIndex) {
  3028. final int iw = columnIndex == getHierarchyColumnIndex() ? scrollBody
  3029. .getMaxIndent() : 0;
  3030. saveNaturalColumnWidthIfNotSaved(columnIndex);
  3031. if (isDefinedWidth()) {
  3032. if (iw > width) {
  3033. return iw;
  3034. }
  3035. return width;
  3036. } else {
  3037. if (iw > naturalWidth) {
  3038. // indent is temporary value, naturalWidth shouldn't be
  3039. // updated
  3040. return iw;
  3041. } else {
  3042. return naturalWidth;
  3043. }
  3044. }
  3045. }
  3046. public void setExpandRatio(float floatAttribute) {
  3047. if (floatAttribute != expandRatio) {
  3048. triggerLazyColumnAdjustment(false);
  3049. }
  3050. expandRatio = floatAttribute;
  3051. }
  3052. public float getExpandRatio() {
  3053. return expandRatio;
  3054. }
  3055. public boolean isSorted() {
  3056. return sorted;
  3057. }
  3058. }
  3059. /**
  3060. * HeaderCell that is header cell for row headers.
  3061. *
  3062. * Reordering disabled and clicking on it resets sorting.
  3063. */
  3064. public class RowHeadersHeaderCell extends HeaderCell {
  3065. RowHeadersHeaderCell() {
  3066. super(ROW_HEADER_COLUMN_KEY, "");
  3067. updateStyleNames(VScrollTable.this.getStylePrimaryName());
  3068. }
  3069. @Override
  3070. protected void updateStyleNames(String primaryStyleName) {
  3071. super.updateStyleNames(primaryStyleName);
  3072. setStyleName(primaryStyleName + "-header-cell-rowheader");
  3073. }
  3074. @Override
  3075. protected void handleCaptionEvent(Event event) {
  3076. // NOP: RowHeaders cannot be reordered
  3077. // TODO It'd be nice to reset sorting here
  3078. }
  3079. }
  3080. public class TableHead extends Panel implements ActionOwner {
  3081. private static final int WRAPPER_WIDTH = 900000;
  3082. ArrayList<Widget> visibleCells = new ArrayList<Widget>();
  3083. HashMap<String, HeaderCell> availableCells = new HashMap<String, HeaderCell>();
  3084. Element div = DOM.createDiv();
  3085. Element hTableWrapper = DOM.createDiv();
  3086. Element hTableContainer = DOM.createDiv();
  3087. Element table = DOM.createTable();
  3088. Element headerTableBody = DOM.createTBody();
  3089. Element tr = DOM.createTR();
  3090. private final Element columnSelector = DOM.createDiv();
  3091. private int focusedSlot = -1;
  3092. public TableHead() {
  3093. if (BrowserInfo.get().isIE()) {
  3094. table.setPropertyInt("cellSpacing", 0);
  3095. }
  3096. hTableWrapper.getStyle().setOverflow(Overflow.HIDDEN);
  3097. columnSelector.getStyle().setDisplay(Display.NONE);
  3098. DOM.appendChild(table, headerTableBody);
  3099. DOM.appendChild(headerTableBody, tr);
  3100. DOM.appendChild(hTableContainer, table);
  3101. DOM.appendChild(hTableWrapper, hTableContainer);
  3102. DOM.appendChild(div, hTableWrapper);
  3103. DOM.appendChild(div, columnSelector);
  3104. setElement(div);
  3105. DOM.sinkEvents(columnSelector, Event.ONCLICK);
  3106. availableCells.put(ROW_HEADER_COLUMN_KEY,
  3107. new RowHeadersHeaderCell());
  3108. }
  3109. protected void updateStyleNames(String primaryStyleName) {
  3110. hTableWrapper.setClassName(primaryStyleName + "-header");
  3111. columnSelector.setClassName(primaryStyleName + "-column-selector");
  3112. setStyleName(primaryStyleName + "-header-wrap");
  3113. for (HeaderCell c : availableCells.values()) {
  3114. c.updateStyleNames(primaryStyleName);
  3115. }
  3116. }
  3117. public void resizeCaptionContainer(HeaderCell cell) {
  3118. HeaderCell lastcell = getHeaderCell(visibleCells.size() - 1);
  3119. int columnSelectorOffset = columnSelector.getOffsetWidth();
  3120. if (cell == lastcell && columnSelectorOffset > 0
  3121. && !hasVerticalScrollbar()) {
  3122. // Measure column widths
  3123. int columnTotalWidth = 0;
  3124. for (Widget w : visibleCells) {
  3125. int cellExtraWidth = w.getOffsetWidth();
  3126. if (scrollBody != null
  3127. && visibleCells.indexOf(w) == getHierarchyColumnIndex()
  3128. && cellExtraWidth < scrollBody.getMaxIndent()) {
  3129. // indent must be taken into consideration even if it
  3130. // hasn't been applied yet
  3131. columnTotalWidth += scrollBody.getMaxIndent();
  3132. } else {
  3133. columnTotalWidth += cellExtraWidth;
  3134. }
  3135. }
  3136. int divOffset = div.getOffsetWidth();
  3137. if (columnTotalWidth >= divOffset - columnSelectorOffset) {
  3138. /*
  3139. * Ensure column caption is visible when placed under the
  3140. * column selector widget by shifting and resizing the
  3141. * caption.
  3142. */
  3143. int offset = 0;
  3144. int diff = divOffset - columnTotalWidth;
  3145. if (diff < columnSelectorOffset && diff > 0) {
  3146. /*
  3147. * If the difference is less than the column selectors
  3148. * width then just offset by the difference
  3149. */
  3150. offset = columnSelectorOffset - diff;
  3151. } else {
  3152. // Else offset by the whole column selector
  3153. offset = columnSelectorOffset;
  3154. }
  3155. lastcell.resizeCaptionContainer(offset);
  3156. } else {
  3157. cell.resizeCaptionContainer(0);
  3158. }
  3159. } else {
  3160. cell.resizeCaptionContainer(0);
  3161. }
  3162. }
  3163. @Override
  3164. public void clear() {
  3165. for (String cid : availableCells.keySet()) {
  3166. removeCell(cid);
  3167. }
  3168. availableCells.clear();
  3169. availableCells.put(ROW_HEADER_COLUMN_KEY,
  3170. new RowHeadersHeaderCell());
  3171. }
  3172. public void updateCellsFromUIDL(UIDL uidl) {
  3173. Iterator<?> it = uidl.getChildIterator();
  3174. HashSet<String> updated = new HashSet<String>();
  3175. boolean refreshContentWidths = initializedAndAttached
  3176. && hadScrollBars != willHaveScrollbars();
  3177. while (it.hasNext()) {
  3178. final UIDL col = (UIDL) it.next();
  3179. final String cid = col.getStringAttribute("cid");
  3180. updated.add(cid);
  3181. String caption = buildCaptionHtmlSnippet(col);
  3182. HeaderCell c = getHeaderCell(cid);
  3183. if (c == null) {
  3184. c = new HeaderCell(cid, caption);
  3185. availableCells.put(cid, c);
  3186. if (initializedAndAttached) {
  3187. // we will need a column width recalculation
  3188. initializedAndAttached = false;
  3189. initialContentReceived = false;
  3190. isNewBody = true;
  3191. }
  3192. } else {
  3193. c.setText(caption);
  3194. if (BrowserInfo.get().isIE10()) {
  3195. // IE10 can some times define min-height to include
  3196. // padding when setting the text...
  3197. // See https://dev.vaadin.com/ticket/15169
  3198. WidgetUtil.forceIERedraw(c.getElement());
  3199. }
  3200. }
  3201. c.setSorted(false);
  3202. if (col.hasAttribute("sortable")) {
  3203. c.setSortable(true);
  3204. } else {
  3205. c.setSortable(false);
  3206. }
  3207. if (col.hasAttribute("align")) {
  3208. c.setAlign(col.getStringAttribute("align").charAt(0));
  3209. } else {
  3210. c.setAlign(ALIGN_LEFT);
  3211. }
  3212. if (col.hasAttribute("width") && !c.isResizing) {
  3213. // Make sure to accomodate for the sort indicator if
  3214. // necessary.
  3215. int width = col.getIntAttribute("width");
  3216. int widthWithoutAddedIndent = width;
  3217. // get min width with indent, no padding
  3218. int minWidth = c.getMinWidth(true, false);
  3219. if (width < minWidth) {
  3220. width = minWidth;
  3221. }
  3222. if (scrollBody != null && width != c.getWidthWithIndent()) {
  3223. // Do a more thorough update if a column is resized from
  3224. // the server *after* the header has been properly
  3225. // initialized
  3226. final int newWidth = width;
  3227. Scheduler.get().scheduleFinally(new ScheduledCommand() {
  3228. @Override
  3229. public void execute() {
  3230. final int colIx = getColIndexByKey(cid);
  3231. setColWidth(colIx, newWidth, true);
  3232. }
  3233. });
  3234. refreshContentWidths = true;
  3235. } else {
  3236. // get min width with no indent or padding
  3237. minWidth = c.getMinWidth(false, false);
  3238. if (widthWithoutAddedIndent < minWidth) {
  3239. widthWithoutAddedIndent = minWidth;
  3240. }
  3241. // save min width without indent
  3242. c.setWidth(widthWithoutAddedIndent, true);
  3243. }
  3244. } else if (col.hasAttribute("er")) {
  3245. c.setExpandRatio(col.getFloatAttribute("er"));
  3246. c.setUndefinedWidthFlagOnly();
  3247. } else if (recalcWidths) {
  3248. c.setUndefinedWidth();
  3249. } else {
  3250. boolean hadExpandRatio = c.getExpandRatio() > 0;
  3251. boolean hadDefinedWidth = c.isDefinedWidth();
  3252. if (hadExpandRatio || hadDefinedWidth) {
  3253. // Someone has removed a expand width or the defined
  3254. // width on the server side (setting it to -1), make the
  3255. // column undefined again and measure columns again.
  3256. c.setUndefinedWidth();
  3257. c.setExpandRatio(0);
  3258. refreshContentWidths = true;
  3259. }
  3260. }
  3261. if (col.hasAttribute("collapsed")) {
  3262. // ensure header is properly removed from parent (case when
  3263. // collapsing happens via servers side api)
  3264. if (c.isAttached()) {
  3265. c.removeFromParent();
  3266. headerChangedDuringUpdate = true;
  3267. }
  3268. }
  3269. }
  3270. if (refreshContentWidths) {
  3271. // Recalculate the column sizings if any column has changed
  3272. Scheduler.get().scheduleFinally(new ScheduledCommand() {
  3273. @Override
  3274. public void execute() {
  3275. triggerLazyColumnAdjustment(true);
  3276. }
  3277. });
  3278. }
  3279. // check for orphaned header cells
  3280. for (Iterator<String> cit = availableCells.keySet().iterator(); cit
  3281. .hasNext();) {
  3282. String cid = cit.next();
  3283. if (!updated.contains(cid)) {
  3284. removeCell(cid);
  3285. cit.remove();
  3286. // we will need a column width recalculation, since columns
  3287. // with expand ratios should expand to fill the void.
  3288. initializedAndAttached = false;
  3289. initialContentReceived = false;
  3290. isNewBody = true;
  3291. }
  3292. }
  3293. }
  3294. public void enableColumn(String cid, int index) {
  3295. final HeaderCell c = getHeaderCell(cid);
  3296. if (!c.isEnabled() || getHeaderCell(index) != c) {
  3297. setHeaderCell(index, c);
  3298. if (initializedAndAttached) {
  3299. headerChangedDuringUpdate = true;
  3300. }
  3301. }
  3302. }
  3303. public int getVisibleCellCount() {
  3304. return visibleCells.size();
  3305. }
  3306. public void setHorizontalScrollPosition(int scrollLeft) {
  3307. hTableWrapper.setScrollLeft(scrollLeft);
  3308. }
  3309. public void setColumnCollapsingAllowed(boolean cc) {
  3310. if (cc) {
  3311. columnSelector.getStyle().setDisplay(Display.BLOCK);
  3312. } else {
  3313. columnSelector.getStyle().setDisplay(Display.NONE);
  3314. }
  3315. }
  3316. public void disableBrowserIntelligence() {
  3317. hTableContainer.getStyle().setWidth(WRAPPER_WIDTH, Unit.PX);
  3318. }
  3319. public void enableBrowserIntelligence() {
  3320. hTableContainer.getStyle().clearWidth();
  3321. }
  3322. public void setHeaderCell(int index, HeaderCell cell) {
  3323. if (cell.isEnabled()) {
  3324. // we're moving the cell
  3325. DOM.removeChild(tr, cell.getElement());
  3326. orphan(cell);
  3327. visibleCells.remove(cell);
  3328. }
  3329. if (index < visibleCells.size()) {
  3330. // insert to right slot
  3331. DOM.insertChild(tr, cell.getElement(), index);
  3332. adopt(cell);
  3333. visibleCells.add(index, cell);
  3334. } else if (index == visibleCells.size()) {
  3335. // simply append
  3336. DOM.appendChild(tr, cell.getElement());
  3337. adopt(cell);
  3338. visibleCells.add(cell);
  3339. } else {
  3340. throw new RuntimeException(
  3341. "Header cells must be appended in order");
  3342. }
  3343. }
  3344. public HeaderCell getHeaderCell(int index) {
  3345. if (index >= 0 && index < visibleCells.size()) {
  3346. return (HeaderCell) visibleCells.get(index);
  3347. } else {
  3348. return null;
  3349. }
  3350. }
  3351. /**
  3352. * Get's HeaderCell by it's column Key.
  3353. *
  3354. * Note that this returns HeaderCell even if it is currently collapsed.
  3355. *
  3356. * @param cid
  3357. * Column key of accessed HeaderCell
  3358. * @return HeaderCell
  3359. */
  3360. public HeaderCell getHeaderCell(String cid) {
  3361. return availableCells.get(cid);
  3362. }
  3363. public void moveCell(int oldIndex, int newIndex) {
  3364. final HeaderCell hCell = getHeaderCell(oldIndex);
  3365. final Element cell = hCell.getElement();
  3366. visibleCells.remove(oldIndex);
  3367. DOM.removeChild(tr, cell);
  3368. DOM.insertChild(tr, cell, newIndex);
  3369. visibleCells.add(newIndex, hCell);
  3370. }
  3371. @Override
  3372. public Iterator<Widget> iterator() {
  3373. return visibleCells.iterator();
  3374. }
  3375. @Override
  3376. public boolean remove(Widget w) {
  3377. if (visibleCells.contains(w)) {
  3378. visibleCells.remove(w);
  3379. orphan(w);
  3380. DOM.removeChild(DOM.getParent(w.getElement()), w.getElement());
  3381. return true;
  3382. }
  3383. return false;
  3384. }
  3385. public void removeCell(String colKey) {
  3386. final HeaderCell c = getHeaderCell(colKey);
  3387. remove(c);
  3388. }
  3389. private void focusSlot(int index) {
  3390. removeSlotFocus();
  3391. if (index > 0) {
  3392. Element child = tr.getChild(index - 1).getFirstChild().cast();
  3393. child.setClassName(VScrollTable.this.getStylePrimaryName()
  3394. + "-resizer");
  3395. child.addClassName(VScrollTable.this.getStylePrimaryName()
  3396. + "-focus-slot-right");
  3397. } else {
  3398. Element child = tr.getChild(index).getFirstChild().cast();
  3399. child.setClassName(VScrollTable.this.getStylePrimaryName()
  3400. + "-resizer");
  3401. child.addClassName(VScrollTable.this.getStylePrimaryName()
  3402. + "-focus-slot-left");
  3403. }
  3404. focusedSlot = index;
  3405. }
  3406. private void removeSlotFocus() {
  3407. if (focusedSlot < 0) {
  3408. return;
  3409. }
  3410. if (focusedSlot == 0) {
  3411. Element child = tr.getChild(focusedSlot).getFirstChild().cast();
  3412. child.setClassName(VScrollTable.this.getStylePrimaryName()
  3413. + "-resizer");
  3414. } else if (focusedSlot > 0) {
  3415. Element child = tr.getChild(focusedSlot - 1).getFirstChild()
  3416. .cast();
  3417. child.setClassName(VScrollTable.this.getStylePrimaryName()
  3418. + "-resizer");
  3419. }
  3420. focusedSlot = -1;
  3421. }
  3422. @Override
  3423. public void onBrowserEvent(Event event) {
  3424. if (enabled) {
  3425. if (event.getEventTarget().cast() == columnSelector) {
  3426. final int left = DOM.getAbsoluteLeft(columnSelector);
  3427. final int top = DOM.getAbsoluteTop(columnSelector)
  3428. + DOM.getElementPropertyInt(columnSelector,
  3429. "offsetHeight");
  3430. client.getContextMenu().showAt(this, left, top);
  3431. }
  3432. }
  3433. }
  3434. @Override
  3435. protected void onDetach() {
  3436. super.onDetach();
  3437. if (client != null) {
  3438. client.getContextMenu().ensureHidden(this);
  3439. }
  3440. }
  3441. class VisibleColumnAction extends Action {
  3442. String colKey;
  3443. private boolean collapsed;
  3444. private boolean noncollapsible = false;
  3445. private VScrollTableRow currentlyFocusedRow;
  3446. public VisibleColumnAction(String colKey) {
  3447. super(VScrollTable.TableHead.this);
  3448. this.colKey = colKey;
  3449. caption = tHead.getHeaderCell(colKey).getCaption();
  3450. currentlyFocusedRow = focusedRow;
  3451. }
  3452. @Override
  3453. public void execute() {
  3454. if (noncollapsible) {
  3455. return;
  3456. }
  3457. client.getContextMenu().hide();
  3458. // toggle selected column
  3459. if (collapsedColumns.contains(colKey)) {
  3460. collapsedColumns.remove(colKey);
  3461. } else {
  3462. tHead.removeCell(colKey);
  3463. collapsedColumns.add(colKey);
  3464. triggerLazyColumnAdjustment(true);
  3465. }
  3466. // update variable to server
  3467. client.updateVariable(paintableId, "collapsedcolumns",
  3468. collapsedColumns.toArray(new String[collapsedColumns
  3469. .size()]), false);
  3470. // let rowRequestHandler determine proper rows
  3471. rowRequestHandler.refreshContent();
  3472. lazyRevertFocusToRow(currentlyFocusedRow);
  3473. }
  3474. public void setCollapsed(boolean b) {
  3475. collapsed = b;
  3476. }
  3477. public void setNoncollapsible(boolean b) {
  3478. noncollapsible = b;
  3479. }
  3480. /**
  3481. * Override default method to distinguish on/off columns
  3482. */
  3483. @Override
  3484. public String getHTML() {
  3485. final StringBuffer buf = new StringBuffer();
  3486. buf.append("<span class=\"");
  3487. if (collapsed) {
  3488. buf.append("v-off");
  3489. } else {
  3490. buf.append("v-on");
  3491. }
  3492. if (noncollapsible) {
  3493. buf.append(" v-disabled");
  3494. }
  3495. buf.append("\">");
  3496. buf.append(super.getHTML());
  3497. buf.append("</span>");
  3498. return buf.toString();
  3499. }
  3500. }
  3501. /*
  3502. * Returns columns as Action array for column select popup
  3503. */
  3504. @Override
  3505. public Action[] getActions() {
  3506. Object[] cols;
  3507. if (columnReordering && columnOrder != null) {
  3508. cols = columnOrder;
  3509. } else {
  3510. // if columnReordering is disabled, we need different way to get
  3511. // all available columns
  3512. cols = visibleColOrder;
  3513. cols = new Object[visibleColOrder.length
  3514. + collapsedColumns.size()];
  3515. int i;
  3516. for (i = 0; i < visibleColOrder.length; i++) {
  3517. cols[i] = visibleColOrder[i];
  3518. }
  3519. for (final Iterator<String> it = collapsedColumns.iterator(); it
  3520. .hasNext();) {
  3521. cols[i++] = it.next();
  3522. }
  3523. }
  3524. final Action[] actions = new Action[cols.length];
  3525. for (int i = 0; i < cols.length; i++) {
  3526. final String cid = (String) cols[i];
  3527. final HeaderCell c = getHeaderCell(cid);
  3528. final VisibleColumnAction a = new VisibleColumnAction(
  3529. c.getColKey());
  3530. a.setCaption(c.getCaption());
  3531. if (!c.isEnabled()) {
  3532. a.setCollapsed(true);
  3533. }
  3534. if (noncollapsibleColumns.contains(cid)) {
  3535. a.setNoncollapsible(true);
  3536. }
  3537. actions[i] = a;
  3538. }
  3539. return actions;
  3540. }
  3541. @Override
  3542. public ApplicationConnection getClient() {
  3543. return client;
  3544. }
  3545. @Override
  3546. public String getPaintableId() {
  3547. return paintableId;
  3548. }
  3549. /**
  3550. * Returns column alignments for visible columns
  3551. */
  3552. public char[] getColumnAlignments() {
  3553. final Iterator<Widget> it = visibleCells.iterator();
  3554. final char[] aligns = new char[visibleCells.size()];
  3555. int colIndex = 0;
  3556. while (it.hasNext()) {
  3557. aligns[colIndex++] = ((HeaderCell) it.next()).getAlign();
  3558. }
  3559. return aligns;
  3560. }
  3561. /**
  3562. * Disables the automatic calculation of all column widths by forcing
  3563. * the widths to be "defined" thus turning off expand ratios and such.
  3564. */
  3565. public void disableAutoColumnWidthCalculation(HeaderCell source) {
  3566. for (HeaderCell cell : availableCells.values()) {
  3567. cell.disableAutoWidthCalculation();
  3568. }
  3569. // fire column resize events for all columns but the source of the
  3570. // resize action, since an event will fire separately for this.
  3571. ArrayList<HeaderCell> columns = new ArrayList<HeaderCell>(
  3572. availableCells.values());
  3573. columns.remove(source);
  3574. sendColumnWidthUpdates(columns);
  3575. forceRealignColumnHeaders();
  3576. }
  3577. }
  3578. /**
  3579. * A cell in the footer
  3580. */
  3581. public class FooterCell extends Widget {
  3582. private final Element td = DOM.createTD();
  3583. private final Element captionContainer = DOM.createDiv();
  3584. private char align = ALIGN_LEFT;
  3585. private int width = -1;
  3586. private float expandRatio = 0;
  3587. private final String cid;
  3588. boolean definedWidth = false;
  3589. private int naturalWidth = -1;
  3590. public FooterCell(String colId, String headerText) {
  3591. cid = colId;
  3592. setText(headerText);
  3593. // ensure no clipping initially (problem on column additions)
  3594. captionContainer.getStyle().setOverflow(Overflow.VISIBLE);
  3595. DOM.sinkEvents(captionContainer, Event.MOUSEEVENTS);
  3596. DOM.appendChild(td, captionContainer);
  3597. DOM.sinkEvents(td, Event.MOUSEEVENTS | Event.ONDBLCLICK
  3598. | Event.ONCONTEXTMENU);
  3599. setElement(td);
  3600. updateStyleNames(VScrollTable.this.getStylePrimaryName());
  3601. }
  3602. protected void updateStyleNames(String primaryStyleName) {
  3603. captionContainer.setClassName(primaryStyleName
  3604. + "-footer-container");
  3605. }
  3606. /**
  3607. * Sets the text of the footer
  3608. *
  3609. * @param footerText
  3610. * The text in the footer
  3611. */
  3612. public void setText(String footerText) {
  3613. if (footerText == null || footerText.equals("")) {
  3614. footerText = "&nbsp;";
  3615. }
  3616. DOM.setInnerHTML(captionContainer, footerText);
  3617. }
  3618. /**
  3619. * Set alignment of the text in the cell
  3620. *
  3621. * @param c
  3622. * The alignment which can be ALIGN_CENTER, ALIGN_LEFT,
  3623. * ALIGN_RIGHT
  3624. */
  3625. public void setAlign(char c) {
  3626. if (align != c) {
  3627. switch (c) {
  3628. case ALIGN_CENTER:
  3629. captionContainer.getStyle().setTextAlign(TextAlign.CENTER);
  3630. break;
  3631. case ALIGN_RIGHT:
  3632. captionContainer.getStyle().setTextAlign(TextAlign.RIGHT);
  3633. break;
  3634. default:
  3635. captionContainer.getStyle().setTextAlign(TextAlign.LEFT);
  3636. break;
  3637. }
  3638. }
  3639. align = c;
  3640. }
  3641. /**
  3642. * Get the alignment of the text int the cell
  3643. *
  3644. * @return Returns either ALIGN_CENTER, ALIGN_LEFT or ALIGN_RIGHT
  3645. */
  3646. public char getAlign() {
  3647. return align;
  3648. }
  3649. /**
  3650. * Sets the width of the cell. This width should not include any
  3651. * possible indent modifications that are present in
  3652. * {@link VScrollTableBody#getMaxIndent()}.
  3653. *
  3654. * @param w
  3655. * The width of the cell
  3656. * @param ensureDefinedWidth
  3657. * Ensures that the given width is not recalculated
  3658. */
  3659. public void setWidth(int w, boolean ensureDefinedWidth) {
  3660. if (ensureDefinedWidth) {
  3661. definedWidth = true;
  3662. // on column resize expand ratio becomes zero
  3663. expandRatio = 0;
  3664. }
  3665. if (width == w) {
  3666. return;
  3667. }
  3668. if (width == -1) {
  3669. // go to default mode, clip content if necessary
  3670. captionContainer.getStyle().clearOverflow();
  3671. }
  3672. width = w;
  3673. if (w == -1) {
  3674. captionContainer.getStyle().clearWidth();
  3675. setWidth("");
  3676. } else {
  3677. /*
  3678. * Reduce width with one pixel for the right border since the
  3679. * footers does not have any spacers between them.
  3680. */
  3681. final int borderWidths = 1;
  3682. // Set the container width (check for negative value)
  3683. captionContainer.getStyle().setPropertyPx("width",
  3684. Math.max(w - borderWidths, 0));
  3685. /*
  3686. * if we already have tBody, set the header width properly, if
  3687. * not defer it. IE will fail with complex float in table header
  3688. * unless TD width is not explicitly set.
  3689. */
  3690. if (scrollBody != null) {
  3691. int maxIndent = scrollBody.getMaxIndent();
  3692. if (w < maxIndent
  3693. && tFoot.visibleCells.indexOf(this) == getHierarchyColumnIndex()) {
  3694. // ensure there's room for the indent
  3695. w = maxIndent;
  3696. }
  3697. int tdWidth = w + scrollBody.getCellExtraWidth()
  3698. - borderWidths;
  3699. setWidth(Math.max(tdWidth, 0) + "px");
  3700. } else {
  3701. Scheduler.get().scheduleDeferred(new Command() {
  3702. @Override
  3703. public void execute() {
  3704. int tdWidth = width;
  3705. int maxIndent = scrollBody.getMaxIndent();
  3706. if (tdWidth < maxIndent
  3707. && tFoot.visibleCells.indexOf(this) == getHierarchyColumnIndex()) {
  3708. // ensure there's room for the indent
  3709. tdWidth = maxIndent;
  3710. }
  3711. tdWidth += scrollBody.getCellExtraWidth()
  3712. - borderWidths;
  3713. setWidth(Math.max(tdWidth, 0) + "px");
  3714. }
  3715. });
  3716. }
  3717. }
  3718. }
  3719. /**
  3720. * Sets the width to undefined
  3721. */
  3722. public void setUndefinedWidth() {
  3723. definedWidth = false;
  3724. setWidth(-1, false);
  3725. }
  3726. /**
  3727. * Detects if width is fixed by developer on server side or resized to
  3728. * current width by user.
  3729. *
  3730. * @return true if defined, false if "natural" width
  3731. */
  3732. public boolean isDefinedWidth() {
  3733. return definedWidth && width >= 0;
  3734. }
  3735. /**
  3736. * Returns the pixels width of the footer cell.
  3737. *
  3738. * @return The width in pixels
  3739. */
  3740. public int getWidth() {
  3741. return width;
  3742. }
  3743. /**
  3744. * Sets the expand ratio of the cell
  3745. *
  3746. * @param floatAttribute
  3747. * The expand ratio
  3748. */
  3749. public void setExpandRatio(float floatAttribute) {
  3750. expandRatio = floatAttribute;
  3751. }
  3752. /**
  3753. * Returns the expand ratio of the cell
  3754. *
  3755. * @return The expand ratio
  3756. */
  3757. public float getExpandRatio() {
  3758. return expandRatio;
  3759. }
  3760. /**
  3761. * Is the cell enabled?
  3762. *
  3763. * @return True if enabled else False
  3764. */
  3765. public boolean isEnabled() {
  3766. return getParent() != null;
  3767. }
  3768. /**
  3769. * Handle column clicking
  3770. */
  3771. @Override
  3772. public void onBrowserEvent(Event event) {
  3773. if (enabled && event != null) {
  3774. handleCaptionEvent(event);
  3775. if (DOM.eventGetType(event) == Event.ONMOUSEUP) {
  3776. scrollBodyPanel.setFocus(true);
  3777. }
  3778. boolean stopPropagation = true;
  3779. if (event.getTypeInt() == Event.ONCONTEXTMENU
  3780. && !client.hasEventListeners(VScrollTable.this,
  3781. TableConstants.FOOTER_CLICK_EVENT_ID)) {
  3782. // Show browser context menu if a footer click listener is
  3783. // not present
  3784. stopPropagation = false;
  3785. }
  3786. if (stopPropagation) {
  3787. event.stopPropagation();
  3788. event.preventDefault();
  3789. }
  3790. }
  3791. }
  3792. /**
  3793. * Handles a event on the captions
  3794. *
  3795. * @param event
  3796. * The event to handle
  3797. */
  3798. protected void handleCaptionEvent(Event event) {
  3799. if (event.getTypeInt() == Event.ONMOUSEUP
  3800. || event.getTypeInt() == Event.ONDBLCLICK) {
  3801. fireFooterClickedEvent(event);
  3802. }
  3803. }
  3804. /**
  3805. * Fires a footer click event after the user has clicked a column footer
  3806. * cell
  3807. *
  3808. * @param event
  3809. * The click event
  3810. */
  3811. private void fireFooterClickedEvent(Event event) {
  3812. if (client.hasEventListeners(VScrollTable.this,
  3813. TableConstants.FOOTER_CLICK_EVENT_ID)) {
  3814. MouseEventDetails details = MouseEventDetailsBuilder
  3815. .buildMouseEventDetails(event);
  3816. client.updateVariable(paintableId, "footerClickEvent",
  3817. details.toString(), false);
  3818. client.updateVariable(paintableId, "footerClickCID", cid, true);
  3819. }
  3820. }
  3821. /**
  3822. * Returns the column key of the column
  3823. *
  3824. * @return The column key
  3825. */
  3826. public String getColKey() {
  3827. return cid;
  3828. }
  3829. /**
  3830. * Saves natural column width if it hasn't been saved already.
  3831. *
  3832. * @param columnIndex
  3833. * @since 7.3.9
  3834. */
  3835. protected void saveNaturalColumnWidthIfNotSaved(int columnIndex) {
  3836. if (naturalWidth < 0) {
  3837. // This is recently revealed column. Try to detect a proper
  3838. // value (greater of header and data cols)
  3839. final int hw = ((Element) getElement().getLastChild())
  3840. .getOffsetWidth() + getHeaderPadding();
  3841. if (columnIndex < 0) {
  3842. columnIndex = 0;
  3843. for (Iterator<Widget> it = tHead.iterator(); it.hasNext(); columnIndex++) {
  3844. if (it.next() == this) {
  3845. break;
  3846. }
  3847. }
  3848. }
  3849. final int cw = scrollBody.getColWidth(columnIndex);
  3850. naturalWidth = (hw > cw ? hw : cw);
  3851. }
  3852. }
  3853. /**
  3854. * Detects the natural minimum width for the column of this header cell.
  3855. * If column is resized by user or the width is defined by server the
  3856. * actual width is returned. Else the natural min width is returned.
  3857. *
  3858. * @param columnIndex
  3859. * column index hint, if -1 (unknown) it will be detected
  3860. *
  3861. * @return
  3862. */
  3863. public int getNaturalColumnWidth(int columnIndex) {
  3864. final int iw = columnIndex == getHierarchyColumnIndex() ? scrollBody
  3865. .getMaxIndent() : 0;
  3866. saveNaturalColumnWidthIfNotSaved(columnIndex);
  3867. if (isDefinedWidth()) {
  3868. if (iw > width) {
  3869. return iw;
  3870. }
  3871. return width;
  3872. } else {
  3873. if (iw > naturalWidth) {
  3874. return iw;
  3875. } else {
  3876. return naturalWidth;
  3877. }
  3878. }
  3879. }
  3880. public void setNaturalMinimumColumnWidth(int w) {
  3881. naturalWidth = w;
  3882. }
  3883. }
  3884. /**
  3885. * HeaderCell that is header cell for row headers.
  3886. *
  3887. * Reordering disabled and clicking on it resets sorting.
  3888. */
  3889. public class RowHeadersFooterCell extends FooterCell {
  3890. RowHeadersFooterCell() {
  3891. super(ROW_HEADER_COLUMN_KEY, "");
  3892. }
  3893. @Override
  3894. protected void handleCaptionEvent(Event event) {
  3895. // NOP: RowHeaders cannot be reordered
  3896. // TODO It'd be nice to reset sorting here
  3897. }
  3898. }
  3899. /**
  3900. * The footer of the table which can be seen in the bottom of the Table.
  3901. */
  3902. public class TableFooter extends Panel {
  3903. private static final int WRAPPER_WIDTH = 900000;
  3904. ArrayList<Widget> visibleCells = new ArrayList<Widget>();
  3905. HashMap<String, FooterCell> availableCells = new HashMap<String, FooterCell>();
  3906. Element div = DOM.createDiv();
  3907. Element hTableWrapper = DOM.createDiv();
  3908. Element hTableContainer = DOM.createDiv();
  3909. Element table = DOM.createTable();
  3910. Element headerTableBody = DOM.createTBody();
  3911. Element tr = DOM.createTR();
  3912. public TableFooter() {
  3913. hTableWrapper.getStyle().setOverflow(Overflow.HIDDEN);
  3914. DOM.appendChild(table, headerTableBody);
  3915. DOM.appendChild(headerTableBody, tr);
  3916. DOM.appendChild(hTableContainer, table);
  3917. DOM.appendChild(hTableWrapper, hTableContainer);
  3918. DOM.appendChild(div, hTableWrapper);
  3919. setElement(div);
  3920. availableCells.put(ROW_HEADER_COLUMN_KEY,
  3921. new RowHeadersFooterCell());
  3922. updateStyleNames(VScrollTable.this.getStylePrimaryName());
  3923. }
  3924. protected void updateStyleNames(String primaryStyleName) {
  3925. hTableWrapper.setClassName(primaryStyleName + "-footer");
  3926. setStyleName(primaryStyleName + "-footer-wrap");
  3927. for (FooterCell c : availableCells.values()) {
  3928. c.updateStyleNames(primaryStyleName);
  3929. }
  3930. }
  3931. @Override
  3932. public void clear() {
  3933. for (String cid : availableCells.keySet()) {
  3934. removeCell(cid);
  3935. }
  3936. availableCells.clear();
  3937. availableCells.put(ROW_HEADER_COLUMN_KEY,
  3938. new RowHeadersFooterCell());
  3939. }
  3940. /*
  3941. * (non-Javadoc)
  3942. *
  3943. * @see
  3944. * com.google.gwt.user.client.ui.Panel#remove(com.google.gwt.user.client
  3945. * .ui.Widget)
  3946. */
  3947. @Override
  3948. public boolean remove(Widget w) {
  3949. if (visibleCells.contains(w)) {
  3950. visibleCells.remove(w);
  3951. orphan(w);
  3952. DOM.removeChild(DOM.getParent(w.getElement()), w.getElement());
  3953. return true;
  3954. }
  3955. return false;
  3956. }
  3957. /*
  3958. * (non-Javadoc)
  3959. *
  3960. * @see com.google.gwt.user.client.ui.HasWidgets#iterator()
  3961. */
  3962. @Override
  3963. public Iterator<Widget> iterator() {
  3964. return visibleCells.iterator();
  3965. }
  3966. /**
  3967. * Gets a footer cell which represents the given columnId
  3968. *
  3969. * @param cid
  3970. * The columnId
  3971. *
  3972. * @return The cell
  3973. */
  3974. public FooterCell getFooterCell(String cid) {
  3975. return availableCells.get(cid);
  3976. }
  3977. /**
  3978. * Gets a footer cell by using a column index
  3979. *
  3980. * @param index
  3981. * The index of the column
  3982. * @return The Cell
  3983. */
  3984. public FooterCell getFooterCell(int index) {
  3985. if (index < visibleCells.size()) {
  3986. return (FooterCell) visibleCells.get(index);
  3987. } else {
  3988. return null;
  3989. }
  3990. }
  3991. /**
  3992. * Updates the cells contents when updateUIDL request is received
  3993. *
  3994. * @param uidl
  3995. * The UIDL
  3996. */
  3997. public void updateCellsFromUIDL(UIDL uidl) {
  3998. Iterator<?> columnIterator = uidl.getChildIterator();
  3999. HashSet<String> updated = new HashSet<String>();
  4000. while (columnIterator.hasNext()) {
  4001. final UIDL col = (UIDL) columnIterator.next();
  4002. final String cid = col.getStringAttribute("cid");
  4003. updated.add(cid);
  4004. String caption = col.hasAttribute("fcaption") ? col
  4005. .getStringAttribute("fcaption") : "";
  4006. FooterCell c = getFooterCell(cid);
  4007. if (c == null) {
  4008. c = new FooterCell(cid, caption);
  4009. availableCells.put(cid, c);
  4010. if (initializedAndAttached) {
  4011. // we will need a column width recalculation
  4012. initializedAndAttached = false;
  4013. initialContentReceived = false;
  4014. isNewBody = true;
  4015. }
  4016. } else {
  4017. c.setText(caption);
  4018. }
  4019. if (col.hasAttribute("align")) {
  4020. c.setAlign(col.getStringAttribute("align").charAt(0));
  4021. } else {
  4022. c.setAlign(ALIGN_LEFT);
  4023. }
  4024. if (col.hasAttribute("width")) {
  4025. if (scrollBody == null || isNewBody) {
  4026. // Already updated by setColWidth called from
  4027. // TableHeads.updateCellsFromUIDL in case of a server
  4028. // side resize
  4029. final int width = col.getIntAttribute("width");
  4030. c.setWidth(width, true);
  4031. }
  4032. } else if (recalcWidths) {
  4033. c.setUndefinedWidth();
  4034. }
  4035. if (col.hasAttribute("er")) {
  4036. c.setExpandRatio(col.getFloatAttribute("er"));
  4037. }
  4038. if (col.hasAttribute("collapsed")) {
  4039. // ensure header is properly removed from parent (case when
  4040. // collapsing happens via servers side api)
  4041. if (c.isAttached()) {
  4042. c.removeFromParent();
  4043. headerChangedDuringUpdate = true;
  4044. }
  4045. }
  4046. }
  4047. // check for orphaned header cells
  4048. for (Iterator<String> cit = availableCells.keySet().iterator(); cit
  4049. .hasNext();) {
  4050. String cid = cit.next();
  4051. if (!updated.contains(cid)) {
  4052. removeCell(cid);
  4053. cit.remove();
  4054. }
  4055. }
  4056. }
  4057. /**
  4058. * Set a footer cell for a specified column index
  4059. *
  4060. * @param index
  4061. * The index
  4062. * @param cell
  4063. * The footer cell
  4064. */
  4065. public void setFooterCell(int index, FooterCell cell) {
  4066. if (cell.isEnabled()) {
  4067. // we're moving the cell
  4068. DOM.removeChild(tr, cell.getElement());
  4069. orphan(cell);
  4070. visibleCells.remove(cell);
  4071. }
  4072. if (index < visibleCells.size()) {
  4073. // insert to right slot
  4074. DOM.insertChild(tr, cell.getElement(), index);
  4075. adopt(cell);
  4076. visibleCells.add(index, cell);
  4077. } else if (index == visibleCells.size()) {
  4078. // simply append
  4079. DOM.appendChild(tr, cell.getElement());
  4080. adopt(cell);
  4081. visibleCells.add(cell);
  4082. } else {
  4083. throw new RuntimeException(
  4084. "Header cells must be appended in order");
  4085. }
  4086. }
  4087. /**
  4088. * Remove a cell by using the columnId
  4089. *
  4090. * @param colKey
  4091. * The columnId to remove
  4092. */
  4093. public void removeCell(String colKey) {
  4094. final FooterCell c = getFooterCell(colKey);
  4095. remove(c);
  4096. }
  4097. /**
  4098. * Enable a column (Sets the footer cell)
  4099. *
  4100. * @param cid
  4101. * The columnId
  4102. * @param index
  4103. * The index of the column
  4104. */
  4105. public void enableColumn(String cid, int index) {
  4106. final FooterCell c = getFooterCell(cid);
  4107. if (!c.isEnabled() || getFooterCell(index) != c) {
  4108. setFooterCell(index, c);
  4109. if (initializedAndAttached) {
  4110. headerChangedDuringUpdate = true;
  4111. }
  4112. }
  4113. }
  4114. /**
  4115. * Disable browser measurement of the table width
  4116. */
  4117. public void disableBrowserIntelligence() {
  4118. hTableContainer.getStyle().setWidth(WRAPPER_WIDTH, Unit.PX);
  4119. }
  4120. /**
  4121. * Enable browser measurement of the table width
  4122. */
  4123. public void enableBrowserIntelligence() {
  4124. hTableContainer.getStyle().clearWidth();
  4125. }
  4126. /**
  4127. * Set the horizontal position in the cell in the footer. This is done
  4128. * when a horizontal scrollbar is present.
  4129. *
  4130. * @param scrollLeft
  4131. * The value of the leftScroll
  4132. */
  4133. public void setHorizontalScrollPosition(int scrollLeft) {
  4134. hTableWrapper.setScrollLeft(scrollLeft);
  4135. }
  4136. /**
  4137. * Swap cells when the column are dragged
  4138. *
  4139. * @param oldIndex
  4140. * The old index of the cell
  4141. * @param newIndex
  4142. * The new index of the cell
  4143. */
  4144. public void moveCell(int oldIndex, int newIndex) {
  4145. final FooterCell hCell = getFooterCell(oldIndex);
  4146. final Element cell = hCell.getElement();
  4147. visibleCells.remove(oldIndex);
  4148. DOM.removeChild(tr, cell);
  4149. DOM.insertChild(tr, cell, newIndex);
  4150. visibleCells.add(newIndex, hCell);
  4151. }
  4152. }
  4153. /**
  4154. * This Panel can only contain VScrollTableRow type of widgets. This
  4155. * "simulates" very large table, keeping spacers which take room of
  4156. * unrendered rows.
  4157. *
  4158. */
  4159. public class VScrollTableBody extends Panel {
  4160. public static final int DEFAULT_ROW_HEIGHT = 24;
  4161. private double rowHeight = -1;
  4162. private final LinkedList<Widget> renderedRows = new LinkedList<Widget>();
  4163. /**
  4164. * Due some optimizations row height measuring is deferred and initial
  4165. * set of rows is rendered detached. Flag set on when table body has
  4166. * been attached in dom and rowheight has been measured.
  4167. */
  4168. private boolean tBodyMeasurementsDone = false;
  4169. Element preSpacer = DOM.createDiv();
  4170. Element postSpacer = DOM.createDiv();
  4171. Element container = DOM.createDiv();
  4172. TableSectionElement tBodyElement = Document.get().createTBodyElement();
  4173. Element table = DOM.createTable();
  4174. private int firstRendered;
  4175. private int lastRendered;
  4176. private char[] aligns;
  4177. protected VScrollTableBody() {
  4178. constructDOM();
  4179. setElement(container);
  4180. }
  4181. public void setLastRendered(int lastRendered) {
  4182. if (totalRows >= 0 && lastRendered > totalRows) {
  4183. VConsole.log("setLastRendered: " + this.lastRendered + " -> "
  4184. + lastRendered);
  4185. this.lastRendered = totalRows - 1;
  4186. } else {
  4187. this.lastRendered = lastRendered;
  4188. }
  4189. }
  4190. public int getLastRendered() {
  4191. return lastRendered;
  4192. }
  4193. public int getFirstRendered() {
  4194. return firstRendered;
  4195. }
  4196. public VScrollTableRow getRowByRowIndex(int indexInTable) {
  4197. int internalIndex = indexInTable - firstRendered;
  4198. if (internalIndex >= 0 && internalIndex < renderedRows.size()) {
  4199. return (VScrollTableRow) renderedRows.get(internalIndex);
  4200. } else {
  4201. return null;
  4202. }
  4203. }
  4204. /**
  4205. * @return the height of scrollable body, subpixels ceiled.
  4206. */
  4207. public int getRequiredHeight() {
  4208. return preSpacer.getOffsetHeight() + postSpacer.getOffsetHeight()
  4209. + WidgetUtil.getRequiredHeight(table);
  4210. }
  4211. private void constructDOM() {
  4212. if (BrowserInfo.get().isIE()) {
  4213. table.setPropertyInt("cellSpacing", 0);
  4214. }
  4215. table.appendChild(tBodyElement);
  4216. DOM.appendChild(container, preSpacer);
  4217. DOM.appendChild(container, table);
  4218. DOM.appendChild(container, postSpacer);
  4219. if (BrowserInfo.get().requiresTouchScrollDelegate()) {
  4220. NodeList<Node> childNodes = container.getChildNodes();
  4221. for (int i = 0; i < childNodes.getLength(); i++) {
  4222. Element item = (Element) childNodes.getItem(i);
  4223. item.getStyle().setProperty("webkitTransform",
  4224. "translate3d(0,0,0)");
  4225. }
  4226. }
  4227. updateStyleNames(VScrollTable.this.getStylePrimaryName());
  4228. }
  4229. protected void updateStyleNames(String primaryStyleName) {
  4230. table.setClassName(primaryStyleName + "-table");
  4231. preSpacer.setClassName(primaryStyleName + "-row-spacer");
  4232. postSpacer.setClassName(primaryStyleName + "-row-spacer");
  4233. for (Widget w : renderedRows) {
  4234. VScrollTableRow row = (VScrollTableRow) w;
  4235. row.updateStyleNames(primaryStyleName);
  4236. }
  4237. }
  4238. public int getAvailableWidth() {
  4239. int availW = scrollBodyPanel.getOffsetWidth() - getBorderWidth();
  4240. return availW;
  4241. }
  4242. public void renderInitialRows(UIDL rowData, int firstIndex, int rows) {
  4243. firstRendered = firstIndex;
  4244. setLastRendered(firstIndex + rows - 1);
  4245. final Iterator<?> it = rowData.getChildIterator();
  4246. aligns = tHead.getColumnAlignments();
  4247. while (it.hasNext()) {
  4248. final VScrollTableRow row = createRow((UIDL) it.next(), aligns);
  4249. addRow(row);
  4250. }
  4251. if (isAttached()) {
  4252. fixSpacers();
  4253. }
  4254. }
  4255. public void renderRows(UIDL rowData, int firstIndex, int rows) {
  4256. // FIXME REVIEW
  4257. aligns = tHead.getColumnAlignments();
  4258. final Iterator<?> it = rowData.getChildIterator();
  4259. if (firstIndex == lastRendered + 1) {
  4260. while (it.hasNext()) {
  4261. final VScrollTableRow row = prepareRow((UIDL) it.next());
  4262. addRow(row);
  4263. setLastRendered(lastRendered + 1);
  4264. }
  4265. fixSpacers();
  4266. } else if (firstIndex + rows == firstRendered) {
  4267. final VScrollTableRow[] rowArray = new VScrollTableRow[rows];
  4268. int i = rows;
  4269. while (it.hasNext()) {
  4270. i--;
  4271. rowArray[i] = prepareRow((UIDL) it.next());
  4272. }
  4273. for (i = 0; i < rows; i++) {
  4274. addRowBeforeFirstRendered(rowArray[i]);
  4275. firstRendered--;
  4276. }
  4277. } else {
  4278. // completely new set of rows
  4279. // there can't be sanity checks for last rendered within this
  4280. // while loop regardless of what has been set previously, so
  4281. // change it temporarily to true and then return the original
  4282. // value
  4283. boolean temp = postponeSanityCheckForLastRendered;
  4284. postponeSanityCheckForLastRendered = true;
  4285. while (lastRendered + 1 > firstRendered) {
  4286. unlinkRow(false);
  4287. }
  4288. postponeSanityCheckForLastRendered = temp;
  4289. VScrollTableRow row = prepareRow((UIDL) it.next());
  4290. firstRendered = firstIndex;
  4291. setLastRendered(firstIndex - 1);
  4292. addRow(row);
  4293. setLastRendered(lastRendered + 1);
  4294. setContainerHeight();
  4295. fixSpacers();
  4296. while (it.hasNext()) {
  4297. addRow(prepareRow((UIDL) it.next()));
  4298. setLastRendered(lastRendered + 1);
  4299. }
  4300. fixSpacers();
  4301. }
  4302. // this may be a new set of rows due content change,
  4303. // ensure we have proper cache rows
  4304. ensureCacheFilled();
  4305. }
  4306. /**
  4307. * Ensure we have the correct set of rows on client side, e.g. if the
  4308. * content on the server side has changed, or the client scroll position
  4309. * has changed since the last request.
  4310. */
  4311. protected void ensureCacheFilled() {
  4312. /**
  4313. * Fixes cache issue #13576 where unnecessary rows are fetched
  4314. */
  4315. if (isLazyScrollerActive()) {
  4316. return;
  4317. }
  4318. int reactFirstRow = (int) (firstRowInViewPort - pageLength
  4319. * cache_react_rate);
  4320. int reactLastRow = (int) (firstRowInViewPort + pageLength + pageLength
  4321. * cache_react_rate);
  4322. if (reactFirstRow < 0) {
  4323. reactFirstRow = 0;
  4324. }
  4325. if (reactLastRow >= totalRows) {
  4326. reactLastRow = totalRows - 1;
  4327. }
  4328. if (lastRendered < reactFirstRow || firstRendered > reactLastRow) {
  4329. /*
  4330. * #8040 - scroll position is completely changed since the
  4331. * latest request, so request a new set of rows.
  4332. *
  4333. * TODO: We should probably check whether the fetched rows match
  4334. * the current scroll position right when they arrive, so as to
  4335. * not waste time rendering a set of rows that will never be
  4336. * visible...
  4337. */
  4338. rowRequestHandler.triggerRowFetch(reactFirstRow, reactLastRow
  4339. - reactFirstRow + 1, 1);
  4340. } else if (lastRendered < reactLastRow) {
  4341. // get some cache rows below visible area
  4342. rowRequestHandler.triggerRowFetch(lastRendered + 1,
  4343. reactLastRow - lastRendered, 1);
  4344. } else if (firstRendered > reactFirstRow) {
  4345. /*
  4346. * Branch for fetching cache above visible area.
  4347. *
  4348. * If cache needed for both before and after visible area, this
  4349. * will be rendered after-cache is received and rendered. So in
  4350. * some rare situations the table may make two cache visits to
  4351. * server.
  4352. */
  4353. rowRequestHandler.triggerRowFetch(reactFirstRow, firstRendered
  4354. - reactFirstRow, 1);
  4355. }
  4356. }
  4357. /**
  4358. * Inserts rows as provided in the rowData starting at firstIndex.
  4359. *
  4360. * @param rowData
  4361. * @param firstIndex
  4362. * @param rows
  4363. * the number of rows
  4364. * @return a list of the rows added.
  4365. */
  4366. protected List<VScrollTableRow> insertRows(UIDL rowData,
  4367. int firstIndex, int rows) {
  4368. aligns = tHead.getColumnAlignments();
  4369. final Iterator<?> it = rowData.getChildIterator();
  4370. List<VScrollTableRow> insertedRows = new ArrayList<VScrollTableRow>();
  4371. if (firstIndex == lastRendered + 1) {
  4372. while (it.hasNext()) {
  4373. final VScrollTableRow row = prepareRow((UIDL) it.next());
  4374. addRow(row);
  4375. insertedRows.add(row);
  4376. if (postponeSanityCheckForLastRendered) {
  4377. lastRendered++;
  4378. } else {
  4379. setLastRendered(lastRendered + 1);
  4380. }
  4381. }
  4382. fixSpacers();
  4383. } else if (firstIndex + rows == firstRendered) {
  4384. final VScrollTableRow[] rowArray = new VScrollTableRow[rows];
  4385. int i = rows;
  4386. while (it.hasNext()) {
  4387. i--;
  4388. rowArray[i] = prepareRow((UIDL) it.next());
  4389. }
  4390. for (i = 0; i < rows; i++) {
  4391. addRowBeforeFirstRendered(rowArray[i]);
  4392. insertedRows.add(rowArray[i]);
  4393. firstRendered--;
  4394. }
  4395. } else {
  4396. // insert in the middle
  4397. int ix = firstIndex;
  4398. while (it.hasNext()) {
  4399. VScrollTableRow row = prepareRow((UIDL) it.next());
  4400. insertRowAt(row, ix);
  4401. insertedRows.add(row);
  4402. if (postponeSanityCheckForLastRendered) {
  4403. lastRendered++;
  4404. } else {
  4405. setLastRendered(lastRendered + 1);
  4406. }
  4407. ix++;
  4408. }
  4409. fixSpacers();
  4410. }
  4411. return insertedRows;
  4412. }
  4413. protected List<VScrollTableRow> insertAndReindexRows(UIDL rowData,
  4414. int firstIndex, int rows) {
  4415. List<VScrollTableRow> inserted = insertRows(rowData, firstIndex,
  4416. rows);
  4417. int actualIxOfFirstRowAfterInserted = firstIndex + rows
  4418. - firstRendered;
  4419. for (int ix = actualIxOfFirstRowAfterInserted; ix < renderedRows
  4420. .size(); ix++) {
  4421. VScrollTableRow r = (VScrollTableRow) renderedRows.get(ix);
  4422. r.setIndex(r.getIndex() + rows);
  4423. }
  4424. setContainerHeight();
  4425. return inserted;
  4426. }
  4427. protected void insertRowsDeleteBelow(UIDL rowData, int firstIndex,
  4428. int rows) {
  4429. unlinkAllRowsStartingAt(firstIndex);
  4430. insertRows(rowData, firstIndex, rows);
  4431. setContainerHeight();
  4432. }
  4433. /**
  4434. * This method is used to instantiate new rows for this table. It
  4435. * automatically sets correct widths to rows cells and assigns correct
  4436. * client reference for child widgets.
  4437. *
  4438. * This method can be called only after table has been initialized
  4439. *
  4440. * @param uidl
  4441. */
  4442. private VScrollTableRow prepareRow(UIDL uidl) {
  4443. final VScrollTableRow row = createRow(uidl, aligns);
  4444. row.initCellWidths();
  4445. return row;
  4446. }
  4447. protected VScrollTableRow createRow(UIDL uidl, char[] aligns2) {
  4448. if (uidl.hasAttribute("gen_html")) {
  4449. // This is a generated row.
  4450. return new VScrollTableGeneratedRow(uidl, aligns2);
  4451. }
  4452. return new VScrollTableRow(uidl, aligns2);
  4453. }
  4454. private void addRowBeforeFirstRendered(VScrollTableRow row) {
  4455. row.setIndex(firstRendered - 1);
  4456. if (row.isSelected()) {
  4457. row.addStyleName("v-selected");
  4458. }
  4459. tBodyElement.insertBefore(row.getElement(),
  4460. tBodyElement.getFirstChild());
  4461. adopt(row);
  4462. renderedRows.add(0, row);
  4463. }
  4464. private void addRow(VScrollTableRow row) {
  4465. row.setIndex(firstRendered + renderedRows.size());
  4466. if (row.isSelected()) {
  4467. row.addStyleName("v-selected");
  4468. }
  4469. tBodyElement.appendChild(row.getElement());
  4470. // Add to renderedRows before adopt so iterator() will return also
  4471. // this row if called in an attach handler (#9264)
  4472. renderedRows.add(row);
  4473. adopt(row);
  4474. }
  4475. private void insertRowAt(VScrollTableRow row, int index) {
  4476. row.setIndex(index);
  4477. if (row.isSelected()) {
  4478. row.addStyleName("v-selected");
  4479. }
  4480. if (index > 0) {
  4481. VScrollTableRow sibling = getRowByRowIndex(index - 1);
  4482. tBodyElement
  4483. .insertAfter(row.getElement(), sibling.getElement());
  4484. } else {
  4485. VScrollTableRow sibling = getRowByRowIndex(index);
  4486. tBodyElement.insertBefore(row.getElement(),
  4487. sibling.getElement());
  4488. }
  4489. adopt(row);
  4490. int actualIx = index - firstRendered;
  4491. renderedRows.add(actualIx, row);
  4492. }
  4493. @Override
  4494. public Iterator<Widget> iterator() {
  4495. return renderedRows.iterator();
  4496. }
  4497. /**
  4498. * @return false if couldn't remove row
  4499. */
  4500. protected boolean unlinkRow(boolean fromBeginning) {
  4501. if (lastRendered - firstRendered < 0) {
  4502. return false;
  4503. }
  4504. int actualIx;
  4505. if (fromBeginning) {
  4506. actualIx = 0;
  4507. firstRendered++;
  4508. } else {
  4509. actualIx = renderedRows.size() - 1;
  4510. if (postponeSanityCheckForLastRendered) {
  4511. --lastRendered;
  4512. } else {
  4513. setLastRendered(lastRendered - 1);
  4514. }
  4515. }
  4516. if (actualIx >= 0) {
  4517. unlinkRowAtActualIndex(actualIx);
  4518. fixSpacers();
  4519. return true;
  4520. }
  4521. return false;
  4522. }
  4523. protected void unlinkRows(int firstIndex, int count) {
  4524. if (count < 1) {
  4525. return;
  4526. }
  4527. if (firstRendered > firstIndex
  4528. && firstRendered < firstIndex + count) {
  4529. count = count - (firstRendered - firstIndex);
  4530. firstIndex = firstRendered;
  4531. }
  4532. int lastIndex = firstIndex + count - 1;
  4533. if (lastRendered < lastIndex) {
  4534. lastIndex = lastRendered;
  4535. }
  4536. for (int ix = lastIndex; ix >= firstIndex; ix--) {
  4537. unlinkRowAtActualIndex(actualIndex(ix));
  4538. if (postponeSanityCheckForLastRendered) {
  4539. // partialUpdate handles sanity check later
  4540. lastRendered--;
  4541. } else {
  4542. setLastRendered(lastRendered - 1);
  4543. }
  4544. }
  4545. fixSpacers();
  4546. }
  4547. protected void unlinkAndReindexRows(int firstIndex, int count) {
  4548. unlinkRows(firstIndex, count);
  4549. int actualFirstIx = firstIndex - firstRendered;
  4550. for (int ix = actualFirstIx; ix < renderedRows.size(); ix++) {
  4551. VScrollTableRow r = (VScrollTableRow) renderedRows.get(ix);
  4552. r.setIndex(r.getIndex() - count);
  4553. }
  4554. setContainerHeight();
  4555. }
  4556. protected void unlinkAllRowsStartingAt(int index) {
  4557. if (firstRendered > index) {
  4558. index = firstRendered;
  4559. }
  4560. for (int ix = renderedRows.size() - 1; ix >= index; ix--) {
  4561. unlinkRowAtActualIndex(actualIndex(ix));
  4562. setLastRendered(lastRendered - 1);
  4563. }
  4564. fixSpacers();
  4565. }
  4566. private int actualIndex(int index) {
  4567. return index - firstRendered;
  4568. }
  4569. private void unlinkRowAtActualIndex(int index) {
  4570. final VScrollTableRow toBeRemoved = (VScrollTableRow) renderedRows
  4571. .get(index);
  4572. tBodyElement.removeChild(toBeRemoved.getElement());
  4573. orphan(toBeRemoved);
  4574. renderedRows.remove(index);
  4575. }
  4576. @Override
  4577. public boolean remove(Widget w) {
  4578. throw new UnsupportedOperationException();
  4579. }
  4580. /**
  4581. * Fix container blocks height according to totalRows to avoid
  4582. * "bouncing" when scrolling
  4583. */
  4584. private void setContainerHeight() {
  4585. fixSpacers();
  4586. container.getStyle().setHeight(measureRowHeightOffset(totalRows),
  4587. Unit.PX);
  4588. }
  4589. private void fixSpacers() {
  4590. int prepx = measureRowHeightOffset(firstRendered);
  4591. if (prepx < 0) {
  4592. prepx = 0;
  4593. }
  4594. preSpacer.getStyle().setPropertyPx("height", prepx);
  4595. int postpx;
  4596. if (pageLength == 0 && totalRows == pageLength) {
  4597. /*
  4598. * TreeTable depends on having lastRendered out of sync in some
  4599. * situations, which makes this method miss the special
  4600. * situation in which one row worth of post spacer to be added
  4601. * if there are no rows in the table. #9203
  4602. */
  4603. postpx = measureRowHeightOffset(1);
  4604. } else {
  4605. postpx = measureRowHeightOffset(totalRows - 1)
  4606. - measureRowHeightOffset(lastRendered);
  4607. }
  4608. if (postpx < 0) {
  4609. postpx = 0;
  4610. }
  4611. postSpacer.getStyle().setPropertyPx("height", postpx);
  4612. }
  4613. public double getRowHeight() {
  4614. return getRowHeight(false);
  4615. }
  4616. public double getRowHeight(boolean forceUpdate) {
  4617. if (tBodyMeasurementsDone && !forceUpdate) {
  4618. return rowHeight;
  4619. } else {
  4620. if (tBodyElement.getRows().getLength() > 0) {
  4621. int tableHeight = getTableHeight();
  4622. int rowCount = tBodyElement.getRows().getLength();
  4623. rowHeight = tableHeight / (double) rowCount;
  4624. } else {
  4625. // Special cases if we can't just measure the current rows
  4626. if (!Double.isNaN(lastKnownRowHeight)) {
  4627. // Use previous value if available
  4628. if (BrowserInfo.get().isIE()) {
  4629. /*
  4630. * IE needs to reflow the table element at this
  4631. * point to work correctly (e.g.
  4632. * com.vaadin.tests.components.table.
  4633. * ContainerSizeChange) - the other code paths
  4634. * already trigger reflows, but here it must be done
  4635. * explicitly.
  4636. */
  4637. getTableHeight();
  4638. }
  4639. rowHeight = lastKnownRowHeight;
  4640. } else if (isAttached()) {
  4641. // measure row height by adding a dummy row
  4642. VScrollTableRow scrollTableRow = new VScrollTableRow();
  4643. tBodyElement.appendChild(scrollTableRow.getElement());
  4644. getRowHeight(forceUpdate);
  4645. tBodyElement.removeChild(scrollTableRow.getElement());
  4646. } else {
  4647. // TODO investigate if this can never happen anymore
  4648. return DEFAULT_ROW_HEIGHT;
  4649. }
  4650. }
  4651. lastKnownRowHeight = rowHeight;
  4652. tBodyMeasurementsDone = true;
  4653. return rowHeight;
  4654. }
  4655. }
  4656. public int getTableHeight() {
  4657. return table.getOffsetHeight();
  4658. }
  4659. /**
  4660. * Returns the width available for column content.
  4661. *
  4662. * @param columnIndex
  4663. * @return
  4664. */
  4665. public int getColWidth(int columnIndex) {
  4666. if (tBodyMeasurementsDone) {
  4667. if (renderedRows.isEmpty()) {
  4668. // no rows yet rendered
  4669. return 0;
  4670. }
  4671. for (Widget row : renderedRows) {
  4672. if (!(row instanceof VScrollTableGeneratedRow)) {
  4673. TableRowElement tr = row.getElement().cast();
  4674. Element wrapperdiv = tr.getCells().getItem(columnIndex)
  4675. .getFirstChildElement().cast();
  4676. return wrapperdiv.getOffsetWidth();
  4677. }
  4678. }
  4679. return 0;
  4680. } else {
  4681. return 0;
  4682. }
  4683. }
  4684. /**
  4685. * Sets the content width of a column.
  4686. *
  4687. * Due IE limitation, we must set the width to a wrapper elements inside
  4688. * table cells (with overflow hidden, which does not work on td
  4689. * elements).
  4690. *
  4691. * To get this work properly crossplatform, we will also set the width
  4692. * of td.
  4693. *
  4694. * @param colIndex
  4695. * @param w
  4696. */
  4697. public void setColWidth(int colIndex, int w) {
  4698. for (Widget row : renderedRows) {
  4699. ((VScrollTableRow) row).setCellWidth(colIndex, w);
  4700. }
  4701. }
  4702. private int cellExtraWidth = -1;
  4703. /**
  4704. * Method to return the space used for cell paddings + border.
  4705. */
  4706. private int getCellExtraWidth() {
  4707. if (cellExtraWidth < 0) {
  4708. detectExtrawidth();
  4709. }
  4710. return cellExtraWidth;
  4711. }
  4712. /**
  4713. * This method exists for the needs of {@link VTreeTable} only. May be
  4714. * removed or replaced in the future.</br> </br> Returns the maximum
  4715. * indent of the hierarcyColumn, if applicable.
  4716. *
  4717. * @see {@link VScrollTable#getHierarchyColumnIndex()}
  4718. *
  4719. * @return maximum indent in pixels
  4720. */
  4721. protected int getMaxIndent() {
  4722. return 0;
  4723. }
  4724. /**
  4725. * This method exists for the needs of {@link VTreeTable} only. May be
  4726. * removed or replaced in the future.</br> </br> Calculates the maximum
  4727. * indent of the hierarcyColumn, if applicable.
  4728. */
  4729. protected void calculateMaxIndent() {
  4730. // NOP
  4731. }
  4732. private void detectExtrawidth() {
  4733. NodeList<TableRowElement> rows = tBodyElement.getRows();
  4734. if (rows.getLength() == 0) {
  4735. /* need to temporary add empty row and detect */
  4736. VScrollTableRow scrollTableRow = new VScrollTableRow();
  4737. scrollTableRow.updateStyleNames(VScrollTable.this
  4738. .getStylePrimaryName());
  4739. tBodyElement.appendChild(scrollTableRow.getElement());
  4740. detectExtrawidth();
  4741. tBodyElement.removeChild(scrollTableRow.getElement());
  4742. } else {
  4743. boolean noCells = false;
  4744. TableRowElement item = rows.getItem(0);
  4745. TableCellElement firstTD = item.getCells().getItem(0);
  4746. if (firstTD == null) {
  4747. // content is currently empty, we need to add a fake cell
  4748. // for measuring
  4749. noCells = true;
  4750. VScrollTableRow next = (VScrollTableRow) iterator().next();
  4751. boolean sorted = tHead.getHeaderCell(0) != null ? tHead
  4752. .getHeaderCell(0).isSorted() : false;
  4753. next.addCell(null, "", ALIGN_LEFT, "", true, sorted);
  4754. firstTD = item.getCells().getItem(0);
  4755. }
  4756. com.google.gwt.dom.client.Element wrapper = firstTD
  4757. .getFirstChildElement();
  4758. cellExtraWidth = firstTD.getOffsetWidth()
  4759. - wrapper.getOffsetWidth();
  4760. if (noCells) {
  4761. firstTD.getParentElement().removeChild(firstTD);
  4762. }
  4763. }
  4764. }
  4765. public void moveCol(int oldIndex, int newIndex) {
  4766. // loop all rows and move given index to its new place
  4767. final Iterator<?> rows = iterator();
  4768. while (rows.hasNext()) {
  4769. final VScrollTableRow row = (VScrollTableRow) rows.next();
  4770. final Element td = DOM.getChild(row.getElement(), oldIndex);
  4771. if (td != null) {
  4772. DOM.removeChild(row.getElement(), td);
  4773. DOM.insertChild(row.getElement(), td, newIndex);
  4774. }
  4775. }
  4776. }
  4777. /**
  4778. * Restore row visibility which is set to "none" when the row is
  4779. * rendered (due a performance optimization).
  4780. */
  4781. private void restoreRowVisibility() {
  4782. for (Widget row : renderedRows) {
  4783. row.getElement().getStyle().setProperty("visibility", "");
  4784. }
  4785. }
  4786. public int indexOf(Widget row) {
  4787. int relIx = -1;
  4788. for (int ix = 0; ix < renderedRows.size(); ix++) {
  4789. if (renderedRows.get(ix) == row) {
  4790. relIx = ix;
  4791. break;
  4792. }
  4793. }
  4794. if (relIx >= 0) {
  4795. return firstRendered + relIx;
  4796. }
  4797. return -1;
  4798. }
  4799. public class VScrollTableRow extends Panel implements ActionOwner,
  4800. ContextMenuOwner {
  4801. private static final int TOUCHSCROLL_TIMEOUT = 100;
  4802. private static final int DRAGMODE_MULTIROW = 2;
  4803. protected ArrayList<Widget> childWidgets = new ArrayList<Widget>();
  4804. private boolean selected = false;
  4805. protected final int rowKey;
  4806. private String[] actionKeys = null;
  4807. private final TableRowElement rowElement;
  4808. private int index;
  4809. private Event touchStart;
  4810. private static final int TOUCH_CONTEXT_MENU_TIMEOUT = 500;
  4811. private Timer contextTouchTimeout;
  4812. private Timer dragTouchTimeout;
  4813. private int touchStartY;
  4814. private int touchStartX;
  4815. private TouchContextProvider touchContextProvider = new TouchContextProvider(
  4816. this);
  4817. private TooltipInfo tooltipInfo = null;
  4818. private Map<TableCellElement, TooltipInfo> cellToolTips = new HashMap<TableCellElement, TooltipInfo>();
  4819. private boolean isDragging = false;
  4820. private String rowStyle = null;
  4821. protected boolean applyZeroWidthFix = true;
  4822. private VScrollTableRow(int rowKey) {
  4823. this.rowKey = rowKey;
  4824. rowElement = Document.get().createTRElement();
  4825. setElement(rowElement);
  4826. DOM.sinkEvents(getElement(), Event.MOUSEEVENTS
  4827. | Event.TOUCHEVENTS | Event.ONDBLCLICK
  4828. | Event.ONCONTEXTMENU | VTooltip.TOOLTIP_EVENTS);
  4829. }
  4830. public VScrollTableRow(UIDL uidl, char[] aligns) {
  4831. this(uidl.getIntAttribute("key"));
  4832. /*
  4833. * Rendering the rows as hidden improves Firefox and Safari
  4834. * performance drastically.
  4835. */
  4836. getElement().getStyle().setProperty("visibility", "hidden");
  4837. rowStyle = uidl.getStringAttribute("rowstyle");
  4838. updateStyleNames(VScrollTable.this.getStylePrimaryName());
  4839. String rowDescription = uidl.getStringAttribute("rowdescr");
  4840. if (rowDescription != null && !rowDescription.equals("")) {
  4841. tooltipInfo = new TooltipInfo(rowDescription, null, this);
  4842. } else {
  4843. tooltipInfo = null;
  4844. }
  4845. tHead.getColumnAlignments();
  4846. int col = 0;
  4847. int visibleColumnIndex = -1;
  4848. // row header
  4849. if (showRowHeaders) {
  4850. boolean sorted = tHead.getHeaderCell(col).isSorted();
  4851. addCell(uidl, buildCaptionHtmlSnippet(uidl), aligns[col++],
  4852. "rowheader", true, sorted);
  4853. visibleColumnIndex++;
  4854. }
  4855. if (uidl.hasAttribute("al")) {
  4856. actionKeys = uidl.getStringArrayAttribute("al");
  4857. }
  4858. addCellsFromUIDL(uidl, aligns, col, visibleColumnIndex);
  4859. if (uidl.hasAttribute("selected") && !isSelected()) {
  4860. toggleSelection();
  4861. }
  4862. }
  4863. protected void updateStyleNames(String primaryStyleName) {
  4864. if (getStylePrimaryName().contains("odd")) {
  4865. setStyleName(primaryStyleName + "-row-odd");
  4866. } else {
  4867. setStyleName(primaryStyleName + "-row");
  4868. }
  4869. if (rowStyle != null) {
  4870. addStyleName(primaryStyleName + "-row-" + rowStyle);
  4871. }
  4872. for (int i = 0; i < rowElement.getChildCount(); i++) {
  4873. TableCellElement cell = (TableCellElement) rowElement
  4874. .getChild(i);
  4875. updateCellStyleNames(cell, primaryStyleName);
  4876. }
  4877. }
  4878. public TooltipInfo getTooltipInfo() {
  4879. return tooltipInfo;
  4880. }
  4881. /**
  4882. * Add a dummy row, used for measurements if Table is empty.
  4883. */
  4884. public VScrollTableRow() {
  4885. this(0);
  4886. addCell(null, "_", 'b', "", true, false);
  4887. }
  4888. protected void initCellWidths() {
  4889. final int cells = tHead.getVisibleCellCount();
  4890. for (int i = 0; i < cells; i++) {
  4891. int w = VScrollTable.this.getColWidth(getColKeyByIndex(i));
  4892. if (w < 0) {
  4893. w = 0;
  4894. }
  4895. setCellWidth(i, w);
  4896. }
  4897. }
  4898. protected void setCellWidth(int cellIx, int width) {
  4899. final Element cell = DOM.getChild(getElement(), cellIx);
  4900. Style wrapperStyle = cell.getFirstChildElement().getStyle();
  4901. int wrapperWidth = width;
  4902. if (BrowserInfo.get().isWebkit()
  4903. || BrowserInfo.get().isOpera10()) {
  4904. /*
  4905. * Some versions of Webkit and Opera ignore the width
  4906. * definition of zero width table cells. Instead, use 1px
  4907. * and compensate with a negative margin.
  4908. */
  4909. if (applyZeroWidthFix && width == 0) {
  4910. wrapperWidth = 1;
  4911. wrapperStyle.setMarginRight(-1, Unit.PX);
  4912. } else {
  4913. wrapperStyle.clearMarginRight();
  4914. }
  4915. }
  4916. wrapperStyle.setPropertyPx("width", wrapperWidth);
  4917. cell.getStyle().setPropertyPx("width", width);
  4918. }
  4919. protected void addCellsFromUIDL(UIDL uidl, char[] aligns, int col,
  4920. int visibleColumnIndex) {
  4921. final Iterator<?> cells = uidl.getChildIterator();
  4922. while (cells.hasNext()) {
  4923. final Object cell = cells.next();
  4924. visibleColumnIndex++;
  4925. String columnId = visibleColOrder[visibleColumnIndex];
  4926. String style = "";
  4927. if (uidl.hasAttribute("style-" + columnId)) {
  4928. style = uidl.getStringAttribute("style-" + columnId);
  4929. }
  4930. String description = null;
  4931. if (uidl.hasAttribute("descr-" + columnId)) {
  4932. description = uidl.getStringAttribute("descr-"
  4933. + columnId);
  4934. }
  4935. boolean sorted = tHead.getHeaderCell(col).isSorted();
  4936. if (cell instanceof String) {
  4937. addCell(uidl, cell.toString(), aligns[col++], style,
  4938. isRenderHtmlInCells(), sorted, description);
  4939. } else {
  4940. final ComponentConnector cellContent = client
  4941. .getPaintable((UIDL) cell);
  4942. addCell(uidl, cellContent.getWidget(), aligns[col++],
  4943. style, sorted, description);
  4944. }
  4945. }
  4946. }
  4947. /**
  4948. * Overriding this and returning true causes all text cells to be
  4949. * rendered as HTML.
  4950. *
  4951. * @return always returns false in the default implementation
  4952. */
  4953. protected boolean isRenderHtmlInCells() {
  4954. return false;
  4955. }
  4956. /**
  4957. * Detects whether row is visible in tables viewport.
  4958. *
  4959. * @return
  4960. */
  4961. public boolean isInViewPort() {
  4962. int absoluteTop = getAbsoluteTop();
  4963. int absoluteBottom = absoluteTop + getOffsetHeight();
  4964. int viewPortTop = scrollBodyPanel.getAbsoluteTop();
  4965. int viewPortBottom = viewPortTop
  4966. + scrollBodyPanel.getOffsetHeight();
  4967. return absoluteBottom > viewPortTop
  4968. && absoluteTop < viewPortBottom;
  4969. }
  4970. /**
  4971. * Makes a check based on indexes whether the row is before the
  4972. * compared row.
  4973. *
  4974. * @param row1
  4975. * @return true if this rows index is smaller than in the row1
  4976. */
  4977. public boolean isBefore(VScrollTableRow row1) {
  4978. return getIndex() < row1.getIndex();
  4979. }
  4980. /**
  4981. * Sets the index of the row in the whole table. Currently used just
  4982. * to set even/odd classname
  4983. *
  4984. * @param indexInWholeTable
  4985. */
  4986. private void setIndex(int indexInWholeTable) {
  4987. index = indexInWholeTable;
  4988. boolean isOdd = indexInWholeTable % 2 == 0;
  4989. // Inverted logic to be backwards compatible with earlier 6.4.
  4990. // It is very strange because rows 1,3,5 are considered "even"
  4991. // and 2,4,6 "odd".
  4992. //
  4993. // First remove any old styles so that both styles aren't
  4994. // applied when indexes are updated.
  4995. String primaryStyleName = getStylePrimaryName();
  4996. if (primaryStyleName != null && !primaryStyleName.equals("")) {
  4997. removeStyleName(getStylePrimaryName());
  4998. }
  4999. if (!isOdd) {
  5000. addStyleName(VScrollTable.this.getStylePrimaryName()
  5001. + "-row-odd");
  5002. } else {
  5003. addStyleName(VScrollTable.this.getStylePrimaryName()
  5004. + "-row");
  5005. }
  5006. }
  5007. public int getIndex() {
  5008. return index;
  5009. }
  5010. @Override
  5011. protected void onDetach() {
  5012. super.onDetach();
  5013. client.getContextMenu().ensureHidden(this);
  5014. }
  5015. public String getKey() {
  5016. return String.valueOf(rowKey);
  5017. }
  5018. public void addCell(UIDL rowUidl, String text, char align,
  5019. String style, boolean textIsHTML, boolean sorted) {
  5020. addCell(rowUidl, text, align, style, textIsHTML, sorted, null);
  5021. }
  5022. public void addCell(UIDL rowUidl, String text, char align,
  5023. String style, boolean textIsHTML, boolean sorted,
  5024. String description) {
  5025. // String only content is optimized by not using Label widget
  5026. final TableCellElement td = DOM.createTD().cast();
  5027. initCellWithText(text, align, style, textIsHTML, sorted,
  5028. description, td);
  5029. }
  5030. protected void initCellWithText(String text, char align,
  5031. String style, boolean textIsHTML, boolean sorted,
  5032. String description, final TableCellElement td) {
  5033. final Element container = DOM.createDiv();
  5034. container.setClassName(VScrollTable.this.getStylePrimaryName()
  5035. + "-cell-wrapper");
  5036. td.setClassName(VScrollTable.this.getStylePrimaryName()
  5037. + "-cell-content");
  5038. if (style != null && !style.equals("")) {
  5039. td.addClassName(VScrollTable.this.getStylePrimaryName()
  5040. + "-cell-content-" + style);
  5041. }
  5042. if (sorted) {
  5043. td.addClassName(VScrollTable.this.getStylePrimaryName()
  5044. + "-cell-content-sorted");
  5045. }
  5046. if (textIsHTML) {
  5047. container.setInnerHTML(text);
  5048. } else {
  5049. container.setInnerText(text);
  5050. }
  5051. setAlign(align, container);
  5052. setTooltip(td, description);
  5053. td.appendChild(container);
  5054. getElement().appendChild(td);
  5055. }
  5056. protected void updateCellStyleNames(TableCellElement td,
  5057. String primaryStyleName) {
  5058. Element container = td.getFirstChild().cast();
  5059. container.setClassName(primaryStyleName + "-cell-wrapper");
  5060. /*
  5061. * Replace old primary style name with new one
  5062. */
  5063. String className = td.getClassName();
  5064. String oldPrimaryName = className.split("-cell-content")[0];
  5065. td.setClassName(className.replaceAll(oldPrimaryName,
  5066. primaryStyleName));
  5067. }
  5068. public void addCell(UIDL rowUidl, Widget w, char align,
  5069. String style, boolean sorted, String description) {
  5070. final TableCellElement td = DOM.createTD().cast();
  5071. initCellWithWidget(w, align, style, sorted, td);
  5072. setTooltip(td, description);
  5073. }
  5074. private void setTooltip(TableCellElement td, String description) {
  5075. if (description != null && !description.equals("")) {
  5076. TooltipInfo info = new TooltipInfo(description, null, this);
  5077. cellToolTips.put(td, info);
  5078. } else {
  5079. cellToolTips.remove(td);
  5080. }
  5081. }
  5082. private void setAlign(char align, final Element container) {
  5083. switch (align) {
  5084. case ALIGN_CENTER:
  5085. container.getStyle().setProperty("textAlign", "center");
  5086. break;
  5087. case ALIGN_LEFT:
  5088. container.getStyle().setProperty("textAlign", "left");
  5089. break;
  5090. case ALIGN_RIGHT:
  5091. default:
  5092. container.getStyle().setProperty("textAlign", "right");
  5093. break;
  5094. }
  5095. }
  5096. protected void initCellWithWidget(Widget w, char align,
  5097. String style, boolean sorted, final TableCellElement td) {
  5098. final Element container = DOM.createDiv();
  5099. String className = VScrollTable.this.getStylePrimaryName()
  5100. + "-cell-content";
  5101. if (style != null && !style.equals("")) {
  5102. className += " " + VScrollTable.this.getStylePrimaryName()
  5103. + "-cell-content-" + style;
  5104. }
  5105. if (sorted) {
  5106. className += " " + VScrollTable.this.getStylePrimaryName()
  5107. + "-cell-content-sorted";
  5108. }
  5109. td.setClassName(className);
  5110. container.setClassName(VScrollTable.this.getStylePrimaryName()
  5111. + "-cell-wrapper");
  5112. setAlign(align, container);
  5113. td.appendChild(container);
  5114. getElement().appendChild(td);
  5115. // ensure widget not attached to another element (possible tBody
  5116. // change)
  5117. w.removeFromParent();
  5118. container.appendChild(w.getElement());
  5119. adopt(w);
  5120. childWidgets.add(w);
  5121. }
  5122. @Override
  5123. public Iterator<Widget> iterator() {
  5124. return childWidgets.iterator();
  5125. }
  5126. @Override
  5127. public boolean remove(Widget w) {
  5128. if (childWidgets.contains(w)) {
  5129. orphan(w);
  5130. DOM.removeChild(DOM.getParent(w.getElement()),
  5131. w.getElement());
  5132. childWidgets.remove(w);
  5133. return true;
  5134. } else {
  5135. return false;
  5136. }
  5137. }
  5138. /**
  5139. * If there are registered click listeners, sends a click event and
  5140. * returns true. Otherwise, does nothing and returns false.
  5141. *
  5142. * @param event
  5143. * @param targetTdOrTr
  5144. * @param immediate
  5145. * Whether the event is sent immediately
  5146. * @return Whether a click event was sent
  5147. */
  5148. private boolean handleClickEvent(Event event, Element targetTdOrTr,
  5149. boolean immediate) {
  5150. if (!client.hasEventListeners(VScrollTable.this,
  5151. TableConstants.ITEM_CLICK_EVENT_ID)) {
  5152. // Don't send an event if nobody is listening
  5153. return false;
  5154. }
  5155. // This row was clicked
  5156. client.updateVariable(paintableId, "clickedKey", "" + rowKey,
  5157. false);
  5158. if (getElement() == targetTdOrTr.getParentElement()) {
  5159. // A specific column was clicked
  5160. int childIndex = DOM.getChildIndex(getElement(),
  5161. targetTdOrTr);
  5162. String colKey = null;
  5163. colKey = tHead.getHeaderCell(childIndex).getColKey();
  5164. client.updateVariable(paintableId, "clickedColKey", colKey,
  5165. false);
  5166. }
  5167. MouseEventDetails details = MouseEventDetailsBuilder
  5168. .buildMouseEventDetails(event);
  5169. client.updateVariable(paintableId, "clickEvent",
  5170. details.toString(), immediate);
  5171. return true;
  5172. }
  5173. public TooltipInfo getTooltip(
  5174. com.google.gwt.dom.client.Element target) {
  5175. TooltipInfo info = null;
  5176. final Element targetTdOrTr = getTdOrTr(target);
  5177. if (targetTdOrTr != null
  5178. && "td".equals(targetTdOrTr.getTagName().toLowerCase())) {
  5179. TableCellElement td = (TableCellElement) targetTdOrTr
  5180. .cast();
  5181. info = cellToolTips.get(td);
  5182. }
  5183. if (info == null) {
  5184. info = tooltipInfo;
  5185. }
  5186. return info;
  5187. }
  5188. private Element getTdOrTr(Element target) {
  5189. Element thisTrElement = getElement();
  5190. if (target == thisTrElement) {
  5191. // This was a on the TR element
  5192. return target;
  5193. }
  5194. // Iterate upwards until we find the TR element
  5195. Element element = target;
  5196. while (element != null
  5197. && element.getParentElement() != thisTrElement) {
  5198. element = element.getParentElement();
  5199. }
  5200. return element;
  5201. }
  5202. /**
  5203. * Special handler for touch devices that support native scrolling
  5204. *
  5205. * @return Whether the event was handled by this method.
  5206. */
  5207. private boolean handleTouchEvent(final Event event) {
  5208. boolean touchEventHandled = false;
  5209. if (enabled && hasNativeTouchScrolling) {
  5210. touchContextProvider.handleTouchEvent(event);
  5211. final Element targetTdOrTr = getEventTargetTdOrTr(event);
  5212. final int type = event.getTypeInt();
  5213. switch (type) {
  5214. case Event.ONTOUCHSTART:
  5215. touchEventHandled = true;
  5216. touchStart = event;
  5217. isDragging = false;
  5218. Touch touch = event.getChangedTouches().get(0);
  5219. // save position to fields, touches in events are same
  5220. // instance during the operation.
  5221. touchStartX = touch.getClientX();
  5222. touchStartY = touch.getClientY();
  5223. if (dragmode != 0) {
  5224. if (dragTouchTimeout == null) {
  5225. dragTouchTimeout = new Timer() {
  5226. @Override
  5227. public void run() {
  5228. if (touchStart != null) {
  5229. // Start a drag if a finger is held
  5230. // in place long enough, then moved
  5231. isDragging = true;
  5232. }
  5233. }
  5234. };
  5235. }
  5236. dragTouchTimeout.schedule(TOUCHSCROLL_TIMEOUT);
  5237. }
  5238. if (actionKeys != null) {
  5239. if (contextTouchTimeout == null) {
  5240. contextTouchTimeout = new Timer() {
  5241. @Override
  5242. public void run() {
  5243. if (touchStart != null) {
  5244. // Open the context menu if finger
  5245. // is held in place long enough.
  5246. showContextMenu(touchStart);
  5247. event.preventDefault();
  5248. touchStart = null;
  5249. }
  5250. }
  5251. };
  5252. }
  5253. contextTouchTimeout
  5254. .schedule(TOUCH_CONTEXT_MENU_TIMEOUT);
  5255. event.stopPropagation();
  5256. }
  5257. break;
  5258. case Event.ONTOUCHMOVE:
  5259. touchEventHandled = true;
  5260. if (isSignificantMove(event)) {
  5261. if (contextTouchTimeout != null) {
  5262. // Moved finger before the context menu timer
  5263. // expired, so let the browser handle this as a
  5264. // scroll.
  5265. contextTouchTimeout.cancel();
  5266. contextTouchTimeout = null;
  5267. }
  5268. if (!isDragging && dragTouchTimeout != null) {
  5269. // Moved finger before the drag timer expired,
  5270. // so let the browser handle this as a scroll.
  5271. dragTouchTimeout.cancel();
  5272. dragTouchTimeout = null;
  5273. }
  5274. if (dragmode != 0 && touchStart != null
  5275. && isDragging) {
  5276. event.preventDefault();
  5277. event.stopPropagation();
  5278. startRowDrag(touchStart, type, targetTdOrTr);
  5279. }
  5280. touchStart = null;
  5281. }
  5282. break;
  5283. case Event.ONTOUCHEND:
  5284. case Event.ONTOUCHCANCEL:
  5285. touchEventHandled = true;
  5286. if (contextTouchTimeout != null) {
  5287. contextTouchTimeout.cancel();
  5288. }
  5289. if (dragTouchTimeout != null) {
  5290. dragTouchTimeout.cancel();
  5291. }
  5292. if (touchStart != null) {
  5293. if (!BrowserInfo.get().isAndroid()) {
  5294. event.preventDefault();
  5295. WidgetUtil.simulateClickFromTouchEvent(
  5296. touchStart, this);
  5297. }
  5298. event.stopPropagation();
  5299. touchStart = null;
  5300. }
  5301. isDragging = false;
  5302. break;
  5303. }
  5304. }
  5305. return touchEventHandled;
  5306. }
  5307. /*
  5308. * React on click that occur on content cells only
  5309. */
  5310. @Override
  5311. public void onBrowserEvent(final Event event) {
  5312. final boolean touchEventHandled = handleTouchEvent(event);
  5313. if (enabled && !touchEventHandled) {
  5314. final int type = event.getTypeInt();
  5315. final Element targetTdOrTr = getEventTargetTdOrTr(event);
  5316. if (type == Event.ONCONTEXTMENU) {
  5317. showContextMenu(event);
  5318. if (enabled
  5319. && (actionKeys != null || client
  5320. .hasEventListeners(
  5321. VScrollTable.this,
  5322. TableConstants.ITEM_CLICK_EVENT_ID))) {
  5323. /*
  5324. * Prevent browser context menu only if there are
  5325. * action handlers or item click listeners
  5326. * registered
  5327. */
  5328. event.stopPropagation();
  5329. event.preventDefault();
  5330. }
  5331. return;
  5332. }
  5333. boolean targetCellOrRowFound = targetTdOrTr != null;
  5334. switch (type) {
  5335. case Event.ONDBLCLICK:
  5336. if (targetCellOrRowFound) {
  5337. handleClickEvent(event, targetTdOrTr, true);
  5338. }
  5339. break;
  5340. case Event.ONMOUSEUP:
  5341. /*
  5342. * Only fire a click if the mouseup hits the same
  5343. * element as the corresponding mousedown. This is first
  5344. * checked in the event preview but we can't fire the
  5345. * event there as the event might get canceled before it
  5346. * gets here.
  5347. */
  5348. if (mouseUpPreviewMatched
  5349. && lastMouseDownTarget != null
  5350. && lastMouseDownTarget == getElementTdOrTr(WidgetUtil
  5351. .getElementUnderMouse(event))) {
  5352. // "Click" with left, right or middle button
  5353. if (targetCellOrRowFound) {
  5354. /*
  5355. * Queue here, send at the same time as the
  5356. * corresponding value change event - see #7127
  5357. */
  5358. boolean clickEventSent = handleClickEvent(
  5359. event, targetTdOrTr, false);
  5360. if (event.getButton() == Event.BUTTON_LEFT
  5361. && isSelectable()) {
  5362. // Ctrl+Shift click
  5363. if ((event.getCtrlKey() || event
  5364. .getMetaKey())
  5365. && event.getShiftKey()
  5366. && isMultiSelectModeDefault()) {
  5367. toggleShiftSelection(false);
  5368. setRowFocus(this);
  5369. // Ctrl click
  5370. } else if ((event.getCtrlKey() || event
  5371. .getMetaKey())
  5372. && isMultiSelectModeDefault()) {
  5373. boolean wasSelected = isSelected();
  5374. toggleSelection();
  5375. setRowFocus(this);
  5376. /*
  5377. * next possible range select must start
  5378. * on this row
  5379. */
  5380. selectionRangeStart = this;
  5381. if (wasSelected) {
  5382. removeRowFromUnsentSelectionRanges(this);
  5383. }
  5384. } else if ((event.getCtrlKey() || event
  5385. .getMetaKey())
  5386. && isSingleSelectMode()) {
  5387. // Ctrl (or meta) click (Single
  5388. // selection)
  5389. if (!isSelected()
  5390. || (isSelected() && nullSelectionAllowed)) {
  5391. if (!isSelected()) {
  5392. deselectAll();
  5393. }
  5394. toggleSelection();
  5395. setRowFocus(this);
  5396. }
  5397. } else if (event.getShiftKey()
  5398. && isMultiSelectModeDefault()) {
  5399. // Shift click
  5400. toggleShiftSelection(true);
  5401. } else {
  5402. // click
  5403. boolean currentlyJustThisRowSelected = selectedRowKeys
  5404. .size() == 1
  5405. && selectedRowKeys
  5406. .contains(getKey());
  5407. if (!currentlyJustThisRowSelected) {
  5408. if (isSingleSelectMode()
  5409. || isMultiSelectModeDefault()) {
  5410. /*
  5411. * For default multi select mode
  5412. * (ctrl/shift) and for single
  5413. * select mode we need to clear
  5414. * the previous selection before
  5415. * selecting a new one when the
  5416. * user clicks on a row. Only in
  5417. * multiselect/simple mode the
  5418. * old selection should remain
  5419. * after a normal click.
  5420. */
  5421. deselectAll();
  5422. }
  5423. toggleSelection();
  5424. } else if ((isSingleSelectMode() || isMultiSelectModeSimple())
  5425. && nullSelectionAllowed) {
  5426. toggleSelection();
  5427. }/*
  5428. * else NOP to avoid excessive server
  5429. * visits (selection is removed with
  5430. * CTRL/META click)
  5431. */
  5432. selectionRangeStart = this;
  5433. setRowFocus(this);
  5434. }
  5435. // Remove IE text selection hack
  5436. if (BrowserInfo.get().isIE()) {
  5437. ((Element) event.getEventTarget()
  5438. .cast()).setPropertyJSO(
  5439. "onselectstart", null);
  5440. }
  5441. // Queue value change
  5442. sendSelectedRows(false);
  5443. }
  5444. /*
  5445. * Send queued click and value change events if
  5446. * any If a click event is sent, send value
  5447. * change with it regardless of the immediate
  5448. * flag, see #7127
  5449. */
  5450. if (immediate || clickEventSent) {
  5451. client.sendPendingVariableChanges();
  5452. }
  5453. }
  5454. }
  5455. mouseUpPreviewMatched = false;
  5456. lastMouseDownTarget = null;
  5457. break;
  5458. case Event.ONTOUCHEND:
  5459. case Event.ONTOUCHCANCEL:
  5460. if (touchStart != null) {
  5461. /*
  5462. * Touch has not been handled as neither context or
  5463. * drag start, handle it as a click.
  5464. */
  5465. WidgetUtil.simulateClickFromTouchEvent(touchStart,
  5466. this);
  5467. touchStart = null;
  5468. }
  5469. touchContextProvider.cancel();
  5470. break;
  5471. case Event.ONTOUCHMOVE:
  5472. if (isSignificantMove(event)) {
  5473. /*
  5474. * TODO figure out scroll delegate don't eat events
  5475. * if row is selected. Null check for active
  5476. * delegate is as a workaround.
  5477. */
  5478. if (dragmode != 0
  5479. && touchStart != null
  5480. && (TouchScrollDelegate
  5481. .getActiveScrollDelegate() == null)) {
  5482. startRowDrag(touchStart, type, targetTdOrTr);
  5483. }
  5484. touchContextProvider.cancel();
  5485. /*
  5486. * Avoid clicks and drags by clearing touch start
  5487. * flag.
  5488. */
  5489. touchStart = null;
  5490. }
  5491. break;
  5492. case Event.ONTOUCHSTART:
  5493. touchStart = event;
  5494. Touch touch = event.getChangedTouches().get(0);
  5495. // save position to fields, touches in events are same
  5496. // instance during the operation.
  5497. touchStartX = touch.getClientX();
  5498. touchStartY = touch.getClientY();
  5499. /*
  5500. * Prevent simulated mouse events.
  5501. */
  5502. touchStart.preventDefault();
  5503. if (dragmode != 0 || actionKeys != null) {
  5504. new Timer() {
  5505. @Override
  5506. public void run() {
  5507. TouchScrollDelegate activeScrollDelegate = TouchScrollDelegate
  5508. .getActiveScrollDelegate();
  5509. /*
  5510. * If there's a scroll delegate, check if
  5511. * we're actually scrolling and handle it.
  5512. * If no delegate, do nothing here and let
  5513. * the row handle potential drag'n'drop or
  5514. * context menu.
  5515. */
  5516. if (activeScrollDelegate != null) {
  5517. if (activeScrollDelegate.isMoved()) {
  5518. /*
  5519. * Prevent the row from handling
  5520. * touch move/end events (the
  5521. * delegate handles those) and from
  5522. * doing drag'n'drop or opening a
  5523. * context menu.
  5524. */
  5525. touchStart = null;
  5526. } else {
  5527. /*
  5528. * Scrolling hasn't started, so
  5529. * cancel delegate and let the row
  5530. * handle potential drag'n'drop or
  5531. * context menu.
  5532. */
  5533. activeScrollDelegate
  5534. .stopScrolling();
  5535. }
  5536. }
  5537. }
  5538. }.schedule(TOUCHSCROLL_TIMEOUT);
  5539. if (contextTouchTimeout == null
  5540. && actionKeys != null) {
  5541. contextTouchTimeout = new Timer() {
  5542. @Override
  5543. public void run() {
  5544. if (touchStart != null) {
  5545. showContextMenu(touchStart);
  5546. touchStart = null;
  5547. }
  5548. }
  5549. };
  5550. }
  5551. if (contextTouchTimeout != null) {
  5552. contextTouchTimeout.cancel();
  5553. contextTouchTimeout
  5554. .schedule(TOUCH_CONTEXT_MENU_TIMEOUT);
  5555. }
  5556. }
  5557. break;
  5558. case Event.ONMOUSEDOWN:
  5559. /*
  5560. * When getting a mousedown event, we must detect where
  5561. * the corresponding mouseup event if it's on a
  5562. * different part of the page.
  5563. */
  5564. lastMouseDownTarget = getElementTdOrTr(WidgetUtil
  5565. .getElementUnderMouse(event));
  5566. mouseUpPreviewMatched = false;
  5567. mouseUpEventPreviewRegistration = Event
  5568. .addNativePreviewHandler(mouseUpPreviewHandler);
  5569. if (targetCellOrRowFound) {
  5570. setRowFocus(this);
  5571. ensureFocus();
  5572. if (dragmode != 0
  5573. && (event.getButton() == NativeEvent.BUTTON_LEFT)) {
  5574. startRowDrag(event, type, targetTdOrTr);
  5575. } else if (event.getCtrlKey()
  5576. || event.getShiftKey()
  5577. || event.getMetaKey()
  5578. && isMultiSelectModeDefault()) {
  5579. // Prevent default text selection in Firefox
  5580. event.preventDefault();
  5581. // Prevent default text selection in IE
  5582. if (BrowserInfo.get().isIE()) {
  5583. ((Element) event.getEventTarget().cast())
  5584. .setPropertyJSO(
  5585. "onselectstart",
  5586. getPreventTextSelectionIEHack());
  5587. }
  5588. event.stopPropagation();
  5589. }
  5590. }
  5591. break;
  5592. case Event.ONMOUSEOUT:
  5593. break;
  5594. default:
  5595. break;
  5596. }
  5597. }
  5598. super.onBrowserEvent(event);
  5599. }
  5600. private boolean isSignificantMove(Event event) {
  5601. if (touchStart == null) {
  5602. // no touch start
  5603. return false;
  5604. }
  5605. /*
  5606. * TODO calculate based on real distance instead of separate
  5607. * axis checks
  5608. */
  5609. Touch touch = event.getChangedTouches().get(0);
  5610. if (Math.abs(touch.getClientX() - touchStartX) > TouchScrollDelegate.SIGNIFICANT_MOVE_THRESHOLD) {
  5611. return true;
  5612. }
  5613. if (Math.abs(touch.getClientY() - touchStartY) > TouchScrollDelegate.SIGNIFICANT_MOVE_THRESHOLD) {
  5614. return true;
  5615. }
  5616. return false;
  5617. }
  5618. /**
  5619. * Checks if the row represented by the row key has been selected
  5620. *
  5621. * @param key
  5622. * The generated row key
  5623. */
  5624. private boolean rowKeyIsSelected(int rowKey) {
  5625. // Check single selections
  5626. if (selectedRowKeys.contains("" + rowKey)) {
  5627. return true;
  5628. }
  5629. // Check range selections
  5630. for (SelectionRange r : selectedRowRanges) {
  5631. if (r.inRange(getRenderedRowByKey("" + rowKey))) {
  5632. return true;
  5633. }
  5634. }
  5635. return false;
  5636. }
  5637. protected void startRowDrag(Event event, final int type,
  5638. Element targetTdOrTr) {
  5639. VTransferable transferable = new VTransferable();
  5640. transferable.setDragSource(ConnectorMap.get(client)
  5641. .getConnector(VScrollTable.this));
  5642. transferable.setData("itemId", "" + rowKey);
  5643. NodeList<TableCellElement> cells = rowElement.getCells();
  5644. for (int i = 0; i < cells.getLength(); i++) {
  5645. if (cells.getItem(i).isOrHasChild(targetTdOrTr)) {
  5646. HeaderCell headerCell = tHead.getHeaderCell(i);
  5647. transferable.setData("propertyId", headerCell.cid);
  5648. break;
  5649. }
  5650. }
  5651. VDragEvent ev = VDragAndDropManager.get().startDrag(
  5652. transferable, event, true);
  5653. if (dragmode == DRAGMODE_MULTIROW && isMultiSelectModeAny()
  5654. && rowKeyIsSelected(rowKey)) {
  5655. // Create a drag image of ALL rows
  5656. ev.createDragImage(scrollBody.tBodyElement, true);
  5657. // Hide rows which are not selected
  5658. Element dragImage = ev.getDragImage();
  5659. int i = 0;
  5660. for (Iterator<Widget> iterator = scrollBody.iterator(); iterator
  5661. .hasNext();) {
  5662. VScrollTableRow next = (VScrollTableRow) iterator
  5663. .next();
  5664. Element child = (Element) dragImage.getChild(i++);
  5665. if (!rowKeyIsSelected(next.rowKey)) {
  5666. child.getStyle().setVisibility(Visibility.HIDDEN);
  5667. }
  5668. }
  5669. } else {
  5670. ev.createDragImage(getElement(), true);
  5671. }
  5672. if (type == Event.ONMOUSEDOWN) {
  5673. event.preventDefault();
  5674. }
  5675. event.stopPropagation();
  5676. }
  5677. /**
  5678. * Finds the TD that the event interacts with. Returns null if the
  5679. * target of the event should not be handled. If the event target is
  5680. * the row directly this method returns the TR element instead of
  5681. * the TD.
  5682. *
  5683. * @param event
  5684. * @return TD or TR element that the event targets (the actual event
  5685. * target is this element or a child of it)
  5686. */
  5687. private Element getEventTargetTdOrTr(Event event) {
  5688. final Element eventTarget = event.getEventTarget().cast();
  5689. return getElementTdOrTr(eventTarget);
  5690. }
  5691. private Element getElementTdOrTr(Element element) {
  5692. Widget widget = WidgetUtil.findWidget(element, null);
  5693. if (widget != this) {
  5694. /*
  5695. * This is a workaround to make Labels, read only TextFields
  5696. * and Embedded in a Table clickable (see #2688). It is
  5697. * really not a fix as it does not work with a custom read
  5698. * only components (not extending VLabel/VEmbedded).
  5699. */
  5700. while (widget != null && widget.getParent() != this) {
  5701. widget = widget.getParent();
  5702. }
  5703. if (!(widget instanceof VLabel)
  5704. && !(widget instanceof VEmbedded)
  5705. && !(widget instanceof VTextField && ((VTextField) widget)
  5706. .isReadOnly())) {
  5707. return null;
  5708. }
  5709. }
  5710. return getTdOrTr(element);
  5711. }
  5712. @Override
  5713. public void showContextMenu(Event event) {
  5714. if (enabled && actionKeys != null) {
  5715. // Show context menu if there are registered action handlers
  5716. int left = WidgetUtil.getTouchOrMouseClientX(event)
  5717. + Window.getScrollLeft();
  5718. int top = WidgetUtil.getTouchOrMouseClientY(event)
  5719. + Window.getScrollTop();
  5720. showContextMenu(left, top);
  5721. }
  5722. }
  5723. public void showContextMenu(int left, int top) {
  5724. VContextMenu menu = client.getContextMenu();
  5725. contextMenu = new ContextMenuDetails(menu, getKey(), left, top);
  5726. menu.showAt(this, left, top);
  5727. }
  5728. /**
  5729. * Has the row been selected?
  5730. *
  5731. * @return Returns true if selected, else false
  5732. */
  5733. public boolean isSelected() {
  5734. return selected;
  5735. }
  5736. /**
  5737. * Toggle the selection of the row
  5738. */
  5739. public void toggleSelection() {
  5740. selected = !selected;
  5741. selectionChanged = true;
  5742. if (selected) {
  5743. selectedRowKeys.add(String.valueOf(rowKey));
  5744. addStyleName("v-selected");
  5745. } else {
  5746. removeStyleName("v-selected");
  5747. selectedRowKeys.remove(String.valueOf(rowKey));
  5748. }
  5749. }
  5750. /**
  5751. * Is called when a user clicks an item when holding SHIFT key down.
  5752. * This will select a new range from the last focused row
  5753. *
  5754. * @param deselectPrevious
  5755. * Should the previous selected range be deselected
  5756. */
  5757. private void toggleShiftSelection(boolean deselectPrevious) {
  5758. /*
  5759. * Ensures that we are in multiselect mode and that we have a
  5760. * previous selection which was not a deselection
  5761. */
  5762. if (isSingleSelectMode()) {
  5763. // No previous selection found
  5764. deselectAll();
  5765. toggleSelection();
  5766. return;
  5767. }
  5768. // Set the selectable range
  5769. VScrollTableRow endRow = this;
  5770. VScrollTableRow startRow = selectionRangeStart;
  5771. if (startRow == null) {
  5772. startRow = focusedRow;
  5773. selectionRangeStart = focusedRow;
  5774. // If start row is null then we have a multipage selection
  5775. // from above
  5776. if (startRow == null) {
  5777. startRow = (VScrollTableRow) scrollBody.iterator()
  5778. .next();
  5779. setRowFocus(endRow);
  5780. }
  5781. } else if (!startRow.isSelected()) {
  5782. // The start row is no longer selected (probably removed)
  5783. // and so we select from above
  5784. startRow = (VScrollTableRow) scrollBody.iterator().next();
  5785. setRowFocus(endRow);
  5786. }
  5787. // Deselect previous items if so desired
  5788. if (deselectPrevious) {
  5789. deselectAll();
  5790. }
  5791. // we'll ensure GUI state from top down even though selection
  5792. // was the opposite way
  5793. if (!startRow.isBefore(endRow)) {
  5794. VScrollTableRow tmp = startRow;
  5795. startRow = endRow;
  5796. endRow = tmp;
  5797. }
  5798. SelectionRange range = new SelectionRange(startRow, endRow);
  5799. for (Widget w : scrollBody) {
  5800. VScrollTableRow row = (VScrollTableRow) w;
  5801. if (range.inRange(row)) {
  5802. if (!row.isSelected()) {
  5803. row.toggleSelection();
  5804. }
  5805. selectedRowKeys.add(row.getKey());
  5806. }
  5807. }
  5808. // Add range
  5809. if (startRow != endRow) {
  5810. selectedRowRanges.add(range);
  5811. }
  5812. }
  5813. /*
  5814. * (non-Javadoc)
  5815. *
  5816. * @see com.vaadin.client.ui.IActionOwner#getActions ()
  5817. */
  5818. @Override
  5819. public Action[] getActions() {
  5820. if (actionKeys == null) {
  5821. return new Action[] {};
  5822. }
  5823. final Action[] actions = new Action[actionKeys.length];
  5824. for (int i = 0; i < actions.length; i++) {
  5825. final String actionKey = actionKeys[i];
  5826. final TreeAction a = new TreeAction(this,
  5827. String.valueOf(rowKey), actionKey) {
  5828. @Override
  5829. public void execute() {
  5830. super.execute();
  5831. lazyRevertFocusToRow(VScrollTableRow.this);
  5832. }
  5833. };
  5834. a.setCaption(getActionCaption(actionKey));
  5835. a.setIconUrl(getActionIcon(actionKey));
  5836. actions[i] = a;
  5837. }
  5838. return actions;
  5839. }
  5840. @Override
  5841. public ApplicationConnection getClient() {
  5842. return client;
  5843. }
  5844. @Override
  5845. public String getPaintableId() {
  5846. return paintableId;
  5847. }
  5848. private int getColIndexOf(Widget child) {
  5849. com.google.gwt.dom.client.Element widgetCell = child
  5850. .getElement().getParentElement().getParentElement();
  5851. NodeList<TableCellElement> cells = rowElement.getCells();
  5852. for (int i = 0; i < cells.getLength(); i++) {
  5853. if (cells.getItem(i) == widgetCell) {
  5854. return i;
  5855. }
  5856. }
  5857. return -1;
  5858. }
  5859. public Widget getWidgetForPaintable() {
  5860. return this;
  5861. }
  5862. }
  5863. protected class VScrollTableGeneratedRow extends VScrollTableRow {
  5864. private boolean spanColumns;
  5865. private boolean htmlContentAllowed;
  5866. public VScrollTableGeneratedRow(UIDL uidl, char[] aligns) {
  5867. super(uidl, aligns);
  5868. addStyleName("v-table-generated-row");
  5869. }
  5870. public boolean isSpanColumns() {
  5871. return spanColumns;
  5872. }
  5873. @Override
  5874. protected void initCellWidths() {
  5875. if (spanColumns) {
  5876. setSpannedColumnWidthAfterDOMFullyInited();
  5877. } else {
  5878. super.initCellWidths();
  5879. }
  5880. }
  5881. private void setSpannedColumnWidthAfterDOMFullyInited() {
  5882. // Defer setting width on spanned columns to make sure that
  5883. // they are added to the DOM before trying to calculate
  5884. // widths.
  5885. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  5886. @Override
  5887. public void execute() {
  5888. if (showRowHeaders) {
  5889. setCellWidth(0, tHead.getHeaderCell(0)
  5890. .getWidthWithIndent());
  5891. calcAndSetSpanWidthOnCell(1);
  5892. } else {
  5893. calcAndSetSpanWidthOnCell(0);
  5894. }
  5895. }
  5896. });
  5897. }
  5898. @Override
  5899. protected boolean isRenderHtmlInCells() {
  5900. return htmlContentAllowed;
  5901. }
  5902. @Override
  5903. protected void addCellsFromUIDL(UIDL uidl, char[] aligns, int col,
  5904. int visibleColumnIndex) {
  5905. htmlContentAllowed = uidl.getBooleanAttribute("gen_html");
  5906. spanColumns = uidl.getBooleanAttribute("gen_span");
  5907. final Iterator<?> cells = uidl.getChildIterator();
  5908. if (spanColumns) {
  5909. int colCount = uidl.getChildCount();
  5910. if (cells.hasNext()) {
  5911. final Object cell = cells.next();
  5912. if (cell instanceof String) {
  5913. addSpannedCell(uidl, cell.toString(), aligns[0],
  5914. "", htmlContentAllowed, false, null,
  5915. colCount);
  5916. } else {
  5917. addSpannedCell(uidl, (Widget) cell, aligns[0], "",
  5918. false, colCount);
  5919. }
  5920. }
  5921. } else {
  5922. super.addCellsFromUIDL(uidl, aligns, col,
  5923. visibleColumnIndex);
  5924. }
  5925. }
  5926. private void addSpannedCell(UIDL rowUidl, Widget w, char align,
  5927. String style, boolean sorted, int colCount) {
  5928. TableCellElement td = DOM.createTD().cast();
  5929. td.setColSpan(colCount);
  5930. initCellWithWidget(w, align, style, sorted, td);
  5931. }
  5932. private void addSpannedCell(UIDL rowUidl, String text, char align,
  5933. String style, boolean textIsHTML, boolean sorted,
  5934. String description, int colCount) {
  5935. // String only content is optimized by not using Label widget
  5936. final TableCellElement td = DOM.createTD().cast();
  5937. td.setColSpan(colCount);
  5938. initCellWithText(text, align, style, textIsHTML, sorted,
  5939. description, td);
  5940. }
  5941. @Override
  5942. protected void setCellWidth(int cellIx, int width) {
  5943. if (isSpanColumns()) {
  5944. if (showRowHeaders) {
  5945. if (cellIx == 0) {
  5946. super.setCellWidth(0, width);
  5947. } else {
  5948. // We need to recalculate the spanning TDs width for
  5949. // every cellIx in order to support column resizing.
  5950. calcAndSetSpanWidthOnCell(1);
  5951. }
  5952. } else {
  5953. // Same as above.
  5954. calcAndSetSpanWidthOnCell(0);
  5955. }
  5956. } else {
  5957. super.setCellWidth(cellIx, width);
  5958. }
  5959. }
  5960. private void calcAndSetSpanWidthOnCell(final int cellIx) {
  5961. int spanWidth = 0;
  5962. for (int ix = (showRowHeaders ? 1 : 0); ix < tHead
  5963. .getVisibleCellCount(); ix++) {
  5964. spanWidth += tHead.getHeaderCell(ix).getOffsetWidth();
  5965. }
  5966. WidgetUtil.setWidthExcludingPaddingAndBorder(
  5967. (Element) getElement().getChild(cellIx), spanWidth, 13,
  5968. false);
  5969. }
  5970. }
  5971. /**
  5972. * Ensure the component has a focus.
  5973. *
  5974. * TODO the current implementation simply always calls focus for the
  5975. * component. In case the Table at some point implements focus/blur
  5976. * listeners, this method needs to be evolved to conditionally call
  5977. * focus only if not currently focused.
  5978. */
  5979. protected void ensureFocus() {
  5980. if (!hasFocus) {
  5981. scrollBodyPanel.setFocus(true);
  5982. }
  5983. }
  5984. }
  5985. /**
  5986. * Deselects all items
  5987. */
  5988. public void deselectAll() {
  5989. for (Widget w : scrollBody) {
  5990. VScrollTableRow row = (VScrollTableRow) w;
  5991. if (row.isSelected()) {
  5992. row.toggleSelection();
  5993. }
  5994. }
  5995. // still ensure all selects are removed from (not necessary rendered)
  5996. selectedRowKeys.clear();
  5997. selectedRowRanges.clear();
  5998. // also notify server that it clears all previous selections (the client
  5999. // side does not know about the invisible ones)
  6000. instructServerToForgetPreviousSelections();
  6001. }
  6002. /**
  6003. * Used in multiselect mode when the client side knows that all selections
  6004. * are in the next request.
  6005. */
  6006. private void instructServerToForgetPreviousSelections() {
  6007. client.updateVariable(paintableId, "clearSelections", true, false);
  6008. }
  6009. /**
  6010. * Determines the pagelength when the table height is fixed.
  6011. */
  6012. public void updatePageLength() {
  6013. // Only update if visible and enabled
  6014. if (!isVisible() || !enabled) {
  6015. return;
  6016. }
  6017. if (scrollBody == null) {
  6018. return;
  6019. }
  6020. if (isDynamicHeight()) {
  6021. return;
  6022. }
  6023. int rowHeight = (int) Math.round(scrollBody.getRowHeight());
  6024. int bodyH = scrollBodyPanel.getOffsetHeight();
  6025. int rowsAtOnce = bodyH / rowHeight;
  6026. boolean anotherPartlyVisible = ((bodyH % rowHeight) != 0);
  6027. if (anotherPartlyVisible) {
  6028. rowsAtOnce++;
  6029. }
  6030. if (pageLength != rowsAtOnce) {
  6031. pageLength = rowsAtOnce;
  6032. client.updateVariable(paintableId, "pagelength", pageLength, false);
  6033. if (!rendering) {
  6034. int currentlyVisible = scrollBody.getLastRendered()
  6035. - scrollBody.getFirstRendered();
  6036. if (currentlyVisible < pageLength
  6037. && currentlyVisible < totalRows) {
  6038. // shake scrollpanel to fill empty space
  6039. scrollBodyPanel.setScrollPosition(scrollTop + 1);
  6040. scrollBodyPanel.setScrollPosition(scrollTop - 1);
  6041. }
  6042. sizeNeedsInit = true;
  6043. }
  6044. }
  6045. }
  6046. /** For internal use only. May be removed or replaced in the future. */
  6047. public void updateWidth() {
  6048. if (!isVisible()) {
  6049. /*
  6050. * Do not update size when the table is hidden as all column widths
  6051. * will be set to zero and they won't be recalculated when the table
  6052. * is set visible again (until the size changes again)
  6053. */
  6054. return;
  6055. }
  6056. if (!isDynamicWidth()) {
  6057. int innerPixels = getOffsetWidth() - getBorderWidth();
  6058. if (innerPixels < 0) {
  6059. innerPixels = 0;
  6060. }
  6061. setContentWidth(innerPixels);
  6062. // readjust undefined width columns
  6063. triggerLazyColumnAdjustment(false);
  6064. } else {
  6065. sizeNeedsInit = true;
  6066. // readjust undefined width columns
  6067. triggerLazyColumnAdjustment(false);
  6068. }
  6069. /*
  6070. * setting width may affect wheter the component has scrollbars -> needs
  6071. * scrolling or not
  6072. */
  6073. setProperTabIndex();
  6074. }
  6075. private static final int LAZY_COLUMN_ADJUST_TIMEOUT = 300;
  6076. private final Timer lazyAdjustColumnWidths = new Timer() {
  6077. /**
  6078. * Check for column widths, and available width, to see if we can fix
  6079. * column widths "optimally". Doing this lazily to avoid expensive
  6080. * calculation when resizing is not yet finished.
  6081. */
  6082. @Override
  6083. public void run() {
  6084. if (scrollBody == null) {
  6085. // Try again later if we get here before scrollBody has been
  6086. // initalized
  6087. triggerLazyColumnAdjustment(false);
  6088. return;
  6089. }
  6090. Iterator<Widget> headCells = tHead.iterator();
  6091. int usedMinimumWidth = 0;
  6092. int totalExplicitColumnsWidths = 0;
  6093. float expandRatioDivider = 0;
  6094. int colIndex = 0;
  6095. int hierarchyColumnIndent = scrollBody.getMaxIndent();
  6096. int hierarchyColumnIndex = getHierarchyColumnIndex();
  6097. HeaderCell hierarchyHeaderInNeedOfFurtherHandling = null;
  6098. while (headCells.hasNext()) {
  6099. final HeaderCell hCell = (HeaderCell) headCells.next();
  6100. boolean hasIndent = hierarchyColumnIndent > 0
  6101. && hCell.isHierarchyColumn();
  6102. if (hCell.isDefinedWidth()) {
  6103. // get width without indent to find out whether adjustments
  6104. // are needed (requires special handling further ahead)
  6105. int w = hCell.getWidth();
  6106. if (hasIndent && w < hierarchyColumnIndent) {
  6107. // enforce indent if necessary
  6108. w = hierarchyColumnIndent;
  6109. hierarchyHeaderInNeedOfFurtherHandling = hCell;
  6110. }
  6111. totalExplicitColumnsWidths += w;
  6112. usedMinimumWidth += w;
  6113. } else {
  6114. // natural width already includes indent if any
  6115. int naturalColumnWidth = hCell
  6116. .getNaturalColumnWidth(colIndex);
  6117. /*
  6118. * TODO If there is extra width, expand ratios are for
  6119. * additional extra widths, not for absolute column widths.
  6120. * Should be fixed in sizeInit(), too.
  6121. */
  6122. if (hCell.getExpandRatio() > 0) {
  6123. naturalColumnWidth = 0;
  6124. }
  6125. usedMinimumWidth += naturalColumnWidth;
  6126. expandRatioDivider += hCell.getExpandRatio();
  6127. if (hasIndent) {
  6128. hierarchyHeaderInNeedOfFurtherHandling = hCell;
  6129. }
  6130. }
  6131. colIndex++;
  6132. }
  6133. int availW = scrollBody.getAvailableWidth();
  6134. // Hey IE, are you really sure about this?
  6135. availW = scrollBody.getAvailableWidth();
  6136. int visibleCellCount = tHead.getVisibleCellCount();
  6137. int totalExtraWidth = scrollBody.getCellExtraWidth()
  6138. * visibleCellCount;
  6139. if (willHaveScrollbars()) {
  6140. totalExtraWidth += WidgetUtil.getNativeScrollbarSize();
  6141. // if there will be vertical scrollbar, let's enable it
  6142. scrollBodyPanel.getElement().getStyle().clearOverflowY();
  6143. } else {
  6144. // if there is no need for vertical scrollbar, let's disable it
  6145. // this is necessary since sometimes the browsers insist showing
  6146. // the scrollbar even if the content would fit perfectly
  6147. scrollBodyPanel.getElement().getStyle()
  6148. .setOverflowY(Overflow.HIDDEN);
  6149. }
  6150. availW -= totalExtraWidth;
  6151. int forceScrollBodyWidth = -1;
  6152. int extraSpace = availW - usedMinimumWidth;
  6153. if (extraSpace < 0) {
  6154. if (getTotalRows() == 0) {
  6155. /*
  6156. * Too wide header combined with no rows in the table.
  6157. *
  6158. * No horizontal scrollbars would be displayed because
  6159. * there's no rows that grows too wide causing the
  6160. * scrollBody container div to overflow. Must explicitely
  6161. * force a width to a scrollbar. (see #9187)
  6162. */
  6163. forceScrollBodyWidth = usedMinimumWidth + totalExtraWidth;
  6164. }
  6165. extraSpace = 0;
  6166. // if there will be horizontal scrollbar, let's enable it
  6167. scrollBodyPanel.getElement().getStyle().clearOverflowX();
  6168. } else {
  6169. // if there is no need for horizontal scrollbar, let's disable
  6170. // it
  6171. // this is necessary since sometimes the browsers insist showing
  6172. // the scrollbar even if the content would fit perfectly
  6173. scrollBodyPanel.getElement().getStyle()
  6174. .setOverflowX(Overflow.HIDDEN);
  6175. }
  6176. if (forceScrollBodyWidth > 0) {
  6177. scrollBody.container.getStyle().setWidth(forceScrollBodyWidth,
  6178. Unit.PX);
  6179. } else {
  6180. // Clear width that might have been set to force horizontal
  6181. // scrolling if there are no rows
  6182. scrollBody.container.getStyle().clearWidth();
  6183. }
  6184. int totalUndefinedNaturalWidths = usedMinimumWidth
  6185. - totalExplicitColumnsWidths;
  6186. if (hierarchyHeaderInNeedOfFurtherHandling != null
  6187. && !hierarchyHeaderInNeedOfFurtherHandling.isDefinedWidth()) {
  6188. // ensure the cell gets enough space for the indent
  6189. int w = hierarchyHeaderInNeedOfFurtherHandling
  6190. .getNaturalColumnWidth(hierarchyColumnIndex);
  6191. int newSpace = Math.round(w + (float) extraSpace * (float) w
  6192. / totalUndefinedNaturalWidths);
  6193. if (newSpace >= hierarchyColumnIndent) {
  6194. // no special handling required
  6195. hierarchyHeaderInNeedOfFurtherHandling = null;
  6196. } else {
  6197. // treat as a defined width column of indent's width
  6198. totalExplicitColumnsWidths += hierarchyColumnIndent;
  6199. usedMinimumWidth -= w - hierarchyColumnIndent;
  6200. totalUndefinedNaturalWidths = usedMinimumWidth
  6201. - totalExplicitColumnsWidths;
  6202. expandRatioDivider += hierarchyHeaderInNeedOfFurtherHandling
  6203. .getExpandRatio();
  6204. extraSpace = Math.max(availW - usedMinimumWidth, 0);
  6205. }
  6206. }
  6207. // we have some space that can be divided optimally
  6208. HeaderCell hCell;
  6209. colIndex = 0;
  6210. headCells = tHead.iterator();
  6211. int checksum = 0;
  6212. while (headCells.hasNext()) {
  6213. hCell = (HeaderCell) headCells.next();
  6214. if (hCell.isResizing) {
  6215. continue;
  6216. }
  6217. if (!hCell.isDefinedWidth()) {
  6218. int w = hCell.getNaturalColumnWidth(colIndex);
  6219. int newSpace;
  6220. if (expandRatioDivider > 0) {
  6221. // divide excess space by expand ratios
  6222. if (hCell.getExpandRatio() > 0) {
  6223. w = 0;
  6224. }
  6225. newSpace = Math.round((w + extraSpace
  6226. * hCell.getExpandRatio() / expandRatioDivider));
  6227. } else {
  6228. if (hierarchyHeaderInNeedOfFurtherHandling == hCell) {
  6229. // still exists, so needs exactly the indent's width
  6230. newSpace = hierarchyColumnIndent;
  6231. } else if (totalUndefinedNaturalWidths != 0) {
  6232. // divide relatively to natural column widths
  6233. newSpace = Math.round(w + (float) extraSpace
  6234. * (float) w / totalUndefinedNaturalWidths);
  6235. } else {
  6236. newSpace = w;
  6237. }
  6238. }
  6239. checksum += newSpace;
  6240. setColWidth(colIndex, newSpace, false);
  6241. } else {
  6242. if (hierarchyHeaderInNeedOfFurtherHandling == hCell) {
  6243. // defined with enforced into indent width
  6244. checksum += hierarchyColumnIndent;
  6245. setColWidth(colIndex, hierarchyColumnIndent, false);
  6246. } else {
  6247. int cellWidth = hCell.getWidthWithIndent();
  6248. checksum += cellWidth;
  6249. if (hCell.isHierarchyColumn()) {
  6250. // update in case the indent has changed
  6251. // (not detectable earlier)
  6252. setColWidth(colIndex, cellWidth, true);
  6253. }
  6254. }
  6255. }
  6256. colIndex++;
  6257. }
  6258. if (extraSpace > 0 && checksum != availW) {
  6259. /*
  6260. * There might be in some cases a rounding error of 1px when
  6261. * extra space is divided so if there is one then we give the
  6262. * first undefined column 1 more pixel
  6263. */
  6264. headCells = tHead.iterator();
  6265. colIndex = 0;
  6266. while (headCells.hasNext()) {
  6267. HeaderCell hc = (HeaderCell) headCells.next();
  6268. if (!hc.isResizing && !hc.isDefinedWidth()) {
  6269. setColWidth(colIndex, hc.getWidthWithIndent() + availW
  6270. - checksum, false);
  6271. break;
  6272. }
  6273. colIndex++;
  6274. }
  6275. }
  6276. if (isDynamicHeight() && totalRows == pageLength) {
  6277. // fix body height (may vary if lazy loading is offhorizontal
  6278. // scrollbar appears/disappears)
  6279. int bodyHeight = scrollBody.getRequiredHeight();
  6280. boolean needsSpaceForHorizontalScrollbar = (availW < usedMinimumWidth);
  6281. if (needsSpaceForHorizontalScrollbar) {
  6282. bodyHeight += WidgetUtil.getNativeScrollbarSize();
  6283. }
  6284. int heightBefore = getOffsetHeight();
  6285. scrollBodyPanel.setHeight(bodyHeight + "px");
  6286. if (heightBefore != getOffsetHeight()) {
  6287. Util.notifyParentOfSizeChange(VScrollTable.this, rendering);
  6288. }
  6289. }
  6290. forceRealignColumnHeaders();
  6291. }
  6292. };
  6293. private void forceRealignColumnHeaders() {
  6294. if (BrowserInfo.get().isIE()) {
  6295. /*
  6296. * IE does not fire onscroll event if scroll position is reverted to
  6297. * 0 due to the content element size growth. Ensure headers are in
  6298. * sync with content manually. Safe to use null event as we don't
  6299. * actually use the event object in listener.
  6300. */
  6301. onScroll(null);
  6302. }
  6303. }
  6304. /**
  6305. * helper to set pixel size of head and body part
  6306. *
  6307. * @param pixels
  6308. */
  6309. private void setContentWidth(int pixels) {
  6310. tHead.setWidth(pixels + "px");
  6311. scrollBodyPanel.setWidth(pixels + "px");
  6312. tFoot.setWidth(pixels + "px");
  6313. }
  6314. private int borderWidth = -1;
  6315. /**
  6316. * @return border left + border right
  6317. */
  6318. private int getBorderWidth() {
  6319. if (borderWidth < 0) {
  6320. borderWidth = WidgetUtil.measureHorizontalPaddingAndBorder(
  6321. scrollBodyPanel.getElement(), 2);
  6322. if (borderWidth < 0) {
  6323. borderWidth = 0;
  6324. }
  6325. }
  6326. return borderWidth;
  6327. }
  6328. /**
  6329. * Ensures scrollable area is properly sized. This method is used when fixed
  6330. * size is used.
  6331. */
  6332. private int containerHeight;
  6333. private void setContainerHeight() {
  6334. if (!isDynamicHeight()) {
  6335. /*
  6336. * Android 2.3 cannot measure the height of the inline-block
  6337. * properly, and will return the wrong offset height. So for android
  6338. * 2.3 we set the element to a block element while measuring and
  6339. * then restore it which yields the correct result. #11331
  6340. */
  6341. if (BrowserInfo.get().isAndroid23()) {
  6342. getElement().getStyle().setDisplay(Display.BLOCK);
  6343. }
  6344. containerHeight = getOffsetHeight();
  6345. containerHeight -= showColHeaders ? tHead.getOffsetHeight() : 0;
  6346. containerHeight -= tFoot.getOffsetHeight();
  6347. containerHeight -= getContentAreaBorderHeight();
  6348. if (containerHeight < 0) {
  6349. containerHeight = 0;
  6350. }
  6351. scrollBodyPanel.setHeight(containerHeight + "px");
  6352. if (BrowserInfo.get().isAndroid23()) {
  6353. getElement().getStyle().clearDisplay();
  6354. }
  6355. }
  6356. }
  6357. private int contentAreaBorderHeight = -1;
  6358. private int scrollLeft;
  6359. private int scrollTop;
  6360. /** For internal use only. May be removed or replaced in the future. */
  6361. public VScrollTableDropHandler dropHandler;
  6362. private boolean navKeyDown;
  6363. /** For internal use only. May be removed or replaced in the future. */
  6364. public boolean multiselectPending;
  6365. /**
  6366. * @return border top + border bottom of the scrollable area of table
  6367. */
  6368. private int getContentAreaBorderHeight() {
  6369. if (contentAreaBorderHeight < 0) {
  6370. scrollBodyPanel.getElement().getStyle()
  6371. .setOverflow(Overflow.HIDDEN);
  6372. int oh = scrollBodyPanel.getOffsetHeight();
  6373. int ch = scrollBodyPanel.getElement()
  6374. .getPropertyInt("clientHeight");
  6375. contentAreaBorderHeight = oh - ch;
  6376. scrollBodyPanel.getElement().getStyle().setOverflow(Overflow.AUTO);
  6377. }
  6378. return contentAreaBorderHeight;
  6379. }
  6380. @Override
  6381. public void setHeight(String height) {
  6382. if (height.length() == 0
  6383. && getElement().getStyle().getHeight().length() != 0) {
  6384. /*
  6385. * Changing from defined to undefined size -> should do a size init
  6386. * to take page length into account again
  6387. */
  6388. sizeNeedsInit = true;
  6389. }
  6390. super.setHeight(height);
  6391. }
  6392. /** For internal use only. May be removed or replaced in the future. */
  6393. public void updateHeight() {
  6394. setContainerHeight();
  6395. if (initializedAndAttached) {
  6396. updatePageLength();
  6397. }
  6398. triggerLazyColumnAdjustment(false);
  6399. /*
  6400. * setting height may affect wheter the component has scrollbars ->
  6401. * needs scrolling or not
  6402. */
  6403. setProperTabIndex();
  6404. }
  6405. /*
  6406. * Overridden due Table might not survive of visibility change (scroll pos
  6407. * lost). Example ITabPanel just set contained components invisible and back
  6408. * when changing tabs.
  6409. */
  6410. @Override
  6411. public void setVisible(boolean visible) {
  6412. if (isVisible() != visible) {
  6413. super.setVisible(visible);
  6414. if (initializedAndAttached) {
  6415. if (visible) {
  6416. Scheduler.get().scheduleDeferred(new Command() {
  6417. @Override
  6418. public void execute() {
  6419. scrollBodyPanel
  6420. .setScrollPosition(measureRowHeightOffset(firstRowInViewPort));
  6421. }
  6422. });
  6423. }
  6424. }
  6425. }
  6426. }
  6427. /**
  6428. * Helper function to build html snippet for column or row headers
  6429. *
  6430. * @param uidl
  6431. * possibly with values caption and icon
  6432. * @return html snippet containing possibly an icon + caption text
  6433. */
  6434. protected String buildCaptionHtmlSnippet(UIDL uidl) {
  6435. String s = uidl.hasAttribute("caption") ? uidl
  6436. .getStringAttribute("caption") : "";
  6437. if (uidl.hasAttribute("icon")) {
  6438. Icon icon = client.getIcon(uidl.getStringAttribute("icon"));
  6439. icon.setAlternateText("icon");
  6440. s = icon.getElement().getString() + s;
  6441. }
  6442. return s;
  6443. }
  6444. // Updates first visible row for the case we cannot wait
  6445. // for onScroll
  6446. private void updateFirstVisibleRow() {
  6447. scrollTop = scrollBodyPanel.getScrollPosition();
  6448. firstRowInViewPort = calcFirstRowInViewPort();
  6449. int maxFirstRow = totalRows - pageLength;
  6450. if (firstRowInViewPort > maxFirstRow && maxFirstRow >= 0) {
  6451. firstRowInViewPort = maxFirstRow;
  6452. }
  6453. lastRequestedFirstvisible = firstRowInViewPort;
  6454. client.updateVariable(paintableId, "firstvisible", firstRowInViewPort,
  6455. false);
  6456. }
  6457. /**
  6458. * This method has logic which rows needs to be requested from server when
  6459. * user scrolls
  6460. */
  6461. @Override
  6462. public void onScroll(ScrollEvent event) {
  6463. // Do not handle scroll events while there is scroll initiated from
  6464. // server side which is not yet executed (#11454)
  6465. if (isLazyScrollerActive()) {
  6466. return;
  6467. }
  6468. scrollLeft = scrollBodyPanel.getElement().getScrollLeft();
  6469. scrollTop = scrollBodyPanel.getScrollPosition();
  6470. /*
  6471. * #6970 - IE sometimes fires scroll events for a detached table.
  6472. *
  6473. * FIXME initializedAndAttached should probably be renamed - its name
  6474. * doesn't seem to reflect its semantics. onDetach() doesn't set it to
  6475. * false, and changing that might break something else, so we need to
  6476. * check isAttached() separately.
  6477. */
  6478. if (!initializedAndAttached || !isAttached()) {
  6479. return;
  6480. }
  6481. if (!enabled) {
  6482. scrollBodyPanel
  6483. .setScrollPosition(measureRowHeightOffset(firstRowInViewPort));
  6484. return;
  6485. }
  6486. rowRequestHandler.cancel();
  6487. if (BrowserInfo.get().isSafari() && event != null && scrollTop == 0) {
  6488. // due to the webkitoverflowworkaround, top may sometimes report 0
  6489. // for webkit, although it really is not. Expecting to have the
  6490. // correct
  6491. // value available soon.
  6492. Scheduler.get().scheduleDeferred(new Command() {
  6493. @Override
  6494. public void execute() {
  6495. onScroll(null);
  6496. }
  6497. });
  6498. return;
  6499. }
  6500. // fix headers horizontal scrolling
  6501. tHead.setHorizontalScrollPosition(scrollLeft);
  6502. // fix footers horizontal scrolling
  6503. tFoot.setHorizontalScrollPosition(scrollLeft);
  6504. if (totalRows == 0) {
  6505. // No rows, no need to fetch new rows
  6506. return;
  6507. }
  6508. firstRowInViewPort = calcFirstRowInViewPort();
  6509. int maxFirstRow = totalRows - pageLength;
  6510. if (firstRowInViewPort > maxFirstRow && maxFirstRow >= 0) {
  6511. firstRowInViewPort = maxFirstRow;
  6512. }
  6513. int postLimit = (int) (firstRowInViewPort + (pageLength - 1) + pageLength
  6514. * cache_react_rate);
  6515. if (postLimit > totalRows - 1) {
  6516. postLimit = totalRows - 1;
  6517. }
  6518. int preLimit = (int) (firstRowInViewPort - pageLength
  6519. * cache_react_rate);
  6520. if (preLimit < 0) {
  6521. preLimit = 0;
  6522. }
  6523. final int lastRendered = scrollBody.getLastRendered();
  6524. final int firstRendered = scrollBody.getFirstRendered();
  6525. if (postLimit <= lastRendered && preLimit >= firstRendered) {
  6526. // we're within no-react area, no need to request more rows
  6527. // remember which firstvisible we requested, in case the server has
  6528. // a differing opinion
  6529. lastRequestedFirstvisible = firstRowInViewPort;
  6530. client.updateVariable(paintableId, "firstvisible",
  6531. firstRowInViewPort, false);
  6532. return;
  6533. }
  6534. if (allRenderedRowsAreNew()) {
  6535. // need a totally new set of rows
  6536. rowRequestHandler
  6537. .setReqFirstRow((firstRowInViewPort - (int) (pageLength * cache_rate)));
  6538. int last = firstRowInViewPort + (int) (cache_rate * pageLength)
  6539. + pageLength - 1;
  6540. if (last >= totalRows) {
  6541. last = totalRows - 1;
  6542. }
  6543. rowRequestHandler.setReqRows(last
  6544. - rowRequestHandler.getReqFirstRow() + 1);
  6545. updatedReqRows = false;
  6546. rowRequestHandler.deferRowFetch();
  6547. return;
  6548. }
  6549. if (preLimit < firstRendered) {
  6550. // need some rows to the beginning of the rendered area
  6551. rowRequestHandler
  6552. .setReqFirstRow((int) (firstRowInViewPort - pageLength
  6553. * cache_rate));
  6554. rowRequestHandler.setReqRows(firstRendered
  6555. - rowRequestHandler.getReqFirstRow());
  6556. rowRequestHandler.deferRowFetch();
  6557. return;
  6558. }
  6559. if (postLimit > lastRendered) {
  6560. // need some rows to the end of the rendered area
  6561. int reqRows = (int) ((firstRowInViewPort + pageLength + pageLength
  6562. * cache_rate) - lastRendered);
  6563. rowRequestHandler.triggerRowFetch(lastRendered + 1, reqRows);
  6564. }
  6565. }
  6566. private boolean allRenderedRowsAreNew() {
  6567. int firstRowInViewPort = calcFirstRowInViewPort();
  6568. int firstRendered = scrollBody.getFirstRendered();
  6569. int lastRendered = scrollBody.getLastRendered();
  6570. return (firstRowInViewPort - pageLength * cache_rate > lastRendered || firstRowInViewPort
  6571. + pageLength + pageLength * cache_rate < firstRendered);
  6572. }
  6573. protected int calcFirstRowInViewPort() {
  6574. return (int) Math.ceil(scrollTop / scrollBody.getRowHeight());
  6575. }
  6576. @Override
  6577. public VScrollTableDropHandler getDropHandler() {
  6578. return dropHandler;
  6579. }
  6580. private static class TableDDDetails {
  6581. int overkey = -1;
  6582. VerticalDropLocation dropLocation;
  6583. String colkey;
  6584. @Override
  6585. public boolean equals(Object obj) {
  6586. if (obj instanceof TableDDDetails) {
  6587. TableDDDetails other = (TableDDDetails) obj;
  6588. return dropLocation == other.dropLocation
  6589. && overkey == other.overkey
  6590. && ((colkey != null && colkey.equals(other.colkey)) || (colkey == null && other.colkey == null));
  6591. }
  6592. return false;
  6593. }
  6594. //
  6595. // public int hashCode() {
  6596. // return overkey;
  6597. // }
  6598. }
  6599. public class VScrollTableDropHandler extends VAbstractDropHandler {
  6600. private static final String ROWSTYLEBASE = "v-table-row-drag-";
  6601. private TableDDDetails dropDetails;
  6602. private TableDDDetails lastEmphasized;
  6603. @Override
  6604. public void dragEnter(VDragEvent drag) {
  6605. updateDropDetails(drag);
  6606. super.dragEnter(drag);
  6607. }
  6608. private void updateDropDetails(VDragEvent drag) {
  6609. dropDetails = new TableDDDetails();
  6610. Element elementOver = drag.getElementOver();
  6611. Class<? extends Widget> clazz = getRowClass();
  6612. VScrollTableRow row = null;
  6613. if (clazz != null) {
  6614. row = WidgetUtil.findWidget(elementOver, clazz);
  6615. }
  6616. if (row != null) {
  6617. dropDetails.overkey = row.rowKey;
  6618. Element tr = row.getElement();
  6619. Element element = elementOver;
  6620. while (element != null && element.getParentElement() != tr) {
  6621. element = element.getParentElement();
  6622. }
  6623. int childIndex = DOM.getChildIndex(tr, element);
  6624. dropDetails.colkey = tHead.getHeaderCell(childIndex)
  6625. .getColKey();
  6626. dropDetails.dropLocation = DDUtil.getVerticalDropLocation(
  6627. row.getElement(), drag.getCurrentGwtEvent(), 0.2);
  6628. }
  6629. drag.getDropDetails().put("itemIdOver", dropDetails.overkey + "");
  6630. drag.getDropDetails().put(
  6631. "detail",
  6632. dropDetails.dropLocation != null ? dropDetails.dropLocation
  6633. .toString() : null);
  6634. }
  6635. private Class<? extends Widget> getRowClass() {
  6636. // get the row type this way to make dd work in derived
  6637. // implementations
  6638. Iterator<Widget> iterator = scrollBody.iterator();
  6639. if (iterator.hasNext()) {
  6640. return iterator.next().getClass();
  6641. } else {
  6642. return null;
  6643. }
  6644. }
  6645. @Override
  6646. public void dragOver(VDragEvent drag) {
  6647. TableDDDetails oldDetails = dropDetails;
  6648. updateDropDetails(drag);
  6649. if (!oldDetails.equals(dropDetails)) {
  6650. deEmphasis();
  6651. final TableDDDetails newDetails = dropDetails;
  6652. VAcceptCallback cb = new VAcceptCallback() {
  6653. @Override
  6654. public void accepted(VDragEvent event) {
  6655. if (newDetails.equals(dropDetails)) {
  6656. dragAccepted(event);
  6657. }
  6658. /*
  6659. * Else new target slot already defined, ignore
  6660. */
  6661. }
  6662. };
  6663. validate(cb, drag);
  6664. }
  6665. }
  6666. @Override
  6667. public void dragLeave(VDragEvent drag) {
  6668. deEmphasis();
  6669. super.dragLeave(drag);
  6670. }
  6671. @Override
  6672. public boolean drop(VDragEvent drag) {
  6673. deEmphasis();
  6674. return super.drop(drag);
  6675. }
  6676. private void deEmphasis() {
  6677. UIObject.setStyleName(getElement(),
  6678. getStylePrimaryName() + "-drag", false);
  6679. if (lastEmphasized == null) {
  6680. return;
  6681. }
  6682. for (Widget w : scrollBody.renderedRows) {
  6683. VScrollTableRow row = (VScrollTableRow) w;
  6684. if (lastEmphasized != null
  6685. && row.rowKey == lastEmphasized.overkey) {
  6686. String stylename = ROWSTYLEBASE
  6687. + lastEmphasized.dropLocation.toString()
  6688. .toLowerCase();
  6689. VScrollTableRow.setStyleName(row.getElement(), stylename,
  6690. false);
  6691. lastEmphasized = null;
  6692. return;
  6693. }
  6694. }
  6695. }
  6696. /**
  6697. * TODO needs different drop modes ?? (on cells, on rows), now only
  6698. * supports rows
  6699. */
  6700. private void emphasis(TableDDDetails details) {
  6701. deEmphasis();
  6702. UIObject.setStyleName(getElement(),
  6703. getStylePrimaryName() + "-drag", true);
  6704. // iterate old and new emphasized row
  6705. for (Widget w : scrollBody.renderedRows) {
  6706. VScrollTableRow row = (VScrollTableRow) w;
  6707. if (details != null && details.overkey == row.rowKey) {
  6708. String stylename = ROWSTYLEBASE
  6709. + details.dropLocation.toString().toLowerCase();
  6710. VScrollTableRow.setStyleName(row.getElement(), stylename,
  6711. true);
  6712. lastEmphasized = details;
  6713. return;
  6714. }
  6715. }
  6716. }
  6717. @Override
  6718. protected void dragAccepted(VDragEvent drag) {
  6719. emphasis(dropDetails);
  6720. }
  6721. @Override
  6722. public ComponentConnector getConnector() {
  6723. return ConnectorMap.get(client).getConnector(VScrollTable.this);
  6724. }
  6725. @Override
  6726. public ApplicationConnection getApplicationConnection() {
  6727. return client;
  6728. }
  6729. }
  6730. protected VScrollTableRow getFocusedRow() {
  6731. return focusedRow;
  6732. }
  6733. /**
  6734. * Moves the selection head to a specific row
  6735. *
  6736. * @param row
  6737. * The row to where the selection head should move
  6738. * @return Returns true if focus was moved successfully, else false
  6739. */
  6740. public boolean setRowFocus(VScrollTableRow row) {
  6741. if (!isSelectable()) {
  6742. return false;
  6743. }
  6744. // Remove previous selection
  6745. if (focusedRow != null && focusedRow != row) {
  6746. focusedRow.removeStyleName(getStylePrimaryName() + "-focus");
  6747. }
  6748. if (row != null) {
  6749. // Apply focus style to new selection
  6750. row.addStyleName(getStylePrimaryName() + "-focus");
  6751. /*
  6752. * Trying to set focus on already focused row
  6753. */
  6754. if (row == focusedRow) {
  6755. return false;
  6756. }
  6757. // Set new focused row
  6758. focusedRow = row;
  6759. if (hasFocus) {
  6760. ensureRowIsVisible(row);
  6761. }
  6762. return true;
  6763. }
  6764. return false;
  6765. }
  6766. /**
  6767. * Ensures that the row is visible
  6768. *
  6769. * @param row
  6770. * The row to ensure is visible
  6771. */
  6772. private void ensureRowIsVisible(VScrollTableRow row) {
  6773. if (BrowserInfo.get().isTouchDevice()) {
  6774. // Skip due to android devices that have broken scrolltop will may
  6775. // get odd scrolling here.
  6776. return;
  6777. }
  6778. /*
  6779. * FIXME The next line doesn't always do what expected, because if the
  6780. * row is not in the DOM it won't scroll to it.
  6781. */
  6782. WidgetUtil.scrollIntoViewVertically(row.getElement());
  6783. }
  6784. /**
  6785. * Handles the keyboard events handled by the table
  6786. *
  6787. * @param event
  6788. * The keyboard event received
  6789. * @return true iff the navigation event was handled
  6790. */
  6791. protected boolean handleNavigation(int keycode, boolean ctrl, boolean shift) {
  6792. if (keycode == KeyCodes.KEY_TAB || keycode == KeyCodes.KEY_SHIFT) {
  6793. // Do not handle tab key
  6794. return false;
  6795. }
  6796. // Down navigation
  6797. if (!isSelectable() && keycode == getNavigationDownKey()) {
  6798. scrollBodyPanel.setScrollPosition(scrollBodyPanel
  6799. .getScrollPosition() + scrollingVelocity);
  6800. return true;
  6801. } else if (keycode == getNavigationDownKey()) {
  6802. if (isMultiSelectModeAny() && moveFocusDown()) {
  6803. selectFocusedRow(ctrl, shift);
  6804. } else if (isSingleSelectMode() && !shift && moveFocusDown()) {
  6805. selectFocusedRow(ctrl, shift);
  6806. }
  6807. return true;
  6808. }
  6809. // Up navigation
  6810. if (!isSelectable() && keycode == getNavigationUpKey()) {
  6811. scrollBodyPanel.setScrollPosition(scrollBodyPanel
  6812. .getScrollPosition() - scrollingVelocity);
  6813. return true;
  6814. } else if (keycode == getNavigationUpKey()) {
  6815. if (isMultiSelectModeAny() && moveFocusUp()) {
  6816. selectFocusedRow(ctrl, shift);
  6817. } else if (isSingleSelectMode() && !shift && moveFocusUp()) {
  6818. selectFocusedRow(ctrl, shift);
  6819. }
  6820. return true;
  6821. }
  6822. if (keycode == getNavigationLeftKey()) {
  6823. // Left navigation
  6824. scrollBodyPanel.setHorizontalScrollPosition(scrollBodyPanel
  6825. .getHorizontalScrollPosition() - scrollingVelocity);
  6826. return true;
  6827. } else if (keycode == getNavigationRightKey()) {
  6828. // Right navigation
  6829. scrollBodyPanel.setHorizontalScrollPosition(scrollBodyPanel
  6830. .getHorizontalScrollPosition() + scrollingVelocity);
  6831. return true;
  6832. }
  6833. // Select navigation
  6834. if (isSelectable() && keycode == getNavigationSelectKey()) {
  6835. if (isSingleSelectMode()) {
  6836. boolean wasSelected = focusedRow.isSelected();
  6837. deselectAll();
  6838. if (!wasSelected || !nullSelectionAllowed) {
  6839. focusedRow.toggleSelection();
  6840. }
  6841. } else {
  6842. focusedRow.toggleSelection();
  6843. removeRowFromUnsentSelectionRanges(focusedRow);
  6844. }
  6845. sendSelectedRows();
  6846. return true;
  6847. }
  6848. // Page Down navigation
  6849. if (keycode == getNavigationPageDownKey()) {
  6850. if (isSelectable()) {
  6851. /*
  6852. * If selectable we plagiate MSW behaviour: first scroll to the
  6853. * end of current view. If at the end, scroll down one page
  6854. * length and keep the selected row in the bottom part of
  6855. * visible area.
  6856. */
  6857. if (!isFocusAtTheEndOfTable()) {
  6858. VScrollTableRow lastVisibleRowInViewPort = scrollBody
  6859. .getRowByRowIndex(firstRowInViewPort
  6860. + getFullyVisibleRowCount() - 1);
  6861. if (lastVisibleRowInViewPort != null
  6862. && lastVisibleRowInViewPort != focusedRow) {
  6863. // focused row is not at the end of the table, move
  6864. // focus and select the last visible row
  6865. setRowFocus(lastVisibleRowInViewPort);
  6866. selectFocusedRow(ctrl, shift);
  6867. updateFirstVisibleAndSendSelectedRows();
  6868. } else {
  6869. int indexOfToBeFocused = focusedRow.getIndex()
  6870. + getFullyVisibleRowCount();
  6871. if (indexOfToBeFocused >= totalRows) {
  6872. indexOfToBeFocused = totalRows - 1;
  6873. }
  6874. VScrollTableRow toBeFocusedRow = scrollBody
  6875. .getRowByRowIndex(indexOfToBeFocused);
  6876. if (toBeFocusedRow != null) {
  6877. /*
  6878. * if the next focused row is rendered
  6879. */
  6880. setRowFocus(toBeFocusedRow);
  6881. selectFocusedRow(ctrl, shift);
  6882. // TODO needs scrollintoview ?
  6883. updateFirstVisibleAndSendSelectedRows();
  6884. } else {
  6885. // scroll down by pixels and return, to wait for
  6886. // new rows, then select the last item in the
  6887. // viewport
  6888. selectLastItemInNextRender = true;
  6889. multiselectPending = shift;
  6890. scrollByPagelength(1);
  6891. }
  6892. }
  6893. }
  6894. } else {
  6895. /* No selections, go page down by scrolling */
  6896. scrollByPagelength(1);
  6897. }
  6898. return true;
  6899. }
  6900. // Page Up navigation
  6901. if (keycode == getNavigationPageUpKey()) {
  6902. if (isSelectable()) {
  6903. /*
  6904. * If selectable we plagiate MSW behaviour: first scroll to the
  6905. * end of current view. If at the end, scroll down one page
  6906. * length and keep the selected row in the bottom part of
  6907. * visible area.
  6908. */
  6909. if (!isFocusAtTheBeginningOfTable()) {
  6910. VScrollTableRow firstVisibleRowInViewPort = scrollBody
  6911. .getRowByRowIndex(firstRowInViewPort);
  6912. if (firstVisibleRowInViewPort != null
  6913. && firstVisibleRowInViewPort != focusedRow) {
  6914. // focus is not at the beginning of the table, move
  6915. // focus and select the first visible row
  6916. setRowFocus(firstVisibleRowInViewPort);
  6917. selectFocusedRow(ctrl, shift);
  6918. updateFirstVisibleAndSendSelectedRows();
  6919. } else {
  6920. int indexOfToBeFocused = focusedRow.getIndex()
  6921. - getFullyVisibleRowCount();
  6922. if (indexOfToBeFocused < 0) {
  6923. indexOfToBeFocused = 0;
  6924. }
  6925. VScrollTableRow toBeFocusedRow = scrollBody
  6926. .getRowByRowIndex(indexOfToBeFocused);
  6927. if (toBeFocusedRow != null) { // if the next focused row
  6928. // is rendered
  6929. setRowFocus(toBeFocusedRow);
  6930. selectFocusedRow(ctrl, shift);
  6931. // TODO needs scrollintoview ?
  6932. updateFirstVisibleAndSendSelectedRows();
  6933. } else {
  6934. // unless waiting for the next rowset already
  6935. // scroll down by pixels and return, to wait for
  6936. // new rows, then select the last item in the
  6937. // viewport
  6938. selectFirstItemInNextRender = true;
  6939. multiselectPending = shift;
  6940. scrollByPagelength(-1);
  6941. }
  6942. }
  6943. }
  6944. } else {
  6945. /* No selections, go page up by scrolling */
  6946. scrollByPagelength(-1);
  6947. }
  6948. return true;
  6949. }
  6950. // Goto start navigation
  6951. if (keycode == getNavigationStartKey()) {
  6952. scrollBodyPanel.setScrollPosition(0);
  6953. if (isSelectable()) {
  6954. if (focusedRow != null && focusedRow.getIndex() == 0) {
  6955. return false;
  6956. } else {
  6957. VScrollTableRow rowByRowIndex = (VScrollTableRow) scrollBody
  6958. .iterator().next();
  6959. if (rowByRowIndex.getIndex() == 0) {
  6960. setRowFocus(rowByRowIndex);
  6961. selectFocusedRow(ctrl, shift);
  6962. updateFirstVisibleAndSendSelectedRows();
  6963. } else {
  6964. // first row of table will come in next row fetch
  6965. if (ctrl) {
  6966. focusFirstItemInNextRender = true;
  6967. } else {
  6968. selectFirstItemInNextRender = true;
  6969. multiselectPending = shift;
  6970. }
  6971. }
  6972. }
  6973. }
  6974. return true;
  6975. }
  6976. // Goto end navigation
  6977. if (keycode == getNavigationEndKey()) {
  6978. scrollBodyPanel.setScrollPosition(scrollBody.getOffsetHeight());
  6979. if (isSelectable()) {
  6980. final int lastRendered = scrollBody.getLastRendered();
  6981. if (lastRendered + 1 == totalRows) {
  6982. VScrollTableRow rowByRowIndex = scrollBody
  6983. .getRowByRowIndex(lastRendered);
  6984. if (focusedRow != rowByRowIndex) {
  6985. setRowFocus(rowByRowIndex);
  6986. selectFocusedRow(ctrl, shift);
  6987. updateFirstVisibleAndSendSelectedRows();
  6988. }
  6989. } else {
  6990. if (ctrl) {
  6991. focusLastItemInNextRender = true;
  6992. } else {
  6993. selectLastItemInNextRender = true;
  6994. multiselectPending = shift;
  6995. }
  6996. }
  6997. }
  6998. return true;
  6999. }
  7000. return false;
  7001. }
  7002. private boolean isFocusAtTheBeginningOfTable() {
  7003. return focusedRow.getIndex() == 0;
  7004. }
  7005. private boolean isFocusAtTheEndOfTable() {
  7006. return focusedRow.getIndex() + 1 >= totalRows;
  7007. }
  7008. private int getFullyVisibleRowCount() {
  7009. return (int) (scrollBodyPanel.getOffsetHeight() / scrollBody
  7010. .getRowHeight());
  7011. }
  7012. private void scrollByPagelength(int i) {
  7013. int pixels = i * scrollBodyPanel.getOffsetHeight();
  7014. int newPixels = scrollBodyPanel.getScrollPosition() + pixels;
  7015. if (newPixels < 0) {
  7016. newPixels = 0;
  7017. } // else if too high, NOP (all know browsers accept illegally big
  7018. // values here)
  7019. scrollBodyPanel.setScrollPosition(newPixels);
  7020. }
  7021. /*
  7022. * (non-Javadoc)
  7023. *
  7024. * @see
  7025. * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
  7026. * .dom.client.FocusEvent)
  7027. */
  7028. @Override
  7029. public void onFocus(FocusEvent event) {
  7030. if (isFocusable()) {
  7031. hasFocus = true;
  7032. // Focus a row if no row is in focus
  7033. if (focusedRow == null) {
  7034. focusRowFromBody();
  7035. } else {
  7036. setRowFocus(focusedRow);
  7037. }
  7038. }
  7039. }
  7040. /*
  7041. * (non-Javadoc)
  7042. *
  7043. * @see
  7044. * com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event
  7045. * .dom.client.BlurEvent)
  7046. */
  7047. @Override
  7048. public void onBlur(BlurEvent event) {
  7049. hasFocus = false;
  7050. navKeyDown = false;
  7051. if (BrowserInfo.get().isIE()) {
  7052. /*
  7053. * IE sometimes moves focus to a clicked table cell... (#7965)
  7054. * ...and sometimes it sends blur events even though the focus
  7055. * handler is still active. (#10464)
  7056. */
  7057. Element focusedElement = WidgetUtil.getFocusedElement();
  7058. if (Util.getConnectorForElement(client, getParent(), focusedElement) == this
  7059. && focusedElement != null
  7060. && focusedElement != scrollBodyPanel.getFocusElement()) {
  7061. /*
  7062. * Steal focus back to the focus handler if it was moved to some
  7063. * other part of the table. Avoid stealing focus in other cases.
  7064. */
  7065. focus();
  7066. return;
  7067. }
  7068. }
  7069. if (isFocusable()) {
  7070. // Unfocus any row
  7071. setRowFocus(null);
  7072. }
  7073. }
  7074. /**
  7075. * Removes a key from a range if the key is found in a selected range
  7076. *
  7077. * @param key
  7078. * The key to remove
  7079. */
  7080. private void removeRowFromUnsentSelectionRanges(VScrollTableRow row) {
  7081. Collection<SelectionRange> newRanges = null;
  7082. for (Iterator<SelectionRange> iterator = selectedRowRanges.iterator(); iterator
  7083. .hasNext();) {
  7084. SelectionRange range = iterator.next();
  7085. if (range.inRange(row)) {
  7086. // Split the range if given row is in range
  7087. Collection<SelectionRange> splitranges = range.split(row);
  7088. if (newRanges == null) {
  7089. newRanges = new ArrayList<SelectionRange>();
  7090. }
  7091. newRanges.addAll(splitranges);
  7092. iterator.remove();
  7093. }
  7094. }
  7095. if (newRanges != null) {
  7096. selectedRowRanges.addAll(newRanges);
  7097. }
  7098. }
  7099. /**
  7100. * Can the Table be focused?
  7101. *
  7102. * @return True if the table can be focused, else false
  7103. */
  7104. public boolean isFocusable() {
  7105. if (scrollBody != null && enabled) {
  7106. return !(!hasHorizontalScrollbar() && !hasVerticalScrollbar() && !isSelectable());
  7107. }
  7108. return false;
  7109. }
  7110. private boolean hasHorizontalScrollbar() {
  7111. return scrollBody.getOffsetWidth() > scrollBodyPanel.getOffsetWidth();
  7112. }
  7113. private boolean hasVerticalScrollbar() {
  7114. return scrollBody.getOffsetHeight() > scrollBodyPanel.getOffsetHeight();
  7115. }
  7116. /*
  7117. * (non-Javadoc)
  7118. *
  7119. * @see com.vaadin.client.Focusable#focus()
  7120. */
  7121. @Override
  7122. public void focus() {
  7123. if (isFocusable()) {
  7124. scrollBodyPanel.focus();
  7125. }
  7126. }
  7127. /**
  7128. * Sets the proper tabIndex for scrollBodyPanel (the focusable elemen in the
  7129. * component).
  7130. * <p>
  7131. * If the component has no explicit tabIndex a zero is given (default
  7132. * tabbing order based on dom hierarchy) or -1 if the component does not
  7133. * need to gain focus. The component needs no focus if it has no scrollabars
  7134. * (not scrollable) and not selectable. Note that in the future shortcut
  7135. * actions may need focus.
  7136. * <p>
  7137. * For internal use only. May be removed or replaced in the future.
  7138. */
  7139. public void setProperTabIndex() {
  7140. int storedScrollTop = 0;
  7141. int storedScrollLeft = 0;
  7142. if (BrowserInfo.get().getOperaVersion() >= 11) {
  7143. // Workaround for Opera scroll bug when changing tabIndex (#6222)
  7144. storedScrollTop = scrollBodyPanel.getScrollPosition();
  7145. storedScrollLeft = scrollBodyPanel.getHorizontalScrollPosition();
  7146. }
  7147. if (tabIndex == 0 && !isFocusable()) {
  7148. scrollBodyPanel.setTabIndex(-1);
  7149. } else {
  7150. scrollBodyPanel.setTabIndex(tabIndex);
  7151. }
  7152. if (BrowserInfo.get().getOperaVersion() >= 11) {
  7153. // Workaround for Opera scroll bug when changing tabIndex (#6222)
  7154. scrollBodyPanel.setScrollPosition(storedScrollTop);
  7155. scrollBodyPanel.setHorizontalScrollPosition(storedScrollLeft);
  7156. }
  7157. }
  7158. public void startScrollingVelocityTimer() {
  7159. if (scrollingVelocityTimer == null) {
  7160. scrollingVelocityTimer = new Timer() {
  7161. @Override
  7162. public void run() {
  7163. scrollingVelocity++;
  7164. }
  7165. };
  7166. scrollingVelocityTimer.scheduleRepeating(100);
  7167. }
  7168. }
  7169. public void cancelScrollingVelocityTimer() {
  7170. if (scrollingVelocityTimer != null) {
  7171. // Remove velocityTimer if it exists and the Table is disabled
  7172. scrollingVelocityTimer.cancel();
  7173. scrollingVelocityTimer = null;
  7174. scrollingVelocity = 10;
  7175. }
  7176. }
  7177. /**
  7178. *
  7179. * @param keyCode
  7180. * @return true if the given keyCode is used by the table for navigation
  7181. */
  7182. private boolean isNavigationKey(int keyCode) {
  7183. return keyCode == getNavigationUpKey()
  7184. || keyCode == getNavigationLeftKey()
  7185. || keyCode == getNavigationRightKey()
  7186. || keyCode == getNavigationDownKey()
  7187. || keyCode == getNavigationPageUpKey()
  7188. || keyCode == getNavigationPageDownKey()
  7189. || keyCode == getNavigationEndKey()
  7190. || keyCode == getNavigationStartKey();
  7191. }
  7192. public void lazyRevertFocusToRow(final VScrollTableRow currentlyFocusedRow) {
  7193. Scheduler.get().scheduleFinally(new ScheduledCommand() {
  7194. @Override
  7195. public void execute() {
  7196. if (currentlyFocusedRow != null) {
  7197. setRowFocus(currentlyFocusedRow);
  7198. } else {
  7199. VConsole.log("no row?");
  7200. focusRowFromBody();
  7201. }
  7202. scrollBody.ensureFocus();
  7203. }
  7204. });
  7205. }
  7206. @Override
  7207. public Action[] getActions() {
  7208. if (bodyActionKeys == null) {
  7209. return new Action[] {};
  7210. }
  7211. final Action[] actions = new Action[bodyActionKeys.length];
  7212. for (int i = 0; i < actions.length; i++) {
  7213. final String actionKey = bodyActionKeys[i];
  7214. Action bodyAction = new TreeAction(this, null, actionKey);
  7215. bodyAction.setCaption(getActionCaption(actionKey));
  7216. bodyAction.setIconUrl(getActionIcon(actionKey));
  7217. actions[i] = bodyAction;
  7218. }
  7219. return actions;
  7220. }
  7221. @Override
  7222. public ApplicationConnection getClient() {
  7223. return client;
  7224. }
  7225. @Override
  7226. public String getPaintableId() {
  7227. return paintableId;
  7228. }
  7229. /**
  7230. * Add this to the element mouse down event by using element.setPropertyJSO
  7231. * ("onselectstart",applyDisableTextSelectionIEHack()); Remove it then again
  7232. * when the mouse is depressed in the mouse up event.
  7233. *
  7234. * @return Returns the JSO preventing text selection
  7235. */
  7236. private static native JavaScriptObject getPreventTextSelectionIEHack()
  7237. /*-{
  7238. return function(){ return false; };
  7239. }-*/;
  7240. public void triggerLazyColumnAdjustment(boolean now) {
  7241. lazyAdjustColumnWidths.cancel();
  7242. if (now) {
  7243. lazyAdjustColumnWidths.run();
  7244. } else {
  7245. lazyAdjustColumnWidths.schedule(LAZY_COLUMN_ADJUST_TIMEOUT);
  7246. }
  7247. }
  7248. private boolean isDynamicWidth() {
  7249. ComponentConnector paintable = ConnectorMap.get(client).getConnector(
  7250. this);
  7251. return paintable.isUndefinedWidth();
  7252. }
  7253. private boolean isDynamicHeight() {
  7254. ComponentConnector paintable = ConnectorMap.get(client).getConnector(
  7255. this);
  7256. if (paintable == null) {
  7257. // This should be refactored. As isDynamicHeight can be called from
  7258. // a timer it is possible that the connector has been unregistered
  7259. // when this method is called, causing getConnector to return null.
  7260. return false;
  7261. }
  7262. return paintable.isUndefinedHeight();
  7263. }
  7264. private void debug(String msg) {
  7265. if (enableDebug) {
  7266. VConsole.error(msg);
  7267. }
  7268. }
  7269. public Widget getWidgetForPaintable() {
  7270. return this;
  7271. }
  7272. private static final String SUBPART_HEADER = "header";
  7273. private static final String SUBPART_FOOTER = "footer";
  7274. private static final String SUBPART_ROW = "row";
  7275. private static final String SUBPART_COL = "col";
  7276. /**
  7277. * Matches header[ix] - used for extracting the index of the targeted header
  7278. * cell
  7279. */
  7280. private static final RegExp SUBPART_HEADER_REGEXP = RegExp
  7281. .compile(SUBPART_HEADER + "\\[(\\d+)\\]");
  7282. /**
  7283. * Matches footer[ix] - used for extracting the index of the targeted footer
  7284. * cell
  7285. */
  7286. private static final RegExp SUBPART_FOOTER_REGEXP = RegExp
  7287. .compile(SUBPART_FOOTER + "\\[(\\d+)\\]");
  7288. /** Matches row[ix] - used for extracting the index of the targeted row */
  7289. private static final RegExp SUBPART_ROW_REGEXP = RegExp.compile(SUBPART_ROW
  7290. + "\\[(\\d+)]");
  7291. /** Matches col[ix] - used for extracting the index of the targeted column */
  7292. private static final RegExp SUBPART_ROW_COL_REGEXP = RegExp
  7293. .compile(SUBPART_ROW + "\\[(\\d+)\\]/" + SUBPART_COL
  7294. + "\\[(\\d+)\\]");
  7295. @Override
  7296. public com.google.gwt.user.client.Element getSubPartElement(String subPart) {
  7297. if (SUBPART_ROW_COL_REGEXP.test(subPart)) {
  7298. MatchResult result = SUBPART_ROW_COL_REGEXP.exec(subPart);
  7299. int rowIx = Integer.valueOf(result.getGroup(1));
  7300. int colIx = Integer.valueOf(result.getGroup(2));
  7301. VScrollTableRow row = scrollBody.getRowByRowIndex(rowIx);
  7302. if (row != null) {
  7303. Element rowElement = row.getElement();
  7304. if (colIx < rowElement.getChildCount()) {
  7305. return rowElement.getChild(colIx).getFirstChild().cast();
  7306. }
  7307. }
  7308. } else if (SUBPART_ROW_REGEXP.test(subPart)) {
  7309. MatchResult result = SUBPART_ROW_REGEXP.exec(subPart);
  7310. int rowIx = Integer.valueOf(result.getGroup(1));
  7311. VScrollTableRow row = scrollBody.getRowByRowIndex(rowIx);
  7312. if (row != null) {
  7313. return row.getElement();
  7314. }
  7315. } else if (SUBPART_HEADER_REGEXP.test(subPart)) {
  7316. MatchResult result = SUBPART_HEADER_REGEXP.exec(subPart);
  7317. int headerIx = Integer.valueOf(result.getGroup(1));
  7318. HeaderCell headerCell = tHead.getHeaderCell(headerIx);
  7319. if (headerCell != null) {
  7320. return headerCell.getElement();
  7321. }
  7322. } else if (SUBPART_FOOTER_REGEXP.test(subPart)) {
  7323. MatchResult result = SUBPART_FOOTER_REGEXP.exec(subPart);
  7324. int footerIx = Integer.valueOf(result.getGroup(1));
  7325. FooterCell footerCell = tFoot.getFooterCell(footerIx);
  7326. if (footerCell != null) {
  7327. return footerCell.getElement();
  7328. }
  7329. }
  7330. // Nothing found.
  7331. return null;
  7332. }
  7333. @Override
  7334. public String getSubPartName(com.google.gwt.user.client.Element subElement) {
  7335. Widget widget = WidgetUtil.findWidget(subElement, null);
  7336. if (widget instanceof HeaderCell) {
  7337. return SUBPART_HEADER + "[" + tHead.visibleCells.indexOf(widget)
  7338. + "]";
  7339. } else if (widget instanceof FooterCell) {
  7340. return SUBPART_FOOTER + "[" + tFoot.visibleCells.indexOf(widget)
  7341. + "]";
  7342. } else if (widget instanceof VScrollTableRow) {
  7343. // a cell in a row
  7344. VScrollTableRow row = (VScrollTableRow) widget;
  7345. int rowIx = scrollBody.indexOf(row);
  7346. if (rowIx >= 0) {
  7347. int colIx = -1;
  7348. for (int ix = 0; ix < row.getElement().getChildCount(); ix++) {
  7349. if (row.getElement().getChild(ix).isOrHasChild(subElement)) {
  7350. colIx = ix;
  7351. break;
  7352. }
  7353. }
  7354. if (colIx >= 0) {
  7355. return SUBPART_ROW + "[" + rowIx + "]/" + SUBPART_COL + "["
  7356. + colIx + "]";
  7357. }
  7358. return SUBPART_ROW + "[" + rowIx + "]";
  7359. }
  7360. }
  7361. // Nothing found.
  7362. return null;
  7363. }
  7364. /**
  7365. * @since 7.2.6
  7366. */
  7367. public void onUnregister() {
  7368. if (addCloseHandler != null) {
  7369. addCloseHandler.removeHandler();
  7370. }
  7371. }
  7372. /*
  7373. * Return true if component need to perform some work and false otherwise.
  7374. */
  7375. @Override
  7376. public boolean isWorkPending() {
  7377. return lazyAdjustColumnWidths.isRunning();
  7378. }
  7379. private static Logger getLogger() {
  7380. return Logger.getLogger(VScrollTable.class.getName());
  7381. }
  7382. }