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.

Grid.java 346KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.widgets;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Map.Entry;
  28. import java.util.Objects;
  29. import java.util.Optional;
  30. import java.util.Set;
  31. import java.util.TreeMap;
  32. import java.util.concurrent.atomic.AtomicInteger;
  33. import java.util.logging.Level;
  34. import java.util.logging.Logger;
  35. import java.util.stream.Stream;
  36. import com.google.gwt.core.client.Scheduler;
  37. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  38. import com.google.gwt.core.shared.GWT;
  39. import com.google.gwt.dom.client.BrowserEvents;
  40. import com.google.gwt.dom.client.DivElement;
  41. import com.google.gwt.dom.client.Document;
  42. import com.google.gwt.dom.client.Element;
  43. import com.google.gwt.dom.client.EventTarget;
  44. import com.google.gwt.dom.client.NativeEvent;
  45. import com.google.gwt.dom.client.Node;
  46. import com.google.gwt.dom.client.Style;
  47. import com.google.gwt.dom.client.Style.Display;
  48. import com.google.gwt.dom.client.Style.Unit;
  49. import com.google.gwt.dom.client.TableCellElement;
  50. import com.google.gwt.dom.client.TableRowElement;
  51. import com.google.gwt.dom.client.TableSectionElement;
  52. import com.google.gwt.dom.client.Touch;
  53. import com.google.gwt.event.dom.client.ClickHandler;
  54. import com.google.gwt.event.dom.client.KeyCodes;
  55. import com.google.gwt.event.dom.client.KeyDownEvent;
  56. import com.google.gwt.event.dom.client.KeyDownHandler;
  57. import com.google.gwt.event.dom.client.KeyEvent;
  58. import com.google.gwt.event.dom.client.MouseEvent;
  59. import com.google.gwt.event.logical.shared.CloseEvent;
  60. import com.google.gwt.event.logical.shared.CloseHandler;
  61. import com.google.gwt.event.shared.HandlerRegistration;
  62. import com.google.gwt.touch.client.Point;
  63. import com.google.gwt.user.client.DOM;
  64. import com.google.gwt.user.client.Event;
  65. import com.google.gwt.user.client.Timer;
  66. import com.google.gwt.user.client.ui.Button;
  67. import com.google.gwt.user.client.ui.CheckBox;
  68. import com.google.gwt.user.client.ui.Composite;
  69. import com.google.gwt.user.client.ui.FlowPanel;
  70. import com.google.gwt.user.client.ui.HasEnabled;
  71. import com.google.gwt.user.client.ui.HasWidgets;
  72. import com.google.gwt.user.client.ui.MenuBar;
  73. import com.google.gwt.user.client.ui.MenuItem;
  74. import com.google.gwt.user.client.ui.PopupPanel;
  75. import com.google.gwt.user.client.ui.ResizeComposite;
  76. import com.google.gwt.user.client.ui.Widget;
  77. import com.vaadin.client.BrowserInfo;
  78. import com.vaadin.client.ComputedStyle;
  79. import com.vaadin.client.DeferredWorker;
  80. import com.vaadin.client.Focusable;
  81. import com.vaadin.client.WidgetUtil;
  82. import com.vaadin.client.WidgetUtil.Reference;
  83. import com.vaadin.client.data.DataChangeHandler;
  84. import com.vaadin.client.data.DataSource;
  85. import com.vaadin.client.data.DataSource.RowHandle;
  86. import com.vaadin.client.renderers.ComplexRenderer;
  87. import com.vaadin.client.renderers.Renderer;
  88. import com.vaadin.client.renderers.WidgetRenderer;
  89. import com.vaadin.client.ui.FocusUtil;
  90. import com.vaadin.client.ui.SubPartAware;
  91. import com.vaadin.client.ui.dd.DragAndDropHandler;
  92. import com.vaadin.client.ui.dd.DragAndDropHandler.DragAndDropCallback;
  93. import com.vaadin.client.ui.dd.DragHandle;
  94. import com.vaadin.client.ui.dd.DragHandle.DragHandleCallback;
  95. import com.vaadin.client.widget.escalator.Cell;
  96. import com.vaadin.client.widget.escalator.ColumnConfiguration;
  97. import com.vaadin.client.widget.escalator.EscalatorUpdater;
  98. import com.vaadin.client.widget.escalator.FlyweightCell;
  99. import com.vaadin.client.widget.escalator.Row;
  100. import com.vaadin.client.widget.escalator.RowContainer;
  101. import com.vaadin.client.widget.escalator.RowVisibilityChangeHandler;
  102. import com.vaadin.client.widget.escalator.ScrollbarBundle.Direction;
  103. import com.vaadin.client.widget.escalator.Spacer;
  104. import com.vaadin.client.widget.escalator.SpacerUpdater;
  105. import com.vaadin.client.widget.escalator.events.RowHeightChangedEvent;
  106. import com.vaadin.client.widget.escalator.events.RowHeightChangedHandler;
  107. import com.vaadin.client.widget.escalator.events.SpacerIndexChangedEvent;
  108. import com.vaadin.client.widget.escalator.events.SpacerIndexChangedHandler;
  109. import com.vaadin.client.widget.escalator.events.SpacerVisibilityChangedEvent;
  110. import com.vaadin.client.widget.escalator.events.SpacerVisibilityChangedHandler;
  111. import com.vaadin.client.widget.grid.AutoScroller;
  112. import com.vaadin.client.widget.grid.AutoScroller.AutoScrollerCallback;
  113. import com.vaadin.client.widget.grid.AutoScroller.ScrollAxis;
  114. import com.vaadin.client.widget.grid.CellReference;
  115. import com.vaadin.client.widget.grid.CellStyleGenerator;
  116. import com.vaadin.client.widget.grid.DataAvailableEvent;
  117. import com.vaadin.client.widget.grid.DataAvailableHandler;
  118. import com.vaadin.client.widget.grid.DefaultEditorEventHandler;
  119. import com.vaadin.client.widget.grid.DetailsGenerator;
  120. import com.vaadin.client.widget.grid.EditorHandler;
  121. import com.vaadin.client.widget.grid.EditorHandler.EditorRequest;
  122. import com.vaadin.client.widget.grid.EventCellReference;
  123. import com.vaadin.client.widget.grid.GridEventHandler;
  124. import com.vaadin.client.widget.grid.HeightAwareDetailsGenerator;
  125. import com.vaadin.client.widget.grid.RendererCellReference;
  126. import com.vaadin.client.widget.grid.RowReference;
  127. import com.vaadin.client.widget.grid.RowStyleGenerator;
  128. import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler;
  129. import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler;
  130. import com.vaadin.client.widget.grid.events.BodyClickHandler;
  131. import com.vaadin.client.widget.grid.events.BodyDoubleClickHandler;
  132. import com.vaadin.client.widget.grid.events.BodyKeyDownHandler;
  133. import com.vaadin.client.widget.grid.events.BodyKeyPressHandler;
  134. import com.vaadin.client.widget.grid.events.BodyKeyUpHandler;
  135. import com.vaadin.client.widget.grid.events.ColumnReorderEvent;
  136. import com.vaadin.client.widget.grid.events.ColumnReorderHandler;
  137. import com.vaadin.client.widget.grid.events.ColumnResizeEvent;
  138. import com.vaadin.client.widget.grid.events.ColumnResizeHandler;
  139. import com.vaadin.client.widget.grid.events.ColumnVisibilityChangeEvent;
  140. import com.vaadin.client.widget.grid.events.ColumnVisibilityChangeHandler;
  141. import com.vaadin.client.widget.grid.events.FooterClickHandler;
  142. import com.vaadin.client.widget.grid.events.FooterDoubleClickHandler;
  143. import com.vaadin.client.widget.grid.events.FooterKeyDownHandler;
  144. import com.vaadin.client.widget.grid.events.FooterKeyPressHandler;
  145. import com.vaadin.client.widget.grid.events.FooterKeyUpHandler;
  146. import com.vaadin.client.widget.grid.events.GridClickEvent;
  147. import com.vaadin.client.widget.grid.events.GridDoubleClickEvent;
  148. import com.vaadin.client.widget.grid.events.GridEnabledEvent;
  149. import com.vaadin.client.widget.grid.events.GridEnabledHandler;
  150. import com.vaadin.client.widget.grid.events.GridKeyDownEvent;
  151. import com.vaadin.client.widget.grid.events.GridKeyPressEvent;
  152. import com.vaadin.client.widget.grid.events.GridKeyUpEvent;
  153. import com.vaadin.client.widget.grid.events.GridSelectionAllowedEvent;
  154. import com.vaadin.client.widget.grid.events.GridSelectionAllowedHandler;
  155. import com.vaadin.client.widget.grid.events.HeaderClickHandler;
  156. import com.vaadin.client.widget.grid.events.HeaderDoubleClickHandler;
  157. import com.vaadin.client.widget.grid.events.HeaderKeyDownHandler;
  158. import com.vaadin.client.widget.grid.events.HeaderKeyPressHandler;
  159. import com.vaadin.client.widget.grid.events.HeaderKeyUpHandler;
  160. import com.vaadin.client.widget.grid.events.ScrollEvent;
  161. import com.vaadin.client.widget.grid.events.ScrollHandler;
  162. import com.vaadin.client.widget.grid.events.SelectAllEvent;
  163. import com.vaadin.client.widget.grid.events.SelectAllHandler;
  164. import com.vaadin.client.widget.grid.selection.HasSelectionHandlers;
  165. import com.vaadin.client.widget.grid.selection.MultiSelectionRenderer;
  166. import com.vaadin.client.widget.grid.selection.SelectionEvent;
  167. import com.vaadin.client.widget.grid.selection.SelectionHandler;
  168. import com.vaadin.client.widget.grid.selection.SelectionModel;
  169. import com.vaadin.client.widget.grid.selection.SelectionModelWithSelectionColumn;
  170. import com.vaadin.client.widget.grid.sort.Sort;
  171. import com.vaadin.client.widget.grid.sort.SortEvent;
  172. import com.vaadin.client.widget.grid.sort.SortHandler;
  173. import com.vaadin.client.widget.grid.sort.SortOrder;
  174. import com.vaadin.client.widgets.Escalator.AbstractRowContainer;
  175. import com.vaadin.client.widgets.Escalator.SubPartArguments;
  176. import com.vaadin.client.widgets.Grid.Editor.State;
  177. import com.vaadin.client.widgets.Grid.StaticSection.StaticCell;
  178. import com.vaadin.client.widgets.Grid.StaticSection.StaticRow;
  179. import com.vaadin.shared.Range;
  180. import com.vaadin.shared.Registration;
  181. import com.vaadin.shared.data.sort.SortDirection;
  182. import com.vaadin.shared.ui.ContentMode;
  183. import com.vaadin.shared.ui.grid.ColumnResizeMode;
  184. import com.vaadin.shared.ui.grid.GridConstants;
  185. import com.vaadin.shared.ui.grid.GridConstants.Section;
  186. import com.vaadin.shared.ui.grid.GridStaticCellType;
  187. import com.vaadin.shared.ui.grid.HeightMode;
  188. import com.vaadin.shared.ui.grid.ScrollDestination;
  189. import com.vaadin.shared.util.SharedUtil;
  190. /**
  191. * A data grid view that supports columns and lazy loading of data rows from a
  192. * data source.
  193. *
  194. * <h1>Columns</h1>
  195. * <p>
  196. * Each column in Grid is represented by a {@link Column}. Each
  197. * {@code GridColumn} has a custom implementation for
  198. * {@link Column#getValue(Object)} that gets the row object as an argument, and
  199. * returns the value for that particular column, extracted from the row object.
  200. * <p>
  201. * Each column also has a Renderer. Its function is to take the value that is
  202. * given by the {@code GridColumn} and display it to the user. A simple column
  203. * might have a {@link com.vaadin.client.renderers.TextRenderer TextRenderer}
  204. * that simply takes in a {@code String} and displays it as the cell's content.
  205. * A more complex renderer might be
  206. * {@link com.vaadin.client.renderers.ProgressBarRenderer ProgressBarRenderer}
  207. * that takes in a floating point number, and displays a progress bar instead,
  208. * based on the given number.
  209. * <p>
  210. * <em>See:</em> {@link #addColumn(Column)}, {@link #addColumn(Column, int)} and
  211. * {@link #addColumns(Column...)}. <em>Also</em>
  212. * {@link Column#setRenderer(Renderer)}.
  213. *
  214. * <h1>Data Sources</h1>
  215. * <p>
  216. * Grid gets its data from a {@link DataSource}, providing row objects to Grid
  217. * from a user-defined endpoint. It can be either a local in-memory data source
  218. * (e.g. {@link com.vaadin.client.widget.grid.datasources.ListDataSource
  219. * ListDataSource}) or even a remote one, retrieving data from e.g. a REST API
  220. * (see {@link com.vaadin.client.data.AbstractRemoteDataSource
  221. * AbstractRemoteDataSource}).
  222. *
  223. *
  224. * @param <T>
  225. * The row type of the grid. The row type is the POJO type from where
  226. * the data is retrieved into the column cells.
  227. * @since 7.4
  228. * @author Vaadin Ltd
  229. */
  230. public class Grid<T> extends ResizeComposite implements HasSelectionHandlers<T>,
  231. SubPartAware, DeferredWorker, Focusable,
  232. com.google.gwt.user.client.ui.Focusable, HasWidgets, HasEnabled {
  233. private static final String STYLE_NAME = "v-grid";
  234. private static final String SELECT_ALL_CHECKBOX_CLASSNAME = "-select-all-checkbox";
  235. /**
  236. * Abstract base class for Grid header and footer sections.
  237. *
  238. * @since 7.5.0
  239. *
  240. * @param <ROWTYPE>
  241. * the type of the rows in the section
  242. */
  243. public abstract static class StaticSection<ROWTYPE extends StaticSection.StaticRow<?>> {
  244. /**
  245. * A header or footer cell. Has a simple textual caption.
  246. *
  247. */
  248. public static class StaticCell {
  249. private Object content = null;
  250. private int colspan = 1;
  251. private StaticSection<?> section;
  252. private GridStaticCellType type = GridStaticCellType.TEXT;
  253. private String styleName = null;
  254. private String description = null;
  255. private ContentMode descriptionContentMode = ContentMode.TEXT;
  256. /**
  257. * Sets the text displayed in this cell.
  258. *
  259. * @param text
  260. * a plain text caption
  261. */
  262. public void setText(String text) {
  263. detach();
  264. this.content = text;
  265. this.type = GridStaticCellType.TEXT;
  266. section.requestSectionRefresh();
  267. }
  268. /**
  269. * Returns the text displayed in this cell.
  270. *
  271. * @return the plain text caption
  272. */
  273. public String getText() {
  274. if (type != GridStaticCellType.TEXT) {
  275. throw new IllegalStateException(
  276. "Cannot fetch Text from a cell with type " + type);
  277. }
  278. return (String) content;
  279. }
  280. protected StaticSection<?> getSection() {
  281. assert section != null;
  282. return section;
  283. }
  284. protected void setSection(StaticSection<?> section) {
  285. this.section = section;
  286. }
  287. /**
  288. * Returns the amount of columns the cell spans. By default is 1.
  289. *
  290. * @return The amount of columns the cell spans.
  291. */
  292. public int getColspan() {
  293. return colspan;
  294. }
  295. /**
  296. * Sets the amount of columns the cell spans. Must be more or equal
  297. * to 1. By default is 1.
  298. *
  299. * @param colspan
  300. * the colspan to set
  301. */
  302. public void setColspan(int colspan) {
  303. if (colspan < 1) {
  304. throw new IllegalArgumentException(
  305. "Colspan cannot be less than 1");
  306. }
  307. this.colspan = colspan;
  308. section.requestSectionRefresh();
  309. }
  310. /**
  311. * Returns the html inside the cell.
  312. *
  313. * @throws IllegalStateException
  314. * if trying to retrive HTML from a cell with a type
  315. * other than {@link GridStaticCellType#HTML}.
  316. * @return the html content of the cell.
  317. */
  318. public String getHtml() {
  319. if (type != GridStaticCellType.HTML) {
  320. throw new IllegalStateException(
  321. "Cannot fetch HTML from a cell with type " + type);
  322. }
  323. return (String) content;
  324. }
  325. /**
  326. * Sets the content of the cell to the provided html. All previous
  327. * content is discarded and the cell type is set to
  328. * {@link GridStaticCellType#HTML}.
  329. *
  330. * @param html
  331. * The html content of the cell
  332. */
  333. public void setHtml(String html) {
  334. detach();
  335. this.content = html;
  336. this.type = GridStaticCellType.HTML;
  337. section.requestSectionRefresh();
  338. }
  339. /**
  340. * Returns the widget in the cell.
  341. *
  342. * @throws IllegalStateException
  343. * if the cell is not {@link GridStaticCellType#WIDGET}
  344. *
  345. * @return the widget in the cell
  346. */
  347. public Widget getWidget() {
  348. if (type != GridStaticCellType.WIDGET) {
  349. throw new IllegalStateException(
  350. "Cannot fetch Widget from a cell with type "
  351. + type);
  352. }
  353. return (Widget) content;
  354. }
  355. /**
  356. * Set widget as the content of the cell. The type of the cell
  357. * becomes {@link GridStaticCellType#WIDGET}. All previous content
  358. * is discarded.
  359. *
  360. * @param widget
  361. * The widget to add to the cell. Should not be
  362. * previously attached anywhere (widget.getParent ==
  363. * null).
  364. */
  365. public void setWidget(Widget widget) {
  366. if (this.content == widget) {
  367. return;
  368. }
  369. detach();
  370. this.content = widget;
  371. this.type = GridStaticCellType.WIDGET;
  372. section.requestSectionRefresh();
  373. }
  374. /**
  375. * Returns the type of the cell.
  376. *
  377. * @return the type of content the cell contains.
  378. */
  379. public GridStaticCellType getType() {
  380. return type;
  381. }
  382. /**
  383. * Returns the custom style name for this cell.
  384. *
  385. * @return the style name or null if no style name has been set
  386. */
  387. public String getStyleName() {
  388. return styleName;
  389. }
  390. /**
  391. * Sets a custom style name for this cell.
  392. *
  393. * @param styleName
  394. * the style name to set or null to not use any style
  395. * name
  396. */
  397. public void setStyleName(String styleName) {
  398. this.styleName = styleName;
  399. section.requestSectionRefresh();
  400. }
  401. /**
  402. * Called when the cell is detached from the row
  403. *
  404. * @since 7.6.3
  405. */
  406. void detach() {
  407. if (content instanceof Widget) {
  408. // Widget in the cell, detach it
  409. section.getGrid().detachWidget((Widget) content);
  410. }
  411. }
  412. /**
  413. * Gets the tooltip for the cell.
  414. * <p>
  415. * The tooltip is shown in the mode returned by
  416. * {@link #getDescriptionContentMode()}.
  417. *
  418. * @return the tooltip text for this cell
  419. * @since 8.4
  420. */
  421. public String getDescription() {
  422. return description;
  423. }
  424. /**
  425. * Sets the tooltip for the cell.
  426. * <p>
  427. * By default, tooltips are shown as plain text. For HTML tooltips,
  428. * see {@link #setDescription(String, ContentMode)} or
  429. * {@link #setDescriptionContentMode(ContentMode)}.
  430. *
  431. * @param description
  432. * the tooltip to show when hovering the cell
  433. * @since 8.4
  434. */
  435. public void setDescription(String description) {
  436. this.description = description;
  437. }
  438. /**
  439. * Sets the tooltip for the cell to be shown with the given content
  440. * mode.
  441. *
  442. * @see ContentMode
  443. * @param description
  444. * the tooltip to show when hovering the cell
  445. * @param descriptionContentMode
  446. * the content mode to use for the tooltip (HTML or plain
  447. * text)
  448. * @since 8.4
  449. */
  450. public void setDescription(String description,
  451. ContentMode descriptionContentMode) {
  452. setDescription(description);
  453. setDescriptionContentMode(descriptionContentMode);
  454. }
  455. /**
  456. * Gets the content mode for the tooltip.
  457. * <p>
  458. * The content mode determines how the tooltip is shown.
  459. *
  460. * @see ContentMode
  461. * @return the content mode for the tooltip
  462. * @since 8.4
  463. */
  464. public ContentMode getDescriptionContentMode() {
  465. return descriptionContentMode;
  466. }
  467. /**
  468. * Sets the content mode for the tooltip.
  469. *
  470. * @see ContentMode
  471. * @param descriptionContentMode
  472. * the content mode for the tooltip
  473. * @since 8.4
  474. */
  475. public void setDescriptionContentMode(
  476. ContentMode descriptionContentMode) {
  477. this.descriptionContentMode = descriptionContentMode;
  478. }
  479. }
  480. /**
  481. * Abstract base class for Grid header and footer rows.
  482. *
  483. * @param <CELLTYPE>
  484. * the type of the cells in the row
  485. */
  486. public abstract static class StaticRow<CELLTYPE extends StaticCell> {
  487. private Map<Column<?, ?>, CELLTYPE> cells = new HashMap<>();
  488. private StaticSection<?> section;
  489. /**
  490. * Map from cell meta data to sets of spanned columns .
  491. */
  492. private Map<CELLTYPE, Set<Column<?, ?>>> cellGroups = new HashMap<>();
  493. /**
  494. * A custom style name for the row or null if none is set.
  495. */
  496. private String styleName = null;
  497. /**
  498. * Returns the cell on given GridColumn. If the column is merged
  499. * returned cell is the cell for the whole group.
  500. *
  501. * @param column
  502. * the column in grid
  503. * @return the cell on given column, merged cell for merged columns,
  504. * null if not found
  505. */
  506. public CELLTYPE getCell(Column<?, ?> column) {
  507. CELLTYPE cell = getMergedCellForColumn(column);
  508. if (cell != null) {
  509. return cell;
  510. }
  511. return cells.get(column);
  512. }
  513. /**
  514. * Returns <code>true</code> if this row contains spanned cells.
  515. *
  516. * @since 7.5.0
  517. * @return does this row contain spanned cells
  518. */
  519. public boolean hasSpannedCells() {
  520. return !cellGroups.isEmpty();
  521. }
  522. /**
  523. * Merges columns cells in a row.
  524. *
  525. * @param columns
  526. * the columns which header should be merged
  527. * @return the remaining visible cell after the merge, or the cell
  528. * on first column if all are hidden
  529. */
  530. public CELLTYPE join(Column<?, ?>... columns) {
  531. if (columns.length <= 1) {
  532. throw new IllegalArgumentException(
  533. "You can't merge less than 2 columns together.");
  534. }
  535. HashSet<Column<?, ?>> columnGroup = new HashSet<>();
  536. // NOTE: this doesn't care about hidden columns, those are
  537. // filtered in calculateColspans()
  538. for (Column<?, ?> column : columns) {
  539. if (!cells.containsKey(column)) {
  540. throw new IllegalArgumentException(
  541. "Given column does not exists on row "
  542. + column);
  543. } else if (getMergedCellForColumn(column) != null) {
  544. throw new IllegalStateException(
  545. "Column is already in a group.");
  546. }
  547. columnGroup.add(column);
  548. }
  549. CELLTYPE joinedCell = createCell();
  550. cellGroups.put(joinedCell, columnGroup);
  551. joinedCell.setSection(getSection());
  552. calculateColspans();
  553. return joinedCell;
  554. }
  555. /**
  556. * Merges columns cells in a row.
  557. *
  558. * @param cells
  559. * The cells to merge. Must be from the same row.
  560. * @return The remaining visible cell after the merge, or the first
  561. * cell if all columns are hidden
  562. */
  563. public CELLTYPE join(CELLTYPE... cells) {
  564. if (cells.length <= 1) {
  565. throw new IllegalArgumentException(
  566. "You can't merge less than 2 cells together.");
  567. }
  568. Column<?, ?>[] columns = new Column<?, ?>[cells.length];
  569. int j = 0;
  570. for (Column<?, ?> column : this.cells.keySet()) {
  571. CELLTYPE cell = this.cells.get(column);
  572. if (!this.cells.containsValue(cells[j])) {
  573. throw new IllegalArgumentException(
  574. "Given cell does not exists on row");
  575. } else if (cell.equals(cells[j])) {
  576. columns[j++] = column;
  577. if (j == cells.length) {
  578. break;
  579. }
  580. }
  581. }
  582. return join(columns);
  583. }
  584. private CELLTYPE getMergedCellForColumn(Column<?, ?> column) {
  585. for (Entry<CELLTYPE, Set<Column<?, ?>>> entry : cellGroups
  586. .entrySet()) {
  587. if (entry.getValue().contains(column)) {
  588. return entry.getKey();
  589. }
  590. }
  591. return null;
  592. }
  593. protected int getSizeOfCellGroup(Column<?, ?> column) {
  594. for (Entry<CELLTYPE, Set<Column<?, ?>>> entry : cellGroups
  595. .entrySet()) {
  596. if (entry.getValue().contains(column)) {
  597. return entry.getValue().size();
  598. }
  599. }
  600. return 0;
  601. }
  602. void calculateColspans() {
  603. // Reset all cells
  604. for (CELLTYPE cell : this.cells.values()) {
  605. cell.setColspan(1);
  606. }
  607. // Set colspan for grouped cells
  608. for (Entry<CELLTYPE, Set<Column<?, ?>>> entry : cellGroups
  609. .entrySet()) {
  610. CELLTYPE mergedCell = entry.getKey();
  611. if (!checkMergedCellIsContinuous(entry.getValue())) {
  612. // on error simply break the merged cell
  613. mergedCell.setColspan(1);
  614. } else {
  615. int colSpan = 0;
  616. for (Column<?, ?> column : entry.getValue()) {
  617. if (!column.isHidden()) {
  618. colSpan++;
  619. }
  620. }
  621. // colspan can't be 0
  622. mergedCell.setColspan(Math.max(1, colSpan));
  623. }
  624. }
  625. }
  626. private boolean checkMergedCellIsContinuous(
  627. Set<Column<?, ?>> mergedCell) {
  628. // no matter if hidden or not, just check for continuous order
  629. final List<Column<?, ?>> columnOrder = new ArrayList<>(
  630. section.grid.getColumns());
  631. if (!columnOrder.containsAll(mergedCell)) {
  632. return false;
  633. }
  634. for (int i = 0; i < columnOrder.size(); ++i) {
  635. if (!mergedCell.contains(columnOrder.get(i))) {
  636. continue;
  637. }
  638. for (int j = 1; j < mergedCell.size(); ++j) {
  639. if (!mergedCell.contains(columnOrder.get(i + j))) {
  640. return false;
  641. }
  642. }
  643. return true;
  644. }
  645. return false;
  646. }
  647. protected void addCell(Column<?, ?> column) {
  648. CELLTYPE cell = createCell();
  649. cell.setSection(getSection());
  650. cells.put(column, cell);
  651. }
  652. protected void removeCell(Column<?, ?> column) {
  653. cells.remove(column);
  654. }
  655. protected abstract CELLTYPE createCell();
  656. protected StaticSection<?> getSection() {
  657. return section;
  658. }
  659. protected void setSection(StaticSection<?> section) {
  660. this.section = section;
  661. }
  662. /**
  663. * Returns the custom style name for this row.
  664. *
  665. * @return the style name or null if no style name has been set
  666. */
  667. public String getStyleName() {
  668. return styleName;
  669. }
  670. /**
  671. * Sets a custom style name for this row.
  672. *
  673. * @param styleName
  674. * the style name to set or null to not use any style
  675. * name
  676. */
  677. public void setStyleName(String styleName) {
  678. this.styleName = styleName;
  679. section.requestSectionRefresh();
  680. }
  681. /**
  682. * Called when the row is detached from the grid
  683. *
  684. * @since 7.6.3
  685. */
  686. void detach() {
  687. // Avoid calling detach twice for a merged cell
  688. HashSet<CELLTYPE> cells = new HashSet<>();
  689. for (Column<?, ?> column : getSection().grid.getColumns()) {
  690. cells.add(getCell(column));
  691. }
  692. for (CELLTYPE cell : cells) {
  693. cell.detach();
  694. }
  695. }
  696. }
  697. private Grid<?> grid;
  698. private List<ROWTYPE> rows = new ArrayList<>();
  699. private boolean visible = true;
  700. /**
  701. * Creates and returns a new instance of the row type.
  702. *
  703. * @return the created row
  704. */
  705. protected abstract ROWTYPE createRow();
  706. /**
  707. * Informs the grid that this section should be re-rendered.
  708. * <p>
  709. * <b>Note</b> that re-render means calling update() on each cell,
  710. * preAttach()/postAttach()/preDetach()/postDetach() is not called as
  711. * the cells are not removed from the DOM.
  712. */
  713. protected abstract void requestSectionRefresh();
  714. /**
  715. * Sets the visibility of the whole section.
  716. *
  717. * @param visible
  718. * true to show this section, false to hide
  719. */
  720. public void setVisible(boolean visible) {
  721. if (this.visible != visible) {
  722. this.visible = visible;
  723. requestSectionRefresh();
  724. }
  725. }
  726. /**
  727. * Returns the visibility of this section.
  728. *
  729. * @return true if visible, false otherwise.
  730. */
  731. public boolean isVisible() {
  732. return visible;
  733. }
  734. /**
  735. * Inserts a new row at the given position. Shifts the row currently at
  736. * that position and any subsequent rows down (adds one to their
  737. * indices).
  738. *
  739. * @param index
  740. * the position at which to insert the row
  741. * @return the new row
  742. *
  743. * @throws IndexOutOfBoundsException
  744. * if the index is out of bounds
  745. * @see #appendRow()
  746. * @see #prependRow()
  747. * @see #removeRow(int)
  748. * @see #removeRow(StaticRow)
  749. */
  750. public ROWTYPE addRowAt(int index) {
  751. ROWTYPE row = createRow();
  752. row.setSection(this);
  753. for (int i = 0; i < getGrid().getColumnCount(); ++i) {
  754. row.addCell(grid.getColumn(i));
  755. }
  756. rows.add(index, row);
  757. requestSectionRefresh();
  758. return row;
  759. }
  760. /**
  761. * Adds a new row at the top of this section.
  762. *
  763. * @return the new row
  764. * @see #appendRow()
  765. * @see #addRowAt(int)
  766. * @see #removeRow(int)
  767. * @see #removeRow(StaticRow)
  768. */
  769. public ROWTYPE prependRow() {
  770. return addRowAt(0);
  771. }
  772. /**
  773. * Adds a new row at the bottom of this section.
  774. *
  775. * @return the new row
  776. * @see #prependRow()
  777. * @see #addRowAt(int)
  778. * @see #removeRow(int)
  779. * @see #removeRow(StaticRow)
  780. */
  781. public ROWTYPE appendRow() {
  782. return addRowAt(rows.size());
  783. }
  784. /**
  785. * Removes the row at the given position.
  786. *
  787. * @param index
  788. * the position of the row
  789. *
  790. * @throws IndexOutOfBoundsException
  791. * if the index is out of bounds
  792. * @see #addRowAt(int)
  793. * @see #appendRow()
  794. * @see #prependRow()
  795. * @see #removeRow(StaticRow)
  796. */
  797. public void removeRow(int index) {
  798. ROWTYPE row = rows.remove(index);
  799. row.detach();
  800. requestSectionRefresh();
  801. }
  802. /**
  803. * Removes the given row from the section.
  804. *
  805. * @param row
  806. * the row to be removed
  807. *
  808. * @throws IllegalArgumentException
  809. * if the row does not exist in this section
  810. * @see #addRowAt(int)
  811. * @see #appendRow()
  812. * @see #prependRow()
  813. * @see #removeRow(int)
  814. */
  815. public void removeRow(ROWTYPE row) {
  816. try {
  817. removeRow(rows.indexOf(row));
  818. } catch (IndexOutOfBoundsException e) {
  819. throw new IllegalArgumentException(
  820. "Section does not contain the given row");
  821. }
  822. }
  823. /**
  824. * Returns the row at the given position.
  825. *
  826. * @param index
  827. * the position of the row
  828. * @return the row with the given index
  829. *
  830. * @throws IndexOutOfBoundsException
  831. * if the index is out of bounds
  832. */
  833. public ROWTYPE getRow(int index) {
  834. try {
  835. return rows.get(index);
  836. } catch (IndexOutOfBoundsException e) {
  837. throw new IllegalArgumentException(
  838. "Row with index " + index + " does not exist");
  839. }
  840. }
  841. /**
  842. * Returns the number of rows in this section.
  843. *
  844. * @return the number of rows
  845. */
  846. public int getRowCount() {
  847. return rows.size();
  848. }
  849. protected List<ROWTYPE> getRows() {
  850. return rows;
  851. }
  852. protected int getVisibleRowCount() {
  853. return isVisible() ? getRowCount() : 0;
  854. }
  855. protected void addColumn(Column<?, ?> column) {
  856. for (ROWTYPE row : rows) {
  857. row.addCell(column);
  858. }
  859. }
  860. protected void removeColumn(Column<?, ?> column) {
  861. for (ROWTYPE row : rows) {
  862. row.removeCell(column);
  863. }
  864. }
  865. protected void setGrid(Grid<?> grid) {
  866. this.grid = grid;
  867. }
  868. protected Grid<?> getGrid() {
  869. assert grid != null;
  870. return grid;
  871. }
  872. protected void updateColSpans() {
  873. for (ROWTYPE row : rows) {
  874. if (row.hasSpannedCells()) {
  875. row.calculateColspans();
  876. }
  877. }
  878. }
  879. }
  880. /**
  881. * Represents the header section of a Grid. A header consists of a single
  882. * header row containing a header cell for each column. Each cell has a
  883. * simple textual caption.
  884. */
  885. protected static class Header extends StaticSection<HeaderRow> {
  886. private HeaderRow defaultRow;
  887. private boolean markAsDirty = false;
  888. @Override
  889. public void removeRow(int index) {
  890. HeaderRow removedRow = getRow(index);
  891. super.removeRow(index);
  892. if (removedRow == defaultRow) {
  893. setDefaultRow(null);
  894. }
  895. }
  896. /**
  897. * Sets the default row of this header. The default row is a special
  898. * header row providing a user interface for sorting columns.
  899. *
  900. * @param row
  901. * the new default row, or null for no default row
  902. *
  903. * @throws IllegalArgumentException
  904. * this header does not contain the row
  905. */
  906. public void setDefaultRow(HeaderRow row) {
  907. if (row == defaultRow) {
  908. return;
  909. }
  910. if (row != null && !getRows().contains(row)) {
  911. throw new IllegalArgumentException(
  912. "Cannot set a default row that does not exist in the container");
  913. }
  914. if (defaultRow != null) {
  915. defaultRow.setDefault(false);
  916. }
  917. if (row != null) {
  918. row.setDefault(true);
  919. }
  920. defaultRow = row;
  921. requestSectionRefresh();
  922. }
  923. /**
  924. * Returns the current default row of this header. The default row is a
  925. * special header row providing a user interface for sorting columns.
  926. *
  927. * @return the default row or null if no default row set
  928. */
  929. public HeaderRow getDefaultRow() {
  930. return defaultRow;
  931. }
  932. @Override
  933. protected HeaderRow createRow() {
  934. return new HeaderRow();
  935. }
  936. @Override
  937. protected void requestSectionRefresh() {
  938. markAsDirty = true;
  939. /*
  940. * Defer the refresh so if we multiple times call refreshSection()
  941. * (for example when updating cell values) we only get one actual
  942. * refresh in the end.
  943. */
  944. Scheduler.get().scheduleFinally(() -> {
  945. if (markAsDirty) {
  946. markAsDirty = false;
  947. getGrid().refreshHeader();
  948. }
  949. });
  950. }
  951. /**
  952. * Returns the events consumed by the header.
  953. *
  954. * @return a collection of BrowserEvents
  955. */
  956. public Collection<String> getConsumedEvents() {
  957. return Arrays.asList(BrowserEvents.TOUCHSTART,
  958. BrowserEvents.TOUCHMOVE, BrowserEvents.TOUCHEND,
  959. BrowserEvents.TOUCHCANCEL, BrowserEvents.CLICK);
  960. }
  961. @Override
  962. protected void addColumn(Column<?, ?> column) {
  963. super.addColumn(column);
  964. // Add default content for new columns.
  965. if (defaultRow != null) {
  966. column.setDefaultHeaderContent(defaultRow.getCell(column));
  967. }
  968. }
  969. }
  970. /**
  971. * A single row in a grid header section.
  972. *
  973. */
  974. public static class HeaderRow extends StaticSection.StaticRow<HeaderCell> {
  975. private boolean isDefault = false;
  976. protected void setDefault(boolean isDefault) {
  977. this.isDefault = isDefault;
  978. if (isDefault) {
  979. for (Column<?, ?> column : getSection().grid.getColumns()) {
  980. column.setDefaultHeaderContent(getCell(column));
  981. }
  982. }
  983. }
  984. public boolean isDefault() {
  985. return isDefault;
  986. }
  987. @Override
  988. protected HeaderCell createCell() {
  989. return new HeaderCell();
  990. }
  991. }
  992. /**
  993. * A single cell in a grid header row. Has a caption and, if it's in a
  994. * default row, a drag handle.
  995. */
  996. public static class HeaderCell extends StaticSection.StaticCell {
  997. }
  998. /**
  999. * Represents the footer section of a Grid. The footer is always empty.
  1000. */
  1001. protected static class Footer extends StaticSection<FooterRow> {
  1002. private boolean markAsDirty = false;
  1003. @Override
  1004. protected FooterRow createRow() {
  1005. return new FooterRow();
  1006. }
  1007. @Override
  1008. protected void requestSectionRefresh() {
  1009. markAsDirty = true;
  1010. /*
  1011. * Defer the refresh so if we multiple times call refreshSection()
  1012. * (for example when updating cell values) we only get one actual
  1013. * refresh in the end.
  1014. */
  1015. Scheduler.get().scheduleFinally(() -> {
  1016. if (markAsDirty) {
  1017. markAsDirty = false;
  1018. getGrid().refreshFooter();
  1019. }
  1020. });
  1021. }
  1022. }
  1023. /**
  1024. * A single cell in a grid Footer row. Has a textual caption.
  1025. *
  1026. */
  1027. public static class FooterCell extends StaticSection.StaticCell {
  1028. }
  1029. /**
  1030. * A single row in a grid Footer section.
  1031. *
  1032. */
  1033. public static class FooterRow extends StaticSection.StaticRow<FooterCell> {
  1034. @Override
  1035. protected FooterCell createCell() {
  1036. return new FooterCell();
  1037. }
  1038. }
  1039. private static class EditorRequestImpl<T> implements EditorRequest<T> {
  1040. /**
  1041. * A callback interface used to notify the invoker of the editor handler
  1042. * of completed editor requests.
  1043. *
  1044. * @param <T>
  1045. * the row data type
  1046. */
  1047. public static interface RequestCallback<T> {
  1048. /**
  1049. * The method that must be called when the request has been
  1050. * processed correctly.
  1051. *
  1052. * @param request
  1053. * the original request object
  1054. */
  1055. public void onSuccess(EditorRequest<T> request);
  1056. /**
  1057. * The method that must be called when processing the request has
  1058. * produced an aborting error.
  1059. *
  1060. * @param request
  1061. * the original request object
  1062. */
  1063. public void onError(EditorRequest<T> request);
  1064. }
  1065. private Grid<T> grid;
  1066. private final int rowIndex;
  1067. private final int columnIndexDOM;
  1068. private RequestCallback<T> callback;
  1069. private boolean completed = false;
  1070. public EditorRequestImpl(Grid<T> grid, int rowIndex, int columnIndexDOM,
  1071. RequestCallback<T> callback) {
  1072. this.grid = grid;
  1073. this.rowIndex = rowIndex;
  1074. this.columnIndexDOM = columnIndexDOM;
  1075. this.callback = callback;
  1076. }
  1077. @Override
  1078. public int getRowIndex() {
  1079. return rowIndex;
  1080. }
  1081. @Override
  1082. public int getColumnIndex() {
  1083. return columnIndexDOM;
  1084. }
  1085. @Override
  1086. public T getRow() {
  1087. return grid.getDataSource().getRow(rowIndex);
  1088. }
  1089. @Override
  1090. public Grid<T> getGrid() {
  1091. return grid;
  1092. }
  1093. @Override
  1094. public Widget getWidget(Grid.Column<?, T> column) {
  1095. Widget w = grid.getEditorWidget(column);
  1096. assert w != null;
  1097. return w;
  1098. }
  1099. private void complete(String errorMessage,
  1100. Collection<Column<?, T>> errorColumns) {
  1101. if (completed) {
  1102. throw new IllegalStateException(
  1103. "An EditorRequest must be completed exactly once");
  1104. }
  1105. completed = true;
  1106. if (errorColumns == null) {
  1107. errorColumns = Collections.emptySet();
  1108. }
  1109. grid.getEditor().setEditorError(errorMessage, errorColumns);
  1110. }
  1111. @Override
  1112. public void success() {
  1113. complete(null, null);
  1114. if (callback != null) {
  1115. callback.onSuccess(this);
  1116. }
  1117. }
  1118. @Override
  1119. public void failure() {
  1120. complete("", null);
  1121. if (callback != null) {
  1122. callback.onError(this);
  1123. }
  1124. }
  1125. @Override
  1126. public boolean isCompleted() {
  1127. return completed;
  1128. }
  1129. }
  1130. /**
  1131. * A wrapper for native DOM events originating from Grid. In addition to the
  1132. * native event, contains a {@link CellReference} instance specifying which
  1133. * cell the event originated from.
  1134. *
  1135. * @since 7.6
  1136. * @param <T>
  1137. * The row type of the grid
  1138. */
  1139. public static class GridEvent<T> {
  1140. private Event event;
  1141. private EventCellReference<T> cell;
  1142. private boolean handled = false;
  1143. protected GridEvent(Event event, EventCellReference<T> cell) {
  1144. this.event = event;
  1145. this.cell = cell;
  1146. }
  1147. /**
  1148. * Returns the wrapped DOM event.
  1149. *
  1150. * @return the DOM event
  1151. */
  1152. public Event getDomEvent() {
  1153. return event;
  1154. }
  1155. /**
  1156. * Returns the Grid cell this event originated from.
  1157. *
  1158. * @return the event cell
  1159. */
  1160. public EventCellReference<T> getCell() {
  1161. return cell;
  1162. }
  1163. /**
  1164. * Returns the Grid instance this event originated from.
  1165. *
  1166. * @return the grid
  1167. */
  1168. public Grid<T> getGrid() {
  1169. return cell.getGrid();
  1170. }
  1171. /**
  1172. * Check whether this event has already been marked as handled.
  1173. *
  1174. * @return whether this event has already been marked as handled
  1175. */
  1176. public boolean isHandled() {
  1177. return handled;
  1178. }
  1179. /**
  1180. * Set the status of this event. Setting to {@code true} effectively
  1181. * marks this event as having already been handled.
  1182. *
  1183. * @param handled
  1184. */
  1185. public void setHandled(boolean handled) {
  1186. this.handled = handled;
  1187. }
  1188. }
  1189. /**
  1190. * A wrapper for native DOM events related to the {@link Editor Grid editor}
  1191. * .
  1192. *
  1193. * @since 7.6
  1194. * @param <T>
  1195. * the row type of the grid
  1196. */
  1197. public static class EditorDomEvent<T> extends GridEvent<T> {
  1198. private final Widget editorWidget;
  1199. protected EditorDomEvent(Event event, EventCellReference<T> cell,
  1200. Widget editorWidget) {
  1201. super(event, cell);
  1202. this.editorWidget = editorWidget;
  1203. }
  1204. /**
  1205. * Returns the editor of the Grid this event originated from.
  1206. *
  1207. * @return the related editor instance
  1208. */
  1209. public Editor<T> getEditor() {
  1210. return getGrid().getEditor();
  1211. }
  1212. /**
  1213. * Returns the currently focused editor widget.
  1214. *
  1215. * @return the focused editor widget or {@code null} if not editable
  1216. */
  1217. public Widget getEditorWidget() {
  1218. return editorWidget;
  1219. }
  1220. /**
  1221. * Returns the row index the editor is open at. If the editor is not
  1222. * open, returns -1.
  1223. *
  1224. * @return the index of the edited row or -1 if editor is not open
  1225. */
  1226. public int getRowIndex() {
  1227. return getEditor().rowIndex;
  1228. }
  1229. /**
  1230. * Returns the DOM column index (excluding hidden columns) the editor
  1231. * was opened at. If the editor is not open, returns -1.
  1232. *
  1233. * @return the column index or -1 if editor is not open
  1234. */
  1235. public int getFocusedColumnIndex() {
  1236. return getEditor().focusedColumnIndexDOM;
  1237. }
  1238. }
  1239. /**
  1240. * An editor UI for Grid rows. A single Grid row at a time can be opened for
  1241. * editing.
  1242. *
  1243. * @since 7.6
  1244. * @param <T>
  1245. * the row type of the grid
  1246. */
  1247. public static class Editor<T> implements DeferredWorker {
  1248. public static final int KEYCODE_SHOW = KeyCodes.KEY_ENTER;
  1249. public static final int KEYCODE_HIDE = KeyCodes.KEY_ESCAPE;
  1250. private static final String ERROR_CLASS_NAME = "error";
  1251. private static final String NOT_EDITABLE_CLASS_NAME = "not-editable";
  1252. /**
  1253. * A handler for events related to the Grid editor. Responsible for
  1254. * opening, moving or closing the editor based on the received event.
  1255. *
  1256. * @since 7.6
  1257. * @author Vaadin Ltd
  1258. * @param <T>
  1259. * the row type of the grid
  1260. */
  1261. public interface EventHandler<T> {
  1262. /**
  1263. * Handles editor-related events in an appropriate way. Opens,
  1264. * moves, or closes the editor based on the given event.
  1265. *
  1266. * @param event
  1267. * the received event
  1268. * @return true if the event was handled and nothing else should be
  1269. * done, false otherwise
  1270. */
  1271. boolean handleEvent(EditorDomEvent<T> event);
  1272. }
  1273. protected enum State {
  1274. INACTIVE, ACTIVATING, BINDING, ACTIVE, SAVING
  1275. }
  1276. private Grid<T> grid;
  1277. private EditorHandler<T> handler;
  1278. private EventHandler<T> eventHandler = GWT
  1279. .create(DefaultEditorEventHandler.class);
  1280. private DivElement editorOverlay = DivElement.as(DOM.createDiv());
  1281. private DivElement cellWrapper = DivElement.as(DOM.createDiv());
  1282. private DivElement frozenCellWrapper = DivElement.as(DOM.createDiv());
  1283. private DivElement messageAndButtonsWrapper = DivElement
  1284. .as(DOM.createDiv());
  1285. private DivElement messageWrapper = DivElement.as(DOM.createDiv());
  1286. private DivElement buttonsWrapper = DivElement.as(DOM.createDiv());
  1287. // Element which contains the error message for the editor
  1288. // Should only be added to the DOM when there's a message to show
  1289. private DivElement message = DivElement.as(DOM.createDiv());
  1290. private Map<Column<?, T>, Widget> columnToWidget = new HashMap<>();
  1291. private List<HandlerRegistration> focusHandlers = new ArrayList<>();
  1292. private boolean enabled = false;
  1293. private State state = State.INACTIVE;
  1294. private int rowIndex = -1;
  1295. private int focusedColumnIndexDOM = -1;
  1296. private String styleName = null;
  1297. private HandlerRegistration hScrollHandler;
  1298. private final Button saveButton;
  1299. private final Button cancelButton;
  1300. private static final int SAVE_TIMEOUT_MS = 5000;
  1301. private final Timer saveTimeout = new Timer() {
  1302. @Override
  1303. public void run() {
  1304. getLogger().warning(
  1305. "Editor save action is taking longer than expected ("
  1306. + SAVE_TIMEOUT_MS + "ms). Does your "
  1307. + EditorHandler.class.getSimpleName()
  1308. + " remember to call success() or fail()?");
  1309. }
  1310. };
  1311. private final EditorRequestImpl.RequestCallback<T> saveRequestCallback = new EditorRequestImpl.RequestCallback<T>() {
  1312. @Override
  1313. public void onSuccess(EditorRequest<T> request) {
  1314. if (state == State.SAVING) {
  1315. cleanup();
  1316. cancel(true);
  1317. grid.clearSortOrder();
  1318. }
  1319. }
  1320. @Override
  1321. public void onError(EditorRequest<T> request) {
  1322. if (state == State.SAVING) {
  1323. cleanup();
  1324. }
  1325. }
  1326. private void cleanup() {
  1327. state = State.ACTIVE;
  1328. setButtonsEnabled(true);
  1329. saveTimeout.cancel();
  1330. }
  1331. };
  1332. private static final int BIND_TIMEOUT_MS = 5000;
  1333. private final Timer bindTimeout = new Timer() {
  1334. @Override
  1335. public void run() {
  1336. getLogger().warning(
  1337. "Editor bind action is taking longer than expected ("
  1338. + BIND_TIMEOUT_MS + "ms). Does your "
  1339. + EditorHandler.class.getSimpleName()
  1340. + " remember to call success() or fail()?");
  1341. }
  1342. };
  1343. private final EditorRequestImpl.RequestCallback<T> bindRequestCallback = new EditorRequestImpl.RequestCallback<T>() {
  1344. @Override
  1345. public void onSuccess(EditorRequest<T> request) {
  1346. if (state == State.BINDING) {
  1347. state = State.ACTIVE;
  1348. bindTimeout.cancel();
  1349. rowIndex = request.getRowIndex();
  1350. focusedColumnIndexDOM = request.getColumnIndex();
  1351. if (focusedColumnIndexDOM >= 0) {
  1352. // Update internal focus of Grid
  1353. grid.focusCell(rowIndex, focusedColumnIndexDOM);
  1354. }
  1355. showOverlay();
  1356. }
  1357. }
  1358. @Override
  1359. public void onError(EditorRequest<T> request) {
  1360. if (state == State.BINDING) {
  1361. if (rowIndex == -1) {
  1362. doCancel();
  1363. } else {
  1364. state = State.ACTIVE;
  1365. // TODO: Maybe restore focus?
  1366. }
  1367. bindTimeout.cancel();
  1368. }
  1369. }
  1370. };
  1371. /** A set of all the columns that display an error flag. */
  1372. private final Set<Column<?, T>> columnErrors = new HashSet<>();
  1373. private boolean buffered = true;
  1374. /** Original position of editor */
  1375. private double originalTop;
  1376. /** Original scroll position of grid when editor was opened */
  1377. private double originalScrollTop;
  1378. private RowHandle<T> pinnedRowHandle;
  1379. public Editor() {
  1380. saveButton = new Button();
  1381. saveButton.setText(GridConstants.DEFAULT_SAVE_CAPTION);
  1382. saveButton.addClickHandler(event -> {
  1383. save();
  1384. FocusUtil.setFocus(grid, true);
  1385. });
  1386. cancelButton = new Button();
  1387. cancelButton.setText(GridConstants.DEFAULT_CANCEL_CAPTION);
  1388. cancelButton.addClickHandler(event -> {
  1389. cancel();
  1390. FocusUtil.setFocus(grid, true);
  1391. });
  1392. }
  1393. public void setEditorError(String errorMessage,
  1394. Collection<Column<?, T>> errorColumns) {
  1395. if (errorMessage == null) {
  1396. message.removeFromParent();
  1397. } else {
  1398. message.setInnerText(errorMessage);
  1399. if (message.getParentElement() == null) {
  1400. messageWrapper.appendChild(message);
  1401. }
  1402. }
  1403. // In unbuffered mode only show message wrapper if there is an error
  1404. if (!isBuffered()) {
  1405. setMessageAndButtonsWrapperVisible(errorMessage != null);
  1406. }
  1407. if (state == State.ACTIVE || state == State.SAVING) {
  1408. for (Column<?, T> c : grid.getColumns()) {
  1409. grid.getEditor().setEditorColumnError(c,
  1410. errorColumns.contains(c));
  1411. }
  1412. }
  1413. }
  1414. public int getRow() {
  1415. return rowIndex;
  1416. }
  1417. /**
  1418. * If a cell of this Grid had focus once this editRow call was
  1419. * triggered, the editor component at the previously focused column
  1420. * index will be focused.
  1421. *
  1422. * If a Grid cell was not focused prior to calling this method, it will
  1423. * be equivalent to {@code editRow(rowIndex, -1)}.
  1424. *
  1425. * @see #editRow(int, int)
  1426. */
  1427. public void editRow(int rowIndex) {
  1428. // Focus the last focused column in the editor if grid or its child
  1429. // was focused before the edit request
  1430. Cell focusedCell = grid.cellFocusHandler.getFocusedCell();
  1431. Element focusedElement = WidgetUtil.getFocusedElement();
  1432. if (focusedCell != null && focusedElement != null
  1433. && grid.getElement().isOrHasChild(focusedElement)) {
  1434. editRow(rowIndex, focusedCell.getColumn());
  1435. } else {
  1436. editRow(rowIndex, -1);
  1437. }
  1438. }
  1439. /**
  1440. * Opens the editor over the row with the given index and attempts to
  1441. * focus the editor widget in the given column index. Does not move
  1442. * focus if the widget is not focusable or if the column index is -1.
  1443. *
  1444. * @param rowIndex
  1445. * the index of the row to be edited
  1446. * @param columnIndexDOM
  1447. * the column index (excluding hidden columns) of the editor
  1448. * widget that should be initially focused or -1 to not set
  1449. * focus
  1450. *
  1451. * @throws IllegalStateException
  1452. * if this editor is not enabled
  1453. * @throws IllegalStateException
  1454. * if this editor is already in edit mode and in buffered
  1455. * mode
  1456. *
  1457. * @since 7.5
  1458. */
  1459. public void editRow(final int rowIndex, final int columnIndexDOM) {
  1460. if (!enabled) {
  1461. throw new IllegalStateException(
  1462. "Cannot edit row: editor is not enabled");
  1463. }
  1464. if (isWorkPending()) {
  1465. // Request pending a response, don't move try to start another
  1466. // request.
  1467. return;
  1468. }
  1469. if (state != State.INACTIVE && this.rowIndex != rowIndex) {
  1470. if (isBuffered()) {
  1471. throw new IllegalStateException(
  1472. "Cannot edit row: editor already in edit mode");
  1473. } else if (!columnErrors.isEmpty()) {
  1474. // Don't move row if errors are present
  1475. // FIXME: Should attempt bind if error field values have
  1476. // changed.
  1477. return;
  1478. }
  1479. }
  1480. if (columnIndexDOM >= grid.getVisibleColumns().size()) {
  1481. throw new IllegalArgumentException(
  1482. "Edited column index " + columnIndexDOM
  1483. + " was bigger than visible column count.");
  1484. }
  1485. if (this.rowIndex == rowIndex
  1486. && focusedColumnIndexDOM == columnIndexDOM) {
  1487. // NO-OP
  1488. return;
  1489. }
  1490. if (this.rowIndex == rowIndex) {
  1491. if (focusedColumnIndexDOM != columnIndexDOM) {
  1492. if (columnIndexDOM >= grid.getFrozenColumnCount()) {
  1493. // Scroll to new focused column.
  1494. grid.getEscalator().scrollToColumn(columnIndexDOM,
  1495. ScrollDestination.ANY, 0);
  1496. }
  1497. focusedColumnIndexDOM = columnIndexDOM;
  1498. }
  1499. updateHorizontalScrollPosition();
  1500. // Update Grid internal focus and focus widget if possible
  1501. if (focusedColumnIndexDOM >= 0) {
  1502. grid.focusCell(rowIndex, focusedColumnIndexDOM);
  1503. focusColumn(focusedColumnIndexDOM);
  1504. }
  1505. // No need to request anything from the editor handler.
  1506. return;
  1507. }
  1508. state = State.ACTIVATING;
  1509. grid.scrollToRow(rowIndex, ScrollDestination.ANY,
  1510. () -> show(rowIndex, columnIndexDOM));
  1511. }
  1512. /**
  1513. * Cancels the currently active edit and hides the editor. Any changes
  1514. * that are not {@link #save() saved} are lost.
  1515. *
  1516. * @throws IllegalStateException
  1517. * if this editor is not enabled
  1518. * @throws IllegalStateException
  1519. * if this editor is not in edit mode
  1520. */
  1521. public void cancel() {
  1522. cancel(false);
  1523. }
  1524. private void cancel(boolean afterSave) {
  1525. if (!enabled) {
  1526. throw new IllegalStateException(
  1527. "Cannot cancel edit: editor is not enabled");
  1528. }
  1529. if (state == State.INACTIVE) {
  1530. throw new IllegalStateException(
  1531. "Cannot cancel edit: editor is not in edit mode");
  1532. }
  1533. handler.cancel(new EditorRequestImpl<>(grid, rowIndex,
  1534. focusedColumnIndexDOM, null), afterSave);
  1535. doCancel();
  1536. }
  1537. private void doCancel() {
  1538. hideOverlay();
  1539. state = State.INACTIVE;
  1540. rowIndex = -1;
  1541. focusedColumnIndexDOM = -1;
  1542. grid.getEscalator().setScrollLocked(Direction.VERTICAL, false);
  1543. updateSelectionCheckboxesAsNeeded(true);
  1544. }
  1545. private void updateSelectionCheckboxesAsNeeded(boolean isEnabled) {
  1546. // FIXME: This is too much guessing. Define a better way to do this.
  1547. if (grid.selectionColumn != null && grid.selectionColumn
  1548. .getRenderer() instanceof MultiSelectionRenderer) {
  1549. grid.refreshBody();
  1550. HeaderCell cell = grid.getDefaultHeaderRow()
  1551. .getCell(grid.selectionColumn);
  1552. // if lazy provider, then no checkbox
  1553. if (cell.getType() == GridStaticCellType.WIDGET) {
  1554. CheckBox checkBox = (CheckBox) grid.getDefaultHeaderRow()
  1555. .getCell(grid.selectionColumn).getWidget();
  1556. checkBox.setEnabled(isEnabled);
  1557. }
  1558. }
  1559. }
  1560. /**
  1561. * Saves any unsaved changes to the data source and hides the editor.
  1562. *
  1563. * @throws IllegalStateException
  1564. * if this editor is not enabled
  1565. * @throws IllegalStateException
  1566. * if this editor is not in edit mode
  1567. */
  1568. public void save() {
  1569. if (!enabled) {
  1570. throw new IllegalStateException(
  1571. "Cannot save: editor is not enabled");
  1572. }
  1573. if (state != State.ACTIVE) {
  1574. throw new IllegalStateException(
  1575. "Cannot save: editor is not in edit mode");
  1576. }
  1577. state = State.SAVING;
  1578. setButtonsEnabled(false);
  1579. saveTimeout.schedule(SAVE_TIMEOUT_MS);
  1580. EditorRequest<T> request = new EditorRequestImpl<>(grid, rowIndex,
  1581. focusedColumnIndexDOM, saveRequestCallback);
  1582. handler.save(request);
  1583. updateSelectionCheckboxesAsNeeded(true);
  1584. }
  1585. /**
  1586. * Returns the handler responsible for binding data and editor widgets
  1587. * to this editor.
  1588. *
  1589. * @return the editor handler or null if not set
  1590. */
  1591. public EditorHandler<T> getHandler() {
  1592. return handler;
  1593. }
  1594. /**
  1595. * Sets the handler responsible for binding data and editor widgets to
  1596. * this editor.
  1597. *
  1598. * @param rowHandler
  1599. * the new editor handler
  1600. *
  1601. * @throws IllegalStateException
  1602. * if this editor is currently in edit mode
  1603. */
  1604. public void setHandler(EditorHandler<T> rowHandler) {
  1605. if (state != State.INACTIVE) {
  1606. throw new IllegalStateException(
  1607. "Cannot set EditorHandler: editor is currently in edit mode");
  1608. }
  1609. handler = rowHandler;
  1610. }
  1611. public boolean isEnabled() {
  1612. return enabled;
  1613. }
  1614. /**
  1615. * Sets the enabled state of this editor.
  1616. *
  1617. * @param enabled
  1618. * true if enabled, false otherwise
  1619. *
  1620. * @throws IllegalStateException
  1621. * if in edit mode and trying to disable
  1622. * @throws IllegalStateException
  1623. * if the editor handler is not set
  1624. */
  1625. public void setEnabled(boolean enabled) {
  1626. if (!enabled && state != State.INACTIVE) {
  1627. throw new IllegalStateException(
  1628. "Cannot disable: editor is in edit mode");
  1629. } else if (enabled && getHandler() == null) {
  1630. throw new IllegalStateException(
  1631. "Cannot enable: EditorHandler not set");
  1632. }
  1633. this.enabled = enabled;
  1634. }
  1635. protected void show(int rowIndex, int columnIndex) {
  1636. if (state == State.ACTIVATING) {
  1637. state = State.BINDING;
  1638. bindTimeout.schedule(BIND_TIMEOUT_MS);
  1639. EditorRequest<T> request = new EditorRequestImpl<>(grid,
  1640. rowIndex, columnIndex, bindRequestCallback);
  1641. handler.bind(request);
  1642. grid.getEscalator().setScrollLocked(Direction.VERTICAL,
  1643. isBuffered());
  1644. updateSelectionCheckboxesAsNeeded(false);
  1645. }
  1646. }
  1647. protected void setGrid(final Grid<T> grid) {
  1648. assert grid != null : "Grid cannot be null";
  1649. assert this.grid == null : "Can only attach editor to Grid once";
  1650. this.grid = grid;
  1651. }
  1652. protected State getState() {
  1653. return state;
  1654. }
  1655. protected void setState(State state) {
  1656. this.state = state;
  1657. }
  1658. /**
  1659. * Returns the editor widget associated with the given column. If the
  1660. * editor is not active or the column is not
  1661. * {@link Grid.Column#isEditable() editable}, returns null.
  1662. *
  1663. * @param column
  1664. * the column
  1665. * @return the widget if the editor is open and the column is editable,
  1666. * null otherwise
  1667. */
  1668. protected Widget getWidget(Column<?, T> column) {
  1669. return columnToWidget.get(column);
  1670. }
  1671. /**
  1672. * Equivalent to {@code showOverlay()}. The argument is ignored.
  1673. *
  1674. * @param unused
  1675. * ignored argument
  1676. *
  1677. * @deprecated As of 7.5, use {@link #showOverlay()} instead.
  1678. */
  1679. @Deprecated
  1680. protected void showOverlay(TableRowElement unused) {
  1681. showOverlay();
  1682. }
  1683. /**
  1684. * Opens the editor overlay over the table row indicated by
  1685. * {@link #getRow()}.
  1686. *
  1687. * @since 7.5
  1688. */
  1689. protected void showOverlay() {
  1690. // Ensure overlay is hidden initially
  1691. hideOverlay();
  1692. DivElement gridElement = DivElement.as(grid.getElement());
  1693. TableRowElement tr = grid.getEscalator().getBody()
  1694. .getRowElement(rowIndex);
  1695. hScrollHandler = grid.addScrollHandler(event -> {
  1696. updateHorizontalScrollPosition();
  1697. updateVerticalScrollPosition();
  1698. });
  1699. gridElement.appendChild(editorOverlay);
  1700. editorOverlay.appendChild(frozenCellWrapper);
  1701. editorOverlay.appendChild(cellWrapper);
  1702. editorOverlay.appendChild(messageAndButtonsWrapper);
  1703. updateBufferedStyleName();
  1704. // Add class name with selected modifier if the editor is being
  1705. // opened on selected row, see #11634
  1706. String selectedStylename = styleName + "-selected";
  1707. if (grid.isSelected(grid.getDataSource().getRow(getRow()))) {
  1708. cellWrapper.addClassName(selectedStylename);
  1709. } else {
  1710. cellWrapper.removeClassName(selectedStylename);
  1711. }
  1712. int frozenColumns = grid.getVisibleFrozenColumnCount();
  1713. double frozenColumnsWidth = 0;
  1714. double cellHeight = 0;
  1715. for (int i = 0; i < tr.getCells().getLength(); i++) {
  1716. Element cell = createCell(tr.getCells().getItem(i));
  1717. cellHeight = Math.max(cellHeight,
  1718. WidgetUtil.getRequiredHeightBoundingClientRectDouble(
  1719. tr.getCells().getItem(i)));
  1720. Column<?, T> column = grid.getVisibleColumn(i);
  1721. if (i < frozenColumns) {
  1722. frozenCellWrapper.appendChild(cell);
  1723. frozenColumnsWidth += WidgetUtil
  1724. .getRequiredWidthBoundingClientRectDouble(
  1725. tr.getCells().getItem(i));
  1726. } else {
  1727. cellWrapper.appendChild(cell);
  1728. }
  1729. if (column.isEditable()) {
  1730. Widget editor = getHandler().getWidget(column);
  1731. if (editor != null) {
  1732. columnToWidget.put(column, editor);
  1733. grid.attachWidget(editor, cell);
  1734. }
  1735. if (i == focusedColumnIndexDOM) {
  1736. focusColumn(focusedColumnIndexDOM);
  1737. }
  1738. } else {
  1739. cell.addClassName(NOT_EDITABLE_CLASS_NAME);
  1740. cell.addClassName(tr.getCells().getItem(i).getClassName());
  1741. // If the focused or frozen stylename is present it should
  1742. // not be inherited by the editor cell as it is not useful
  1743. // in the editor and would look broken without additional
  1744. // style rules. This is a bit of a hack.
  1745. cell.removeClassName(grid.cellFocusStyleName);
  1746. cell.removeClassName("frozen");
  1747. if (column == grid.selectionColumn) {
  1748. // Duplicate selection column CheckBox
  1749. pinnedRowHandle = grid.getDataSource().getHandle(
  1750. grid.getDataSource().getRow(rowIndex));
  1751. pinnedRowHandle.pin();
  1752. // We need to duplicate the selection CheckBox for the
  1753. // editor overlay since the original one is hidden by
  1754. // the overlay
  1755. final CheckBox checkBox = GWT.create(CheckBox.class);
  1756. checkBox.setValue(
  1757. grid.isSelected(pinnedRowHandle.getRow()));
  1758. checkBox.sinkEvents(Event.ONCLICK);
  1759. checkBox.addClickHandler(event -> {
  1760. T row = pinnedRowHandle.getRow();
  1761. if (grid.isSelected(row)) {
  1762. grid.deselect(row);
  1763. } else {
  1764. grid.select(row);
  1765. }
  1766. });
  1767. grid.attachWidget(checkBox, cell);
  1768. columnToWidget.put(column, checkBox);
  1769. // Only enable CheckBox in non-buffered mode
  1770. checkBox.setEnabled(!isBuffered());
  1771. } else if (!(column
  1772. .getRenderer() instanceof WidgetRenderer)) {
  1773. // Copy non-widget content directly
  1774. cell.setInnerHTML(
  1775. tr.getCells().getItem(i).getInnerHTML());
  1776. }
  1777. }
  1778. }
  1779. setBounds(frozenCellWrapper, 0, 0, frozenColumnsWidth, 0);
  1780. setBounds(cellWrapper, frozenColumnsWidth, 0,
  1781. tr.getOffsetWidth() - frozenColumnsWidth, cellHeight);
  1782. // Only add these elements once
  1783. if (!messageAndButtonsWrapper.isOrHasChild(messageWrapper)) {
  1784. messageAndButtonsWrapper.appendChild(messageWrapper);
  1785. messageAndButtonsWrapper.appendChild(buttonsWrapper);
  1786. }
  1787. if (isBuffered()) {
  1788. grid.attachWidget(saveButton, buttonsWrapper);
  1789. grid.attachWidget(cancelButton, buttonsWrapper);
  1790. }
  1791. setMessageAndButtonsWrapperVisible(isBuffered());
  1792. updateHorizontalScrollPosition();
  1793. AbstractRowContainer body = (AbstractRowContainer) grid
  1794. .getEscalator().getBody();
  1795. double rowTop = body.getRowTop(tr);
  1796. int bodyTop = body.getElement().getAbsoluteTop();
  1797. int gridTop = gridElement.getAbsoluteTop();
  1798. double overlayTop = rowTop + bodyTop - gridTop;
  1799. originalScrollTop = grid.getScrollTop();
  1800. if (!isBuffered() || buttonsShouldBeRenderedBelow(tr)) {
  1801. // Default case, editor buttons are below the edited row
  1802. editorOverlay.getStyle().setTop(overlayTop, Unit.PX);
  1803. originalTop = overlayTop;
  1804. editorOverlay.getStyle().clearBottom();
  1805. } else {
  1806. // Move message and buttons wrapper on top of cell wrapper if
  1807. // there is not enough space visible space under and fix the
  1808. // overlay from the bottom
  1809. editorOverlay.insertFirst(messageAndButtonsWrapper);
  1810. int gridHeight = grid.getElement().getOffsetHeight();
  1811. editorOverlay.getStyle().setBottom(
  1812. gridHeight - overlayTop - tr.getOffsetHeight(),
  1813. Unit.PX);
  1814. editorOverlay.getStyle().clearTop();
  1815. }
  1816. // Do not render over the vertical scrollbar
  1817. editorOverlay.getStyle().setWidth(grid.escalator.getInnerWidth(),
  1818. Unit.PX);
  1819. }
  1820. private void focusColumn(int columnIndexDOM) {
  1821. if (columnIndexDOM < 0
  1822. || columnIndexDOM >= grid.getVisibleColumns().size()) {
  1823. // NO-OP
  1824. return;
  1825. }
  1826. Widget editor = getWidget(grid.getVisibleColumn(columnIndexDOM));
  1827. if (editor instanceof Focusable) {
  1828. ((Focusable) editor).focus();
  1829. } else if (editor instanceof com.google.gwt.user.client.ui.Focusable) {
  1830. ((com.google.gwt.user.client.ui.Focusable) editor)
  1831. .setFocus(true);
  1832. } else {
  1833. grid.focus();
  1834. }
  1835. }
  1836. private boolean buttonsShouldBeRenderedBelow(TableRowElement tr) {
  1837. TableSectionElement tfoot = grid.escalator.getFooter().getElement();
  1838. double tfootPageTop = WidgetUtil.getBoundingClientRect(tfoot)
  1839. .getTop();
  1840. double trPageBottom = WidgetUtil.getBoundingClientRect(tr)
  1841. .getBottom();
  1842. int messageAndButtonsHeight = messageAndButtonsWrapper
  1843. .getOffsetHeight();
  1844. double bottomOfButtons = trPageBottom + messageAndButtonsHeight;
  1845. return bottomOfButtons < tfootPageTop;
  1846. }
  1847. protected void hideOverlay() {
  1848. if (editorOverlay.getParentElement() == null) {
  1849. return;
  1850. }
  1851. if (pinnedRowHandle != null) {
  1852. pinnedRowHandle.unpin();
  1853. pinnedRowHandle = null;
  1854. }
  1855. for (HandlerRegistration r : focusHandlers) {
  1856. r.removeHandler();
  1857. }
  1858. focusHandlers.clear();
  1859. for (Widget w : columnToWidget.values()) {
  1860. setParent(w, null);
  1861. }
  1862. columnToWidget.clear();
  1863. if (isBuffered()) {
  1864. grid.detachWidget(saveButton);
  1865. grid.detachWidget(cancelButton);
  1866. }
  1867. editorOverlay.removeAllChildren();
  1868. cellWrapper.removeAllChildren();
  1869. frozenCellWrapper.removeAllChildren();
  1870. editorOverlay.removeFromParent();
  1871. hScrollHandler.removeHandler();
  1872. clearEditorColumnErrors();
  1873. }
  1874. private void updateBufferedStyleName() {
  1875. if (isBuffered()) {
  1876. editorOverlay.removeClassName("unbuffered");
  1877. editorOverlay.addClassName("buffered");
  1878. } else {
  1879. editorOverlay.removeClassName("buffered");
  1880. editorOverlay.addClassName("unbuffered");
  1881. }
  1882. }
  1883. protected void setStylePrimaryName(String primaryName) {
  1884. if (styleName != null) {
  1885. editorOverlay.removeClassName(styleName);
  1886. cellWrapper.removeClassName(styleName + "-cells");
  1887. frozenCellWrapper.removeClassName(styleName + "-cells");
  1888. messageAndButtonsWrapper.removeClassName(styleName + "-footer");
  1889. messageWrapper.removeClassName(styleName + "-message");
  1890. buttonsWrapper.removeClassName(styleName + "-buttons");
  1891. saveButton.removeStyleName(styleName + "-save");
  1892. cancelButton.removeStyleName(styleName + "-cancel");
  1893. }
  1894. styleName = primaryName + "-editor";
  1895. editorOverlay.setClassName(styleName);
  1896. cellWrapper.setClassName(styleName + "-cells");
  1897. frozenCellWrapper.setClassName(styleName + "-cells frozen");
  1898. messageAndButtonsWrapper.setClassName(styleName + "-footer");
  1899. messageWrapper.setClassName(styleName + "-message");
  1900. buttonsWrapper.setClassName(styleName + "-buttons");
  1901. saveButton.setStyleName(styleName + "-save");
  1902. cancelButton.setStyleName(styleName + "-cancel");
  1903. }
  1904. /**
  1905. * Creates an editor cell corresponding to the given table cell. The
  1906. * returned element is empty and has the same dimensions and position as
  1907. * the table cell.
  1908. *
  1909. * @param td
  1910. * the table cell used as a reference
  1911. * @return an editor cell corresponding to the given cell
  1912. */
  1913. protected Element createCell(TableCellElement td) {
  1914. DivElement cell = DivElement.as(DOM.createDiv());
  1915. double width = WidgetUtil
  1916. .getRequiredWidthBoundingClientRectDouble(td);
  1917. double height = WidgetUtil
  1918. .getRequiredHeightBoundingClientRectDouble(td);
  1919. setBounds(cell, td.getOffsetLeft(), td.getOffsetTop(), width,
  1920. height);
  1921. return cell;
  1922. }
  1923. private static void setBounds(Element e, double left, double top,
  1924. double width, double height) {
  1925. Style style = e.getStyle();
  1926. style.setLeft(left, Unit.PX);
  1927. style.setTop(top, Unit.PX);
  1928. style.setWidth(width, Unit.PX);
  1929. style.setHeight(height, Unit.PX);
  1930. }
  1931. private void updateHorizontalScrollPosition() {
  1932. double scrollLeft = grid.getScrollLeft();
  1933. cellWrapper.getStyle().setLeft(
  1934. frozenCellWrapper.getOffsetWidth() - scrollLeft, Unit.PX);
  1935. }
  1936. /**
  1937. * Moves the editor overlay on scroll so that it stays on top of the
  1938. * edited row. This will also snap the editor to top or bottom of the
  1939. * row container if the edited row is scrolled out of the visible area.
  1940. */
  1941. private void updateVerticalScrollPosition() {
  1942. if (isBuffered()) {
  1943. return;
  1944. }
  1945. double newScrollTop = grid.getScrollTop();
  1946. int gridTop = grid.getElement().getAbsoluteTop();
  1947. int editorHeight = editorOverlay.getOffsetHeight();
  1948. Escalator escalator = grid.getEscalator();
  1949. TableSectionElement header = escalator.getHeader().getElement();
  1950. int footerTop = escalator.getFooter().getElement().getAbsoluteTop();
  1951. int headerBottom = header.getAbsoluteBottom();
  1952. double newTop = originalTop - (newScrollTop - originalScrollTop);
  1953. if (newTop + gridTop < headerBottom) {
  1954. // Snap editor to top of the row container
  1955. newTop = header.getOffsetHeight();
  1956. } else if (newTop + gridTop > footerTop - editorHeight) {
  1957. // Snap editor to the bottom of the row container
  1958. newTop = footerTop - editorHeight - gridTop;
  1959. }
  1960. editorOverlay.getStyle().setTop(newTop, Unit.PX);
  1961. }
  1962. protected void setGridEnabled(boolean enabled) {
  1963. // TODO: This should be informed to handler as well so possible
  1964. // fields can be disabled.
  1965. setButtonsEnabled(enabled);
  1966. }
  1967. private void setButtonsEnabled(boolean enabled) {
  1968. saveButton.setEnabled(enabled);
  1969. cancelButton.setEnabled(enabled);
  1970. }
  1971. public void setSaveCaption(String saveCaption)
  1972. throws IllegalArgumentException {
  1973. if (saveCaption == null) {
  1974. throw new IllegalArgumentException(
  1975. "Save caption cannot be null");
  1976. }
  1977. saveButton.setText(saveCaption);
  1978. }
  1979. public String getSaveCaption() {
  1980. return saveButton.getText();
  1981. }
  1982. public void setCancelCaption(String cancelCaption)
  1983. throws IllegalArgumentException {
  1984. if (cancelCaption == null) {
  1985. throw new IllegalArgumentException(
  1986. "Cancel caption cannot be null");
  1987. }
  1988. cancelButton.setText(cancelCaption);
  1989. }
  1990. public String getCancelCaption() {
  1991. return cancelButton.getText();
  1992. }
  1993. public void setEditorColumnError(Column<?, T> column,
  1994. boolean hasError) {
  1995. if (state != State.ACTIVE && state != State.SAVING) {
  1996. throw new IllegalStateException("Cannot set cell error "
  1997. + "status: editor is neither active nor saving.");
  1998. }
  1999. if (isEditorColumnError(column) == hasError) {
  2000. return;
  2001. }
  2002. Element editorCell = getWidget(column).getElement()
  2003. .getParentElement();
  2004. if (hasError) {
  2005. editorCell.addClassName(ERROR_CLASS_NAME);
  2006. columnErrors.add(column);
  2007. } else {
  2008. editorCell.removeClassName(ERROR_CLASS_NAME);
  2009. columnErrors.remove(column);
  2010. }
  2011. }
  2012. public void clearEditorColumnErrors() {
  2013. /*
  2014. * editorOverlay has no children if it's not active, effectively
  2015. * making this loop a NOOP.
  2016. */
  2017. Element e = editorOverlay.getFirstChildElement();
  2018. while (e != null) {
  2019. e.removeClassName(ERROR_CLASS_NAME);
  2020. e = e.getNextSiblingElement();
  2021. }
  2022. columnErrors.clear();
  2023. }
  2024. public boolean isEditorColumnError(Column<?, T> column) {
  2025. return columnErrors.contains(column);
  2026. }
  2027. public void setBuffered(boolean buffered) {
  2028. this.buffered = buffered;
  2029. setMessageAndButtonsWrapperVisible(buffered);
  2030. }
  2031. public boolean isBuffered() {
  2032. return buffered;
  2033. }
  2034. private void setMessageAndButtonsWrapperVisible(boolean visible) {
  2035. if (visible) {
  2036. messageAndButtonsWrapper.getStyle().clearDisplay();
  2037. } else {
  2038. messageAndButtonsWrapper.getStyle().setDisplay(Display.NONE);
  2039. }
  2040. }
  2041. /**
  2042. * Sets the event handler for this Editor.
  2043. *
  2044. * @since 7.6
  2045. * @param handler
  2046. * the new event handler
  2047. */
  2048. public void setEventHandler(EventHandler<T> handler) {
  2049. eventHandler = handler;
  2050. }
  2051. /**
  2052. * Returns the event handler of this Editor.
  2053. *
  2054. * @since 7.6
  2055. * @return the current event handler
  2056. */
  2057. public EventHandler<T> getEventHandler() {
  2058. return eventHandler;
  2059. }
  2060. @Override
  2061. public boolean isWorkPending() {
  2062. return saveTimeout.isRunning() || bindTimeout.isRunning();
  2063. }
  2064. protected int getElementColumn(Element e) {
  2065. int frozenCells = frozenCellWrapper.getChildCount();
  2066. if (frozenCellWrapper.isOrHasChild(e)) {
  2067. for (int i = 0; i < frozenCells; ++i) {
  2068. if (frozenCellWrapper.getChild(i).isOrHasChild(e)) {
  2069. return i;
  2070. }
  2071. }
  2072. }
  2073. if (cellWrapper.isOrHasChild(e)) {
  2074. for (int i = 0; i < cellWrapper.getChildCount(); ++i) {
  2075. if (cellWrapper.getChild(i).isOrHasChild(e)) {
  2076. return i + frozenCells;
  2077. }
  2078. }
  2079. }
  2080. return -1;
  2081. }
  2082. }
  2083. public abstract static class AbstractGridKeyEvent<HANDLER extends AbstractGridKeyEventHandler>
  2084. extends KeyEvent<HANDLER> {
  2085. /**
  2086. * @since 7.7.9
  2087. */
  2088. public AbstractGridKeyEvent() {
  2089. }
  2090. /**
  2091. * @deprecated This constructor's arguments are no longer used. Use the
  2092. * no-args constructor instead.
  2093. */
  2094. @Deprecated
  2095. public AbstractGridKeyEvent(Grid<?> grid, CellReference<?> targetCell) {
  2096. }
  2097. protected abstract String getBrowserEventType();
  2098. /**
  2099. * Gets the Grid instance for this event, if it originated from a Grid.
  2100. *
  2101. * @return the grid this event originated from, or {@code null} if this
  2102. * event did not originate from a grid
  2103. */
  2104. public Grid<?> getGrid() {
  2105. EventTarget target = getNativeEvent().getEventTarget();
  2106. if (!Element.is(target)) {
  2107. return null;
  2108. }
  2109. return WidgetUtil.findWidget(Element.as(target), Grid.class, false);
  2110. }
  2111. /**
  2112. * Gets the reference of target cell for this event, if this event
  2113. * originated from a Grid.
  2114. *
  2115. * @return target cell, or {@code null} if this event did not originate
  2116. * from a grid
  2117. */
  2118. public CellReference<?> getFocusedCell() {
  2119. return getGrid().getEventCell();
  2120. }
  2121. @Override
  2122. protected void dispatch(HANDLER handler) {
  2123. EventTarget target = getNativeEvent().getEventTarget();
  2124. Grid<?> grid = getGrid();
  2125. if (Element.is(target) && grid != null) {
  2126. final RowContainer container = Stream
  2127. .of(grid.escalator.getHeader(),
  2128. grid.escalator.getBody(),
  2129. grid.escalator.getFooter())
  2130. .filter(c -> c.getCell(target.cast()) != null)
  2131. .findFirst()
  2132. .orElse(grid.cellFocusHandler.containerWithFocus);
  2133. Section section = Section.FOOTER;
  2134. if (container == grid.escalator.getHeader()) {
  2135. section = Section.HEADER;
  2136. } else if (container == getGrid().escalator.getBody()) {
  2137. section = Section.BODY;
  2138. }
  2139. // Don't handle event of child widget unless the column has been
  2140. // explicitly permitted to do so
  2141. if (grid.isElementInChildWidget(Element.as(target))) {
  2142. Cell cell = container.getCell(target.cast());
  2143. if (cell != null) {
  2144. Column<?, ?> column = grid
  2145. .getVisibleColumn(cell.getColumn());
  2146. if (column == null || !column.isHandleWidgetEvents()) {
  2147. return;
  2148. }
  2149. }
  2150. }
  2151. doDispatch(handler, section);
  2152. }
  2153. }
  2154. protected abstract void doDispatch(HANDLER handler, Section section);
  2155. }
  2156. public abstract static class AbstractGridMouseEvent<HANDLER extends AbstractGridMouseEventHandler>
  2157. extends MouseEvent<HANDLER> {
  2158. /**
  2159. * @since 7.7.9
  2160. */
  2161. public AbstractGridMouseEvent() {
  2162. }
  2163. /**
  2164. * @deprecated This constructor's arguments are no longer used. Use the
  2165. * no-args constructor instead.
  2166. */
  2167. @Deprecated
  2168. public AbstractGridMouseEvent(Grid<?> grid,
  2169. CellReference<?> targetCell) {
  2170. }
  2171. protected abstract String getBrowserEventType();
  2172. /**
  2173. * Gets the Grid instance for this event, if it originated from a Grid.
  2174. *
  2175. * @return the grid this event originated from, or {@code null} if this
  2176. * event did not originate from a grid
  2177. */
  2178. public Grid<?> getGrid() {
  2179. EventTarget target = getNativeEvent().getEventTarget();
  2180. if (!Element.is(target)) {
  2181. return null;
  2182. }
  2183. return WidgetUtil.findWidget(Element.as(target), Grid.class, false);
  2184. }
  2185. /**
  2186. * Gets the reference of target cell for this event, if this event
  2187. * originated from a Grid.
  2188. *
  2189. * @return target cell, or {@code null} if this event did not originate
  2190. * from a grid
  2191. */
  2192. public CellReference<?> getTargetCell() {
  2193. Grid<?> grid = getGrid();
  2194. if (grid == null) {
  2195. return null;
  2196. }
  2197. return grid.getEventCell();
  2198. }
  2199. @Override
  2200. protected void dispatch(HANDLER handler) {
  2201. EventTarget target = getNativeEvent().getEventTarget();
  2202. if (!Element.is(target)) {
  2203. // Target is not an element
  2204. return;
  2205. }
  2206. Grid<?> grid = getGrid();
  2207. if (grid == null) {
  2208. // Target is not an element of a grid
  2209. return;
  2210. }
  2211. Element targetElement = Element.as(target);
  2212. if (ignoreEventFromTarget(grid, targetElement)) {
  2213. // Event on this target should be ignored
  2214. return;
  2215. }
  2216. final RowContainer container = grid.escalator
  2217. .findRowContainer(targetElement);
  2218. if (container == null) {
  2219. // No container for given element
  2220. return;
  2221. }
  2222. Section section = Section.FOOTER;
  2223. if (container == grid.escalator.getHeader()) {
  2224. section = Section.HEADER;
  2225. } else if (container == grid.escalator.getBody()) {
  2226. section = Section.BODY;
  2227. }
  2228. doDispatch(handler, section);
  2229. }
  2230. /**
  2231. * Returns whether the mouse event on the target element should be
  2232. * ignored.
  2233. *
  2234. * @param grid
  2235. * the {@code Grid} instance from which the event originated
  2236. * @param targetElement
  2237. * the element from which the event originated
  2238. * @return {@code true} if the event should be ignored, {@code false} if
  2239. * it should be handled
  2240. * @since 8.2
  2241. */
  2242. protected boolean ignoreEventFromTarget(Grid<?> grid,
  2243. Element targetElement) {
  2244. boolean childWidget = grid.isElementInChildWidget(targetElement);
  2245. boolean handleWidgetEvent = false;
  2246. RowContainer container = grid.getEscalator()
  2247. .findRowContainer(targetElement);
  2248. if (container != null) {
  2249. Cell cell = container.getCell(targetElement);
  2250. if (cell != null) {
  2251. Column<?, ?> column = grid
  2252. .getVisibleColumn(cell.getColumn());
  2253. handleWidgetEvent = column != null
  2254. && column.isHandleWidgetEvents();
  2255. }
  2256. }
  2257. return childWidget && !handleWidgetEvent;
  2258. }
  2259. protected abstract void doDispatch(HANDLER handler, Section section);
  2260. }
  2261. private static final String CUSTOM_STYLE_PROPERTY_NAME = "customStyle";
  2262. /**
  2263. * An initial height that is given to new details rows before rendering the
  2264. * appropriate widget that we then can be measure
  2265. *
  2266. * @see Grid.GridSpacerUpdater
  2267. */
  2268. private static final double DETAILS_ROW_INITIAL_HEIGHT = 50;
  2269. private EventCellReference<T> eventCell = new EventCellReference<T>(this);
  2270. private class CellFocusHandler {
  2271. private RowContainer containerWithFocus = escalator.getBody();
  2272. private int rowWithFocus = 0;
  2273. private Range cellFocusRange = Range.withLength(0, 1);
  2274. private int lastFocusedBodyRow = 0;
  2275. private int lastFocusedHeaderRow = 0;
  2276. private int lastFocusedFooterRow = 0;
  2277. private TableCellElement cellWithFocusStyle = null;
  2278. private TableRowElement rowWithFocusStyle = null;
  2279. public CellFocusHandler() {
  2280. sinkEvents(getNavigationEvents());
  2281. }
  2282. private Cell getFocusedCell() {
  2283. return new Cell(rowWithFocus, cellFocusRange.getStart(),
  2284. cellWithFocusStyle);
  2285. }
  2286. /**
  2287. * Sets style names for given cell when needed.
  2288. */
  2289. public void updateFocusedCellStyle(FlyweightCell cell,
  2290. RowContainer cellContainer) {
  2291. int cellRow = cell.getRow();
  2292. int cellColumn = cell.getColumn();
  2293. int colSpan = cell.getColSpan();
  2294. boolean columnHasFocus = Range.withLength(cellColumn, colSpan)
  2295. .intersects(cellFocusRange);
  2296. if (cellContainer == containerWithFocus) {
  2297. // Cell is in the current container
  2298. if (cellRow == rowWithFocus && columnHasFocus) {
  2299. if (cellWithFocusStyle != cell.getElement()) {
  2300. // Cell is correct but it does not have focused style
  2301. if (cellWithFocusStyle != null) {
  2302. // Remove old focus style
  2303. setStyleName(cellWithFocusStyle, cellFocusStyleName,
  2304. false);
  2305. }
  2306. cellWithFocusStyle = cell.getElement();
  2307. // Add focus style to correct cell.
  2308. setStyleName(cellWithFocusStyle, cellFocusStyleName,
  2309. true);
  2310. }
  2311. } else if (cellWithFocusStyle == cell.getElement()) {
  2312. // Due to escalator reusing cells, a new cell has the same
  2313. // element but is not the focused cell.
  2314. setStyleName(cellWithFocusStyle, cellFocusStyleName, false);
  2315. cellWithFocusStyle = null;
  2316. }
  2317. }
  2318. }
  2319. /**
  2320. * Sets focus style for the given row if needed.
  2321. *
  2322. * @param row
  2323. * a row object
  2324. */
  2325. public void updateFocusedRowStyle(Row row) {
  2326. if (rowWithFocus == row.getRow()
  2327. && containerWithFocus == escalator.getBody()) {
  2328. if (row.getElement() != rowWithFocusStyle) {
  2329. // Row should have focus style but does not have it.
  2330. if (rowWithFocusStyle != null) {
  2331. setStyleName(rowWithFocusStyle, rowFocusStyleName,
  2332. false);
  2333. }
  2334. rowWithFocusStyle = row.getElement();
  2335. setStyleName(rowWithFocusStyle, rowFocusStyleName, true);
  2336. }
  2337. } else if (rowWithFocusStyle == row.getElement()
  2338. || containerWithFocus != escalator.getBody()
  2339. && rowWithFocusStyle != null) {
  2340. // Remove focus style.
  2341. setStyleName(rowWithFocusStyle, rowFocusStyleName, false);
  2342. rowWithFocusStyle = null;
  2343. }
  2344. }
  2345. /**
  2346. * Sets the currently focused.
  2347. * <p>
  2348. * <em>NOTE:</em> the column index is the index in DOM, not the logical
  2349. * column index which includes hidden columns.
  2350. *
  2351. * @param rowIndex
  2352. * the index of the row having focus
  2353. * @param columnIndexDOM
  2354. * the index of the cell having focus
  2355. * @param container
  2356. * the row container having focus
  2357. */
  2358. private void setCellFocus(int rowIndex, int columnIndexDOM,
  2359. RowContainer container) {
  2360. if (container == null || rowIndex == rowWithFocus
  2361. && cellFocusRange.contains(columnIndexDOM)
  2362. && container == this.containerWithFocus) {
  2363. return;
  2364. }
  2365. int oldRow = rowWithFocus;
  2366. rowWithFocus = rowIndex;
  2367. Range oldRange = cellFocusRange;
  2368. if (container == escalator.getBody()) {
  2369. scrollToRow(rowWithFocus);
  2370. cellFocusRange = Range.withLength(columnIndexDOM, 1);
  2371. } else {
  2372. int i = 0;
  2373. Element cell = container.getRowElement(rowWithFocus)
  2374. .getFirstChildElement();
  2375. do {
  2376. int colSpan = cell
  2377. .getPropertyInt(FlyweightCell.COLSPAN_ATTR);
  2378. Range cellRange = Range.withLength(i, colSpan);
  2379. if (cellRange.contains(columnIndexDOM)) {
  2380. cellFocusRange = cellRange;
  2381. break;
  2382. }
  2383. cell = cell.getNextSiblingElement();
  2384. ++i;
  2385. } while (cell != null);
  2386. }
  2387. if (columnIndexDOM >= escalator.getColumnConfiguration()
  2388. .getFrozenColumnCount()) {
  2389. escalator.scrollToColumn(columnIndexDOM, ScrollDestination.ANY,
  2390. 10);
  2391. }
  2392. if (this.containerWithFocus == container) {
  2393. if (oldRange.equals(cellFocusRange) && oldRow != rowWithFocus) {
  2394. refreshRow(oldRow);
  2395. } else {
  2396. refreshHeader();
  2397. refreshFooter();
  2398. }
  2399. } else {
  2400. RowContainer oldContainer = this.containerWithFocus;
  2401. this.containerWithFocus = container;
  2402. if (oldContainer == escalator.getBody()) {
  2403. lastFocusedBodyRow = oldRow;
  2404. } else if (oldContainer == escalator.getHeader()) {
  2405. lastFocusedHeaderRow = oldRow;
  2406. } else {
  2407. lastFocusedFooterRow = oldRow;
  2408. }
  2409. if (!oldRange.equals(cellFocusRange)) {
  2410. refreshHeader();
  2411. refreshFooter();
  2412. if (oldContainer == escalator.getBody()) {
  2413. oldContainer.refreshRows(oldRow, 1);
  2414. }
  2415. } else {
  2416. oldContainer.refreshRows(oldRow, 1);
  2417. }
  2418. }
  2419. refreshRow(rowWithFocus);
  2420. }
  2421. /**
  2422. * Sets focus on a cell.
  2423. *
  2424. * <p>
  2425. * <em>Note</em>: cell focus is not the same as JavaScript's
  2426. * {@code document.activeElement}.
  2427. *
  2428. * @param cell
  2429. * a cell object
  2430. */
  2431. public void setCellFocus(CellReference<T> cell) {
  2432. setCellFocus(cell.getRowIndex(), cell.getColumnIndexDOM(),
  2433. escalator.findRowContainer(cell.getElement()));
  2434. }
  2435. /**
  2436. * Gets list of events that can be used for cell focusing.
  2437. *
  2438. * @return list of navigation related event types
  2439. */
  2440. public Collection<String> getNavigationEvents() {
  2441. return Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.CLICK);
  2442. }
  2443. /**
  2444. * Handle events that can move the cell focus.
  2445. */
  2446. public void handleNavigationEvent(Event event, CellReference<T> cell) {
  2447. if (event.getType().equals(BrowserEvents.CLICK)) {
  2448. setCellFocus(cell);
  2449. // Grid should have focus when clicked.
  2450. getElement().focus();
  2451. } else if (event.getType().equals(BrowserEvents.KEYDOWN)) {
  2452. int newRow = rowWithFocus;
  2453. RowContainer newContainer = containerWithFocus;
  2454. int newColumn = cellFocusRange.getStart();
  2455. switch (event.getKeyCode()) {
  2456. case KeyCodes.KEY_DOWN:
  2457. ++newRow;
  2458. break;
  2459. case KeyCodes.KEY_UP:
  2460. --newRow;
  2461. break;
  2462. case KeyCodes.KEY_RIGHT:
  2463. if (cellFocusRange.getEnd() >= getVisibleColumns().size()) {
  2464. return;
  2465. }
  2466. newColumn = cellFocusRange.getEnd();
  2467. break;
  2468. case KeyCodes.KEY_LEFT:
  2469. if (newColumn == 0) {
  2470. return;
  2471. }
  2472. --newColumn;
  2473. break;
  2474. case KeyCodes.KEY_TAB:
  2475. if (event.getShiftKey()) {
  2476. newContainer = getPreviousContainer(containerWithFocus);
  2477. } else {
  2478. newContainer = getNextContainer(containerWithFocus);
  2479. }
  2480. if (newContainer == containerWithFocus) {
  2481. return;
  2482. }
  2483. break;
  2484. case KeyCodes.KEY_HOME:
  2485. if (newContainer.getRowCount() > 0) {
  2486. newRow = 0;
  2487. }
  2488. break;
  2489. case KeyCodes.KEY_END:
  2490. if (newContainer.getRowCount() > 0) {
  2491. newRow = newContainer.getRowCount() - 1;
  2492. }
  2493. break;
  2494. case KeyCodes.KEY_PAGEDOWN:
  2495. case KeyCodes.KEY_PAGEUP:
  2496. if (newContainer.getRowCount() > 0) {
  2497. boolean down = event
  2498. .getKeyCode() == KeyCodes.KEY_PAGEDOWN;
  2499. // If there is a visible focused cell, scroll by one
  2500. // page from its position. Otherwise, use the first or
  2501. // the last visible row as the scroll start position.
  2502. // This avoids jumping when using both keyboard and the
  2503. // scroll bar for scrolling.
  2504. int firstVisible = getFirstVisibleRowIndex();
  2505. int lastVisible = getLastVisibleRowIndex();
  2506. if (newRow < firstVisible || newRow > lastVisible) {
  2507. newRow = down ? lastVisible : firstVisible;
  2508. }
  2509. // Scroll by a little less than the visible area to
  2510. // account for the possibility that the top and the
  2511. // bottom row are only partially visible.
  2512. int moveFocusBy = Math.max(1,
  2513. lastVisible - firstVisible - 1);
  2514. moveFocusBy *= down ? 1 : -1;
  2515. newRow += moveFocusBy;
  2516. newRow = Math.max(0, Math
  2517. .min(newContainer.getRowCount() - 1, newRow));
  2518. }
  2519. break;
  2520. default:
  2521. return;
  2522. }
  2523. if (newContainer != containerWithFocus) {
  2524. if (newContainer == escalator.getBody()) {
  2525. newRow = lastFocusedBodyRow;
  2526. } else if (newContainer == escalator.getHeader()) {
  2527. newRow = lastFocusedHeaderRow;
  2528. } else {
  2529. newRow = lastFocusedFooterRow;
  2530. }
  2531. } else if (newRow < 0) {
  2532. newContainer = getPreviousContainer(newContainer);
  2533. if (newContainer == containerWithFocus) {
  2534. newRow = 0;
  2535. } else if (newContainer == escalator.getBody()) {
  2536. newRow = getLastVisibleRowIndex();
  2537. } else {
  2538. newRow = newContainer.getRowCount() - 1;
  2539. }
  2540. } else if (newRow >= containerWithFocus.getRowCount()) {
  2541. newContainer = getNextContainer(newContainer);
  2542. if (newContainer == containerWithFocus) {
  2543. newRow = containerWithFocus.getRowCount() - 1;
  2544. } else if (newContainer == escalator.getBody()) {
  2545. newRow = getFirstVisibleRowIndex();
  2546. } else {
  2547. newRow = 0;
  2548. }
  2549. }
  2550. if (newContainer.getRowCount() == 0) {
  2551. /*
  2552. * There are no rows in the container. Can't change the
  2553. * focused cell.
  2554. */
  2555. return;
  2556. }
  2557. event.preventDefault();
  2558. event.stopPropagation();
  2559. setCellFocus(newRow, newColumn, newContainer);
  2560. }
  2561. }
  2562. private RowContainer getPreviousContainer(RowContainer current) {
  2563. if (current == escalator.getFooter()) {
  2564. current = escalator.getBody();
  2565. } else if (current == escalator.getBody()) {
  2566. current = escalator.getHeader();
  2567. } else {
  2568. return current;
  2569. }
  2570. if (current.getRowCount() == 0) {
  2571. return getPreviousContainer(current);
  2572. }
  2573. return current;
  2574. }
  2575. private RowContainer getNextContainer(RowContainer current) {
  2576. if (current == escalator.getHeader()) {
  2577. current = escalator.getBody();
  2578. } else if (current == escalator.getBody()) {
  2579. current = escalator.getFooter();
  2580. } else {
  2581. return current;
  2582. }
  2583. if (current.getRowCount() == 0) {
  2584. return getNextContainer(current);
  2585. }
  2586. return current;
  2587. }
  2588. private void refreshRow(int row) {
  2589. containerWithFocus.refreshRows(row, 1);
  2590. }
  2591. /**
  2592. * Offsets the focused cell's range.
  2593. *
  2594. * @param offset
  2595. * offset for fixing focused cell's range
  2596. */
  2597. public void offsetRangeBy(int offset) {
  2598. cellFocusRange = cellFocusRange.offsetBy(offset);
  2599. }
  2600. /**
  2601. * Informs {@link CellFocusHandler} that certain range of rows has been
  2602. * added to the Grid body. {@link CellFocusHandler} will fix indices
  2603. * accordingly.
  2604. *
  2605. * @param added
  2606. * a range of added rows
  2607. */
  2608. public void rowsAddedToBody(Range added) {
  2609. boolean bodyHasFocus = containerWithFocus == escalator.getBody();
  2610. boolean insertionIsAboveFocusedCell = added
  2611. .getStart() < rowWithFocus;
  2612. if (bodyHasFocus && insertionIsAboveFocusedCell) {
  2613. rowWithFocus += added.length();
  2614. rowWithFocus = Math.min(rowWithFocus,
  2615. escalator.getBody().getRowCount() - 1);
  2616. refreshRow(rowWithFocus);
  2617. }
  2618. }
  2619. /**
  2620. * Informs {@link CellFocusHandler} that certain range of rows has been
  2621. * removed from the Grid body. {@link CellFocusHandler} will fix indices
  2622. * accordingly.
  2623. *
  2624. * @param removed
  2625. * a range of removed rows
  2626. */
  2627. public void rowsRemovedFromBody(Range removed) {
  2628. if (containerWithFocus != escalator.getBody()) {
  2629. return;
  2630. } else if (!removed.contains(rowWithFocus)) {
  2631. if (removed.getStart() > rowWithFocus) {
  2632. return;
  2633. }
  2634. rowWithFocus = rowWithFocus - removed.length();
  2635. } else {
  2636. if (containerWithFocus.getRowCount() > removed.getEnd()) {
  2637. rowWithFocus = removed.getStart();
  2638. } else if (removed.getStart() > 0) {
  2639. rowWithFocus = removed.getStart() - 1;
  2640. } else {
  2641. if (escalator.getHeader().getRowCount() > 0) {
  2642. rowWithFocus = Math.min(lastFocusedHeaderRow,
  2643. escalator.getHeader().getRowCount() - 1);
  2644. containerWithFocus = escalator.getHeader();
  2645. } else if (escalator.getFooter().getRowCount() > 0) {
  2646. rowWithFocus = Math.min(lastFocusedFooterRow,
  2647. escalator.getFooter().getRowCount() - 1);
  2648. containerWithFocus = escalator.getFooter();
  2649. }
  2650. }
  2651. }
  2652. refreshRow(rowWithFocus);
  2653. }
  2654. }
  2655. public final class SelectionColumn extends Column<Boolean, T>
  2656. implements GridEnabledHandler, GridSelectionAllowedHandler {
  2657. private boolean initDone = false;
  2658. private boolean selected = false;
  2659. private CheckBox selectAllCheckBox;
  2660. private boolean selectAllCheckBoxVisible;
  2661. private HeaderCell selectionCell;
  2662. SelectionColumn(final Renderer<Boolean> selectColumnRenderer) {
  2663. super(selectColumnRenderer);
  2664. addEnabledHandler(this);
  2665. addSelectionAllowedHandler(this);
  2666. }
  2667. void initDone() {
  2668. setWidth(-1);
  2669. setEditable(false);
  2670. setResizable(false);
  2671. updateEnable();
  2672. initDone = true;
  2673. }
  2674. @Override
  2675. protected void setDefaultHeaderContent(HeaderCell selectionCell) {
  2676. this.selectionCell = selectionCell;
  2677. if (selectAllCheckBox == null) {
  2678. // there is no checkbox yet -> create it
  2679. selectAllCheckBox = GWT.create(CheckBox.class);
  2680. selectAllCheckBox.setStylePrimaryName(
  2681. getStylePrimaryName() + SELECT_ALL_CHECKBOX_CLASSNAME);
  2682. // label of checkbox should only be visible for assistive
  2683. // devices
  2684. selectAllCheckBox.addStyleName("v-assistive-device-only-label");
  2685. selectAllCheckBox.addValueChangeHandler(event -> {
  2686. selected = event.getValue();
  2687. fireEvent(new SelectAllEvent<>(getSelectionModel(),
  2688. selected));
  2689. });
  2690. selectAllCheckBox.setText("Selects all rows of the table.");
  2691. selectAllCheckBox.setValue(selected);
  2692. addHeaderClickHandler(this::onHeaderClickEvent);
  2693. // Select all with space when "select all" cell is active
  2694. addHeaderKeyUpHandler(this::onHeaderKeyUpEvent);
  2695. } else {
  2696. // checkbox exists, but default header row has changed -> clear
  2697. // rows
  2698. for (HeaderRow row : header.getRows()) {
  2699. if (row.getCell(this)
  2700. .getType() == GridStaticCellType.WIDGET) {
  2701. // Detach from old header.
  2702. row.getCell(this).setText("");
  2703. }
  2704. }
  2705. }
  2706. // attach the checkbox to default row depending on visibility
  2707. doSetSelectAllCheckBoxVisible();
  2708. }
  2709. @Override
  2710. public Column<Boolean, T> setWidth(double pixels) {
  2711. if (pixels != getWidth() && initDone) {
  2712. throw new UnsupportedOperationException("The selection "
  2713. + "column cannot be modified after init");
  2714. } else {
  2715. super.setWidth(pixels);
  2716. }
  2717. return this;
  2718. }
  2719. @Override
  2720. public Boolean getValue(T row) {
  2721. return Boolean.valueOf(isSelected(row));
  2722. }
  2723. @Override
  2724. public Column<Boolean, T> setExpandRatio(int ratio) {
  2725. throw new UnsupportedOperationException(
  2726. "can't change the expand ratio of the selection column");
  2727. }
  2728. @Override
  2729. public int getExpandRatio() {
  2730. return 0;
  2731. }
  2732. @Override
  2733. public Column<Boolean, T> setMaximumWidth(double pixels) {
  2734. throw new UnsupportedOperationException(
  2735. "can't change the maximum width of the selection column");
  2736. }
  2737. @Override
  2738. public double getMaximumWidth() {
  2739. return -1;
  2740. }
  2741. @Override
  2742. public Column<Boolean, T> setMinimumWidth(double pixels) {
  2743. throw new UnsupportedOperationException(
  2744. "can't change the minimum width of the selection column");
  2745. }
  2746. @Override
  2747. public double getMinimumWidth() {
  2748. return -1;
  2749. }
  2750. @Override
  2751. public Column<Boolean, T> setEditable(boolean editable) {
  2752. if (initDone) {
  2753. throw new UnsupportedOperationException(
  2754. "can't set the selection column editable");
  2755. }
  2756. super.setEditable(editable);
  2757. return this;
  2758. }
  2759. @Override
  2760. public void onEnabled(boolean enabled) {
  2761. updateEnable();
  2762. }
  2763. /**
  2764. * Sets the select all checkbox visible in the default header row for
  2765. * selection column.
  2766. *
  2767. * @param selectAllCheckBoxVisible
  2768. * {@code true} for visible, {@code false} for not
  2769. */
  2770. public void setSelectAllCheckBoxVisible(
  2771. boolean selectAllCheckBoxVisible) {
  2772. if (this.selectAllCheckBoxVisible != selectAllCheckBoxVisible) {
  2773. this.selectAllCheckBoxVisible = selectAllCheckBoxVisible;
  2774. doSetSelectAllCheckBoxVisible();
  2775. }
  2776. }
  2777. /**
  2778. * Returns whether the select all checkbox is visible or not.
  2779. *
  2780. * @return {@code true} for visible, {@code false} for not
  2781. */
  2782. public boolean isSelectAllCheckBoxVisible() {
  2783. return selectAllCheckBoxVisible;
  2784. }
  2785. /**
  2786. * Returns the select all checkbox, which is present in the default
  2787. * header if the used selection model is of type
  2788. * {@link SelectionModelWithSelectionColumn}.
  2789. *
  2790. * To handle select all, add {@link SelectAllHandler} the grid with
  2791. * {@link #addSelectAllHandler(SelectAllHandler)}.
  2792. *
  2793. * @return the select all checkbox, or an empty optional if not in use
  2794. */
  2795. public Optional<CheckBox> getSelectAllCheckBox() {
  2796. return Optional.ofNullable(selectionColumn == null ? null
  2797. : selectionColumn.selectAllCheckBox);
  2798. }
  2799. /**
  2800. * Sets the select all checkbox visible or hidden.
  2801. */
  2802. protected void doSetSelectAllCheckBoxVisible() {
  2803. if (selectAllCheckBox == null || selectionCell == null) {
  2804. // There is no default header row to display select all checkbox
  2805. return;
  2806. }
  2807. if (selectAllCheckBoxVisible) {
  2808. selectionCell.setWidget(selectAllCheckBox);
  2809. } else {
  2810. selectionCell.setText("");
  2811. }
  2812. }
  2813. private void updateEnable() {
  2814. if (selectAllCheckBox != null) {
  2815. selectAllCheckBox.setEnabled(isEnabled()
  2816. && getSelectionModel().isSelectionAllowed());
  2817. }
  2818. }
  2819. private void onHeaderClickEvent(GridClickEvent event) {
  2820. if (selectAllCheckBox.isEnabled()) {
  2821. CellReference<?> targetCell = event.getTargetCell();
  2822. int defaultRowIndex = getHeader().getRows()
  2823. .indexOf(getDefaultHeaderRow());
  2824. if (targetCell.getColumnIndex() == 0
  2825. && targetCell.getRowIndex() == defaultRowIndex) {
  2826. selectAllCheckBox.setValue(!selectAllCheckBox.getValue(),
  2827. true);
  2828. }
  2829. }
  2830. }
  2831. private void onHeaderKeyUpEvent(GridKeyUpEvent event) {
  2832. if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE
  2833. || !selectAllCheckBox.isEnabled()) {
  2834. return;
  2835. }
  2836. HeaderRow targetHeaderRow = getHeader()
  2837. .getRow(event.getFocusedCell().getRowIndex());
  2838. if (!targetHeaderRow.isDefault()) {
  2839. return;
  2840. }
  2841. if (event.getFocusedCell().getColumn() == SelectionColumn.this) {
  2842. // Send events to ensure state is updated
  2843. selectAllCheckBox.setValue(!selectAllCheckBox.getValue(), true);
  2844. }
  2845. }
  2846. @Override
  2847. public void onSelectionAllowed(GridSelectionAllowedEvent event) {
  2848. updateEnable();
  2849. }
  2850. }
  2851. /**
  2852. * Helper class for performing sorting through the user interface. Controls
  2853. * the sort() method, reporting USER as the event originator. This is a
  2854. * completely internal class, and is, as such, safe to re-name should a more
  2855. * descriptive name come to mind.
  2856. */
  2857. private final class UserSorter {
  2858. private final Timer timer;
  2859. private boolean scheduledMultisort;
  2860. private Column<?, T> column;
  2861. private UserSorter() {
  2862. timer = new Timer() {
  2863. @Override
  2864. public void run() {
  2865. scheduledMultisort = true;
  2866. }
  2867. };
  2868. }
  2869. /**
  2870. * Toggle sorting for a cell. If the multisort parameter is set to true,
  2871. * the cell's sort order is modified as a natural part of a multi-sort
  2872. * chain. If false, the sorting order is set to ASCENDING for that
  2873. * cell's column. If that column was already the only sorted column in
  2874. * the Grid, the sort direction is flipped.
  2875. *
  2876. * @param cell
  2877. * a valid cell reference
  2878. * @param multisort
  2879. * whether the sort command should act as a multi-sort stack
  2880. * or not
  2881. */
  2882. public void sort(Column<?, ?> column, boolean multisort) {
  2883. if (!columns.contains(column)) {
  2884. throw new IllegalArgumentException(
  2885. "Given column is not a column in this grid. " + column);
  2886. }
  2887. if (!column.isSortable()) {
  2888. return;
  2889. }
  2890. final SortOrder so = getSortOrder(column);
  2891. if (multisort) {
  2892. // If the sort order exists, replace existing value with its
  2893. // opposite
  2894. if (so != null) {
  2895. final int idx = sortOrder.indexOf(so);
  2896. sortOrder.set(idx, so.getOpposite());
  2897. } else {
  2898. // If it doesn't, just add a new sort order to the end of
  2899. // the list
  2900. sortOrder.add(new SortOrder(column));
  2901. }
  2902. } else {
  2903. // Since we're doing single column sorting, first clear the
  2904. // list. Then, if the sort order existed, add its opposite,
  2905. // otherwise just add a new sort value
  2906. int items = sortOrder.size();
  2907. sortOrder.clear();
  2908. if (so != null && items == 1) {
  2909. sortOrder.add(so.getOpposite());
  2910. } else {
  2911. sortOrder.add(new SortOrder(column));
  2912. }
  2913. }
  2914. // sortOrder has been changed; tell the Grid to re-sort itself by
  2915. // user request.
  2916. Grid.this.sort(true);
  2917. }
  2918. /**
  2919. * Invoked on touchstart, marks itself that we will perform sorting on
  2920. * touchend. By default single sort is performed, however on long touch
  2921. * we will perform multitouch.
  2922. *
  2923. * Actual sorting is only performed after {@link #onTouchEnd()} is
  2924. * invoked.
  2925. *
  2926. * @param delay
  2927. * delay, in milliseconds
  2928. */
  2929. public void awaitForTouchEnd(int delay) {
  2930. cancelAwaitForTouchEnd();
  2931. column = eventCell.getColumn();
  2932. scheduledMultisort = false;
  2933. timer.schedule(delay);
  2934. }
  2935. /**
  2936. * Notifies that the finger has been lifted from the tablet/mobile.
  2937. * Depending on how much time has passed, we need to perform singlesort
  2938. * or multisort.
  2939. *
  2940. * Does nothing if the await has been canceled by a call to
  2941. * {@link #cancelAwaitForTouchEnd()}.
  2942. */
  2943. public void onTouchEnd() {
  2944. if (column != null) {
  2945. sort(column, scheduledMultisort);
  2946. cancelAwaitForTouchEnd();
  2947. }
  2948. }
  2949. /**
  2950. * Cancel a scheduled sort.
  2951. */
  2952. public void cancelAwaitForTouchEnd() {
  2953. timer.cancel();
  2954. column = null;
  2955. }
  2956. }
  2957. /**
  2958. * @see Grid#autoColumnWidthsRecalculator
  2959. */
  2960. private class AutoColumnWidthsRecalculator {
  2961. private double lastCalculatedInnerWidth = -1;
  2962. private double lastCalculatedInnerHeight = -1;
  2963. private final ScheduledCommand calculateCommand = new ScheduledCommand() {
  2964. @Override
  2965. public void execute() {
  2966. if (!isScheduled) {
  2967. // something cancelled running this.
  2968. return;
  2969. }
  2970. if (header.markAsDirty || footer.markAsDirty) {
  2971. if (rescheduleCount < 10) {
  2972. /*
  2973. * Headers and footers are rendered as finally, this way
  2974. * we re-schedule this loop as finally, at the end of
  2975. * the queue, so that the headers have a chance to
  2976. * render themselves.
  2977. */
  2978. Scheduler.get().scheduleFinally(this);
  2979. rescheduleCount++;
  2980. } else {
  2981. /*
  2982. * We've tried too many times reschedule finally. Seems
  2983. * like something is being deferred. Let the queue
  2984. * execute and retry again.
  2985. */
  2986. rescheduleCount = 0;
  2987. Scheduler.get().scheduleDeferred(this);
  2988. }
  2989. } else if (currentDataAvailable.isEmpty()
  2990. && (dataSource.isWaitingForData()
  2991. || escalator.getBody().getRowCount() > 0)) {
  2992. Scheduler.get().scheduleDeferred(this);
  2993. } else {
  2994. calculate();
  2995. }
  2996. }
  2997. };
  2998. private int rescheduleCount = 0;
  2999. private boolean isScheduled;
  3000. /**
  3001. * Calculates and applies column widths, taking into account fixed
  3002. * widths and column expand rules.
  3003. *
  3004. * @see Column#setWidth(double)
  3005. * @see Column#setExpandRatio(int)
  3006. * @see Column#setMinimumWidth(double)
  3007. * @see Column#setMaximumWidth(double)
  3008. */
  3009. public void schedule() {
  3010. if (!isScheduled && isAttached() && !(currentDataAvailable.isEmpty()
  3011. && escalator.getBody().getRowCount() > 0)) {
  3012. isScheduled = true;
  3013. Scheduler.get().scheduleFinally(calculateCommand);
  3014. }
  3015. }
  3016. private void calculate() {
  3017. isScheduled = false;
  3018. rescheduleCount = 0;
  3019. assert !(currentDataAvailable.isEmpty() && dataSource
  3020. .isWaitingForData()) : "Trying to calculate column widths without data while data is still being fetched.";
  3021. // Make SelectAllCheckbox visible
  3022. getSelectionColumn().ifPresent(col -> {
  3023. if (getDefaultHeaderRow() == null) {
  3024. return;
  3025. }
  3026. HeaderCell headerCell = getDefaultHeaderRow().getCell(col);
  3027. if (headerCell.getType().equals(GridStaticCellType.WIDGET)) {
  3028. // SelectAllCheckbox is present already
  3029. return;
  3030. }
  3031. headerCell.setWidget(col.selectAllCheckBox);
  3032. refreshHeader(); // Paint.
  3033. });
  3034. if (columnsAreGuaranteedToBeWiderThanGrid()) {
  3035. applyColumnWidths();
  3036. } else {
  3037. applyColumnWidthsWithExpansion();
  3038. }
  3039. // Hide the SelectAllCheckbox if needed
  3040. getSelectionColumn()
  3041. .ifPresent(SelectionColumn::doSetSelectAllCheckBoxVisible);
  3042. // Update latest width to prevent recalculate on height change.
  3043. lastCalculatedInnerWidth = escalator.getInnerWidth();
  3044. lastCalculatedInnerHeight = getEscalatorInnerHeight();
  3045. }
  3046. private boolean columnsAreGuaranteedToBeWiderThanGrid() {
  3047. double freeSpace = escalator.getInnerWidth();
  3048. for (Column<?, ?> column : getVisibleColumns()) {
  3049. if (column.getWidth() >= 0) {
  3050. freeSpace -= column.getWidth();
  3051. } else if (column.getMinimumWidth() >= 0) {
  3052. freeSpace -= column.getMinimumWidth();
  3053. }
  3054. }
  3055. return freeSpace < 0;
  3056. }
  3057. @SuppressWarnings("boxing")
  3058. private void applyColumnWidths() {
  3059. /* Step 1: Apply all column widths as they are. */
  3060. Map<Integer, Double> selfWidths = new LinkedHashMap<>();
  3061. List<Column<?, T>> columns = getVisibleColumns();
  3062. for (int index = 0; index < columns.size(); index++) {
  3063. selfWidths.put(index, columns.get(index).getWidth());
  3064. }
  3065. Grid.this.escalator.getColumnConfiguration()
  3066. .setColumnWidths(selfWidths);
  3067. /*
  3068. * Step 2: Make sure that each column ends up obeying their min/max
  3069. * width constraints if defined as autowidth. If constraints are
  3070. * violated, fix it.
  3071. */
  3072. Map<Integer, Double> constrainedWidths = new LinkedHashMap<>();
  3073. for (int index = 0; index < columns.size(); index++) {
  3074. Column<?, T> column = columns.get(index);
  3075. boolean hasAutoWidth = column.getWidth() < 0;
  3076. if (!hasAutoWidth) {
  3077. continue;
  3078. }
  3079. // TODO: bug: these don't honor the CSS max/min. :(
  3080. double actualWidth = column.getWidthActual();
  3081. if (actualWidth < getMinWidth(column)) {
  3082. constrainedWidths.put(index, column.getMinimumWidth());
  3083. } else if (actualWidth > getMaxWidth(column)) {
  3084. constrainedWidths.put(index, column.getMaximumWidth());
  3085. }
  3086. }
  3087. Grid.this.escalator.getColumnConfiguration()
  3088. .setColumnWidths(constrainedWidths);
  3089. }
  3090. private void applyColumnWidthsWithExpansion() {
  3091. boolean defaultExpandRatios = true;
  3092. int totalRatios = 0;
  3093. double reservedPixels = 0;
  3094. final Set<Column<?, T>> columnsToExpand = new HashSet<>();
  3095. List<Column<?, T>> nonFixedColumns = new ArrayList<>();
  3096. Map<Integer, Double> columnSizes = new HashMap<>();
  3097. final List<Column<?, T>> visibleColumns = getVisibleColumns();
  3098. /*
  3099. * Set all fixed widths and also calculate the size-to-fit widths
  3100. * for the autocalculated columns.
  3101. *
  3102. * This way we know with how many pixels we have left to expand the
  3103. * rest.
  3104. */
  3105. for (Column<?, T> column : visibleColumns) {
  3106. final double widthAsIs = column.getWidth();
  3107. final boolean isFixedWidth = widthAsIs >= 0;
  3108. // Check for max width just to be sure we don't break the limits
  3109. final double widthFixed = Math.max(
  3110. Math.min(getMaxWidth(column), widthAsIs),
  3111. column.getMinimumWidth());
  3112. defaultExpandRatios = defaultExpandRatios
  3113. && (column.getExpandRatio() == -1
  3114. || column == selectionColumn);
  3115. if (isFixedWidth) {
  3116. columnSizes.put(visibleColumns.indexOf(column), widthFixed);
  3117. reservedPixels += widthFixed;
  3118. } else {
  3119. nonFixedColumns.add(column);
  3120. columnSizes.put(visibleColumns.indexOf(column), -1.0d);
  3121. }
  3122. }
  3123. setColumnSizes(columnSizes);
  3124. for (Column<?, T> column : nonFixedColumns) {
  3125. final int expandRatio = defaultExpandRatios ? 1
  3126. : column.getExpandRatio();
  3127. final double maxWidth = getMaxWidth(column);
  3128. double newWidth;
  3129. if (column.isMinimumWidthFromContent()) {
  3130. newWidth = Math.min(maxWidth, column.getWidthActual());
  3131. } else {
  3132. newWidth = 0;
  3133. }
  3134. boolean shouldExpand = newWidth < maxWidth && expandRatio > 0
  3135. && column != selectionColumn;
  3136. if (shouldExpand) {
  3137. totalRatios += expandRatio;
  3138. columnsToExpand.add(column);
  3139. }
  3140. reservedPixels += newWidth;
  3141. columnSizes.put(visibleColumns.indexOf(column), newWidth);
  3142. }
  3143. /*
  3144. * Now that we know how many pixels we need at the very least, we
  3145. * can distribute the remaining pixels to all columns according to
  3146. * their expand ratios.
  3147. */
  3148. double pixelsToDistribute = escalator.getInnerWidth()
  3149. - reservedPixels;
  3150. if (pixelsToDistribute <= 0 || totalRatios <= 0) {
  3151. if (pixelsToDistribute <= 0) {
  3152. // Set column sizes for expanding columns
  3153. setColumnSizes(columnSizes);
  3154. }
  3155. return;
  3156. }
  3157. /*
  3158. * Check for columns that hit their max width. Adjust
  3159. * pixelsToDistribute and totalRatios accordingly. Recheck. Stop
  3160. * when no new columns hit their max width
  3161. */
  3162. boolean aColumnHasMaxedOut;
  3163. do {
  3164. aColumnHasMaxedOut = false;
  3165. final double widthPerRatio = pixelsToDistribute / totalRatios;
  3166. final Iterator<Column<?, T>> i = columnsToExpand.iterator();
  3167. while (i.hasNext()) {
  3168. final Column<?, T> column = i.next();
  3169. final int expandRatio = getExpandRatio(column,
  3170. defaultExpandRatios);
  3171. final int columnIndex = visibleColumns.indexOf(column);
  3172. final double autoWidth = columnSizes.get(columnIndex);
  3173. final double maxWidth = getMaxWidth(column);
  3174. double expandedWidth = autoWidth
  3175. + widthPerRatio * expandRatio;
  3176. if (maxWidth <= expandedWidth) {
  3177. i.remove();
  3178. totalRatios -= expandRatio;
  3179. aColumnHasMaxedOut = true;
  3180. pixelsToDistribute -= maxWidth - autoWidth;
  3181. columnSizes.put(columnIndex, maxWidth);
  3182. }
  3183. }
  3184. } while (aColumnHasMaxedOut);
  3185. if (totalRatios <= 0 && columnsToExpand.isEmpty()) {
  3186. setColumnSizes(columnSizes);
  3187. return;
  3188. }
  3189. assert pixelsToDistribute > 0 : "We've run out of pixels to distribute ("
  3190. + pixelsToDistribute + "px to " + totalRatios
  3191. + " ratios between " + columnsToExpand.size() + " columns)";
  3192. assert totalRatios > 0 && !columnsToExpand
  3193. .isEmpty() : "Bookkeeping out of sync. Ratios: "
  3194. + totalRatios + " Columns: "
  3195. + columnsToExpand.size();
  3196. /*
  3197. * If we still have anything left, distribute the remaining pixels
  3198. * to the remaining columns.
  3199. */
  3200. final double widthPerRatio;
  3201. int leftOver = 0;
  3202. if (BrowserInfo.getBrowserString().contains("PhantomJS")) {
  3203. // These browsers report subpixels as integers. this usually
  3204. // results into issues..
  3205. widthPerRatio = (int) (pixelsToDistribute / totalRatios);
  3206. leftOver = (int) (pixelsToDistribute
  3207. - widthPerRatio * totalRatios);
  3208. } else {
  3209. widthPerRatio = pixelsToDistribute / totalRatios;
  3210. }
  3211. for (Column<?, T> column : columnsToExpand) {
  3212. final int expandRatio = getExpandRatio(column,
  3213. defaultExpandRatios);
  3214. final int columnIndex = visibleColumns.indexOf(column);
  3215. final double autoWidth = columnSizes.get(columnIndex);
  3216. double totalWidth = autoWidth + widthPerRatio * expandRatio;
  3217. if (leftOver > 0) {
  3218. totalWidth += 1;
  3219. leftOver--;
  3220. }
  3221. columnSizes.put(columnIndex, totalWidth);
  3222. totalRatios -= expandRatio;
  3223. }
  3224. assert totalRatios == 0 : "Bookkeeping error: there were still some ratios left undistributed: "
  3225. + totalRatios;
  3226. /*
  3227. * Check the guarantees for minimum width and scoot back the columns
  3228. * that don't care.
  3229. */
  3230. boolean minWidthsCausedReflows;
  3231. do {
  3232. minWidthsCausedReflows = false;
  3233. /*
  3234. * First, let's check which columns were too cramped, and expand
  3235. * them. Also keep track on how many pixels we grew - we need to
  3236. * remove those pixels from other columns
  3237. */
  3238. double pixelsToRemoveFromOtherColumns = 0;
  3239. for (Column<?, T> column : visibleColumns) {
  3240. /*
  3241. * We can't iterate over columnsToExpand, even though that
  3242. * would be convenient. This is because some column without
  3243. * an expand ratio might still have a min width - those
  3244. * wouldn't show up in that set.
  3245. */
  3246. double minWidth = getMinWidth(column);
  3247. final int columnIndex = visibleColumns.indexOf(column);
  3248. double currentWidth = columnSizes.get(columnIndex);
  3249. boolean hasAutoWidth = column.getWidth() < 0;
  3250. if (hasAutoWidth && currentWidth < minWidth) {
  3251. columnSizes.put(columnIndex, minWidth);
  3252. pixelsToRemoveFromOtherColumns += minWidth
  3253. - currentWidth;
  3254. minWidthsCausedReflows = true;
  3255. /*
  3256. * Remove this column form the set if it exists. This
  3257. * way we make sure that it doesn't get shrunk in the
  3258. * next step.
  3259. */
  3260. columnsToExpand.remove(column);
  3261. }
  3262. }
  3263. /*
  3264. * Now we need to shrink the remaining columns according to
  3265. * their ratios. Recalculate the sum of remaining ratios.
  3266. */
  3267. totalRatios = 0;
  3268. for (Column<?, ?> column : columnsToExpand) {
  3269. totalRatios += getExpandRatio(column, defaultExpandRatios);
  3270. }
  3271. final double pixelsToRemovePerRatio = pixelsToRemoveFromOtherColumns
  3272. / totalRatios;
  3273. for (Column<?, T> column : columnsToExpand) {
  3274. final double pixelsToRemove = pixelsToRemovePerRatio
  3275. * getExpandRatio(column, defaultExpandRatios);
  3276. int colIndex = visibleColumns.indexOf(column);
  3277. columnSizes.put(colIndex,
  3278. columnSizes.get(colIndex) - pixelsToRemove);
  3279. }
  3280. } while (minWidthsCausedReflows);
  3281. // Finally set all the column sizes.
  3282. setColumnSizes(columnSizes);
  3283. }
  3284. private void setColumnSizes(Map<Integer, Double> columnSizes) {
  3285. // Set all widths at once
  3286. escalator.getColumnConfiguration().setColumnWidths(columnSizes);
  3287. }
  3288. private int getExpandRatio(Column<?, ?> column,
  3289. boolean defaultExpandRatios) {
  3290. int expandRatio = column.getExpandRatio();
  3291. if (expandRatio > 0) {
  3292. return expandRatio;
  3293. } else if (expandRatio < 0) {
  3294. assert defaultExpandRatios : "No columns should've expanded";
  3295. return 1;
  3296. } else {
  3297. assert false : "this method should've not been called at all if expandRatio is 0";
  3298. return 0;
  3299. }
  3300. }
  3301. /**
  3302. * Returns the maximum width of the column, or {@link Double#MAX_VALUE}
  3303. * if defined as negative.
  3304. */
  3305. private double getMaxWidth(Column<?, ?> column) {
  3306. double maxWidth = column.getMaximumWidth();
  3307. if (maxWidth >= 0) {
  3308. return maxWidth;
  3309. } else {
  3310. return Double.MAX_VALUE;
  3311. }
  3312. }
  3313. /**
  3314. * Returns the minimum width of the column, or {@link Double#MIN_VALUE}
  3315. * if defined as negative.
  3316. */
  3317. private double getMinWidth(Column<?, ?> column) {
  3318. double minWidth = column.getMinimumWidth();
  3319. if (minWidth >= 0) {
  3320. return minWidth;
  3321. } else {
  3322. return Double.MIN_VALUE;
  3323. }
  3324. }
  3325. /**
  3326. * Check whether the auto width calculation is currently scheduled.
  3327. *
  3328. * @return <code>true</code> if auto width calculation is currently
  3329. * scheduled
  3330. */
  3331. public boolean isScheduled() {
  3332. return isScheduled;
  3333. }
  3334. }
  3335. private class GridSpacerUpdater implements SpacerUpdater {
  3336. private static final String STRIPE_CLASSNAME = "stripe";
  3337. private final Map<Element, Widget> elementToWidgetMap = new HashMap<>();
  3338. @Override
  3339. public void init(Spacer spacer) {
  3340. initTheming(spacer);
  3341. int rowIndex = spacer.getRow();
  3342. Widget detailsWidget = null;
  3343. try {
  3344. detailsWidget = detailsGenerator.getDetails(rowIndex);
  3345. } catch (Throwable e) {
  3346. getLogger().log(Level.SEVERE,
  3347. "Exception while generating details for row "
  3348. + rowIndex,
  3349. e);
  3350. }
  3351. final double spacerHeight;
  3352. Element spacerElement = spacer.getElement();
  3353. if (detailsWidget == null) {
  3354. spacerElement.removeAllChildren();
  3355. spacerHeight = DETAILS_ROW_INITIAL_HEIGHT;
  3356. } else {
  3357. Element element = detailsWidget.getElement();
  3358. spacerElement.appendChild(element);
  3359. setParent(detailsWidget, Grid.this);
  3360. Widget previousWidget = elementToWidgetMap.put(element,
  3361. detailsWidget);
  3362. assert previousWidget == null : "Overwrote a pre-existing widget on row "
  3363. + rowIndex + " without proper removal first.";
  3364. /*
  3365. * Once we have the content properly inside the DOM, we should
  3366. * re-measure it to make sure that it's the correct height.
  3367. *
  3368. * This is rather tricky, since the row (tr) will get the
  3369. * height, but the spacer cell (td) has the borders, which
  3370. * should go on top of the previous row and next row.
  3371. */
  3372. final double contentHeight;
  3373. if (detailsGenerator instanceof HeightAwareDetailsGenerator) {
  3374. HeightAwareDetailsGenerator sadg = (HeightAwareDetailsGenerator) detailsGenerator;
  3375. contentHeight = sadg.getDetailsHeight(rowIndex);
  3376. } else {
  3377. contentHeight = WidgetUtil
  3378. .getRequiredHeightBoundingClientRectDouble(element);
  3379. }
  3380. double borderTopAndBottomHeight = WidgetUtil
  3381. .getBorderTopAndBottomThickness(spacerElement);
  3382. double measuredHeight = 0d;
  3383. if (contentHeight > 0) {
  3384. measuredHeight = contentHeight + borderTopAndBottomHeight;
  3385. } else {
  3386. Scheduler.get().scheduleFinally(() -> {
  3387. // make sure the spacer hasn't got removed
  3388. if (spacer.getElement().getParentElement() != null) {
  3389. // re-check the height
  3390. double confirmedContentHeight = WidgetUtil
  3391. .getRequiredHeightBoundingClientRectDouble(
  3392. element);
  3393. if (confirmedContentHeight > 0) {
  3394. double confirmedMeasuredHeight = confirmedContentHeight
  3395. + WidgetUtil
  3396. .getBorderTopAndBottomThickness(
  3397. spacer.getElement());
  3398. escalator.getBody().setSpacer(spacer.getRow(),
  3399. confirmedMeasuredHeight);
  3400. if (getHeightMode() == HeightMode.UNDEFINED) {
  3401. setHeightByRows(getEscalator().getBody()
  3402. .getRowCount());
  3403. }
  3404. }
  3405. }
  3406. });
  3407. }
  3408. assert getElement().isOrHasChild(
  3409. spacerElement) : "The spacer element wasn't in the DOM during measurement, but was assumed to be.";
  3410. spacerHeight = measuredHeight;
  3411. }
  3412. escalator.getBody().setSpacer(rowIndex, spacerHeight);
  3413. if (getHeightMode() == HeightMode.UNDEFINED) {
  3414. setHeightByRows(getEscalator().getBody().getRowCount());
  3415. }
  3416. }
  3417. @Override
  3418. public void destroy(Spacer spacer) {
  3419. Element spacerElement = spacer.getElement();
  3420. assert getElement().isOrHasChild(spacerElement) : "Trying "
  3421. + "to destroy a spacer that is not connected to this "
  3422. + "Grid's DOM. (row: " + spacer.getRow() + ", element: "
  3423. + spacerElement + ")";
  3424. Widget detailsWidget = elementToWidgetMap
  3425. .remove(spacerElement.getFirstChildElement());
  3426. if (detailsWidget != null) {
  3427. /*
  3428. * The widget may be null here if the previous generator
  3429. * returned a null widget.
  3430. */
  3431. assert spacerElement.getFirstChild() != null : "The "
  3432. + "details row to destroy did not contain a widget - "
  3433. + "probably removed by something else without "
  3434. + "permission? (row: " + spacer.getRow() + ", element: "
  3435. + spacerElement + ")";
  3436. setParent(detailsWidget, null);
  3437. spacerElement.removeAllChildren();
  3438. if (getHeightMode() == HeightMode.UNDEFINED) {
  3439. setHeightByRows(getEscalator().getBody().getRowCount());
  3440. }
  3441. }
  3442. }
  3443. private void initTheming(Spacer spacer) {
  3444. Element spacerRoot = spacer.getElement();
  3445. if (spacer.getRow() % 2 == 1) {
  3446. spacerRoot.getParentElement().addClassName(STRIPE_CLASSNAME);
  3447. } else {
  3448. spacerRoot.getParentElement().removeClassName(STRIPE_CLASSNAME);
  3449. }
  3450. }
  3451. }
  3452. /**
  3453. * Sidebar displaying toggles for hidable columns and custom widgets
  3454. * provided by the application.
  3455. * <p>
  3456. * The button for opening the sidebar is automatically visible inside the
  3457. * grid, if it contains any column hiding options or custom widgets. The
  3458. * column hiding toggles and custom widgets become visible once the sidebar
  3459. * has been opened.
  3460. *
  3461. * @since 7.5.0
  3462. */
  3463. private static class Sidebar extends Composite implements HasEnabled {
  3464. private final ClickHandler openCloseButtonHandler = event -> {
  3465. if (!isOpen()) {
  3466. open();
  3467. } else {
  3468. close();
  3469. }
  3470. };
  3471. private final FlowPanel rootContainer;
  3472. private final FlowPanel content;
  3473. private final MenuBar menuBar;
  3474. private final Button openCloseButton;
  3475. private final Grid<?> grid;
  3476. private Overlay overlay;
  3477. private Sidebar(Grid<?> grid) {
  3478. this.grid = grid;
  3479. rootContainer = new FlowPanel();
  3480. initWidget(rootContainer);
  3481. openCloseButton = new Button();
  3482. openCloseButton.addClickHandler(openCloseButtonHandler);
  3483. rootContainer.add(openCloseButton);
  3484. content = new FlowPanel() {
  3485. @Override
  3486. public boolean remove(Widget w) {
  3487. // Check here to catch child.removeFromParent() calls
  3488. boolean removed = super.remove(w);
  3489. if (removed) {
  3490. updateVisibility();
  3491. }
  3492. return removed;
  3493. }
  3494. };
  3495. createOverlay();
  3496. menuBar = new MenuBar(true) {
  3497. @Override
  3498. public MenuItem insertItem(MenuItem item, int beforeIndex)
  3499. throws IndexOutOfBoundsException {
  3500. if (getParent() == null) {
  3501. content.insert(this, 0);
  3502. updateVisibility();
  3503. }
  3504. return super.insertItem(item, beforeIndex);
  3505. }
  3506. @Override
  3507. public void removeItem(MenuItem item) {
  3508. super.removeItem(item);
  3509. if (getItems().isEmpty()) {
  3510. menuBar.removeFromParent();
  3511. }
  3512. }
  3513. @Override
  3514. public void onBrowserEvent(Event event) {
  3515. // selecting a item with enter will lose the focus and
  3516. // selected item, which means that further keyboard
  3517. // selection won't work unless we do this:
  3518. if (event.getTypeInt() == Event.ONKEYDOWN
  3519. && event.getKeyCode() == KeyCodes.KEY_ENTER) {
  3520. final MenuItem item = getSelectedItem();
  3521. super.onBrowserEvent(event);
  3522. Scheduler.get().scheduleDeferred(() -> {
  3523. selectItem(item);
  3524. focus();
  3525. });
  3526. } else {
  3527. super.onBrowserEvent(event);
  3528. }
  3529. }
  3530. };
  3531. KeyDownHandler keyDownHandler = event -> {
  3532. if (event.getNativeKeyCode() == KeyCodes.KEY_ESCAPE) {
  3533. close();
  3534. }
  3535. };
  3536. openCloseButton.addDomHandler(keyDownHandler,
  3537. KeyDownEvent.getType());
  3538. menuBar.addDomHandler(keyDownHandler, KeyDownEvent.getType());
  3539. }
  3540. /**
  3541. * Creates and initializes the overlay.
  3542. */
  3543. private void createOverlay() {
  3544. overlay = GWT.create(Overlay.class);
  3545. overlay.setOwner(grid);
  3546. overlay.setAutoHideEnabled(true);
  3547. overlay.addStyleDependentName("popup");
  3548. overlay.add(content);
  3549. overlay.addAutoHidePartner(rootContainer.getElement());
  3550. overlay.addCloseHandler(new CloseHandler<PopupPanel>() {
  3551. @Override
  3552. public void onClose(CloseEvent<PopupPanel> event) {
  3553. removeStyleName("open");
  3554. addStyleName("closed");
  3555. }
  3556. });
  3557. overlay.setFitInWindow(true);
  3558. }
  3559. /**
  3560. * Opens the sidebar if not yet opened. Opening the sidebar has no
  3561. * effect if it is empty.
  3562. */
  3563. public void open() {
  3564. if (!isOpen() && isInDOM()) {
  3565. addStyleName("open");
  3566. removeStyleName("closed");
  3567. overlay.showRelativeTo(rootContainer);
  3568. }
  3569. }
  3570. /**
  3571. * Closes the sidebar if not yet closed.
  3572. */
  3573. public void close() {
  3574. overlay.hide();
  3575. }
  3576. /**
  3577. * Returns whether the sidebar is open or not.
  3578. *
  3579. * @return <code>true</code> if open, <code>false</code> if not
  3580. */
  3581. public boolean isOpen() {
  3582. return overlay != null && overlay.isShowing();
  3583. }
  3584. @Override
  3585. public void setStylePrimaryName(String styleName) {
  3586. super.setStylePrimaryName(styleName);
  3587. overlay.setStylePrimaryName(styleName);
  3588. content.setStylePrimaryName(styleName + "-content");
  3589. openCloseButton.setStylePrimaryName(styleName + "-button");
  3590. if (isOpen()) {
  3591. addStyleName("open");
  3592. removeStyleName("closed");
  3593. } else {
  3594. removeStyleName("open");
  3595. addStyleName("closed");
  3596. }
  3597. }
  3598. @Override
  3599. public void addStyleName(String style) {
  3600. super.addStyleName(style);
  3601. overlay.addStyleName(style);
  3602. }
  3603. @Override
  3604. public void removeStyleName(String style) {
  3605. super.removeStyleName(style);
  3606. overlay.removeStyleName(style);
  3607. }
  3608. private void setHeightToHeaderCellHeight() {
  3609. RowContainer header = grid.escalator.getHeader();
  3610. if (!WidgetUtil.isDisplayed(header.getElement())
  3611. || header.getRowCount() == 0
  3612. || !header.getRowElement(0).hasChildNodes()) {
  3613. getLogger().info(
  3614. "No header cell available when calculating sidebar button height");
  3615. // If the Grid is hidden with styles when this is called the
  3616. // border height will be off, but it's usually only a matter of
  3617. // a pixel or so. Removing a style name cannot trigger a full
  3618. // refresh of the layout, it's developer's responsibility to do
  3619. // that where needed.
  3620. double height = header.getDefaultRowHeight()
  3621. - WidgetUtil.measureVerticalBorder(getElement()) / 2;
  3622. openCloseButton.setHeight(height + "px");
  3623. return;
  3624. }
  3625. // Use actual height instead of expected height in case the height
  3626. // is modified by styles.
  3627. Element firstHeaderCell = header.getRowElement(0)
  3628. .getFirstChildElement();
  3629. double height = WidgetUtil
  3630. .getRequiredHeightBoundingClientRectDouble(firstHeaderCell)
  3631. - WidgetUtil.measureVerticalBorder(getElement()) / 2;
  3632. openCloseButton.setHeight(height + "px");
  3633. }
  3634. private void updateVisibility() {
  3635. final boolean hasWidgets = content.getWidgetCount() > 0;
  3636. final boolean isVisible = isInDOM();
  3637. if (isVisible && !hasWidgets) {
  3638. Grid.setParent(this, null);
  3639. getElement().removeFromParent();
  3640. } else if (!isVisible && hasWidgets) {
  3641. close();
  3642. grid.getElement().appendChild(getElement());
  3643. Grid.setParent(this, grid);
  3644. }
  3645. }
  3646. private boolean isInDOM() {
  3647. return getParent() != null;
  3648. }
  3649. @Override
  3650. protected void onAttach() {
  3651. super.onAttach();
  3652. // Make sure the button will get correct height whenever the Sidebar
  3653. // is added to the Grid.
  3654. setHeightToHeaderCellHeight();
  3655. }
  3656. @Override
  3657. public boolean isEnabled() {
  3658. return openCloseButton.isEnabled();
  3659. }
  3660. @Override
  3661. public void setEnabled(boolean enabled) {
  3662. if (!enabled && isOpen()) {
  3663. close();
  3664. }
  3665. openCloseButton.setEnabled(enabled);
  3666. }
  3667. }
  3668. /**
  3669. * UI and functionality related to hiding columns with toggles in the
  3670. * sidebar.
  3671. */
  3672. private final class ColumnHider {
  3673. /** Map from columns to their hiding toggles, component might change */
  3674. private Map<Column<?, T>, MenuItem> columnToHidingToggleMap = new HashMap<>();
  3675. /**
  3676. * When column is being hidden with a toggle, do not refresh toggles for
  3677. * no reason. Also helps for keeping the keyboard navigation working.
  3678. */
  3679. private boolean hidingColumn;
  3680. /**
  3681. * When several columns are set hidable, don't reset the Sidebar for
  3682. * every column separately.
  3683. */
  3684. private boolean toggleUpdateTriggered;
  3685. private void updateColumnHidable(final Column<?, T> column) {
  3686. if (column.isHidable()) {
  3687. MenuItem toggle = columnToHidingToggleMap.get(column);
  3688. if (toggle == null) {
  3689. toggle = createToggle(column);
  3690. }
  3691. toggle.setStyleName("hidden", column.isHidden());
  3692. } else if (columnToHidingToggleMap.containsKey(column)) {
  3693. sidebar.menuBar
  3694. .removeItem(columnToHidingToggleMap.remove(column));
  3695. }
  3696. updateTogglesOrder();
  3697. }
  3698. private MenuItem createToggle(final Column<?, T> column) {
  3699. MenuItem toggle = new MenuItem(createHTML(column), true, () -> {
  3700. hidingColumn = true;
  3701. column.setHidden(!column.isHidden(), true);
  3702. hidingColumn = false;
  3703. });
  3704. toggle.addStyleName("column-hiding-toggle");
  3705. columnToHidingToggleMap.put(column, toggle);
  3706. return toggle;
  3707. }
  3708. private String createHTML(Column<?, T> column) {
  3709. final StringBuilder buf = new StringBuilder();
  3710. buf.append("<span class=\"");
  3711. if (column.isHidden()) {
  3712. buf.append("v-off");
  3713. } else {
  3714. buf.append("v-on");
  3715. }
  3716. buf.append("\"><div>");
  3717. String caption = column.getHidingToggleCaption();
  3718. if (caption == null) {
  3719. caption = column.headerCaption;
  3720. }
  3721. buf.append(caption);
  3722. buf.append("</div></span>");
  3723. return buf.toString();
  3724. }
  3725. private void updateTogglesOrder() {
  3726. if (!hidingColumn && !toggleUpdateTriggered) {
  3727. // This method is called whenever a column is set hidable. If
  3728. // there are multiple hidable columns, it will get called
  3729. // separately for all of them. There is no need to update the
  3730. // order more than once and no other layouting is dependent on
  3731. // the Sidebar layouting getting finished first, so wait until
  3732. // all calls have arrived before proceeding further.
  3733. toggleUpdateTriggered = true;
  3734. Scheduler.get().scheduleFinally(() -> {
  3735. int lastIndex = 0;
  3736. for (Column<?, T> column : getColumns()) {
  3737. if (column.isHidable()) {
  3738. final MenuItem menuItem = columnToHidingToggleMap
  3739. .get(column);
  3740. sidebar.menuBar.removeItem(menuItem);
  3741. sidebar.menuBar.insertItem(menuItem, lastIndex++);
  3742. }
  3743. }
  3744. toggleUpdateTriggered = false;
  3745. });
  3746. }
  3747. }
  3748. private void updateHidingToggle(Column<?, T> column) {
  3749. if (column.isHidable()) {
  3750. MenuItem toggle = columnToHidingToggleMap.get(column);
  3751. toggle.setHTML(createHTML(column));
  3752. toggle.setStyleName("hidden", column.isHidden());
  3753. } // else we can just ignore
  3754. }
  3755. private void removeColumnHidingToggle(Column<?, T> column) {
  3756. sidebar.menuBar.removeItem(columnToHidingToggleMap.get(column));
  3757. }
  3758. }
  3759. /**
  3760. * Escalator used internally by grid to render the rows
  3761. */
  3762. private Escalator escalator = GWT.create(Escalator.class);
  3763. private final Header header = GWT.create(Header.class);
  3764. private final Footer footer = GWT.create(Footer.class);
  3765. private final Sidebar sidebar = new Sidebar(this);
  3766. /**
  3767. * List of columns in the grid. Order defines the visible order.
  3768. */
  3769. private List<Column<?, T>> columns = new ArrayList<>();
  3770. /**
  3771. * The datasource currently in use. <em>Note:</em> it is <code>null</code>
  3772. * on initialization, but not after that.
  3773. */
  3774. private DataSource<T> dataSource;
  3775. private Registration changeHandler;
  3776. private boolean recalculateColumnWidthsNeeded = false;
  3777. /**
  3778. * Currently available row range in DataSource.
  3779. */
  3780. private Range currentDataAvailable = Range.withLength(0, 0);
  3781. /**
  3782. * The number of frozen columns, 0 freezes the selection column if
  3783. * displayed, -1 also prevents selection col from freezing.
  3784. */
  3785. private int frozenColumnCount = 0;
  3786. /**
  3787. * Current sort order. The (private) sort() method reads this list to
  3788. * determine the order in which to present rows.
  3789. */
  3790. private List<SortOrder> sortOrder = new ArrayList<>();
  3791. private Renderer<Boolean> selectColumnRenderer = null;
  3792. private SelectionColumn selectionColumn;
  3793. private String rowStripeStyleName;
  3794. private String rowHasDataStyleName;
  3795. private String rowSelectedStyleName;
  3796. private String cellFocusStyleName;
  3797. private String rowFocusStyleName;
  3798. /**
  3799. * Current selection model.
  3800. */
  3801. private SelectionModel<T> selectionModel;
  3802. protected final CellFocusHandler cellFocusHandler;
  3803. private final UserSorter sorter = new UserSorter();
  3804. private final Editor<T> editor = GWT.create(Editor.class);
  3805. /**
  3806. * The cell a click event originated from
  3807. * <p>
  3808. * This is a workaround to make Chrome work like Firefox. In Chrome,
  3809. * normally if you start a drag on one cell and release on:
  3810. * <ul>
  3811. * <li>that same cell, the click event is that <code>&lt;td></code>.
  3812. * <li>a cell on that same row, the click event is the parent
  3813. * <code>&lt;tr></code>.
  3814. * <li>a cell on another row, the click event is the table section ancestor
  3815. * ({@code <thead>}, {@code <tbody>} or {@code <tfoot>}).
  3816. * </ul>
  3817. *
  3818. * @see #onBrowserEvent(Event)
  3819. */
  3820. private Cell cellOnPrevMouseDown;
  3821. /**
  3822. * A scheduled command to re-evaluate the widths of <em>all columns</em>
  3823. * that have calculated widths. Most probably called because
  3824. * minwidth/maxwidth/expandratio has changed.
  3825. */
  3826. private final AutoColumnWidthsRecalculator autoColumnWidthsRecalculator = new AutoColumnWidthsRecalculator();
  3827. private boolean enabled = true;
  3828. private DetailsGenerator detailsGenerator = DetailsGenerator.NULL;
  3829. private GridSpacerUpdater gridSpacerUpdater = new GridSpacerUpdater();
  3830. /** A set keeping track of the indices of all currently open details */
  3831. private Set<Integer> visibleDetails = new HashSet<>();
  3832. /** A set of indices of details to reopen after detach and on attach */
  3833. private final Set<Integer> reattachVisibleDetails = new HashSet<>();
  3834. private boolean columnReorderingAllowed;
  3835. private ColumnHider columnHider = new ColumnHider();
  3836. private DragAndDropHandler dndHandler = new DragAndDropHandler();
  3837. private AutoScroller autoScroller = new AutoScroller(this);
  3838. private ColumnResizeMode columnResizeMode = ColumnResizeMode.ANIMATED;
  3839. private final List<GridEventHandler<T>> browserEventHandlers = new ArrayList<>();
  3840. private CellStyleGenerator<T> cellStyleGenerator;
  3841. private RowStyleGenerator<T> rowStyleGenerator;
  3842. private RowReference<T> rowReference = new RowReference<>(this);
  3843. private CellReference<T> cellReference = new CellReference<>(rowReference);
  3844. private RendererCellReference rendererCellReference = new RendererCellReference(
  3845. (RowReference<Object>) rowReference);
  3846. private boolean refreshBodyRequested = false;
  3847. private DragAndDropHandler.DragAndDropCallback headerCellDndCallback = new DragAndDropCallback() {
  3848. private final AutoScrollerCallback autoScrollerCallback = new AutoScrollerCallback() {
  3849. @Override
  3850. public void onAutoScroll(int scrollDiff) {
  3851. autoScrollX = scrollDiff;
  3852. onDragUpdate(null);
  3853. }
  3854. @Override
  3855. public void onAutoScrollReachedMin() {
  3856. // make sure the drop marker is visible on the left
  3857. autoScrollX = 0;
  3858. updateDragDropMarker(clientX);
  3859. }
  3860. @Override
  3861. public void onAutoScrollReachedMax() {
  3862. // make sure the drop marker is visible on the right
  3863. autoScrollX = 0;
  3864. updateDragDropMarker(clientX);
  3865. }
  3866. };
  3867. /**
  3868. * Elements for displaying the dragged column(s) and drop marker
  3869. * properly
  3870. */
  3871. private Element table;
  3872. private Element tableHeader;
  3873. /** Marks the column drop location */
  3874. private Element dropMarker;
  3875. /** A copy of the dragged column(s), moves with cursor. */
  3876. private Element dragElement;
  3877. /** Tracks index of the column whose left side the drop would occur */
  3878. private int latestColumnDropIndex;
  3879. /**
  3880. * Map of possible drop positions for the column and the corresponding
  3881. * column index.
  3882. */
  3883. private final TreeMap<Double, Integer> possibleDropPositions = new TreeMap<>();
  3884. /**
  3885. * Makes sure that drag cancel doesn't cause anything unwanted like sort
  3886. */
  3887. private HandlerRegistration columnSortPreventRegistration;
  3888. private int clientX;
  3889. /** How much the grid is being auto scrolled while dragging. */
  3890. private int autoScrollX;
  3891. /** Captures the value of the focused column before reordering */
  3892. private int focusedColumnIndex;
  3893. /** Offset caused by the drag and drop marker width */
  3894. private double dropMarkerWidthOffset;
  3895. private void initHeaderDragElementDOM() {
  3896. if (table == null) {
  3897. tableHeader = DOM.createTHead();
  3898. dropMarker = DOM.createDiv();
  3899. tableHeader.appendChild(dropMarker);
  3900. table = DOM.createTable();
  3901. table.appendChild(tableHeader);
  3902. table.setClassName("header-drag-table");
  3903. }
  3904. // update the style names on each run in case primary name has been
  3905. // modified
  3906. tableHeader.setClassName(
  3907. escalator.getHeader().getElement().getClassName());
  3908. dropMarker.setClassName(getStylePrimaryName() + "-drop-marker");
  3909. int topOffset = 0;
  3910. for (int i = 0; i < eventCell.getRowIndex(); i++) {
  3911. topOffset += escalator.getHeader().getRowElement(i)
  3912. .getFirstChildElement().getOffsetHeight();
  3913. }
  3914. tableHeader.getStyle().setTop(topOffset, Unit.PX);
  3915. getElement().appendChild(table);
  3916. dropMarkerWidthOffset = WidgetUtil
  3917. .getRequiredWidthBoundingClientRectDouble(dropMarker) / 2;
  3918. }
  3919. @Override
  3920. public void onDragUpdate(Event e) {
  3921. if (e != null) {
  3922. clientX = WidgetUtil.getTouchOrMouseClientX(e);
  3923. autoScrollX = 0;
  3924. }
  3925. resolveDragElementHorizontalPosition(clientX);
  3926. updateDragDropMarker(clientX);
  3927. }
  3928. private void updateDragDropMarker(final int clientX) {
  3929. final double scrollLeft = getScrollLeft();
  3930. final double cursorXCoordinate = clientX
  3931. - escalator.getHeader().getElement().getAbsoluteLeft();
  3932. final Entry<Double, Integer> cellEdgeOnRight = possibleDropPositions
  3933. .ceilingEntry(cursorXCoordinate);
  3934. final Entry<Double, Integer> cellEdgeOnLeft = possibleDropPositions
  3935. .floorEntry(cursorXCoordinate);
  3936. final double diffToRightEdge = cellEdgeOnRight == null
  3937. ? Double.MAX_VALUE
  3938. : cellEdgeOnRight.getKey() - cursorXCoordinate;
  3939. final double diffToLeftEdge = cellEdgeOnLeft == null
  3940. ? Double.MAX_VALUE
  3941. : cursorXCoordinate - cellEdgeOnLeft.getKey();
  3942. double dropMarkerLeft = 0 - scrollLeft;
  3943. if (diffToRightEdge > diffToLeftEdge) {
  3944. latestColumnDropIndex = cellEdgeOnLeft.getValue();
  3945. dropMarkerLeft += cellEdgeOnLeft.getKey();
  3946. } else {
  3947. latestColumnDropIndex = cellEdgeOnRight.getValue();
  3948. dropMarkerLeft += cellEdgeOnRight.getKey();
  3949. }
  3950. dropMarkerLeft += autoScrollX;
  3951. final double frozenColumnsWidth = autoScroller
  3952. .getFrozenColumnsWidth();
  3953. final double rightBoundaryForDrag = getSidebarBoundaryComparedTo(
  3954. dropMarkerLeft);
  3955. final int visibleColumns = getVisibleColumns().size();
  3956. // First check if the drop marker should move left because of the
  3957. // sidebar opening button. this only the case if the grid is
  3958. // scrolled to the right
  3959. if (latestColumnDropIndex == visibleColumns
  3960. && rightBoundaryForDrag < dropMarkerLeft
  3961. && dropMarkerLeft <= escalator.getInnerWidth()) {
  3962. dropMarkerLeft = rightBoundaryForDrag - dropMarkerWidthOffset;
  3963. } else if (
  3964. // Check if the drop marker shouldn't be shown at all
  3965. dropMarkerLeft < frozenColumnsWidth || dropMarkerLeft > Math
  3966. .min(rightBoundaryForDrag, escalator.getInnerWidth())
  3967. || dropMarkerLeft < 0) {
  3968. dropMarkerLeft = -10000000;
  3969. }
  3970. dropMarker.getStyle().setLeft(dropMarkerLeft, Unit.PX);
  3971. }
  3972. private void resolveDragElementHorizontalPosition(final int clientX) {
  3973. double left = clientX - table.getAbsoluteLeft();
  3974. // Do not show the drag element beyond a spanned header cell
  3975. // limitation
  3976. final Double leftBound = possibleDropPositions.firstKey();
  3977. final Double rightBound = possibleDropPositions.lastKey();
  3978. final double scrollLeft = getScrollLeft();
  3979. if (left + scrollLeft < leftBound) {
  3980. left = leftBound - scrollLeft + autoScrollX;
  3981. } else if (left + scrollLeft > rightBound) {
  3982. left = rightBound - scrollLeft + autoScrollX;
  3983. }
  3984. // Do not show the drag element beyond the grid
  3985. final double sidebarBoundary = getSidebarBoundaryComparedTo(left);
  3986. final double gridBoundary = escalator.getInnerWidth();
  3987. final double rightBoundary = Math.min(sidebarBoundary,
  3988. gridBoundary);
  3989. // Do not show on left of the frozen columns (even if scrolled)
  3990. final int frozenColumnsWidth = (int) autoScroller
  3991. .getFrozenColumnsWidth();
  3992. left = Math.max(frozenColumnsWidth, Math.min(left, rightBoundary));
  3993. left -= dragElement.getClientWidth() / 2;
  3994. dragElement.getStyle().setLeft(left, Unit.PX);
  3995. }
  3996. private boolean isSidebarOnDraggedRow() {
  3997. return eventCell.getRowIndex() == 0 && sidebar.isInDOM()
  3998. && !sidebar.isOpen();
  3999. }
  4000. /**
  4001. * Returns the sidebar left coordinate, in relation to the grid. Or
  4002. * Double.MAX_VALUE if it doesn't cause a boundary.
  4003. */
  4004. private double getSidebarBoundaryComparedTo(double left) {
  4005. if (isSidebarOnDraggedRow()) {
  4006. double absoluteLeft = left + getElement().getAbsoluteLeft();
  4007. double sidebarLeft = sidebar.getElement().getAbsoluteLeft();
  4008. double diff = absoluteLeft - sidebarLeft;
  4009. if (diff > 0) {
  4010. return left - diff;
  4011. }
  4012. }
  4013. return Double.MAX_VALUE;
  4014. }
  4015. @Override
  4016. public boolean onDragStart(Event e) {
  4017. calculatePossibleDropPositions();
  4018. if (possibleDropPositions.isEmpty()) {
  4019. return false;
  4020. }
  4021. initHeaderDragElementDOM();
  4022. // needs to clone focus and sorting indicators too (UX)
  4023. dragElement = DOM.clone(eventCell.getElement(), true);
  4024. dragElement.getStyle().clearWidth();
  4025. dropMarker.getStyle().setProperty("height",
  4026. dragElement.getStyle().getHeight());
  4027. tableHeader.appendChild(dragElement);
  4028. // mark the column being dragged for styling
  4029. eventCell.getElement().addClassName("dragged");
  4030. // mark the floating cell, for styling & testing
  4031. dragElement.addClassName("dragged-column-header");
  4032. // start the auto scroll handler
  4033. autoScroller.setScrollArea(60);
  4034. autoScroller.start(e, ScrollAxis.HORIZONTAL, autoScrollerCallback);
  4035. return true;
  4036. }
  4037. @Override
  4038. public void onDragEnd() {
  4039. table.removeFromParent();
  4040. dragElement.removeFromParent();
  4041. eventCell.getElement().removeClassName("dragged");
  4042. }
  4043. @Override
  4044. public void onDrop() {
  4045. final int draggedColumnIndex = eventCell.getColumnIndex();
  4046. final int colspan = header.getRow(eventCell.getRowIndex())
  4047. .getCell(eventCell.getColumn()).getColspan();
  4048. if (latestColumnDropIndex != draggedColumnIndex
  4049. && latestColumnDropIndex != draggedColumnIndex + colspan) {
  4050. List<Column<?, T>> columns = getColumns();
  4051. List<Column<?, T>> reordered = new ArrayList<>();
  4052. if (draggedColumnIndex < latestColumnDropIndex) {
  4053. reordered.addAll(columns.subList(0, draggedColumnIndex));
  4054. reordered.addAll(
  4055. columns.subList(draggedColumnIndex + colspan,
  4056. latestColumnDropIndex));
  4057. reordered.addAll(columns.subList(draggedColumnIndex,
  4058. draggedColumnIndex + colspan));
  4059. reordered.addAll(columns.subList(latestColumnDropIndex,
  4060. columns.size()));
  4061. } else {
  4062. reordered.addAll(columns.subList(0, latestColumnDropIndex));
  4063. reordered.addAll(columns.subList(draggedColumnIndex,
  4064. draggedColumnIndex + colspan));
  4065. reordered.addAll(columns.subList(latestColumnDropIndex,
  4066. draggedColumnIndex));
  4067. reordered.addAll(columns.subList(
  4068. draggedColumnIndex + colspan, columns.size()));
  4069. }
  4070. // since setColumnOrder will add it anyway!
  4071. reordered.remove(selectionColumn);
  4072. // capture focused cell column before reorder
  4073. Cell focusedCell = cellFocusHandler.getFocusedCell();
  4074. if (focusedCell != null) {
  4075. // take hidden columns into account
  4076. focusedColumnIndex = getColumns()
  4077. .indexOf(getVisibleColumn(focusedCell.getColumn()));
  4078. }
  4079. Column<?, T>[] array = reordered
  4080. .toArray(new Column[reordered.size()]);
  4081. setColumnOrder(true, array);
  4082. transferCellFocusOnDrop();
  4083. } // else
  4084. // no
  4085. // reordering
  4086. }
  4087. private void transferCellFocusOnDrop() {
  4088. final Cell focusedCell = cellFocusHandler.getFocusedCell();
  4089. if (focusedCell != null && focusedCell.getElement() != null) {
  4090. final int focusedColumnIndexDOM = focusedCell.getColumn();
  4091. final int focusedRowIndex = focusedCell.getRow();
  4092. final int draggedColumnIndex = eventCell.getColumnIndex();
  4093. // transfer focus if it was effected by the new column order
  4094. final RowContainer rowContainer = escalator
  4095. .findRowContainer(focusedCell.getElement());
  4096. if (focusedColumnIndex == draggedColumnIndex) {
  4097. // move with the dragged column
  4098. int adjustedDropIndex = latestColumnDropIndex > draggedColumnIndex
  4099. ? latestColumnDropIndex - 1
  4100. : latestColumnDropIndex;
  4101. // remove hidden columns from indexing
  4102. adjustedDropIndex = getVisibleColumns()
  4103. .indexOf(getColumn(adjustedDropIndex));
  4104. cellFocusHandler.setCellFocus(focusedRowIndex,
  4105. adjustedDropIndex, rowContainer);
  4106. } else if (latestColumnDropIndex <= focusedColumnIndex
  4107. && draggedColumnIndex > focusedColumnIndex) {
  4108. cellFocusHandler.setCellFocus(focusedRowIndex,
  4109. focusedColumnIndexDOM + 1, rowContainer);
  4110. } else if (latestColumnDropIndex > focusedColumnIndex
  4111. && draggedColumnIndex < focusedColumnIndex) {
  4112. cellFocusHandler.setCellFocus(focusedRowIndex,
  4113. focusedColumnIndexDOM - 1, rowContainer);
  4114. }
  4115. }
  4116. }
  4117. @Override
  4118. public void onDragCancel() {
  4119. // cancel next click so that we may prevent column sorting if
  4120. // mouse was released on top of the dragged cell
  4121. if (columnSortPreventRegistration == null) {
  4122. columnSortPreventRegistration = Event
  4123. .addNativePreviewHandler(event -> {
  4124. if (event.getTypeInt() == Event.ONCLICK) {
  4125. event.cancel();
  4126. event.getNativeEvent().preventDefault();
  4127. columnSortPreventRegistration.removeHandler();
  4128. columnSortPreventRegistration = null;
  4129. }
  4130. });
  4131. }
  4132. autoScroller.stop();
  4133. }
  4134. /**
  4135. * Returns the amount of frozen columns. The selection column is always
  4136. * considered frozen, since it can't be moved.
  4137. */
  4138. private int getSelectionAndFrozenColumnCount() {
  4139. // no matter if selection column is frozen or not, it is considered
  4140. // frozen for column dnd reorder
  4141. if (getSelectionModel() instanceof SelectionModelWithSelectionColumn) {
  4142. return Math.max(0, getFrozenColumnCount()) + 1;
  4143. } else {
  4144. return Math.max(0, getFrozenColumnCount());
  4145. }
  4146. }
  4147. @SuppressWarnings("boxing")
  4148. private void calculatePossibleDropPositions() {
  4149. possibleDropPositions.clear();
  4150. final int draggedColumnIndex = eventCell.getColumnIndex();
  4151. final StaticRow<?> draggedCellRow = header
  4152. .getRow(eventCell.getRowIndex());
  4153. final int draggedColumnRightIndex = draggedColumnIndex
  4154. + draggedCellRow.getCell(eventCell.getColumn())
  4155. .getColspan();
  4156. final int frozenColumns = getSelectionAndFrozenColumnCount();
  4157. final Range draggedCellRange = Range.between(draggedColumnIndex,
  4158. draggedColumnRightIndex);
  4159. /*
  4160. * If the dragged cell intersects with a spanned cell in any other
  4161. * header or footer row, then the drag is limited inside that
  4162. * spanned cell. The same rules apply: the cell can't be dropped
  4163. * inside another spanned cell. The left and right bounds keep track
  4164. * of the edges of the most limiting spanned cell.
  4165. */
  4166. int leftBound = -1;
  4167. int rightBound = getColumnCount() + 1;
  4168. final HashSet<Integer> unavailableColumnDropIndices = new HashSet<>();
  4169. final List<StaticRow<?>> rows = new ArrayList<>();
  4170. rows.addAll(header.getRows());
  4171. rows.addAll(footer.getRows());
  4172. for (StaticRow<?> row : rows) {
  4173. if (!row.hasSpannedCells()) {
  4174. continue;
  4175. }
  4176. final boolean isDraggedCellRow = row.equals(draggedCellRow);
  4177. for (int cellColumnIndex = frozenColumns; cellColumnIndex < getColumnCount(); cellColumnIndex++) {
  4178. StaticCell cell = row.getCell(getColumn(cellColumnIndex));
  4179. int colspan = cell.getColspan();
  4180. if (colspan <= 1) {
  4181. continue;
  4182. }
  4183. final int cellColumnRightIndex = cellColumnIndex + row
  4184. .getSizeOfCellGroup(getColumn(cellColumnIndex));
  4185. final Range cellRange = Range.between(cellColumnIndex,
  4186. cellColumnRightIndex);
  4187. final boolean intersects = draggedCellRange
  4188. .intersects(cellRange);
  4189. if (intersects && !isDraggedCellRow) {
  4190. // if the currently iterated cell is inside or same as
  4191. // the dragged cell, then it doesn't restrict the drag
  4192. if (cellRange.isSubsetOf(draggedCellRange)) {
  4193. cellColumnIndex = cellColumnRightIndex - 1;
  4194. continue;
  4195. }
  4196. /*
  4197. * if the dragged cell is a spanned cell and it crosses
  4198. * with the currently iterated cell without sharing
  4199. * either start or end then not possible to drag the
  4200. * cell.
  4201. */
  4202. if (!draggedCellRange.isSubsetOf(cellRange)) {
  4203. return;
  4204. }
  4205. // the spanned cell overlaps the dragged cell (but is
  4206. // not the dragged cell)
  4207. if (cellColumnIndex <= draggedColumnIndex
  4208. && cellColumnIndex > leftBound) {
  4209. leftBound = cellColumnIndex;
  4210. }
  4211. if (cellColumnRightIndex < rightBound) {
  4212. rightBound = cellColumnRightIndex;
  4213. }
  4214. cellColumnIndex = cellColumnRightIndex - 1;
  4215. } else {
  4216. // can't drop inside a spanned cell, or this is the
  4217. // dragged cell
  4218. while (colspan > 1) {
  4219. cellColumnIndex++;
  4220. colspan--;
  4221. unavailableColumnDropIndices.add(cellColumnIndex);
  4222. }
  4223. }
  4224. }
  4225. }
  4226. if (leftBound == rightBound - 1) {
  4227. return;
  4228. }
  4229. double position = autoScroller.getFrozenColumnsWidth();
  4230. // iterate column indices and add possible drop positions
  4231. for (int i = frozenColumns; i < getColumnCount(); i++) {
  4232. Column<?, T> column = getColumn(i);
  4233. if (!unavailableColumnDropIndices.contains(i)
  4234. && !column.isHidden()) {
  4235. if (leftBound != -1) {
  4236. if (i >= leftBound && i <= rightBound) {
  4237. possibleDropPositions.put(position, i);
  4238. }
  4239. } else {
  4240. possibleDropPositions.put(position, i);
  4241. }
  4242. }
  4243. position += column.getWidthActual();
  4244. }
  4245. if (leftBound == -1) {
  4246. // add the right side of the last column as columns.size()
  4247. possibleDropPositions.put(position, getColumnCount());
  4248. }
  4249. }
  4250. };
  4251. /**
  4252. * Base class for grid columns internally used by the Grid. The user should
  4253. * use {@link Column} when creating new columns.
  4254. *
  4255. * @param <C>
  4256. * the column type
  4257. *
  4258. * @param <T>
  4259. * the row type
  4260. */
  4261. public abstract static class Column<C, T> {
  4262. /**
  4263. * Default renderer for GridColumns. Renders everything into text
  4264. * through {@link Object#toString()}.
  4265. */
  4266. private final class DefaultTextRenderer implements Renderer<Object> {
  4267. boolean warned = false;
  4268. private static final String DEFAULT_RENDERER_WARNING = "This column uses a dummy default TextRenderer. "
  4269. + "A more suitable renderer should be set using the setRenderer() method.";
  4270. @Override
  4271. public void render(RendererCellReference cell, Object data) {
  4272. if (!warned && !(data instanceof String)) {
  4273. getLogger().warning(
  4274. Column.this + ": " + DEFAULT_RENDERER_WARNING);
  4275. warned = true;
  4276. }
  4277. final String text;
  4278. if (data == null) {
  4279. text = "";
  4280. } else {
  4281. text = data.toString();
  4282. }
  4283. cell.getElement().setInnerText(text);
  4284. }
  4285. }
  4286. /**
  4287. * the column is associated with
  4288. */
  4289. private Grid<T> grid;
  4290. /**
  4291. * Width of column in pixels as {@link #setWidth(double)} has been
  4292. * called
  4293. */
  4294. private double widthUser = GridConstants.DEFAULT_COLUMN_WIDTH_PX;
  4295. /**
  4296. * Renderer for rendering a value into the cell
  4297. */
  4298. private Renderer<? super C> bodyRenderer;
  4299. private boolean sortable = false;
  4300. private boolean editable = true;
  4301. private boolean resizable = true;
  4302. private boolean hidden = false;
  4303. private boolean hidable = false;
  4304. private String headerCaption = "";
  4305. private String assistiveCaption = null;
  4306. private String hidingToggleCaption = null;
  4307. private boolean handleWidgetEvents = false;
  4308. private double minimumWidthPx = GridConstants.DEFAULT_MIN_WIDTH;
  4309. private double maximumWidthPx = GridConstants.DEFAULT_MAX_WIDTH;
  4310. private int expandRatio = GridConstants.DEFAULT_EXPAND_RATIO;
  4311. private boolean minimumWidthFromContent = true;
  4312. /**
  4313. * Constructs a new column with a simple TextRenderer.
  4314. */
  4315. public Column() {
  4316. setRenderer(new DefaultTextRenderer());
  4317. }
  4318. /**
  4319. * Constructs a new column with a simple TextRenderer.
  4320. *
  4321. * @param caption
  4322. * The header caption for this column
  4323. *
  4324. * @throws IllegalArgumentException
  4325. * if given header caption is null
  4326. */
  4327. public Column(String caption) throws IllegalArgumentException {
  4328. this();
  4329. setHeaderCaption(caption);
  4330. }
  4331. /**
  4332. * Constructs a new column with a custom renderer.
  4333. *
  4334. * @param renderer
  4335. * The renderer to use for rendering the cells
  4336. *
  4337. * @throws IllegalArgumentException
  4338. * if given Renderer is null
  4339. */
  4340. public Column(Renderer<? super C> renderer)
  4341. throws IllegalArgumentException {
  4342. setRenderer(renderer);
  4343. }
  4344. /**
  4345. * Constructs a new column with a custom renderer.
  4346. *
  4347. * @param renderer
  4348. * The renderer to use for rendering the cells
  4349. * @param caption
  4350. * The header caption for this column
  4351. *
  4352. * @throws IllegalArgumentException
  4353. * if given Renderer or header caption is null
  4354. */
  4355. public Column(String caption, Renderer<? super C> renderer)
  4356. throws IllegalArgumentException {
  4357. this(renderer);
  4358. setHeaderCaption(caption);
  4359. }
  4360. /**
  4361. * Internally used by the grid to set itself
  4362. *
  4363. * @param grid
  4364. */
  4365. private void setGrid(Grid<T> grid) {
  4366. if (this.grid != null && grid != null) {
  4367. // Trying to replace grid
  4368. throw new IllegalStateException("Column already is attached "
  4369. + "to a grid. Remove the column first from the grid "
  4370. + "and then add it. (in: " + toString() + ")");
  4371. }
  4372. if (this.grid != null) {
  4373. this.grid.recalculateColumnWidths();
  4374. }
  4375. this.grid = grid;
  4376. if (this.grid != null) {
  4377. this.grid.recalculateColumnWidths();
  4378. }
  4379. }
  4380. /**
  4381. * Sets a header caption for this column.
  4382. *
  4383. * @param caption
  4384. * The header caption for this column
  4385. * @return the column itself
  4386. *
  4387. */
  4388. public Column<C, T> setHeaderCaption(String caption) {
  4389. if (caption == null) {
  4390. caption = "";
  4391. }
  4392. if (!this.headerCaption.equals(caption)) {
  4393. this.headerCaption = caption;
  4394. if (grid != null) {
  4395. updateHeader();
  4396. }
  4397. }
  4398. return this;
  4399. }
  4400. /**
  4401. * Returns the current header caption for this column.
  4402. *
  4403. * @since 7.6
  4404. * @return the header caption string
  4405. */
  4406. public String getHeaderCaption() {
  4407. return headerCaption;
  4408. }
  4409. /**
  4410. * Sets the header aria-label for this column.
  4411. *
  4412. * @param caption
  4413. * The header aria-label for this column
  4414. * @return the column itself
  4415. *
  4416. * @since 8.2
  4417. */
  4418. public Column<C, T> setAssistiveCaption(String caption) {
  4419. if (!Objects.equals(this.assistiveCaption, caption)) {
  4420. this.assistiveCaption = caption;
  4421. if (grid != null) {
  4422. grid.getHeader().requestSectionRefresh();
  4423. }
  4424. }
  4425. return this;
  4426. }
  4427. /**
  4428. * Returns the current header aria-label for this column.
  4429. *
  4430. * @return the header aria-label string
  4431. *
  4432. * @since 8.2
  4433. */
  4434. public String getAssistiveCaption() {
  4435. return assistiveCaption;
  4436. }
  4437. private void updateHeader() {
  4438. HeaderRow row = grid.getHeader().getDefaultRow();
  4439. if (row != null) {
  4440. setDefaultHeaderContent(row.getCell(this));
  4441. if (isHidable()) {
  4442. grid.columnHider.updateHidingToggle(this);
  4443. }
  4444. }
  4445. }
  4446. /**
  4447. * Returns the data that should be rendered into the cell. By default
  4448. * returning Strings and Widgets are supported. If the return type is a
  4449. * String then it will be treated as preformatted text.
  4450. * <p>
  4451. * To support other types you will need to pass a custom renderer to the
  4452. * column via the column constructor.
  4453. *
  4454. * @param row
  4455. * The row object that provides the cell content.
  4456. *
  4457. * @return The cell content
  4458. */
  4459. public abstract C getValue(T row);
  4460. /**
  4461. * The renderer to render the cell with. By default renders the data as
  4462. * a String or adds the widget into the cell if the column type is of
  4463. * widget type.
  4464. *
  4465. * @return The renderer to render the cell content with
  4466. */
  4467. public Renderer<? super C> getRenderer() {
  4468. return bodyRenderer;
  4469. }
  4470. /**
  4471. * Sets a custom {@link Renderer} for this column.
  4472. *
  4473. * @param renderer
  4474. * The renderer to use for rendering the cells
  4475. * @return the column itself
  4476. *
  4477. * @throws IllegalArgumentException
  4478. * if given Renderer is null
  4479. */
  4480. public Column<C, T> setRenderer(Renderer<? super C> renderer)
  4481. throws IllegalArgumentException {
  4482. if (renderer == null) {
  4483. throw new IllegalArgumentException("Renderer cannot be null.");
  4484. }
  4485. if (renderer != bodyRenderer) {
  4486. // Variables used to restore removed column.
  4487. boolean columnRemoved = false;
  4488. double widthInConfiguration = 0.0d;
  4489. ColumnConfiguration conf = null;
  4490. int index = 0;
  4491. if (!isHidden() && grid != null
  4492. && (bodyRenderer instanceof WidgetRenderer
  4493. || renderer instanceof WidgetRenderer)) {
  4494. // Column needs to be recreated.
  4495. index = grid.getVisibleColumns().indexOf(this);
  4496. conf = grid.escalator.getColumnConfiguration();
  4497. widthInConfiguration = conf.getColumnWidth(index);
  4498. conf.removeColumns(index, 1);
  4499. columnRemoved = true;
  4500. }
  4501. // Complex renderers need to be destroyed.
  4502. if (bodyRenderer instanceof ComplexRenderer) {
  4503. ((ComplexRenderer) bodyRenderer).destroy();
  4504. }
  4505. bodyRenderer = renderer;
  4506. if (columnRemoved) {
  4507. // Restore the column.
  4508. conf.insertColumns(index, 1);
  4509. conf.setColumnWidth(index, widthInConfiguration);
  4510. }
  4511. if (!isHidden() && grid != null) {
  4512. grid.requestRefreshBody();
  4513. }
  4514. }
  4515. return this;
  4516. }
  4517. /**
  4518. * Sets the pixel width of the column. Use a negative value for the grid
  4519. * to autosize column based on content and available space.
  4520. * <p>
  4521. * This action is done "finally", once the current execution loop
  4522. * returns. This is done to reduce overhead of unintentionally always
  4523. * recalculate all columns, when modifying several columns at once.
  4524. * <p>
  4525. * If the column is currently {@link #isHidden() hidden}, then this set
  4526. * width has effect only once the column has been made visible again.
  4527. *
  4528. * @param pixels
  4529. * the width in pixels or negative for auto sizing
  4530. * @return this column
  4531. */
  4532. public Column<C, T> setWidth(double pixels) {
  4533. if (!WidgetUtil.pixelValuesEqual(widthUser, pixels)) {
  4534. widthUser = pixels;
  4535. if (!isHidden()) {
  4536. scheduleColumnWidthRecalculator();
  4537. }
  4538. }
  4539. return this;
  4540. }
  4541. void doSetWidth(double pixels) {
  4542. assert !isHidden() : "applying width for a hidden column";
  4543. if (grid != null) {
  4544. int index = grid.getVisibleColumns().indexOf(this);
  4545. ColumnConfiguration conf = grid.escalator
  4546. .getColumnConfiguration();
  4547. conf.setColumnWidth(index, pixels);
  4548. }
  4549. }
  4550. /**
  4551. * Returns the pixel width of the column as given by the user.
  4552. * <p>
  4553. * <em>Note:</em> If a negative value was given to
  4554. * {@link #setWidth(double)}, that same negative value is returned here.
  4555. * <p>
  4556. * <em>Note:</em> Returns the value, even if the column is currently
  4557. * {@link #isHidden() hidden}.
  4558. *
  4559. * @return pixel width of the column, or a negative number if the column
  4560. * width has been automatically calculated.
  4561. * @see #setWidth(double)
  4562. * @see #getWidthActual()
  4563. */
  4564. public double getWidth() {
  4565. return widthUser;
  4566. }
  4567. /**
  4568. * Returns the effective pixel width of the column.
  4569. * <p>
  4570. * This differs from {@link #getWidth()} only when the column has been
  4571. * automatically resized, or when the column is currently
  4572. * {@link #isHidden() hidden}, when the value is 0.
  4573. *
  4574. * @return pixel width of the column.
  4575. */
  4576. public double getWidthActual() {
  4577. if (isHidden()) {
  4578. return 0;
  4579. }
  4580. return grid.escalator.getColumnConfiguration().getColumnWidthActual(
  4581. grid.getVisibleColumns().indexOf(this));
  4582. }
  4583. void reapplyWidth() {
  4584. scheduleColumnWidthRecalculator();
  4585. }
  4586. /**
  4587. * Sets whether the column should be sortable by the user. The grid can
  4588. * be sorted by a sortable column by clicking or tapping the column's
  4589. * default header. Programmatic sorting using the Grid#sort methods is
  4590. * not affected by this setting.
  4591. *
  4592. * @param sortable
  4593. * {@code true} if the user should be able to sort the
  4594. * column, {@code false} otherwise
  4595. * @return the column itself
  4596. */
  4597. public Column<C, T> setSortable(boolean sortable) {
  4598. if (this.sortable != sortable) {
  4599. this.sortable = sortable;
  4600. if (grid != null) {
  4601. grid.getHeader().requestSectionRefresh();
  4602. }
  4603. }
  4604. return this;
  4605. }
  4606. /**
  4607. * Returns whether the user can sort the grid by this column.
  4608. * <p>
  4609. * <em>Note:</em> it is possible to sort by this column programmatically
  4610. * using the Grid#sort methods regardless of the returned value.
  4611. *
  4612. * @return {@code true} if the column is sortable by the user,
  4613. * {@code false} otherwise
  4614. */
  4615. public boolean isSortable() {
  4616. return sortable;
  4617. }
  4618. /**
  4619. * Sets whether this column can be resized by the user.
  4620. *
  4621. * @since 7.6
  4622. *
  4623. * @param resizable
  4624. * {@code true} if this column should be resizable,
  4625. * {@code false} otherwise
  4626. * @return this column
  4627. */
  4628. public Column<C, T> setResizable(boolean resizable) {
  4629. if (this.resizable != resizable) {
  4630. this.resizable = resizable;
  4631. if (grid != null) {
  4632. grid.getHeader().requestSectionRefresh();
  4633. }
  4634. }
  4635. return this;
  4636. }
  4637. /**
  4638. * Returns whether this column can be resized by the user. Default is
  4639. * {@code true}.
  4640. * <p>
  4641. * <em>Note:</em> the column can be programmatically resized using
  4642. * {@link #setWidth(double)} and {@link #setWidthUndefined()} regardless
  4643. * of the returned value.
  4644. *
  4645. * @since 7.6
  4646. *
  4647. * @return {@code true} if this column is resizable, {@code false}
  4648. * otherwise
  4649. */
  4650. public boolean isResizable() {
  4651. return grid.isEnabled() && resizable;
  4652. }
  4653. /**
  4654. * Hides or shows the column. By default columns are visible before
  4655. * explicitly hiding them.
  4656. *
  4657. * @since 7.5.0
  4658. * @param hidden
  4659. * <code>true</code> to hide the column, <code>false</code>
  4660. * to show
  4661. * @return this column
  4662. */
  4663. public Column<C, T> setHidden(boolean hidden) {
  4664. setHidden(hidden, false);
  4665. return this;
  4666. }
  4667. private void setHidden(boolean hidden, boolean userOriginated) {
  4668. if (this.hidden != hidden) {
  4669. if (grid == null) {
  4670. // Not yet attached so just update the flag so that a column
  4671. // can initially be hidden
  4672. this.hidden = hidden;
  4673. return;
  4674. }
  4675. if (hidden) {
  4676. grid.escalator.getColumnConfiguration().removeColumns(
  4677. grid.getVisibleColumns().indexOf(this), 1);
  4678. this.hidden = hidden;
  4679. } else {
  4680. this.hidden = hidden;
  4681. int columnIndex = grid.getVisibleColumns().indexOf(this);
  4682. grid.escalator.getColumnConfiguration()
  4683. .insertColumns(columnIndex, 1);
  4684. // make sure column is set to frozen if it needs to be,
  4685. // escalator doesn't handle situation where the added column
  4686. // would be the last frozen column
  4687. int gridFrozenColumns = grid.getFrozenColumnCount();
  4688. // Correct column index for multiselect mode
  4689. if (grid.getSelectionColumn().isPresent()) {
  4690. gridFrozenColumns++;
  4691. }
  4692. int escalatorFrozenColumns = grid.escalator
  4693. .getColumnConfiguration().getFrozenColumnCount();
  4694. if (gridFrozenColumns > escalatorFrozenColumns
  4695. && escalatorFrozenColumns == columnIndex
  4696. && grid.getColumns()
  4697. .indexOf(this) < gridFrozenColumns) {
  4698. grid.escalator.getColumnConfiguration()
  4699. .setFrozenColumnCount(++escalatorFrozenColumns);
  4700. }
  4701. }
  4702. grid.columnHider.updateHidingToggle(this);
  4703. grid.header.updateColSpans();
  4704. grid.footer.updateColSpans();
  4705. scheduleColumnWidthRecalculator();
  4706. this.grid.fireEvent(new ColumnVisibilityChangeEvent<>(this,
  4707. hidden, userOriginated));
  4708. }
  4709. }
  4710. /**
  4711. * Returns whether this column is hidden. Default is {@code false}.
  4712. *
  4713. * @since 7.5.0
  4714. * @return {@code true} if the column is currently hidden, {@code false}
  4715. * otherwise
  4716. */
  4717. public boolean isHidden() {
  4718. return hidden;
  4719. }
  4720. /**
  4721. * Set whether it is possible for the user to hide this column or not.
  4722. * Default is {@code false}.
  4723. * <p>
  4724. * <em>Note:</em> it is still possible to hide the column
  4725. * programmatically using {@link #setHidden(boolean)}.
  4726. *
  4727. * @since 7.5.0
  4728. * @param hidable
  4729. * {@code true} the user can hide this column, {@code false}
  4730. * otherwise
  4731. * @return this column
  4732. */
  4733. public Column<C, T> setHidable(boolean hidable) {
  4734. if (this.hidable != hidable) {
  4735. this.hidable = hidable;
  4736. grid.columnHider.updateColumnHidable(this);
  4737. }
  4738. return this;
  4739. }
  4740. /**
  4741. * Is it possible for the the user to hide this column. Default is
  4742. * {@code false}.
  4743. * <p>
  4744. * <em>Note:</em> the column can be programmatically hidden using
  4745. * {@link #setHidden(boolean)} regardless of the returned value.
  4746. *
  4747. * @since 7.5.0
  4748. * @return <code>true</code> if the user can hide the column,
  4749. * <code>false</code> if not
  4750. */
  4751. public boolean isHidable() {
  4752. return hidable;
  4753. }
  4754. /**
  4755. * Sets the hiding toggle's caption for this column. Shown in the toggle
  4756. * for this column in the grid's sidebar when the column is
  4757. * {@link #isHidable() hidable}.
  4758. * <p>
  4759. * The default value is <code>null</code>. In this case the header
  4760. * caption is used, see {@link #setHeaderCaption(String)}.
  4761. *
  4762. * @since 7.5.0
  4763. * @param hidingToggleCaption
  4764. * the caption for the hiding toggle for this column
  4765. * @return this column
  4766. */
  4767. public Column<C, T> setHidingToggleCaption(String hidingToggleCaption) {
  4768. this.hidingToggleCaption = hidingToggleCaption;
  4769. if (isHidable()) {
  4770. grid.columnHider.updateHidingToggle(this);
  4771. }
  4772. return this;
  4773. }
  4774. /**
  4775. * Gets the hiding toggle caption for this column.
  4776. *
  4777. * @since 7.5.0
  4778. * @see #setHidingToggleCaption(String)
  4779. * @return the hiding toggle's caption for this column
  4780. */
  4781. public String getHidingToggleCaption() {
  4782. return hidingToggleCaption;
  4783. }
  4784. @Override
  4785. public String toString() {
  4786. String details = "";
  4787. if (headerCaption != null && !headerCaption.isEmpty()) {
  4788. details += "header:\"" + headerCaption + "\" ";
  4789. } else {
  4790. details += "header:empty ";
  4791. }
  4792. if (grid != null) {
  4793. int index = grid.getColumns().indexOf(this);
  4794. if (index != -1) {
  4795. details += "attached:#" + index + " ";
  4796. } else {
  4797. details += "attached:unindexed ";
  4798. }
  4799. } else {
  4800. details += "detached ";
  4801. }
  4802. details += "sortable:" + sortable + " ";
  4803. return getClass().getSimpleName() + "[" + details.trim() + "]";
  4804. }
  4805. /**
  4806. * Sets the minimum width for this column.
  4807. * <p>
  4808. * This defines the minimum guaranteed pixel width of the column
  4809. * <em>when it is set to expand</em>.
  4810. * <p>
  4811. * This action is done "finally", once the current execution loop
  4812. * returns. This is done to reduce overhead of unintentionally always
  4813. * recalculate all columns, when modifying several columns at once.
  4814. *
  4815. * @param pixels
  4816. * the minimum width
  4817. * @return this column
  4818. */
  4819. public Column<C, T> setMinimumWidth(double pixels) {
  4820. final double maxwidth = getMaximumWidth();
  4821. if (pixels >= 0 && pixels > maxwidth && maxwidth >= 0) {
  4822. throw new IllegalArgumentException("New minimum width ("
  4823. + pixels + ") was greater than maximum width ("
  4824. + maxwidth + ")");
  4825. }
  4826. if (minimumWidthPx != pixels) {
  4827. minimumWidthPx = pixels;
  4828. scheduleColumnWidthRecalculator();
  4829. }
  4830. return this;
  4831. }
  4832. /**
  4833. * Sets whether the width of the contents in the column should be
  4834. * considered minimum width for this column.
  4835. * <p>
  4836. * If this is set to <code>true</code> (default for backwards
  4837. * compatibility), then a column will not shrink to smaller than the
  4838. * width required to show the contents available when calculating the
  4839. * widths (first N rows).
  4840. * <p>
  4841. * If this is set to <code>false</code>, then a column will shrink if
  4842. * necessary to the minimum width defined by
  4843. * {@link #setMinimumWidth(double)} <em>when it is set to expand</em>.
  4844. *
  4845. * @param minimumWidthFromContent
  4846. * <code>true</code> to reserve space for all contents,
  4847. * <code>false</code> to allow the column to shrink smaller
  4848. * than the contents
  4849. * @since 8.1
  4850. */
  4851. public void setMinimumWidthFromContent(
  4852. boolean minimumWidthFromContent) {
  4853. this.minimumWidthFromContent = minimumWidthFromContent;
  4854. }
  4855. /**
  4856. * Gets whether the width of the contents in the column should be
  4857. * considered minimum width for this column.
  4858. *
  4859. * @return <code>true</code> to reserve space for all contents,
  4860. * <code>false</code> to allow the column to shrink smaller than
  4861. * the contents
  4862. * @since 8.1
  4863. */
  4864. public boolean isMinimumWidthFromContent() {
  4865. return minimumWidthFromContent;
  4866. }
  4867. /**
  4868. * Sets the maximum width for this column.
  4869. * <p>
  4870. * This defines the maximum allowed pixel width of the column <em>when
  4871. * it is set to expand</em>.
  4872. * <p>
  4873. * This action is done "finally", once the current execution loop
  4874. * returns. This is done to reduce overhead of unintentionally always
  4875. * recalculate all columns, when modifying several columns at once.
  4876. *
  4877. * @param pixels
  4878. * the maximum width
  4879. * @return this column
  4880. */
  4881. public Column<C, T> setMaximumWidth(double pixels) {
  4882. final double minwidth = getMinimumWidth();
  4883. if (pixels >= 0 && pixels < minwidth && minwidth >= 0) {
  4884. throw new IllegalArgumentException("New maximum width ("
  4885. + pixels + ") was less than minimum width (" + minwidth
  4886. + ")");
  4887. }
  4888. if (maximumWidthPx != pixels) {
  4889. maximumWidthPx = pixels;
  4890. scheduleColumnWidthRecalculator();
  4891. }
  4892. return this;
  4893. }
  4894. /**
  4895. * Sets the ratio with which the column expands.
  4896. * <p>
  4897. * By default, all columns expand equally (treated as if all of them had
  4898. * an expand ratio of 1). Once at least one column gets a defined expand
  4899. * ratio, the implicit expand ratio is removed, and only the defined
  4900. * expand ratios are taken into account.
  4901. * <p>
  4902. * If a column has a defined width ({@link #setWidth(double)}), it
  4903. * overrides this method's effects.
  4904. * <p>
  4905. * <em>Example:</em> A grid with three columns, with expand ratios 0, 1
  4906. * and 2, respectively. The column with a <strong>ratio of 0 is exactly
  4907. * as wide as its contents requires</strong>. The column with a ratio of
  4908. * 1 is as wide as it needs, <strong>plus a third of any excess
  4909. * space</strong>, bceause we have 3 parts total, and this column
  4910. * reservs only one of those. The column with a ratio of 2, is as wide
  4911. * as it needs to be, <strong>plus two thirds</strong> of the excess
  4912. * width.
  4913. * <p>
  4914. * This action is done "finally", once the current execution loop
  4915. * returns. This is done to reduce overhead of unintentionally always
  4916. * recalculate all columns, when modifying several columns at once.
  4917. *
  4918. * @param ratio
  4919. * the expand ratio of this column. {@code 0} to not have it
  4920. * expand at all. A negative number to clear the expand
  4921. * value.
  4922. * @return this column
  4923. */
  4924. public Column<C, T> setExpandRatio(int ratio) {
  4925. if (expandRatio != ratio) {
  4926. expandRatio = ratio;
  4927. scheduleColumnWidthRecalculator();
  4928. }
  4929. return this;
  4930. }
  4931. /**
  4932. * Clears the column's expand ratio.
  4933. * <p>
  4934. * Same as calling {@link #setExpandRatio(int) setExpandRatio(-1)}
  4935. *
  4936. * @return this column
  4937. */
  4938. public Column<C, T> clearExpandRatio() {
  4939. return setExpandRatio(-1);
  4940. }
  4941. /**
  4942. * Gets the minimum width for this column.
  4943. *
  4944. * @return the minimum width for this column
  4945. * @see #setMinimumWidth(double)
  4946. */
  4947. public double getMinimumWidth() {
  4948. return minimumWidthPx;
  4949. }
  4950. /**
  4951. * Gets the maximum width for this column.
  4952. *
  4953. * @return the maximum width for this column
  4954. * @see #setMaximumWidth(double)
  4955. */
  4956. public double getMaximumWidth() {
  4957. return maximumWidthPx;
  4958. }
  4959. /**
  4960. * Gets the expand ratio for this column.
  4961. *
  4962. * @return the expand ratio for this column
  4963. * @see #setExpandRatio(int)
  4964. */
  4965. public int getExpandRatio() {
  4966. return expandRatio;
  4967. }
  4968. /**
  4969. * Sets whether the values in this column should be editable by the user
  4970. * when the row editor is active. By default columns are editable.
  4971. *
  4972. * @param editable
  4973. * {@code true} to set this column editable, {@code false}
  4974. * otherwise
  4975. * @return this column
  4976. *
  4977. * @throws IllegalStateException
  4978. * if the editor is currently active
  4979. *
  4980. * @see Grid#editRow(int)
  4981. * @see Grid#isEditorActive()
  4982. */
  4983. public Column<C, T> setEditable(boolean editable) {
  4984. if (editable != this.editable && grid != null
  4985. && grid.isEditorActive()) {
  4986. throw new IllegalStateException(
  4987. "Cannot change column editable status while the editor is active");
  4988. }
  4989. this.editable = editable;
  4990. return this;
  4991. }
  4992. /**
  4993. * Returns whether the values in this column are editable by the user
  4994. * when the row editor is active.
  4995. *
  4996. * @return {@code true} if this column is editable, {@code false}
  4997. * otherwise
  4998. *
  4999. * @see #setEditable(boolean)
  5000. */
  5001. public boolean isEditable() {
  5002. return editable;
  5003. }
  5004. private void scheduleColumnWidthRecalculator() {
  5005. if (grid != null) {
  5006. grid.recalculateColumnWidths();
  5007. } else {
  5008. /*
  5009. * NOOP
  5010. *
  5011. * Since setGrid() will call reapplyWidths as the colum is
  5012. * attached to a grid, it will call setWidth, which, in turn,
  5013. * will call this method again. Therefore, it's guaranteed that
  5014. * the recalculation is scheduled eventually, once the column is
  5015. * attached to a grid.
  5016. */
  5017. }
  5018. }
  5019. /**
  5020. * Resets the default header cell contents to column header captions.
  5021. *
  5022. * @since 7.5.1
  5023. * @param cell
  5024. * default header cell for this column
  5025. */
  5026. protected void setDefaultHeaderContent(HeaderCell cell) {
  5027. cell.setText(headerCaption);
  5028. }
  5029. /**
  5030. * Returns whether Grid should handle events from Widgets in this
  5031. * Column.
  5032. *
  5033. * @return {@code true} to handle events from widgets; {@code false} to
  5034. * not
  5035. * @since 8.3
  5036. */
  5037. public boolean isHandleWidgetEvents() {
  5038. return handleWidgetEvents;
  5039. }
  5040. /**
  5041. * Sets whether Grid should handle events from Widgets in this Column.
  5042. *
  5043. * @param handleWidgetEvents
  5044. * {@code true} to let grid handle events from widgets;
  5045. * {@code false} to not
  5046. *
  5047. * @since 8.3
  5048. */
  5049. public void setHandleWidgetEvents(boolean handleWidgetEvents) {
  5050. this.handleWidgetEvents = handleWidgetEvents;
  5051. }
  5052. }
  5053. protected class BodyUpdater implements EscalatorUpdater {
  5054. @Override
  5055. public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
  5056. int rowIndex = row.getRow();
  5057. rowReference.set(rowIndex, getDataSource().getRow(rowIndex),
  5058. row.getElement());
  5059. for (FlyweightCell cell : cellsToAttach) {
  5060. Renderer<?> renderer = findRenderer(cell);
  5061. if (renderer instanceof ComplexRenderer) {
  5062. try {
  5063. Column<?, T> column = getVisibleColumn(
  5064. cell.getColumn());
  5065. rendererCellReference.set(cell,
  5066. getColumns().indexOf(column), column);
  5067. ((ComplexRenderer<?>) renderer)
  5068. .init(rendererCellReference);
  5069. } catch (RuntimeException e) {
  5070. getLogger().log(Level.SEVERE,
  5071. "Error initing cell in column "
  5072. + cell.getColumn(),
  5073. e);
  5074. }
  5075. }
  5076. }
  5077. }
  5078. @Override
  5079. public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) {
  5080. for (FlyweightCell cell : attachedCells) {
  5081. Renderer<?> renderer = findRenderer(cell);
  5082. if (renderer instanceof WidgetRenderer) {
  5083. try {
  5084. WidgetRenderer<?, ?> widgetRenderer = (WidgetRenderer<?, ?>) renderer;
  5085. Widget widget = widgetRenderer.createWidget();
  5086. assert widget != null : "WidgetRenderer.createWidget() returned null. It should return a widget.";
  5087. assert widget
  5088. .getParent() == null : "WidgetRenderer.createWidget() returned a widget which already is attached.";
  5089. assert cell.getElement()
  5090. .getChildCount() == 0 : "Cell content should be empty when adding Widget";
  5091. // Physical attach
  5092. cell.getElement().appendChild(widget.getElement());
  5093. // Logical attach
  5094. setParent(widget, Grid.this);
  5095. } catch (RuntimeException e) {
  5096. getLogger().log(Level.SEVERE,
  5097. "Error attaching child widget in column "
  5098. + cell.getColumn(),
  5099. e);
  5100. }
  5101. }
  5102. }
  5103. }
  5104. @Override
  5105. public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) {
  5106. int rowIndex = row.getRow();
  5107. TableRowElement rowElement = row.getElement();
  5108. T rowData = dataSource.getRow(rowIndex);
  5109. boolean hasData = rowData != null;
  5110. /*
  5111. * TODO could be more efficient to build a list of all styles that
  5112. * should be used and update the element only once instead of
  5113. * attempting to update only the ones that have changed.
  5114. */
  5115. // Assign stylename for rows with data
  5116. boolean usedToHaveData = rowElement
  5117. .hasClassName(rowHasDataStyleName);
  5118. if (usedToHaveData != hasData) {
  5119. setStyleName(rowElement, rowHasDataStyleName, hasData);
  5120. }
  5121. boolean isEvenIndex = row.getRow() % 2 == 0;
  5122. setStyleName(rowElement, rowStripeStyleName, !isEvenIndex);
  5123. rowReference.set(rowIndex, rowData, rowElement);
  5124. boolean isSelected = hasData && isSelected(rowData);
  5125. if (Grid.this.selectionModel.isSelectionAllowed()) {
  5126. rowElement.setAttribute("aria-selected",
  5127. String.valueOf(isSelected));
  5128. } else {
  5129. rowElement.removeAttribute("aria-selected");
  5130. }
  5131. if (hasData) {
  5132. setStyleName(rowElement, rowSelectedStyleName, isSelected);
  5133. if (rowStyleGenerator != null) {
  5134. try {
  5135. String rowStylename = rowStyleGenerator
  5136. .getStyle(rowReference);
  5137. setCustomStyleName(rowElement, rowStylename);
  5138. } catch (RuntimeException e) {
  5139. getLogger().log(Level.SEVERE,
  5140. "Error generating styles for row "
  5141. + row.getRow(),
  5142. e);
  5143. }
  5144. } else {
  5145. // Remove in case there was a generator previously
  5146. setCustomStyleName(rowElement, null);
  5147. }
  5148. } else if (usedToHaveData) {
  5149. setStyleName(rowElement, rowSelectedStyleName, false);
  5150. setCustomStyleName(rowElement, null);
  5151. }
  5152. cellFocusHandler.updateFocusedRowStyle(row);
  5153. for (FlyweightCell cell : cellsToUpdate) {
  5154. Column<?, T> column = getVisibleColumn(cell.getColumn());
  5155. final int columnIndex = getColumns().indexOf(column);
  5156. assert column != null : "Column was not found from cell ("
  5157. + cell.getColumn() + "," + cell.getRow() + ")";
  5158. cellFocusHandler.updateFocusedCellStyle(cell,
  5159. escalator.getBody());
  5160. if (hasData && cellStyleGenerator != null) {
  5161. try {
  5162. cellReference.set(cell.getColumn(), columnIndex,
  5163. column);
  5164. String generatedStyle = cellStyleGenerator
  5165. .getStyle(cellReference);
  5166. setCustomStyleName(cell.getElement(), generatedStyle);
  5167. } catch (RuntimeException e) {
  5168. getLogger().log(Level.SEVERE,
  5169. "Error generating style for cell in column "
  5170. + cell.getColumn(),
  5171. e);
  5172. }
  5173. } else if (hasData || usedToHaveData) {
  5174. setCustomStyleName(cell.getElement(), null);
  5175. }
  5176. Renderer renderer = column.getRenderer();
  5177. try {
  5178. rendererCellReference.set(cell, columnIndex, column);
  5179. if (renderer instanceof ComplexRenderer) {
  5180. // Hide cell content if needed
  5181. ComplexRenderer clxRenderer = (ComplexRenderer) renderer;
  5182. if (hasData) {
  5183. if (!usedToHaveData) {
  5184. // Prepare cell for rendering
  5185. clxRenderer.setContentVisible(
  5186. rendererCellReference, true);
  5187. }
  5188. Object value = column.getValue(rowData);
  5189. clxRenderer.render(rendererCellReference, value);
  5190. } else {
  5191. // Prepare cell for no data
  5192. clxRenderer.setContentVisible(rendererCellReference,
  5193. false);
  5194. }
  5195. } else if (hasData) {
  5196. // Simple renderers just render
  5197. Object value = column.getValue(rowData);
  5198. renderer.render(rendererCellReference, value);
  5199. } else {
  5200. // Clear cell if there is no data
  5201. cell.getElement().removeAllChildren();
  5202. }
  5203. } catch (RuntimeException e) {
  5204. getLogger().log(Level.SEVERE,
  5205. "Error rendering cell in column "
  5206. + cell.getColumn(),
  5207. e);
  5208. }
  5209. }
  5210. }
  5211. @Override
  5212. public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
  5213. for (FlyweightCell cell : cellsToDetach) {
  5214. Renderer<?> renderer = findRenderer(cell);
  5215. if (renderer instanceof WidgetRenderer) {
  5216. try {
  5217. Widget w = WidgetUtil.findWidget(
  5218. cell.getElement().getFirstChildElement());
  5219. if (w != null) {
  5220. // Logical detach
  5221. setParent(w, null);
  5222. // Physical detach
  5223. cell.getElement().removeChild(w.getElement());
  5224. }
  5225. } catch (RuntimeException e) {
  5226. getLogger().log(Level.SEVERE,
  5227. "Error detaching widget in column "
  5228. + cell.getColumn(),
  5229. e);
  5230. }
  5231. }
  5232. }
  5233. }
  5234. @Override
  5235. public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) {
  5236. int rowIndex = row.getRow();
  5237. // Passing null row data since it might not exist in the data source
  5238. // any more
  5239. rowReference.set(rowIndex, null, row.getElement());
  5240. for (FlyweightCell cell : detachedCells) {
  5241. Renderer<?> renderer = findRenderer(cell);
  5242. if (renderer instanceof ComplexRenderer) {
  5243. try {
  5244. Column<?, T> column = getVisibleColumn(
  5245. cell.getColumn());
  5246. rendererCellReference.set(cell,
  5247. getColumns().indexOf(column), column);
  5248. ((ComplexRenderer) renderer)
  5249. .destroy(rendererCellReference);
  5250. } catch (RuntimeException e) {
  5251. getLogger().log(Level.SEVERE,
  5252. "Error destroying cell in column "
  5253. + cell.getColumn(),
  5254. e);
  5255. }
  5256. }
  5257. }
  5258. }
  5259. }
  5260. protected class StaticSectionUpdater implements EscalatorUpdater {
  5261. private StaticSection<?> section;
  5262. private RowContainer container;
  5263. public StaticSectionUpdater(StaticSection<?> section,
  5264. RowContainer container) {
  5265. super();
  5266. this.section = section;
  5267. this.container = container;
  5268. }
  5269. @Override
  5270. public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) {
  5271. StaticSection.StaticRow<?> staticRow = section.getRow(row.getRow());
  5272. final List<Column<?, T>> columns = getVisibleColumns();
  5273. setCustomStyleName(row.getElement(), staticRow.getStyleName());
  5274. for (FlyweightCell cell : cellsToUpdate) {
  5275. final StaticSection.StaticCell metadata = staticRow
  5276. .getCell(columns.get(cell.getColumn()));
  5277. // Decorate default row with sorting indicators
  5278. if (staticRow instanceof HeaderRow) {
  5279. addAriaLabelToHeaderRow(cell);
  5280. addSortingIndicatorsToHeaderRow((HeaderRow) staticRow,
  5281. cell);
  5282. }
  5283. // Assign colspan to cell before rendering
  5284. cell.setColSpan(metadata.getColspan());
  5285. Element td = cell.getElement();
  5286. td.removeAllChildren();
  5287. setCustomStyleName(td, metadata.getStyleName());
  5288. Element content;
  5289. // Wrap text or html content in default header to isolate
  5290. // the content from the possible column resize drag handle
  5291. // next to it
  5292. if (metadata.getType() != GridStaticCellType.WIDGET) {
  5293. content = DOM.createDiv();
  5294. if (staticRow instanceof HeaderRow) {
  5295. content.setClassName(getStylePrimaryName()
  5296. + "-column-header-content");
  5297. if (((HeaderRow) staticRow).isDefault()) {
  5298. content.setClassName(content.getClassName() + " "
  5299. + getStylePrimaryName()
  5300. + "-column-default-header-content");
  5301. }
  5302. } else if (staticRow instanceof FooterRow) {
  5303. content.setClassName(getStylePrimaryName()
  5304. + "-column-footer-content");
  5305. } else {
  5306. getLogger().severe("Unhandled static row type "
  5307. + staticRow.getClass().getCanonicalName());
  5308. }
  5309. td.appendChild(content);
  5310. } else {
  5311. content = td;
  5312. }
  5313. switch (metadata.getType()) {
  5314. case TEXT:
  5315. content.setInnerText(metadata.getText());
  5316. break;
  5317. case HTML:
  5318. content.setInnerHTML(metadata.getHtml());
  5319. break;
  5320. case WIDGET:
  5321. preDetach(row, Arrays.asList(cell));
  5322. content.setInnerHTML("");
  5323. postAttach(row, Arrays.asList(cell));
  5324. break;
  5325. }
  5326. // XXX: Should add only once in preAttach/postAttach or when
  5327. // resizable status changes
  5328. // Only add resize handles to default header row for now
  5329. if (columns.get(cell.getColumn()).isResizable()
  5330. && staticRow instanceof HeaderRow
  5331. && ((HeaderRow) staticRow).isDefault()) {
  5332. final DivElement resizeElement = Document.get()
  5333. .createDivElement();
  5334. resizeElement.addClassName(getStylePrimaryName()
  5335. + "-column-resize-simple-indicator");
  5336. final int column = cell.getColumn();
  5337. final DragHandle dragger = new DragHandle(
  5338. getStylePrimaryName() + "-column-resize-handle");
  5339. dragger.addTo(td);
  5340. // Common functionality for drag handle callback
  5341. // implementations
  5342. abstract class AbstractDHCallback
  5343. implements DragHandleCallback {
  5344. protected Column<?, T> col = getVisibleColumn(column);
  5345. protected double initialWidth = 0;
  5346. protected double minCellWidth;
  5347. protected double width;
  5348. protected void dragStarted() {
  5349. initialWidth = col.getWidthActual();
  5350. width = initialWidth;
  5351. minCellWidth = escalator.getMinCellWidth(
  5352. getVisibleColumns().indexOf(col));
  5353. for (Column<?, T> c : getVisibleColumns()) {
  5354. if (selectionColumn == c) {
  5355. // Don't modify selection column.
  5356. continue;
  5357. }
  5358. if (c.getWidth() < 0) {
  5359. c.setWidth(c.getWidthActual());
  5360. fireEvent(new ColumnResizeEvent<>(c));
  5361. }
  5362. }
  5363. WidgetUtil.setTextSelectionEnabled(getElement(),
  5364. false);
  5365. }
  5366. protected void dragEnded() {
  5367. WidgetUtil.setTextSelectionEnabled(getElement(),
  5368. true);
  5369. }
  5370. }
  5371. final DragHandleCallback simpleResizeMode = new AbstractDHCallback() {
  5372. @Override
  5373. protected void dragEnded() {
  5374. super.dragEnded();
  5375. dragger.getElement().removeChild(resizeElement);
  5376. }
  5377. @Override
  5378. public void onStart() {
  5379. dragStarted();
  5380. dragger.getElement().appendChild(resizeElement);
  5381. resizeElement.getStyle().setLeft(
  5382. (dragger.getElement().getOffsetWidth()
  5383. - resizeElement.getOffsetWidth())
  5384. * .5,
  5385. Unit.PX);
  5386. resizeElement.getStyle().setHeight(
  5387. col.grid.getOffsetHeight(), Unit.PX);
  5388. }
  5389. @Override
  5390. public void onUpdate(double deltaX, double deltaY) {
  5391. width = Math.max(minCellWidth,
  5392. initialWidth + deltaX);
  5393. resizeElement.getStyle().setLeft(
  5394. (dragger.getElement().getOffsetWidth()
  5395. - resizeElement.getOffsetWidth())
  5396. * .5 + (width - initialWidth),
  5397. Unit.PX);
  5398. }
  5399. @Override
  5400. public void onCancel() {
  5401. dragEnded();
  5402. }
  5403. @Override
  5404. public void onComplete() {
  5405. dragEnded();
  5406. col.setWidth(width);
  5407. // Need to wait for column width recalculation
  5408. // scheduled by setWidth() before firing the event
  5409. Scheduler.get().scheduleDeferred(() -> fireEvent(
  5410. new ColumnResizeEvent<>(col)));
  5411. }
  5412. };
  5413. final DragHandleCallback animatedResizeMode = new AbstractDHCallback() {
  5414. @Override
  5415. public void onStart() {
  5416. dragStarted();
  5417. }
  5418. @Override
  5419. public void onUpdate(double deltaX, double deltaY) {
  5420. width = Math.max(minCellWidth,
  5421. initialWidth + deltaX);
  5422. col.setWidth(width);
  5423. }
  5424. @Override
  5425. public void onCancel() {
  5426. dragEnded();
  5427. col.setWidth(initialWidth);
  5428. }
  5429. @Override
  5430. public void onComplete() {
  5431. dragEnded();
  5432. col.setWidth(width);
  5433. fireEvent(new ColumnResizeEvent<>(col));
  5434. }
  5435. };
  5436. // DragHandle gets assigned a 'master callback' that
  5437. // delegates
  5438. // functionality to the correct case-specific implementation
  5439. dragger.setCallback(new DragHandleCallback() {
  5440. private DragHandleCallback currentCallback;
  5441. @Override
  5442. public void onStart() {
  5443. switch (getColumnResizeMode()) {
  5444. case SIMPLE:
  5445. currentCallback = simpleResizeMode;
  5446. break;
  5447. case ANIMATED:
  5448. currentCallback = animatedResizeMode;
  5449. break;
  5450. default:
  5451. throw new UnsupportedOperationException(
  5452. "Support for current column resize mode is not yet implemented");
  5453. }
  5454. currentCallback.onStart();
  5455. }
  5456. @Override
  5457. public void onUpdate(double deltaX, double deltaY) {
  5458. currentCallback.onUpdate(deltaX, deltaY);
  5459. }
  5460. @Override
  5461. public void onCancel() {
  5462. currentCallback.onCancel();
  5463. }
  5464. @Override
  5465. public void onComplete() {
  5466. currentCallback.onComplete();
  5467. }
  5468. });
  5469. }
  5470. cellFocusHandler.updateFocusedCellStyle(cell, container);
  5471. }
  5472. }
  5473. private void addAriaLabelToHeaderRow(FlyweightCell cell) {
  5474. Element cellElement = cell.getElement();
  5475. final Column<?, T> column = getVisibleColumn(cell.getColumn());
  5476. if (column.getAssistiveCaption() != null) {
  5477. cellElement.setAttribute("aria-label",
  5478. column.getAssistiveCaption());
  5479. } else {
  5480. cellElement.removeAttribute("aria-label");
  5481. }
  5482. }
  5483. private void addSortingIndicatorsToHeaderRow(HeaderRow headerRow,
  5484. FlyweightCell cell) {
  5485. Element cellElement = cell.getElement();
  5486. boolean sortedBefore = cellElement.hasClassName("sort-asc")
  5487. || cellElement.hasClassName("sort-desc");
  5488. cleanup(cell);
  5489. if (!headerRow.isDefault()) {
  5490. // Nothing more to do if not in the default row
  5491. return;
  5492. }
  5493. final Column<?, T> column = getVisibleColumn(cell.getColumn());
  5494. SortOrder sortingOrder = getSortOrder(column);
  5495. boolean sortable = column.isSortable();
  5496. if (sortable) {
  5497. cellElement.addClassName("sortable");
  5498. cellElement.setAttribute("aria-sort", "none");
  5499. }
  5500. if (!sortable || sortingOrder == null) {
  5501. // Only apply sorting indicators to sortable header columns
  5502. return;
  5503. }
  5504. if (SortDirection.ASCENDING == sortingOrder.getDirection()) {
  5505. cellElement.addClassName("sort-asc");
  5506. cellElement.setAttribute("aria-sort", "ascending");
  5507. } else {
  5508. cellElement.addClassName("sort-desc");
  5509. cellElement.setAttribute("aria-sort", "descending");
  5510. }
  5511. int sortIndex = Grid.this.getSortOrder().indexOf(sortingOrder);
  5512. if (sortIndex > -1 && Grid.this.getSortOrder().size() > 1) {
  5513. // Show sort order indicator if column is
  5514. // sorted and other sorted columns also exists.
  5515. cellElement.setAttribute("sort-order",
  5516. String.valueOf(sortIndex + 1));
  5517. cellElement.setAttribute("aria-sort", "other");
  5518. }
  5519. if (!sortedBefore) {
  5520. verifyColumnWidth(column);
  5521. }
  5522. }
  5523. /**
  5524. * Sort indicator requires a bit more space from the cell than normally.
  5525. * This method check that the now sorted column has enough width.
  5526. *
  5527. * @param column
  5528. * sorted column
  5529. */
  5530. private void verifyColumnWidth(Column<?, T> column) {
  5531. int colIndex = getColumns().indexOf(column);
  5532. double minWidth = escalator.getMinCellWidth(colIndex);
  5533. if (column.getWidthActual() < minWidth) {
  5534. // Fix column size
  5535. escalator.getColumnConfiguration().setColumnWidth(colIndex,
  5536. minWidth);
  5537. fireEvent(new ColumnResizeEvent<>(column));
  5538. }
  5539. }
  5540. /**
  5541. * Finds the sort order for this column
  5542. */
  5543. private SortOrder getSortOrder(Column<?, ?> column) {
  5544. for (SortOrder order : Grid.this.getSortOrder()) {
  5545. if (order.getColumn() == column) {
  5546. return order;
  5547. }
  5548. }
  5549. return null;
  5550. }
  5551. private void cleanup(FlyweightCell cell) {
  5552. Element cellElement = cell.getElement();
  5553. cellElement.removeAttribute("sort-order");
  5554. cellElement.removeAttribute("aria-sort");
  5555. cellElement.removeClassName("sort-desc");
  5556. cellElement.removeClassName("sort-asc");
  5557. cellElement.removeClassName("sortable");
  5558. }
  5559. @Override
  5560. public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
  5561. }
  5562. @Override
  5563. public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) {
  5564. StaticSection.StaticRow<?> gridRow = section.getRow(row.getRow());
  5565. List<Column<?, T>> columns = getVisibleColumns();
  5566. for (FlyweightCell cell : attachedCells) {
  5567. StaticSection.StaticCell metadata = gridRow
  5568. .getCell(columns.get(cell.getColumn()));
  5569. /*
  5570. * If the cell contains widgets that are not currently attached
  5571. * then attach them now.
  5572. */
  5573. if (GridStaticCellType.WIDGET.equals(metadata.getType())) {
  5574. final Widget widget = metadata.getWidget();
  5575. if (widget != null && !widget.isAttached()) {
  5576. getGrid().attachWidget(metadata.getWidget(),
  5577. cell.getElement());
  5578. }
  5579. }
  5580. }
  5581. }
  5582. @Override
  5583. public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
  5584. if (section.getRowCount() > row.getRow()) {
  5585. StaticSection.StaticRow<?> gridRow = section
  5586. .getRow(row.getRow());
  5587. List<Column<?, T>> columns = getVisibleColumns();
  5588. for (FlyweightCell cell : cellsToDetach) {
  5589. StaticSection.StaticCell metadata = gridRow
  5590. .getCell(columns.get(cell.getColumn()));
  5591. if (GridStaticCellType.WIDGET.equals(metadata.getType())
  5592. && metadata.getWidget() != null
  5593. && metadata.getWidget().isAttached()) {
  5594. getGrid().detachWidget(metadata.getWidget());
  5595. }
  5596. }
  5597. }
  5598. }
  5599. protected Grid getGrid() {
  5600. return section.grid;
  5601. }
  5602. @Override
  5603. public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) {
  5604. }
  5605. };
  5606. /**
  5607. * Creates a new instance.
  5608. */
  5609. public Grid() {
  5610. initWidget(escalator);
  5611. getElement().setTabIndex(0);
  5612. cellFocusHandler = new CellFocusHandler();
  5613. setStylePrimaryName(STYLE_NAME);
  5614. setAriaRole("grid");
  5615. escalator.getHeader().setEscalatorUpdater(createHeaderUpdater());
  5616. escalator.getBody().setEscalatorUpdater(createBodyUpdater());
  5617. escalator.getFooter().setEscalatorUpdater(createFooterUpdater());
  5618. header.setGrid(this);
  5619. HeaderRow defaultRow = header.appendRow();
  5620. header.setDefaultRow(defaultRow);
  5621. footer.setGrid(this);
  5622. editor.setGrid(this);
  5623. setSelectionModel(new SelectionModel.NoSelectionModel<>());
  5624. escalator.getBody().setSpacerUpdater(gridSpacerUpdater);
  5625. escalator.addScrollHandler(event -> fireEvent(new ScrollEvent()));
  5626. escalator.addRowVisibilityChangeHandler(event -> {
  5627. if (dataSource != null && dataSource.size() != 0) {
  5628. dataSource.ensureAvailability(event.getFirstVisibleRow(),
  5629. event.getVisibleRowCount());
  5630. }
  5631. });
  5632. escalator.addVerticalScrollbarVisibilityChangeHandler(event -> {
  5633. if (!(currentDataAvailable.isEmpty()
  5634. && escalator.getBody().getRowCount() > 0)) {
  5635. recalculateColumnWidths();
  5636. } else {
  5637. recalculateColumnWidthsNeeded = true;
  5638. }
  5639. });
  5640. // Default action on SelectionEvents. Refresh the body so changed
  5641. // become visible.
  5642. addSelectionHandler(new SelectionHandler<T>() {
  5643. @Override
  5644. public void onSelect(SelectionEvent<T> event) {
  5645. refreshBody();
  5646. }
  5647. });
  5648. addSpacerIndexChangedHandler(new SpacerIndexChangedHandler() {
  5649. @Override
  5650. public void onSpacerIndexChanged(SpacerIndexChangedEvent event) {
  5651. // remove old index and add new index
  5652. visibleDetails.remove(event.getOldIndex());
  5653. visibleDetails.add(event.getNewIndex());
  5654. }
  5655. });
  5656. // Sink header events and key events
  5657. sinkEvents(getHeader().getConsumedEvents());
  5658. sinkEvents(Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.KEYUP,
  5659. BrowserEvents.KEYPRESS, BrowserEvents.DBLCLICK,
  5660. BrowserEvents.MOUSEDOWN, BrowserEvents.CLICK));
  5661. // Make ENTER and SHIFT+ENTER in the header perform sorting
  5662. addHeaderKeyUpHandler(event -> {
  5663. if (event.getNativeKeyCode() != KeyCodes.KEY_ENTER) {
  5664. return;
  5665. }
  5666. if (getHeader().getRow(event.getFocusedCell().getRowIndex())
  5667. .isDefault()) {
  5668. // Only sort for enter on the default header
  5669. sorter.sort(event.getFocusedCell().getColumn(),
  5670. event.isShiftKeyDown());
  5671. }
  5672. });
  5673. browserEventHandlers.addAll(Arrays.asList(
  5674. // Opening, closing and navigating in the editor
  5675. new EditorEventHandler(),
  5676. // Keyboard and click handlers, Escalator events
  5677. new SuperEventHandler(),
  5678. // Column reordering via header drag&drop
  5679. new HeaderCellDragStartHandler(),
  5680. // Column sorting via header click
  5681. new HeaderDefaultRowEventHandler(),
  5682. // Invoking event-aware renderers
  5683. new RendererEventHandler(),
  5684. // Moving cell focus by keyboard or mouse
  5685. new CellFocusEventHandler()));
  5686. }
  5687. @Override
  5688. public boolean isEnabled() {
  5689. return enabled;
  5690. }
  5691. @Override
  5692. public void setEnabled(boolean enabled) {
  5693. if (enabled == this.enabled) {
  5694. return;
  5695. }
  5696. this.enabled = enabled;
  5697. getElement().setTabIndex(enabled ? 0 : -1);
  5698. // Editor save and cancel buttons need to be disabled.
  5699. boolean editorOpen = editor.getState() != State.INACTIVE;
  5700. if (editorOpen) {
  5701. editor.setGridEnabled(enabled);
  5702. }
  5703. sidebar.setEnabled(enabled);
  5704. getEscalator().setScrollLocked(Direction.VERTICAL,
  5705. !enabled || editorOpen);
  5706. getEscalator().setScrollLocked(Direction.HORIZONTAL, !enabled);
  5707. getHeader().requestSectionRefresh();
  5708. fireEvent(new GridEnabledEvent(enabled));
  5709. }
  5710. /**
  5711. * Sets the column resize mode to use. The default mode is
  5712. * {@link ColumnResizeMode.ANIMATED}.
  5713. *
  5714. * @param mode
  5715. * a ColumnResizeMode value
  5716. *
  5717. * @since 7.7.5
  5718. */
  5719. public void setColumnResizeMode(ColumnResizeMode mode) {
  5720. columnResizeMode = mode;
  5721. }
  5722. /**
  5723. * Returns the current column resize mode. The default mode is
  5724. * {@link ColumnResizeMode.ANIMATED}.
  5725. *
  5726. * @return a ColumnResizeMode value
  5727. * @since 7.7.5
  5728. */
  5729. public ColumnResizeMode getColumnResizeMode() {
  5730. return columnResizeMode;
  5731. }
  5732. @Override
  5733. public void setStylePrimaryName(String style) {
  5734. super.setStylePrimaryName(style);
  5735. escalator.setStylePrimaryName(style);
  5736. editor.setStylePrimaryName(style);
  5737. sidebar.setStylePrimaryName(style + "-sidebar");
  5738. sidebar.addStyleName("v-contextmenu");
  5739. String rowStyle = getStylePrimaryName() + "-row";
  5740. rowHasDataStyleName = rowStyle + "-has-data";
  5741. rowSelectedStyleName = rowStyle + "-selected";
  5742. rowStripeStyleName = rowStyle + "-stripe";
  5743. cellFocusStyleName = getStylePrimaryName() + "-cell-focused";
  5744. rowFocusStyleName = getStylePrimaryName() + "-row-focused";
  5745. if (isAttached()) {
  5746. refreshHeader();
  5747. requestRefreshBody();
  5748. refreshFooter();
  5749. }
  5750. }
  5751. /**
  5752. * Adds the given role as 'role="$param"' to the <code>&lt;table&gt;</code>
  5753. * element of the grid.
  5754. *
  5755. * @param role
  5756. * the role param
  5757. * @since 8.2
  5758. */
  5759. protected void setAriaRole(String role) {
  5760. escalator.getTable().setAttribute("role", role);
  5761. }
  5762. /**
  5763. * Creates the escalator updater used to update the header rows in this
  5764. * grid. The updater is invoked when header rows or columns are added or
  5765. * removed, or the content of existing header cells is changed.
  5766. *
  5767. * @return the new header updater instance
  5768. *
  5769. * @see GridHeader
  5770. * @see Grid#getHeader()
  5771. */
  5772. protected EscalatorUpdater createHeaderUpdater() {
  5773. return new StaticSectionUpdater(header, escalator.getHeader());
  5774. }
  5775. /**
  5776. * Creates the escalator updater used to update the body rows in this grid.
  5777. * The updater is invoked when body rows or columns are added or removed,
  5778. * the content of body cells is changed, or the body is scrolled to expose
  5779. * previously hidden content.
  5780. *
  5781. * @return the new body updater instance
  5782. */
  5783. protected EscalatorUpdater createBodyUpdater() {
  5784. return new BodyUpdater();
  5785. }
  5786. /**
  5787. * Creates the escalator updater used to update the footer rows in this
  5788. * grid. The updater is invoked when header rows or columns are added or
  5789. * removed, or the content of existing header cells is changed.
  5790. *
  5791. * @return the new footer updater instance
  5792. *
  5793. * @see GridFooter
  5794. * @see #getFooter()
  5795. */
  5796. protected EscalatorUpdater createFooterUpdater() {
  5797. return new StaticSectionUpdater(footer, escalator.getFooter());
  5798. }
  5799. /**
  5800. * Refreshes header or footer rows on demand
  5801. *
  5802. * @param rows
  5803. * The row container
  5804. * @param firstRowIsVisible
  5805. * is the first row visible
  5806. * @param isHeader
  5807. * <code>true</code> if we refreshing the header, else assumed
  5808. * the footer
  5809. */
  5810. private void refreshRowContainer(RowContainer rows,
  5811. StaticSection<?> section) {
  5812. // Add or Remove rows on demand
  5813. int rowDiff = section.getVisibleRowCount() - rows.getRowCount();
  5814. if (rowDiff > 0) {
  5815. rows.insertRows(0, rowDiff);
  5816. } else if (rowDiff < 0) {
  5817. rows.removeRows(0, -rowDiff);
  5818. }
  5819. // Refresh all the rows
  5820. if (rows.getRowCount() > 0) {
  5821. rows.refreshRows(0, rows.getRowCount());
  5822. }
  5823. }
  5824. /**
  5825. * Focus a body cell by row and column index.
  5826. *
  5827. * @param rowIndex
  5828. * index of row to focus
  5829. * @param columnIndexDOM
  5830. * index (excluding hidden columns) of cell to focus
  5831. */
  5832. void focusCell(int rowIndex, int columnIndexDOM) {
  5833. final Range rowRange = Range.between(0, dataSource.size());
  5834. final Range columnRange = Range.between(0, getVisibleColumns().size());
  5835. assert rowRange.contains(
  5836. rowIndex) : "Illegal row index. Should be in range " + rowRange;
  5837. assert columnRange.contains(
  5838. columnIndexDOM) : "Illegal column index. Should be in range "
  5839. + columnRange;
  5840. if (rowRange.contains(rowIndex)
  5841. && columnRange.contains(columnIndexDOM)) {
  5842. cellFocusHandler.setCellFocus(rowIndex, columnIndexDOM,
  5843. escalator.getBody());
  5844. WidgetUtil.focus(getElement());
  5845. }
  5846. }
  5847. /**
  5848. * Refreshes all header rows
  5849. */
  5850. void refreshHeader() {
  5851. refreshRowContainer(escalator.getHeader(), header);
  5852. }
  5853. /**
  5854. * Refreshes all body rows
  5855. */
  5856. private void refreshBody() {
  5857. escalator.getBody().refreshRows(0, escalator.getBody().getRowCount());
  5858. }
  5859. /**
  5860. * Request delayed refresh of all body rows.
  5861. *
  5862. * @since 8.1
  5863. */
  5864. public void requestRefreshBody() {
  5865. if (!refreshBodyRequested) {
  5866. refreshBodyRequested = true;
  5867. Scheduler.get().scheduleFinally(() -> {
  5868. refreshBodyRequested = false;
  5869. refreshBody();
  5870. });
  5871. }
  5872. }
  5873. /**
  5874. * Refreshes all footer rows
  5875. */
  5876. void refreshFooter() {
  5877. refreshRowContainer(escalator.getFooter(), footer);
  5878. }
  5879. /**
  5880. * Adds columns as the last columns in the grid.
  5881. *
  5882. * @param columns
  5883. * the columns to add
  5884. */
  5885. public void addColumns(Column<?, T>... columns) {
  5886. if (columns.length == 0) {
  5887. // Nothing to add.
  5888. return;
  5889. }
  5890. addColumnsSkipSelectionColumnCheck(Arrays.asList(columns),
  5891. getVisibleColumns().size());
  5892. }
  5893. /**
  5894. * Adds a column as the last column in the grid.
  5895. *
  5896. * @param column
  5897. * the column to add
  5898. * @return given column
  5899. */
  5900. public <C extends Column<?, T>> C addColumn(C column) {
  5901. addColumn(column, getColumnCount());
  5902. return column;
  5903. }
  5904. /**
  5905. * Inserts a column into a specific position in the grid.
  5906. *
  5907. * @param index
  5908. * the index where the column should be inserted into
  5909. * @param column
  5910. * the column to add
  5911. * @return given column
  5912. *
  5913. * @throws IllegalStateException
  5914. * if Grid's current selection model renders a selection column,
  5915. * and {@code index} is 0.
  5916. */
  5917. public <C extends Column<?, T>> C addColumn(C column, int index) {
  5918. if (column == selectionColumn) {
  5919. throw new IllegalArgumentException(
  5920. "The selection column may not be added manually");
  5921. } else if (selectionColumn != null && index == 0) {
  5922. throw new IllegalStateException("A column cannot be inserted "
  5923. + "before the selection column");
  5924. }
  5925. addColumnsSkipSelectionColumnCheck(Collections.singleton(column),
  5926. index);
  5927. return column;
  5928. }
  5929. private void addColumnsSkipSelectionColumnCheck(
  5930. Collection<Column<?, T>> columnsToAdd, int startIndex) {
  5931. AtomicInteger index = new AtomicInteger(startIndex);
  5932. columnsToAdd.forEach(col -> {
  5933. // Register column with grid
  5934. columns.add(index.getAndIncrement(), col);
  5935. header.addColumn(col);
  5936. footer.addColumn(col);
  5937. // Register this grid instance with the column
  5938. col.setGrid(this);
  5939. });
  5940. escalator.getColumnConfiguration().insertColumns(startIndex,
  5941. (int) columnsToAdd.stream().filter(col -> !col.isHidden())
  5942. .count());
  5943. columnsToAdd.forEach(col -> {
  5944. // Reapply column width
  5945. col.reapplyWidth();
  5946. // Sink all renderer events
  5947. Set<String> events = new HashSet<>();
  5948. events.addAll(getConsumedEventsForRenderer(col.getRenderer()));
  5949. if (col.isHidable()) {
  5950. columnHider.updateColumnHidable(col);
  5951. }
  5952. sinkEvents(events);
  5953. });
  5954. }
  5955. private void sinkEvents(Collection<String> events) {
  5956. assert events != null;
  5957. int eventsToSink = 0;
  5958. for (String typeName : events) {
  5959. int typeInt = Event.getTypeInt(typeName);
  5960. if (typeInt < 0) {
  5961. // Type not recognized by typeInt
  5962. sinkBitlessEvent(typeName);
  5963. } else {
  5964. eventsToSink |= typeInt;
  5965. }
  5966. }
  5967. if (eventsToSink > 0) {
  5968. sinkEvents(eventsToSink);
  5969. }
  5970. }
  5971. private Renderer<?> findRenderer(FlyweightCell cell) {
  5972. Column<?, T> column = getVisibleColumn(cell.getColumn());
  5973. assert column != null : "Could not find column at index:"
  5974. + cell.getColumn();
  5975. return column.getRenderer();
  5976. }
  5977. /**
  5978. * Removes a column from the grid.
  5979. *
  5980. * @param column
  5981. * the column to remove
  5982. */
  5983. public void removeColumn(Column<?, T> column) {
  5984. if (column != null && column.equals(selectionColumn)) {
  5985. throw new IllegalArgumentException(
  5986. "The selection column may not be removed manually.");
  5987. }
  5988. removeColumnSkipSelectionColumnCheck(column);
  5989. }
  5990. private void removeColumnSkipSelectionColumnCheck(Column<?, T> column) {
  5991. int columnIndex = columns.indexOf(column);
  5992. // Remove from column configuration
  5993. int visibleColumnIndex = getVisibleColumns().indexOf(column);
  5994. if (visibleColumnIndex < 0) {
  5995. assert column.isHidden();
  5996. // Hidden columns are not included in Escalator
  5997. } else {
  5998. getEscalator().getColumnConfiguration()
  5999. .removeColumns(visibleColumnIndex, 1);
  6000. }
  6001. header.removeColumn(column);
  6002. footer.removeColumn(column);
  6003. // de-register column with grid
  6004. ((Column<?, T>) column).setGrid(null);
  6005. columns.remove(columnIndex);
  6006. if (column.isHidable()) {
  6007. columnHider.removeColumnHidingToggle(column);
  6008. }
  6009. updateFrozenColumns();
  6010. }
  6011. /**
  6012. * Returns the amount of columns in the grid.
  6013. * <p>
  6014. * <em>NOTE:</em> this includes the hidden columns in the count.
  6015. *
  6016. * @return The number of columns in the grid
  6017. */
  6018. public int getColumnCount() {
  6019. return columns.size();
  6020. }
  6021. /**
  6022. * Returns a list columns in the grid, including hidden columns.
  6023. * <p>
  6024. * For currently visible columns, use {@link #getVisibleColumns()}.
  6025. *
  6026. * @return A unmodifiable list of the columns in the grid
  6027. */
  6028. public List<Column<?, T>> getColumns() {
  6029. return Collections.unmodifiableList(new ArrayList<>(columns));
  6030. }
  6031. /**
  6032. * Returns a list of the currently visible columns in the grid.
  6033. * <p>
  6034. * No {@link Column#isHidden() hidden} columns included.
  6035. *
  6036. * @since 7.5.0
  6037. * @return A unmodifiable list of the currently visible columns in the grid
  6038. */
  6039. public List<Column<?, T>> getVisibleColumns() {
  6040. List<Column<?, T>> visible = new ArrayList<>();
  6041. for (Column<?, T> c : columns) {
  6042. if (!c.isHidden()) {
  6043. visible.add(c);
  6044. }
  6045. }
  6046. return Collections.unmodifiableList(visible);
  6047. }
  6048. /**
  6049. * Returns a column by its index in the grid.
  6050. * <p>
  6051. * <em>NOTE:</em> The indexing includes hidden columns.
  6052. *
  6053. * @param index
  6054. * the index of the column
  6055. * @return The column in the given index
  6056. * @throws IllegalArgumentException
  6057. * if the column index does not exist in the grid
  6058. */
  6059. public Column<?, T> getColumn(int index) throws IllegalArgumentException {
  6060. if (index < 0 || index >= columns.size()) {
  6061. throw new IllegalStateException("Column not found.");
  6062. }
  6063. return columns.get(index);
  6064. }
  6065. private Column<?, T> getVisibleColumn(int index)
  6066. throws IllegalArgumentException {
  6067. List<Column<?, T>> visibleColumns = getVisibleColumns();
  6068. if (index < 0 || index >= visibleColumns.size()) {
  6069. throw new IllegalStateException("Column not found.");
  6070. }
  6071. return visibleColumns.get(index);
  6072. }
  6073. /**
  6074. * Returns the header section of this grid. The default header contains a
  6075. * single row displaying the column captions.
  6076. *
  6077. * @return the header
  6078. */
  6079. protected Header getHeader() {
  6080. return header;
  6081. }
  6082. /**
  6083. * Gets the header row at given index.
  6084. *
  6085. * @param rowIndex
  6086. * 0 based index for row. Counted from top to bottom
  6087. * @return header row at given index
  6088. * @throws IllegalArgumentException
  6089. * if no row exists at given index
  6090. */
  6091. public HeaderRow getHeaderRow(int rowIndex) {
  6092. return header.getRow(rowIndex);
  6093. }
  6094. /**
  6095. * Inserts a new row at the given position to the header section. Shifts the
  6096. * row currently at that position and any subsequent rows down (adds one to
  6097. * their indices).
  6098. *
  6099. * @param index
  6100. * the position at which to insert the row
  6101. * @return the new row
  6102. *
  6103. * @throws IllegalArgumentException
  6104. * if the index is less than 0 or greater than row count
  6105. * @see #appendHeaderRow()
  6106. * @see #prependHeaderRow()
  6107. * @see #removeHeaderRow(HeaderRow)
  6108. * @see #removeHeaderRow(int)
  6109. */
  6110. public HeaderRow addHeaderRowAt(int index) {
  6111. return header.addRowAt(index);
  6112. }
  6113. /**
  6114. * Adds a new row at the bottom of the header section.
  6115. *
  6116. * @return the new row
  6117. * @see #prependHeaderRow()
  6118. * @see #addHeaderRowAt(int)
  6119. * @see #removeHeaderRow(HeaderRow)
  6120. * @see #removeHeaderRow(int)
  6121. */
  6122. public HeaderRow appendHeaderRow() {
  6123. return header.appendRow();
  6124. }
  6125. /**
  6126. * Returns the current default row of the header section. The default row is
  6127. * a special header row providing a user interface for sorting columns.
  6128. * Setting a header caption for column updates cells in the default header.
  6129. *
  6130. * @return the default row or null if no default row set
  6131. */
  6132. public HeaderRow getDefaultHeaderRow() {
  6133. return header.getDefaultRow();
  6134. }
  6135. /**
  6136. * Gets the row count for the header section.
  6137. *
  6138. * @return row count
  6139. */
  6140. public int getHeaderRowCount() {
  6141. return header.getRowCount();
  6142. }
  6143. /**
  6144. * Adds a new row at the top of the header section.
  6145. *
  6146. * @return the new row
  6147. * @see #appendHeaderRow()
  6148. * @see #addHeaderRowAt(int)
  6149. * @see #removeHeaderRow(HeaderRow)
  6150. * @see #removeHeaderRow(int)
  6151. */
  6152. public HeaderRow prependHeaderRow() {
  6153. return header.prependRow();
  6154. }
  6155. /**
  6156. * Removes the given row from the header section.
  6157. *
  6158. * @param row
  6159. * the row to be removed
  6160. *
  6161. * @throws IllegalArgumentException
  6162. * if the row does not exist in this section
  6163. * @see #removeHeaderRow(int)
  6164. * @see #addHeaderRowAt(int)
  6165. * @see #appendHeaderRow()
  6166. * @see #prependHeaderRow()
  6167. */
  6168. public void removeHeaderRow(HeaderRow row) {
  6169. header.removeRow(row);
  6170. }
  6171. /**
  6172. * Removes the row at the given position from the header section.
  6173. *
  6174. * @param rowIndex
  6175. * the position of the row
  6176. *
  6177. * @throws IllegalArgumentException
  6178. * if no row exists at given index
  6179. * @see #removeHeaderRow(HeaderRow)
  6180. * @see #addHeaderRowAt(int)
  6181. * @see #appendHeaderRow()
  6182. * @see #prependHeaderRow()
  6183. */
  6184. public void removeHeaderRow(int rowIndex) {
  6185. header.removeRow(rowIndex);
  6186. }
  6187. /**
  6188. * Sets the default row of the header. The default row is a special header
  6189. * row providing a user interface for sorting columns.
  6190. * <p>
  6191. * Note: Setting the default header row will reset all cell contents to
  6192. * Column defaults.
  6193. *
  6194. * @param row
  6195. * the new default row, or null for no default row
  6196. *
  6197. * @throws IllegalArgumentException
  6198. * header does not contain the row
  6199. */
  6200. public void setDefaultHeaderRow(HeaderRow row) {
  6201. header.setDefaultRow(row);
  6202. }
  6203. /**
  6204. * Sets the visibility of the header section.
  6205. *
  6206. * @param visible
  6207. * true to show header section, false to hide
  6208. */
  6209. public void setHeaderVisible(boolean visible) {
  6210. header.setVisible(visible);
  6211. }
  6212. /**
  6213. * Returns the visibility of the header section.
  6214. *
  6215. * @return true if visible, false otherwise.
  6216. */
  6217. public boolean isHeaderVisible() {
  6218. return header.isVisible();
  6219. }
  6220. /* Grid Footers */
  6221. /**
  6222. * Returns the footer section of this grid. The default footer is empty.
  6223. *
  6224. * @return the footer
  6225. */
  6226. protected Footer getFooter() {
  6227. return footer;
  6228. }
  6229. /**
  6230. * Gets the footer row at given index.
  6231. *
  6232. * @param rowIndex
  6233. * 0 based index for row. Counted from top to bottom
  6234. * @return footer row at given index
  6235. * @throws IllegalArgumentException
  6236. * if no row exists at given index
  6237. */
  6238. public FooterRow getFooterRow(int rowIndex) {
  6239. return footer.getRow(rowIndex);
  6240. }
  6241. /**
  6242. * Inserts a new row at the given position to the footer section. Shifts the
  6243. * row currently at that position and any subsequent rows down (adds one to
  6244. * their indices).
  6245. *
  6246. * @param index
  6247. * the position at which to insert the row
  6248. * @return the new row
  6249. *
  6250. * @throws IllegalArgumentException
  6251. * if the index is less than 0 or greater than row count
  6252. * @see #appendFooterRow()
  6253. * @see #prependFooterRow()
  6254. * @see #removeFooterRow(FooterRow)
  6255. * @see #removeFooterRow(int)
  6256. */
  6257. public FooterRow addFooterRowAt(int index) {
  6258. return footer.addRowAt(index);
  6259. }
  6260. /**
  6261. * Adds a new row at the bottom of the footer section.
  6262. *
  6263. * @return the new row
  6264. * @see #prependFooterRow()
  6265. * @see #addFooterRowAt(int)
  6266. * @see #removeFooterRow(FooterRow)
  6267. * @see #removeFooterRow(int)
  6268. */
  6269. public FooterRow appendFooterRow() {
  6270. return footer.appendRow();
  6271. }
  6272. /**
  6273. * Gets the row count for the footer.
  6274. *
  6275. * @return row count
  6276. */
  6277. public int getFooterRowCount() {
  6278. return footer.getRowCount();
  6279. }
  6280. /**
  6281. * Adds a new row at the top of the footer section.
  6282. *
  6283. * @return the new row
  6284. * @see #appendFooterRow()
  6285. * @see #addFooterRowAt(int)
  6286. * @see #removeFooterRow(FooterRow)
  6287. * @see #removeFooterRow(int)
  6288. */
  6289. public FooterRow prependFooterRow() {
  6290. return footer.prependRow();
  6291. }
  6292. /**
  6293. * Removes the given row from the footer section.
  6294. *
  6295. * @param row
  6296. * the row to be removed
  6297. *
  6298. * @throws IllegalArgumentException
  6299. * if the row does not exist in this section
  6300. * @see #removeFooterRow(int)
  6301. * @see #addFooterRowAt(int)
  6302. * @see #appendFooterRow()
  6303. * @see #prependFooterRow()
  6304. */
  6305. public void removeFooterRow(FooterRow row) {
  6306. footer.removeRow(row);
  6307. }
  6308. /**
  6309. * Removes the row at the given position from the footer section.
  6310. *
  6311. * @param rowIndex
  6312. * the position of the row
  6313. *
  6314. * @throws IllegalArgumentException
  6315. * if no row exists at given index
  6316. * @see #removeFooterRow(FooterRow)
  6317. * @see #addFooterRowAt(int)
  6318. * @see #appendFooterRow()
  6319. * @see #prependFooterRow()
  6320. */
  6321. public void removeFooterRow(int rowIndex) {
  6322. footer.removeRow(rowIndex);
  6323. }
  6324. /**
  6325. * Sets the visibility of the footer section.
  6326. *
  6327. * @param visible
  6328. * true to show footer section, false to hide
  6329. */
  6330. public void setFooterVisible(boolean visible) {
  6331. footer.setVisible(visible);
  6332. }
  6333. /**
  6334. * Returns the visibility of the footer section.
  6335. *
  6336. * @return true if visible, false otherwise.
  6337. */
  6338. public boolean isFooterVisible() {
  6339. return footer.isVisible();
  6340. }
  6341. public Editor<T> getEditor() {
  6342. return editor;
  6343. }
  6344. /**
  6345. * Gets the {@link Escalator} used by this Grid instance.
  6346. *
  6347. * @return the escalator instance, never <code>null</code>
  6348. */
  6349. public Escalator getEscalator() {
  6350. return escalator;
  6351. }
  6352. /**
  6353. * {@inheritDoc}
  6354. * <p>
  6355. * <em>Note:</em> This method will change the widget's size in the browser
  6356. * only if {@link #getHeightMode()} returns {@link HeightMode#CSS}.
  6357. *
  6358. * @see #setHeightMode(HeightMode)
  6359. */
  6360. @Override
  6361. public void setHeight(String height) {
  6362. escalator.setHeight(height);
  6363. }
  6364. @Override
  6365. public void setWidth(String width) {
  6366. escalator.setWidth(width);
  6367. }
  6368. /**
  6369. * Sets the data source used by this grid.
  6370. *
  6371. * @param dataSource
  6372. * the data source to use, not null
  6373. * @throws IllegalArgumentException
  6374. * if <code>dataSource</code> is <code>null</code>
  6375. */
  6376. public void setDataSource(final DataSource<T> dataSource)
  6377. throws IllegalArgumentException {
  6378. if (dataSource == null) {
  6379. throw new IllegalArgumentException("dataSource can't be null.");
  6380. }
  6381. if (changeHandler != null) {
  6382. changeHandler.remove();
  6383. changeHandler = null;
  6384. }
  6385. this.dataSource = dataSource;
  6386. changeHandler = dataSource
  6387. .addDataChangeHandler(new DataChangeHandler() {
  6388. @Override
  6389. public void dataUpdated(int firstIndex, int numberOfItems) {
  6390. escalator.getBody().refreshRows(firstIndex,
  6391. numberOfItems);
  6392. }
  6393. @Override
  6394. public void dataRemoved(int firstIndex, int numberOfItems) {
  6395. for (int i = 0; i < numberOfItems; ++i) {
  6396. visibleDetails.remove(firstIndex + i);
  6397. }
  6398. escalator.getBody().removeRows(firstIndex,
  6399. numberOfItems);
  6400. Range removed = Range.withLength(firstIndex,
  6401. numberOfItems);
  6402. cellFocusHandler.rowsRemovedFromBody(removed);
  6403. }
  6404. @Override
  6405. public void dataAdded(int firstIndex, int numberOfItems) {
  6406. escalator.getBody().insertRows(firstIndex,
  6407. numberOfItems);
  6408. Range added = Range.withLength(firstIndex,
  6409. numberOfItems);
  6410. cellFocusHandler.rowsAddedToBody(added);
  6411. }
  6412. @Override
  6413. public void dataAvailable(int firstIndex,
  6414. int numberOfItems) {
  6415. currentDataAvailable = Range.withLength(firstIndex,
  6416. numberOfItems);
  6417. if (recalculateColumnWidthsNeeded) {
  6418. // Ensure that cache has actually been populated or
  6419. // all rows removed, otherwise wait for next call.
  6420. if (numberOfItems > 0
  6421. || getDataSource().size() == 0) {
  6422. recalculateColumnWidths();
  6423. recalculateColumnWidthsNeeded = false;
  6424. }
  6425. }
  6426. fireEvent(new DataAvailableEvent(currentDataAvailable));
  6427. }
  6428. @Override
  6429. public void resetDataAndSize(int newSize) {
  6430. // It might take a while for new data to arrive,
  6431. // clear the record of cached rows.
  6432. currentDataAvailable = Range.emptyRange();
  6433. RowContainer body = escalator.getBody();
  6434. int oldSize = body.getRowCount();
  6435. // Hide all details.
  6436. Set<Integer> oldDetails = new HashSet<>(visibleDetails);
  6437. for (int i : oldDetails) {
  6438. setDetailsVisible(i, false);
  6439. }
  6440. if (newSize > oldSize) {
  6441. if (oldSize == 0 && !isHeaderVisible()) {
  6442. // Fixes framework/issues/11607
  6443. // Need to recalculate column widths when the
  6444. // first row is added to a non-header grid,
  6445. // otherwise the checkbox will be aligned in a
  6446. // wrong place. Wait until the cache has been
  6447. // populated before making the call.
  6448. recalculateColumnWidthsNeeded = true;
  6449. }
  6450. body.insertRows(oldSize, newSize - oldSize);
  6451. cellFocusHandler.rowsAddedToBody(Range
  6452. .withLength(oldSize, newSize - oldSize));
  6453. } else if (newSize < oldSize) {
  6454. body.removeRows(newSize, oldSize - newSize);
  6455. cellFocusHandler.rowsRemovedFromBody(Range
  6456. .withLength(newSize, oldSize - newSize));
  6457. }
  6458. if (newSize > 0) {
  6459. Range visibleRowRange = escalator
  6460. .getVisibleRowRange();
  6461. dataSource.ensureAvailability(
  6462. visibleRowRange.getStart(),
  6463. visibleRowRange.length());
  6464. } else {
  6465. // We won't expect any data more data updates, so
  6466. // just make the bookkeeping happy.
  6467. dataAvailable(0, 0);
  6468. }
  6469. assert body.getRowCount() == newSize;
  6470. }
  6471. });
  6472. int previousRowCount = escalator.getBody().getRowCount();
  6473. if (previousRowCount != 0) {
  6474. escalator.getBody().removeRows(0, previousRowCount);
  6475. }
  6476. setEscalatorSizeFromDataSource();
  6477. }
  6478. private void setEscalatorSizeFromDataSource() {
  6479. assert escalator.getBody().getRowCount() == 0;
  6480. int size = dataSource.size();
  6481. if (size > 0) {
  6482. escalator.getBody().insertRows(0, size);
  6483. }
  6484. }
  6485. /**
  6486. * Gets the {@Link DataSource} for this Grid.
  6487. *
  6488. * @return the data source used by this grid
  6489. */
  6490. public DataSource<T> getDataSource() {
  6491. return dataSource;
  6492. }
  6493. /**
  6494. * Sets the number of frozen columns in this grid. Setting the count to 0
  6495. * means that no data columns will be frozen, but the built-in selection
  6496. * checkbox column will still be frozen if it's in use. Setting the count to
  6497. * -1 will also disable the selection column.
  6498. * <p>
  6499. * The default value is 0.
  6500. *
  6501. * @param numberOfColumns
  6502. * the number of columns that should be frozen
  6503. *
  6504. * @throws IllegalArgumentException
  6505. * if the column count is < -1 or > the number of visible
  6506. * columns
  6507. */
  6508. public void setFrozenColumnCount(int numberOfColumns) {
  6509. if (numberOfColumns < -1 || numberOfColumns > getColumnCount()) {
  6510. throw new IllegalArgumentException(
  6511. "count must be between -1 and the current number of columns ("
  6512. + getColumnCount() + ")");
  6513. }
  6514. frozenColumnCount = numberOfColumns;
  6515. updateFrozenColumns();
  6516. }
  6517. private void updateFrozenColumns() {
  6518. int visibleFrozenColumnCount = getVisibleFrozenColumnCount();
  6519. ColumnConfiguration columnConfiguration = escalator
  6520. .getColumnConfiguration();
  6521. if (columnConfiguration.getColumnCount() < visibleFrozenColumnCount) {
  6522. // new columns may not have got added yet, delay and check the
  6523. // correct count again
  6524. Scheduler.get().scheduleFinally(() -> columnConfiguration
  6525. .setFrozenColumnCount(getVisibleFrozenColumnCount()));
  6526. } else {
  6527. columnConfiguration.setFrozenColumnCount(visibleFrozenColumnCount);
  6528. }
  6529. }
  6530. private int getVisibleFrozenColumnCount() {
  6531. int numberOfColumns = getFrozenColumnCount();
  6532. // for the escalator the hidden columns are not in the frozen column
  6533. // count, but for grid they are. thus need to convert the index
  6534. int limit = getFrozenColumnCount();
  6535. if (getSelectionColumn().isPresent()) {
  6536. // If the grid is in MultiSelect mode, getColumn(0) in the following
  6537. // for loop returns the selection column. Accordingly, verifying
  6538. // which frozen columns are visible if the selection column is
  6539. // present should take this fact into account.
  6540. limit++;
  6541. }
  6542. for (int i = 0; i < limit; i++) {
  6543. if (i >= getColumnCount() || getColumn(i).isHidden()) {
  6544. numberOfColumns--;
  6545. }
  6546. }
  6547. if (numberOfColumns == -1) {
  6548. numberOfColumns = 0;
  6549. } else if (selectionColumn != null) {
  6550. numberOfColumns++;
  6551. }
  6552. return numberOfColumns;
  6553. }
  6554. /**
  6555. * Gets the number of frozen columns in this grid. 0 means that no data
  6556. * columns will be frozen, but the built-in selection checkbox column will
  6557. * still be frozen if it's in use. -1 means that not even the selection
  6558. * column is frozen.
  6559. * <p>
  6560. * <em>NOTE:</em> This includes {@link Column#isHidden() hidden columns} in
  6561. * the count.
  6562. *
  6563. * @return the number of frozen columns
  6564. */
  6565. public int getFrozenColumnCount() {
  6566. return frozenColumnCount;
  6567. }
  6568. public HandlerRegistration addRowVisibilityChangeHandler(
  6569. RowVisibilityChangeHandler handler) {
  6570. /*
  6571. * Reusing Escalator's RowVisibilityChangeHandler, since a scroll
  6572. * concept is too abstract. e.g. the event needs to be re-sent when the
  6573. * widget is resized.
  6574. */
  6575. return escalator.addRowVisibilityChangeHandler(handler);
  6576. }
  6577. /**
  6578. * Scrolls to a certain row, using {@link ScrollDestination#ANY}.
  6579. * <p>
  6580. * If the details for that row are visible, those will be taken into account
  6581. * as well.
  6582. *
  6583. * @param rowIndex
  6584. * zero-based index of the row to scroll to.
  6585. * @throws IllegalArgumentException
  6586. * if rowIndex is below zero, or above the maximum value
  6587. * supported by the data source.
  6588. */
  6589. public void scrollToRow(int rowIndex) throws IllegalArgumentException {
  6590. scrollToRow(rowIndex, ScrollDestination.ANY,
  6591. GridConstants.DEFAULT_PADDING);
  6592. }
  6593. /**
  6594. * Scrolls to a certain row, using user-specified scroll destination.
  6595. * <p>
  6596. * If the details for that row are visible, those will be taken into account
  6597. * as well.
  6598. *
  6599. * @param rowIndex
  6600. * zero-based index of the row to scroll to.
  6601. * @param destination
  6602. * desired destination placement of scrolled-to-row. See
  6603. * {@link ScrollDestination} for more information.
  6604. * @throws IllegalArgumentException
  6605. * if rowIndex is below zero, or above the maximum value
  6606. * supported by the data source.
  6607. */
  6608. public void scrollToRow(int rowIndex, ScrollDestination destination)
  6609. throws IllegalArgumentException {
  6610. scrollToRow(rowIndex, destination,
  6611. destination == ScrollDestination.MIDDLE ? 0
  6612. : GridConstants.DEFAULT_PADDING);
  6613. }
  6614. /**
  6615. * Helper method for making sure desired row is visible and it is properly
  6616. * rendered.
  6617. *
  6618. * @param rowIndex
  6619. * the row to look for
  6620. * @param destination
  6621. * the desired scroll destination
  6622. * @param callback
  6623. * the callback command to execute when row is available
  6624. * @since 8.4
  6625. */
  6626. public void scrollToRow(int rowIndex, ScrollDestination destination,
  6627. Runnable callback) {
  6628. waitUntilVisible(rowIndex, destination, callback);
  6629. }
  6630. /**
  6631. * Helper method for making sure desired row is visible and it is properly
  6632. * rendered.
  6633. *
  6634. * @param rowIndex
  6635. * the row to look for
  6636. * @param whenRendered
  6637. * the callback command to execute when row is available
  6638. * @since 8.4
  6639. */
  6640. public void scrollToRow(int rowIndex, Runnable whenRendered) {
  6641. scrollToRow(rowIndex, ScrollDestination.ANY, whenRendered);
  6642. }
  6643. /**
  6644. * Scrolls to a certain row using only user-specified parameters.
  6645. * <p>
  6646. * If the details for that row are visible, those will be taken into account
  6647. * as well.
  6648. *
  6649. * @param rowIndex
  6650. * zero-based index of the row to scroll to.
  6651. * @param destination
  6652. * desired destination placement of scrolled-to-row. See
  6653. * {@link ScrollDestination} for more information.
  6654. * @param paddingPx
  6655. * number of pixels to overscroll. Behavior depends on
  6656. * destination.
  6657. * @throws IllegalArgumentException
  6658. * if {@code destination} is {@link ScrollDestination#MIDDLE}
  6659. * and padding is nonzero, because having a padding on a
  6660. * centered row is undefined behavior, or if rowIndex is below
  6661. * zero or above the row count of the data source.
  6662. */
  6663. private void scrollToRow(int rowIndex, ScrollDestination destination,
  6664. int paddingPx) throws IllegalArgumentException {
  6665. int maxsize = escalator.getBody().getRowCount() - 1;
  6666. if (rowIndex < 0) {
  6667. throw new IllegalArgumentException(
  6668. "Row index (" + rowIndex + ") is below zero!");
  6669. }
  6670. if (rowIndex > maxsize) {
  6671. throw new IllegalArgumentException("Row index (" + rowIndex
  6672. + ") is above maximum (" + maxsize + ")!");
  6673. }
  6674. escalator.scrollToRowAndSpacer(rowIndex, destination, paddingPx);
  6675. }
  6676. /**
  6677. * Helper method for scrolling and making sure row is visible.
  6678. *
  6679. * @param rowIndex
  6680. * the row index to make visible
  6681. * @param destination
  6682. * the desired scroll destination
  6683. * @param whenVisible
  6684. * the callback method to call when row is visible
  6685. */
  6686. private void waitUntilVisible(int rowIndex, ScrollDestination destination,
  6687. Runnable whenVisible) {
  6688. boolean waitForCache = false;
  6689. if (getDataSource().getRow(rowIndex) == null) {
  6690. // not yet in cache, wait for this to change
  6691. waitForCache = true;
  6692. Reference<Registration> registration = new Reference<>();
  6693. registration.set(getDataSource()
  6694. .addDataChangeHandler(new DataChangeHandler() {
  6695. @Override
  6696. public void resetDataAndSize(int estimatedNewDataSize) {
  6697. // data set changed, cancel the operation
  6698. registration.get().remove();
  6699. }
  6700. @Override
  6701. public void dataUpdated(int firstRowIndex,
  6702. int numberOfRows) {
  6703. // NOP
  6704. }
  6705. @Override
  6706. public void dataRemoved(int firstRowIndex,
  6707. int numberOfRows) {
  6708. // data set changed, cancel the operation
  6709. registration.get().remove();
  6710. }
  6711. @Override
  6712. public void dataAvailable(int firstRowIndex,
  6713. int numberOfRows) {
  6714. // if new available range contains the row,
  6715. // try again
  6716. if (Range.withLength(firstRowIndex, numberOfRows)
  6717. .contains(rowIndex)) {
  6718. registration.get().remove();
  6719. waitUntilVisible(rowIndex, destination,
  6720. whenVisible);
  6721. }
  6722. }
  6723. @Override
  6724. public void dataAdded(int firstRowIndex,
  6725. int numberOfRows) {
  6726. // data set changed, cancel the operation
  6727. registration.get().remove();
  6728. }
  6729. }));
  6730. }
  6731. scrollToRow(rowIndex, destination);
  6732. if (!waitForCache) {
  6733. // all necessary adjustments done, time to perform
  6734. whenVisible.run();
  6735. }
  6736. }
  6737. /**
  6738. * Scrolls to the beginning of the very first row.
  6739. */
  6740. public void scrollToStart() {
  6741. if (getEscalator().getBody().getRowCount() > 0) {
  6742. scrollToRow(0, ScrollDestination.START);
  6743. }
  6744. }
  6745. /**
  6746. * Scrolls to the end of the very last row.
  6747. */
  6748. public void scrollToEnd() {
  6749. if (getEscalator().getBody().getRowCount() > 0) {
  6750. scrollToRow(escalator.getBody().getRowCount() - 1,
  6751. ScrollDestination.END);
  6752. }
  6753. }
  6754. /**
  6755. * Sets the vertical scroll offset.
  6756. *
  6757. * @param px
  6758. * the number of pixels this grid should be scrolled down
  6759. */
  6760. public void setScrollTop(double px) {
  6761. escalator.setScrollTop(px);
  6762. }
  6763. /**
  6764. * Gets the vertical scroll offset.
  6765. *
  6766. * @return the number of pixels this grid is scrolled down
  6767. */
  6768. public double getScrollTop() {
  6769. return escalator.getScrollTop();
  6770. }
  6771. /**
  6772. * Sets the horizontal scroll offset.
  6773. *
  6774. * @since 7.5.0
  6775. * @param px
  6776. * the number of pixels this grid should be scrolled right
  6777. */
  6778. public void setScrollLeft(double px) {
  6779. escalator.setScrollLeft(px);
  6780. }
  6781. /**
  6782. * Gets the horizontal scroll offset.
  6783. *
  6784. * @return the number of pixels this grid is scrolled to the right
  6785. */
  6786. public double getScrollLeft() {
  6787. return escalator.getScrollLeft();
  6788. }
  6789. /**
  6790. * Returns the height of the scrollable area in pixels.
  6791. *
  6792. * @since 7.5.0
  6793. * @return the height of the scrollable area in pixels
  6794. */
  6795. public double getScrollHeight() {
  6796. return escalator.getScrollHeight();
  6797. }
  6798. /**
  6799. * Returns the width of the scrollable area in pixels.
  6800. *
  6801. * @since 7.5.0
  6802. * @return the width of the scrollable area in pixels.
  6803. */
  6804. public double getScrollWidth() {
  6805. return escalator.getScrollWidth();
  6806. }
  6807. private static final Logger getLogger() {
  6808. return Logger.getLogger(Grid.class.getName());
  6809. }
  6810. /**
  6811. * Sets the number of rows that should be visible in Grid's body, while
  6812. * {@link #getHeightMode()} is {@link HeightMode#ROW}.
  6813. * <p>
  6814. * If Grid is currently not in {@link HeightMode#ROW}, the given value is
  6815. * remembered, and applied once the mode is applied.
  6816. *
  6817. * @param rows
  6818. * The height in terms of number of rows displayed in Grid's
  6819. * body. If Grid doesn't contain enough rows, white space is
  6820. * displayed instead.
  6821. * @throws IllegalArgumentException
  6822. * if {@code rows} is zero or less
  6823. * @throws IllegalArgumentException
  6824. * if {@code rows} is {@link Double#isInifinite(double)
  6825. * infinite}
  6826. * @throws IllegalArgumentException
  6827. * if {@code rows} is {@link Double#isNaN(double) NaN}
  6828. *
  6829. * @see #setHeightMode(HeightMode)
  6830. */
  6831. public void setHeightByRows(double rows) throws IllegalArgumentException {
  6832. escalator.setHeightByRows(rows);
  6833. }
  6834. /**
  6835. * Gets the amount of rows in Grid's body that are shown, while
  6836. * {@link #getHeightMode()} is {@link HeightMode#ROW}.
  6837. * <p>
  6838. * By default, it is {@value Escalator#DEFAULT_HEIGHT_BY_ROWS}.
  6839. *
  6840. * @return the amount of rows that should be shown in Grid's body, while in
  6841. * {@link HeightMode#ROW}.
  6842. * @see #setHeightByRows(double)
  6843. */
  6844. public double getHeightByRows() {
  6845. return escalator.getHeightByRows();
  6846. }
  6847. /**
  6848. * Defines the mode in which the Grid widget's height is calculated.
  6849. * <p>
  6850. * If {@link HeightMode#CSS} is given, Grid will respect the values given
  6851. * via {@link #setHeight(String)}, and behave as a traditional Widget.
  6852. * <p>
  6853. * If {@link HeightMode#ROW} is given, Grid will make sure that the body
  6854. * will display as many rows as {@link #getHeightByRows()} defines.
  6855. * <em>Note:</em> If headers/footers are inserted or removed, the widget
  6856. * will resize itself to still display the required amount of rows in its
  6857. * body. It also takes the horizontal scrollbar into account.
  6858. *
  6859. * @param heightMode
  6860. * the mode in to which Grid should be set
  6861. */
  6862. public void setHeightMode(HeightMode heightMode) {
  6863. /*
  6864. * This method is a workaround for the fact that Vaadin re-applies
  6865. * widget dimensions (height/width) on each state change event. The
  6866. * original design was to have setHeight an setHeightByRow be equals,
  6867. * and whichever was called the latest was considered in effect.
  6868. *
  6869. * But, because of Vaadin always calling setHeight on the widget, this
  6870. * approach doesn't work.
  6871. */
  6872. escalator.setHeightMode(heightMode);
  6873. }
  6874. /**
  6875. * Returns the current {@link HeightMode} the Grid is in.
  6876. * <p>
  6877. * Defaults to {@link HeightMode#CSS}.
  6878. *
  6879. * @return the current HeightMode
  6880. */
  6881. public HeightMode getHeightMode() {
  6882. return escalator.getHeightMode();
  6883. }
  6884. private Set<String> getConsumedEventsForRenderer(Renderer<?> renderer) {
  6885. Set<String> events = new HashSet<>();
  6886. if (renderer instanceof ComplexRenderer) {
  6887. Collection<String> consumedEvents = ((ComplexRenderer<?>) renderer)
  6888. .getConsumedEvents();
  6889. if (consumedEvents != null) {
  6890. events.addAll(consumedEvents);
  6891. }
  6892. }
  6893. return events;
  6894. }
  6895. @Override
  6896. public void onBrowserEvent(Event event) {
  6897. if (!isEnabled()) {
  6898. return;
  6899. }
  6900. String eventType = event.getType();
  6901. if (eventType.equals(BrowserEvents.FOCUS)
  6902. || eventType.equals(BrowserEvents.BLUR)) {
  6903. super.onBrowserEvent(event);
  6904. return;
  6905. }
  6906. EventTarget target = event.getEventTarget();
  6907. if (!Element.is(target) || isOrContainsInSpacer(Element.as(target))) {
  6908. return;
  6909. }
  6910. Element element = Element.as(target);
  6911. RowContainer container = escalator.findRowContainer(element);
  6912. Cell cell;
  6913. if (container == null) {
  6914. if (eventType.equals(BrowserEvents.KEYDOWN)
  6915. || eventType.equals(BrowserEvents.KEYUP)
  6916. || eventType.equals(BrowserEvents.KEYPRESS)) {
  6917. cell = cellFocusHandler.getFocusedCell();
  6918. container = cellFocusHandler.containerWithFocus;
  6919. } else {
  6920. // Click might be in an editor cell, should still map.
  6921. if (editor.editorOverlay != null
  6922. && editor.editorOverlay.isOrHasChild(element)) {
  6923. container = escalator.getBody();
  6924. int rowIndex = editor.getRow();
  6925. int colIndex = editor.getElementColumn(element);
  6926. if (colIndex < 0) {
  6927. // Click in editor, but not for any column.
  6928. return;
  6929. }
  6930. try {
  6931. TableCellElement cellElement = container
  6932. .getRowElement(rowIndex).getCells()
  6933. .getItem(colIndex);
  6934. cell = new Cell(rowIndex, colIndex, cellElement);
  6935. } catch (IllegalStateException exception) {
  6936. // IllegalStateException may occur if user has scrolled Grid so
  6937. // that Escalator has updated, and row under Editor is no longer
  6938. // there
  6939. return;
  6940. }
  6941. } else {
  6942. if (escalator.getElement().isOrHasChild(element)) {
  6943. eventCell.set(new Cell(-1, -1, null), Section.BODY);
  6944. // Fire native events.
  6945. super.onBrowserEvent(event);
  6946. }
  6947. return;
  6948. }
  6949. }
  6950. } else {
  6951. cell = container.getCell(element);
  6952. if (eventType.equals(BrowserEvents.MOUSEDOWN)) {
  6953. cellOnPrevMouseDown = cell;
  6954. } else if (cell == null && eventType.equals(BrowserEvents.CLICK)) {
  6955. /*
  6956. * Chrome has an interesting idea on click targets (see
  6957. * cellOnPrevMouseDown javadoc). Firefox, on the other hand, has
  6958. * the mousedown target as the click target.
  6959. */
  6960. cell = cellOnPrevMouseDown;
  6961. }
  6962. }
  6963. if (cell == null) {
  6964. getLogger().log(Level.WARNING,
  6965. "received " + eventType + "-event with a null cell target");
  6966. return;
  6967. }
  6968. eventCell.set(cell, getSectionFromContainer(container));
  6969. GridEvent<T> gridEvent = new GridEvent<>(event, eventCell);
  6970. for (GridEventHandler<T> handler : browserEventHandlers) {
  6971. handler.onEvent(gridEvent);
  6972. }
  6973. }
  6974. private Section getSectionFromContainer(RowContainer container) {
  6975. assert container != null : "RowContainer should not be null";
  6976. if (container == escalator.getBody()) {
  6977. return Section.BODY;
  6978. } else if (container == escalator.getFooter()) {
  6979. return Section.FOOTER;
  6980. } else if (container == escalator.getHeader()) {
  6981. return Section.HEADER;
  6982. }
  6983. assert false : "RowContainer was not header, footer or body.";
  6984. return null;
  6985. }
  6986. private boolean isOrContainsInSpacer(Node node) {
  6987. Node n = node;
  6988. while (n != null && n != getElement()) {
  6989. boolean isElement = Element.is(n);
  6990. if (isElement) {
  6991. String className = Element.as(n).getClassName();
  6992. // Also check whether className is indeed a string. For
  6993. // SVGElement it may be of type SVGAnimatedString.
  6994. // https://developer.mozilla.org/en-US/docs/Web/API/Element/className#Notes
  6995. if (WidgetUtil.isString(className) && className
  6996. .contains(getStylePrimaryName() + "-spacer")) {
  6997. return true;
  6998. }
  6999. }
  7000. n = n.getParentNode();
  7001. }
  7002. return false;
  7003. }
  7004. private boolean isElementInChildWidget(Element e) {
  7005. Widget w = WidgetUtil.findWidget(e);
  7006. if (w == this) {
  7007. return false;
  7008. }
  7009. /*
  7010. * If e is directly inside this grid, but the grid is wrapped in a
  7011. * Composite, findWidget is not going to find this, only the wrapper.
  7012. * Thus we need to check its parents to see if we encounter this; if we
  7013. * don't, the found widget is actually a parent of this, so we should
  7014. * return false.
  7015. */
  7016. while (w != null && w != this) {
  7017. w = w.getParent();
  7018. }
  7019. return w != null;
  7020. }
  7021. private class EditorEventHandler implements GridEventHandler<T> {
  7022. @Override
  7023. public void onEvent(GridEvent<T> event) {
  7024. if (!isEditorEnabled()) {
  7025. return;
  7026. }
  7027. Widget widget;
  7028. if (editor.focusedColumnIndexDOM < 0) {
  7029. widget = null;
  7030. } else {
  7031. widget = editor.getWidget(
  7032. getVisibleColumn(editor.focusedColumnIndexDOM));
  7033. }
  7034. EditorDomEvent<T> editorEvent = new EditorDomEvent<>(
  7035. event.getDomEvent(), event.getCell(), widget);
  7036. event.setHandled(
  7037. getEditor().getEventHandler().handleEvent(editorEvent));
  7038. }
  7039. };
  7040. private class SuperEventHandler implements GridEventHandler<T> {
  7041. @Override
  7042. public void onEvent(GridEvent<T> event) {
  7043. if (event.isHandled()) {
  7044. return;
  7045. }
  7046. Grid.super.onBrowserEvent(event.getDomEvent());
  7047. }
  7048. };
  7049. private abstract class AbstractGridEventHandler
  7050. implements GridEventHandler<T> {
  7051. @Override
  7052. public void onEvent(GridEvent<T> event) {
  7053. if (event.isHandled()) {
  7054. return;
  7055. }
  7056. event.setHandled(isElementInChildWidget(
  7057. Element.as(event.getDomEvent().getEventTarget())));
  7058. }
  7059. };
  7060. private class RendererEventHandler extends AbstractGridEventHandler {
  7061. @Override
  7062. public void onEvent(GridEvent<T> event) {
  7063. super.onEvent(event);
  7064. if (event.isHandled()) {
  7065. return;
  7066. }
  7067. if (!event.getCell().isBody()) {
  7068. return;
  7069. }
  7070. Column<?, T> gridColumn = event.getCell().getColumn();
  7071. boolean enterKey = event.getDomEvent().getType()
  7072. .equals(BrowserEvents.KEYDOWN)
  7073. && event.getDomEvent().getKeyCode() == KeyCodes.KEY_ENTER;
  7074. boolean doubleClick = event.getDomEvent().getType()
  7075. .equals(BrowserEvents.DBLCLICK);
  7076. if (gridColumn.getRenderer() instanceof ComplexRenderer) {
  7077. ComplexRenderer<?> cplxRenderer = (ComplexRenderer<?>) gridColumn
  7078. .getRenderer();
  7079. if (cplxRenderer.getConsumedEvents()
  7080. .contains(event.getDomEvent().getType())) {
  7081. if (cplxRenderer.onBrowserEvent(event.getCell(),
  7082. event.getDomEvent())) {
  7083. event.setHandled(true);
  7084. }
  7085. }
  7086. // Calls onActivate if KeyDown and Enter or double click
  7087. if ((enterKey || doubleClick)
  7088. && cplxRenderer.onActivate(event.getCell())) {
  7089. event.setHandled(true);
  7090. }
  7091. }
  7092. }
  7093. };
  7094. private class CellFocusEventHandler extends AbstractGridEventHandler {
  7095. @Override
  7096. public void onEvent(GridEvent<T> event) {
  7097. super.onEvent(event);
  7098. if (event.isHandled()) {
  7099. return;
  7100. }
  7101. Collection<String> navigation = cellFocusHandler
  7102. .getNavigationEvents();
  7103. if (navigation.contains(event.getDomEvent().getType())) {
  7104. cellFocusHandler.handleNavigationEvent(event.getDomEvent(),
  7105. event.getCell());
  7106. }
  7107. }
  7108. };
  7109. private class HeaderCellDragStartHandler extends AbstractGridEventHandler {
  7110. @Override
  7111. public void onEvent(GridEvent<T> event) {
  7112. super.onEvent(event);
  7113. if (event.isHandled()) {
  7114. return;
  7115. }
  7116. if (!isColumnReorderingAllowed()) {
  7117. return;
  7118. }
  7119. if (!event.getCell().isHeader()) {
  7120. return;
  7121. }
  7122. int offset = 0; // apply offset depending on selection column, see
  7123. // #10546
  7124. if (getSelectionColumn().isPresent()) {
  7125. offset = -1;
  7126. }
  7127. if (event.getCell().getColumnIndex()
  7128. + offset < getFrozenColumnCount()) {
  7129. return;
  7130. }
  7131. if (event.getDomEvent().getTypeInt() == Event.ONMOUSEDOWN
  7132. && event.getDomEvent()
  7133. .getButton() == NativeEvent.BUTTON_LEFT
  7134. || event.getDomEvent().getTypeInt() == Event.ONTOUCHSTART) {
  7135. dndHandler.onDragStartOnDraggableElement(event.getDomEvent(),
  7136. headerCellDndCallback);
  7137. event.getDomEvent().preventDefault();
  7138. event.getDomEvent().stopPropagation();
  7139. // fixes https://github.com/vaadin/framework/issues/8632
  7140. // don't mark the event as handled, in order for the next
  7141. // handler in the handler chain (HeaderDefaultRowEventHandler)
  7142. // to be able to receive it. This should be safe since the next
  7143. // handlers in the chain (RendererEventHandler and
  7144. // CellFocusEventHandler) do not react to header touches/clicks.
  7145. // event.setHandled(true);
  7146. }
  7147. }
  7148. };
  7149. private class HeaderDefaultRowEventHandler
  7150. extends AbstractGridEventHandler {
  7151. private Point rowEventTouchStartingPoint;
  7152. @Override
  7153. public void onEvent(GridEvent<T> event) {
  7154. super.onEvent(event);
  7155. if (event.isHandled()) {
  7156. return;
  7157. }
  7158. if (!event.getCell().isHeader()) {
  7159. return;
  7160. }
  7161. if (!getHeader().getRow(event.getCell().getRowIndex())
  7162. .isDefault()) {
  7163. return;
  7164. }
  7165. if (!event.getCell().getColumn().isSortable()) {
  7166. // Only handle sorting events if the column is sortable
  7167. return;
  7168. }
  7169. if (BrowserEvents.MOUSEDOWN.equals(event.getDomEvent().getType())
  7170. && event.getDomEvent().getShiftKey()) {
  7171. // Don't select text when shift clicking on a header.
  7172. event.getDomEvent().preventDefault();
  7173. }
  7174. if (BrowserEvents.TOUCHSTART
  7175. .equals(event.getDomEvent().getType())) {
  7176. if (event.getDomEvent().getTouches().length() > 1) {
  7177. return;
  7178. }
  7179. event.getDomEvent().preventDefault();
  7180. Touch touch = event.getDomEvent().getChangedTouches().get(0);
  7181. rowEventTouchStartingPoint = new Point(touch.getClientX(),
  7182. touch.getClientY());
  7183. sorter.awaitForTouchEnd(GridConstants.LONG_TAP_DELAY);
  7184. event.setHandled(true);
  7185. } else if (BrowserEvents.TOUCHMOVE
  7186. .equals(event.getDomEvent().getType())) {
  7187. if (event.getDomEvent().getTouches().length() > 1) {
  7188. return;
  7189. }
  7190. if (rowEventTouchStartingPoint == null) {
  7191. return;
  7192. }
  7193. event.getDomEvent().preventDefault();
  7194. Touch touch = event.getDomEvent().getChangedTouches().get(0);
  7195. double diffX = Math.abs(
  7196. touch.getClientX() - rowEventTouchStartingPoint.getX());
  7197. double diffY = Math.abs(
  7198. touch.getClientY() - rowEventTouchStartingPoint.getY());
  7199. // Cancel long tap if finger strays too far from
  7200. // starting point
  7201. if (diffX > GridConstants.LONG_TAP_THRESHOLD
  7202. || diffY > GridConstants.LONG_TAP_THRESHOLD) {
  7203. sorter.cancelAwaitForTouchEnd();
  7204. }
  7205. event.setHandled(true);
  7206. } else if (BrowserEvents.TOUCHEND
  7207. .equals(event.getDomEvent().getType())) {
  7208. if (event.getDomEvent().getTouches().length() > 1) {
  7209. return;
  7210. }
  7211. if (rowEventTouchStartingPoint == null) {
  7212. return;
  7213. }
  7214. sorter.onTouchEnd();
  7215. event.setHandled(true);
  7216. } else if (BrowserEvents.TOUCHCANCEL
  7217. .equals(event.getDomEvent().getType())) {
  7218. if (event.getDomEvent().getTouches().length() > 1) {
  7219. return;
  7220. }
  7221. sorter.cancelAwaitForTouchEnd();
  7222. event.setHandled(true);
  7223. } else if (BrowserEvents.CLICK
  7224. .equals(event.getDomEvent().getType())) {
  7225. sorter.sort(event.getCell().getColumn(),
  7226. event.getDomEvent().getShiftKey());
  7227. }
  7228. }
  7229. };
  7230. @Override
  7231. @SuppressWarnings("deprecation")
  7232. public com.google.gwt.user.client.Element getSubPartElement(
  7233. String subPart) {
  7234. /*
  7235. * handles details[] (translated to spacer[] for Escalator), cell[],
  7236. * header[] and footer[]
  7237. */
  7238. // "#header[0][0]/DRAGhANDLE"
  7239. Element escalatorElement = escalator.getSubPartElement(
  7240. subPart.replaceFirst("^details\\[", "spacer["));
  7241. if (escalatorElement != null) {
  7242. int detailIdx = subPart.indexOf("/");
  7243. if (detailIdx > 0) {
  7244. String detail = subPart.substring(detailIdx + 1);
  7245. getLogger().severe("Looking up detail from index " + detailIdx
  7246. + " onward: \"" + detail + "\"");
  7247. if (detail.equalsIgnoreCase("content")) {
  7248. // XXX: Fix this to look up by class name!
  7249. return DOM.asOld(Element.as(escalatorElement.getChild(0)));
  7250. }
  7251. if (detail.equalsIgnoreCase("draghandle")) {
  7252. // XXX: Fix this to look up by class name!
  7253. return DOM.asOld(Element.as(escalatorElement.getChild(1)));
  7254. }
  7255. }
  7256. return DOM.asOld(escalatorElement);
  7257. }
  7258. SubPartArguments args = SubPartArguments.create(subPart);
  7259. Element editor = getSubPartElementEditor(args);
  7260. if (editor != null) {
  7261. return DOM.asOld(editor);
  7262. }
  7263. return null;
  7264. }
  7265. private Element getSubPartElementEditor(SubPartArguments args) {
  7266. if (!args.getType().equalsIgnoreCase("editor")
  7267. || editor.getState() != State.ACTIVE) {
  7268. return null;
  7269. }
  7270. if (args.getIndicesLength() == 0) {
  7271. return editor.editorOverlay;
  7272. } else if (args.getIndicesLength() == 1) {
  7273. int index = args.getIndex(0);
  7274. if (index >= columns.size()) {
  7275. return null;
  7276. }
  7277. escalator.scrollToColumn(index, ScrollDestination.ANY, 0);
  7278. Widget widget = editor.getWidget(columns.get(index));
  7279. if (widget != null) {
  7280. return widget.getElement();
  7281. }
  7282. // No widget for the column.
  7283. return null;
  7284. }
  7285. return null;
  7286. }
  7287. @Override
  7288. @SuppressWarnings("deprecation")
  7289. public String getSubPartName(
  7290. com.google.gwt.user.client.Element subElement) {
  7291. String escalatorStructureName = escalator.getSubPartName(subElement);
  7292. if (escalatorStructureName != null) {
  7293. return escalatorStructureName.replaceFirst("^spacer", "details");
  7294. }
  7295. String editorName = getSubPartNameEditor(subElement);
  7296. if (editorName != null) {
  7297. return editorName;
  7298. }
  7299. return null;
  7300. }
  7301. private String getSubPartNameEditor(Element subElement) {
  7302. if (editor.getState() != State.ACTIVE
  7303. || !editor.editorOverlay.isOrHasChild(subElement)) {
  7304. return null;
  7305. }
  7306. int i = 0;
  7307. for (Column<?, T> column : columns) {
  7308. if (editor.getWidget(column).getElement()
  7309. .isOrHasChild(subElement)) {
  7310. return "editor[" + i + "]";
  7311. }
  7312. ++i;
  7313. }
  7314. return "editor";
  7315. }
  7316. private void setSelectColumnRenderer(
  7317. final Renderer<Boolean> selectColumnRenderer) {
  7318. if (this.selectColumnRenderer == selectColumnRenderer) {
  7319. return;
  7320. }
  7321. if (this.selectColumnRenderer != null) {
  7322. if (this.selectColumnRenderer instanceof ComplexRenderer) {
  7323. // End of Life for the old selection column renderer.
  7324. ((ComplexRenderer<?>) this.selectColumnRenderer).destroy();
  7325. }
  7326. // Clear field so frozen column logic in the remove method knows
  7327. // what to do
  7328. Column<?, T> colToRemove = selectionColumn;
  7329. selectionColumn = null;
  7330. removeColumnSkipSelectionColumnCheck(colToRemove);
  7331. cellFocusHandler.offsetRangeBy(-1);
  7332. }
  7333. this.selectColumnRenderer = selectColumnRenderer;
  7334. if (selectColumnRenderer != null) {
  7335. cellFocusHandler.offsetRangeBy(1);
  7336. selectionColumn = new SelectionColumn(selectColumnRenderer);
  7337. addColumnsSkipSelectionColumnCheck(
  7338. Collections.singleton(selectionColumn), 0);
  7339. selectionColumn.initDone();
  7340. } else {
  7341. selectionColumn = null;
  7342. requestRefreshBody();
  7343. }
  7344. updateFrozenColumns();
  7345. }
  7346. /**
  7347. * Sets the current selection model.
  7348. *
  7349. * @param selectionModel
  7350. * a selection model implementation.
  7351. * @throws IllegalArgumentException
  7352. * if selection model argument is null
  7353. */
  7354. public void setSelectionModel(SelectionModel<T> selectionModel) {
  7355. if (selectionModel == null) {
  7356. throw new IllegalArgumentException("Selection model can't be null");
  7357. }
  7358. this.selectionModel = selectionModel;
  7359. if (selectionModel instanceof SelectionModelWithSelectionColumn) {
  7360. setSelectColumnRenderer(
  7361. ((SelectionModelWithSelectionColumn) selectionModel)
  7362. .getRenderer());
  7363. } else {
  7364. setSelectColumnRenderer(null);
  7365. }
  7366. if (this.selectionModel.isMultiSelectionAllowed()) {
  7367. escalator.getTable().setAttribute("aria-multiselectable", "true");
  7368. } else if (this.selectionModel.isSelectionAllowed()) {
  7369. escalator.getTable().setAttribute("aria-multiselectable", "false");
  7370. } else {
  7371. escalator.getTable().removeAttribute("aria-multiselectable");
  7372. }
  7373. // Refresh rendered rows to update selection, if it has changed
  7374. requestRefreshBody();
  7375. }
  7376. /**
  7377. * Gets a reference to the current selection model.
  7378. *
  7379. * @return the currently used SelectionModel instance.
  7380. */
  7381. public SelectionModel<T> getSelectionModel() {
  7382. return selectionModel;
  7383. }
  7384. /**
  7385. * Returns if a row is selected.
  7386. *
  7387. * @param row
  7388. * a row object
  7389. * @return {@code true}, if the current selection model considers the
  7390. * provided row object selected.
  7391. */
  7392. public boolean isSelected(T row) {
  7393. return selectionModel.isSelected(row);
  7394. }
  7395. /**
  7396. * Selects a row using the current selection model.
  7397. * <p>
  7398. * Only selection models implementing {@link SelectionModel.Single} and
  7399. * {@link SelectionModel.Multi} are supported; for anything else, an
  7400. * exception will be thrown.
  7401. *
  7402. * @param row
  7403. * a row object
  7404. * @throws IllegalStateException
  7405. * if the current selection model is not an instance of
  7406. * {@link SelectionModel.Single} or {@link SelectionModel.Multi}
  7407. */
  7408. public void select(T row) {
  7409. if (getSelectionModel().isSelectionAllowed()) {
  7410. getSelectionModel().select(row);
  7411. }
  7412. }
  7413. /**
  7414. * Deselects a row using the current selection model.
  7415. * <p>
  7416. * Only selection models implementing {@link SelectionModel.Single} and
  7417. * {@link SelectionModel.Multi} are supported; for anything else, an
  7418. * exception will be thrown.
  7419. *
  7420. * @param row
  7421. * a row object
  7422. * @throws IllegalStateException
  7423. * if the current selection model is not an instance of
  7424. * {@link SelectionModel.Single} or {@link SelectionModel.Multi}
  7425. */
  7426. public void deselect(T row) {
  7427. if (getSelectionModel().isSelectionAllowed()) {
  7428. getSelectionModel().deselect(row);
  7429. }
  7430. }
  7431. /**
  7432. * Deselects all rows using the current selection model.
  7433. *
  7434. * @throws IllegalStateException
  7435. * if the current selection model is not an instance of
  7436. * {@link SelectionModel.Single} or {@link SelectionModel.Multi}
  7437. */
  7438. public void deselectAll() {
  7439. getSelectionModel().deselectAll();
  7440. }
  7441. @Override
  7442. public HandlerRegistration addSelectionHandler(
  7443. final SelectionHandler<T> handler) {
  7444. return addHandler(handler, SelectionEvent.getType());
  7445. }
  7446. /**
  7447. * Sets the current sort order using the fluid Sort API. Read the
  7448. * documentation for {@link Sort} for more information.
  7449. *
  7450. * @param s
  7451. * a sort instance
  7452. */
  7453. public void sort(Sort s) {
  7454. setSortOrder(s.build());
  7455. }
  7456. /**
  7457. * Sorts the Grid data in ascending order along one column.
  7458. *
  7459. * @param column
  7460. * a grid column reference
  7461. */
  7462. public <C> void sort(Column<C, T> column) {
  7463. sort(column, SortDirection.ASCENDING);
  7464. }
  7465. /**
  7466. * Sorts the Grid data along one column.
  7467. *
  7468. * @param column
  7469. * a grid column reference
  7470. * @param direction
  7471. * a sort direction value
  7472. */
  7473. public <C> void sort(Column<C, T> column, SortDirection direction) {
  7474. sort(Sort.by(column, direction));
  7475. }
  7476. /**
  7477. * Sets the sort order to use. Setting this causes the Grid to re-sort
  7478. * itself.
  7479. *
  7480. * @param order
  7481. * a sort order list. If set to null, the sort order is cleared.
  7482. */
  7483. public void setSortOrder(List<SortOrder> order) {
  7484. setSortOrder(order, false);
  7485. }
  7486. /**
  7487. * Clears the sort order and indicators without re-sorting.
  7488. */
  7489. private void clearSortOrder() {
  7490. sortOrder.clear();
  7491. refreshHeader();
  7492. }
  7493. private void setSortOrder(List<SortOrder> order, boolean userOriginated) {
  7494. if (order != sortOrder) {
  7495. sortOrder.clear();
  7496. if (order != null) {
  7497. sortOrder.addAll(order);
  7498. }
  7499. }
  7500. sort(userOriginated);
  7501. }
  7502. /**
  7503. * Get a copy of the current sort order array.
  7504. *
  7505. * @return a copy of the current sort order array
  7506. */
  7507. public List<SortOrder> getSortOrder() {
  7508. return Collections.unmodifiableList(sortOrder);
  7509. }
  7510. /**
  7511. * Finds the sorting order for this column
  7512. */
  7513. private SortOrder getSortOrder(Column<?, ?> column) {
  7514. for (SortOrder order : getSortOrder()) {
  7515. if (order.getColumn() == column) {
  7516. return order;
  7517. }
  7518. }
  7519. return null;
  7520. }
  7521. /**
  7522. * Register a GWT event handler for a sorting event. This handler gets
  7523. * called whenever this Grid needs its data source to provide data sorted in
  7524. * a specific order.
  7525. *
  7526. * @param handler
  7527. * a sort event handler
  7528. * @return the registration for the event
  7529. */
  7530. public HandlerRegistration addSortHandler(SortHandler<T> handler) {
  7531. return addHandler(handler, SortEvent.getType());
  7532. }
  7533. /**
  7534. * Register a GWT event handler for a select all event. This handler gets
  7535. * called whenever Grid needs all rows selected.
  7536. * <p>
  7537. * In case the select all checkbox is not visible in the
  7538. * {@link SelectionColumn}, it will be come visible after adding the
  7539. * handler.
  7540. *
  7541. * @param handler
  7542. * a select all event handler
  7543. * @return the registration for the event
  7544. */
  7545. public HandlerRegistration addSelectAllHandler(
  7546. SelectAllHandler<T> handler) {
  7547. HandlerRegistration registration = addHandler(handler,
  7548. SelectAllEvent.getType());
  7549. return registration;
  7550. }
  7551. /**
  7552. * Register a GWT event handler for a data available event. This handler
  7553. * gets called whenever the {@link DataSource} for this Grid has new data
  7554. * available.
  7555. * <p>
  7556. * This handle will be fired with the current available data after
  7557. * registration is done.
  7558. *
  7559. * @param handler
  7560. * a data available event handler
  7561. * @return the registration for the event
  7562. */
  7563. public HandlerRegistration addDataAvailableHandler(
  7564. final DataAvailableHandler handler) {
  7565. // Deferred call to handler with current row range
  7566. Scheduler.get().scheduleFinally(() -> {
  7567. if (!dataSource.isWaitingForData()) {
  7568. handler.onDataAvailable(
  7569. new DataAvailableEvent(currentDataAvailable));
  7570. }
  7571. });
  7572. return addHandler(handler, DataAvailableEvent.TYPE);
  7573. }
  7574. /**
  7575. * Register a BodyKeyDownHandler to this Grid. The event for this handler is
  7576. * fired when a KeyDown event occurs while cell focus is in the Body of this
  7577. * Grid.
  7578. *
  7579. * @param handler
  7580. * the key handler to register
  7581. * @return the registration for the event
  7582. */
  7583. public HandlerRegistration addBodyKeyDownHandler(
  7584. BodyKeyDownHandler handler) {
  7585. return addHandler(handler, GridKeyDownEvent.TYPE);
  7586. }
  7587. /**
  7588. * Register a BodyKeyUpHandler to this Grid. The event for this handler is
  7589. * fired when a KeyUp event occurs while cell focus is in the Body of this
  7590. * Grid.
  7591. *
  7592. * @param handler
  7593. * the key handler to register
  7594. * @return the registration for the event
  7595. */
  7596. public HandlerRegistration addBodyKeyUpHandler(BodyKeyUpHandler handler) {
  7597. return addHandler(handler, GridKeyUpEvent.TYPE);
  7598. }
  7599. /**
  7600. * Register a BodyKeyPressHandler to this Grid. The event for this handler
  7601. * is fired when a KeyPress event occurs while cell focus is in the Body of
  7602. * this Grid.
  7603. *
  7604. * @param handler
  7605. * the key handler to register
  7606. * @return the registration for the event
  7607. */
  7608. public HandlerRegistration addBodyKeyPressHandler(
  7609. BodyKeyPressHandler handler) {
  7610. return addHandler(handler, GridKeyPressEvent.TYPE);
  7611. }
  7612. /**
  7613. * Register a HeaderKeyDownHandler to this Grid. The event for this handler
  7614. * is fired when a KeyDown event occurs while cell focus is in the Header of
  7615. * this Grid.
  7616. *
  7617. * @param handler
  7618. * the key handler to register
  7619. * @return the registration for the event
  7620. */
  7621. public HandlerRegistration addHeaderKeyDownHandler(
  7622. HeaderKeyDownHandler handler) {
  7623. return addHandler(handler, GridKeyDownEvent.TYPE);
  7624. }
  7625. /**
  7626. * Register a HeaderKeyUpHandler to this Grid. The event for this handler is
  7627. * fired when a KeyUp event occurs while cell focus is in the Header of this
  7628. * Grid.
  7629. *
  7630. * @param handler
  7631. * the key handler to register
  7632. * @return the registration for the event
  7633. */
  7634. public HandlerRegistration addHeaderKeyUpHandler(
  7635. HeaderKeyUpHandler handler) {
  7636. return addHandler(handler, GridKeyUpEvent.TYPE);
  7637. }
  7638. /**
  7639. * Register a HeaderKeyPressHandler to this Grid. The event for this handler
  7640. * is fired when a KeyPress event occurs while cell focus is in the Header
  7641. * of this Grid.
  7642. *
  7643. * @param handler
  7644. * the key handler to register
  7645. * @return the registration for the event
  7646. */
  7647. public HandlerRegistration addHeaderKeyPressHandler(
  7648. HeaderKeyPressHandler handler) {
  7649. return addHandler(handler, GridKeyPressEvent.TYPE);
  7650. }
  7651. /**
  7652. * Register a FooterKeyDownHandler to this Grid. The event for this handler
  7653. * is fired when a KeyDown event occurs while cell focus is in the Footer of
  7654. * this Grid.
  7655. *
  7656. * @param handler
  7657. * the key handler to register
  7658. * @return the registration for the event
  7659. */
  7660. public HandlerRegistration addFooterKeyDownHandler(
  7661. FooterKeyDownHandler handler) {
  7662. return addHandler(handler, GridKeyDownEvent.TYPE);
  7663. }
  7664. /**
  7665. * Register a FooterKeyUpHandler to this Grid. The event for this handler is
  7666. * fired when a KeyUp event occurs while cell focus is in the Footer of this
  7667. * Grid.
  7668. *
  7669. * @param handler
  7670. * the key handler to register
  7671. * @return the registration for the event
  7672. */
  7673. public HandlerRegistration addFooterKeyUpHandler(
  7674. FooterKeyUpHandler handler) {
  7675. return addHandler(handler, GridKeyUpEvent.TYPE);
  7676. }
  7677. /**
  7678. * Register a FooterKeyPressHandler to this Grid. The event for this handler
  7679. * is fired when a KeyPress event occurs while cell focus is in the Footer
  7680. * of this Grid.
  7681. *
  7682. * @param handler
  7683. * the key handler to register
  7684. * @return the registration for the event
  7685. */
  7686. public HandlerRegistration addFooterKeyPressHandler(
  7687. FooterKeyPressHandler handler) {
  7688. return addHandler(handler, GridKeyPressEvent.TYPE);
  7689. }
  7690. /**
  7691. * Register a BodyClickHandler to this Grid. The event for this handler is
  7692. * fired when a Click event occurs in the Body of this Grid.
  7693. *
  7694. * @param handler
  7695. * the click handler to register
  7696. * @return the registration for the event
  7697. */
  7698. public HandlerRegistration addBodyClickHandler(BodyClickHandler handler) {
  7699. return addHandler(handler, GridClickEvent.TYPE);
  7700. }
  7701. /**
  7702. * Register a HeaderClickHandler to this Grid. The event for this handler is
  7703. * fired when a Click event occurs in the Header of this Grid.
  7704. *
  7705. * @param handler
  7706. * the click handler to register
  7707. * @return the registration for the event
  7708. */
  7709. public HandlerRegistration addHeaderClickHandler(
  7710. HeaderClickHandler handler) {
  7711. return addHandler(handler, GridClickEvent.TYPE);
  7712. }
  7713. /**
  7714. * Register a FooterClickHandler to this Grid. The event for this handler is
  7715. * fired when a Click event occurs in the Footer of this Grid.
  7716. *
  7717. * @param handler
  7718. * the click handler to register
  7719. * @return the registration for the event
  7720. */
  7721. public HandlerRegistration addFooterClickHandler(
  7722. FooterClickHandler handler) {
  7723. return addHandler(handler, GridClickEvent.TYPE);
  7724. }
  7725. /**
  7726. * Register a BodyDoubleClickHandler to this Grid. The event for this
  7727. * handler is fired when a double click event occurs in the Body of this
  7728. * Grid.
  7729. *
  7730. * @param handler
  7731. * the double click handler to register
  7732. * @return the registration for the event
  7733. */
  7734. public HandlerRegistration addBodyDoubleClickHandler(
  7735. BodyDoubleClickHandler handler) {
  7736. return addHandler(handler, GridDoubleClickEvent.TYPE);
  7737. }
  7738. /**
  7739. * Register a HeaderDoubleClickHandler to this Grid. The event for this
  7740. * handler is fired when a double click event occurs in the Header of this
  7741. * Grid.
  7742. *
  7743. * @param handler
  7744. * the double click handler to register
  7745. * @return the registration for the event
  7746. */
  7747. public HandlerRegistration addHeaderDoubleClickHandler(
  7748. HeaderDoubleClickHandler handler) {
  7749. return addHandler(handler, GridDoubleClickEvent.TYPE);
  7750. }
  7751. /**
  7752. * Register a FooterDoubleClickHandler to this Grid. The event for this
  7753. * handler is fired when a double click event occurs in the Footer of this
  7754. * Grid.
  7755. *
  7756. * @param handler
  7757. * the double click handler to register
  7758. * @return the registration for the event
  7759. */
  7760. public HandlerRegistration addFooterDoubleClickHandler(
  7761. FooterDoubleClickHandler handler) {
  7762. return addHandler(handler, GridDoubleClickEvent.TYPE);
  7763. }
  7764. /**
  7765. * Register a column reorder handler to this Grid. The event for this
  7766. * handler is fired when the Grid's columns are reordered.
  7767. *
  7768. * @since 7.5.0
  7769. * @param handler
  7770. * the handler for the event
  7771. * @return the registration for the event
  7772. */
  7773. public HandlerRegistration addColumnReorderHandler(
  7774. ColumnReorderHandler<T> handler) {
  7775. return addHandler(handler, ColumnReorderEvent.getType());
  7776. }
  7777. /**
  7778. * Register a column visibility change handler to this Grid. The event for
  7779. * this handler is fired when the Grid's columns change visibility.
  7780. *
  7781. * @since 7.5.0
  7782. * @param handler
  7783. * the handler for the event
  7784. * @return the registration for the event
  7785. */
  7786. public HandlerRegistration addColumnVisibilityChangeHandler(
  7787. ColumnVisibilityChangeHandler<T> handler) {
  7788. return addHandler(handler, ColumnVisibilityChangeEvent.getType());
  7789. }
  7790. /**
  7791. * Register a column resize handler to this Grid. The event for this handler
  7792. * is fired when the Grid's columns are resized.
  7793. *
  7794. * @since 7.6
  7795. * @param handler
  7796. * the handler for the event
  7797. * @return the registration for the event
  7798. */
  7799. public HandlerRegistration addColumnResizeHandler(
  7800. ColumnResizeHandler<T> handler) {
  7801. return addHandler(handler, ColumnResizeEvent.getType());
  7802. }
  7803. /**
  7804. * Register a enabled status change handler to this Grid. The event for this
  7805. * handler is fired when the Grid changes from disabled to enabled and
  7806. * vice-versa.
  7807. *
  7808. * @param handler
  7809. * the handler for the event
  7810. * @return the registration for the event
  7811. */
  7812. public HandlerRegistration addEnabledHandler(GridEnabledHandler handler) {
  7813. return addHandler(handler, GridEnabledEvent.TYPE);
  7814. }
  7815. /**
  7816. * Register a selection allowed status change handler to this Grid. The
  7817. * event for this handler is fired when the Grid changes selection allowed
  7818. * state.
  7819. *
  7820. * @param handler
  7821. * the handler for the event
  7822. * @return the registration for the event
  7823. */
  7824. public HandlerRegistration addSelectionAllowedHandler(
  7825. GridSelectionAllowedHandler handler) {
  7826. return addHandler(handler, GridSelectionAllowedEvent.TYPE);
  7827. }
  7828. public HandlerRegistration addRowHeightChangedHandler(
  7829. RowHeightChangedHandler handler) {
  7830. return escalator.addHandler(handler, RowHeightChangedEvent.TYPE);
  7831. }
  7832. /**
  7833. * Adds a spacer visibility changed handler to the underlying escalator.
  7834. *
  7835. * @param handler
  7836. * the handler to be called when a spacer's visibility changes
  7837. * @return the registration object with which the handler can be removed
  7838. * @since 8.3.2
  7839. */
  7840. public HandlerRegistration addSpacerVisibilityChangedHandler(
  7841. SpacerVisibilityChangedHandler handler) {
  7842. return escalator.addHandler(handler, SpacerVisibilityChangedEvent.TYPE);
  7843. }
  7844. /**
  7845. * Adds a spacer index changed handler to the underlying escalator.
  7846. *
  7847. * @param handler
  7848. * the handler to be called when a spacer's index changes
  7849. * @return the registration object with which the handler can be removed
  7850. * @since 8.9
  7851. */
  7852. public HandlerRegistration addSpacerIndexChangedHandler(
  7853. SpacerIndexChangedHandler handler) {
  7854. return escalator.addHandler(handler, SpacerIndexChangedEvent.TYPE);
  7855. }
  7856. /**
  7857. * Adds a low-level DOM event handler to this Grid. The handler is inserted
  7858. * into the given position in the list of handlers. The handlers are invoked
  7859. * in order. If the
  7860. * {@link GridEventHandler#onEvent(Event, EventCellReference) onEvent}
  7861. * method of a handler returns true, subsequent handlers are not invoked.
  7862. *
  7863. * @param index
  7864. * the index to insert the handler to
  7865. * @param handler
  7866. * the handler to add
  7867. */
  7868. public void addBrowserEventHandler(int index, GridEventHandler<T> handler) {
  7869. browserEventHandlers.add(index, handler);
  7870. }
  7871. /**
  7872. * Apply sorting to data source.
  7873. */
  7874. private void sort(boolean userOriginated) {
  7875. refreshHeader();
  7876. fireEvent(new SortEvent<>(this, Collections.unmodifiableList(sortOrder),
  7877. userOriginated));
  7878. }
  7879. private int getLastVisibleRowIndex() {
  7880. int lastRowIndex = escalator.getVisibleRowRange().getEnd();
  7881. int footerTop = escalator.getFooter().getElement().getAbsoluteTop();
  7882. Element lastRow;
  7883. do {
  7884. lastRow = escalator.getBody().getRowElement(--lastRowIndex);
  7885. } while (lastRow.getAbsoluteTop() > footerTop);
  7886. return lastRowIndex;
  7887. }
  7888. private int getFirstVisibleRowIndex() {
  7889. int firstRowIndex = escalator.getVisibleRowRange().getStart();
  7890. int headerBottom = escalator.getHeader().getElement()
  7891. .getAbsoluteBottom();
  7892. Element firstRow = escalator.getBody().getRowElement(firstRowIndex);
  7893. while (firstRow.getAbsoluteBottom() < headerBottom) {
  7894. firstRow = escalator.getBody().getRowElement(++firstRowIndex);
  7895. }
  7896. return firstRowIndex;
  7897. }
  7898. /**
  7899. * Adds a scroll handler to this grid.
  7900. *
  7901. * @param handler
  7902. * the scroll handler to add
  7903. * @return a handler registration for the registered scroll handler
  7904. */
  7905. public HandlerRegistration addScrollHandler(ScrollHandler handler) {
  7906. return addHandler(handler, ScrollEvent.TYPE);
  7907. }
  7908. @Override
  7909. public boolean isWorkPending() {
  7910. return escalator.isWorkPending() || dataSource.isWaitingForData()
  7911. || autoColumnWidthsRecalculator.isScheduled()
  7912. || editor.isWorkPending();
  7913. }
  7914. /**
  7915. * Returns whether columns can be reordered with drag and drop.
  7916. *
  7917. * @since 7.5.0
  7918. * @return <code>true</code> if columns can be reordered, false otherwise
  7919. */
  7920. public boolean isColumnReorderingAllowed() {
  7921. return columnReorderingAllowed;
  7922. }
  7923. /**
  7924. * Sets whether column reordering with drag and drop is allowed or not.
  7925. *
  7926. * @since 7.5.0
  7927. * @param columnReorderingAllowed
  7928. * specifies whether column reordering is allowed
  7929. */
  7930. public void setColumnReorderingAllowed(boolean columnReorderingAllowed) {
  7931. this.columnReorderingAllowed = columnReorderingAllowed;
  7932. }
  7933. /**
  7934. * Sets a new column order for the grid. All columns which are not ordered
  7935. * here will remain in the order they were before as the last columns of
  7936. * grid.
  7937. *
  7938. * @param orderedColumns
  7939. * array of columns in wanted order
  7940. */
  7941. public void setColumnOrder(Column<?, T>... orderedColumns) {
  7942. setColumnOrder(false, orderedColumns);
  7943. }
  7944. private void setColumnOrder(boolean isUserOriginated,
  7945. Column<?, T>... orderedColumns) {
  7946. List<Column<?, T>> newOrder = new ArrayList<>();
  7947. if (selectionColumn != null) {
  7948. newOrder.add(selectionColumn);
  7949. }
  7950. int i = 0;
  7951. for (Column<?, T> column : orderedColumns) {
  7952. if (columns.contains(column)) {
  7953. newOrder.add(column);
  7954. ++i;
  7955. } else {
  7956. throw new IllegalArgumentException("Given column at index " + i
  7957. + " does not exist in Grid");
  7958. }
  7959. }
  7960. if (columns.size() != newOrder.size()) {
  7961. columns.stream().filter(col -> !newOrder.contains(col))
  7962. .forEach(newOrder::add);
  7963. }
  7964. if (columns.equals(newOrder)) {
  7965. // No changes in order.
  7966. return;
  7967. }
  7968. // Prepare event for firing
  7969. ColumnReorderEvent<T> event = new ColumnReorderEvent<>(columns,
  7970. newOrder, isUserOriginated);
  7971. // Trigger ComplexRenderer.destroy for old content
  7972. ColumnConfiguration conf = getEscalator().getColumnConfiguration();
  7973. conf.removeColumns(0, conf.getColumnCount());
  7974. // Update the order
  7975. columns = newOrder;
  7976. // Do ComplexRenderer.init and render new content
  7977. conf.insertColumns(0, getVisibleColumns().size());
  7978. // Number of frozen columns should be kept same #16901
  7979. updateFrozenColumns();
  7980. // Update column widths.
  7981. for (Column<?, T> column : columns) {
  7982. column.reapplyWidth();
  7983. }
  7984. // Recalculate all the colspans
  7985. for (HeaderRow row : header.getRows()) {
  7986. row.calculateColspans();
  7987. }
  7988. for (FooterRow row : footer.getRows()) {
  7989. row.calculateColspans();
  7990. }
  7991. columnHider.updateTogglesOrder();
  7992. fireEvent(event);
  7993. }
  7994. /**
  7995. * Sets the style generator that is used for generating styles for cells.
  7996. *
  7997. * @param cellStyleGenerator
  7998. * the cell style generator to set, or <code>null</code> to
  7999. * remove a previously set generator
  8000. */
  8001. public void setCellStyleGenerator(
  8002. CellStyleGenerator<T> cellStyleGenerator) {
  8003. this.cellStyleGenerator = cellStyleGenerator;
  8004. requestRefreshBody();
  8005. }
  8006. /**
  8007. * Gets the style generator that is used for generating styles for cells.
  8008. *
  8009. * @return the cell style generator, or <code>null</code> if no generator is
  8010. * set
  8011. */
  8012. public CellStyleGenerator<T> getCellStyleGenerator() {
  8013. return cellStyleGenerator;
  8014. }
  8015. /**
  8016. * Sets the style generator that is used for generating styles for rows.
  8017. *
  8018. * @param rowStyleGenerator
  8019. * the row style generator to set, or <code>null</code> to remove
  8020. * a previously set generator
  8021. */
  8022. public void setRowStyleGenerator(RowStyleGenerator<T> rowStyleGenerator) {
  8023. this.rowStyleGenerator = rowStyleGenerator;
  8024. requestRefreshBody();
  8025. }
  8026. /**
  8027. * Gets the style generator that is used for generating styles for rows.
  8028. *
  8029. * @return the row style generator, or <code>null</code> if no generator is
  8030. * set
  8031. */
  8032. public RowStyleGenerator<T> getRowStyleGenerator() {
  8033. return rowStyleGenerator;
  8034. }
  8035. private static void setCustomStyleName(Element element, String styleName) {
  8036. assert element != null;
  8037. String oldStyleName = element
  8038. .getPropertyString(CUSTOM_STYLE_PROPERTY_NAME);
  8039. if (!SharedUtil.equals(oldStyleName, styleName)) {
  8040. if (oldStyleName != null && !oldStyleName.isEmpty()) {
  8041. element.removeClassName(oldStyleName);
  8042. }
  8043. if (styleName != null && !styleName.isEmpty()) {
  8044. element.addClassName(styleName);
  8045. }
  8046. element.setPropertyString(CUSTOM_STYLE_PROPERTY_NAME, styleName);
  8047. }
  8048. }
  8049. /**
  8050. * Opens the editor over the row with the given index.
  8051. *
  8052. * @param rowIndex
  8053. * the index of the row to be edited
  8054. *
  8055. * @throws IllegalStateException
  8056. * if the editor is not enabled
  8057. * @throws IllegalStateException
  8058. * if the editor is already in edit mode
  8059. */
  8060. public void editRow(int rowIndex) {
  8061. editor.editRow(rowIndex);
  8062. }
  8063. /**
  8064. * Returns whether the editor is currently open on some row.
  8065. *
  8066. * @return {@code true} if the editor is active, {@code false} otherwise.
  8067. */
  8068. public boolean isEditorActive() {
  8069. return editor.getState() != State.INACTIVE;
  8070. }
  8071. /**
  8072. * Saves any unsaved changes in the editor to the data source.
  8073. *
  8074. * @throws IllegalStateException
  8075. * if the editor is not enabled
  8076. * @throws IllegalStateException
  8077. * if the editor is not in edit mode
  8078. */
  8079. public void saveEditor() {
  8080. editor.save();
  8081. }
  8082. /**
  8083. * Cancels the currently active edit and hides the editor. Any changes that
  8084. * are not {@link #saveEditor() saved} are lost.
  8085. *
  8086. * @throws IllegalStateException
  8087. * if the editor is not enabled
  8088. * @throws IllegalStateException
  8089. * if the editor is not in edit mode
  8090. */
  8091. public void cancelEditor() {
  8092. editor.cancel();
  8093. }
  8094. /**
  8095. * Returns the handler responsible for binding data and editor widgets to
  8096. * the editor.
  8097. *
  8098. * @return the editor handler or null if not set
  8099. */
  8100. public EditorHandler<T> getEditorHandler() {
  8101. return editor.getHandler();
  8102. }
  8103. /**
  8104. * Sets the handler responsible for binding data and editor widgets to the
  8105. * editor.
  8106. *
  8107. * @param handler
  8108. * the new editor handler
  8109. *
  8110. * @throws IllegalStateException
  8111. * if the editor is currently in edit mode
  8112. */
  8113. public void setEditorHandler(EditorHandler<T> handler) {
  8114. editor.setHandler(handler);
  8115. }
  8116. /**
  8117. * Returns the enabled state of the editor.
  8118. *
  8119. * @return true if editing is enabled, false otherwise
  8120. */
  8121. public boolean isEditorEnabled() {
  8122. return editor.isEnabled();
  8123. }
  8124. /**
  8125. * Sets the enabled state of the editor.
  8126. *
  8127. * @param enabled
  8128. * true to enable editing, false to disable
  8129. *
  8130. * @throws IllegalStateException
  8131. * if in edit mode and trying to disable
  8132. * @throws IllegalStateException
  8133. * if the editor handler is not set
  8134. */
  8135. public void setEditorEnabled(boolean enabled) {
  8136. editor.setEnabled(enabled);
  8137. }
  8138. /**
  8139. * Returns the editor widget associated with the given column. If the editor
  8140. * is not active, returns null.
  8141. *
  8142. * @param column
  8143. * the column
  8144. * @return the widget if the editor is open, null otherwise
  8145. */
  8146. public Widget getEditorWidget(Column<?, T> column) {
  8147. return editor.getWidget(column);
  8148. }
  8149. /**
  8150. * Sets the caption on the save button in the Grid editor.
  8151. *
  8152. * @param saveCaption
  8153. * the caption to set
  8154. * @throws IllegalArgumentException
  8155. * if {@code saveCaption} is {@code null}
  8156. */
  8157. public void setEditorSaveCaption(String saveCaption)
  8158. throws IllegalArgumentException {
  8159. editor.setSaveCaption(saveCaption);
  8160. }
  8161. /**
  8162. * Gets the current caption on the save button in the Grid editor.
  8163. *
  8164. * @return the current caption on the save button
  8165. */
  8166. public String getEditorSaveCaption() {
  8167. return editor.getSaveCaption();
  8168. }
  8169. /**
  8170. * Sets the caption on the cancel button in the Grid editor.
  8171. *
  8172. * @param cancelCaption
  8173. * the caption to set
  8174. * @throws IllegalArgumentException
  8175. * if {@code cancelCaption} is {@code null}
  8176. */
  8177. public void setEditorCancelCaption(String cancelCaption)
  8178. throws IllegalArgumentException {
  8179. editor.setCancelCaption(cancelCaption);
  8180. }
  8181. /**
  8182. * Gets the caption on the cancel button in the Grid editor.
  8183. *
  8184. * @return the current caption on the cancel button
  8185. */
  8186. public String getEditorCancelCaption() {
  8187. return editor.getCancelCaption();
  8188. }
  8189. @Override
  8190. protected void onAttach() {
  8191. super.onAttach();
  8192. // Grid was just attached to DOM. Column widths should be
  8193. // calculated.
  8194. recalculateColumnWidths();
  8195. if (getEscalator().getBody().getRowCount() == 0 && dataSource != null) {
  8196. setEscalatorSizeFromDataSource();
  8197. }
  8198. for (int row : reattachVisibleDetails) {
  8199. setDetailsVisible(row, true);
  8200. }
  8201. reattachVisibleDetails.clear();
  8202. }
  8203. @Override
  8204. protected void onDetach() {
  8205. Set<Integer> details = new HashSet<>(visibleDetails);
  8206. reattachVisibleDetails.clear();
  8207. reattachVisibleDetails.addAll(details);
  8208. for (int row : details) {
  8209. setDetailsVisible(row, false);
  8210. }
  8211. super.onDetach();
  8212. }
  8213. @Override
  8214. public void onResize() {
  8215. super.onResize();
  8216. /*
  8217. * Delay calculation to be deferred so Escalator can do it's magic.
  8218. */
  8219. Scheduler.get().scheduleFinally(() -> {
  8220. if (escalator
  8221. .getInnerWidth() != autoColumnWidthsRecalculator.lastCalculatedInnerWidth) {
  8222. recalculateColumnWidths();
  8223. }
  8224. // Vertical resizing could make editor positioning invalid so it
  8225. // needs to be recalculated on resize
  8226. if (isEditorActive()) {
  8227. editor.updateVerticalScrollPosition();
  8228. }
  8229. // if there is a resize, we need to refresh the body to avoid an
  8230. // off-by-one error which occurs when the user scrolls all the
  8231. // way to the bottom.
  8232. refreshBody();
  8233. });
  8234. }
  8235. private double getEscalatorInnerHeight() {
  8236. return new ComputedStyle(getEscalator().getTableWrapper())
  8237. .getHeightIncludingBorderPadding();
  8238. }
  8239. /**
  8240. * Grid does not support adding Widgets this way.
  8241. * <p>
  8242. * This method is implemented only because removing widgets from Grid (added
  8243. * via e.g. {@link Renderer}s) requires the {@link HasWidgets} interface.
  8244. *
  8245. * @param w
  8246. * irrelevant
  8247. * @throws UnsupportedOperationException
  8248. * always
  8249. */
  8250. @Override
  8251. @Deprecated
  8252. public void add(Widget w) {
  8253. throw new UnsupportedOperationException(
  8254. "Cannot add widgets to Grid with this method");
  8255. }
  8256. /**
  8257. * Grid does not support clearing Widgets this way.
  8258. * <p>
  8259. * This method is implemented only because removing widgets from Grid (added
  8260. * via e.g. {@link Renderer}s) requires the {@link HasWidgets} interface.
  8261. *
  8262. * @throws UnsupportedOperationException
  8263. * always
  8264. */
  8265. @Override
  8266. @Deprecated
  8267. public void clear() {
  8268. throw new UnsupportedOperationException(
  8269. "Cannot clear widgets from Grid this way");
  8270. }
  8271. /**
  8272. * Grid does not support iterating through Widgets this way.
  8273. * <p>
  8274. * This method is implemented only because removing widgets from Grid (added
  8275. * via e.g. {@link Renderer}s) requires the {@link HasWidgets} interface.
  8276. *
  8277. * @return never
  8278. * @throws UnsupportedOperationException
  8279. * always
  8280. */
  8281. @Override
  8282. @Deprecated
  8283. public Iterator<Widget> iterator() {
  8284. throw new UnsupportedOperationException(
  8285. "Cannot iterate through widgets in Grid this way");
  8286. }
  8287. /**
  8288. * Grid does not support removing Widgets this way.
  8289. * <p>
  8290. * This method is implemented only because removing widgets from Grid (added
  8291. * via e.g. {@link Renderer}s) requires the {@link HasWidgets} interface.
  8292. *
  8293. * @return always <code>false</code>
  8294. */
  8295. @Override
  8296. @Deprecated
  8297. public boolean remove(Widget w) {
  8298. /*
  8299. * This is the method that is the sole reason to have Grid implement
  8300. * HasWidget - when Vaadin removes a Component from the hierarchy, the
  8301. * corresponding Widget will call removeFromParent() on itself. GWT will
  8302. * check there that its parent (i.e. Grid) implements HasWidgets, and
  8303. * will call this remove(Widget) method.
  8304. *
  8305. * tl;dr: all this song and dance to make sure GWT's sanity checks
  8306. * aren't triggered, even though they effectively do nothing interesting
  8307. * from Grid's perspective.
  8308. */
  8309. return false;
  8310. }
  8311. /**
  8312. * Accesses the package private method Widget#setParent()
  8313. *
  8314. * @param widget
  8315. * The widget to access
  8316. * @param parent
  8317. * The parent to set
  8318. */
  8319. private static final native void setParent(Widget widget, Grid<?> parent)
  8320. /*-{
  8321. widget.@com.google.gwt.user.client.ui.Widget::setParent(Lcom/google/gwt/user/client/ui/Widget;)(parent);
  8322. }-*/;
  8323. private static final native void onAttach(Widget widget)
  8324. /*-{
  8325. widget.@Widget::onAttach()();
  8326. }-*/;
  8327. private static final native void onDetach(Widget widget)
  8328. /*-{
  8329. widget.@Widget::onDetach()();
  8330. }-*/;
  8331. @Override
  8332. protected void doAttachChildren() {
  8333. if (sidebar.getParent() == this) {
  8334. onAttach(sidebar);
  8335. }
  8336. }
  8337. @Override
  8338. protected void doDetachChildren() {
  8339. if (sidebar.getParent() == this) {
  8340. onDetach(sidebar);
  8341. }
  8342. }
  8343. private void attachWidget(Widget w, Element parent) {
  8344. assert w.getParent() == null;
  8345. parent.appendChild(w.getElement());
  8346. setParent(w, this);
  8347. }
  8348. private void detachWidget(Widget w) {
  8349. assert w.getParent() == this;
  8350. setParent(w, null);
  8351. w.getElement().removeFromParent();
  8352. }
  8353. /**
  8354. * Resets all cached pixel sizes and reads new values from the DOM. This
  8355. * methods should be used e.g. when styles affecting the dimensions of
  8356. * elements in this grid have been changed.
  8357. */
  8358. public void resetSizesFromDom() {
  8359. getEscalator().resetSizesFromDom();
  8360. }
  8361. /**
  8362. * Sets a new details generator for row details.
  8363. * <p>
  8364. * The currently opened row details will be re-rendered.
  8365. *
  8366. * @since 7.5.0
  8367. * @param detailsGenerator
  8368. * the details generator to set
  8369. * @throws IllegalArgumentException
  8370. * if detailsGenerator is <code>null</code>;
  8371. */
  8372. public void setDetailsGenerator(DetailsGenerator detailsGenerator)
  8373. throws IllegalArgumentException {
  8374. if (detailsGenerator == null) {
  8375. throw new IllegalArgumentException(
  8376. "Details generator may not be null");
  8377. }
  8378. for (Integer index : visibleDetails) {
  8379. setDetailsVisible(index, false);
  8380. }
  8381. this.detailsGenerator = detailsGenerator;
  8382. // this will refresh all visible spacers
  8383. escalator.getBody().setSpacerUpdater(gridSpacerUpdater);
  8384. }
  8385. /**
  8386. * Gets the current details generator for row details.
  8387. *
  8388. * @since 7.5.0
  8389. * @return the detailsGenerator the current details generator
  8390. */
  8391. public DetailsGenerator getDetailsGenerator() {
  8392. return detailsGenerator;
  8393. }
  8394. /**
  8395. * Shows or hides the details for a specific row.
  8396. * <p>
  8397. * This method does nothing if trying to set show already-visible details,
  8398. * or hide already-hidden details.
  8399. *
  8400. * @since 7.5.0
  8401. * @param rowIndex
  8402. * the index of the affected row
  8403. * @param visible
  8404. * <code>true</code> to show the details, or <code>false</code>
  8405. * to hide them
  8406. * @see #isDetailsVisible(int)
  8407. */
  8408. public void setDetailsVisible(int rowIndex, boolean visible) {
  8409. if (DetailsGenerator.NULL.equals(detailsGenerator)) {
  8410. return;
  8411. }
  8412. Integer rowIndexInteger = Integer.valueOf(rowIndex);
  8413. /*
  8414. * We want to prevent opening a details row twice, so any subsequent
  8415. * openings (or closings) of details is a NOOP.
  8416. *
  8417. * When a details row is opened, it is given an arbitrary height
  8418. * (because Escalator requires a height upon opening). Only when it's
  8419. * opened, Escalator will ask the generator to generate a widget, which
  8420. * we then can measure. When measured, we correct the initial height by
  8421. * the original height.
  8422. *
  8423. * Without this check, we would override the measured height, and revert
  8424. * back to the initial, arbitrary, height which would most probably be
  8425. * wrong.
  8426. *
  8427. * see GridSpacerUpdater.init for implementation details.
  8428. *
  8429. * The order of operations isn't entirely stable. Sometimes Escalator
  8430. * knows about the spacer visibility updates first and doesn't need
  8431. * updating again but Grid's visibleDetails set still does.
  8432. */
  8433. boolean isVisible = isDetailsVisible(rowIndex);
  8434. boolean isVisibleInEscalator = escalator.getBody()
  8435. .spacerExists(rowIndex);
  8436. if (visible) {
  8437. if (!isVisibleInEscalator) {
  8438. escalator.getBody().setSpacer(rowIndex,
  8439. DETAILS_ROW_INITIAL_HEIGHT);
  8440. }
  8441. if (!isVisible) {
  8442. visibleDetails.add(rowIndexInteger);
  8443. }
  8444. } else {
  8445. if (isVisibleInEscalator) {
  8446. escalator.getBody().setSpacer(rowIndex, -1);
  8447. }
  8448. if (isVisible) {
  8449. visibleDetails.remove(rowIndexInteger);
  8450. }
  8451. }
  8452. }
  8453. /**
  8454. * Check whether the details for a row is visible or not.
  8455. *
  8456. * @since 7.5.0
  8457. * @param rowIndex
  8458. * the index of the row for which to check details
  8459. * @return <code>true</code> if the details for the given row is visible
  8460. * @see #setDetailsVisible(int, boolean)
  8461. */
  8462. public boolean isDetailsVisible(int rowIndex) {
  8463. return visibleDetails.contains(Integer.valueOf(rowIndex));
  8464. }
  8465. /**
  8466. * Update details row height.
  8467. *
  8468. * @since 8.1.3
  8469. * @param rowIndex
  8470. * the index of the row for which to update details height
  8471. * @param height
  8472. * new height of the details row
  8473. */
  8474. public void setDetailsHeight(int rowIndex, double height) {
  8475. escalator.getBody().setSpacer(rowIndex, height);
  8476. }
  8477. /**
  8478. * Requests that the column widths should be recalculated.
  8479. * <p>
  8480. * The actual recalculation is not necessarily done immediately so you
  8481. * cannot rely on the columns being the correct width after the call
  8482. * returns.
  8483. *
  8484. * @since 7.4.1
  8485. */
  8486. public void recalculateColumnWidths() {
  8487. autoColumnWidthsRecalculator.schedule();
  8488. }
  8489. /**
  8490. * Gets the customizable menu bar that is by default used for toggling
  8491. * column hidability. The application developer is allowed to add their
  8492. * custom items to the end of the menu, but should try to avoid modifying
  8493. * the items in the beginning of the menu that control the column hiding if
  8494. * any columns are marked as hidable. A toggle for opening the menu will be
  8495. * displayed whenever the menu contains at least one item.
  8496. *
  8497. * @since 7.5.0
  8498. * @return the menu bar
  8499. */
  8500. public MenuBar getSidebarMenu() {
  8501. return sidebar.menuBar;
  8502. }
  8503. /**
  8504. * Tests whether the sidebar menu is currently open.
  8505. *
  8506. * @since 7.5.0
  8507. * @see #getSidebarMenu()
  8508. * @return <code>true</code> if the sidebar is open; <code>false</code> if
  8509. * it is closed
  8510. */
  8511. public boolean isSidebarOpen() {
  8512. return sidebar.isOpen();
  8513. }
  8514. /**
  8515. * Sets whether the sidebar menu is open.
  8516. *
  8517. *
  8518. * @since 7.5.0
  8519. * @see #getSidebarMenu()
  8520. * @see #isSidebarOpen()
  8521. * @param sidebarOpen
  8522. * <code>true</code> to open the sidebar; <code>false</code> to
  8523. * close it
  8524. */
  8525. public void setSidebarOpen(boolean sidebarOpen) {
  8526. if (sidebarOpen) {
  8527. sidebar.open();
  8528. } else {
  8529. sidebar.close();
  8530. }
  8531. }
  8532. @Override
  8533. public int getTabIndex() {
  8534. return FocusUtil.getTabIndex(this);
  8535. }
  8536. @Override
  8537. public void setAccessKey(char key) {
  8538. FocusUtil.setAccessKey(this, key);
  8539. }
  8540. @Override
  8541. public void setFocus(boolean focused) {
  8542. FocusUtil.setFocus(this, focused);
  8543. }
  8544. @Override
  8545. public void setTabIndex(int index) {
  8546. FocusUtil.setTabIndex(this, index);
  8547. }
  8548. @Override
  8549. public void focus() {
  8550. setFocus(true);
  8551. }
  8552. /**
  8553. * Sets the buffered editor mode.
  8554. *
  8555. * @since 7.6
  8556. * @param editorBuffered
  8557. * <code>true</code> to enable buffered editor,
  8558. * <code>false</code> to disable it
  8559. */
  8560. public void setEditorBuffered(boolean editorBuffered) {
  8561. editor.setBuffered(editorBuffered);
  8562. }
  8563. /**
  8564. * Gets the buffered editor mode.
  8565. *
  8566. * @since 7.6
  8567. * @return <code>true</code> if buffered editor is enabled,
  8568. * <code>false</code> otherwise
  8569. */
  8570. public boolean isEditorBuffered() {
  8571. return editor.isBuffered();
  8572. }
  8573. /**
  8574. * Returns the {@link EventCellReference} for the latest event fired from
  8575. * this Grid.
  8576. * <p>
  8577. * Note: This cell reference will be updated when firing the next event.
  8578. *
  8579. * @since 7.5
  8580. * @return event cell reference
  8581. */
  8582. public EventCellReference<T> getEventCell() {
  8583. return eventCell;
  8584. }
  8585. /**
  8586. * Returns a CellReference for the cell to which the given element belongs
  8587. * to.
  8588. *
  8589. * @since 7.6
  8590. * @param element
  8591. * Element to find from the cell's content.
  8592. * @return CellReference or <code>null</code> if cell was not found.
  8593. */
  8594. public CellReference<T> getCellReference(Element element) {
  8595. RowContainer container = getEscalator().findRowContainer(element);
  8596. if (container != null) {
  8597. Cell cell = container.getCell(element);
  8598. if (cell != null) {
  8599. EventCellReference<T> cellRef = new EventCellReference<>(this);
  8600. cellRef.set(cell, getSectionFromContainer(container));
  8601. return cellRef;
  8602. }
  8603. }
  8604. return null;
  8605. }
  8606. /**
  8607. * Returns the selection column for the grid if the selection model is of
  8608. * type {@link SelectionModelWithSelectionColumn}.
  8609. *
  8610. * @return the select all checkbox, or an empty optional if not in use
  8611. */
  8612. public Optional<SelectionColumn> getSelectionColumn() {
  8613. return Optional.ofNullable(selectionColumn);
  8614. }
  8615. }