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

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