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.

Table.java 220KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560
  1. /*
  2. * Copyright 2000-2016 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.v7.ui;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.Iterator;
  25. import java.util.LinkedHashMap;
  26. import java.util.LinkedHashSet;
  27. import java.util.LinkedList;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Set;
  31. import java.util.StringTokenizer;
  32. import java.util.logging.Level;
  33. import java.util.logging.Logger;
  34. import org.jsoup.nodes.Element;
  35. import org.jsoup.select.Elements;
  36. import com.vaadin.event.Action;
  37. import com.vaadin.event.Action.Handler;
  38. import com.vaadin.event.ContextClickEvent;
  39. import com.vaadin.event.MouseEvents.ClickEvent;
  40. import com.vaadin.event.dd.DragAndDropEvent;
  41. import com.vaadin.event.dd.DragSource;
  42. import com.vaadin.event.dd.DropHandler;
  43. import com.vaadin.event.dd.DropTarget;
  44. import com.vaadin.event.dd.acceptcriteria.ServerSideCriterion;
  45. import com.vaadin.server.KeyMapper;
  46. import com.vaadin.server.LegacyCommunicationManager;
  47. import com.vaadin.server.LegacyPaint;
  48. import com.vaadin.server.PaintException;
  49. import com.vaadin.server.PaintTarget;
  50. import com.vaadin.server.Resource;
  51. import com.vaadin.shared.MouseEventDetails;
  52. import com.vaadin.shared.ui.MultiSelectMode;
  53. import com.vaadin.shared.util.SharedUtil;
  54. import com.vaadin.ui.Component;
  55. import com.vaadin.ui.HasChildMeasurementHint;
  56. import com.vaadin.ui.HasComponents;
  57. import com.vaadin.ui.UniqueSerializable;
  58. import com.vaadin.ui.declarative.DesignAttributeHandler;
  59. import com.vaadin.ui.declarative.DesignContext;
  60. import com.vaadin.ui.declarative.DesignException;
  61. import com.vaadin.ui.declarative.DesignFormatter;
  62. import com.vaadin.util.ReflectTools;
  63. import com.vaadin.v7.data.Container;
  64. import com.vaadin.v7.data.Item;
  65. import com.vaadin.v7.data.Property;
  66. import com.vaadin.v7.data.util.ContainerOrderedWrapper;
  67. import com.vaadin.v7.data.util.IndexedContainer;
  68. import com.vaadin.v7.data.util.converter.Converter;
  69. import com.vaadin.v7.data.util.converter.ConverterUtil;
  70. import com.vaadin.v7.event.DataBoundTransferable;
  71. import com.vaadin.v7.event.ItemClickEvent;
  72. import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
  73. import com.vaadin.v7.event.ItemClickEvent.ItemClickNotifier;
  74. import com.vaadin.v7.shared.ui.table.CollapseMenuContent;
  75. import com.vaadin.v7.shared.ui.table.TableConstants;
  76. import com.vaadin.v7.shared.ui.table.TableConstants.Section;
  77. import com.vaadin.v7.shared.ui.table.TableServerRpc;
  78. import com.vaadin.v7.shared.ui.table.TableState;
  79. /**
  80. * <p>
  81. * <code>Table</code> is used for representing data or components in a pageable
  82. * and selectable table.
  83. * </p>
  84. *
  85. * <p>
  86. * Scalability of the Table is largely dictated by the container. A table does
  87. * not have a limit for the number of items and is just as fast with hundreds of
  88. * thousands of items as with just a few. The current GWT implementation with
  89. * scrolling however limits the number of rows to around 500000, depending on
  90. * the browser and the pixel height of rows.
  91. * </p>
  92. *
  93. * <p>
  94. * Components in a Table will not have their caption nor icon rendered.
  95. * </p>
  96. *
  97. * @author Vaadin Ltd.
  98. * @since 3.0
  99. */
  100. @Deprecated
  101. @SuppressWarnings({ "deprecation" })
  102. public class Table extends AbstractSelect implements Action.Container,
  103. Container.Ordered, Container.Sortable, ItemClickNotifier, DragSource,
  104. DropTarget, HasComponents, HasChildMeasurementHint {
  105. private transient Logger logger = null;
  106. /**
  107. * Modes that Table support as drag sourse.
  108. */
  109. @Deprecated
  110. public enum TableDragMode {
  111. /**
  112. * Table does not start drag and drop events. HTM5 style events started
  113. * by browser may still happen.
  114. */
  115. NONE,
  116. /**
  117. * Table starts drag with a one row only.
  118. */
  119. ROW,
  120. /**
  121. * Table drags selected rows, if drag starts on a selected rows. Else it
  122. * starts like in ROW mode. Note, that in Transferable there will still
  123. * be only the row on which the drag started, other dragged rows need to
  124. * be checked from the source Table.
  125. */
  126. MULTIROW
  127. }
  128. protected static final int CELL_KEY = 0;
  129. protected static final int CELL_HEADER = 1;
  130. protected static final int CELL_ICON = 2;
  131. protected static final int CELL_ITEMID = 3;
  132. protected static final int CELL_GENERATED_ROW = 4;
  133. protected static final int CELL_FIRSTCOL = 5;
  134. @Deprecated
  135. public enum Align {
  136. /**
  137. * Left column alignment. <b>This is the default behaviour. </b>
  138. */
  139. LEFT("b"),
  140. /**
  141. * Center column alignment.
  142. */
  143. CENTER("c"),
  144. /**
  145. * Right column alignment.
  146. */
  147. RIGHT("e");
  148. private String alignment;
  149. private Align(String alignment) {
  150. this.alignment = alignment;
  151. }
  152. @Override
  153. public String toString() {
  154. return alignment;
  155. }
  156. public Align convertStringToAlign(String string) {
  157. if (string == null) {
  158. return null;
  159. }
  160. if (string.equals("b")) {
  161. return Align.LEFT;
  162. } else if (string.equals("c")) {
  163. return Align.CENTER;
  164. } else if (string.equals("e")) {
  165. return Align.RIGHT;
  166. } else {
  167. return null;
  168. }
  169. }
  170. }
  171. /**
  172. * @deprecated As of 7.0, use {@link Align#LEFT} instead
  173. */
  174. @Deprecated
  175. public static final Align ALIGN_LEFT = Align.LEFT;
  176. /**
  177. * @deprecated As of 7.0, use {@link Align#CENTER} instead
  178. */
  179. @Deprecated
  180. public static final Align ALIGN_CENTER = Align.CENTER;
  181. /**
  182. * @deprecated As of 7.0, use {@link Align#RIGHT} instead
  183. */
  184. @Deprecated
  185. public static final Align ALIGN_RIGHT = Align.RIGHT;
  186. @Deprecated
  187. public enum ColumnHeaderMode {
  188. /**
  189. * Column headers are hidden.
  190. */
  191. HIDDEN,
  192. /**
  193. * Property ID:s are used as column headers.
  194. */
  195. ID,
  196. /**
  197. * Column headers are explicitly specified with
  198. * {@link #setColumnHeaders(String[])}.
  199. */
  200. EXPLICIT,
  201. /**
  202. * Column headers are explicitly specified with
  203. * {@link #setColumnHeaders(String[])}. If a header is not specified for
  204. * a given property, its property id is used instead.
  205. * <p>
  206. * <b>This is the default behavior. </b>
  207. */
  208. EXPLICIT_DEFAULTS_ID
  209. }
  210. /**
  211. * @deprecated As of 7.0, use {@link ColumnHeaderMode#HIDDEN} instead
  212. */
  213. @Deprecated
  214. public static final ColumnHeaderMode COLUMN_HEADER_MODE_HIDDEN = ColumnHeaderMode.HIDDEN;
  215. /**
  216. * @deprecated As of 7.0, use {@link ColumnHeaderMode#ID} instead
  217. */
  218. @Deprecated
  219. public static final ColumnHeaderMode COLUMN_HEADER_MODE_ID = ColumnHeaderMode.ID;
  220. /**
  221. * @deprecated As of 7.0, use {@link ColumnHeaderMode#EXPLICIT} instead
  222. */
  223. @Deprecated
  224. public static final ColumnHeaderMode COLUMN_HEADER_MODE_EXPLICIT = ColumnHeaderMode.EXPLICIT;
  225. /**
  226. * @deprecated As of 7.0, use {@link ColumnHeaderMode#EXPLICIT_DEFAULTS_ID}
  227. * instead
  228. */
  229. @Deprecated
  230. public static final ColumnHeaderMode COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID = ColumnHeaderMode.EXPLICIT_DEFAULTS_ID;
  231. @Deprecated
  232. public enum RowHeaderMode {
  233. /**
  234. * Row caption mode: The row headers are hidden. <b>This is the default
  235. * mode. </b>
  236. */
  237. HIDDEN(null),
  238. /**
  239. * Row caption mode: Items Id-objects toString is used as row caption.
  240. */
  241. ID(ItemCaptionMode.ID),
  242. /**
  243. * Row caption mode: Item-objects toString is used as row caption.
  244. */
  245. ITEM(ItemCaptionMode.ITEM),
  246. /**
  247. * Row caption mode: Index of the item is used as item caption. The
  248. * index mode can only be used with the containers implementing the
  249. * {@link com.vaadin.data.Container.Indexed} interface.
  250. */
  251. INDEX(ItemCaptionMode.INDEX),
  252. /**
  253. * Row caption mode: Item captions are explicitly specified, but if the
  254. * caption is missing, the item id objects <code>toString()</code> is
  255. * used instead.
  256. */
  257. EXPLICIT_DEFAULTS_ID(ItemCaptionMode.EXPLICIT_DEFAULTS_ID),
  258. /**
  259. * Row caption mode: Item captions are explicitly specified.
  260. */
  261. EXPLICIT(ItemCaptionMode.EXPLICIT),
  262. /**
  263. * Row caption mode: Only icons are shown, the captions are hidden.
  264. */
  265. ICON_ONLY(ItemCaptionMode.ICON_ONLY),
  266. /**
  267. * Row caption mode: Item captions are read from property specified with
  268. * {@link #setItemCaptionPropertyId(Object)} .
  269. */
  270. PROPERTY(ItemCaptionMode.PROPERTY);
  271. ItemCaptionMode mode;
  272. private RowHeaderMode(ItemCaptionMode mode) {
  273. this.mode = mode;
  274. }
  275. public ItemCaptionMode getItemCaptionMode() {
  276. return mode;
  277. }
  278. }
  279. /**
  280. * @deprecated As of 7.0, use {@link RowHeaderMode#HIDDEN} instead
  281. */
  282. @Deprecated
  283. public static final RowHeaderMode ROW_HEADER_MODE_HIDDEN = RowHeaderMode.HIDDEN;
  284. /**
  285. * @deprecated As of 7.0, use {@link RowHeaderMode#ID} instead
  286. */
  287. @Deprecated
  288. public static final RowHeaderMode ROW_HEADER_MODE_ID = RowHeaderMode.ID;
  289. /**
  290. * @deprecated As of 7.0, use {@link RowHeaderMode#ITEM} instead
  291. */
  292. @Deprecated
  293. public static final RowHeaderMode ROW_HEADER_MODE_ITEM = RowHeaderMode.ITEM;
  294. /**
  295. * @deprecated As of 7.0, use {@link RowHeaderMode#INDEX} instead
  296. */
  297. @Deprecated
  298. public static final RowHeaderMode ROW_HEADER_MODE_INDEX = RowHeaderMode.INDEX;
  299. /**
  300. * @deprecated As of 7.0, use {@link RowHeaderMode#EXPLICIT_DEFAULTS_ID}
  301. * instead
  302. */
  303. @Deprecated
  304. public static final RowHeaderMode ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID = RowHeaderMode.EXPLICIT_DEFAULTS_ID;
  305. /**
  306. * @deprecated As of 7.0, use {@link RowHeaderMode#EXPLICIT} instead
  307. */
  308. @Deprecated
  309. public static final RowHeaderMode ROW_HEADER_MODE_EXPLICIT = RowHeaderMode.EXPLICIT;
  310. /**
  311. * @deprecated As of 7.0, use {@link RowHeaderMode#ICON_ONLY} instead
  312. */
  313. @Deprecated
  314. public static final RowHeaderMode ROW_HEADER_MODE_ICON_ONLY = RowHeaderMode.ICON_ONLY;
  315. /**
  316. * @deprecated As of 7.0, use {@link RowHeaderMode#PROPERTY} instead
  317. */
  318. @Deprecated
  319. public static final RowHeaderMode ROW_HEADER_MODE_PROPERTY = RowHeaderMode.PROPERTY;
  320. /**
  321. * The default rate that table caches rows for smooth scrolling.
  322. */
  323. private static final double CACHE_RATE_DEFAULT = 2;
  324. private static final String ROW_HEADER_COLUMN_KEY = "0";
  325. private static final Object ROW_HEADER_FAKE_PROPERTY_ID = new UniqueSerializable() {
  326. };
  327. /**
  328. * How layout manager should behave when measuring Table's child components
  329. */
  330. private ChildMeasurementHint childMeasurementHint = ChildMeasurementHint.MEASURE_ALWAYS;
  331. /* Private table extensions to Select */
  332. /**
  333. * True if column collapsing is allowed.
  334. */
  335. private boolean columnCollapsingAllowed = false;
  336. /**
  337. * True if reordering of columns is allowed on the client side.
  338. */
  339. private boolean columnReorderingAllowed = false;
  340. /**
  341. * Keymapper for column ids.
  342. */
  343. private final KeyMapper<Object> columnIdMap = new KeyMapper<>();
  344. /**
  345. * Holds visible column propertyIds - in order.
  346. */
  347. private LinkedList<Object> visibleColumns = new LinkedList<>();
  348. /**
  349. * Holds noncollapsible columns.
  350. */
  351. private HashSet<Object> noncollapsibleColumns = new HashSet<>();
  352. /**
  353. * Holds propertyIds of currently collapsed columns.
  354. */
  355. private final HashSet<Object> collapsedColumns = new HashSet<>();
  356. /**
  357. * Holds headers for visible columns (by propertyId).
  358. */
  359. private final HashMap<Object, String> columnHeaders = new HashMap<>();
  360. /**
  361. * Holds footers for visible columns (by propertyId).
  362. */
  363. private final HashMap<Object, String> columnFooters = new HashMap<>();
  364. /**
  365. * Holds icons for visible columns (by propertyId).
  366. */
  367. private final HashMap<Object, Resource> columnIcons = new HashMap<>();
  368. /**
  369. * Holds alignments for visible columns (by propertyId).
  370. */
  371. private HashMap<Object, Align> columnAlignments = new HashMap<>();
  372. /**
  373. * Holds column widths in pixels for visible columns (by propertyId).
  374. */
  375. private final HashMap<Object, Integer> columnWidths = new HashMap<>();
  376. /**
  377. * Holds column expand rations for visible columns (by propertyId).
  378. */
  379. private final HashMap<Object, Float> columnExpandRatios = new HashMap<>();
  380. /**
  381. * Holds column generators
  382. */
  383. private final HashMap<Object, ColumnGenerator> columnGenerators = new LinkedHashMap<>();
  384. /**
  385. * Holds value of property pageLength. 0 disables paging.
  386. */
  387. private int pageLength = 15;
  388. /**
  389. * Id the first item on the current page.
  390. */
  391. private Object currentPageFirstItemId = null;
  392. /*
  393. * If all rows get removed then scroll position of the previous container
  394. * can be restored after re-adding/replacing rows via addAll(). This
  395. * resolves #14581.
  396. */
  397. private int repairOnReAddAllRowsDataScrollPositionItemIndex = -1;
  398. /**
  399. * Index of the first item on the current page.
  400. */
  401. private int currentPageFirstItemIndex = 0;
  402. /**
  403. * Index of the "first" item on the last page if a user has used
  404. * setCurrentPageFirstItemIndex to scroll down. -1 if not set.
  405. */
  406. private int currentPageFirstItemIndexOnLastPage = -1;
  407. /**
  408. * Holds value of property selectable.
  409. */
  410. private Boolean selectable;
  411. /**
  412. * Holds value of property columnHeaderMode.
  413. */
  414. private ColumnHeaderMode columnHeaderMode = ColumnHeaderMode.EXPLICIT_DEFAULTS_ID;
  415. /**
  416. * Holds value of property rowHeaderMode.
  417. */
  418. private RowHeaderMode rowHeaderMode = RowHeaderMode.EXPLICIT_DEFAULTS_ID;
  419. /**
  420. * Should the Table footer be visible?
  421. */
  422. private boolean columnFootersVisible = false;
  423. /**
  424. * Page contents buffer used in buffered mode.
  425. */
  426. private Object[][] pageBuffer = null;
  427. /**
  428. * Set of properties listened - the list is kept to release the listeners
  429. * later.
  430. */
  431. private HashSet<Property<?>> listenedProperties = null;
  432. /**
  433. * Set of visible components - the is used for needsRepaint calculation.
  434. */
  435. private HashSet<Component> visibleComponents = null;
  436. /**
  437. * List of action handlers.
  438. */
  439. private LinkedList<Handler> actionHandlers = null;
  440. /**
  441. * Action mapper.
  442. */
  443. private KeyMapper<Action> actionMapper = null;
  444. /**
  445. * Table cell editor factory.
  446. */
  447. private TableFieldFactory fieldFactory = DefaultFieldFactory.get();
  448. /**
  449. * Is table editable.
  450. */
  451. private boolean editable = false;
  452. /**
  453. * Current sorting direction.
  454. */
  455. private boolean sortAscending = true;
  456. /**
  457. * Currently table is sorted on this propertyId.
  458. */
  459. private Object sortContainerPropertyId = null;
  460. /**
  461. * Is table sorting by the user enabled.
  462. */
  463. private boolean sortEnabled = true;
  464. /**
  465. * Number of rows explicitly requested by the client to be painted on next
  466. * paint. This is -1 if no request by the client is made. Painting the
  467. * component will automatically reset this to -1.
  468. */
  469. private int reqRowsToPaint = -1;
  470. /**
  471. * Index of the first rows explicitly requested by the client to be painted.
  472. * This is -1 if no request by the client is made. Painting the component
  473. * will automatically reset this to -1.
  474. */
  475. private int reqFirstRowToPaint = -1;
  476. private int firstToBeRenderedInClient = -1;
  477. private int lastToBeRenderedInClient = -1;
  478. private boolean isContentRefreshesEnabled = true;
  479. private int pageBufferFirstIndex;
  480. private boolean containerChangeToBeRendered = false;
  481. /**
  482. * Table cell specific style generator
  483. */
  484. private CellStyleGenerator cellStyleGenerator = null;
  485. /**
  486. * Table cell specific tooltip generator
  487. */
  488. private ItemDescriptionGenerator itemDescriptionGenerator;
  489. /*
  490. * EXPERIMENTAL feature: will tell the client to re-calculate column widths
  491. * if set to true. Currently no setter: extend to enable.
  492. */
  493. protected boolean alwaysRecalculateColumnWidths = false;
  494. private double cacheRate = CACHE_RATE_DEFAULT;
  495. private TableDragMode dragMode = TableDragMode.NONE;
  496. private DropHandler dropHandler;
  497. private MultiSelectMode multiSelectMode = MultiSelectMode.DEFAULT;
  498. private boolean rowCacheInvalidated;
  499. private RowGenerator rowGenerator = null;
  500. private final Map<Field<?>, Property<?>> associatedProperties = new HashMap<>();
  501. private boolean painted = false;
  502. private HashMap<Object, Converter<String, Object>> propertyValueConverters = new HashMap<>();
  503. /**
  504. * Set to true if the client-side should be informed that the key mapper has
  505. * been reset so it can avoid sending back references to keys that are no
  506. * longer present.
  507. */
  508. private boolean keyMapperReset;
  509. private List<Throwable> exceptionsDuringCachePopulation = new ArrayList<>();
  510. private boolean isBeingPainted;
  511. /* Table constructors */
  512. /**
  513. * Creates a new empty table.
  514. */
  515. public Table() {
  516. setRowHeaderMode(ROW_HEADER_MODE_HIDDEN);
  517. registerRpc(new TableServerRpc() {
  518. @Override
  519. public void contextClick(String rowKey, String colKey,
  520. Section section, MouseEventDetails details) {
  521. Object itemId = itemIdMapper.get(rowKey);
  522. Object propertyId = columnIdMap.get(colKey);
  523. fireEvent(new TableContextClickEvent(Table.this, details,
  524. itemId, propertyId, section));
  525. }
  526. });
  527. }
  528. /**
  529. * Creates a new empty table with caption.
  530. *
  531. * @param caption
  532. */
  533. public Table(String caption) {
  534. this();
  535. setCaption(caption);
  536. }
  537. /**
  538. * Creates a new table with caption and connect it to a Container.
  539. *
  540. * @param caption
  541. * @param dataSource
  542. */
  543. public Table(String caption, Container dataSource) {
  544. this();
  545. setCaption(caption);
  546. setContainerDataSource(dataSource);
  547. }
  548. /* Table functionality */
  549. /**
  550. * Gets the array of visible column id:s, including generated columns.
  551. *
  552. * <p>
  553. * The columns are show in the order of their appearance in this array.
  554. * </p>
  555. *
  556. * @return an array of currently visible propertyIds and generated column
  557. * ids.
  558. */
  559. public Object[] getVisibleColumns() {
  560. if (visibleColumns == null) {
  561. return null;
  562. }
  563. return visibleColumns.toArray();
  564. }
  565. /**
  566. * Sets the array of visible column property id:s.
  567. *
  568. * <p>
  569. * The columns are show in the order of their appearance in this array.
  570. * </p>
  571. *
  572. * @param visibleColumns
  573. * the Array of shown property id:s.
  574. */
  575. public void setVisibleColumns(Object... visibleColumns) {
  576. // Visible columns must exist
  577. if (visibleColumns == null) {
  578. throw new NullPointerException(
  579. "Can not set visible columns to null value");
  580. }
  581. final LinkedList<Object> newVC = new LinkedList<>();
  582. // Checks that the new visible columns contains no nulls, properties
  583. // exist and that there are no duplicates before adding them to newVC.
  584. final Collection<?> properties = getContainerPropertyIds();
  585. for (int i = 0; i < visibleColumns.length; i++) {
  586. if (visibleColumns[i] == null) {
  587. throw new NullPointerException("Ids must be non-nulls");
  588. } else if (!properties.contains(visibleColumns[i])
  589. && !columnGenerators.containsKey(visibleColumns[i])) {
  590. throw new IllegalArgumentException(
  591. "Ids must exist in the Container or as a generated column, missing id: "
  592. + visibleColumns[i]);
  593. } else if (newVC.contains(visibleColumns[i])) {
  594. throw new IllegalArgumentException(
  595. "Ids must be unique, duplicate id: "
  596. + visibleColumns[i]);
  597. } else {
  598. newVC.add(visibleColumns[i]);
  599. }
  600. }
  601. this.visibleColumns = newVC;
  602. // Assures visual refresh
  603. refreshRowCache();
  604. }
  605. /**
  606. * Gets the headers of the columns.
  607. *
  608. * <p>
  609. * The headers match the property id:s given by the set visible column
  610. * headers. The table must be set in either
  611. * {@link #COLUMN_HEADER_MODE_EXPLICIT} or
  612. * {@link #COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID} mode to show the
  613. * headers. In the defaults mode any nulls in the headers array are replaced
  614. * with id.toString().
  615. * </p>
  616. *
  617. * @return the Array of column headers.
  618. */
  619. public String[] getColumnHeaders() {
  620. if (columnHeaders == null) {
  621. return null;
  622. }
  623. final String[] headers = new String[visibleColumns.size()];
  624. int i = 0;
  625. for (final Iterator<Object> it = visibleColumns.iterator(); it
  626. .hasNext(); i++) {
  627. headers[i] = getColumnHeader(it.next());
  628. }
  629. return headers;
  630. }
  631. /**
  632. * Sets the headers of the columns.
  633. *
  634. * <p>
  635. * The headers match the property id:s given by the set visible column
  636. * headers. The table must be set in either
  637. * {@link #COLUMN_HEADER_MODE_EXPLICIT} or
  638. * {@link #COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID} mode to show the
  639. * headers. In the defaults mode any nulls in the headers array are replaced
  640. * with id.toString() outputs when rendering.
  641. * </p>
  642. *
  643. * @param columnHeaders
  644. * the Array of column headers that match the
  645. * {@link #getVisibleColumns()} method.
  646. */
  647. public void setColumnHeaders(String... columnHeaders) {
  648. if (columnHeaders.length != visibleColumns.size()) {
  649. throw new IllegalArgumentException(
  650. "The length of the headers array must match the number of visible columns");
  651. }
  652. this.columnHeaders.clear();
  653. int i = 0;
  654. for (final Iterator<Object> it = visibleColumns.iterator(); it.hasNext()
  655. && i < columnHeaders.length; i++) {
  656. this.columnHeaders.put(it.next(), columnHeaders[i]);
  657. }
  658. markAsDirty();
  659. }
  660. /**
  661. * Gets the icons of the columns.
  662. *
  663. * <p>
  664. * The icons in headers match the property id:s given by the set visible
  665. * column headers. The table must be set in either
  666. * {@link #COLUMN_HEADER_MODE_EXPLICIT} or
  667. * {@link #COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID} mode to show the headers
  668. * with icons.
  669. * </p>
  670. *
  671. * @return the Array of icons that match the {@link #getVisibleColumns()}.
  672. */
  673. public Resource[] getColumnIcons() {
  674. if (columnIcons == null) {
  675. return null;
  676. }
  677. final Resource[] icons = new Resource[visibleColumns.size()];
  678. int i = 0;
  679. for (final Iterator<Object> it = visibleColumns.iterator(); it
  680. .hasNext(); i++) {
  681. icons[i] = columnIcons.get(it.next());
  682. }
  683. return icons;
  684. }
  685. /**
  686. * Sets the icons of the columns.
  687. *
  688. * <p>
  689. * The icons in headers match the property id:s given by the set visible
  690. * column headers. The table must be set in either
  691. * {@link #COLUMN_HEADER_MODE_EXPLICIT} or
  692. * {@link #COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID} mode to show the headers
  693. * with icons.
  694. * </p>
  695. *
  696. * @param columnIcons
  697. * the Array of icons that match the {@link #getVisibleColumns()}
  698. * .
  699. */
  700. public void setColumnIcons(Resource... columnIcons) {
  701. if (columnIcons.length != visibleColumns.size()) {
  702. throw new IllegalArgumentException(
  703. "The length of the icons array must match the number of visible columns");
  704. }
  705. this.columnIcons.clear();
  706. int i = 0;
  707. for (final Iterator<Object> it = visibleColumns.iterator(); it.hasNext()
  708. && i < columnIcons.length; i++) {
  709. this.columnIcons.put(it.next(), columnIcons[i]);
  710. }
  711. markAsDirty();
  712. }
  713. /**
  714. * Gets the array of column alignments.
  715. *
  716. * <p>
  717. * The items in the array must match the properties identified by
  718. * {@link #getVisibleColumns()}. The possible values for the alignments
  719. * include:
  720. * <ul>
  721. * <li>{@link Align#LEFT}: Left alignment</li>
  722. * <li>{@link Align#CENTER}: Centered</li>
  723. * <li>{@link Align#RIGHT}: Right alignment</li>
  724. * </ul>
  725. * The alignments default to {@link Align#LEFT}: any null values are
  726. * rendered as align lefts.
  727. * </p>
  728. *
  729. * @return the Column alignments array.
  730. */
  731. public Align[] getColumnAlignments() {
  732. if (columnAlignments == null) {
  733. return null;
  734. }
  735. final Align[] alignments = new Align[visibleColumns.size()];
  736. int i = 0;
  737. for (final Iterator<Object> it = visibleColumns.iterator(); it
  738. .hasNext(); i++) {
  739. alignments[i] = getColumnAlignment(it.next());
  740. }
  741. return alignments;
  742. }
  743. /**
  744. * Sets the column alignments.
  745. *
  746. * <p>
  747. * The amount of items in the array must match the amount of properties
  748. * identified by {@link #getVisibleColumns()}. The possible values for the
  749. * alignments include:
  750. * <ul>
  751. * <li>{@link Align#LEFT}: Left alignment</li>
  752. * <li>{@link Align#CENTER}: Centered</li>
  753. * <li>{@link Align#RIGHT}: Right alignment</li>
  754. * </ul>
  755. * The alignments default to {@link Align#LEFT}
  756. * </p>
  757. *
  758. * @param columnAlignments
  759. * the Column alignments array.
  760. */
  761. public void setColumnAlignments(Align... columnAlignments) {
  762. if (columnAlignments.length != visibleColumns.size()) {
  763. throw new IllegalArgumentException(
  764. "The length of the alignments array must match the number of visible columns");
  765. }
  766. // Resets the alignments
  767. final HashMap<Object, Align> newCA = new HashMap<>();
  768. int i = 0;
  769. for (final Iterator<Object> it = visibleColumns.iterator(); it.hasNext()
  770. && i < columnAlignments.length; i++) {
  771. newCA.put(it.next(), columnAlignments[i]);
  772. }
  773. this.columnAlignments = newCA;
  774. // Assures the visual refresh. No need to reset the page buffer before
  775. // as the content has not changed, only the alignments.
  776. refreshRenderedCells();
  777. }
  778. /**
  779. * Sets columns width (in pixels). Theme may not necessarily respect very
  780. * small or very big values. Setting width to -1 (default) means that theme
  781. * will make decision of width.
  782. *
  783. * <p>
  784. * Column can either have a fixed width or expand ratio. The latter one set
  785. * is used. See @link {@link #setColumnExpandRatio(Object, float)}.
  786. *
  787. * @param propertyId
  788. * columns property id
  789. * @param width
  790. * width to be reserved for columns content
  791. * @since 4.0.3
  792. */
  793. public void setColumnWidth(Object propertyId, int width) {
  794. if (propertyId == null) {
  795. // Since propertyId is null, this is the row header. Use the magic
  796. // id to store the width of the row header.
  797. propertyId = ROW_HEADER_FAKE_PROPERTY_ID;
  798. }
  799. // Setting column width should remove any expand ratios as well
  800. columnExpandRatios.remove(propertyId);
  801. if (width < 0) {
  802. columnWidths.remove(propertyId);
  803. } else {
  804. columnWidths.put(propertyId, width);
  805. }
  806. markAsDirty();
  807. }
  808. /**
  809. * Sets the column expand ratio for given column.
  810. * <p>
  811. * Expand ratios can be defined to customize the way how excess space is
  812. * divided among columns. Table can have excess space if it has its width
  813. * defined and there is horizontally more space than columns consume
  814. * naturally. Excess space is the space that is not used by columns with
  815. * explicit width (see {@link #setColumnWidth(Object, int)}) or with natural
  816. * width (no width nor expand ratio).
  817. *
  818. * <p>
  819. * By default (without expand ratios) the excess space is divided
  820. * proportionally to columns natural widths.
  821. *
  822. * <p>
  823. * Only expand ratios of visible columns are used in final calculations.
  824. *
  825. * <p>
  826. * Column can either have a fixed width or expand ratio. The latter one set
  827. * is used.
  828. *
  829. * <p>
  830. * A column with expand ratio is considered to be minimum width by default
  831. * (if no excess space exists). The minimum width is defined by terminal
  832. * implementation.
  833. *
  834. * <p>
  835. * If terminal implementation supports re-sizable columns the column becomes
  836. * fixed width column if users resizes the column.
  837. *
  838. * @param propertyId
  839. * columns property id
  840. * @param expandRatio
  841. * the expandRatio used to divide excess space for this column
  842. */
  843. public void setColumnExpandRatio(Object propertyId, float expandRatio) {
  844. if (propertyId == null) {
  845. // Since propertyId is null, this is the row header. Use the magic
  846. // id to store the width of the row header.
  847. propertyId = ROW_HEADER_FAKE_PROPERTY_ID;
  848. }
  849. // Setting the column expand ratio should remove and defined column
  850. // width
  851. columnWidths.remove(propertyId);
  852. if (expandRatio < 0) {
  853. columnExpandRatios.remove(propertyId);
  854. } else {
  855. columnExpandRatios.put(propertyId, expandRatio);
  856. }
  857. requestRepaint();
  858. }
  859. /**
  860. * Gets the column expand ratio for a column. See
  861. * {@link #setColumnExpandRatio(Object, float)}
  862. *
  863. * @param propertyId
  864. * columns property id
  865. * @return the expandRatio used to divide excess space for this column
  866. */
  867. public float getColumnExpandRatio(Object propertyId) {
  868. final Float width = columnExpandRatios.get(propertyId);
  869. if (width == null) {
  870. return -1;
  871. }
  872. return width.floatValue();
  873. }
  874. /**
  875. * Gets the pixel width of column
  876. *
  877. * @param propertyId
  878. * @return width of column or -1 when value not set
  879. */
  880. public int getColumnWidth(Object propertyId) {
  881. if (propertyId == null) {
  882. // Since propertyId is null, this is the row header. Use the magic
  883. // id to retrieve the width of the row header.
  884. propertyId = ROW_HEADER_FAKE_PROPERTY_ID;
  885. }
  886. final Integer width = columnWidths.get(propertyId);
  887. if (width == null) {
  888. return -1;
  889. }
  890. return width.intValue();
  891. }
  892. /**
  893. * Gets the page length.
  894. *
  895. * <p>
  896. * Setting page length 0 disables paging.
  897. * </p>
  898. *
  899. * @return the Length of one page.
  900. */
  901. public int getPageLength() {
  902. return pageLength;
  903. }
  904. /**
  905. * Sets the page length.
  906. *
  907. * <p>
  908. * Setting page length 0 disables paging. The page length defaults to 15.
  909. * </p>
  910. *
  911. * <p>
  912. * If Table has height set ({@link #setHeight(float, Unit)} ) the client
  913. * side may update the page length automatically the correct value.
  914. * </p>
  915. *
  916. * @param pageLength
  917. * the length of one page.
  918. */
  919. public void setPageLength(int pageLength) {
  920. if (pageLength >= 0 && this.pageLength != pageLength) {
  921. this.pageLength = pageLength;
  922. // Assures the visual refresh
  923. refreshRowCache();
  924. }
  925. }
  926. /**
  927. * This method adjusts a possible caching mechanism of table implementation.
  928. *
  929. * <p>
  930. * Table component may fetch and render some rows outside visible area. With
  931. * complex tables (for example containing layouts and components), the
  932. * client side may become unresponsive. Setting the value lower, UI will
  933. * become more responsive. With higher values scrolling in client will hit
  934. * server less frequently.
  935. *
  936. * <p>
  937. * The amount of cached rows will be cacheRate multiplied with pageLength (
  938. * {@link #setPageLength(int)} both below and above visible area..
  939. *
  940. * @param cacheRate
  941. * a value over 0 (fastest rendering time). Higher value will
  942. * cache more rows on server (smoother scrolling). Default value
  943. * is 2.
  944. */
  945. public void setCacheRate(double cacheRate) {
  946. if (cacheRate < 0) {
  947. throw new IllegalArgumentException(
  948. "cacheRate cannot be less than zero");
  949. }
  950. if (this.cacheRate != cacheRate) {
  951. this.cacheRate = cacheRate;
  952. markAsDirty();
  953. }
  954. }
  955. /**
  956. * @see #setCacheRate(double)
  957. *
  958. * @return the current cache rate value
  959. */
  960. public double getCacheRate() {
  961. return cacheRate;
  962. }
  963. /**
  964. * Getter for property currentPageFirstItem.
  965. *
  966. * @return the Value of property currentPageFirstItem.
  967. */
  968. public Object getCurrentPageFirstItemId() {
  969. // Prioritise index over id if indexes are supported
  970. if (items instanceof Container.Indexed) {
  971. final int index = getCurrentPageFirstItemIndex();
  972. Object id = null;
  973. if (index >= 0 && index < size()) {
  974. id = getIdByIndex(index);
  975. }
  976. if (id != null && !id.equals(currentPageFirstItemId)) {
  977. currentPageFirstItemId = id;
  978. }
  979. }
  980. // If there is no item id at all, use the first one
  981. if (currentPageFirstItemId == null) {
  982. currentPageFirstItemId = firstItemId();
  983. }
  984. return currentPageFirstItemId;
  985. }
  986. /**
  987. * Returns the item ID for the item represented by the index given. Assumes
  988. * that the current container implements {@link Container.Indexed}.
  989. *
  990. * See {@link Container.Indexed#getIdByIndex(int)} for more information
  991. * about the exceptions that can be thrown.
  992. *
  993. * @param index
  994. * the index for which the item ID should be fetched
  995. * @return the item ID for the given index
  996. *
  997. * @throws ClassCastException
  998. * if container does not implement {@link Container.Indexed}
  999. * @throws IndexOutOfBoundsException
  1000. * thrown by {@link Container.Indexed#getIdByIndex(int)} if the
  1001. * index is invalid
  1002. */
  1003. protected Object getIdByIndex(int index) {
  1004. return ((Container.Indexed) items).getIdByIndex(index);
  1005. }
  1006. /**
  1007. * Setter for property currentPageFirstItemId.
  1008. *
  1009. * @param currentPageFirstItemId
  1010. * the New value of property currentPageFirstItemId.
  1011. */
  1012. public void setCurrentPageFirstItemId(Object currentPageFirstItemId) {
  1013. // Gets the corresponding index
  1014. int index = -1;
  1015. if (items instanceof Container.Indexed) {
  1016. index = indexOfId(currentPageFirstItemId);
  1017. } else {
  1018. // If the table item container does not have index, we have to
  1019. // calculates the index by hand
  1020. Object id = firstItemId();
  1021. while (id != null && !id.equals(currentPageFirstItemId)) {
  1022. index++;
  1023. id = nextItemId(id);
  1024. }
  1025. if (id == null) {
  1026. index = -1;
  1027. }
  1028. }
  1029. // If the search for item index was successful
  1030. if (index >= 0) {
  1031. /*
  1032. * The table is not capable of displaying an item in the container
  1033. * as the first if there are not enough items following the selected
  1034. * item so the whole table (pagelength) is filled.
  1035. */
  1036. int maxIndex = size() - pageLength;
  1037. if (maxIndex < 0) {
  1038. maxIndex = 0;
  1039. }
  1040. if (index > maxIndex) {
  1041. // Note that we pass index, not maxIndex, letting
  1042. // setCurrentPageFirstItemIndex handle the situation.
  1043. setCurrentPageFirstItemIndex(index);
  1044. return;
  1045. }
  1046. this.currentPageFirstItemId = currentPageFirstItemId;
  1047. currentPageFirstItemIndex = index;
  1048. }
  1049. // Assures the visual refresh
  1050. refreshRowCache();
  1051. }
  1052. protected int indexOfId(Object itemId) {
  1053. return ((Container.Indexed) items).indexOfId(itemId);
  1054. }
  1055. /**
  1056. * Gets the icon Resource for the specified column.
  1057. *
  1058. * @param propertyId
  1059. * the propertyId identifying the column.
  1060. * @return the icon for the specified column; null if the column has no icon
  1061. * set, or if the column is not visible.
  1062. */
  1063. public Resource getColumnIcon(Object propertyId) {
  1064. return columnIcons.get(propertyId);
  1065. }
  1066. /**
  1067. * Sets the icon Resource for the specified column.
  1068. * <p>
  1069. * Throws IllegalArgumentException if the specified column is not visible.
  1070. * </p>
  1071. *
  1072. * @param propertyId
  1073. * the propertyId identifying the column.
  1074. * @param icon
  1075. * the icon Resource to set.
  1076. */
  1077. public void setColumnIcon(Object propertyId, Resource icon) {
  1078. if (icon == null) {
  1079. columnIcons.remove(propertyId);
  1080. } else {
  1081. columnIcons.put(propertyId, icon);
  1082. }
  1083. markAsDirty();
  1084. }
  1085. /**
  1086. * Gets the header for the specified column.
  1087. *
  1088. * @param propertyId
  1089. * the propertyId identifying the column.
  1090. * @return the header for the specified column if it has one.
  1091. */
  1092. public String getColumnHeader(Object propertyId) {
  1093. if (getColumnHeaderMode() == ColumnHeaderMode.HIDDEN) {
  1094. return null;
  1095. }
  1096. String header = columnHeaders.get(propertyId);
  1097. if ((header == null
  1098. && getColumnHeaderMode() == ColumnHeaderMode.EXPLICIT_DEFAULTS_ID)
  1099. || getColumnHeaderMode() == ColumnHeaderMode.ID) {
  1100. header = propertyId.toString();
  1101. }
  1102. return header;
  1103. }
  1104. /**
  1105. * Sets the column header for the specified column;
  1106. *
  1107. * @param propertyId
  1108. * the propertyId identifying the column.
  1109. * @param header
  1110. * the header to set.
  1111. */
  1112. public void setColumnHeader(Object propertyId, String header) {
  1113. if (header == null) {
  1114. columnHeaders.remove(propertyId);
  1115. } else {
  1116. columnHeaders.put(propertyId, header);
  1117. }
  1118. markAsDirty();
  1119. }
  1120. /**
  1121. * Gets the specified column's alignment.
  1122. *
  1123. * @param propertyId
  1124. * the propertyID identifying the column.
  1125. * @return the specified column's alignment if it as one; {@link Align#LEFT}
  1126. * otherwise.
  1127. */
  1128. public Align getColumnAlignment(Object propertyId) {
  1129. final Align a = columnAlignments.get(propertyId);
  1130. return a == null ? Align.LEFT : a;
  1131. }
  1132. /**
  1133. * Sets the specified column's alignment.
  1134. *
  1135. * <p>
  1136. * Throws IllegalArgumentException if the alignment is not one of the
  1137. * following: {@link Align#LEFT}, {@link Align#CENTER} or
  1138. * {@link Align#RIGHT}
  1139. * </p>
  1140. *
  1141. * @param propertyId
  1142. * the propertyID identifying the column.
  1143. * @param alignment
  1144. * the desired alignment.
  1145. */
  1146. public void setColumnAlignment(Object propertyId, Align alignment) {
  1147. if (alignment == null || alignment == Align.LEFT) {
  1148. columnAlignments.remove(propertyId);
  1149. } else {
  1150. columnAlignments.put(propertyId, alignment);
  1151. }
  1152. // Assures the visual refresh. No need to reset the page buffer before
  1153. // as the content has not changed, only the alignments.
  1154. refreshRenderedCells();
  1155. }
  1156. /**
  1157. * Checks if the specified column is collapsed.
  1158. *
  1159. * @param propertyId
  1160. * the propertyID identifying the column.
  1161. * @return true if the column is collapsed; false otherwise;
  1162. */
  1163. public boolean isColumnCollapsed(Object propertyId) {
  1164. return collapsedColumns != null
  1165. && collapsedColumns.contains(propertyId);
  1166. }
  1167. /**
  1168. * Sets whether the specified column is collapsed or not.
  1169. *
  1170. *
  1171. * @param propertyId
  1172. * the propertyID identifying the column.
  1173. * @param collapsed
  1174. * the desired collapsedness.
  1175. * @throws IllegalStateException
  1176. * if column collapsing is not allowed
  1177. * @throws IllegalArgumentException
  1178. * if the property id does not exist
  1179. */
  1180. public void setColumnCollapsed(Object propertyId, boolean collapsed)
  1181. throws IllegalStateException {
  1182. if (!isColumnCollapsingAllowed()) {
  1183. throw new IllegalStateException("Column collapsing not allowed!");
  1184. }
  1185. if (collapsed && noncollapsibleColumns.contains(propertyId)) {
  1186. throw new IllegalStateException("The column is noncollapsible!");
  1187. }
  1188. if (!getContainerPropertyIds().contains(propertyId)
  1189. && !columnGenerators.containsKey(propertyId)) {
  1190. throw new IllegalArgumentException("Property '" + propertyId
  1191. + "' was not found in the container");
  1192. }
  1193. if (collapsed) {
  1194. if (collapsedColumns.add(propertyId)) {
  1195. fireColumnCollapseEvent(propertyId);
  1196. }
  1197. } else {
  1198. if (collapsedColumns.remove(propertyId)) {
  1199. fireColumnCollapseEvent(propertyId);
  1200. }
  1201. }
  1202. // Assures the visual refresh
  1203. refreshRowCache();
  1204. }
  1205. /**
  1206. * Checks if column collapsing is allowed.
  1207. *
  1208. * @return true if columns can be collapsed; false otherwise.
  1209. */
  1210. public boolean isColumnCollapsingAllowed() {
  1211. return columnCollapsingAllowed;
  1212. }
  1213. /**
  1214. * Sets whether column collapsing is allowed or not.
  1215. *
  1216. * @param collapsingAllowed
  1217. * specifies whether column collapsing is allowed.
  1218. */
  1219. public void setColumnCollapsingAllowed(boolean collapsingAllowed) {
  1220. columnCollapsingAllowed = collapsingAllowed;
  1221. if (!collapsingAllowed) {
  1222. collapsedColumns.clear();
  1223. }
  1224. // Assures the visual refresh. No need to reset the page buffer before
  1225. // as the content has not changed, only the alignments.
  1226. refreshRenderedCells();
  1227. }
  1228. /**
  1229. * Sets whether the given column is collapsible. Note that collapsible
  1230. * columns can only be actually collapsed (via UI or with
  1231. * {@link #setColumnCollapsed(Object, boolean) setColumnCollapsed()}) if
  1232. * {@link #isColumnCollapsingAllowed()} is true. By default all columns are
  1233. * collapsible.
  1234. *
  1235. * @param propertyId
  1236. * the propertyID identifying the column.
  1237. * @param collapsible
  1238. * true if the column should be collapsible, false otherwise.
  1239. */
  1240. public void setColumnCollapsible(Object propertyId, boolean collapsible) {
  1241. if (collapsible) {
  1242. noncollapsibleColumns.remove(propertyId);
  1243. } else {
  1244. noncollapsibleColumns.add(propertyId);
  1245. collapsedColumns.remove(propertyId);
  1246. }
  1247. refreshRowCache();
  1248. }
  1249. /**
  1250. * Checks if the given column is collapsible. Note that even if this method
  1251. * returns <code>true</code>, the column can only be actually collapsed (via
  1252. * UI or with {@link #setColumnCollapsed(Object, boolean)
  1253. * setColumnCollapsed()}) if {@link #isColumnCollapsingAllowed()} is also
  1254. * true.
  1255. *
  1256. * @return true if the column can be collapsed; false otherwise.
  1257. */
  1258. public boolean isColumnCollapsible(Object propertyId) {
  1259. return !noncollapsibleColumns.contains(propertyId);
  1260. }
  1261. /**
  1262. * Checks if column reordering is allowed.
  1263. *
  1264. * @return true if columns can be reordered; false otherwise.
  1265. */
  1266. public boolean isColumnReorderingAllowed() {
  1267. return columnReorderingAllowed;
  1268. }
  1269. /**
  1270. * Sets whether column reordering is allowed or not.
  1271. *
  1272. * @param columnReorderingAllowed
  1273. * specifies whether column reordering is allowed.
  1274. */
  1275. public void setColumnReorderingAllowed(boolean columnReorderingAllowed) {
  1276. if (columnReorderingAllowed != this.columnReorderingAllowed) {
  1277. this.columnReorderingAllowed = columnReorderingAllowed;
  1278. markAsDirty();
  1279. }
  1280. }
  1281. /*
  1282. * Arranges visible columns according to given columnOrder. Silently ignores
  1283. * colimnId:s that are not visible columns, and keeps the internal order of
  1284. * visible columns left out of the ordering (trailing). Silently does
  1285. * nothing if columnReordering is not allowed.
  1286. */
  1287. private void setColumnOrder(Object[] columnOrder) {
  1288. if (columnOrder == null || !isColumnReorderingAllowed()) {
  1289. return;
  1290. }
  1291. final LinkedList<Object> newOrder = new LinkedList<>();
  1292. for (int i = 0; i < columnOrder.length; i++) {
  1293. if (columnOrder[i] != null
  1294. && visibleColumns.contains(columnOrder[i])) {
  1295. visibleColumns.remove(columnOrder[i]);
  1296. newOrder.add(columnOrder[i]);
  1297. }
  1298. }
  1299. for (final Iterator<Object> it = visibleColumns.iterator(); it
  1300. .hasNext();) {
  1301. final Object columnId = it.next();
  1302. if (!newOrder.contains(columnId)) {
  1303. newOrder.add(columnId);
  1304. }
  1305. }
  1306. visibleColumns = newOrder;
  1307. // Assure visual refresh
  1308. refreshRowCache();
  1309. }
  1310. /**
  1311. * Getter for property currentPageFirstItem.
  1312. *
  1313. * @return the Value of property currentPageFirstItem.
  1314. */
  1315. public int getCurrentPageFirstItemIndex() {
  1316. return currentPageFirstItemIndex;
  1317. }
  1318. void setCurrentPageFirstItemIndex(int newIndex,
  1319. boolean needsPageBufferReset) {
  1320. if (newIndex < 0) {
  1321. newIndex = 0;
  1322. }
  1323. /*
  1324. * minimize Container.size() calls which may be expensive. For example
  1325. * it may cause sql query.
  1326. */
  1327. final int size = size();
  1328. /*
  1329. * The table is not capable of displaying an item in the container as
  1330. * the first if there are not enough items following the selected item
  1331. * so the whole table (pagelength) is filled.
  1332. */
  1333. int maxIndex = size - pageLength;
  1334. if (maxIndex < 0) {
  1335. maxIndex = 0;
  1336. }
  1337. /*
  1338. * If the new index is on the last page we set the index to be the first
  1339. * item on that last page and make a note of the real index for the
  1340. * client side to be able to move the scroll position to the correct
  1341. * position.
  1342. */
  1343. int indexOnLastPage = -1;
  1344. if (newIndex > maxIndex) {
  1345. indexOnLastPage = newIndex;
  1346. newIndex = maxIndex;
  1347. }
  1348. // Refresh first item id
  1349. if (items instanceof Container.Indexed) {
  1350. try {
  1351. currentPageFirstItemId = getIdByIndex(newIndex);
  1352. } catch (final IndexOutOfBoundsException e) {
  1353. currentPageFirstItemId = null;
  1354. }
  1355. currentPageFirstItemIndex = newIndex;
  1356. if (needsPageBufferReset) {
  1357. /*
  1358. * The flag currentPageFirstItemIndexOnLastPage denotes a user
  1359. * set scrolling position on the last page via
  1360. * setCurrentPageFirstItemIndex() and shouldn't be changed by
  1361. * the table component internally changing the firstvisible item
  1362. * on lazy row fetching. Doing so would make the scrolling
  1363. * position not be updated correctly when the lazy rows are
  1364. * finally rendered.
  1365. */
  1366. boolean isLastRowPossiblyPartiallyVisible = true;
  1367. if (indexOnLastPage != -1) {
  1368. /*
  1369. * If the requested row was greater than maxIndex, the last
  1370. * row should be fully visible (See
  1371. * TestCurrentPageFirstItem).
  1372. */
  1373. isLastRowPossiblyPartiallyVisible = false;
  1374. }
  1375. int extraRows = isLastRowPossiblyPartiallyVisible ? 0 : 1;
  1376. currentPageFirstItemIndexOnLastPage = currentPageFirstItemIndex
  1377. + extraRows;
  1378. } else {
  1379. currentPageFirstItemIndexOnLastPage = -1;
  1380. }
  1381. } else {
  1382. // For containers not supporting indexes, we must iterate the
  1383. // container forwards / backwards
  1384. // next available item forward or backward
  1385. currentPageFirstItemId = firstItemId();
  1386. // Go forwards in the middle of the list (respect borders)
  1387. while (currentPageFirstItemIndex < newIndex
  1388. && !isLastId(currentPageFirstItemId)) {
  1389. currentPageFirstItemIndex++;
  1390. currentPageFirstItemId = nextItemId(currentPageFirstItemId);
  1391. }
  1392. // If we did hit the border
  1393. if (isLastId(currentPageFirstItemId)) {
  1394. currentPageFirstItemIndex = size - 1;
  1395. }
  1396. // Go backwards in the middle of the list (respect borders)
  1397. while (currentPageFirstItemIndex > newIndex
  1398. && !isFirstId(currentPageFirstItemId)) {
  1399. currentPageFirstItemIndex--;
  1400. currentPageFirstItemId = prevItemId(currentPageFirstItemId);
  1401. }
  1402. // If we did hit the border
  1403. if (isFirstId(currentPageFirstItemId)) {
  1404. currentPageFirstItemIndex = 0;
  1405. }
  1406. // Go forwards once more
  1407. while (currentPageFirstItemIndex < newIndex
  1408. && !isLastId(currentPageFirstItemId)) {
  1409. currentPageFirstItemIndex++;
  1410. currentPageFirstItemId = nextItemId(currentPageFirstItemId);
  1411. }
  1412. // If for some reason we do hit border again, override
  1413. // the user index request
  1414. if (isLastId(currentPageFirstItemId)) {
  1415. newIndex = currentPageFirstItemIndex = size - 1;
  1416. }
  1417. }
  1418. if (needsPageBufferReset) {
  1419. // Assures the visual refresh
  1420. refreshRowCache();
  1421. }
  1422. }
  1423. /**
  1424. * Setter for property currentPageFirstItem.
  1425. *
  1426. * @param newIndex
  1427. * the New value of property currentPageFirstItem.
  1428. */
  1429. public void setCurrentPageFirstItemIndex(int newIndex) {
  1430. setCurrentPageFirstItemIndex(newIndex, true);
  1431. }
  1432. /**
  1433. * Returns whether table is selectable.
  1434. *
  1435. * <p>
  1436. * The table is not selectable until it's explicitly set as selectable or at
  1437. * least one {@link ValueChangeListener} is added.
  1438. * </p>
  1439. *
  1440. * @return whether table is selectable.
  1441. */
  1442. public boolean isSelectable() {
  1443. if (selectable == null) {
  1444. return hasListeners(ValueChangeEvent.class);
  1445. }
  1446. return selectable;
  1447. }
  1448. /**
  1449. * Setter for property selectable.
  1450. *
  1451. * <p>
  1452. * The table is not selectable until it's explicitly set as selectable via
  1453. * this method or alternatively at least one {@link ValueChangeListener} is
  1454. * added.
  1455. * </p>
  1456. *
  1457. * @param selectable
  1458. * the New value of property selectable.
  1459. */
  1460. public void setSelectable(boolean selectable) {
  1461. if (!SharedUtil.equals(this.selectable, selectable)) {
  1462. this.selectable = selectable;
  1463. markAsDirty();
  1464. }
  1465. }
  1466. /**
  1467. * Getter for property columnHeaderMode.
  1468. *
  1469. * @return the Value of property columnHeaderMode.
  1470. */
  1471. public ColumnHeaderMode getColumnHeaderMode() {
  1472. return columnHeaderMode;
  1473. }
  1474. /**
  1475. * Setter for property columnHeaderMode.
  1476. *
  1477. * @param columnHeaderMode
  1478. * the New value of property columnHeaderMode.
  1479. */
  1480. public void setColumnHeaderMode(ColumnHeaderMode columnHeaderMode) {
  1481. if (columnHeaderMode == null) {
  1482. throw new IllegalArgumentException(
  1483. "Column header mode can not be null");
  1484. }
  1485. if (columnHeaderMode != this.columnHeaderMode) {
  1486. this.columnHeaderMode = columnHeaderMode;
  1487. markAsDirty();
  1488. }
  1489. }
  1490. /**
  1491. * Refreshes the rows in the internal cache. Only if
  1492. * {@link #resetPageBuffer()} is called before this then all values are
  1493. * guaranteed to be recreated.
  1494. */
  1495. protected void refreshRenderedCells() {
  1496. if (!isAttached()) {
  1497. return;
  1498. }
  1499. if (!isContentRefreshesEnabled) {
  1500. return;
  1501. }
  1502. // Collects the basic facts about the table page
  1503. final int pagelen = getPageLength();
  1504. int rows, totalRows;
  1505. rows = totalRows = size();
  1506. int firstIndex = Math.min(getCurrentPageFirstItemIndex(),
  1507. totalRows - 1);
  1508. if (rows > 0 && firstIndex >= 0) {
  1509. rows -= firstIndex;
  1510. }
  1511. if (pagelen > 0 && pagelen < rows) {
  1512. rows = pagelen;
  1513. }
  1514. // If "to be painted next" variables are set, use them
  1515. if (lastToBeRenderedInClient - firstToBeRenderedInClient > 0) {
  1516. rows = lastToBeRenderedInClient - firstToBeRenderedInClient + 1;
  1517. }
  1518. if (firstToBeRenderedInClient >= 0) {
  1519. if (firstToBeRenderedInClient < totalRows) {
  1520. firstIndex = firstToBeRenderedInClient;
  1521. } else {
  1522. firstIndex = totalRows - 1;
  1523. }
  1524. } else {
  1525. // initial load
  1526. // #8805 send one extra row in the beginning in case a partial
  1527. // row is shown on the UI
  1528. if (firstIndex > 0) {
  1529. firstIndex = firstIndex - 1;
  1530. rows = rows + 1;
  1531. }
  1532. firstToBeRenderedInClient = firstIndex;
  1533. }
  1534. if (totalRows > 0) {
  1535. if (rows + firstIndex > totalRows) {
  1536. rows = totalRows - firstIndex;
  1537. }
  1538. } else {
  1539. rows = 0;
  1540. }
  1541. // Saves the results to internal buffer
  1542. pageBuffer = getVisibleCellsNoCache(firstIndex, rows, true);
  1543. if (rows > 0) {
  1544. pageBufferFirstIndex = firstIndex;
  1545. }
  1546. if (getPageLength() != 0) {
  1547. removeUnnecessaryRows();
  1548. }
  1549. setRowCacheInvalidated(true);
  1550. markAsDirty();
  1551. maybeThrowCacheUpdateExceptions();
  1552. }
  1553. private void maybeThrowCacheUpdateExceptions() {
  1554. if (!exceptionsDuringCachePopulation.isEmpty()) {
  1555. Throwable[] causes = new Throwable[exceptionsDuringCachePopulation
  1556. .size()];
  1557. exceptionsDuringCachePopulation.toArray(causes);
  1558. exceptionsDuringCachePopulation.clear();
  1559. throw new CacheUpdateException(this,
  1560. "Error during Table cache update.", causes);
  1561. }
  1562. }
  1563. /**
  1564. * Exception thrown when one or more exceptions occurred during updating of
  1565. * the Table cache.
  1566. * <p>
  1567. * Contains all exceptions which occurred during the cache update. The first
  1568. * occurred exception is set as the cause of this exception. All occurred
  1569. * exceptions can be accessed using {@link #getCauses()}.
  1570. * </p>
  1571. *
  1572. */
  1573. @Deprecated
  1574. public static class CacheUpdateException extends RuntimeException {
  1575. private Throwable[] causes;
  1576. private Table table;
  1577. public CacheUpdateException(Table table, String message,
  1578. Throwable[] causes) {
  1579. super(maybeSupplementMessage(message, causes.length), causes[0]);
  1580. this.table = table;
  1581. this.causes = causes;
  1582. }
  1583. private static String maybeSupplementMessage(String message,
  1584. int causeCount) {
  1585. if (causeCount > 1) {
  1586. return message + " Additional causes not shown.";
  1587. } else {
  1588. return message;
  1589. }
  1590. }
  1591. /**
  1592. * Returns the cause(s) for this exception
  1593. *
  1594. * @return the exception(s) which caused this exception
  1595. */
  1596. public Throwable[] getCauses() {
  1597. return causes;
  1598. }
  1599. public Table getTable() {
  1600. return table;
  1601. }
  1602. }
  1603. /**
  1604. * Removes rows that fall outside the required cache.
  1605. */
  1606. private void removeUnnecessaryRows() {
  1607. int minPageBufferIndex = getMinPageBufferIndex();
  1608. int maxPageBufferIndex = getMaxPageBufferIndex();
  1609. int maxBufferSize = maxPageBufferIndex - minPageBufferIndex + 1;
  1610. /*
  1611. * Number of rows that were previously cached. This is not necessarily
  1612. * the same as pageLength if we do not have enough rows in the
  1613. * container.
  1614. */
  1615. int currentlyCachedRowCount = pageBuffer[CELL_ITEMID].length;
  1616. if (currentlyCachedRowCount <= maxBufferSize) {
  1617. // removal unnecessary
  1618. return;
  1619. }
  1620. /* Figure out which rows to get rid of. */
  1621. int firstCacheRowToRemoveInPageBuffer = -1;
  1622. if (minPageBufferIndex > pageBufferFirstIndex) {
  1623. firstCacheRowToRemoveInPageBuffer = pageBufferFirstIndex;
  1624. } else if (maxPageBufferIndex < pageBufferFirstIndex
  1625. + currentlyCachedRowCount) {
  1626. firstCacheRowToRemoveInPageBuffer = maxPageBufferIndex + 1;
  1627. }
  1628. if (firstCacheRowToRemoveInPageBuffer
  1629. - pageBufferFirstIndex < currentlyCachedRowCount) {
  1630. /*
  1631. * Unregister all components that fall beyond the cache limits after
  1632. * inserting the new rows.
  1633. */
  1634. unregisterComponentsAndPropertiesInRows(
  1635. firstCacheRowToRemoveInPageBuffer, currentlyCachedRowCount
  1636. - firstCacheRowToRemoveInPageBuffer);
  1637. }
  1638. }
  1639. /**
  1640. * Requests that the Table should be repainted as soon as possible.
  1641. *
  1642. * Note that a {@code Table} does not necessarily repaint its contents when
  1643. * this method has been called. See {@link #refreshRowCache()} for forcing
  1644. * an update of the contents.
  1645. *
  1646. * @deprecated As of 7.0, use {@link #markAsDirty()} instead
  1647. */
  1648. @Deprecated
  1649. @Override
  1650. public void requestRepaint() {
  1651. markAsDirty();
  1652. }
  1653. /**
  1654. * Requests that the Table should be repainted as soon as possible.
  1655. *
  1656. * Note that a {@code Table} does not necessarily repaint its contents when
  1657. * this method has been called. See {@link #refreshRowCache()} for forcing
  1658. * an update of the contents.
  1659. */
  1660. @Override
  1661. public void markAsDirty() {
  1662. // Overridden only for javadoc
  1663. super.markAsDirty();
  1664. }
  1665. @Override
  1666. public void markAsDirtyRecursive() {
  1667. super.markAsDirtyRecursive();
  1668. // Avoid sending a partial repaint (#8714)
  1669. refreshRowCache();
  1670. }
  1671. private void removeRowsFromCacheAndFillBottom(int firstIndex, int rows) {
  1672. int totalCachedRows = pageBuffer[CELL_ITEMID].length;
  1673. int totalRows = size();
  1674. int firstIndexInPageBuffer = firstIndex - pageBufferFirstIndex;
  1675. /*
  1676. * firstIndexInPageBuffer is the first row to be removed. "rows" rows
  1677. * after that should be removed. If the page buffer does not contain
  1678. * that many rows, we only remove the rows that actually are in the page
  1679. * buffer.
  1680. */
  1681. if (firstIndexInPageBuffer + rows > totalCachedRows) {
  1682. rows = totalCachedRows - firstIndexInPageBuffer;
  1683. }
  1684. /*
  1685. * Unregister components that will no longer be in the page buffer to
  1686. * make sure that no components leak.
  1687. */
  1688. unregisterComponentsAndPropertiesInRows(firstIndex, rows);
  1689. /*
  1690. * The number of rows that should be in the cache after this operation
  1691. * is done (pageBuffer currently contains the expanded items).
  1692. */
  1693. int newCachedRowCount = totalCachedRows;
  1694. if (newCachedRowCount + pageBufferFirstIndex > totalRows) {
  1695. newCachedRowCount = totalRows - pageBufferFirstIndex;
  1696. }
  1697. /*
  1698. * The index at which we should render the first row that does not come
  1699. * from the previous page buffer.
  1700. */
  1701. int firstAppendedRowInPageBuffer = totalCachedRows - rows;
  1702. int firstAppendedRow = firstAppendedRowInPageBuffer
  1703. + pageBufferFirstIndex;
  1704. /*
  1705. * Calculate the maximum number of new rows that we can add to the page
  1706. * buffer. Less than the rows we removed if the container does not
  1707. * contain that many items afterwards.
  1708. */
  1709. int maxRowsToRender = (totalRows - firstAppendedRow);
  1710. int rowsToAdd = rows;
  1711. if (rowsToAdd > maxRowsToRender) {
  1712. rowsToAdd = maxRowsToRender;
  1713. }
  1714. Object[][] cells = null;
  1715. if (rowsToAdd > 0) {
  1716. cells = getVisibleCellsNoCache(firstAppendedRow, rowsToAdd, false);
  1717. }
  1718. /*
  1719. * Create the new cache buffer by copying the first rows from the old
  1720. * buffer, moving the following rows upwards and appending more rows if
  1721. * applicable.
  1722. */
  1723. Object[][] newPageBuffer = new Object[pageBuffer.length][newCachedRowCount];
  1724. for (int i = 0; i < pageBuffer.length; i++) {
  1725. for (int row = 0; row < firstIndexInPageBuffer; row++) {
  1726. // Copy the first rows
  1727. newPageBuffer[i][row] = pageBuffer[i][row];
  1728. }
  1729. for (int row = firstIndexInPageBuffer; row < firstAppendedRowInPageBuffer; row++) {
  1730. // Move the rows that were after the expanded rows
  1731. newPageBuffer[i][row] = pageBuffer[i][row + rows];
  1732. }
  1733. for (int row = firstAppendedRowInPageBuffer; row < newCachedRowCount; row++) {
  1734. // Add the newly rendered rows. Only used if rowsToAdd > 0
  1735. // (cells != null)
  1736. newPageBuffer[i][row] = cells[i][row
  1737. - firstAppendedRowInPageBuffer];
  1738. }
  1739. }
  1740. pageBuffer = newPageBuffer;
  1741. }
  1742. private Object[][] getVisibleCellsUpdateCacheRows(int firstIndex,
  1743. int rows) {
  1744. Object[][] cells = getVisibleCellsNoCache(firstIndex, rows, false);
  1745. int cacheIx = firstIndex - pageBufferFirstIndex;
  1746. // update the new rows in the cache.
  1747. int totalCachedRows = pageBuffer[CELL_ITEMID].length;
  1748. int end = Math.min(cacheIx + rows, totalCachedRows);
  1749. for (int ix = cacheIx; ix < end; ix++) {
  1750. for (int i = 0; i < pageBuffer.length; i++) {
  1751. pageBuffer[i][ix] = cells[i][ix - cacheIx];
  1752. }
  1753. }
  1754. return cells;
  1755. }
  1756. /**
  1757. * @param firstIndex
  1758. * The position where new rows should be inserted
  1759. * @param rows
  1760. * The maximum number of rows that should be inserted at position
  1761. * firstIndex. Less rows will be inserted if the page buffer is
  1762. * too small.
  1763. * @return
  1764. */
  1765. private Object[][] getVisibleCellsInsertIntoCache(int firstIndex,
  1766. int rows) {
  1767. getLogger().log(Level.FINEST,
  1768. "Insert {0} rows at index {1} to existing page buffer requested",
  1769. new Object[] { rows, firstIndex });
  1770. int minPageBufferIndex = getMinPageBufferIndex();
  1771. int maxPageBufferIndex = getMaxPageBufferIndex();
  1772. int maxBufferSize = maxPageBufferIndex - minPageBufferIndex + 1;
  1773. if (getPageLength() == 0) {
  1774. // If pageLength == 0 then all rows should be rendered
  1775. maxBufferSize = pageBuffer[CELL_ITEMID].length + rows;
  1776. }
  1777. /*
  1778. * Number of rows that were previously cached. This is not necessarily
  1779. * the same as maxBufferSize.
  1780. */
  1781. int currentlyCachedRowCount = pageBuffer[CELL_ITEMID].length;
  1782. /* If rows > size available in page buffer */
  1783. if (firstIndex + rows - 1 > maxPageBufferIndex) {
  1784. rows = maxPageBufferIndex - firstIndex + 1;
  1785. }
  1786. /*
  1787. * "rows" rows will be inserted at firstIndex. Find out how many old
  1788. * rows fall outside the new buffer so we can unregister components in
  1789. * the cache.
  1790. */
  1791. /*
  1792. * if there are rows before the new pageBuffer limits they must be
  1793. * removed
  1794. */
  1795. int lastCacheRowToRemove = minPageBufferIndex - 1;
  1796. int rowsFromBeginning = lastCacheRowToRemove - pageBufferFirstIndex + 1;
  1797. if (lastCacheRowToRemove >= pageBufferFirstIndex) {
  1798. unregisterComponentsAndPropertiesInRows(pageBufferFirstIndex,
  1799. rowsFromBeginning);
  1800. } else {
  1801. rowsFromBeginning = 0;
  1802. }
  1803. /*
  1804. * the rows that fall outside of the new pageBuffer limits after the new
  1805. * rows are inserted must also be removed
  1806. */
  1807. int firstCacheRowToRemove = firstIndex;
  1808. /*
  1809. * IF there is space remaining in the buffer after the rows have been
  1810. * inserted, we can keep more rows.
  1811. */
  1812. int numberOfOldRowsAfterInsertedRows = Math.min(
  1813. pageBufferFirstIndex + currentlyCachedRowCount + rows,
  1814. maxPageBufferIndex + 1) - (firstIndex + rows - 1);
  1815. if (numberOfOldRowsAfterInsertedRows > 0) {
  1816. firstCacheRowToRemove += numberOfOldRowsAfterInsertedRows;
  1817. }
  1818. int rowsFromAfter = currentlyCachedRowCount
  1819. - (firstCacheRowToRemove - pageBufferFirstIndex);
  1820. if (rowsFromAfter > 0) {
  1821. /*
  1822. * Unregister all components that fall beyond the cache limits after
  1823. * inserting the new rows.
  1824. */
  1825. unregisterComponentsAndPropertiesInRows(firstCacheRowToRemove,
  1826. rowsFromAfter);
  1827. }
  1828. // Calculate the new cache size
  1829. int newCachedRowCount = maxBufferSize;
  1830. if (pageBufferFirstIndex + currentlyCachedRowCount + rows
  1831. - 1 < maxPageBufferIndex) {
  1832. // there aren't enough rows to fill the whole potential -> use what
  1833. // there is
  1834. newCachedRowCount -= maxPageBufferIndex - (pageBufferFirstIndex
  1835. + currentlyCachedRowCount + rows - 1);
  1836. } else if (minPageBufferIndex < pageBufferFirstIndex) {
  1837. newCachedRowCount -= pageBufferFirstIndex - minPageBufferIndex;
  1838. }
  1839. /*
  1840. * calculate the internal location of the new rows within the new cache
  1841. */
  1842. int firstIndexInNewPageBuffer = firstIndex - pageBufferFirstIndex
  1843. - rowsFromBeginning;
  1844. /* Paint the new rows into a separate buffer */
  1845. Object[][] cells = getVisibleCellsNoCache(firstIndex, rows, false);
  1846. /*
  1847. * Create the new cache buffer and fill it with the data from the old
  1848. * buffer as well as the inserted rows.
  1849. */
  1850. Object[][] newPageBuffer = new Object[pageBuffer.length][newCachedRowCount];
  1851. for (int i = 0; i < pageBuffer.length; i++) {
  1852. for (int row = 0; row < firstIndexInNewPageBuffer; row++) {
  1853. // Copy the first rows
  1854. newPageBuffer[i][row] = pageBuffer[i][rowsFromBeginning + row];
  1855. }
  1856. for (int row = firstIndexInNewPageBuffer; row < firstIndexInNewPageBuffer
  1857. + rows; row++) {
  1858. // Copy the newly created rows
  1859. newPageBuffer[i][row] = cells[i][row
  1860. - firstIndexInNewPageBuffer];
  1861. }
  1862. for (int row = firstIndexInNewPageBuffer
  1863. + rows; row < newCachedRowCount; row++) {
  1864. // Move the old rows down below the newly inserted rows
  1865. newPageBuffer[i][row] = pageBuffer[i][rowsFromBeginning + row
  1866. - rows];
  1867. }
  1868. }
  1869. pageBuffer = newPageBuffer;
  1870. pageBufferFirstIndex = Math.max(
  1871. pageBufferFirstIndex + rowsFromBeginning, minPageBufferIndex);
  1872. if (getLogger().isLoggable(Level.FINEST)) {
  1873. getLogger().log(Level.FINEST,
  1874. "Page Buffer now contains {0} rows ({1}-{2})",
  1875. new Object[] { pageBuffer[CELL_ITEMID].length,
  1876. pageBufferFirstIndex, (pageBufferFirstIndex
  1877. + pageBuffer[CELL_ITEMID].length - 1) });
  1878. }
  1879. return cells;
  1880. }
  1881. private int getMaxPageBufferIndex() {
  1882. int total = size();
  1883. if (getPageLength() == 0) {
  1884. // everything is shown at once, no caching
  1885. return total - 1;
  1886. }
  1887. // Page buffer must not become larger than pageLength*cacheRate after
  1888. // the current page
  1889. int maxPageBufferIndex = getCurrentPageFirstItemIndex()
  1890. + (int) (getPageLength() * (1 + getCacheRate()));
  1891. if (shouldHideNullSelectionItem()) {
  1892. --total;
  1893. }
  1894. if (maxPageBufferIndex >= total) {
  1895. maxPageBufferIndex = total - 1;
  1896. }
  1897. return maxPageBufferIndex;
  1898. }
  1899. private int getMinPageBufferIndex() {
  1900. if (getPageLength() == 0) {
  1901. // everything is shown at once, no caching
  1902. return 0;
  1903. }
  1904. // Page buffer must not become larger than pageLength*cacheRate before
  1905. // the current page
  1906. int minPageBufferIndex = getCurrentPageFirstItemIndex()
  1907. - (int) (getPageLength() * getCacheRate());
  1908. if (minPageBufferIndex < 0) {
  1909. minPageBufferIndex = 0;
  1910. }
  1911. return minPageBufferIndex;
  1912. }
  1913. /**
  1914. * Render rows with index "firstIndex" to "firstIndex+rows-1" to a new
  1915. * buffer.
  1916. *
  1917. * Reuses values from the current page buffer if the rows are found there.
  1918. *
  1919. * @param firstIndex
  1920. * @param rows
  1921. * @param replaceListeners
  1922. * @return
  1923. */
  1924. private Object[][] getVisibleCellsNoCache(int firstIndex, int rows,
  1925. boolean replaceListeners) {
  1926. if (getLogger().isLoggable(Level.FINEST)) {
  1927. getLogger().log(Level.FINEST,
  1928. "Render visible cells for rows {0}-{1}",
  1929. new Object[] { firstIndex, (firstIndex + rows - 1) });
  1930. }
  1931. final Object[] colids = getVisibleColumns();
  1932. final int cols = colids.length;
  1933. HashSet<Property<?>> oldListenedProperties = listenedProperties;
  1934. HashSet<Component> oldVisibleComponents = visibleComponents;
  1935. if (replaceListeners) {
  1936. // initialize the listener collections, this should only be done if
  1937. // the entire cache is refreshed (through refreshRenderedCells)
  1938. listenedProperties = new HashSet<>();
  1939. visibleComponents = new HashSet<>();
  1940. }
  1941. Object[][] cells = new Object[cols + CELL_FIRSTCOL][rows];
  1942. if (rows == 0) {
  1943. unregisterPropertiesAndComponents(oldListenedProperties,
  1944. oldVisibleComponents);
  1945. return cells;
  1946. }
  1947. final RowHeaderMode headmode = getRowHeaderMode();
  1948. final boolean[] iscomponent = new boolean[cols];
  1949. for (int i = 0; i < cols; i++) {
  1950. iscomponent[i] = columnGenerators.containsKey(colids[i])
  1951. || Component.class.isAssignableFrom(getType(colids[i]));
  1952. }
  1953. int firstIndexNotInCache;
  1954. if (pageBuffer != null && pageBuffer[CELL_ITEMID].length > 0) {
  1955. firstIndexNotInCache = pageBufferFirstIndex
  1956. + pageBuffer[CELL_ITEMID].length;
  1957. } else {
  1958. firstIndexNotInCache = -1;
  1959. }
  1960. // Creates the page contents
  1961. int filledRows = 0;
  1962. if (items instanceof Container.Indexed) {
  1963. // more efficient implementation for containers supporting access by
  1964. // index
  1965. List<?> itemIds = getItemIds(firstIndex, rows);
  1966. for (int i = 0; i < rows && i < itemIds.size(); i++) {
  1967. Object id = itemIds.get(i);
  1968. if (id == null) {
  1969. throw new IllegalStateException(
  1970. "Null itemId returned from container");
  1971. }
  1972. // Start by parsing the values, id should already be set
  1973. parseItemIdToCells(cells, id, i, firstIndex, headmode, cols,
  1974. colids, firstIndexNotInCache, iscomponent,
  1975. oldListenedProperties);
  1976. filledRows++;
  1977. }
  1978. } else {
  1979. // slow back-up implementation for cases where the container does
  1980. // not support access by index
  1981. // Gets the first item id
  1982. Object id = firstItemId();
  1983. for (int i = 0; i < firstIndex; i++) {
  1984. id = nextItemId(id);
  1985. }
  1986. for (int i = 0; i < rows && id != null; i++) {
  1987. // Start by parsing the values, id should already be set
  1988. parseItemIdToCells(cells, id, i, firstIndex, headmode, cols,
  1989. colids, firstIndexNotInCache, iscomponent,
  1990. oldListenedProperties);
  1991. // Gets the next item id for non indexed container
  1992. id = nextItemId(id);
  1993. filledRows++;
  1994. }
  1995. }
  1996. // Assures that all the rows of the cell-buffer are valid
  1997. if (filledRows != cells[0].length) {
  1998. final Object[][] temp = new Object[cells.length][filledRows];
  1999. for (int i = 0; i < cells.length; i++) {
  2000. for (int j = 0; j < filledRows; j++) {
  2001. temp[i][j] = cells[i][j];
  2002. }
  2003. }
  2004. cells = temp;
  2005. }
  2006. unregisterPropertiesAndComponents(oldListenedProperties,
  2007. oldVisibleComponents);
  2008. return cells;
  2009. }
  2010. protected List<Object> getItemIds(int firstIndex, int rows) {
  2011. return (List<Object>) ((Container.Indexed) items).getItemIds(firstIndex,
  2012. rows);
  2013. }
  2014. /**
  2015. * Update a cache array for a row, register any relevant listeners etc.
  2016. *
  2017. * This is an internal method extracted from
  2018. * {@link #getVisibleCellsNoCache(int, int, boolean)} and should be removed
  2019. * when the Table is rewritten.
  2020. */
  2021. private void parseItemIdToCells(Object[][] cells, Object id, int i,
  2022. int firstIndex, RowHeaderMode headmode, int cols, Object[] colids,
  2023. int firstIndexNotInCache, boolean[] iscomponent,
  2024. HashSet<Property<?>> oldListenedProperties) {
  2025. cells[CELL_ITEMID][i] = id;
  2026. cells[CELL_KEY][i] = itemIdMapper.key(id);
  2027. if (headmode != ROW_HEADER_MODE_HIDDEN) {
  2028. switch (headmode) {
  2029. case INDEX:
  2030. cells[CELL_HEADER][i] = String.valueOf(i + firstIndex + 1);
  2031. break;
  2032. default:
  2033. try {
  2034. cells[CELL_HEADER][i] = getItemCaption(id);
  2035. } catch (Exception e) {
  2036. exceptionsDuringCachePopulation.add(e);
  2037. cells[CELL_HEADER][i] = "";
  2038. }
  2039. }
  2040. try {
  2041. cells[CELL_ICON][i] = getItemIcon(id);
  2042. } catch (Exception e) {
  2043. exceptionsDuringCachePopulation.add(e);
  2044. cells[CELL_ICON][i] = null;
  2045. }
  2046. }
  2047. GeneratedRow generatedRow = rowGenerator != null
  2048. ? rowGenerator.generateRow(this, id) : null;
  2049. cells[CELL_GENERATED_ROW][i] = generatedRow;
  2050. for (int j = 0; j < cols; j++) {
  2051. if (isColumnCollapsed(colids[j])) {
  2052. continue;
  2053. }
  2054. Property<?> p = null;
  2055. Object value = "";
  2056. boolean isGeneratedRow = generatedRow != null;
  2057. boolean isGeneratedColumn = columnGenerators.containsKey(colids[j]);
  2058. boolean isGenerated = isGeneratedRow || isGeneratedColumn;
  2059. if (!isGenerated) {
  2060. try {
  2061. p = getContainerProperty(id, colids[j]);
  2062. } catch (Exception e) {
  2063. exceptionsDuringCachePopulation.add(e);
  2064. value = null;
  2065. }
  2066. }
  2067. if (isGeneratedRow) {
  2068. if (generatedRow.isSpanColumns() && j > 0) {
  2069. value = null;
  2070. } else if (generatedRow.isSpanColumns() && j == 0
  2071. && generatedRow.getValue() instanceof Component) {
  2072. value = generatedRow.getValue();
  2073. } else if (generatedRow.getText().length > j) {
  2074. value = generatedRow.getText()[j];
  2075. }
  2076. } else {
  2077. // check if current pageBuffer already has row
  2078. int index = firstIndex + i;
  2079. if (p != null || isGenerated) {
  2080. int indexInOldBuffer = index - pageBufferFirstIndex;
  2081. if (index < firstIndexNotInCache
  2082. && index >= pageBufferFirstIndex
  2083. && pageBuffer[CELL_GENERATED_ROW][indexInOldBuffer] == null
  2084. && id.equals(
  2085. pageBuffer[CELL_ITEMID][indexInOldBuffer])) {
  2086. // we already have data in our cache,
  2087. // recycle it instead of fetching it via
  2088. // getValue/getPropertyValue
  2089. value = pageBuffer[CELL_FIRSTCOL + j][indexInOldBuffer];
  2090. if (!isGeneratedColumn && iscomponent[j]
  2091. || !(value instanceof Component)) {
  2092. listenProperty(p, oldListenedProperties);
  2093. }
  2094. } else {
  2095. if (isGeneratedColumn) {
  2096. ColumnGenerator cg = columnGenerators
  2097. .get(colids[j]);
  2098. try {
  2099. value = cg.generateCell(this, id, colids[j]);
  2100. } catch (Exception e) {
  2101. exceptionsDuringCachePopulation.add(e);
  2102. value = null;
  2103. }
  2104. if (value != null && !(value instanceof Component)
  2105. && !(value instanceof String)) {
  2106. // Avoid errors if a generator returns
  2107. // something
  2108. // other than a Component or a String
  2109. value = value.toString();
  2110. }
  2111. } else if (iscomponent[j]) {
  2112. try {
  2113. value = p.getValue();
  2114. } catch (Exception e) {
  2115. exceptionsDuringCachePopulation.add(e);
  2116. value = null;
  2117. }
  2118. listenProperty(p, oldListenedProperties);
  2119. } else if (p != null) {
  2120. try {
  2121. value = getPropertyValue(id, colids[j], p);
  2122. } catch (Exception e) {
  2123. exceptionsDuringCachePopulation.add(e);
  2124. value = null;
  2125. }
  2126. /*
  2127. * If returned value is Component (via fieldfactory
  2128. * or overridden getPropertyValue) we expect it to
  2129. * listen property value changes. Otherwise if
  2130. * property emits value change events, table will
  2131. * start to listen them and refresh content when
  2132. * needed.
  2133. */
  2134. if (!(value instanceof Component)) {
  2135. listenProperty(p, oldListenedProperties);
  2136. }
  2137. } else {
  2138. try {
  2139. value = getPropertyValue(id, colids[j], null);
  2140. } catch (Exception e) {
  2141. exceptionsDuringCachePopulation.add(e);
  2142. value = null;
  2143. }
  2144. }
  2145. }
  2146. }
  2147. }
  2148. if (value instanceof Component) {
  2149. registerComponent((Component) value);
  2150. }
  2151. cells[CELL_FIRSTCOL + j][i] = value;
  2152. }
  2153. }
  2154. protected void registerComponent(Component component) {
  2155. getLogger().log(Level.FINEST, "Registered {0}: {1}", new Object[] {
  2156. component.getClass().getSimpleName(), component.getCaption() });
  2157. if (!equals(component.getParent())) {
  2158. component.setParent(this);
  2159. }
  2160. visibleComponents.add(component);
  2161. }
  2162. private void listenProperty(Property<?> p,
  2163. HashSet<Property<?>> oldListenedProperties) {
  2164. if (p instanceof Property.ValueChangeNotifier) {
  2165. if (oldListenedProperties == null
  2166. || !oldListenedProperties.contains(p)) {
  2167. ((Property.ValueChangeNotifier) p).addListener(this);
  2168. }
  2169. /*
  2170. * register listened properties, so we can do proper cleanup to free
  2171. * memory. Essential if table has loads of data and it is used for a
  2172. * long time.
  2173. */
  2174. listenedProperties.add(p);
  2175. }
  2176. }
  2177. /**
  2178. * @param firstIx
  2179. * Index of the first row to process. Global index, not relative
  2180. * to page buffer.
  2181. * @param count
  2182. */
  2183. private void unregisterComponentsAndPropertiesInRows(int firstIx,
  2184. int count) {
  2185. if (getLogger().isLoggable(Level.FINEST)) {
  2186. getLogger().log(Level.FINEST,
  2187. "Unregistering components in rows {0}-{1}",
  2188. new Object[] { firstIx, (firstIx + count - 1) });
  2189. }
  2190. Object[] colids = getVisibleColumns();
  2191. if (pageBuffer != null && pageBuffer[CELL_ITEMID].length > 0) {
  2192. int bufSize = pageBuffer[CELL_ITEMID].length;
  2193. int ix = firstIx - pageBufferFirstIndex;
  2194. ix = ix < 0 ? 0 : ix;
  2195. if (ix < bufSize) {
  2196. count = count > bufSize - ix ? bufSize - ix : count;
  2197. for (int i = 0; i < count; i++) {
  2198. for (int c = 0; c < colids.length; c++) {
  2199. Object cellVal = pageBuffer[CELL_FIRSTCOL + c][i + ix];
  2200. if (cellVal instanceof Component
  2201. && visibleComponents.contains(cellVal)) {
  2202. visibleComponents.remove(cellVal);
  2203. unregisterComponent((Component) cellVal);
  2204. } else {
  2205. Property<?> p = getContainerProperty(
  2206. pageBuffer[CELL_ITEMID][i + ix], colids[c]);
  2207. if (p instanceof ValueChangeNotifier
  2208. && listenedProperties.contains(p)) {
  2209. listenedProperties.remove(p);
  2210. ((ValueChangeNotifier) p).removeListener(this);
  2211. }
  2212. }
  2213. }
  2214. }
  2215. }
  2216. }
  2217. }
  2218. /**
  2219. * Helper method to remove listeners and maintain correct component
  2220. * hierarchy. Detaches properties and components if those are no more
  2221. * rendered in client.
  2222. *
  2223. * @param oldListenedProperties
  2224. * set of properties that where listened in last render
  2225. * @param oldVisibleComponents
  2226. * set of components that where attached in last render
  2227. */
  2228. private void unregisterPropertiesAndComponents(
  2229. HashSet<Property<?>> oldListenedProperties,
  2230. HashSet<Component> oldVisibleComponents) {
  2231. if (oldVisibleComponents != null) {
  2232. for (final Iterator<Component> i = oldVisibleComponents
  2233. .iterator(); i.hasNext();) {
  2234. Component c = i.next();
  2235. if (!visibleComponents.contains(c)) {
  2236. unregisterComponent(c);
  2237. }
  2238. }
  2239. }
  2240. if (oldListenedProperties != null) {
  2241. for (final Iterator<Property<?>> i = oldListenedProperties
  2242. .iterator(); i.hasNext();) {
  2243. Property.ValueChangeNotifier o = (ValueChangeNotifier) i.next();
  2244. if (!listenedProperties.contains(o)) {
  2245. o.removeListener(this);
  2246. }
  2247. }
  2248. }
  2249. }
  2250. /**
  2251. * This method cleans up a Component that has been generated when Table is
  2252. * in editable mode. The component needs to be detached from its parent and
  2253. * if it is a field, it needs to be detached from its property data source
  2254. * in order to allow garbage collection to take care of removing the unused
  2255. * component from memory.
  2256. *
  2257. * Override this method and getPropertyValue(Object, Object, Property) with
  2258. * custom logic if you need to deal with buffered fields.
  2259. *
  2260. * @see #getPropertyValue(Object, Object, Property)
  2261. *
  2262. * @param component
  2263. * component that should be unregistered.
  2264. */
  2265. protected void unregisterComponent(Component component) {
  2266. getLogger().log(Level.FINEST, "Unregistered {0}: {1}", new Object[] {
  2267. component.getClass().getSimpleName(), component.getCaption() });
  2268. component.setParent(null);
  2269. /*
  2270. * Also remove property data sources to unregister listeners keeping the
  2271. * fields in memory.
  2272. */
  2273. if (component instanceof Field) {
  2274. Field<?> field = (Field<?>) component;
  2275. Property<?> associatedProperty = associatedProperties
  2276. .remove(component);
  2277. if (associatedProperty != null
  2278. && field.getPropertyDataSource() == associatedProperty) {
  2279. // Remove the property data source only if it's the one we
  2280. // added in getPropertyValue
  2281. field.setPropertyDataSource(null);
  2282. }
  2283. }
  2284. }
  2285. /**
  2286. * Sets the row header mode.
  2287. * <p>
  2288. * The mode can be one of the following ones:
  2289. * <ul>
  2290. * <li>{@link #ROW_HEADER_MODE_HIDDEN}: The row captions are hidden.</li>
  2291. * <li>{@link #ROW_HEADER_MODE_ID}: Items Id-objects <code>toString()</code>
  2292. * is used as row caption.
  2293. * <li>{@link #ROW_HEADER_MODE_ITEM}: Item-objects <code>toString()</code>
  2294. * is used as row caption.
  2295. * <li>{@link #ROW_HEADER_MODE_PROPERTY}: Property set with
  2296. * {@link #setItemCaptionPropertyId(Object)} is used as row header.
  2297. * <li>{@link #ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID}: Items Id-objects
  2298. * <code>toString()</code> is used as row header. If caption is explicitly
  2299. * specified, it overrides the id-caption.
  2300. * <li>{@link #ROW_HEADER_MODE_EXPLICIT}: The row headers must be explicitly
  2301. * specified.</li>
  2302. * <li>{@link #ROW_HEADER_MODE_INDEX}: The index of the item is used as row
  2303. * caption. The index mode can only be used with the containers implementing
  2304. * <code>Container.Indexed</code> interface.</li>
  2305. * </ul>
  2306. * The default value is {@link #ROW_HEADER_MODE_HIDDEN}
  2307. * </p>
  2308. *
  2309. * @param mode
  2310. * the One of the modes listed above.
  2311. */
  2312. public void setRowHeaderMode(RowHeaderMode mode) {
  2313. if (mode != null) {
  2314. rowHeaderMode = mode;
  2315. if (mode != RowHeaderMode.HIDDEN) {
  2316. setItemCaptionMode(mode.getItemCaptionMode());
  2317. }
  2318. // Assures the visual refresh. No need to reset the page buffer
  2319. // before
  2320. // as the content has not changed, only the alignments.
  2321. refreshRenderedCells();
  2322. }
  2323. }
  2324. /**
  2325. * Gets the row header mode.
  2326. *
  2327. * @return the Row header mode.
  2328. * @see #setRowHeaderMode
  2329. */
  2330. public RowHeaderMode getRowHeaderMode() {
  2331. return rowHeaderMode;
  2332. }
  2333. /**
  2334. * Adds the new row to table and fill the visible cells (except generated
  2335. * columns) with given values.
  2336. *
  2337. * @param cells
  2338. * the Object array that is used for filling the visible cells
  2339. * new row. The types must be settable to visible column property
  2340. * types.
  2341. * @param itemId
  2342. * the Id the new row. If null, a new id is automatically
  2343. * assigned. If given, the table cannot already have a item with
  2344. * given id.
  2345. * @return Returns item id for the new row. Returns null if operation fails.
  2346. */
  2347. public Object addItem(Object[] cells, Object itemId)
  2348. throws UnsupportedOperationException {
  2349. // remove generated columns from the list of columns being assigned
  2350. final LinkedList<Object> availableCols = new LinkedList<>();
  2351. for (Iterator<Object> it = visibleColumns.iterator(); it.hasNext();) {
  2352. Object id = it.next();
  2353. if (!columnGenerators.containsKey(id)) {
  2354. availableCols.add(id);
  2355. }
  2356. }
  2357. // Checks that a correct number of cells are given
  2358. if (cells.length != availableCols.size()) {
  2359. return null;
  2360. }
  2361. // Creates new item
  2362. Item item;
  2363. if (itemId == null) {
  2364. itemId = items.addItem();
  2365. if (itemId == null) {
  2366. return null;
  2367. }
  2368. item = items.getItem(itemId);
  2369. } else {
  2370. item = items.addItem(itemId);
  2371. }
  2372. if (item == null) {
  2373. return null;
  2374. }
  2375. // Fills the item properties
  2376. for (int i = 0; i < availableCols.size(); i++) {
  2377. item.getItemProperty(availableCols.get(i)).setValue(cells[i]);
  2378. }
  2379. if (!(items instanceof Container.ItemSetChangeNotifier)) {
  2380. refreshRowCache();
  2381. }
  2382. return itemId;
  2383. }
  2384. /**
  2385. * Discards and recreates the internal row cache. Call this if you make
  2386. * changes that affect the rows but the information about the changes are
  2387. * not automatically propagated to the Table.
  2388. * <p>
  2389. * Do not call this e.g. if you have updated the data model through a
  2390. * Property. These types of changes are automatically propagated to the
  2391. * Table.
  2392. * <p>
  2393. * A typical case when this is needed is if you update a generator (e.g.
  2394. * CellStyleGenerator) and want to ensure that the rows are redrawn with new
  2395. * styles.
  2396. * <p>
  2397. * <i>Note that calling this method is not cheap so avoid calling it
  2398. * unnecessarily.</i>
  2399. *
  2400. * @since 6.7.2
  2401. */
  2402. public void refreshRowCache() {
  2403. resetPageBuffer();
  2404. refreshRenderedCells();
  2405. }
  2406. /**
  2407. * Sets the Container that serves as the data source of the viewer. As a
  2408. * side-effect the table's selection value is set to null as the old
  2409. * selection might not exist in new Container.<br>
  2410. * <br>
  2411. * All rows and columns are generated as visible using this method. If the
  2412. * new container contains properties that are not meant to be shown you
  2413. * should use {@link Table#setContainerDataSource(Container, Collection)}
  2414. * instead, especially if the table is editable.
  2415. * <p>
  2416. * Keeps propertyValueConverters if the corresponding id exists in the new
  2417. * data source and is of a compatible type.
  2418. * </p>
  2419. *
  2420. * @param newDataSource
  2421. * the new data source.
  2422. */
  2423. @Override
  2424. public void setContainerDataSource(Container newDataSource) {
  2425. if (newDataSource == null) {
  2426. newDataSource = new IndexedContainer();
  2427. }
  2428. Collection<Object> generated;
  2429. if (columnGenerators != null) {
  2430. generated = columnGenerators.keySet();
  2431. } else {
  2432. generated = Collections.emptyList();
  2433. }
  2434. List<Object> visibleIds = new ArrayList<>();
  2435. if (generated.isEmpty()) {
  2436. visibleIds.addAll(newDataSource.getContainerPropertyIds());
  2437. } else {
  2438. for (Object id : newDataSource.getContainerPropertyIds()) {
  2439. // don't add duplicates
  2440. if (!generated.contains(id)) {
  2441. visibleIds.add(id);
  2442. }
  2443. }
  2444. // generated columns to the end
  2445. visibleIds.addAll(generated);
  2446. }
  2447. setContainerDataSource(newDataSource, visibleIds);
  2448. }
  2449. /**
  2450. * Sets the container data source and the columns that will be visible.
  2451. * Columns are shown in the collection's iteration order.
  2452. * <p>
  2453. * Keeps propertyValueConverters if the corresponding id exists in the new
  2454. * data source and is of a compatible type.
  2455. * </p>
  2456. *
  2457. * @see Table#setContainerDataSource(Container)
  2458. * @see Table#setVisibleColumns(Object[])
  2459. * @see Table#setConverter(Object, Converter<String, ?>)
  2460. *
  2461. * @param newDataSource
  2462. * the new data source.
  2463. * @param visibleIds
  2464. * IDs of the visible columns
  2465. */
  2466. public void setContainerDataSource(Container newDataSource,
  2467. Collection<?> visibleIds) {
  2468. disableContentRefreshing();
  2469. if (newDataSource == null) {
  2470. newDataSource = new IndexedContainer();
  2471. }
  2472. if (visibleIds == null) {
  2473. visibleIds = new ArrayList<>();
  2474. }
  2475. // Retain propertyValueConverters if their corresponding ids are
  2476. // properties of the new
  2477. // data source and are of a compatible type
  2478. if (propertyValueConverters != null) {
  2479. Collection<?> newPropertyIds = newDataSource
  2480. .getContainerPropertyIds();
  2481. LinkedList<Object> retainableValueConverters = new LinkedList<>();
  2482. for (Object propertyId : newPropertyIds) {
  2483. Converter<String, ?> converter = getConverter(propertyId);
  2484. if (converter != null) {
  2485. if (typeIsCompatible(converter.getModelType(),
  2486. newDataSource.getType(propertyId))) {
  2487. retainableValueConverters.add(propertyId);
  2488. }
  2489. }
  2490. }
  2491. propertyValueConverters.keySet()
  2492. .retainAll(retainableValueConverters);
  2493. }
  2494. // Assures that the data source is ordered by making unordered
  2495. // containers ordered by wrapping them
  2496. if (newDataSource instanceof Container.Ordered) {
  2497. super.setContainerDataSource(newDataSource);
  2498. } else {
  2499. super.setContainerDataSource(
  2500. new ContainerOrderedWrapper(newDataSource));
  2501. }
  2502. // Resets page position
  2503. currentPageFirstItemId = null;
  2504. currentPageFirstItemIndex = 0;
  2505. // Resets column properties
  2506. if (collapsedColumns != null) {
  2507. collapsedColumns.clear();
  2508. }
  2509. // don't add the same id twice
  2510. Collection<Object> col = new LinkedList<>();
  2511. for (Iterator<?> it = visibleIds.iterator(); it.hasNext();) {
  2512. Object id = it.next();
  2513. if (!col.contains(id)) {
  2514. col.add(id);
  2515. }
  2516. }
  2517. setVisibleColumns(col.toArray());
  2518. // Assure visual refresh
  2519. resetPageBuffer();
  2520. enableContentRefreshing(true);
  2521. }
  2522. /**
  2523. * Checks if class b can be safely assigned to class a.
  2524. *
  2525. * @param a
  2526. * @param b
  2527. * @return
  2528. */
  2529. private boolean typeIsCompatible(Class<?> a, Class<?> b) {
  2530. // TODO Implement this check properly
  2531. // Basically we need to do a a.isAssignableFrom(b)
  2532. // with special considerations for primitive types.
  2533. return true;
  2534. }
  2535. /**
  2536. * Gets items ids from a range of key values
  2537. *
  2538. * @param itemId
  2539. * The start key
  2540. * @param length
  2541. * amount of items to be retrieved
  2542. * @return
  2543. */
  2544. private LinkedHashSet<Object> getItemIdsInRange(Object itemId,
  2545. final int length) {
  2546. LinkedHashSet<Object> ids = new LinkedHashSet<>();
  2547. for (int i = 0; i < length; i++) {
  2548. assert itemId != null; // should not be null unless client-server
  2549. // are out of sync
  2550. ids.add(itemId);
  2551. itemId = nextItemId(itemId);
  2552. }
  2553. return ids;
  2554. }
  2555. /**
  2556. * Handles selection if selection is a multiselection
  2557. *
  2558. * @param variables
  2559. * The variables
  2560. */
  2561. private void handleSelectedItems(Map<String, Object> variables) {
  2562. final String[] ka = (String[]) variables.get("selected");
  2563. final String[] ranges = (String[]) variables.get("selectedRanges");
  2564. Set<Object> renderedButNotSelectedItemIds = getCurrentlyRenderedItemIds();
  2565. @SuppressWarnings("unchecked")
  2566. HashSet<Object> newValue = new LinkedHashSet<>(
  2567. (Collection<Object>) getValue());
  2568. if (variables.containsKey("clearSelections")) {
  2569. // the client side has instructed to swipe all previous selections
  2570. newValue.clear();
  2571. }
  2572. /*
  2573. * Then add (possibly some of them back) rows that are currently
  2574. * selected on the client side (the ones that the client side is aware
  2575. * of).
  2576. */
  2577. for (int i = 0; i < ka.length; i++) {
  2578. // key to id
  2579. final Object id = itemIdMapper.get(ka[i]);
  2580. if (!isNullSelectionAllowed()
  2581. && (id == null || id == getNullSelectionItemId())) {
  2582. // skip empty selection if nullselection is not allowed
  2583. markAsDirty();
  2584. } else if (id != null && containsId(id)) {
  2585. newValue.add(id);
  2586. renderedButNotSelectedItemIds.remove(id);
  2587. }
  2588. }
  2589. /* Add range items aka shift clicked multiselection areas */
  2590. if (ranges != null) {
  2591. for (String range : ranges) {
  2592. String[] split = range.split("-");
  2593. Object startItemId = itemIdMapper.get(split[0]);
  2594. int length = Integer.valueOf(split[1]);
  2595. LinkedHashSet<Object> itemIdsInRange = getItemIdsInRange(
  2596. startItemId, length);
  2597. newValue.addAll(itemIdsInRange);
  2598. renderedButNotSelectedItemIds.removeAll(itemIdsInRange);
  2599. }
  2600. }
  2601. /*
  2602. * finally clear all currently rendered rows (the ones that the client
  2603. * side counterpart is aware of) that the client didn't send as selected
  2604. */
  2605. newValue.removeAll(renderedButNotSelectedItemIds);
  2606. if (!isNullSelectionAllowed() && newValue.isEmpty()) {
  2607. // empty selection not allowed, keep old value
  2608. markAsDirty();
  2609. return;
  2610. }
  2611. setValue(newValue, true);
  2612. }
  2613. private Set<Object> getCurrentlyRenderedItemIds() {
  2614. HashSet<Object> ids = new HashSet<>();
  2615. if (pageBuffer != null) {
  2616. for (int i = 0; i < pageBuffer[CELL_ITEMID].length; i++) {
  2617. ids.add(pageBuffer[CELL_ITEMID][i]);
  2618. }
  2619. }
  2620. return ids;
  2621. }
  2622. /* Component basics */
  2623. /**
  2624. * Invoked when the value of a variable has changed.
  2625. *
  2626. * @see com.vaadin.v7.ui.Select#changeVariables(java.lang.Object,
  2627. * java.util.Map)
  2628. */
  2629. @Override
  2630. public void changeVariables(Object source, Map<String, Object> variables) {
  2631. boolean clientNeedsContentRefresh = false;
  2632. handleClickEvent(variables);
  2633. handleColumnResizeEvent(variables);
  2634. handleColumnWidthUpdates(variables);
  2635. disableContentRefreshing();
  2636. if (!isSelectable() && variables.containsKey("selected")) {
  2637. // Not-selectable is a special case, AbstractSelect does not support
  2638. // TODO could be optimized.
  2639. variables = new HashMap<>(variables);
  2640. variables.remove("selected");
  2641. }
  2642. /*
  2643. * The AbstractSelect cannot handle the multiselection properly, instead
  2644. * we handle it ourself
  2645. */
  2646. else if (isSelectable() && isMultiSelect()
  2647. && variables.containsKey("selected")
  2648. && multiSelectMode == MultiSelectMode.DEFAULT) {
  2649. handleSelectedItems(variables);
  2650. variables = new HashMap<>(variables);
  2651. variables.remove("selected");
  2652. }
  2653. super.changeVariables(source, variables);
  2654. // Client might update the pagelength if Table height is fixed
  2655. if (variables.containsKey("pagelength")) {
  2656. // Sets pageLength directly to avoid repaint that setter causes
  2657. pageLength = (Integer) variables.get("pagelength");
  2658. }
  2659. // Page start index
  2660. if (variables.containsKey("firstvisible")) {
  2661. final Integer value = (Integer) variables.get("firstvisible");
  2662. if (value != null) {
  2663. setCurrentPageFirstItemIndex(value.intValue(), false);
  2664. }
  2665. }
  2666. // Sets requested firstrow and rows for the next paint
  2667. if (variables.containsKey("reqfirstrow")
  2668. || variables.containsKey("reqrows")) {
  2669. try {
  2670. firstToBeRenderedInClient = ((Integer) variables
  2671. .get("firstToBeRendered")).intValue();
  2672. lastToBeRenderedInClient = ((Integer) variables
  2673. .get("lastToBeRendered")).intValue();
  2674. } catch (Exception e) {
  2675. // FIXME: Handle exception
  2676. getLogger().log(Level.FINER,
  2677. "Could not parse the first and/or last rows.", e);
  2678. }
  2679. // respect suggested rows only if table is not otherwise updated
  2680. // (row caches emptied by other event)
  2681. if (!containerChangeToBeRendered) {
  2682. Integer value = (Integer) variables.get("reqfirstrow");
  2683. if (value != null) {
  2684. reqFirstRowToPaint = value.intValue();
  2685. }
  2686. value = (Integer) variables.get("reqrows");
  2687. if (value != null) {
  2688. reqRowsToPaint = value.intValue();
  2689. int size = size();
  2690. // sanity check
  2691. if (reqFirstRowToPaint >= size) {
  2692. reqFirstRowToPaint = size;
  2693. }
  2694. if (reqFirstRowToPaint + reqRowsToPaint > size) {
  2695. reqRowsToPaint = size - reqFirstRowToPaint;
  2696. }
  2697. }
  2698. }
  2699. if (getLogger().isLoggable(Level.FINEST)) {
  2700. getLogger().log(Level.FINEST, "Client wants rows {0}-{1}",
  2701. new Object[] { reqFirstRowToPaint,
  2702. (reqFirstRowToPaint + reqRowsToPaint - 1) });
  2703. }
  2704. clientNeedsContentRefresh = true;
  2705. }
  2706. if (isSortEnabled()) {
  2707. // Sorting
  2708. boolean doSort = false;
  2709. if (variables.containsKey("sortcolumn")) {
  2710. final String colId = (String) variables.get("sortcolumn");
  2711. if (colId != null && !"".equals(colId)
  2712. && !"null".equals(colId)) {
  2713. final Object id = columnIdMap.get(colId);
  2714. setSortContainerPropertyId(id, false);
  2715. doSort = true;
  2716. }
  2717. }
  2718. if (variables.containsKey("sortascending")) {
  2719. final boolean state = ((Boolean) variables.get("sortascending"))
  2720. .booleanValue();
  2721. if (state != sortAscending) {
  2722. setSortAscending(state, false);
  2723. doSort = true;
  2724. }
  2725. }
  2726. if (doSort) {
  2727. this.sort();
  2728. resetPageBuffer();
  2729. }
  2730. }
  2731. // Dynamic column hide/show and order
  2732. // Update visible columns
  2733. if (isColumnCollapsingAllowed()) {
  2734. if (variables.containsKey("collapsedcolumns")) {
  2735. try {
  2736. final Object[] ids = (Object[]) variables
  2737. .get("collapsedcolumns");
  2738. Set<Object> idSet = new HashSet<>();
  2739. for (Object id : ids) {
  2740. idSet.add(columnIdMap.get(id.toString()));
  2741. }
  2742. for (final Iterator<Object> it = visibleColumns
  2743. .iterator(); it.hasNext();) {
  2744. Object propertyId = it.next();
  2745. if (isColumnCollapsed(propertyId)) {
  2746. if (!idSet.contains(propertyId)) {
  2747. setColumnCollapsed(propertyId, false);
  2748. }
  2749. } else if (idSet.contains(propertyId)) {
  2750. setColumnCollapsed(propertyId, true);
  2751. }
  2752. }
  2753. } catch (final Exception e) {
  2754. // FIXME: Handle exception
  2755. getLogger().log(Level.FINER,
  2756. "Could not determine column collapsing state", e);
  2757. }
  2758. clientNeedsContentRefresh = true;
  2759. }
  2760. }
  2761. if (isColumnReorderingAllowed()) {
  2762. if (variables.containsKey("columnorder")) {
  2763. try {
  2764. final Object[] ids = (Object[]) variables
  2765. .get("columnorder");
  2766. // need a real Object[], ids can be a String[]
  2767. final Object[] idsTemp = new Object[ids.length];
  2768. for (int i = 0; i < ids.length; i++) {
  2769. idsTemp[i] = columnIdMap.get(ids[i].toString());
  2770. }
  2771. setColumnOrder(idsTemp);
  2772. if (hasListeners(ColumnReorderEvent.class)) {
  2773. fireEvent(new ColumnReorderEvent(this));
  2774. }
  2775. } catch (final Exception e) {
  2776. // FIXME: Handle exception
  2777. getLogger().log(Level.FINER,
  2778. "Could not determine column reordering state", e);
  2779. }
  2780. clientNeedsContentRefresh = true;
  2781. }
  2782. }
  2783. enableContentRefreshing(clientNeedsContentRefresh);
  2784. // Actions
  2785. if (variables.containsKey("action")) {
  2786. final StringTokenizer st = new StringTokenizer(
  2787. (String) variables.get("action"), ",");
  2788. if (st.countTokens() == 2) {
  2789. final Object itemId = itemIdMapper.get(st.nextToken());
  2790. final Action action = actionMapper.get(st.nextToken());
  2791. if (action != null && (itemId == null || containsId(itemId))
  2792. && actionHandlers != null) {
  2793. for (Handler ah : actionHandlers) {
  2794. ah.handleAction(action, this, itemId);
  2795. }
  2796. }
  2797. }
  2798. }
  2799. }
  2800. /**
  2801. * Handles click event
  2802. *
  2803. * @param variables
  2804. */
  2805. private void handleClickEvent(Map<String, Object> variables) {
  2806. // Item click event
  2807. if (variables.containsKey("clickEvent")) {
  2808. String key = (String) variables.get("clickedKey");
  2809. Object itemId = itemIdMapper.get(key);
  2810. Object propertyId = null;
  2811. String colkey = (String) variables.get("clickedColKey");
  2812. // click is not necessary on a property
  2813. if (colkey != null) {
  2814. propertyId = columnIdMap.get(colkey);
  2815. }
  2816. MouseEventDetails evt = MouseEventDetails
  2817. .deSerialize((String) variables.get("clickEvent"));
  2818. Item item = getItem(itemId);
  2819. if (item != null) {
  2820. fireEvent(new ItemClickEvent(this, item, itemId, propertyId,
  2821. evt));
  2822. }
  2823. }
  2824. // Header click event
  2825. else if (variables.containsKey("headerClickEvent")) {
  2826. MouseEventDetails details = MouseEventDetails
  2827. .deSerialize((String) variables.get("headerClickEvent"));
  2828. Object cid = variables.get("headerClickCID");
  2829. Object propertyId = null;
  2830. if (cid != null) {
  2831. propertyId = columnIdMap.get(cid.toString());
  2832. }
  2833. fireEvent(new HeaderClickEvent(this, propertyId, details));
  2834. }
  2835. // Footer click event
  2836. else if (variables.containsKey("footerClickEvent")) {
  2837. MouseEventDetails details = MouseEventDetails
  2838. .deSerialize((String) variables.get("footerClickEvent"));
  2839. Object cid = variables.get("footerClickCID");
  2840. Object propertyId = null;
  2841. if (cid != null) {
  2842. propertyId = columnIdMap.get(cid.toString());
  2843. }
  2844. fireEvent(new FooterClickEvent(this, propertyId, details));
  2845. }
  2846. }
  2847. /**
  2848. * Handles the column resize event sent by the client.
  2849. *
  2850. * @param variables
  2851. */
  2852. private void handleColumnResizeEvent(Map<String, Object> variables) {
  2853. if (variables.containsKey("columnResizeEventColumn")) {
  2854. Object cid = variables.get("columnResizeEventColumn");
  2855. Object propertyId = null;
  2856. if (cid != null) {
  2857. propertyId = columnIdMap.get(cid.toString());
  2858. Object prev = variables.get("columnResizeEventPrev");
  2859. int previousWidth = -1;
  2860. if (prev != null) {
  2861. previousWidth = Integer.valueOf(prev.toString());
  2862. }
  2863. Object curr = variables.get("columnResizeEventCurr");
  2864. int currentWidth = -1;
  2865. if (curr != null) {
  2866. currentWidth = Integer.valueOf(curr.toString());
  2867. }
  2868. fireColumnResizeEvent(propertyId, previousWidth, currentWidth);
  2869. }
  2870. }
  2871. }
  2872. private void fireColumnCollapseEvent(Object propertyId) {
  2873. fireEvent(new ColumnCollapseEvent(this, propertyId));
  2874. }
  2875. private void fireColumnResizeEvent(Object propertyId, int previousWidth,
  2876. int currentWidth) {
  2877. /*
  2878. * Update the sizes on the server side. If a column previously had a
  2879. * expand ratio and the user resized the column then the expand ratio
  2880. * will be turned into a static pixel size.
  2881. */
  2882. setColumnWidth(propertyId, currentWidth);
  2883. fireEvent(new ColumnResizeEvent(this, propertyId, previousWidth,
  2884. currentWidth));
  2885. }
  2886. private void handleColumnWidthUpdates(Map<String, Object> variables) {
  2887. if (variables.containsKey("columnWidthUpdates")) {
  2888. String[] events = (String[]) variables.get("columnWidthUpdates");
  2889. for (String str : events) {
  2890. String[] eventDetails = str.split(":");
  2891. Object propertyId = columnIdMap.get(eventDetails[0]);
  2892. if (propertyId == null) {
  2893. propertyId = ROW_HEADER_FAKE_PROPERTY_ID;
  2894. }
  2895. int width = Integer.valueOf(eventDetails[1]);
  2896. setColumnWidth(propertyId, width);
  2897. }
  2898. }
  2899. }
  2900. /**
  2901. * Go to mode where content updates are not done. This is due we want to
  2902. * bypass expensive content for some reason (like when we know we may have
  2903. * other content changes on their way).
  2904. *
  2905. * @return true if content refresh flag was enabled prior this call
  2906. */
  2907. protected boolean disableContentRefreshing() {
  2908. boolean wasDisabled = isContentRefreshesEnabled;
  2909. isContentRefreshesEnabled = false;
  2910. return wasDisabled;
  2911. }
  2912. /**
  2913. * Go to mode where content content refreshing has effect.
  2914. *
  2915. * @param refreshContent
  2916. * true if content refresh needs to be done
  2917. */
  2918. protected void enableContentRefreshing(boolean refreshContent) {
  2919. isContentRefreshesEnabled = true;
  2920. if (refreshContent) {
  2921. refreshRenderedCells();
  2922. // Ensure that client gets a response
  2923. markAsDirty();
  2924. }
  2925. }
  2926. @Override
  2927. public void beforeClientResponse(boolean initial) {
  2928. super.beforeClientResponse(initial);
  2929. // Ensure pageBuffer is filled before sending the response to avoid
  2930. // calls to markAsDirty during paint
  2931. getVisibleCells();
  2932. }
  2933. /*
  2934. * (non-Javadoc)
  2935. *
  2936. * @see com.vaadin.ui.AbstractSelect#paintContent(com.vaadin.
  2937. * terminal.PaintTarget)
  2938. */
  2939. @Override
  2940. public void paintContent(PaintTarget target) throws PaintException {
  2941. isBeingPainted = true;
  2942. try {
  2943. doPaintContent(target);
  2944. } finally {
  2945. isBeingPainted = false;
  2946. }
  2947. }
  2948. private void doPaintContent(PaintTarget target) throws PaintException {
  2949. /*
  2950. * Body actions - Actions which has the target null and can be invoked
  2951. * by right clicking on the table body.
  2952. */
  2953. final Set<Action> actionSet = findAndPaintBodyActions(target);
  2954. final Object[][] cells = getVisibleCells();
  2955. int rows = findNumRowsToPaint(target, cells);
  2956. int total = size();
  2957. if (shouldHideNullSelectionItem()) {
  2958. total--;
  2959. rows--;
  2960. }
  2961. // Table attributes
  2962. paintTableAttributes(target, rows, total);
  2963. paintVisibleColumnOrder(target);
  2964. // Rows
  2965. if (isPartialRowUpdate() && painted && !target.isFullRepaint()) {
  2966. paintPartialRowUpdate(target, actionSet);
  2967. } else if (target.isFullRepaint() || isRowCacheInvalidated()) {
  2968. paintRows(target, cells, actionSet);
  2969. setRowCacheInvalidated(false);
  2970. }
  2971. /*
  2972. * Send the page buffer indexes to ensure that the client side stays in
  2973. * sync. Otherwise we _might_ have the situation where the client side
  2974. * discards too few or too many rows, causing out of sync issues.
  2975. */
  2976. int pageBufferLastIndex = pageBufferFirstIndex
  2977. + pageBuffer[CELL_ITEMID].length - 1;
  2978. target.addAttribute(TableConstants.ATTRIBUTE_PAGEBUFFER_FIRST,
  2979. pageBufferFirstIndex);
  2980. target.addAttribute(TableConstants.ATTRIBUTE_PAGEBUFFER_LAST,
  2981. pageBufferLastIndex);
  2982. paintSorting(target);
  2983. resetVariablesAndPageBuffer(target);
  2984. // Actions
  2985. paintActions(target, actionSet);
  2986. paintColumnOrder(target);
  2987. // Available columns
  2988. paintAvailableColumns(target);
  2989. paintVisibleColumns(target);
  2990. if (keyMapperReset) {
  2991. keyMapperReset = false;
  2992. target.addAttribute(TableConstants.ATTRIBUTE_KEY_MAPPER_RESET,
  2993. true);
  2994. }
  2995. if (dropHandler != null) {
  2996. dropHandler.getAcceptCriterion().paint(target);
  2997. }
  2998. painted = true;
  2999. }
  3000. private void setRowCacheInvalidated(boolean invalidated) {
  3001. rowCacheInvalidated = invalidated;
  3002. }
  3003. protected boolean isRowCacheInvalidated() {
  3004. return rowCacheInvalidated;
  3005. }
  3006. private void paintPartialRowUpdate(PaintTarget target,
  3007. Set<Action> actionSet) throws PaintException {
  3008. paintPartialRowUpdates(target, actionSet);
  3009. paintPartialRowAdditions(target, actionSet);
  3010. }
  3011. private void paintPartialRowUpdates(PaintTarget target,
  3012. Set<Action> actionSet) throws PaintException {
  3013. final boolean[] iscomponent = findCellsWithComponents();
  3014. int firstIx = getFirstUpdatedItemIndex();
  3015. int count = getUpdatedRowCount();
  3016. target.startTag("urows");
  3017. target.addAttribute("firsturowix", firstIx);
  3018. target.addAttribute("numurows", count);
  3019. // Partial row updates bypass the normal caching mechanism.
  3020. Object[][] cells = getVisibleCellsUpdateCacheRows(firstIx, count);
  3021. for (int indexInRowbuffer = 0; indexInRowbuffer < count; indexInRowbuffer++) {
  3022. final Object itemId = cells[CELL_ITEMID][indexInRowbuffer];
  3023. if (shouldHideNullSelectionItem()) {
  3024. // Remove null selection item if null selection is not allowed
  3025. continue;
  3026. }
  3027. paintRow(target, cells, isEditable(), actionSet, iscomponent,
  3028. indexInRowbuffer, itemId);
  3029. }
  3030. target.endTag("urows");
  3031. maybeThrowCacheUpdateExceptions();
  3032. }
  3033. private void paintPartialRowAdditions(PaintTarget target,
  3034. Set<Action> actionSet) throws PaintException {
  3035. final boolean[] iscomponent = findCellsWithComponents();
  3036. int firstIx = getFirstAddedItemIndex();
  3037. int count = getAddedRowCount();
  3038. target.startTag("prows");
  3039. if (!shouldHideAddedRows()) {
  3040. getLogger().log(Level.FINEST,
  3041. "Paint rows for add. Index: {0}, count: {1}.",
  3042. new Object[] { firstIx, count });
  3043. // Partial row additions bypass the normal caching mechanism.
  3044. Object[][] cells = getVisibleCellsInsertIntoCache(firstIx, count);
  3045. if (cells[0].length < count) {
  3046. // delete the rows below, since they will fall beyond the cache
  3047. // page.
  3048. target.addAttribute("delbelow", true);
  3049. count = cells[0].length;
  3050. }
  3051. for (int indexInRowbuffer = 0; indexInRowbuffer < count; indexInRowbuffer++) {
  3052. final Object itemId = cells[CELL_ITEMID][indexInRowbuffer];
  3053. if (shouldHideNullSelectionItem()) {
  3054. // Remove null selection item if null selection is not
  3055. // allowed
  3056. continue;
  3057. }
  3058. paintRow(target, cells, isEditable(), actionSet, iscomponent,
  3059. indexInRowbuffer, itemId);
  3060. }
  3061. } else {
  3062. getLogger().log(Level.FINEST,
  3063. "Paint rows for remove. Index: {0}, count: {1}.",
  3064. new Object[] { firstIx, count });
  3065. removeRowsFromCacheAndFillBottom(firstIx, count);
  3066. target.addAttribute("hide", true);
  3067. }
  3068. target.addAttribute("firstprowix", firstIx);
  3069. target.addAttribute("numprows", count);
  3070. target.endTag("prows");
  3071. maybeThrowCacheUpdateExceptions();
  3072. }
  3073. /**
  3074. * Subclass and override this to enable partial row updates and additions,
  3075. * which bypass the normal caching mechanism. This is useful for e.g.
  3076. * TreeTable.
  3077. *
  3078. * @return true if this update is a partial row update, false if not. For
  3079. * plain Table it is always false.
  3080. */
  3081. protected boolean isPartialRowUpdate() {
  3082. return false;
  3083. }
  3084. /**
  3085. * Subclass and override this to enable partial row additions, bypassing the
  3086. * normal caching mechanism. This is useful for e.g. TreeTable, where
  3087. * expanding a node should only fetch and add the items inside of that node.
  3088. *
  3089. * @return The index of the first added item. For plain Table it is always
  3090. * 0.
  3091. */
  3092. protected int getFirstAddedItemIndex() {
  3093. return 0;
  3094. }
  3095. /**
  3096. * Subclass and override this to enable partial row additions, bypassing the
  3097. * normal caching mechanism. This is useful for e.g. TreeTable, where
  3098. * expanding a node should only fetch and add the items inside of that node.
  3099. *
  3100. * @return the number of rows to be added, starting at the index returned by
  3101. * {@link #getFirstAddedItemIndex()}. For plain Table it is always
  3102. * 0.
  3103. */
  3104. protected int getAddedRowCount() {
  3105. return 0;
  3106. }
  3107. /**
  3108. * Subclass and override this to enable removing of rows, bypassing the
  3109. * normal caching and lazy loading mechanism. This is useful for e.g.
  3110. * TreeTable, when you need to hide certain rows as a node is collapsed.
  3111. *
  3112. * This should return true if the rows pointed to by
  3113. * {@link #getFirstAddedItemIndex()} and {@link #getAddedRowCount()} should
  3114. * be hidden instead of added.
  3115. *
  3116. * @return whether the rows to add (see {@link #getFirstAddedItemIndex()}
  3117. * and {@link #getAddedRowCount()}) should be added or hidden. For
  3118. * plain Table it is always false.
  3119. */
  3120. protected boolean shouldHideAddedRows() {
  3121. return false;
  3122. }
  3123. /**
  3124. * Subclass and override this to enable partial row updates, bypassing the
  3125. * normal caching and lazy loading mechanism. This is useful for updating
  3126. * the state of certain rows, e.g. in the TreeTable the collapsed state of a
  3127. * single node is updated using this mechanism.
  3128. *
  3129. * @return the index of the first item to be updated. For plain Table it is
  3130. * always 0.
  3131. */
  3132. protected int getFirstUpdatedItemIndex() {
  3133. return 0;
  3134. }
  3135. /**
  3136. * Subclass and override this to enable partial row updates, bypassing the
  3137. * normal caching and lazy loading mechanism. This is useful for updating
  3138. * the state of certain rows, e.g. in the TreeTable the collapsed state of a
  3139. * single node is updated using this mechanism.
  3140. *
  3141. * @return the number of rows to update, starting at the index returned by
  3142. * {@link #getFirstUpdatedItemIndex()}. For plain table it is always
  3143. * 0.
  3144. */
  3145. protected int getUpdatedRowCount() {
  3146. return 0;
  3147. }
  3148. private void paintTableAttributes(PaintTarget target, int rows, int total)
  3149. throws PaintException {
  3150. paintTabIndex(target);
  3151. paintDragMode(target);
  3152. paintSelectMode(target);
  3153. paintTableChildLayoutMeasureMode(target);
  3154. if (cacheRate != CACHE_RATE_DEFAULT) {
  3155. target.addAttribute("cr", cacheRate);
  3156. }
  3157. target.addAttribute("cols", getVisibleColumns().length);
  3158. target.addAttribute("rows", rows);
  3159. target.addAttribute("firstrow", (reqFirstRowToPaint >= 0
  3160. ? reqFirstRowToPaint : firstToBeRenderedInClient));
  3161. target.addAttribute("totalrows", total);
  3162. if (getPageLength() != 0) {
  3163. target.addAttribute("pagelength", getPageLength());
  3164. }
  3165. if (areColumnHeadersEnabled()) {
  3166. target.addAttribute("colheaders", true);
  3167. }
  3168. if (rowHeadersAreEnabled()) {
  3169. target.addAttribute("rowheaders", true);
  3170. }
  3171. target.addAttribute("colfooters", columnFootersVisible);
  3172. // The cursors are only shown on pageable table
  3173. if (getCurrentPageFirstItemIndex() != 0 || getPageLength() > 0) {
  3174. target.addVariable(this, "firstvisible",
  3175. getCurrentPageFirstItemIndex());
  3176. target.addVariable(this, "firstvisibleonlastpage",
  3177. currentPageFirstItemIndexOnLastPage);
  3178. }
  3179. }
  3180. /**
  3181. * Resets and paints "to be painted next" variables. Also reset pageBuffer
  3182. */
  3183. private void resetVariablesAndPageBuffer(PaintTarget target)
  3184. throws PaintException {
  3185. reqFirstRowToPaint = -1;
  3186. reqRowsToPaint = -1;
  3187. containerChangeToBeRendered = false;
  3188. target.addVariable(this, "reqrows", reqRowsToPaint);
  3189. target.addVariable(this, "reqfirstrow", reqFirstRowToPaint);
  3190. }
  3191. private boolean areColumnHeadersEnabled() {
  3192. return getColumnHeaderMode() != ColumnHeaderMode.HIDDEN;
  3193. }
  3194. private void paintVisibleColumns(PaintTarget target) throws PaintException {
  3195. target.startTag("visiblecolumns");
  3196. if (rowHeadersAreEnabled()) {
  3197. target.startTag("column");
  3198. target.addAttribute("cid", ROW_HEADER_COLUMN_KEY);
  3199. paintColumnWidth(target, ROW_HEADER_FAKE_PROPERTY_ID);
  3200. paintColumnExpandRatio(target, ROW_HEADER_FAKE_PROPERTY_ID);
  3201. target.endTag("column");
  3202. }
  3203. final Collection<?> sortables = getSortableContainerPropertyIds();
  3204. for (Object colId : visibleColumns) {
  3205. if (colId != null) {
  3206. target.startTag("column");
  3207. target.addAttribute("cid", columnIdMap.key(colId));
  3208. final String head = getColumnHeader(colId);
  3209. target.addAttribute("caption", (head != null ? head : ""));
  3210. final String foot = getColumnFooter(colId);
  3211. target.addAttribute("fcaption", (foot != null ? foot : ""));
  3212. if (isColumnCollapsed(colId)) {
  3213. target.addAttribute("collapsed", true);
  3214. }
  3215. if (areColumnHeadersEnabled()) {
  3216. if (getColumnIcon(colId) != null) {
  3217. target.addAttribute("icon", getColumnIcon(colId));
  3218. }
  3219. if (sortables.contains(colId)) {
  3220. target.addAttribute("sortable", true);
  3221. }
  3222. }
  3223. if (!Align.LEFT.equals(getColumnAlignment(colId))) {
  3224. target.addAttribute("align",
  3225. getColumnAlignment(colId).toString());
  3226. }
  3227. paintColumnWidth(target, colId);
  3228. paintColumnExpandRatio(target, colId);
  3229. target.endTag("column");
  3230. }
  3231. }
  3232. target.endTag("visiblecolumns");
  3233. }
  3234. private void paintAvailableColumns(PaintTarget target)
  3235. throws PaintException {
  3236. if (columnCollapsingAllowed) {
  3237. final HashSet<Object> collapsedCols = new HashSet<>();
  3238. for (Object colId : visibleColumns) {
  3239. if (isColumnCollapsed(colId)) {
  3240. collapsedCols.add(colId);
  3241. }
  3242. }
  3243. final String[] collapsedKeys = new String[collapsedCols.size()];
  3244. int nextColumn = 0;
  3245. for (Object colId : visibleColumns) {
  3246. if (isColumnCollapsed(colId)) {
  3247. collapsedKeys[nextColumn++] = columnIdMap.key(colId);
  3248. }
  3249. }
  3250. target.addVariable(this, "collapsedcolumns", collapsedKeys);
  3251. final String[] noncollapsibleKeys = new String[noncollapsibleColumns
  3252. .size()];
  3253. nextColumn = 0;
  3254. for (Object colId : noncollapsibleColumns) {
  3255. noncollapsibleKeys[nextColumn++] = columnIdMap.key(colId);
  3256. }
  3257. target.addVariable(this, "noncollapsiblecolumns",
  3258. noncollapsibleKeys);
  3259. }
  3260. }
  3261. private void paintActions(PaintTarget target, final Set<Action> actionSet)
  3262. throws PaintException {
  3263. if (!actionSet.isEmpty()) {
  3264. target.addVariable(this, "action", "");
  3265. target.startTag("actions");
  3266. for (Action a : actionSet) {
  3267. target.startTag("action");
  3268. if (a.getCaption() != null) {
  3269. target.addAttribute("caption", a.getCaption());
  3270. }
  3271. if (a.getIcon() != null) {
  3272. target.addAttribute("icon", a.getIcon());
  3273. }
  3274. target.addAttribute("key", actionMapper.key(a));
  3275. target.endTag("action");
  3276. }
  3277. target.endTag("actions");
  3278. }
  3279. }
  3280. private void paintColumnOrder(PaintTarget target) throws PaintException {
  3281. if (columnReorderingAllowed) {
  3282. final String[] colorder = new String[visibleColumns.size()];
  3283. int i = 0;
  3284. for (Object colId : visibleColumns) {
  3285. colorder[i++] = columnIdMap.key(colId);
  3286. }
  3287. target.addVariable(this, "columnorder", colorder);
  3288. }
  3289. }
  3290. private void paintSorting(PaintTarget target) throws PaintException {
  3291. // Sorting
  3292. if (getContainerDataSource() instanceof Container.Sortable) {
  3293. target.addVariable(this, "sortcolumn",
  3294. columnIdMap.key(sortContainerPropertyId));
  3295. target.addVariable(this, "sortascending", sortAscending);
  3296. }
  3297. }
  3298. private void paintRows(PaintTarget target, final Object[][] cells,
  3299. final Set<Action> actionSet) throws PaintException {
  3300. final boolean[] iscomponent = findCellsWithComponents();
  3301. target.startTag("rows");
  3302. // cells array contains all that are supposed to be visible on client,
  3303. // but we'll start from the one requested by client
  3304. int start = 0;
  3305. if (reqFirstRowToPaint != -1 && firstToBeRenderedInClient != -1) {
  3306. start = reqFirstRowToPaint - firstToBeRenderedInClient;
  3307. }
  3308. int end = cells[0].length;
  3309. if (reqRowsToPaint != -1) {
  3310. end = start + reqRowsToPaint;
  3311. }
  3312. // sanity check
  3313. if (lastToBeRenderedInClient != -1 && lastToBeRenderedInClient < end) {
  3314. end = lastToBeRenderedInClient + 1;
  3315. }
  3316. if (start > cells[CELL_ITEMID].length || start < 0) {
  3317. start = 0;
  3318. }
  3319. if (end > cells[CELL_ITEMID].length) {
  3320. end = cells[CELL_ITEMID].length;
  3321. }
  3322. for (int indexInRowbuffer = start; indexInRowbuffer < end; indexInRowbuffer++) {
  3323. final Object itemId = cells[CELL_ITEMID][indexInRowbuffer];
  3324. if (shouldHideNullSelectionItem()) {
  3325. // Remove null selection item if null selection is not allowed
  3326. continue;
  3327. }
  3328. paintRow(target, cells, isEditable(), actionSet, iscomponent,
  3329. indexInRowbuffer, itemId);
  3330. }
  3331. target.endTag("rows");
  3332. }
  3333. private boolean[] findCellsWithComponents() {
  3334. final boolean[] isComponent = new boolean[visibleColumns.size()];
  3335. int ix = 0;
  3336. for (Object columnId : visibleColumns) {
  3337. if (columnGenerators.containsKey(columnId)) {
  3338. isComponent[ix++] = true;
  3339. } else {
  3340. final Class<?> colType = getType(columnId);
  3341. isComponent[ix++] = colType != null
  3342. && Component.class.isAssignableFrom(colType);
  3343. }
  3344. }
  3345. return isComponent;
  3346. }
  3347. private void paintVisibleColumnOrder(PaintTarget target) {
  3348. // Visible column order
  3349. final ArrayList<String> visibleColOrder = new ArrayList<>();
  3350. for (Object columnId : visibleColumns) {
  3351. if (!isColumnCollapsed(columnId)) {
  3352. visibleColOrder.add(columnIdMap.key(columnId));
  3353. }
  3354. }
  3355. target.addAttribute("vcolorder", visibleColOrder.toArray());
  3356. }
  3357. private Set<Action> findAndPaintBodyActions(PaintTarget target) {
  3358. Set<Action> actionSet = new LinkedHashSet<>();
  3359. if (actionHandlers != null) {
  3360. final ArrayList<String> keys = new ArrayList<>();
  3361. for (Handler ah : actionHandlers) {
  3362. // Getting actions for the null item, which in this case means
  3363. // the body item
  3364. final Action[] actions = ah.getActions(null, this);
  3365. if (actions != null) {
  3366. for (Action action : actions) {
  3367. actionSet.add(action);
  3368. keys.add(actionMapper.key(action));
  3369. }
  3370. }
  3371. }
  3372. target.addAttribute("alb", keys.toArray());
  3373. }
  3374. return actionSet;
  3375. }
  3376. private boolean shouldHideNullSelectionItem() {
  3377. return !isNullSelectionAllowed() && getNullSelectionItemId() != null
  3378. && containsId(getNullSelectionItemId());
  3379. }
  3380. private int findNumRowsToPaint(PaintTarget target, final Object[][] cells)
  3381. throws PaintException {
  3382. int rows;
  3383. if (reqRowsToPaint >= 0) {
  3384. rows = reqRowsToPaint;
  3385. } else {
  3386. rows = cells[0].length;
  3387. if (alwaysRecalculateColumnWidths) {
  3388. // TODO experimental feature for now: tell the client to
  3389. // recalculate column widths.
  3390. // We'll only do this for paints that do not originate from
  3391. // table scroll/cache requests (i.e when reqRowsToPaint<0)
  3392. target.addAttribute("recalcWidths", true);
  3393. }
  3394. }
  3395. return rows;
  3396. }
  3397. private void paintSelectMode(PaintTarget target) throws PaintException {
  3398. if (multiSelectMode != MultiSelectMode.DEFAULT) {
  3399. target.addAttribute("multiselectmode", multiSelectMode.ordinal());
  3400. }
  3401. if (isSelectable()) {
  3402. target.addAttribute("selectmode",
  3403. (isMultiSelect() ? "multi" : "single"));
  3404. } else {
  3405. target.addAttribute("selectmode", "none");
  3406. }
  3407. if (!isNullSelectionAllowed()) {
  3408. target.addAttribute("nsa", false);
  3409. }
  3410. // selection support
  3411. // The select variable is only enabled if selectable
  3412. if (isSelectable()) {
  3413. target.addVariable(this, "selected", findSelectedKeys());
  3414. }
  3415. }
  3416. private String[] findSelectedKeys() {
  3417. LinkedList<String> selectedKeys = new LinkedList<>();
  3418. if (isMultiSelect()) {
  3419. HashSet<?> sel = new HashSet<Object>((Set<?>) getValue());
  3420. Collection<?> vids = getVisibleItemIds();
  3421. for (Iterator<?> it = vids.iterator(); it.hasNext();) {
  3422. Object id = it.next();
  3423. if (sel.contains(id)) {
  3424. selectedKeys.add(itemIdMapper.key(id));
  3425. }
  3426. }
  3427. } else {
  3428. Object value = getValue();
  3429. if (value == null) {
  3430. value = getNullSelectionItemId();
  3431. }
  3432. if (value != null) {
  3433. selectedKeys.add(itemIdMapper.key(value));
  3434. }
  3435. }
  3436. return selectedKeys.toArray(new String[selectedKeys.size()]);
  3437. }
  3438. private void paintDragMode(PaintTarget target) throws PaintException {
  3439. if (dragMode != TableDragMode.NONE) {
  3440. target.addAttribute("dragmode", dragMode.ordinal());
  3441. }
  3442. }
  3443. private void paintTabIndex(PaintTarget target) throws PaintException {
  3444. // The tab ordering number
  3445. if (getTabIndex() > 0) {
  3446. target.addAttribute("tabindex", getTabIndex());
  3447. }
  3448. }
  3449. private void paintColumnWidth(PaintTarget target, final Object columnId)
  3450. throws PaintException {
  3451. if (columnWidths.containsKey(columnId)) {
  3452. target.addAttribute("width", getColumnWidth(columnId));
  3453. }
  3454. }
  3455. private void paintColumnExpandRatio(PaintTarget target,
  3456. final Object columnId) throws PaintException {
  3457. if (columnExpandRatios.containsKey(columnId)) {
  3458. target.addAttribute("er", getColumnExpandRatio(columnId));
  3459. }
  3460. }
  3461. private void paintTableChildLayoutMeasureMode(PaintTarget target)
  3462. throws PaintException {
  3463. target.addAttribute("measurehint", getChildMeasurementHint().ordinal());
  3464. }
  3465. /**
  3466. * Checks whether row headers are visible.
  3467. *
  3468. * @return {@code false} if row headers are hidden, {@code true} otherwise
  3469. * @since 7.3.9
  3470. */
  3471. protected boolean rowHeadersAreEnabled() {
  3472. return getRowHeaderMode() != RowHeaderMode.HIDDEN;
  3473. }
  3474. private void paintRow(PaintTarget target, final Object[][] cells,
  3475. final boolean iseditable, final Set<Action> actionSet,
  3476. final boolean[] iscomponent, int indexInRowbuffer,
  3477. final Object itemId) throws PaintException {
  3478. target.startTag("tr");
  3479. paintRowAttributes(target, cells, actionSet, indexInRowbuffer, itemId);
  3480. // cells
  3481. int currentColumn = 0;
  3482. for (final Iterator<Object> it = visibleColumns.iterator(); it
  3483. .hasNext(); currentColumn++) {
  3484. final Object columnId = it.next();
  3485. if (columnId == null || isColumnCollapsed(columnId)) {
  3486. continue;
  3487. }
  3488. /*
  3489. * For each cell, if a cellStyleGenerator is specified, get the
  3490. * specific style for the cell. If there is any, add it to the
  3491. * target.
  3492. */
  3493. if (cellStyleGenerator != null) {
  3494. String cellStyle = cellStyleGenerator.getStyle(this, itemId,
  3495. columnId);
  3496. if (cellStyle != null && !cellStyle.equals("")) {
  3497. target.addAttribute("style-" + columnIdMap.key(columnId),
  3498. cellStyle);
  3499. }
  3500. }
  3501. if ((iscomponent[currentColumn] || iseditable
  3502. || cells[CELL_GENERATED_ROW][indexInRowbuffer] != null)
  3503. && Component.class.isInstance(cells[CELL_FIRSTCOL
  3504. + currentColumn][indexInRowbuffer])) {
  3505. final Component c = (Component) cells[CELL_FIRSTCOL
  3506. + currentColumn][indexInRowbuffer];
  3507. if (c == null || !LegacyCommunicationManager
  3508. .isComponentVisibleToClient(c)) {
  3509. target.addText("");
  3510. } else {
  3511. LegacyPaint.paint(c, target);
  3512. }
  3513. } else {
  3514. target.addText((String) cells[CELL_FIRSTCOL
  3515. + currentColumn][indexInRowbuffer]);
  3516. }
  3517. paintCellTooltips(target, itemId, columnId);
  3518. }
  3519. target.endTag("tr");
  3520. }
  3521. private void paintCellTooltips(PaintTarget target, Object itemId,
  3522. Object columnId) throws PaintException {
  3523. if (itemDescriptionGenerator != null) {
  3524. String itemDescription = itemDescriptionGenerator
  3525. .generateDescription(this, itemId, columnId);
  3526. if (itemDescription != null && !itemDescription.equals("")) {
  3527. target.addAttribute("descr-" + columnIdMap.key(columnId),
  3528. itemDescription);
  3529. }
  3530. }
  3531. }
  3532. private void paintRowTooltips(PaintTarget target, Object itemId)
  3533. throws PaintException {
  3534. if (itemDescriptionGenerator != null) {
  3535. String rowDescription = itemDescriptionGenerator
  3536. .generateDescription(this, itemId, null);
  3537. if (rowDescription != null && !rowDescription.equals("")) {
  3538. target.addAttribute("rowdescr", rowDescription);
  3539. }
  3540. }
  3541. }
  3542. private void paintRowAttributes(PaintTarget target, final Object[][] cells,
  3543. final Set<Action> actionSet, int indexInRowbuffer,
  3544. final Object itemId) throws PaintException {
  3545. // tr attributes
  3546. paintRowIcon(target, cells, indexInRowbuffer);
  3547. paintRowHeader(target, cells, indexInRowbuffer);
  3548. paintGeneratedRowInfo(target, cells, indexInRowbuffer);
  3549. target.addAttribute("key",
  3550. Integer.parseInt(cells[CELL_KEY][indexInRowbuffer].toString()));
  3551. if (isSelected(itemId)) {
  3552. target.addAttribute("selected", true);
  3553. }
  3554. // Actions
  3555. if (actionHandlers != null) {
  3556. final ArrayList<String> keys = new ArrayList<>();
  3557. for (Handler ah : actionHandlers) {
  3558. final Action[] aa = ah.getActions(itemId, this);
  3559. if (aa != null) {
  3560. for (int ai = 0; ai < aa.length; ai++) {
  3561. final String key = actionMapper.key(aa[ai]);
  3562. actionSet.add(aa[ai]);
  3563. keys.add(key);
  3564. }
  3565. }
  3566. }
  3567. target.addAttribute("al", keys.toArray());
  3568. }
  3569. /*
  3570. * For each row, if a cellStyleGenerator is specified, get the specific
  3571. * style for the cell, using null as propertyId. If there is any, add it
  3572. * to the target.
  3573. */
  3574. if (cellStyleGenerator != null) {
  3575. String rowStyle = cellStyleGenerator.getStyle(this, itemId, null);
  3576. if (rowStyle != null && !rowStyle.equals("")) {
  3577. target.addAttribute("rowstyle", rowStyle);
  3578. }
  3579. }
  3580. paintRowTooltips(target, itemId);
  3581. paintRowAttributes(target, itemId);
  3582. }
  3583. private void paintGeneratedRowInfo(PaintTarget target, Object[][] cells,
  3584. int indexInRowBuffer) throws PaintException {
  3585. GeneratedRow generatedRow = (GeneratedRow) cells[CELL_GENERATED_ROW][indexInRowBuffer];
  3586. if (generatedRow != null) {
  3587. target.addAttribute("gen_html",
  3588. generatedRow.isHtmlContentAllowed());
  3589. target.addAttribute("gen_span", generatedRow.isSpanColumns());
  3590. target.addAttribute("gen_widget",
  3591. generatedRow.getValue() instanceof Component);
  3592. }
  3593. }
  3594. protected void paintRowHeader(PaintTarget target, Object[][] cells,
  3595. int indexInRowbuffer) throws PaintException {
  3596. if (rowHeadersAreEnabled()) {
  3597. if (cells[CELL_HEADER][indexInRowbuffer] != null) {
  3598. target.addAttribute("caption",
  3599. (String) cells[CELL_HEADER][indexInRowbuffer]);
  3600. }
  3601. }
  3602. }
  3603. protected void paintRowIcon(PaintTarget target, final Object[][] cells,
  3604. int indexInRowbuffer) throws PaintException {
  3605. if (rowHeadersAreEnabled()
  3606. && cells[CELL_ICON][indexInRowbuffer] != null) {
  3607. target.addAttribute("icon",
  3608. (Resource) cells[CELL_ICON][indexInRowbuffer]);
  3609. }
  3610. }
  3611. /**
  3612. * A method where extended Table implementations may add their custom
  3613. * attributes for rows.
  3614. *
  3615. * @param target
  3616. * @param itemId
  3617. */
  3618. protected void paintRowAttributes(PaintTarget target, Object itemId)
  3619. throws PaintException {
  3620. }
  3621. /**
  3622. * Gets the cached visible table contents.
  3623. *
  3624. * @return the cached visible table contents.
  3625. */
  3626. private Object[][] getVisibleCells() {
  3627. if (pageBuffer == null) {
  3628. refreshRenderedCells();
  3629. }
  3630. return pageBuffer;
  3631. }
  3632. /**
  3633. * Gets the value of property.
  3634. *
  3635. * By default if the table is editable the fieldFactory is used to create
  3636. * editors for table cells. Otherwise formatPropertyValue is used to format
  3637. * the value representation.
  3638. *
  3639. * @param rowId
  3640. * the Id of the row (same as item Id).
  3641. * @param colId
  3642. * the Id of the column.
  3643. * @param property
  3644. * the Property to be presented.
  3645. * @return Object Either formatted value or Component for field.
  3646. * @see #setTableFieldFactory(TableFieldFactory)
  3647. */
  3648. protected Object getPropertyValue(Object rowId, Object colId,
  3649. Property property) {
  3650. if (isEditable() && fieldFactory != null) {
  3651. final Field<?> f = fieldFactory
  3652. .createField(getContainerDataSource(), rowId, colId, this);
  3653. if (f != null) {
  3654. // Remember that we have made this association so we can remove
  3655. // it when the component is removed
  3656. associatedProperties.put(f, property);
  3657. bindPropertyToField(rowId, colId, property, f);
  3658. return f;
  3659. }
  3660. }
  3661. return formatPropertyValue(rowId, colId, property);
  3662. }
  3663. /**
  3664. * Binds an item property to a field generated by TableFieldFactory. The
  3665. * default behavior is to bind property straight to LegacyField. If
  3666. * Property.Viewer type property (e.g. PropertyFormatter) is already set for
  3667. * field, the property is bound to that Property.Viewer.
  3668. *
  3669. * @param rowId
  3670. * @param colId
  3671. * @param property
  3672. * @param field
  3673. * @since 6.7.3
  3674. */
  3675. protected void bindPropertyToField(Object rowId, Object colId,
  3676. Property property, Field field) {
  3677. // check if field has a property that is Viewer set. In that case we
  3678. // expect developer has e.g. PropertyFormatter that he wishes to use and
  3679. // assign the property to the Viewer instead.
  3680. boolean hasFilterProperty = field.getPropertyDataSource() != null
  3681. && (field.getPropertyDataSource() instanceof Property.Viewer);
  3682. if (hasFilterProperty) {
  3683. ((Property.Viewer) field.getPropertyDataSource())
  3684. .setPropertyDataSource(property);
  3685. } else {
  3686. field.setPropertyDataSource(property);
  3687. }
  3688. }
  3689. /**
  3690. * Formats table cell property values. By default the property.toString()
  3691. * and return a empty string for null properties.
  3692. *
  3693. * @param rowId
  3694. * the Id of the row (same as item Id).
  3695. * @param colId
  3696. * the Id of the column.
  3697. * @param property
  3698. * the Property to be formatted.
  3699. * @return the String representation of property and its value.
  3700. * @since 3.1
  3701. */
  3702. protected String formatPropertyValue(Object rowId, Object colId,
  3703. Property<?> property) {
  3704. if (property == null) {
  3705. return "";
  3706. }
  3707. Converter<String, Object> converter = null;
  3708. if (hasConverter(colId)) {
  3709. converter = getConverter(colId);
  3710. } else {
  3711. converter = (Converter) ConverterUtil.getConverter(String.class,
  3712. property.getType(), getSession());
  3713. }
  3714. Object value = property.getValue();
  3715. if (converter != null) {
  3716. return converter.convertToPresentation(value, String.class,
  3717. getLocale());
  3718. }
  3719. return (null != value) ? value.toString() : "";
  3720. }
  3721. /* Action container */
  3722. /**
  3723. * Registers a new action handler for this container
  3724. *
  3725. * @see com.vaadin.event.Action.Container#addActionHandler(Action.Handler)
  3726. */
  3727. @Override
  3728. public void addActionHandler(Action.Handler actionHandler) {
  3729. if (actionHandler != null) {
  3730. if (actionHandlers == null) {
  3731. actionHandlers = new LinkedList<>();
  3732. actionMapper = new KeyMapper<>();
  3733. }
  3734. if (!actionHandlers.contains(actionHandler)) {
  3735. actionHandlers.add(actionHandler);
  3736. // Assures the visual refresh. No need to reset the page buffer
  3737. // before as the content has not changed, only the action
  3738. // handlers.
  3739. refreshRenderedCells();
  3740. }
  3741. }
  3742. }
  3743. /**
  3744. * Removes a previously registered action handler for the contents of this
  3745. * container.
  3746. *
  3747. * @see com.vaadin.event.Action.Container#removeActionHandler(Action.Handler)
  3748. */
  3749. @Override
  3750. public void removeActionHandler(Action.Handler actionHandler) {
  3751. if (actionHandlers != null && actionHandlers.contains(actionHandler)) {
  3752. actionHandlers.remove(actionHandler);
  3753. if (actionHandlers.isEmpty()) {
  3754. actionHandlers = null;
  3755. actionMapper = null;
  3756. }
  3757. // Assures the visual refresh. No need to reset the page buffer
  3758. // before as the content has not changed, only the action
  3759. // handlers.
  3760. refreshRenderedCells();
  3761. }
  3762. }
  3763. /**
  3764. * Removes all action handlers
  3765. */
  3766. public void removeAllActionHandlers() {
  3767. actionHandlers = null;
  3768. actionMapper = null;
  3769. // Assures the visual refresh. No need to reset the page buffer
  3770. // before as the content has not changed, only the action
  3771. // handlers.
  3772. refreshRenderedCells();
  3773. }
  3774. /* Property value change listening support */
  3775. /**
  3776. * Notifies this listener that the Property's value has changed.
  3777. *
  3778. * Also listens changes in rendered items to refresh content area.
  3779. *
  3780. * @see com.vaadin.v7.data.Property.ValueChangeListener#valueChange(Property.ValueChangeEvent)
  3781. */
  3782. @Override
  3783. public void valueChange(Property.ValueChangeEvent event) {
  3784. if (equals(event.getProperty())
  3785. || event.getProperty() == getPropertyDataSource()) {
  3786. super.valueChange(event);
  3787. } else {
  3788. refreshRowCache();
  3789. containerChangeToBeRendered = true;
  3790. }
  3791. markAsDirty();
  3792. }
  3793. /**
  3794. * Clears the current page buffer. Call this before
  3795. * {@link #refreshRenderedCells()} to ensure that all content is updated
  3796. * from the properties.
  3797. */
  3798. protected void resetPageBuffer() {
  3799. firstToBeRenderedInClient = -1;
  3800. lastToBeRenderedInClient = -1;
  3801. reqFirstRowToPaint = -1;
  3802. reqRowsToPaint = -1;
  3803. pageBuffer = null;
  3804. }
  3805. /**
  3806. * Notifies the component that it is connected to an application.
  3807. *
  3808. * @see com.vaadin.ui.Component#attach()
  3809. */
  3810. @Override
  3811. public void attach() {
  3812. super.attach();
  3813. refreshRenderedCells();
  3814. }
  3815. /**
  3816. * Notifies the component that it is detached from the application
  3817. *
  3818. * @see com.vaadin.ui.Component#detach()
  3819. */
  3820. @Override
  3821. public void detach() {
  3822. super.detach();
  3823. }
  3824. /**
  3825. * Removes all Items from the Container.
  3826. *
  3827. * @see com.vaadin.v7.data.Container#removeAllItems()
  3828. */
  3829. @Override
  3830. public boolean removeAllItems() {
  3831. currentPageFirstItemId = null;
  3832. currentPageFirstItemIndex = 0;
  3833. return super.removeAllItems();
  3834. }
  3835. /**
  3836. * Removes the Item identified by <code>ItemId</code> from the Container.
  3837. *
  3838. * @see com.vaadin.v7.data.Container#removeItem(Object)
  3839. */
  3840. @Override
  3841. public boolean removeItem(Object itemId) {
  3842. final Object nextItemId = nextItemId(itemId);
  3843. final boolean ret = super.removeItem(itemId);
  3844. if (ret && (itemId != null)
  3845. && (itemId.equals(currentPageFirstItemId))) {
  3846. currentPageFirstItemId = nextItemId;
  3847. }
  3848. if (!(items instanceof Container.ItemSetChangeNotifier)) {
  3849. refreshRowCache();
  3850. }
  3851. return ret;
  3852. }
  3853. /**
  3854. * Removes a Property specified by the given Property ID from the Container.
  3855. *
  3856. * @see com.vaadin.v7.data.Container#removeContainerProperty(Object)
  3857. */
  3858. @Override
  3859. public boolean removeContainerProperty(Object propertyId)
  3860. throws UnsupportedOperationException {
  3861. // If a visible property is removed, remove the corresponding column
  3862. visibleColumns.remove(propertyId);
  3863. columnAlignments.remove(propertyId);
  3864. columnIcons.remove(propertyId);
  3865. columnHeaders.remove(propertyId);
  3866. columnFooters.remove(propertyId);
  3867. // If a propertyValueConverter was defined for the property, remove it.
  3868. propertyValueConverters.remove(propertyId);
  3869. return super.removeContainerProperty(propertyId);
  3870. }
  3871. /**
  3872. * Adds a new property to the table and show it as a visible column.
  3873. *
  3874. * @param propertyId
  3875. * the Id of the property.
  3876. * @param type
  3877. * the class of the property.
  3878. * @param defaultValue
  3879. * the default value given for all existing items.
  3880. * @see com.vaadin.v7.data.Container#addContainerProperty(Object, Class,
  3881. * Object)
  3882. */
  3883. @Override
  3884. public boolean addContainerProperty(Object propertyId, Class<?> type,
  3885. Object defaultValue) throws UnsupportedOperationException {
  3886. boolean visibleColAdded = false;
  3887. if (!visibleColumns.contains(propertyId)) {
  3888. visibleColumns.add(propertyId);
  3889. visibleColAdded = true;
  3890. }
  3891. if (!super.addContainerProperty(propertyId, type, defaultValue)) {
  3892. if (visibleColAdded) {
  3893. visibleColumns.remove(propertyId);
  3894. }
  3895. return false;
  3896. }
  3897. if (!(items instanceof Container.PropertySetChangeNotifier)) {
  3898. refreshRowCache();
  3899. }
  3900. return true;
  3901. }
  3902. /**
  3903. * Adds a new property to the table and show it as a visible column.
  3904. *
  3905. * @param propertyId
  3906. * the Id of the property
  3907. * @param type
  3908. * the class of the property
  3909. * @param defaultValue
  3910. * the default value given for all existing items
  3911. * @param columnHeader
  3912. * the Explicit header of the column. If explicit header is not
  3913. * needed, this should be set null.
  3914. * @param columnIcon
  3915. * the Icon of the column. If icon is not needed, this should be
  3916. * set null.
  3917. * @param columnAlignment
  3918. * the Alignment of the column. Null implies align left.
  3919. * @throws UnsupportedOperationException
  3920. * if the operation is not supported.
  3921. * @see com.vaadin.v7.data.Container#addContainerProperty(Object, Class,
  3922. * Object)
  3923. */
  3924. public boolean addContainerProperty(Object propertyId, Class<?> type,
  3925. Object defaultValue, String columnHeader, Resource columnIcon,
  3926. Align columnAlignment) throws UnsupportedOperationException {
  3927. if (!this.addContainerProperty(propertyId, type, defaultValue)) {
  3928. return false;
  3929. }
  3930. setColumnAlignment(propertyId, columnAlignment);
  3931. setColumnHeader(propertyId, columnHeader);
  3932. setColumnIcon(propertyId, columnIcon);
  3933. return true;
  3934. }
  3935. /**
  3936. * Adds a generated column to the Table.
  3937. * <p>
  3938. * A generated column is a column that exists only in the Table, not as a
  3939. * property in the underlying Container. It shows up just as a regular
  3940. * column.
  3941. * </p>
  3942. * <p>
  3943. * A generated column will override a property with the same id, so that the
  3944. * generated column is shown instead of the column representing the
  3945. * property. Note that getContainerProperty() will still get the real
  3946. * property.
  3947. * </p>
  3948. * <p>
  3949. * Table will not listen to value change events from properties overridden
  3950. * by generated columns. If the content of your generated column depends on
  3951. * properties that are not directly visible in the table, attach value
  3952. * change listener to update the content on all depended properties.
  3953. * Otherwise your UI might not get updated as expected.
  3954. * </p>
  3955. * <p>
  3956. * Also note that getVisibleColumns() will return the generated columns,
  3957. * while getContainerPropertyIds() will not.
  3958. * </p>
  3959. *
  3960. * @param id
  3961. * the id of the column to be added
  3962. * @param generatedColumn
  3963. * the {@link ColumnGenerator} to use for this column
  3964. */
  3965. public void addGeneratedColumn(Object id, ColumnGenerator generatedColumn) {
  3966. if (generatedColumn == null) {
  3967. throw new IllegalArgumentException(
  3968. "Can not add null as a GeneratedColumn");
  3969. }
  3970. if (columnGenerators.containsKey(id)) {
  3971. throw new IllegalArgumentException(
  3972. "Can not add the same GeneratedColumn twice, id:" + id);
  3973. } else {
  3974. columnGenerators.put(id, generatedColumn);
  3975. /*
  3976. * add to visible column list unless already there (overriding
  3977. * column from DS)
  3978. */
  3979. if (!visibleColumns.contains(id)) {
  3980. visibleColumns.add(id);
  3981. }
  3982. refreshRowCache();
  3983. }
  3984. }
  3985. /**
  3986. * Returns the ColumnGenerator used to generate the given column.
  3987. *
  3988. * @param columnId
  3989. * The id of the generated column
  3990. * @return The ColumnGenerator used for the given columnId or null.
  3991. */
  3992. public ColumnGenerator getColumnGenerator(Object columnId)
  3993. throws IllegalArgumentException {
  3994. return columnGenerators.get(columnId);
  3995. }
  3996. /**
  3997. * Removes a generated column previously added with addGeneratedColumn.
  3998. *
  3999. * @param columnId
  4000. * id of the generated column to remove
  4001. * @return true if the column could be removed (existed in the Table)
  4002. */
  4003. public boolean removeGeneratedColumn(Object columnId) {
  4004. if (columnGenerators.containsKey(columnId)) {
  4005. columnGenerators.remove(columnId);
  4006. // remove column from visibleColumns list unless it exists in
  4007. // container (generator previously overrode this column)
  4008. if (!items.getContainerPropertyIds().contains(columnId)) {
  4009. visibleColumns.remove(columnId);
  4010. }
  4011. refreshRowCache();
  4012. return true;
  4013. } else {
  4014. return false;
  4015. }
  4016. }
  4017. /**
  4018. * Returns item identifiers of the items which are currently rendered on the
  4019. * client.
  4020. * <p>
  4021. * Note, that some due to historical reasons the name of the method is bit
  4022. * misleading. Some items may be partly or totally out of the viewport of
  4023. * the table's scrollable area. Actually detecting rows which can be
  4024. * actually seen by the end user may be problematic due to the client server
  4025. * architecture. Using {@link #getCurrentPageFirstItemId()} combined with
  4026. * {@link #getPageLength()} may produce good enough estimates in some
  4027. * situations.
  4028. *
  4029. * @see com.vaadin.v7.ui.Select#getVisibleItemIds()
  4030. */
  4031. @Override
  4032. public Collection<?> getVisibleItemIds() {
  4033. final LinkedList<Object> visible = new LinkedList<>();
  4034. final Object[][] cells = getVisibleCells();
  4035. // may be null if the table has not been rendered yet (e.g. not attached
  4036. // to a layout)
  4037. if (null != cells) {
  4038. for (int i = 0; i < cells[CELL_ITEMID].length; i++) {
  4039. visible.add(cells[CELL_ITEMID][i]);
  4040. }
  4041. }
  4042. return visible;
  4043. }
  4044. /**
  4045. * Container datasource item set change. Table must flush its buffers on
  4046. * change.
  4047. *
  4048. * @see com.vaadin.v7.data.Container.ItemSetChangeListener#containerItemSetChange(com.vaadin.v7.data.Container.ItemSetChangeEvent)
  4049. */
  4050. @Override
  4051. public void containerItemSetChange(Container.ItemSetChangeEvent event) {
  4052. if (isBeingPainted) {
  4053. return;
  4054. }
  4055. super.containerItemSetChange(event);
  4056. // super method clears the key map, must inform client about this to
  4057. // avoid getting invalid keys back (#8584)
  4058. keyMapperReset = true;
  4059. int currentFirstItemIndex = getCurrentPageFirstItemIndex();
  4060. if (event.getContainer().size() == 0) {
  4061. repairOnReAddAllRowsDataScrollPositionItemIndex = getCurrentPageFirstItemIndex();
  4062. } else {
  4063. if (repairOnReAddAllRowsDataScrollPositionItemIndex != -1) {
  4064. currentFirstItemIndex = repairOnReAddAllRowsDataScrollPositionItemIndex;
  4065. /*
  4066. * Reset repairOnReAddAllRowsDataScrollPositionItemIndex.
  4067. *
  4068. * Next string should be commented (removed) if we want to have
  4069. * possibility to restore scroll position during adding items to
  4070. * container one by one via add() but not only addAll(). The
  4071. * problem in this case: we cannot track what happened between
  4072. * add() and add()... So it is ambiguous where to stop restore
  4073. * scroll position.
  4074. */
  4075. repairOnReAddAllRowsDataScrollPositionItemIndex = -1;
  4076. }
  4077. }
  4078. // ensure that page still has first item in page, ignore buffer refresh
  4079. // (forced in this method)
  4080. setCurrentPageFirstItemIndex(currentFirstItemIndex, false);
  4081. refreshRowCache();
  4082. }
  4083. /**
  4084. * Container datasource property set change. Table must flush its buffers on
  4085. * change.
  4086. *
  4087. * @see com.vaadin.v7.data.Container.PropertySetChangeListener#containerPropertySetChange(com.vaadin.v7.data.Container.PropertySetChangeEvent)
  4088. */
  4089. @Override
  4090. public void containerPropertySetChange(
  4091. Container.PropertySetChangeEvent event) {
  4092. if (isBeingPainted) {
  4093. return;
  4094. }
  4095. disableContentRefreshing();
  4096. super.containerPropertySetChange(event);
  4097. // sanitize visibleColumns. note that we are not adding previously
  4098. // non-existing properties as columns
  4099. Collection<?> containerPropertyIds = getContainerDataSource()
  4100. .getContainerPropertyIds();
  4101. LinkedList<Object> newVisibleColumns = new LinkedList<>(visibleColumns);
  4102. for (Iterator<Object> iterator = newVisibleColumns.iterator(); iterator
  4103. .hasNext();) {
  4104. Object id = iterator.next();
  4105. if (!(containerPropertyIds.contains(id)
  4106. || columnGenerators.containsKey(id))) {
  4107. iterator.remove();
  4108. }
  4109. }
  4110. setVisibleColumns(newVisibleColumns.toArray());
  4111. // same for collapsed columns
  4112. for (Iterator<Object> iterator = collapsedColumns.iterator(); iterator
  4113. .hasNext();) {
  4114. Object id = iterator.next();
  4115. if (!(containerPropertyIds.contains(id)
  4116. || columnGenerators.containsKey(id))) {
  4117. iterator.remove();
  4118. }
  4119. }
  4120. resetPageBuffer();
  4121. enableContentRefreshing(true);
  4122. }
  4123. /**
  4124. * Adding new items is not supported.
  4125. *
  4126. * @throws UnsupportedOperationException
  4127. * if set to true.
  4128. * @see com.vaadin.v7.ui.Select#setNewItemsAllowed(boolean)
  4129. */
  4130. @Override
  4131. public void setNewItemsAllowed(boolean allowNewOptions)
  4132. throws UnsupportedOperationException {
  4133. if (allowNewOptions) {
  4134. throw new UnsupportedOperationException();
  4135. }
  4136. }
  4137. /**
  4138. * Gets the ID of the Item following the Item that corresponds to itemId.
  4139. *
  4140. * @see com.vaadin.v7.data.Container.Ordered#nextItemId(java.lang.Object)
  4141. */
  4142. @Override
  4143. public Object nextItemId(Object itemId) {
  4144. return ((Container.Ordered) items).nextItemId(itemId);
  4145. }
  4146. /**
  4147. * Gets the ID of the Item preceding the Item that corresponds to the
  4148. * itemId.
  4149. *
  4150. * @see com.vaadin.v7.data.Container.Ordered#prevItemId(java.lang.Object)
  4151. */
  4152. @Override
  4153. public Object prevItemId(Object itemId) {
  4154. return ((Container.Ordered) items).prevItemId(itemId);
  4155. }
  4156. /**
  4157. * Gets the ID of the first Item in the Container.
  4158. *
  4159. * @see com.vaadin.v7.data.Container.Ordered#firstItemId()
  4160. */
  4161. @Override
  4162. public Object firstItemId() {
  4163. return ((Container.Ordered) items).firstItemId();
  4164. }
  4165. /**
  4166. * Gets the ID of the last Item in the Container.
  4167. *
  4168. * @see com.vaadin.v7.data.Container.Ordered#lastItemId()
  4169. */
  4170. @Override
  4171. public Object lastItemId() {
  4172. return ((Container.Ordered) items).lastItemId();
  4173. }
  4174. /**
  4175. * Tests if the Item corresponding to the given Item ID is the first Item in
  4176. * the Container.
  4177. *
  4178. * @see com.vaadin.v7.data.Container.Ordered#isFirstId(java.lang.Object)
  4179. */
  4180. @Override
  4181. public boolean isFirstId(Object itemId) {
  4182. return ((Container.Ordered) items).isFirstId(itemId);
  4183. }
  4184. /**
  4185. * Tests if the Item corresponding to the given Item ID is the last Item in
  4186. * the Container.
  4187. *
  4188. * @see com.vaadin.v7.data.Container.Ordered#isLastId(java.lang.Object)
  4189. */
  4190. @Override
  4191. public boolean isLastId(Object itemId) {
  4192. return ((Container.Ordered) items).isLastId(itemId);
  4193. }
  4194. /**
  4195. * Adds new item after the given item.
  4196. *
  4197. * @see com.vaadin.v7.data.Container.Ordered#addItemAfter(java.lang.Object)
  4198. */
  4199. @Override
  4200. public Object addItemAfter(Object previousItemId)
  4201. throws UnsupportedOperationException {
  4202. Object itemId = ((Container.Ordered) items)
  4203. .addItemAfter(previousItemId);
  4204. if (!(items instanceof Container.ItemSetChangeNotifier)) {
  4205. refreshRowCache();
  4206. }
  4207. return itemId;
  4208. }
  4209. /**
  4210. * Adds new item after the given item.
  4211. *
  4212. * @see com.vaadin.v7.data.Container.Ordered#addItemAfter(java.lang.Object,
  4213. * java.lang.Object)
  4214. */
  4215. @Override
  4216. public Item addItemAfter(Object previousItemId, Object newItemId)
  4217. throws UnsupportedOperationException {
  4218. Item item = ((Container.Ordered) items).addItemAfter(previousItemId,
  4219. newItemId);
  4220. if (!(items instanceof Container.ItemSetChangeNotifier)) {
  4221. refreshRowCache();
  4222. }
  4223. return item;
  4224. }
  4225. /**
  4226. * Sets the TableFieldFactory that is used to create editor for table cells.
  4227. *
  4228. * The TableFieldFactory is only used if the Table is editable. By default
  4229. * the DefaultFieldFactory is used.
  4230. *
  4231. * @param fieldFactory
  4232. * the field factory to set.
  4233. * @see #isEditable
  4234. * @see DefaultFieldFactory
  4235. */
  4236. public void setTableFieldFactory(TableFieldFactory fieldFactory) {
  4237. this.fieldFactory = fieldFactory;
  4238. // Assure visual refresh
  4239. refreshRowCache();
  4240. }
  4241. /**
  4242. * Gets the TableFieldFactory that is used to create editor for table cells.
  4243. *
  4244. * The FieldFactory is only used if the Table is editable.
  4245. *
  4246. * @return TableFieldFactory used to create the LegacyField instances.
  4247. * @see #isEditable
  4248. */
  4249. public TableFieldFactory getTableFieldFactory() {
  4250. return fieldFactory;
  4251. }
  4252. /**
  4253. * Is table editable.
  4254. *
  4255. * If table is editable a editor of type LegacyField is created for each
  4256. * table cell. The assigned FieldFactory is used to create the instances.
  4257. *
  4258. * To provide custom editors for table cells create a class implementing the
  4259. * FieldFactory interface, and assign it to table, and set the editable
  4260. * property to true.
  4261. *
  4262. * @return true if table is editable, false otherwise.
  4263. * @see Field
  4264. * @see FieldFactory
  4265. *
  4266. */
  4267. public boolean isEditable() {
  4268. return editable;
  4269. }
  4270. /**
  4271. * Sets the editable property.
  4272. *
  4273. * If table is editable a editor of type LegacyField is created for each
  4274. * table cell. The assigned FieldFactory is used to create the instances.
  4275. *
  4276. * To provide custom editors for table cells create a class implementing the
  4277. * FieldFactory interface, and assign it to table, and set the editable
  4278. * property to true.
  4279. *
  4280. * @param editable
  4281. * true if table should be editable by user.
  4282. * @see Field
  4283. * @see FieldFactory
  4284. *
  4285. */
  4286. public void setEditable(boolean editable) {
  4287. this.editable = editable;
  4288. // Assure visual refresh
  4289. refreshRowCache();
  4290. }
  4291. /**
  4292. * Sorts the table.
  4293. *
  4294. * @throws UnsupportedOperationException
  4295. * if the container data source does not implement
  4296. * Container.Sortable
  4297. * @see com.vaadin.v7.data.Container.Sortable#sort(java.lang.Object[],
  4298. * boolean[])
  4299. *
  4300. */
  4301. @Override
  4302. public void sort(Object[] propertyId, boolean[] ascending)
  4303. throws UnsupportedOperationException {
  4304. final Container c = getContainerDataSource();
  4305. if (c instanceof Container.Sortable) {
  4306. final int pageIndex = getCurrentPageFirstItemIndex();
  4307. boolean refreshingPreviouslyEnabled = disableContentRefreshing();
  4308. ((Container.Sortable) c).sort(propertyId, ascending);
  4309. setCurrentPageFirstItemIndex(pageIndex);
  4310. if (refreshingPreviouslyEnabled) {
  4311. enableContentRefreshing(true);
  4312. }
  4313. if (propertyId.length > 0 && ascending.length > 0) {
  4314. // The first propertyId is the primary sorting criterion,
  4315. // therefore the sort indicator should be there
  4316. sortAscending = ascending[0];
  4317. sortContainerPropertyId = propertyId[0];
  4318. } else {
  4319. sortAscending = true;
  4320. sortContainerPropertyId = null;
  4321. }
  4322. } else if (c != null) {
  4323. throw new UnsupportedOperationException(
  4324. "Underlying Data does not allow sorting");
  4325. }
  4326. }
  4327. /**
  4328. * Sorts the table by currently selected sorting column.
  4329. *
  4330. * @throws UnsupportedOperationException
  4331. * if the container data source does not implement
  4332. * Container.Sortable
  4333. */
  4334. public void sort() {
  4335. if (getSortContainerPropertyId() == null) {
  4336. return;
  4337. }
  4338. sort(new Object[] { sortContainerPropertyId },
  4339. new boolean[] { sortAscending });
  4340. }
  4341. /**
  4342. * Gets the container property IDs, which can be used to sort the item.
  4343. * <p>
  4344. * Note that the {@link #isSortEnabled()} state affects what this method
  4345. * returns. Disabling sorting causes this method to always return an empty
  4346. * collection.
  4347. * </p>
  4348. *
  4349. * @see com.vaadin.v7.data.Container.Sortable#getSortableContainerPropertyIds()
  4350. */
  4351. @Override
  4352. public Collection<?> getSortableContainerPropertyIds() {
  4353. final Container c = getContainerDataSource();
  4354. if (c instanceof Container.Sortable && isSortEnabled()) {
  4355. return ((Container.Sortable) c).getSortableContainerPropertyIds();
  4356. } else {
  4357. return Collections.EMPTY_LIST;
  4358. }
  4359. }
  4360. /**
  4361. * Gets the currently sorted column property ID.
  4362. *
  4363. * @return the Container property id of the currently sorted column.
  4364. */
  4365. public Object getSortContainerPropertyId() {
  4366. return sortContainerPropertyId;
  4367. }
  4368. /**
  4369. * Sets the currently sorted column property id.
  4370. *
  4371. * @param propertyId
  4372. * the Container property id of the currently sorted column.
  4373. */
  4374. public void setSortContainerPropertyId(Object propertyId) {
  4375. setSortContainerPropertyId(propertyId, true);
  4376. }
  4377. /**
  4378. * Internal method to set currently sorted column property id. With doSort
  4379. * flag actual sorting may be bypassed.
  4380. *
  4381. * @param propertyId
  4382. * @param doSort
  4383. */
  4384. private void setSortContainerPropertyId(Object propertyId, boolean doSort) {
  4385. if ((sortContainerPropertyId != null
  4386. && !sortContainerPropertyId.equals(propertyId))
  4387. || (sortContainerPropertyId == null && propertyId != null)) {
  4388. sortContainerPropertyId = propertyId;
  4389. if (doSort) {
  4390. sort();
  4391. // Assures the visual refresh. This should not be necessary as
  4392. // sort() calls refreshRowCache
  4393. refreshRenderedCells();
  4394. }
  4395. }
  4396. }
  4397. /**
  4398. * Is the table currently sorted in ascending order.
  4399. *
  4400. * @return <code>true</code> if ascending, <code>false</code> if descending.
  4401. */
  4402. public boolean isSortAscending() {
  4403. return sortAscending;
  4404. }
  4405. /**
  4406. * Sets the table in ascending order.
  4407. *
  4408. * @param ascending
  4409. * <code>true</code> if ascending, <code>false</code> if
  4410. * descending.
  4411. */
  4412. public void setSortAscending(boolean ascending) {
  4413. setSortAscending(ascending, true);
  4414. }
  4415. /**
  4416. * Internal method to set sort ascending. With doSort flag actual sort can
  4417. * be bypassed.
  4418. *
  4419. * @param ascending
  4420. * @param doSort
  4421. */
  4422. private void setSortAscending(boolean ascending, boolean doSort) {
  4423. if (sortAscending != ascending) {
  4424. sortAscending = ascending;
  4425. if (doSort) {
  4426. sort();
  4427. // Assures the visual refresh. This should not be necessary as
  4428. // sort() calls refreshRowCache
  4429. refreshRenderedCells();
  4430. }
  4431. }
  4432. }
  4433. /**
  4434. * Is sorting disabled altogether.
  4435. *
  4436. * True iff no sortable columns are given even in the case where data source
  4437. * would support this.
  4438. *
  4439. * @return True iff sorting is disabled.
  4440. * @deprecated As of 7.0, use {@link #isSortEnabled()} instead
  4441. */
  4442. @Deprecated
  4443. public boolean isSortDisabled() {
  4444. return !isSortEnabled();
  4445. }
  4446. /**
  4447. * Checks if sorting is enabled.
  4448. *
  4449. * @return true if sorting by the user is allowed, false otherwise
  4450. */
  4451. public boolean isSortEnabled() {
  4452. return sortEnabled;
  4453. }
  4454. /**
  4455. * Disables the sorting by the user altogether.
  4456. *
  4457. * @param sortDisabled
  4458. * True iff sorting is disabled.
  4459. * @deprecated As of 7.0, use {@link #setSortEnabled(boolean)} instead
  4460. */
  4461. @Deprecated
  4462. public void setSortDisabled(boolean sortDisabled) {
  4463. setSortEnabled(!sortDisabled);
  4464. }
  4465. /**
  4466. * Enables or disables sorting.
  4467. * <p>
  4468. * Setting this to false disallows sorting by the user. It is still possible
  4469. * to call {@link #sort()}.
  4470. * </p>
  4471. *
  4472. * @param sortEnabled
  4473. * true to allow the user to sort the table, false to disallow it
  4474. */
  4475. public void setSortEnabled(boolean sortEnabled) {
  4476. if (this.sortEnabled != sortEnabled) {
  4477. this.sortEnabled = sortEnabled;
  4478. markAsDirty();
  4479. }
  4480. }
  4481. /**
  4482. * Used to create "generated columns"; columns that exist only in the Table,
  4483. * not in the underlying Container. Implement this interface and pass it to
  4484. * Table.addGeneratedColumn along with an id for the column to be generated.
  4485. *
  4486. */
  4487. @Deprecated
  4488. public interface ColumnGenerator extends Serializable {
  4489. /**
  4490. * Called by Table when a cell in a generated column needs to be
  4491. * generated.
  4492. *
  4493. * @param source
  4494. * the source Table
  4495. * @param itemId
  4496. * the itemId (aka rowId) for the of the cell to be generated
  4497. * @param columnId
  4498. * the id for the generated column (as specified in
  4499. * addGeneratedColumn)
  4500. * @return A {@link Component} that should be rendered in the cell or a
  4501. * {@link String} that should be displayed in the cell. Other
  4502. * return values are not supported.
  4503. */
  4504. public abstract Object generateCell(Table source, Object itemId,
  4505. Object columnId);
  4506. }
  4507. /**
  4508. * Set cell style generator for Table.
  4509. *
  4510. * @param cellStyleGenerator
  4511. * New cell style generator or null to remove generator.
  4512. */
  4513. public void setCellStyleGenerator(CellStyleGenerator cellStyleGenerator) {
  4514. this.cellStyleGenerator = cellStyleGenerator;
  4515. // Assures the visual refresh. No need to reset the page buffer
  4516. // before as the content has not changed, only the style generators
  4517. refreshRenderedCells();
  4518. }
  4519. /**
  4520. * Get the current cell style generator.
  4521. *
  4522. */
  4523. public CellStyleGenerator getCellStyleGenerator() {
  4524. return cellStyleGenerator;
  4525. }
  4526. /**
  4527. * Allow to define specific style on cells (and rows) contents. Implements
  4528. * this interface and pass it to Table.setCellStyleGenerator. Row styles are
  4529. * generated when porpertyId is null. The CSS class name that will be added
  4530. * to the cell content is <tt>v-table-cell-content-[style name]</tt>, and
  4531. * the row style will be <tt>v-table-row-[style name]</tt>.
  4532. */
  4533. @Deprecated
  4534. public interface CellStyleGenerator extends Serializable {
  4535. /**
  4536. * Called by Table when a cell (and row) is painted.
  4537. *
  4538. * @param source
  4539. * the source Table
  4540. * @param itemId
  4541. * The itemId of the painted cell
  4542. * @param propertyId
  4543. * The propertyId of the cell, null when getting row style
  4544. * @return The style name to add to this cell or row. (the CSS class
  4545. * name will be v-table-cell-content-[style name], or
  4546. * v-table-row-[style name] for rows)
  4547. */
  4548. public abstract String getStyle(Table source, Object itemId,
  4549. Object propertyId);
  4550. }
  4551. @Override
  4552. public void addItemClickListener(ItemClickListener listener) {
  4553. addListener(TableConstants.ITEM_CLICK_EVENT_ID, ItemClickEvent.class,
  4554. listener, ItemClickEvent.ITEM_CLICK_METHOD);
  4555. }
  4556. /**
  4557. * @deprecated As of 7.0, replaced by
  4558. * {@link #addItemClickListener(ItemClickListener)}
  4559. **/
  4560. @Override
  4561. @Deprecated
  4562. public void addListener(ItemClickListener listener) {
  4563. addItemClickListener(listener);
  4564. }
  4565. @Override
  4566. public void removeItemClickListener(ItemClickListener listener) {
  4567. removeListener(TableConstants.ITEM_CLICK_EVENT_ID, ItemClickEvent.class,
  4568. listener);
  4569. }
  4570. /**
  4571. * @deprecated As of 7.0, replaced by
  4572. * {@link #removeItemClickListener(ItemClickListener)}
  4573. **/
  4574. @Override
  4575. @Deprecated
  4576. public void removeListener(ItemClickListener listener) {
  4577. removeItemClickListener(listener);
  4578. }
  4579. // Identical to AbstractCompoenentContainer.setEnabled();
  4580. @Override
  4581. public void setEnabled(boolean enabled) {
  4582. super.setEnabled(enabled);
  4583. if (getParent() != null && !getParent().isEnabled()) {
  4584. // some ancestor still disabled, don't update children
  4585. return;
  4586. } else {
  4587. markAsDirtyRecursive();
  4588. }
  4589. }
  4590. /**
  4591. * Sets the drag start mode of the Table. Drag start mode controls how Table
  4592. * behaves as a drag source.
  4593. *
  4594. * @param newDragMode
  4595. */
  4596. public void setDragMode(TableDragMode newDragMode) {
  4597. dragMode = newDragMode;
  4598. markAsDirty();
  4599. }
  4600. /**
  4601. * @return the current start mode of the Table. Drag start mode controls how
  4602. * Table behaves as a drag source.
  4603. */
  4604. public TableDragMode getDragMode() {
  4605. return dragMode;
  4606. }
  4607. /**
  4608. * Concrete implementation of {@link DataBoundTransferable} for data
  4609. * transferred from a table.
  4610. *
  4611. * @see DataBoundTransferable
  4612. *
  4613. * @since 6.3
  4614. */
  4615. @Deprecated
  4616. public class TableTransferable extends DataBoundTransferable {
  4617. protected TableTransferable(Map<String, Object> rawVariables) {
  4618. super(Table.this, rawVariables);
  4619. Object object = rawVariables.get("itemId");
  4620. if (object != null) {
  4621. setData("itemId", itemIdMapper.get((String) object));
  4622. }
  4623. object = rawVariables.get("propertyId");
  4624. if (object != null) {
  4625. setData("propertyId", columnIdMap.get((String) object));
  4626. }
  4627. }
  4628. @Override
  4629. public Object getItemId() {
  4630. return getData("itemId");
  4631. }
  4632. @Override
  4633. public Object getPropertyId() {
  4634. return getData("propertyId");
  4635. }
  4636. @Override
  4637. public Table getSourceComponent() {
  4638. return (Table) super.getSourceComponent();
  4639. }
  4640. }
  4641. @Override
  4642. public TableTransferable getTransferable(Map<String, Object> rawVariables) {
  4643. TableTransferable transferable = new TableTransferable(rawVariables);
  4644. return transferable;
  4645. }
  4646. @Override
  4647. public DropHandler getDropHandler() {
  4648. return dropHandler;
  4649. }
  4650. public void setDropHandler(DropHandler dropHandler) {
  4651. this.dropHandler = dropHandler;
  4652. }
  4653. @Override
  4654. public AbstractSelectTargetDetails translateDropTargetDetails(
  4655. Map<String, Object> clientVariables) {
  4656. return new AbstractSelectTargetDetails(clientVariables);
  4657. }
  4658. /**
  4659. * Sets the behavior of how the multi-select mode should behave when the
  4660. * table is both selectable and in multi-select mode.
  4661. * <p>
  4662. * Note, that on some clients the mode may not be respected. E.g. on touch
  4663. * based devices CTRL/SHIFT base selection method is invalid, so touch based
  4664. * browsers always use the {@link MultiSelectMode#SIMPLE}.
  4665. *
  4666. * @param mode
  4667. * The select mode of the table
  4668. */
  4669. public void setMultiSelectMode(MultiSelectMode mode) {
  4670. multiSelectMode = mode;
  4671. markAsDirty();
  4672. }
  4673. /**
  4674. * Returns the select mode in which multi-select is used.
  4675. *
  4676. * @return The multi select mode
  4677. */
  4678. public MultiSelectMode getMultiSelectMode() {
  4679. return multiSelectMode;
  4680. }
  4681. /**
  4682. * Lazy loading accept criterion for Table. Accepted target rows are loaded
  4683. * from server once per drag and drop operation. Developer must override one
  4684. * method that decides on which rows the currently dragged data can be
  4685. * dropped.
  4686. *
  4687. * <p>
  4688. * Initially pretty much no data is sent to client. On first required
  4689. * criterion check (per drag request) the client side data structure is
  4690. * initialized from server and no subsequent requests requests are needed
  4691. * during that drag and drop operation.
  4692. */
  4693. @Deprecated
  4694. public static abstract class TableDropCriterion
  4695. extends ServerSideCriterion {
  4696. private Table table;
  4697. private Set<Object> allowedItemIds;
  4698. /*
  4699. * (non-Javadoc)
  4700. *
  4701. * @see
  4702. * com.vaadin.event.dd.acceptcriteria.ServerSideCriterion#getIdentifier
  4703. * ()
  4704. */
  4705. @Override
  4706. protected String getIdentifier() {
  4707. return TableDropCriterion.class.getCanonicalName();
  4708. }
  4709. /*
  4710. * (non-Javadoc)
  4711. *
  4712. * @see
  4713. * com.vaadin.event.dd.acceptcriteria.AcceptCriterion#accepts(com.vaadin
  4714. * .event.dd.DragAndDropEvent)
  4715. */
  4716. @Override
  4717. @SuppressWarnings("unchecked")
  4718. public boolean accept(DragAndDropEvent dragEvent) {
  4719. AbstractSelectTargetDetails dropTargetData = (AbstractSelectTargetDetails) dragEvent
  4720. .getTargetDetails();
  4721. table = (Table) dragEvent.getTargetDetails().getTarget();
  4722. Collection<?> visibleItemIds = table.getVisibleItemIds();
  4723. allowedItemIds = getAllowedItemIds(dragEvent, table,
  4724. (Collection<Object>) visibleItemIds);
  4725. return allowedItemIds.contains(dropTargetData.getItemIdOver());
  4726. }
  4727. /*
  4728. * (non-Javadoc)
  4729. *
  4730. * @see
  4731. * com.vaadin.event.dd.acceptcriteria.AcceptCriterion#paintResponse(
  4732. * com.vaadin.server.PaintTarget)
  4733. */
  4734. @Override
  4735. public void paintResponse(PaintTarget target) throws PaintException {
  4736. /*
  4737. * send allowed nodes to client so subsequent requests can be
  4738. * avoided
  4739. */
  4740. Object[] array = allowedItemIds.toArray();
  4741. for (int i = 0; i < array.length; i++) {
  4742. String key = table.itemIdMapper.key(array[i]);
  4743. array[i] = key;
  4744. }
  4745. target.addAttribute("allowedIds", array);
  4746. }
  4747. /**
  4748. * @param dragEvent
  4749. * @param table
  4750. * the table for which the allowed item identifiers are
  4751. * defined
  4752. * @param visibleItemIds
  4753. * the list of currently rendered item identifiers, accepted
  4754. * item id's need to be detected only for these visible items
  4755. * @return the set of identifiers for items on which the dragEvent will
  4756. * be accepted
  4757. */
  4758. protected abstract Set<Object> getAllowedItemIds(
  4759. DragAndDropEvent dragEvent, Table table,
  4760. Collection<Object> visibleItemIds);
  4761. }
  4762. /**
  4763. * Click event fired when clicking on the Table headers. The event includes
  4764. * a reference the the Table the event originated from, the property id of
  4765. * the column which header was pressed and details about the mouse event
  4766. * itself.
  4767. */
  4768. @Deprecated
  4769. public static class HeaderClickEvent extends ClickEvent {
  4770. public static final Method HEADER_CLICK_METHOD;
  4771. static {
  4772. try {
  4773. // Set the header click method
  4774. HEADER_CLICK_METHOD = HeaderClickListener.class
  4775. .getDeclaredMethod("headerClick",
  4776. new Class[] { HeaderClickEvent.class });
  4777. } catch (final java.lang.NoSuchMethodException e) {
  4778. // This should never happen
  4779. throw new java.lang.RuntimeException(e);
  4780. }
  4781. }
  4782. // The property id of the column which header was pressed
  4783. private final Object columnPropertyId;
  4784. public HeaderClickEvent(Component source, Object propertyId,
  4785. MouseEventDetails details) {
  4786. super(source, details);
  4787. columnPropertyId = propertyId;
  4788. }
  4789. /**
  4790. * Gets the property id of the column which header was pressed
  4791. *
  4792. * @return The column property id
  4793. */
  4794. public Object getPropertyId() {
  4795. return columnPropertyId;
  4796. }
  4797. }
  4798. /**
  4799. * Click event fired when clicking on the Table footers. The event includes
  4800. * a reference the the Table the event originated from, the property id of
  4801. * the column which header was pressed and details about the mouse event
  4802. * itself.
  4803. */
  4804. @Deprecated
  4805. public static class FooterClickEvent extends ClickEvent {
  4806. public static final Method FOOTER_CLICK_METHOD;
  4807. static {
  4808. try {
  4809. // Set the header click method
  4810. FOOTER_CLICK_METHOD = FooterClickListener.class
  4811. .getDeclaredMethod("footerClick",
  4812. new Class[] { FooterClickEvent.class });
  4813. } catch (final java.lang.NoSuchMethodException e) {
  4814. // This should never happen
  4815. throw new java.lang.RuntimeException(e);
  4816. }
  4817. }
  4818. // The property id of the column which header was pressed
  4819. private final Object columnPropertyId;
  4820. /**
  4821. * Constructor
  4822. *
  4823. * @param source
  4824. * The source of the component
  4825. * @param propertyId
  4826. * The propertyId of the column
  4827. * @param details
  4828. * The mouse details of the click
  4829. */
  4830. public FooterClickEvent(Component source, Object propertyId,
  4831. MouseEventDetails details) {
  4832. super(source, details);
  4833. columnPropertyId = propertyId;
  4834. }
  4835. /**
  4836. * Gets the property id of the column which header was pressed
  4837. *
  4838. * @return The column property id
  4839. */
  4840. public Object getPropertyId() {
  4841. return columnPropertyId;
  4842. }
  4843. }
  4844. /**
  4845. * Interface for the listener for column header mouse click events. The
  4846. * headerClick method is called when the user presses a header column cell.
  4847. */
  4848. @Deprecated
  4849. public interface HeaderClickListener extends Serializable {
  4850. /**
  4851. * Called when a user clicks a header column cell
  4852. *
  4853. * @param event
  4854. * The event which contains information about the column and
  4855. * the mouse click event
  4856. */
  4857. public void headerClick(HeaderClickEvent event);
  4858. }
  4859. /**
  4860. * Interface for the listener for column footer mouse click events. The
  4861. * footerClick method is called when the user presses a footer column cell.
  4862. */
  4863. @Deprecated
  4864. public interface FooterClickListener extends Serializable {
  4865. /**
  4866. * Called when a user clicks a footer column cell
  4867. *
  4868. * @param event
  4869. * The event which contains information about the column and
  4870. * the mouse click event
  4871. */
  4872. public void footerClick(FooterClickEvent event);
  4873. }
  4874. /**
  4875. * Adds a header click listener which handles the click events when the user
  4876. * clicks on a column header cell in the Table.
  4877. * <p>
  4878. * The listener will receive events which contain information about which
  4879. * column was clicked and some details about the mouse event.
  4880. * </p>
  4881. *
  4882. * @param listener
  4883. * The handler which should handle the header click events.
  4884. */
  4885. public void addHeaderClickListener(HeaderClickListener listener) {
  4886. addListener(TableConstants.HEADER_CLICK_EVENT_ID,
  4887. HeaderClickEvent.class, listener,
  4888. HeaderClickEvent.HEADER_CLICK_METHOD);
  4889. }
  4890. /**
  4891. * @deprecated As of 7.0, replaced by
  4892. * {@link #addHeaderClickListener(HeaderClickListener)}
  4893. **/
  4894. @Deprecated
  4895. public void addListener(HeaderClickListener listener) {
  4896. addHeaderClickListener(listener);
  4897. }
  4898. /**
  4899. * Removes a header click listener
  4900. *
  4901. * @param listener
  4902. * The listener to remove.
  4903. */
  4904. public void removeHeaderClickListener(HeaderClickListener listener) {
  4905. removeListener(TableConstants.HEADER_CLICK_EVENT_ID,
  4906. HeaderClickEvent.class, listener);
  4907. }
  4908. /**
  4909. * @deprecated As of 7.0, replaced by
  4910. * {@link #removeHeaderClickListener(HeaderClickListener)}
  4911. **/
  4912. @Deprecated
  4913. public void removeListener(HeaderClickListener listener) {
  4914. removeHeaderClickListener(listener);
  4915. }
  4916. /**
  4917. * Adds a footer click listener which handles the click events when the user
  4918. * clicks on a column footer cell in the Table.
  4919. * <p>
  4920. * The listener will receive events which contain information about which
  4921. * column was clicked and some details about the mouse event.
  4922. * </p>
  4923. *
  4924. * @param listener
  4925. * The handler which should handle the footer click events.
  4926. */
  4927. public void addFooterClickListener(FooterClickListener listener) {
  4928. addListener(TableConstants.FOOTER_CLICK_EVENT_ID,
  4929. FooterClickEvent.class, listener,
  4930. FooterClickEvent.FOOTER_CLICK_METHOD);
  4931. }
  4932. /**
  4933. * @deprecated As of 7.0, replaced by
  4934. * {@link #addFooterClickListener(FooterClickListener)}
  4935. **/
  4936. @Deprecated
  4937. public void addListener(FooterClickListener listener) {
  4938. addFooterClickListener(listener);
  4939. }
  4940. /**
  4941. * Removes a footer click listener
  4942. *
  4943. * @param listener
  4944. * The listener to remove.
  4945. */
  4946. public void removeFooterClickListener(FooterClickListener listener) {
  4947. removeListener(TableConstants.FOOTER_CLICK_EVENT_ID,
  4948. FooterClickEvent.class, listener);
  4949. }
  4950. /**
  4951. * @deprecated As of 7.0, replaced by
  4952. * {@link #removeFooterClickListener(FooterClickListener)}
  4953. **/
  4954. @Deprecated
  4955. public void removeListener(FooterClickListener listener) {
  4956. removeFooterClickListener(listener);
  4957. }
  4958. /**
  4959. * Gets the footer caption beneath the rows
  4960. *
  4961. * @param propertyId
  4962. * The propertyId of the column *
  4963. * @return The caption of the footer or NULL if not set
  4964. */
  4965. public String getColumnFooter(Object propertyId) {
  4966. return columnFooters.get(propertyId);
  4967. }
  4968. /**
  4969. * Sets the column footer caption. The column footer caption is the text
  4970. * displayed beneath the column if footers have been set visible.
  4971. *
  4972. * @param propertyId
  4973. * The properyId of the column
  4974. *
  4975. * @param footer
  4976. * The caption of the footer
  4977. */
  4978. public void setColumnFooter(Object propertyId, String footer) {
  4979. if (footer == null) {
  4980. columnFooters.remove(propertyId);
  4981. } else {
  4982. columnFooters.put(propertyId, footer);
  4983. }
  4984. markAsDirty();
  4985. }
  4986. /**
  4987. * Sets the footer visible in the bottom of the table.
  4988. * <p>
  4989. * The footer can be used to add column related data like sums to the bottom
  4990. * of the Table using setColumnFooter(Object propertyId, String footer).
  4991. * </p>
  4992. *
  4993. * @param visible
  4994. * Should the footer be visible
  4995. */
  4996. public void setFooterVisible(boolean visible) {
  4997. if (visible != columnFootersVisible) {
  4998. columnFootersVisible = visible;
  4999. markAsDirty();
  5000. }
  5001. }
  5002. /**
  5003. * Is the footer currently visible?
  5004. *
  5005. * @return Returns true if visible else false
  5006. */
  5007. public boolean isFooterVisible() {
  5008. return columnFootersVisible;
  5009. }
  5010. /**
  5011. * This event is fired when a column is resized. The event contains the
  5012. * columns property id which was fired, the previous width of the column and
  5013. * the width of the column after the resize.
  5014. */
  5015. @Deprecated
  5016. public static class ColumnResizeEvent extends Component.Event {
  5017. public static final Method COLUMN_RESIZE_METHOD;
  5018. static {
  5019. try {
  5020. COLUMN_RESIZE_METHOD = ColumnResizeListener.class
  5021. .getDeclaredMethod("columnResize",
  5022. new Class[] { ColumnResizeEvent.class });
  5023. } catch (final java.lang.NoSuchMethodException e) {
  5024. // This should never happen
  5025. throw new java.lang.RuntimeException(e);
  5026. }
  5027. }
  5028. private final int previousWidth;
  5029. private final int currentWidth;
  5030. private final Object columnPropertyId;
  5031. /**
  5032. * Constructor
  5033. *
  5034. * @param source
  5035. * The source of the event
  5036. * @param propertyId
  5037. * The columns property id
  5038. * @param previous
  5039. * The width in pixels of the column before the resize event
  5040. * @param current
  5041. * The width in pixels of the column after the resize event
  5042. */
  5043. public ColumnResizeEvent(Component source, Object propertyId,
  5044. int previous, int current) {
  5045. super(source);
  5046. previousWidth = previous;
  5047. currentWidth = current;
  5048. columnPropertyId = propertyId;
  5049. }
  5050. /**
  5051. * Get the column property id of the column that was resized.
  5052. *
  5053. * @return The column property id
  5054. */
  5055. public Object getPropertyId() {
  5056. return columnPropertyId;
  5057. }
  5058. /**
  5059. * Get the width in pixels of the column before the resize event
  5060. *
  5061. * @return Width in pixels
  5062. */
  5063. public int getPreviousWidth() {
  5064. return previousWidth;
  5065. }
  5066. /**
  5067. * Get the width in pixels of the column after the resize event
  5068. *
  5069. * @return Width in pixels
  5070. */
  5071. public int getCurrentWidth() {
  5072. return currentWidth;
  5073. }
  5074. }
  5075. /**
  5076. * Interface for listening to column resize events.
  5077. */
  5078. @Deprecated
  5079. public interface ColumnResizeListener extends Serializable {
  5080. /**
  5081. * This method is triggered when the column has been resized
  5082. *
  5083. * @param event
  5084. * The event which contains the column property id, the
  5085. * previous width of the column and the current width of the
  5086. * column
  5087. */
  5088. public void columnResize(ColumnResizeEvent event);
  5089. }
  5090. /**
  5091. * Adds a column resize listener to the Table. A column resize listener is
  5092. * called when a user resizes a columns width.
  5093. *
  5094. * @param listener
  5095. * The listener to attach to the Table
  5096. */
  5097. public void addColumnResizeListener(ColumnResizeListener listener) {
  5098. addListener(TableConstants.COLUMN_RESIZE_EVENT_ID,
  5099. ColumnResizeEvent.class, listener,
  5100. ColumnResizeEvent.COLUMN_RESIZE_METHOD);
  5101. }
  5102. /**
  5103. * @deprecated As of 7.0, replaced by
  5104. * {@link #addColumnResizeListener(ColumnResizeListener)}
  5105. **/
  5106. @Deprecated
  5107. public void addListener(ColumnResizeListener listener) {
  5108. addColumnResizeListener(listener);
  5109. }
  5110. /**
  5111. * Removes a column resize listener from the Table.
  5112. *
  5113. * @param listener
  5114. * The listener to remove
  5115. */
  5116. public void removeColumnResizeListener(ColumnResizeListener listener) {
  5117. removeListener(TableConstants.COLUMN_RESIZE_EVENT_ID,
  5118. ColumnResizeEvent.class, listener);
  5119. }
  5120. /**
  5121. * @deprecated As of 7.0, replaced by
  5122. * {@link #removeColumnResizeListener(ColumnResizeListener)}
  5123. **/
  5124. @Deprecated
  5125. public void removeListener(ColumnResizeListener listener) {
  5126. removeColumnResizeListener(listener);
  5127. }
  5128. /**
  5129. * This event is fired when a columns are reordered by the end user user.
  5130. */
  5131. @Deprecated
  5132. public static class ColumnReorderEvent extends Component.Event {
  5133. public static final Method METHOD;
  5134. static {
  5135. try {
  5136. METHOD = ColumnReorderListener.class.getDeclaredMethod(
  5137. "columnReorder",
  5138. new Class[] { ColumnReorderEvent.class });
  5139. } catch (final java.lang.NoSuchMethodException e) {
  5140. // This should never happen
  5141. throw new java.lang.RuntimeException(e);
  5142. }
  5143. }
  5144. /**
  5145. * Constructor
  5146. *
  5147. * @param source
  5148. * The source of the event
  5149. */
  5150. public ColumnReorderEvent(Component source) {
  5151. super(source);
  5152. }
  5153. }
  5154. /**
  5155. * Interface for listening to column reorder events.
  5156. */
  5157. @Deprecated
  5158. public interface ColumnReorderListener extends Serializable {
  5159. /**
  5160. * This method is triggered when the column has been reordered
  5161. *
  5162. * @param event
  5163. */
  5164. public void columnReorder(ColumnReorderEvent event);
  5165. }
  5166. /**
  5167. * This event is fired when the collapse state of a column changes.
  5168. *
  5169. * @since 7.6
  5170. */
  5171. @Deprecated
  5172. public static class ColumnCollapseEvent extends Component.Event {
  5173. public static final Method METHOD = ReflectTools.findMethod(
  5174. ColumnCollapseListener.class, "columnCollapseStateChange",
  5175. ColumnCollapseEvent.class);
  5176. private Object propertyId;
  5177. /**
  5178. * Constructor
  5179. *
  5180. * @param source
  5181. * The source of the event
  5182. * @param propertyId
  5183. * The id of the column
  5184. */
  5185. public ColumnCollapseEvent(Component source, Object propertyId) {
  5186. super(source);
  5187. this.propertyId = propertyId;
  5188. }
  5189. /**
  5190. * Gets the id of the column whose collapse state changed
  5191. *
  5192. * @return the property id of the column
  5193. */
  5194. public Object getPropertyId() {
  5195. return propertyId;
  5196. }
  5197. }
  5198. /**
  5199. * Interface for listening to column collapse events.
  5200. *
  5201. * @since 7.6
  5202. */
  5203. @Deprecated
  5204. public interface ColumnCollapseListener extends Serializable {
  5205. /**
  5206. * This method is triggered when the collapse state for a column has
  5207. * changed
  5208. *
  5209. * @param event
  5210. */
  5211. public void columnCollapseStateChange(ColumnCollapseEvent event);
  5212. }
  5213. /**
  5214. * Adds a column reorder listener to the Table. A column reorder listener is
  5215. * called when a user reorders columns.
  5216. *
  5217. * @param listener
  5218. * The listener to attach to the Table
  5219. */
  5220. public void addColumnReorderListener(ColumnReorderListener listener) {
  5221. addListener(TableConstants.COLUMN_REORDER_EVENT_ID,
  5222. ColumnReorderEvent.class, listener, ColumnReorderEvent.METHOD);
  5223. }
  5224. /**
  5225. * @deprecated As of 7.0, replaced by
  5226. * {@link #addColumnReorderListener(ColumnReorderListener)}
  5227. **/
  5228. @Deprecated
  5229. public void addListener(ColumnReorderListener listener) {
  5230. addColumnReorderListener(listener);
  5231. }
  5232. /**
  5233. * Removes a column reorder listener from the Table.
  5234. *
  5235. * @param listener
  5236. * The listener to remove
  5237. */
  5238. public void removeColumnReorderListener(ColumnReorderListener listener) {
  5239. removeListener(TableConstants.COLUMN_REORDER_EVENT_ID,
  5240. ColumnReorderEvent.class, listener);
  5241. }
  5242. /**
  5243. * @deprecated As of 7.0, replaced by
  5244. * {@link #removeColumnReorderListener(ColumnReorderListener)}
  5245. **/
  5246. @Deprecated
  5247. public void removeListener(ColumnReorderListener listener) {
  5248. removeColumnReorderListener(listener);
  5249. }
  5250. /**
  5251. * Adds a column collapse listener to the Table. A column collapse listener
  5252. * is called when the collapsed state of a column changes.
  5253. *
  5254. * @since 7.6
  5255. *
  5256. * @param listener
  5257. * The listener to attach
  5258. */
  5259. public void addColumnCollapseListener(ColumnCollapseListener listener) {
  5260. addListener(TableConstants.COLUMN_COLLAPSE_EVENT_ID,
  5261. ColumnCollapseEvent.class, listener,
  5262. ColumnCollapseEvent.METHOD);
  5263. }
  5264. /**
  5265. * Removes a column reorder listener from the Table.
  5266. *
  5267. * @since 7.6
  5268. * @param listener
  5269. * The listener to remove
  5270. */
  5271. public void removeColumnCollapseListener(ColumnCollapseListener listener) {
  5272. removeListener(TableConstants.COLUMN_COLLAPSE_EVENT_ID,
  5273. ColumnCollapseEvent.class, listener);
  5274. }
  5275. /**
  5276. * Set the item description generator which generates tooltips for cells and
  5277. * rows in the Table
  5278. *
  5279. * @param generator
  5280. * The generator to use or null to disable
  5281. */
  5282. public void setItemDescriptionGenerator(
  5283. ItemDescriptionGenerator generator) {
  5284. if (generator != itemDescriptionGenerator) {
  5285. itemDescriptionGenerator = generator;
  5286. // Assures the visual refresh. No need to reset the page buffer
  5287. // before as the content has not changed, only the descriptions
  5288. refreshRenderedCells();
  5289. }
  5290. }
  5291. /**
  5292. * Get the item description generator which generates tooltips for cells and
  5293. * rows in the Table.
  5294. */
  5295. public ItemDescriptionGenerator getItemDescriptionGenerator() {
  5296. return itemDescriptionGenerator;
  5297. }
  5298. /**
  5299. * Row generators can be used to replace certain items in a table with a
  5300. * generated string. The generator is called each time the table is
  5301. * rendered, which means that new strings can be generated each time.
  5302. *
  5303. * Row generators can be used for e.g. summary rows or grouping of items.
  5304. */
  5305. @Deprecated
  5306. public interface RowGenerator extends Serializable {
  5307. /**
  5308. * Called for every row that is painted in the Table. Returning a
  5309. * GeneratedRow object will cause the row to be painted based on the
  5310. * contents of the GeneratedRow. A generated row is by default styled
  5311. * similarly to a header or footer row.
  5312. * <p>
  5313. * The GeneratedRow data object contains the text that should be
  5314. * rendered in the row. The itemId in the container thus works only as a
  5315. * placeholder.
  5316. * <p>
  5317. * If GeneratedRow.setSpanColumns(true) is used, there will be one
  5318. * String spanning all columns (use setText("Spanning text")). Otherwise
  5319. * you can define one String per visible column.
  5320. * <p>
  5321. * If GeneratedRow.setRenderAsHtml(true) is used, the strings can
  5322. * contain HTML markup, otherwise all strings will be rendered as text
  5323. * (the default).
  5324. * <p>
  5325. * A "v-table-generated-row" CSS class is added to all generated rows.
  5326. * For custom styling of a generated row you can combine a RowGenerator
  5327. * with a CellStyleGenerator.
  5328. * <p>
  5329. *
  5330. * @param table
  5331. * The Table that is being painted
  5332. * @param itemId
  5333. * The itemId for the row
  5334. * @return A GeneratedRow describing how the row should be painted or
  5335. * null to paint the row with the contents from the container
  5336. */
  5337. public GeneratedRow generateRow(Table table, Object itemId);
  5338. }
  5339. @Deprecated
  5340. public static class GeneratedRow implements Serializable {
  5341. private boolean htmlContentAllowed = false;
  5342. private boolean spanColumns = false;
  5343. private String[] text = null;
  5344. /**
  5345. * Creates a new generated row. If only one string is passed in, columns
  5346. * are automatically spanned.
  5347. *
  5348. * @param text
  5349. */
  5350. public GeneratedRow(String... text) {
  5351. setHtmlContentAllowed(false);
  5352. setSpanColumns(text == null || text.length == 1);
  5353. setText(text);
  5354. }
  5355. /**
  5356. * Pass one String if spanColumns is used, one String for each visible
  5357. * column otherwise
  5358. */
  5359. public void setText(String... text) {
  5360. if (text == null || (text.length == 1 && text[0] == null)) {
  5361. text = new String[] { "" };
  5362. }
  5363. this.text = text;
  5364. }
  5365. protected String[] getText() {
  5366. return text;
  5367. }
  5368. protected Object getValue() {
  5369. return getText();
  5370. }
  5371. protected boolean isHtmlContentAllowed() {
  5372. return htmlContentAllowed;
  5373. }
  5374. /**
  5375. * If set to true, all strings passed to {@link #setText(String...)}
  5376. * will be rendered as HTML.
  5377. *
  5378. * @param htmlContentAllowed
  5379. */
  5380. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  5381. this.htmlContentAllowed = htmlContentAllowed;
  5382. }
  5383. protected boolean isSpanColumns() {
  5384. return spanColumns;
  5385. }
  5386. /**
  5387. * If set to true, only one string will be rendered, spanning the entire
  5388. * row.
  5389. *
  5390. * @param spanColumns
  5391. */
  5392. public void setSpanColumns(boolean spanColumns) {
  5393. this.spanColumns = spanColumns;
  5394. }
  5395. }
  5396. /**
  5397. * Assigns a row generator to the table. The row generator will be able to
  5398. * replace rows in the table when it is rendered.
  5399. *
  5400. * @param generator
  5401. * the new row generator
  5402. */
  5403. public void setRowGenerator(RowGenerator generator) {
  5404. rowGenerator = generator;
  5405. refreshRowCache();
  5406. }
  5407. /**
  5408. * @return the current row generator
  5409. */
  5410. public RowGenerator getRowGenerator() {
  5411. return rowGenerator;
  5412. }
  5413. /**
  5414. * Sets a converter for a property id.
  5415. * <p>
  5416. * The converter is used to format the the data for the given property id
  5417. * before displaying it in the table.
  5418. * </p>
  5419. *
  5420. * @param propertyId
  5421. * The propertyId to format using the converter
  5422. * @param converter
  5423. * The converter to use for the property id
  5424. */
  5425. public void setConverter(Object propertyId,
  5426. Converter<String, ?> converter) {
  5427. if (!getContainerPropertyIds().contains(propertyId)) {
  5428. throw new IllegalArgumentException(
  5429. "PropertyId " + propertyId + " must be in the container");
  5430. }
  5431. if (!typeIsCompatible(converter.getModelType(), getType(propertyId))) {
  5432. throw new IllegalArgumentException(
  5433. "Property type (" + getType(propertyId)
  5434. + ") must match converter source type ("
  5435. + converter.getModelType() + ")");
  5436. }
  5437. propertyValueConverters.put(propertyId,
  5438. (Converter<String, Object>) converter);
  5439. refreshRowCache();
  5440. }
  5441. /**
  5442. * Checks if there is a converter set explicitly for the given property id.
  5443. *
  5444. * @param propertyId
  5445. * The propertyId to check
  5446. * @return true if a converter has been set for the property id, false
  5447. * otherwise
  5448. */
  5449. protected boolean hasConverter(Object propertyId) {
  5450. return propertyValueConverters.containsKey(propertyId);
  5451. }
  5452. /**
  5453. * Returns the converter used to format the given propertyId.
  5454. *
  5455. * @param propertyId
  5456. * The propertyId to check
  5457. * @return The converter used to format the propertyId or null if no
  5458. * converter has been set
  5459. */
  5460. public Converter<String, Object> getConverter(Object propertyId) {
  5461. return propertyValueConverters.get(propertyId);
  5462. }
  5463. @Override
  5464. public void setVisible(boolean visible) {
  5465. if (visible) {
  5466. // We need to ensure that the rows are sent to the client when the
  5467. // Table is made visible if it has been rendered as invisible.
  5468. setRowCacheInvalidated(true);
  5469. }
  5470. super.setVisible(visible);
  5471. }
  5472. @Override
  5473. public Iterator<Component> iterator() {
  5474. if (visibleComponents == null) {
  5475. Collection<Component> empty = Collections.emptyList();
  5476. return empty.iterator();
  5477. }
  5478. return visibleComponents.iterator();
  5479. }
  5480. /**
  5481. * @deprecated As of 7.0, use {@link #iterator()} instead.
  5482. */
  5483. @Deprecated
  5484. public Iterator<Component> getComponentIterator() {
  5485. return iterator();
  5486. }
  5487. @Override
  5488. public void readDesign(Element design, DesignContext context) {
  5489. super.readDesign(design, context);
  5490. if (design.hasAttr("sortable")) {
  5491. setSortEnabled(DesignAttributeHandler.readAttribute("sortable",
  5492. design.attributes(), boolean.class));
  5493. }
  5494. readColumns(design);
  5495. readHeader(design);
  5496. readBody(design, context);
  5497. readFooter(design);
  5498. }
  5499. private void readColumns(Element design) {
  5500. Element colgroup = design.select("> table > colgroup").first();
  5501. if (colgroup != null) {
  5502. int i = 0;
  5503. List<Object> pIds = new ArrayList<>();
  5504. for (Element col : colgroup.children()) {
  5505. if (!col.tagName().equals("col")) {
  5506. throw new DesignException("invalid column");
  5507. }
  5508. String id = DesignAttributeHandler.readAttribute("property-id",
  5509. col.attributes(), "property-" + i++, String.class);
  5510. pIds.add(id);
  5511. addContainerProperty(id, String.class, null);
  5512. if (col.hasAttr("width")) {
  5513. setColumnWidth(id, DesignAttributeHandler.readAttribute(
  5514. "width", col.attributes(), Integer.class));
  5515. }
  5516. if (col.hasAttr("center")) {
  5517. setColumnAlignment(id, Align.CENTER);
  5518. } else if (col.hasAttr("right")) {
  5519. setColumnAlignment(id, Align.RIGHT);
  5520. }
  5521. if (col.hasAttr("expand")) {
  5522. if (col.attr("expand").isEmpty()) {
  5523. setColumnExpandRatio(id, 1);
  5524. } else {
  5525. setColumnExpandRatio(id,
  5526. DesignAttributeHandler.readAttribute("expand",
  5527. col.attributes(), float.class));
  5528. }
  5529. }
  5530. if (col.hasAttr("collapsible")) {
  5531. setColumnCollapsible(id,
  5532. DesignAttributeHandler.readAttribute("collapsible",
  5533. col.attributes(), boolean.class));
  5534. }
  5535. if (col.hasAttr("collapsed")) {
  5536. setColumnCollapsed(id, DesignAttributeHandler.readAttribute(
  5537. "collapsed", col.attributes(), boolean.class));
  5538. }
  5539. }
  5540. setVisibleColumns(pIds.toArray());
  5541. }
  5542. }
  5543. private void readFooter(Element design) {
  5544. readHeaderOrFooter(design, false);
  5545. }
  5546. private void readHeader(Element design) {
  5547. readHeaderOrFooter(design, true);
  5548. }
  5549. @Override
  5550. protected void readItems(Element design, DesignContext context) {
  5551. // Do nothing - header/footer and inline data must be handled after
  5552. // colgroup.
  5553. }
  5554. private void readHeaderOrFooter(Element design, boolean header) {
  5555. String selector = header ? "> table > thead" : "> table > tfoot";
  5556. Element elem = design.select(selector).first();
  5557. if (elem != null) {
  5558. if (!header) {
  5559. setFooterVisible(true);
  5560. }
  5561. if (elem.children().size() != 1) {
  5562. throw new DesignException(
  5563. "Table header and footer should contain exactly one <tr> element");
  5564. }
  5565. Element tr = elem.child(0);
  5566. Elements elems = tr.children();
  5567. Collection<?> propertyIds = visibleColumns;
  5568. if (elems.size() != propertyIds.size()) {
  5569. throw new DesignException(
  5570. "Table header and footer should contain as many items as there"
  5571. + " are columns in the Table.");
  5572. }
  5573. Iterator<?> propertyIt = propertyIds.iterator();
  5574. for (Element e : elems) {
  5575. String columnValue = DesignFormatter
  5576. .decodeFromTextNode(e.html());
  5577. Object propertyId = propertyIt.next();
  5578. if (header) {
  5579. setColumnHeader(propertyId, columnValue);
  5580. if (e.hasAttr("icon")) {
  5581. setColumnIcon(propertyId,
  5582. DesignAttributeHandler.readAttribute("icon",
  5583. e.attributes(), Resource.class));
  5584. }
  5585. } else {
  5586. setColumnFooter(propertyId, columnValue);
  5587. }
  5588. }
  5589. }
  5590. }
  5591. protected void readBody(Element design, DesignContext context) {
  5592. Element tbody = design.select("> table > tbody").first();
  5593. if (tbody == null) {
  5594. return;
  5595. }
  5596. Set<String> selected = new HashSet<>();
  5597. for (Element tr : tbody.children()) {
  5598. readItem(tr, selected, context);
  5599. }
  5600. }
  5601. @Override
  5602. protected Object readItem(Element tr, Set<String> selected,
  5603. DesignContext context) {
  5604. Elements cells = tr.children();
  5605. if (visibleColumns.size() != cells.size()) {
  5606. throw new DesignException(
  5607. "Wrong number of columns in a Table row. Expected "
  5608. + visibleColumns.size() + ", was " + cells.size()
  5609. + ".");
  5610. }
  5611. Object[] data = new String[cells.size()];
  5612. for (int c = 0; c < cells.size(); ++c) {
  5613. data[c] = DesignFormatter.decodeFromTextNode(cells.get(c).html());
  5614. }
  5615. Object itemId = addItem(data,
  5616. tr.hasAttr("item-id") ? tr.attr("item-id") : null);
  5617. if (itemId == null) {
  5618. throw new DesignException("Failed to add a Table row: " + data);
  5619. }
  5620. return itemId;
  5621. }
  5622. @Override
  5623. public void writeDesign(Element design, DesignContext context) {
  5624. Table def = context.getDefaultInstance(this);
  5625. DesignAttributeHandler.writeAttribute("sortable", design.attributes(),
  5626. isSortEnabled(), def.isSortEnabled(), boolean.class, context);
  5627. Element table = null;
  5628. boolean hasColumns = getVisibleColumns().length != 0;
  5629. if (hasColumns) {
  5630. table = design.appendElement("table");
  5631. writeColumns(table, def, context);
  5632. writeHeader(table, def, context);
  5633. }
  5634. super.writeDesign(design, context);
  5635. if (hasColumns) {
  5636. writeFooter(table);
  5637. }
  5638. }
  5639. private void writeColumns(Element table, Table def, DesignContext context) {
  5640. Object[] columns = getVisibleColumns();
  5641. if (columns.length == 0) {
  5642. return;
  5643. }
  5644. Element colgroup = table.appendElement("colgroup");
  5645. for (Object id : columns) {
  5646. Element col = colgroup.appendElement("col");
  5647. col.attr("property-id", id.toString());
  5648. if (getColumnAlignment(id) == Align.CENTER) {
  5649. col.attr("center", true);
  5650. } else if (getColumnAlignment(id) == Align.RIGHT) {
  5651. col.attr("right", true);
  5652. }
  5653. DesignAttributeHandler.writeAttribute("width", col.attributes(),
  5654. getColumnWidth(id), def.getColumnWidth(null), int.class,
  5655. context);
  5656. DesignAttributeHandler.writeAttribute("expand", col.attributes(),
  5657. getColumnExpandRatio(id), def.getColumnExpandRatio(null),
  5658. float.class, context);
  5659. DesignAttributeHandler.writeAttribute("collapsible",
  5660. col.attributes(), isColumnCollapsible(id),
  5661. def.isColumnCollapsible(null), boolean.class, context);
  5662. DesignAttributeHandler.writeAttribute("collapsed", col.attributes(),
  5663. isColumnCollapsed(id), def.isColumnCollapsed(null),
  5664. boolean.class, context);
  5665. }
  5666. }
  5667. private void writeHeader(Element table, Table def, DesignContext context) {
  5668. Object[] columns = getVisibleColumns();
  5669. if (columns.length == 0
  5670. || (columnIcons.isEmpty() && columnHeaders.isEmpty())) {
  5671. return;
  5672. }
  5673. Element header = table.appendElement("thead").appendElement("tr");
  5674. for (Object id : columns) {
  5675. Element th = header.appendElement("th");
  5676. th.html(getColumnHeader(id));
  5677. DesignAttributeHandler.writeAttribute("icon", th.attributes(),
  5678. getColumnIcon(id), def.getColumnIcon(null), Resource.class,
  5679. context);
  5680. }
  5681. }
  5682. private void writeFooter(Element table) {
  5683. Object[] columns = getVisibleColumns();
  5684. if (columns.length == 0 || columnFooters.isEmpty()) {
  5685. return;
  5686. }
  5687. Element footer = table.appendElement("tfoot").appendElement("tr");
  5688. for (Object id : columns) {
  5689. footer.appendElement("td").text(getColumnFooter(id));
  5690. }
  5691. }
  5692. @Override
  5693. protected void writeItems(Element design, DesignContext context) {
  5694. if (getVisibleColumns().length == 0) {
  5695. return;
  5696. }
  5697. Element tbody = design.child(0).appendElement("tbody");
  5698. super.writeItems(tbody, context);
  5699. }
  5700. @Override
  5701. protected Element writeItem(Element tbody, Object itemId,
  5702. DesignContext context) {
  5703. Element tr = tbody.appendElement("tr");
  5704. tr.attr("item-id", String.valueOf(itemId));
  5705. Item item = getItem(itemId);
  5706. for (Object id : getVisibleColumns()) {
  5707. Element td = tr.appendElement("td");
  5708. Object value = item.getItemProperty(id).getValue();
  5709. td.html(value != null ? value.toString() : "");
  5710. }
  5711. return tr;
  5712. }
  5713. @Override
  5714. protected Collection<String> getCustomAttributes() {
  5715. Collection<String> result = super.getCustomAttributes();
  5716. result.add("sortable");
  5717. result.add("sort-enabled");
  5718. result.add("sort-disabled");
  5719. result.add("footer-visible");
  5720. result.add("item-caption-mode");
  5721. result.add("current-page-first-item-id");
  5722. result.add("current-page-first-item-index");
  5723. return result;
  5724. }
  5725. /**
  5726. * ContextClickEvent for the Table Component.
  5727. *
  5728. * @since 7.6
  5729. */
  5730. @Deprecated
  5731. public static class TableContextClickEvent extends ContextClickEvent {
  5732. private final Object itemId;
  5733. private final Object propertyId;
  5734. private final Section section;
  5735. public TableContextClickEvent(Table source,
  5736. MouseEventDetails mouseEventDetails, Object itemId,
  5737. Object propertyId, Section section) {
  5738. super(source, mouseEventDetails);
  5739. this.itemId = itemId;
  5740. this.propertyId = propertyId;
  5741. this.section = section;
  5742. }
  5743. /**
  5744. * Returns the item id of context clicked row.
  5745. *
  5746. * @return item id of clicked row; <code>null</code> if header, footer
  5747. * or empty area of Table
  5748. */
  5749. public Object getItemId() {
  5750. return itemId;
  5751. }
  5752. /**
  5753. * Returns the property id of context clicked column.
  5754. *
  5755. * @return property id; or <code>null</code> if we've clicked on the
  5756. * empty area of the Table
  5757. */
  5758. public Object getPropertyId() {
  5759. return propertyId;
  5760. }
  5761. /**
  5762. * Returns the clicked section of Table.
  5763. *
  5764. * @return section of Table
  5765. */
  5766. public Section getSection() {
  5767. return section;
  5768. }
  5769. @Override
  5770. public Table getComponent() {
  5771. return (Table) super.getComponent();
  5772. }
  5773. }
  5774. @Override
  5775. protected TableState getState() {
  5776. return getState(true);
  5777. }
  5778. @Override
  5779. protected TableState getState(boolean markAsDirty) {
  5780. return (TableState) super.getState(markAsDirty);
  5781. }
  5782. private final Logger getLogger() {
  5783. if (logger == null) {
  5784. logger = Logger.getLogger(Table.class.getName());
  5785. }
  5786. return logger;
  5787. }
  5788. @Override
  5789. public void setChildMeasurementHint(ChildMeasurementHint hint) {
  5790. if (hint == null) {
  5791. childMeasurementHint = ChildMeasurementHint.MEASURE_ALWAYS;
  5792. } else {
  5793. childMeasurementHint = hint;
  5794. }
  5795. }
  5796. @Override
  5797. public ChildMeasurementHint getChildMeasurementHint() {
  5798. return childMeasurementHint;
  5799. }
  5800. /**
  5801. * Sets whether only collapsible columns should be shown to the user in the
  5802. * column collapse menu. The default is
  5803. * {@link CollapseMenuContent#ALL_COLUMNS}.
  5804. *
  5805. *
  5806. * @since 7.6
  5807. * @param content
  5808. * the desired collapsible menu content setting
  5809. */
  5810. public void setCollapseMenuContent(CollapseMenuContent content) {
  5811. getState().collapseMenuContent = content;
  5812. }
  5813. /**
  5814. * Checks whether only collapsible columns are shown to the user in the
  5815. * column collapse menu. The default is
  5816. * {@link CollapseMenuContent#ALL_COLUMNS} .
  5817. *
  5818. * @since 7.6
  5819. * @return the current collapsible menu content setting
  5820. */
  5821. public CollapseMenuContent getCollapseMenuContent() {
  5822. return getState(false).collapseMenuContent;
  5823. }
  5824. }