You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Grid.java 170KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.lang.reflect.Type;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Comparator;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.Iterator;
  28. import java.util.LinkedHashSet;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Objects;
  32. import java.util.Optional;
  33. import java.util.Set;
  34. import java.util.UUID;
  35. import java.util.function.BinaryOperator;
  36. import java.util.function.Function;
  37. import java.util.stream.Collectors;
  38. import java.util.stream.Stream;
  39. import org.jsoup.Jsoup;
  40. import org.jsoup.nodes.Attributes;
  41. import org.jsoup.nodes.Element;
  42. import org.jsoup.select.Elements;
  43. import com.vaadin.data.BeanPropertySet;
  44. import com.vaadin.data.Binder;
  45. import com.vaadin.data.Binder.Binding;
  46. import com.vaadin.data.HasDataProvider;
  47. import com.vaadin.data.HasValue;
  48. import com.vaadin.data.PropertyDefinition;
  49. import com.vaadin.data.PropertySet;
  50. import com.vaadin.data.ValueProvider;
  51. import com.vaadin.data.provider.CallbackDataProvider;
  52. import com.vaadin.data.provider.DataCommunicator;
  53. import com.vaadin.data.provider.DataGenerator;
  54. import com.vaadin.data.provider.DataProvider;
  55. import com.vaadin.data.provider.GridSortOrder;
  56. import com.vaadin.data.provider.GridSortOrderBuilder;
  57. import com.vaadin.data.provider.InMemoryDataProvider;
  58. import com.vaadin.data.provider.Query;
  59. import com.vaadin.data.provider.QuerySortOrder;
  60. import com.vaadin.event.ConnectorEvent;
  61. import com.vaadin.event.ContextClickEvent;
  62. import com.vaadin.event.HasUserOriginated;
  63. import com.vaadin.event.SortEvent;
  64. import com.vaadin.event.SortEvent.SortListener;
  65. import com.vaadin.event.SortEvent.SortNotifier;
  66. import com.vaadin.event.selection.MultiSelectionListener;
  67. import com.vaadin.event.selection.SelectionListener;
  68. import com.vaadin.event.selection.SingleSelectionListener;
  69. import com.vaadin.server.AbstractExtension;
  70. import com.vaadin.server.EncodeResult;
  71. import com.vaadin.server.Extension;
  72. import com.vaadin.server.JsonCodec;
  73. import com.vaadin.server.SerializableComparator;
  74. import com.vaadin.server.SerializableSupplier;
  75. import com.vaadin.server.Setter;
  76. import com.vaadin.server.VaadinServiceClassLoaderUtil;
  77. import com.vaadin.shared.Connector;
  78. import com.vaadin.shared.MouseEventDetails;
  79. import com.vaadin.shared.Registration;
  80. import com.vaadin.shared.data.DataCommunicatorConstants;
  81. import com.vaadin.shared.data.sort.SortDirection;
  82. import com.vaadin.shared.ui.ContentMode;
  83. import com.vaadin.shared.ui.grid.AbstractGridExtensionState;
  84. import com.vaadin.shared.ui.grid.ColumnResizeMode;
  85. import com.vaadin.shared.ui.grid.ColumnState;
  86. import com.vaadin.shared.ui.grid.DetailsManagerState;
  87. import com.vaadin.shared.ui.grid.GridClientRpc;
  88. import com.vaadin.shared.ui.grid.GridConstants;
  89. import com.vaadin.shared.ui.grid.GridConstants.Section;
  90. import com.vaadin.shared.ui.grid.GridServerRpc;
  91. import com.vaadin.shared.ui.grid.GridState;
  92. import com.vaadin.shared.ui.grid.GridStaticCellType;
  93. import com.vaadin.shared.ui.grid.HeightMode;
  94. import com.vaadin.shared.ui.grid.ScrollDestination;
  95. import com.vaadin.shared.ui.grid.SectionState;
  96. import com.vaadin.ui.components.grid.ColumnReorderListener;
  97. import com.vaadin.ui.components.grid.ColumnResizeListener;
  98. import com.vaadin.ui.components.grid.ColumnVisibilityChangeListener;
  99. import com.vaadin.ui.components.grid.DetailsGenerator;
  100. import com.vaadin.ui.components.grid.Editor;
  101. import com.vaadin.ui.components.grid.EditorImpl;
  102. import com.vaadin.ui.components.grid.Footer;
  103. import com.vaadin.ui.components.grid.FooterRow;
  104. import com.vaadin.ui.components.grid.GridMultiSelect;
  105. import com.vaadin.ui.components.grid.GridSelectionModel;
  106. import com.vaadin.ui.components.grid.GridSingleSelect;
  107. import com.vaadin.ui.components.grid.Header;
  108. import com.vaadin.ui.components.grid.Header.Row;
  109. import com.vaadin.ui.components.grid.HeaderCell;
  110. import com.vaadin.ui.components.grid.HeaderRow;
  111. import com.vaadin.ui.components.grid.ItemClickListener;
  112. import com.vaadin.ui.components.grid.MultiSelectionModel;
  113. import com.vaadin.ui.components.grid.MultiSelectionModelImpl;
  114. import com.vaadin.ui.components.grid.NoSelectionModel;
  115. import com.vaadin.ui.components.grid.SingleSelectionModel;
  116. import com.vaadin.ui.components.grid.SingleSelectionModelImpl;
  117. import com.vaadin.ui.components.grid.SortOrderProvider;
  118. import com.vaadin.ui.declarative.DesignAttributeHandler;
  119. import com.vaadin.ui.declarative.DesignContext;
  120. import com.vaadin.ui.declarative.DesignException;
  121. import com.vaadin.ui.declarative.DesignFormatter;
  122. import com.vaadin.ui.renderers.AbstractRenderer;
  123. import com.vaadin.ui.renderers.ComponentRenderer;
  124. import com.vaadin.ui.renderers.HtmlRenderer;
  125. import com.vaadin.ui.renderers.Renderer;
  126. import com.vaadin.ui.renderers.TextRenderer;
  127. import com.vaadin.util.ReflectTools;
  128. import elemental.json.Json;
  129. import elemental.json.JsonObject;
  130. import elemental.json.JsonValue;
  131. /**
  132. * A grid component for displaying tabular data.
  133. *
  134. * @author Vaadin Ltd
  135. * @since 8.0
  136. *
  137. * @param <T>
  138. * the grid bean type
  139. */
  140. public class Grid<T> extends AbstractListing<T> implements HasComponents,
  141. HasDataProvider<T>, SortNotifier<GridSortOrder<T>> {
  142. private static final String DECLARATIVE_DATA_ITEM_TYPE = "data-item-type";
  143. /**
  144. * A callback method for fetching items. The callback is provided with a
  145. * list of sort orders, offset index and limit.
  146. *
  147. * @param <T>
  148. * the grid bean type
  149. */
  150. @FunctionalInterface
  151. public interface FetchItemsCallback<T> extends Serializable {
  152. /**
  153. * Returns a stream of items ordered by given sort orders, limiting the
  154. * results with given offset and limit.
  155. * <p>
  156. * This method is called after the size of the data set is asked from a
  157. * related size callback. The offset and limit are promised to be within
  158. * the size of the data set.
  159. *
  160. * @param sortOrder
  161. * a list of sort orders
  162. * @param offset
  163. * the first index to fetch
  164. * @param limit
  165. * the fetched item count
  166. * @return stream of items
  167. */
  168. public Stream<T> fetchItems(List<QuerySortOrder> sortOrder, int offset,
  169. int limit);
  170. }
  171. @Deprecated
  172. private static final Method COLUMN_REORDER_METHOD = ReflectTools.findMethod(
  173. ColumnReorderListener.class, "columnReorder",
  174. ColumnReorderEvent.class);
  175. private static final Method SORT_ORDER_CHANGE_METHOD = ReflectTools
  176. .findMethod(SortListener.class, "sort", SortEvent.class);
  177. @Deprecated
  178. private static final Method COLUMN_RESIZE_METHOD = ReflectTools.findMethod(
  179. ColumnResizeListener.class, "columnResize",
  180. ColumnResizeEvent.class);
  181. @Deprecated
  182. private static final Method ITEM_CLICK_METHOD = ReflectTools
  183. .findMethod(ItemClickListener.class, "itemClick", ItemClick.class);
  184. @Deprecated
  185. private static final Method COLUMN_VISIBILITY_METHOD = ReflectTools
  186. .findMethod(ColumnVisibilityChangeListener.class,
  187. "columnVisibilityChanged",
  188. ColumnVisibilityChangeEvent.class);
  189. /**
  190. * Selection mode representing the built-in selection models in grid.
  191. * <p>
  192. * These enums can be used in {@link Grid#setSelectionMode(SelectionMode)}
  193. * to easily switch between the build-in selection models.
  194. *
  195. * @see Grid#setSelectionMode(SelectionMode)
  196. * @see Grid#setSelectionModel(GridSelectionModel)
  197. */
  198. public enum SelectionMode {
  199. /**
  200. * Single selection mode that maps to build-in
  201. * {@link SingleSelectionModel}.
  202. *
  203. * @see SingleSelectionModelImpl
  204. */
  205. SINGLE {
  206. @Override
  207. protected <T> GridSelectionModel<T> createModel() {
  208. return new SingleSelectionModelImpl<>();
  209. }
  210. },
  211. /**
  212. * Multiselection mode that maps to build-in {@link MultiSelectionModel}
  213. * .
  214. *
  215. * @see MultiSelectionModelImpl
  216. */
  217. MULTI {
  218. @Override
  219. protected <T> GridSelectionModel<T> createModel() {
  220. return new MultiSelectionModelImpl<>();
  221. }
  222. },
  223. /**
  224. * Selection model that doesn't allow selection.
  225. *
  226. * @see NoSelectionModel
  227. */
  228. NONE {
  229. @Override
  230. protected <T> GridSelectionModel<T> createModel() {
  231. return new NoSelectionModel<>();
  232. }
  233. };
  234. /**
  235. * Creates the selection model to use with this enum.
  236. *
  237. * @param <T>
  238. * the type of items in the grid
  239. * @return the selection model
  240. */
  241. protected abstract <T> GridSelectionModel<T> createModel();
  242. }
  243. /**
  244. * An event that is fired when the columns are reordered.
  245. */
  246. public static class ColumnReorderEvent extends Component.Event
  247. implements HasUserOriginated {
  248. private final boolean userOriginated;
  249. /**
  250. *
  251. * @param source
  252. * the grid where the event originated from
  253. * @param userOriginated
  254. * <code>true</code> if event is a result of user
  255. * interaction, <code>false</code> if from API call
  256. */
  257. public ColumnReorderEvent(Grid source, boolean userOriginated) {
  258. super(source);
  259. this.userOriginated = userOriginated;
  260. }
  261. /**
  262. * Returns <code>true</code> if the column reorder was done by the user,
  263. * <code>false</code> if not and it was triggered by server side code.
  264. *
  265. * @return <code>true</code> if event is a result of user interaction
  266. */
  267. @Override
  268. public boolean isUserOriginated() {
  269. return userOriginated;
  270. }
  271. }
  272. /**
  273. * An event that is fired when a column is resized, either programmatically
  274. * or by the user.
  275. */
  276. public static class ColumnResizeEvent extends Component.Event
  277. implements HasUserOriginated {
  278. private final Column<?, ?> column;
  279. private final boolean userOriginated;
  280. /**
  281. *
  282. * @param source
  283. * the grid where the event originated from
  284. * @param userOriginated
  285. * <code>true</code> if event is a result of user
  286. * interaction, <code>false</code> if from API call
  287. */
  288. public ColumnResizeEvent(Grid<?> source, Column<?, ?> column,
  289. boolean userOriginated) {
  290. super(source);
  291. this.column = column;
  292. this.userOriginated = userOriginated;
  293. }
  294. /**
  295. * Returns the column that was resized.
  296. *
  297. * @return the resized column.
  298. */
  299. public Column<?, ?> getColumn() {
  300. return column;
  301. }
  302. /**
  303. * Returns <code>true</code> if the column resize was done by the user,
  304. * <code>false</code> if not and it was triggered by server side code.
  305. *
  306. * @return <code>true</code> if event is a result of user interaction
  307. */
  308. @Override
  309. public boolean isUserOriginated() {
  310. return userOriginated;
  311. }
  312. }
  313. /**
  314. * An event fired when an item in the Grid has been clicked.
  315. *
  316. * @param <T>
  317. * the grid bean type
  318. */
  319. public static class ItemClick<T> extends ConnectorEvent {
  320. private final T item;
  321. private final Column<T, ?> column;
  322. private final MouseEventDetails mouseEventDetails;
  323. private final int rowIndex;
  324. /**
  325. * Creates a new {@code ItemClick} event containing the given item and
  326. * Column originating from the given Grid.
  327. *
  328. */
  329. public ItemClick(Grid<T> source, Column<T, ?> column, T item,
  330. MouseEventDetails mouseEventDetails, int rowIndex) {
  331. super(source);
  332. this.column = column;
  333. this.item = item;
  334. this.mouseEventDetails = mouseEventDetails;
  335. this.rowIndex = rowIndex;
  336. }
  337. /**
  338. * Returns the clicked item.
  339. *
  340. * @return the clicked item
  341. */
  342. public T getItem() {
  343. return item;
  344. }
  345. /**
  346. * Returns the clicked column.
  347. *
  348. * @return the clicked column
  349. */
  350. public Column<T, ?> getColumn() {
  351. return column;
  352. }
  353. /**
  354. * Returns the source Grid.
  355. *
  356. * @return the grid
  357. */
  358. @Override
  359. public Grid<T> getSource() {
  360. return (Grid<T>) super.getSource();
  361. }
  362. /**
  363. * Returns the mouse event details.
  364. *
  365. * @return the mouse event details
  366. */
  367. public MouseEventDetails getMouseEventDetails() {
  368. return mouseEventDetails;
  369. }
  370. /**
  371. * Returns the clicked rowIndex.
  372. *
  373. * @return the clicked rowIndex
  374. * @since 8.4
  375. */
  376. public int getRowIndex() {
  377. return rowIndex;
  378. }
  379. }
  380. /**
  381. * ContextClickEvent for the Grid Component.
  382. *
  383. * <p>
  384. * Usage:
  385. *
  386. * <pre>
  387. * grid.addContextClickListener(event -&gt; Notification.show(
  388. * ((GridContextClickEvent&lt;Person&gt;) event).getItem() + " Clicked"));
  389. * </pre>
  390. *
  391. * @param <T>
  392. * the grid bean type
  393. */
  394. public static class GridContextClickEvent<T> extends ContextClickEvent {
  395. private final T item;
  396. private final int rowIndex;
  397. private final Column<T, ?> column;
  398. private final Section section;
  399. /**
  400. * Creates a new context click event.
  401. *
  402. * @param source
  403. * the grid where the context click occurred
  404. * @param mouseEventDetails
  405. * details about mouse position
  406. * @param section
  407. * the section of the grid which was clicked
  408. * @param rowIndex
  409. * the index of the row which was clicked
  410. * @param item
  411. * the item which was clicked
  412. * @param column
  413. * the column which was clicked
  414. */
  415. public GridContextClickEvent(Grid<T> source,
  416. MouseEventDetails mouseEventDetails, Section section,
  417. int rowIndex, T item, Column<T, ?> column) {
  418. super(source, mouseEventDetails);
  419. this.item = item;
  420. this.section = section;
  421. this.column = column;
  422. this.rowIndex = rowIndex;
  423. }
  424. /**
  425. * Returns the item of context clicked row.
  426. *
  427. * @return item of clicked row; <code>null</code> if header or footer
  428. */
  429. public T getItem() {
  430. return item;
  431. }
  432. /**
  433. * Returns the clicked column.
  434. *
  435. * @return the clicked column
  436. */
  437. public Column<T, ?> getColumn() {
  438. return column;
  439. }
  440. /**
  441. * Return the clicked section of Grid.
  442. *
  443. * @return section of grid
  444. */
  445. public Section getSection() {
  446. return section;
  447. }
  448. /**
  449. * Returns the clicked row index.
  450. * <p>
  451. * Header and Footer rows for index can be fetched with
  452. * {@link Grid#getHeaderRow(int)} and {@link Grid#getFooterRow(int)}.
  453. *
  454. * @return row index in section
  455. */
  456. public int getRowIndex() {
  457. return rowIndex;
  458. }
  459. @Override
  460. public Grid<T> getComponent() {
  461. return (Grid<T>) super.getComponent();
  462. }
  463. }
  464. /**
  465. * An event that is fired when a column's visibility changes.
  466. *
  467. * @since 7.5.0
  468. */
  469. public static class ColumnVisibilityChangeEvent extends Component.Event
  470. implements HasUserOriginated {
  471. private final Column<?, ?> column;
  472. private final boolean userOriginated;
  473. private final boolean hidden;
  474. /**
  475. * Constructor for a column visibility change event.
  476. *
  477. * @param source
  478. * the grid from which this event originates
  479. * @param column
  480. * the column that changed its visibility
  481. * @param hidden
  482. * <code>true</code> if the column was hidden,
  483. * <code>false</code> if it became visible
  484. * @param isUserOriginated
  485. * <code>true</code> if the event was triggered by an UI
  486. * interaction
  487. */
  488. public ColumnVisibilityChangeEvent(Grid<?> source, Column<?, ?> column,
  489. boolean hidden, boolean isUserOriginated) {
  490. super(source);
  491. this.column = column;
  492. this.hidden = hidden;
  493. userOriginated = isUserOriginated;
  494. }
  495. /**
  496. * Gets the column that became hidden or visible.
  497. *
  498. * @return the column that became hidden or visible.
  499. * @see Column#isHidden()
  500. */
  501. public Column<?, ?> getColumn() {
  502. return column;
  503. }
  504. /**
  505. * Was the column set hidden or visible.
  506. *
  507. * @return <code>true</code> if the column was hidden <code>false</code>
  508. * if it was set visible
  509. */
  510. public boolean isHidden() {
  511. return hidden;
  512. }
  513. @Override
  514. public boolean isUserOriginated() {
  515. return userOriginated;
  516. }
  517. }
  518. /**
  519. * A helper base class for creating extensions for the Grid component.
  520. *
  521. * @param <T>
  522. */
  523. public abstract static class AbstractGridExtension<T>
  524. extends AbstractListingExtension<T> {
  525. @Override
  526. public void extend(AbstractListing<T> grid) {
  527. if (!(grid instanceof Grid)) {
  528. throw new IllegalArgumentException(
  529. getClass().getSimpleName() + " can only extend Grid");
  530. }
  531. super.extend(grid);
  532. }
  533. /**
  534. * Adds given component to the connector hierarchy of Grid.
  535. *
  536. * @param c
  537. * the component to add
  538. */
  539. protected void addComponentToGrid(Component c) {
  540. getParent().addExtensionComponent(c);
  541. }
  542. /**
  543. * Removes given component from the connector hierarchy of Grid.
  544. *
  545. * @param c
  546. * the component to remove
  547. */
  548. protected void removeComponentFromGrid(Component c) {
  549. getParent().removeExtensionComponent(c);
  550. }
  551. @Override
  552. public Grid<T> getParent() {
  553. return (Grid<T>) super.getParent();
  554. }
  555. @Override
  556. protected AbstractGridExtensionState getState() {
  557. return (AbstractGridExtensionState) super.getState();
  558. }
  559. @Override
  560. protected AbstractGridExtensionState getState(boolean markAsDirty) {
  561. return (AbstractGridExtensionState) super.getState(markAsDirty);
  562. }
  563. protected String getInternalIdForColumn(Column<T, ?> column) {
  564. return getParent().getInternalIdForColumn(column);
  565. }
  566. }
  567. private final class GridServerRpcImpl implements GridServerRpc {
  568. @Override
  569. public void sort(String[] columnInternalIds, SortDirection[] directions,
  570. boolean isUserOriginated) {
  571. assert columnInternalIds.length == directions.length : "Column and sort direction counts don't match.";
  572. List<GridSortOrder<T>> list = new ArrayList<>(directions.length);
  573. for (int i = 0; i < columnInternalIds.length; ++i) {
  574. Column<T, ?> column = columnKeys.get(columnInternalIds[i]);
  575. list.add(new GridSortOrder<>(column, directions[i]));
  576. }
  577. setSortOrder(list, isUserOriginated);
  578. }
  579. @Override
  580. public void itemClick(String rowKey, String columnInternalId,
  581. MouseEventDetails details, int rowIndex) {
  582. Column<T, ?> column = getColumnByInternalId(columnInternalId);
  583. T item = getDataCommunicator().getKeyMapper().get(rowKey);
  584. fireEvent(new ItemClick<>(Grid.this, column, item, details,
  585. rowIndex));
  586. }
  587. @Override
  588. public void contextClick(int rowIndex, String rowKey,
  589. String columnInternalId, Section section,
  590. MouseEventDetails details) {
  591. T item = null;
  592. if (rowKey != null) {
  593. item = getDataCommunicator().getKeyMapper().get(rowKey);
  594. }
  595. fireEvent(new GridContextClickEvent<>(Grid.this, details, section,
  596. rowIndex, item, getColumnByInternalId(columnInternalId)));
  597. }
  598. @Override
  599. public void columnsReordered(List<String> newColumnOrder,
  600. List<String> oldColumnOrder) {
  601. final String diffStateKey = "columnOrder";
  602. ConnectorTracker connectorTracker = getUI().getConnectorTracker();
  603. JsonObject diffState = connectorTracker.getDiffState(Grid.this);
  604. // discard the change if the columns have been reordered from
  605. // the server side, as the server side is always right
  606. if (getState(false).columnOrder.equals(oldColumnOrder)) {
  607. // Don't mark as dirty since client has the state already
  608. getState(false).columnOrder = newColumnOrder;
  609. // write changes to diffState so that possible reverting the
  610. // column order is sent to client
  611. assert diffState
  612. .hasKey(diffStateKey) : "Field name has changed";
  613. Type type = null;
  614. try {
  615. type = getState(false).getClass().getField(diffStateKey)
  616. .getGenericType();
  617. } catch (NoSuchFieldException | SecurityException e) {
  618. e.printStackTrace();
  619. }
  620. EncodeResult encodeResult = JsonCodec.encode(
  621. getState(false).columnOrder, diffState, type,
  622. connectorTracker);
  623. diffState.put(diffStateKey, encodeResult.getEncodedValue());
  624. fireColumnReorderEvent(true);
  625. } else {
  626. // make sure the client is reverted to the order that the
  627. // server thinks it is
  628. diffState.remove(diffStateKey);
  629. markAsDirty();
  630. }
  631. }
  632. @Override
  633. public void columnVisibilityChanged(String internalId, boolean hidden) {
  634. Column<T, ?> column = getColumnByInternalId(internalId);
  635. column.checkColumnIsAttached();
  636. if (column.isHidden() != hidden) {
  637. column.getState().hidden = hidden;
  638. fireColumnVisibilityChangeEvent(column, hidden, true);
  639. }
  640. }
  641. @Override
  642. public void columnResized(String internalId, double pixels) {
  643. final Column<T, ?> column = getColumnByInternalId(internalId);
  644. if (column != null && column.isResizable()) {
  645. column.getState().width = pixels;
  646. fireColumnResizeEvent(column, true);
  647. }
  648. }
  649. }
  650. /**
  651. * Class for managing visible details rows.
  652. *
  653. * @param <T>
  654. * the grid bean type
  655. */
  656. public static class DetailsManager<T> extends AbstractGridExtension<T> {
  657. private final Set<T> visibleDetails = new HashSet<>();
  658. private final Map<T, Component> components = new HashMap<>();
  659. private DetailsGenerator<T> generator;
  660. /**
  661. * Sets the details component generator.
  662. *
  663. * @param generator
  664. * the generator for details components
  665. */
  666. public void setDetailsGenerator(DetailsGenerator<T> generator) {
  667. if (this.generator != generator) {
  668. removeAllComponents();
  669. }
  670. getState().hasDetailsGenerator = generator != null;
  671. this.generator = generator;
  672. visibleDetails.forEach(this::refresh);
  673. }
  674. @Override
  675. public void remove() {
  676. removeAllComponents();
  677. super.remove();
  678. }
  679. private void removeAllComponents() {
  680. // Clean up old components
  681. components.values().forEach(this::removeComponentFromGrid);
  682. components.clear();
  683. }
  684. @Override
  685. public void generateData(T item, JsonObject jsonObject) {
  686. if (generator == null || !visibleDetails.contains(item)) {
  687. return;
  688. }
  689. if (!components.containsKey(item)) {
  690. Component detailsComponent = generator.apply(item);
  691. Objects.requireNonNull(detailsComponent,
  692. "Details generator can't create null components");
  693. if (detailsComponent.getParent() != null) {
  694. throw new IllegalStateException(
  695. "Details component was already attached");
  696. }
  697. addComponentToGrid(detailsComponent);
  698. components.put(item, detailsComponent);
  699. }
  700. jsonObject.put(GridState.JSONKEY_DETAILS_VISIBLE,
  701. components.get(item).getConnectorId());
  702. }
  703. @Override
  704. public void destroyData(T item) {
  705. // No clean up needed. Components are removed when hiding details
  706. // and/or changing details generator
  707. }
  708. /**
  709. * Sets the visibility of details component for given item.
  710. *
  711. * @param item
  712. * the item to show details for
  713. * @param visible
  714. * {@code true} if details component should be visible;
  715. * {@code false} if it should be hidden
  716. */
  717. public void setDetailsVisible(T item, boolean visible) {
  718. boolean refresh = false;
  719. if (!visible) {
  720. refresh = visibleDetails.remove(item);
  721. if (components.containsKey(item)) {
  722. removeComponentFromGrid(components.remove(item));
  723. }
  724. } else {
  725. refresh = visibleDetails.add(item);
  726. }
  727. if (refresh) {
  728. refresh(item);
  729. }
  730. }
  731. /**
  732. * Returns the visibility of details component for given item.
  733. *
  734. * @param item
  735. * the item to show details for
  736. *
  737. * @return {@code true} if details component should be visible;
  738. * {@code false} if it should be hidden
  739. */
  740. public boolean isDetailsVisible(T item) {
  741. return visibleDetails.contains(item);
  742. }
  743. @Override
  744. public Grid<T> getParent() {
  745. return super.getParent();
  746. }
  747. @Override
  748. protected DetailsManagerState getState() {
  749. return (DetailsManagerState) super.getState();
  750. }
  751. @Override
  752. protected DetailsManagerState getState(boolean markAsDirty) {
  753. return (DetailsManagerState) super.getState(markAsDirty);
  754. }
  755. }
  756. /**
  757. * This extension manages the configuration and data communication for a
  758. * Column inside of a Grid component.
  759. *
  760. * @param <T>
  761. * the grid bean type
  762. * @param <V>
  763. * the column value type
  764. */
  765. public static class Column<T, V> extends AbstractExtension {
  766. /**
  767. * behavior when parsing nested properties which may contain
  768. * <code>null</code> values in the property chain
  769. */
  770. public enum NestedNullBehavior {
  771. /**
  772. * throw a NullPointerException if there is a nested
  773. * <code>null</code> value
  774. */
  775. THROW,
  776. /**
  777. * silently ignore any exceptions caused by nested <code>null</code>
  778. * values
  779. */
  780. ALLOW_NULLS
  781. }
  782. private final ValueProvider<T, V> valueProvider;
  783. private ValueProvider<V, ?> presentationProvider;
  784. private SortOrderProvider sortOrderProvider = direction -> {
  785. String id = getId();
  786. if (id == null) {
  787. return Stream.empty();
  788. }
  789. return Stream.of(new QuerySortOrder(id, direction));
  790. };
  791. private NestedNullBehavior nestedNullBehavior = NestedNullBehavior.THROW;
  792. private boolean sortable = true;
  793. private SerializableComparator<T> comparator;
  794. private StyleGenerator<T> styleGenerator = item -> null;
  795. private DescriptionGenerator<T> descriptionGenerator;
  796. private DataGenerator<T> dataGenerator = new DataGenerator<T>() {
  797. @Override
  798. public void generateData(T item, JsonObject jsonObject) {
  799. ColumnState state = getState(false);
  800. String communicationId = getConnectorId();
  801. assert communicationId != null : "No communication ID set for column "
  802. + state.caption;
  803. JsonObject obj = getDataObject(jsonObject,
  804. DataCommunicatorConstants.DATA);
  805. obj.put(communicationId, generateRendererValue(item,
  806. presentationProvider, state.renderer));
  807. String style = styleGenerator.apply(item);
  808. if (style != null && !style.isEmpty()) {
  809. JsonObject styleObj = getDataObject(jsonObject,
  810. GridState.JSONKEY_CELLSTYLES);
  811. styleObj.put(communicationId, style);
  812. }
  813. if (descriptionGenerator != null) {
  814. String description = descriptionGenerator.apply(item);
  815. if (description != null && !description.isEmpty()) {
  816. JsonObject descriptionObj = getDataObject(jsonObject,
  817. GridState.JSONKEY_CELLDESCRIPTION);
  818. descriptionObj.put(communicationId, description);
  819. }
  820. }
  821. }
  822. @Override
  823. public void destroyData(T item) {
  824. removeComponent(getGrid().getDataProvider().getId(item));
  825. }
  826. @Override
  827. public void destroyAllData() {
  828. // Make a defensive copy of keys, as the map gets cleared when
  829. // removing components.
  830. new HashSet<>(activeComponents.keySet())
  831. .forEach(component -> removeComponent(component));
  832. }
  833. };
  834. private Binding<T, ?> editorBinding;
  835. private Map<Object, Component> activeComponents = new HashMap<>();
  836. private String userId;
  837. /**
  838. * Constructs a new Column configuration with given renderer and value
  839. * provider.
  840. *
  841. * @param valueProvider
  842. * the function to get values from items, not
  843. * <code>null</code>
  844. * @param renderer
  845. * the value renderer, not <code>null</code>
  846. */
  847. protected Column(ValueProvider<T, V> valueProvider,
  848. Renderer<? super V> renderer) {
  849. this(valueProvider, ValueProvider.identity(), renderer);
  850. }
  851. /**
  852. * Constructs a new Column configuration with given renderer and value
  853. * provider.
  854. * <p>
  855. * For a more complete explanation on presentation provider, see
  856. * {@link #setRenderer(ValueProvider, Renderer)}.
  857. *
  858. * @param valueProvider
  859. * the function to get values from items, not
  860. * <code>null</code>
  861. * @param presentationProvider
  862. * the function to get presentations from the value of this
  863. * column, not <code>null</code>. For more details, see
  864. * {@link #setRenderer(ValueProvider, Renderer)}
  865. * @param renderer
  866. * the presentation renderer, not <code>null</code>
  867. * @param <P>
  868. * the presentation type
  869. *
  870. * @since 8.1
  871. */
  872. protected <P> Column(ValueProvider<T, V> valueProvider,
  873. ValueProvider<V, P> presentationProvider,
  874. Renderer<? super P> renderer) {
  875. Objects.requireNonNull(valueProvider,
  876. "Value provider can't be null");
  877. Objects.requireNonNull(presentationProvider,
  878. "Presentation provider can't be null");
  879. Objects.requireNonNull(renderer, "Renderer can't be null");
  880. ColumnState state = getState();
  881. this.valueProvider = valueProvider;
  882. this.presentationProvider = presentationProvider;
  883. state.renderer = renderer;
  884. state.caption = "";
  885. // Add the renderer as a child extension of this extension, thus
  886. // ensuring the renderer will be unregistered when this column is
  887. // removed
  888. addExtension(renderer);
  889. Class<? super P> valueType = renderer.getPresentationType();
  890. if (Comparable.class.isAssignableFrom(valueType)) {
  891. comparator = (a, b) -> compareComparables(
  892. valueProvider.apply(a), valueProvider.apply(b));
  893. } else if (Number.class.isAssignableFrom(valueType)) {
  894. /*
  895. * Value type will be Number whenever using NumberRenderer.
  896. * Provide explicit comparison support in this case even though
  897. * Number itself isn't Comparable.
  898. */
  899. comparator = (a, b) -> compareNumbers(
  900. (Number) valueProvider.apply(a),
  901. (Number) valueProvider.apply(b));
  902. } else {
  903. comparator = (a, b) -> compareMaybeComparables(
  904. valueProvider.apply(a), valueProvider.apply(b));
  905. }
  906. }
  907. /**
  908. * Constructs a new Column configuration with given renderer and value
  909. * provider.
  910. * <p>
  911. * For a more complete explanation on presentation provider, see
  912. * {@link #setRenderer(ValueProvider, Renderer)}.
  913. *
  914. * @param valueProvider
  915. * the function to get values from items, not
  916. * <code>null</code>
  917. * @param presentationProvider
  918. * the function to get presentations from the value of this
  919. * column, not <code>null</code>. For more details, see
  920. * {@link #setRenderer(ValueProvider, Renderer)}
  921. * @param nestedNullBehavior
  922. * behavior on encountering nested <code>null</code> values
  923. * when reading the value from the bean
  924. * @param renderer
  925. * the presentation renderer, not <code>null</code>
  926. * @param <P>
  927. * the presentation type
  928. *
  929. * @since 8.8
  930. */
  931. protected <P> Column(ValueProvider<T, V> valueProvider,
  932. ValueProvider<V, P> presentationProvider,
  933. Renderer<? super P> renderer,
  934. NestedNullBehavior nestedNullBehavior) {
  935. this(valueProvider, presentationProvider, renderer);
  936. this.nestedNullBehavior = nestedNullBehavior;
  937. }
  938. private static int compareMaybeComparables(Object a, Object b) {
  939. if (hasCommonComparableBaseType(a, b)) {
  940. return compareComparables(a, b);
  941. }
  942. return compareComparables(Objects.toString(a, ""),
  943. Objects.toString(b, ""));
  944. }
  945. private static boolean hasCommonComparableBaseType(Object a, Object b) {
  946. if (a instanceof Comparable<?> && b instanceof Comparable<?>) {
  947. Class<?> aClass = a.getClass();
  948. Class<?> bClass = b.getClass();
  949. if (aClass == bClass) {
  950. return true;
  951. }
  952. Class<?> baseType = ReflectTools.findCommonBaseType(aClass,
  953. bClass);
  954. if (Comparable.class.isAssignableFrom(baseType)) {
  955. return true;
  956. }
  957. }
  958. if ((a == null && b instanceof Comparable<?>)
  959. || (b == null && a instanceof Comparable<?>)) {
  960. return true;
  961. }
  962. return false;
  963. }
  964. @SuppressWarnings({ "unchecked", "rawtypes" })
  965. private static int compareComparables(Object a, Object b) {
  966. return ((Comparator) Comparator
  967. .nullsLast(Comparator.naturalOrder())).compare(a, b);
  968. }
  969. @SuppressWarnings("unchecked")
  970. private static int compareNumbers(Number a, Number b) {
  971. Number valueA = a != null ? a : Double.POSITIVE_INFINITY;
  972. Number valueB = b != null ? b : Double.POSITIVE_INFINITY;
  973. // Most Number implementations are Comparable
  974. if (valueA instanceof Comparable
  975. && valueA.getClass().isInstance(valueB)) {
  976. return ((Comparable<Number>) valueA).compareTo(valueB);
  977. }
  978. if (valueA.equals(valueB)) {
  979. return 0;
  980. }
  981. // Fall back to comparing based on potentially truncated values
  982. int compare = Long.compare(valueA.longValue(), valueB.longValue());
  983. if (compare == 0) {
  984. // This might still produce 0 even though the values are not
  985. // equals, but there's nothing more we can do about that
  986. compare = Double.compare(valueA.doubleValue(),
  987. valueB.doubleValue());
  988. }
  989. return compare;
  990. }
  991. @SuppressWarnings("unchecked")
  992. private <P> JsonValue generateRendererValue(T item,
  993. ValueProvider<V, P> presentationProvider, Connector renderer) {
  994. V value;
  995. try {
  996. value = valueProvider.apply(item);
  997. } catch (NullPointerException npe) {
  998. value = null;
  999. if (NestedNullBehavior.THROW == nestedNullBehavior) {
  1000. throw npe;
  1001. }
  1002. }
  1003. P presentationValue = presentationProvider.apply(value);
  1004. // Make Grid track components.
  1005. if (renderer instanceof ComponentRenderer
  1006. && presentationValue instanceof Component) {
  1007. addComponent(getGrid().getDataProvider().getId(item),
  1008. (Component) presentationValue);
  1009. }
  1010. return ((Renderer<P>) renderer).encode(presentationValue);
  1011. }
  1012. private void addComponent(Object item, Component component) {
  1013. if (activeComponents.containsKey(item)) {
  1014. if (activeComponents.get(item).equals(component)) {
  1015. // Reusing old component
  1016. return;
  1017. }
  1018. removeComponent(item);
  1019. }
  1020. activeComponents.put(item, component);
  1021. getGrid().addExtensionComponent(component);
  1022. }
  1023. private void removeComponent(Object item) {
  1024. Component component = activeComponents.remove(item);
  1025. if (component != null) {
  1026. getGrid().removeExtensionComponent(component);
  1027. }
  1028. }
  1029. /**
  1030. * Gets a data object with the given key from the given JsonObject. If
  1031. * there is no object with the key, this method creates a new
  1032. * JsonObject.
  1033. *
  1034. * @param jsonObject
  1035. * the json object
  1036. * @param key
  1037. * the key where the desired data object is stored
  1038. * @return data object for the given key
  1039. */
  1040. private JsonObject getDataObject(JsonObject jsonObject, String key) {
  1041. if (!jsonObject.hasKey(key)) {
  1042. jsonObject.put(key, Json.createObject());
  1043. }
  1044. return jsonObject.getObject(key);
  1045. }
  1046. @Override
  1047. protected ColumnState getState() {
  1048. return getState(true);
  1049. }
  1050. @Override
  1051. protected ColumnState getState(boolean markAsDirty) {
  1052. return (ColumnState) super.getState(markAsDirty);
  1053. }
  1054. /**
  1055. * This method extends the given Grid with this Column.
  1056. *
  1057. * @param grid
  1058. * the grid to extend
  1059. */
  1060. private void extend(Grid<T> grid) {
  1061. super.extend(grid);
  1062. }
  1063. /**
  1064. * Returns the identifier used with this Column in communication.
  1065. *
  1066. * @return the identifier string
  1067. */
  1068. private String getInternalId() {
  1069. return getState(false).internalId;
  1070. }
  1071. /**
  1072. * Sets the identifier to use with this Column in communication.
  1073. *
  1074. * @param id
  1075. * the identifier string
  1076. */
  1077. private void setInternalId(String id) {
  1078. Objects.requireNonNull(id, "Communication ID can't be null");
  1079. getState().internalId = id;
  1080. }
  1081. /**
  1082. * Returns the user-defined identifier for this column.
  1083. *
  1084. * @return the identifier string
  1085. */
  1086. public String getId() {
  1087. return userId;
  1088. }
  1089. /**
  1090. * Sets the user-defined identifier to map this column. The identifier
  1091. * can be used for example in {@link Grid#getColumn(String)}.
  1092. * <p>
  1093. * The id is also used as the {@link #setSortProperty(String...) backend
  1094. * sort property} for this column if no sort property or sort order
  1095. * provider has been set for this column.
  1096. *
  1097. * @see #setSortProperty(String...)
  1098. * @see #setSortOrderProvider(SortOrderProvider)
  1099. *
  1100. * @param id
  1101. * the identifier string
  1102. * @return this column
  1103. */
  1104. public Column<T, V> setId(String id) {
  1105. Objects.requireNonNull(id, "Column identifier cannot be null");
  1106. if (userId != null) {
  1107. throw new IllegalStateException(
  1108. "Column identifier cannot be changed");
  1109. }
  1110. userId = id;
  1111. getGrid().setColumnId(id, this);
  1112. updateSortable();
  1113. return this;
  1114. }
  1115. private void updateSortable() {
  1116. boolean inMemory = getGrid().getDataProvider().isInMemory();
  1117. boolean hasSortOrder = getSortOrder(SortDirection.ASCENDING)
  1118. .count() != 0;
  1119. getState().sortable = this.sortable && (inMemory || hasSortOrder);
  1120. }
  1121. /**
  1122. * Gets the function used to produce the value for data in this column
  1123. * based on the row item.
  1124. *
  1125. * @return the value provider function
  1126. *
  1127. * @since 8.0.3
  1128. */
  1129. public ValueProvider<T, V> getValueProvider() {
  1130. return valueProvider;
  1131. }
  1132. /**
  1133. * Sets whether the user can sort this column or not. Whether the column
  1134. * is actually sortable after {@code setSortable(true)} depends on the
  1135. * {@link DataProvider} and the defined sort order for this column. When
  1136. * using an {@link InMemoryDataProvider} sorting can be automatic.
  1137. *
  1138. * @param sortable
  1139. * {@code true} to enable sorting for this column;
  1140. * {@code false} to disable it
  1141. * @return this column
  1142. */
  1143. public Column<T, V> setSortable(boolean sortable) {
  1144. if (this.sortable != sortable) {
  1145. this.sortable = sortable;
  1146. updateSortable();
  1147. }
  1148. return this;
  1149. }
  1150. /**
  1151. * Gets whether sorting is enabled for this column.
  1152. *
  1153. * @return {@code true} if the sorting is enabled for this column;
  1154. * {@code false} if not
  1155. */
  1156. public boolean isSortable() {
  1157. return sortable;
  1158. }
  1159. /**
  1160. * Gets whether the user can actually sort this column.
  1161. *
  1162. * @return {@code true} if the column can be sorted by the user;
  1163. * {@code false} if not
  1164. *
  1165. * @since 8.3.2
  1166. */
  1167. public boolean isSortableByUser() {
  1168. return getState(false).sortable;
  1169. }
  1170. /**
  1171. * Sets the header aria-label for this column.
  1172. *
  1173. * @param caption
  1174. * the header aria-label, null removes the aria-label from
  1175. * this column
  1176. *
  1177. * @return this column
  1178. *
  1179. * @since 8.2
  1180. */
  1181. public Column<T, V> setAssistiveCaption(String caption) {
  1182. if (Objects.equals(caption, getAssistiveCaption())) {
  1183. return this;
  1184. }
  1185. getState().assistiveCaption = caption;
  1186. return this;
  1187. }
  1188. /**
  1189. * Gets the header caption for this column.
  1190. *
  1191. * @return header caption
  1192. *
  1193. * @since 8.2
  1194. */
  1195. public String getAssistiveCaption() {
  1196. return getState(false).assistiveCaption;
  1197. }
  1198. /**
  1199. * Sets the header caption for this column.
  1200. *
  1201. * @param caption
  1202. * the header caption, not null
  1203. *
  1204. * @return this column
  1205. */
  1206. public Column<T, V> setCaption(String caption) {
  1207. Objects.requireNonNull(caption, "Header caption can't be null");
  1208. caption = Jsoup.parse(caption).text();
  1209. if (caption.equals(getState(false).caption)) {
  1210. return this;
  1211. }
  1212. getState().caption = caption;
  1213. HeaderRow row = getGrid().getDefaultHeaderRow();
  1214. if (row != null) {
  1215. row.getCell(this).setText(caption);
  1216. }
  1217. return this;
  1218. }
  1219. /**
  1220. * Gets the header caption for this column.
  1221. *
  1222. * @return header caption
  1223. */
  1224. public String getCaption() {
  1225. return getState(false).caption;
  1226. }
  1227. /**
  1228. * Sets a comparator to use with in-memory sorting with this column.
  1229. * Sorting with a back-end is done using
  1230. * {@link Column#setSortProperty(String...)}.
  1231. *
  1232. * @param comparator
  1233. * the comparator to use when sorting data in this column
  1234. * @return this column
  1235. */
  1236. public Column<T, V> setComparator(
  1237. SerializableComparator<T> comparator) {
  1238. Objects.requireNonNull(comparator, "Comparator can't be null");
  1239. this.comparator = comparator;
  1240. return this;
  1241. }
  1242. /**
  1243. * Gets the comparator to use with in-memory sorting for this column
  1244. * when sorting in the given direction.
  1245. *
  1246. * @param sortDirection
  1247. * the direction this column is sorted by
  1248. * @return comparator for this column
  1249. */
  1250. public SerializableComparator<T> getComparator(
  1251. SortDirection sortDirection) {
  1252. Objects.requireNonNull(comparator,
  1253. "No comparator defined for sorted column.");
  1254. boolean reverse = sortDirection != SortDirection.ASCENDING;
  1255. return reverse ? (t1, t2) -> comparator.reversed().compare(t1, t2)
  1256. : comparator;
  1257. }
  1258. /**
  1259. * Sets strings describing back end properties to be used when sorting
  1260. * this column.
  1261. * <p>
  1262. * By default, the {@link #setId(String) column id} will be used as the
  1263. * sort property.
  1264. *
  1265. * @param properties
  1266. * the array of strings describing backend properties
  1267. * @return this column
  1268. */
  1269. public Column<T, V> setSortProperty(String... properties) {
  1270. Objects.requireNonNull(properties, "Sort properties can't be null");
  1271. return setSortOrderProvider(dir -> Arrays.stream(properties)
  1272. .map(s -> new QuerySortOrder(s, dir)));
  1273. }
  1274. /**
  1275. * Sets the sort orders when sorting this column. The sort order
  1276. * provider is a function which provides {@link QuerySortOrder} objects
  1277. * to describe how to sort by this column.
  1278. * <p>
  1279. * By default, the {@link #setId(String) column id} will be used as the
  1280. * sort property.
  1281. *
  1282. * @param provider
  1283. * the function to use when generating sort orders with the
  1284. * given direction
  1285. * @return this column
  1286. */
  1287. public Column<T, V> setSortOrderProvider(SortOrderProvider provider) {
  1288. Objects.requireNonNull(provider,
  1289. "Sort order provider can't be null");
  1290. sortOrderProvider = provider;
  1291. // Update state
  1292. updateSortable();
  1293. return this;
  1294. }
  1295. /**
  1296. * Gets the sort orders to use with back-end sorting for this column
  1297. * when sorting in the given direction.
  1298. *
  1299. * @see #setSortProperty(String...)
  1300. * @see #setId(String)
  1301. * @see #setSortOrderProvider(SortOrderProvider)
  1302. *
  1303. * @param direction
  1304. * the sorting direction
  1305. * @return stream of sort orders
  1306. */
  1307. public Stream<QuerySortOrder> getSortOrder(SortDirection direction) {
  1308. return sortOrderProvider.apply(direction);
  1309. }
  1310. /**
  1311. * Sets the style generator that is used for generating class names for
  1312. * cells in this column. Returning null from the generator results in no
  1313. * custom style name being set.
  1314. *
  1315. * Note: The style generator is applied only to the body cells, not to
  1316. * the Editor.
  1317. *
  1318. * @param cellStyleGenerator
  1319. * the cell style generator to set, not null
  1320. * @return this column
  1321. * @throws NullPointerException
  1322. * if {@code cellStyleGenerator} is {@code null}
  1323. */
  1324. public Column<T, V> setStyleGenerator(
  1325. StyleGenerator<T> cellStyleGenerator) {
  1326. Objects.requireNonNull(cellStyleGenerator,
  1327. "Cell style generator must not be null");
  1328. this.styleGenerator = cellStyleGenerator;
  1329. getGrid().getDataCommunicator().reset();
  1330. return this;
  1331. }
  1332. /**
  1333. * Gets the style generator that is used for generating styles for
  1334. * cells.
  1335. *
  1336. * @return the cell style generator
  1337. */
  1338. public StyleGenerator<T> getStyleGenerator() {
  1339. return styleGenerator;
  1340. }
  1341. /**
  1342. * Sets the description generator that is used for generating
  1343. * descriptions for cells in this column. This method uses the
  1344. * {@link ContentMode#PREFORMATTED} content mode.
  1345. *
  1346. * @see #setDescriptionGenerator(DescriptionGenerator, ContentMode)
  1347. *
  1348. * @param cellDescriptionGenerator
  1349. * the cell description generator to set, or {@code null} to
  1350. * remove a previously set generator
  1351. * @return this column
  1352. */
  1353. public Column<T, V> setDescriptionGenerator(
  1354. DescriptionGenerator<T> cellDescriptionGenerator) {
  1355. return setDescriptionGenerator(cellDescriptionGenerator,
  1356. ContentMode.PREFORMATTED);
  1357. }
  1358. /**
  1359. * Sets the description generator that is used for generating
  1360. * descriptions for cells in this column. This method uses the given
  1361. * content mode.
  1362. *
  1363. * @see #setDescriptionGenerator(DescriptionGenerator)
  1364. *
  1365. * @param cellDescriptionGenerator
  1366. * the cell description generator to set, or {@code null} to
  1367. * remove a previously set generator
  1368. * @param tooltipContentMode
  1369. * the content mode for tooltips
  1370. * @return this column
  1371. *
  1372. * @since 8.2
  1373. */
  1374. public Column<T, V> setDescriptionGenerator(
  1375. DescriptionGenerator<T> cellDescriptionGenerator,
  1376. ContentMode tooltipContentMode) {
  1377. this.descriptionGenerator = cellDescriptionGenerator;
  1378. getState().tooltipContentMode = tooltipContentMode;
  1379. getGrid().getDataCommunicator().reset();
  1380. return this;
  1381. }
  1382. /**
  1383. * Gets the description generator that is used for generating
  1384. * descriptions for cells.
  1385. *
  1386. * @return the cell description generator, or <code>null</code> if no
  1387. * generator is set
  1388. */
  1389. public DescriptionGenerator<T> getDescriptionGenerator() {
  1390. return descriptionGenerator;
  1391. }
  1392. /**
  1393. * Sets the ratio with which the column expands.
  1394. * <p>
  1395. * By default, all columns expand equally (treated as if all of them had
  1396. * an expand ratio of 1). Once at least one column gets a defined expand
  1397. * ratio, the implicit expand ratio is removed, and only the defined
  1398. * expand ratios are taken into account.
  1399. * <p>
  1400. * If a column has a defined width ({@link #setWidth(double)}), it
  1401. * overrides this method's effects.
  1402. * <p>
  1403. * <em>Example:</em> A grid with three columns, with expand ratios 0, 1
  1404. * and 2, respectively. The column with a <strong>ratio of 0 is exactly
  1405. * as wide as its contents requires</strong>. The column with a ratio of
  1406. * 1 is as wide as it needs, <strong>plus a third of any excess
  1407. * space</strong>, because we have 3 parts total, and this column
  1408. * reserves only one of those. The column with a ratio of 2, is as wide
  1409. * as it needs to be, <strong>plus two thirds</strong> of the excess
  1410. * width.
  1411. *
  1412. * @param expandRatio
  1413. * the expand ratio of this column. {@code 0} to not have it
  1414. * expand at all. A negative number to clear the expand
  1415. * value.
  1416. * @throws IllegalStateException
  1417. * if the column is no longer attached to any grid
  1418. * @see #setWidth(double)
  1419. */
  1420. public Column<T, V> setExpandRatio(int expandRatio)
  1421. throws IllegalStateException {
  1422. checkColumnIsAttached();
  1423. if (expandRatio != getExpandRatio()) {
  1424. getState().expandRatio = expandRatio;
  1425. getGrid().markAsDirty();
  1426. }
  1427. return this;
  1428. }
  1429. /**
  1430. * Returns the column's expand ratio.
  1431. *
  1432. * @return the column's expand ratio
  1433. * @see #setExpandRatio(int)
  1434. */
  1435. public int getExpandRatio() {
  1436. return getState(false).expandRatio;
  1437. }
  1438. /**
  1439. * Clears the expand ratio for this column.
  1440. * <p>
  1441. * Equal to calling {@link #setExpandRatio(int) setExpandRatio(-1)}
  1442. *
  1443. * @throws IllegalStateException
  1444. * if the column is no longer attached to any grid
  1445. */
  1446. public Column<T, V> clearExpandRatio() throws IllegalStateException {
  1447. return setExpandRatio(-1);
  1448. }
  1449. /**
  1450. * Returns the width (in pixels). By default a column width is
  1451. * {@value com.vaadin.shared.ui.grid.GridConstants#DEFAULT_COLUMN_WIDTH_PX}
  1452. * (undefined).
  1453. *
  1454. * @return the width in pixels of the column
  1455. * @throws IllegalStateException
  1456. * if the column is no longer attached to any grid
  1457. */
  1458. public double getWidth() throws IllegalStateException {
  1459. checkColumnIsAttached();
  1460. return getState(false).width;
  1461. }
  1462. /**
  1463. * Sets the width (in pixels).
  1464. * <p>
  1465. * This overrides any configuration set by any of
  1466. * {@link #setExpandRatio(int)}, {@link #setMinimumWidth(double)} or
  1467. * {@link #setMaximumWidth(double)}.
  1468. *
  1469. * @param pixelWidth
  1470. * the new pixel width of the column
  1471. * @return the column itself
  1472. *
  1473. * @throws IllegalStateException
  1474. * if the column is no longer attached to any grid
  1475. * @throws IllegalArgumentException
  1476. * thrown if pixel width is less than zero
  1477. */
  1478. public Column<T, V> setWidth(double pixelWidth)
  1479. throws IllegalStateException, IllegalArgumentException {
  1480. checkColumnIsAttached();
  1481. if (pixelWidth < 0) {
  1482. throw new IllegalArgumentException(
  1483. "Pixel width should be greated than 0 (in " + toString()
  1484. + ")");
  1485. }
  1486. if (pixelWidth != getWidth()) {
  1487. getState().width = pixelWidth;
  1488. getGrid().markAsDirty();
  1489. getGrid().fireColumnResizeEvent(this, false);
  1490. }
  1491. return this;
  1492. }
  1493. /**
  1494. * Returns whether this column has an undefined width.
  1495. *
  1496. * @since 7.6
  1497. * @return whether the width is undefined
  1498. * @throws IllegalStateException
  1499. * if the column is no longer attached to any grid
  1500. */
  1501. public boolean isWidthUndefined() {
  1502. checkColumnIsAttached();
  1503. return getState(false).width < 0;
  1504. }
  1505. /**
  1506. * Marks the column width as undefined. An undefined width means the
  1507. * grid is free to resize the column based on the cell contents and
  1508. * available space in the grid.
  1509. *
  1510. * @return the column itself
  1511. */
  1512. public Column<T, V> setWidthUndefined() {
  1513. checkColumnIsAttached();
  1514. if (!isWidthUndefined()) {
  1515. getState().width = -1;
  1516. getGrid().markAsDirty();
  1517. getGrid().fireColumnResizeEvent(this, false);
  1518. }
  1519. return this;
  1520. }
  1521. /**
  1522. * Sets the minimum width for this column.
  1523. * <p>
  1524. * This defines the minimum guaranteed pixel width of the column
  1525. * <em>when it is set to expand</em>.
  1526. *
  1527. * Note: Value -1 is not accepted, use {@link #setWidthUndefined()}
  1528. * instead.
  1529. *
  1530. * @param pixels
  1531. * the minimum width for the column
  1532. * @throws IllegalStateException
  1533. * if the column is no longer attached to any grid
  1534. * @see #setExpandRatio(int)
  1535. * @return the column itself
  1536. */
  1537. public Column<T, V> setMinimumWidth(double pixels)
  1538. throws IllegalStateException {
  1539. checkColumnIsAttached();
  1540. final double maxwidth = getMaximumWidth();
  1541. if (pixels >= 0 && pixels > maxwidth && maxwidth >= 0) {
  1542. throw new IllegalArgumentException("New minimum width ("
  1543. + pixels + ") was greater than maximum width ("
  1544. + maxwidth + ")");
  1545. }
  1546. getState().minWidth = pixels;
  1547. getGrid().markAsDirty();
  1548. return this;
  1549. }
  1550. /**
  1551. * Return the minimum width for this column.
  1552. *
  1553. * @return the minimum width for this column
  1554. * @see #setMinimumWidth(double)
  1555. */
  1556. public double getMinimumWidth() {
  1557. return getState(false).minWidth;
  1558. }
  1559. /**
  1560. * Sets whether the width of the contents in the column should be
  1561. * considered minimum width for this column.
  1562. * <p>
  1563. * If this is set to <code>true</code> (default for backwards
  1564. * compatibility), then a column will not shrink to smaller than the
  1565. * width required to show the contents available when calculating the
  1566. * widths (only the widths of the initially rendered rows are
  1567. * considered).
  1568. * <p>
  1569. * If this is set to <code>false</code> and the column has been set to
  1570. * expand using #setExpandRatio(int), then the contents of the column
  1571. * will be ignored when calculating the width, and the column will thus
  1572. * shrink down to the minimum width defined by #setMinimumWidth(double)
  1573. * if necessary.
  1574. *
  1575. * @param minimumWidthFromContent
  1576. * <code>true</code> to reserve space for all contents,
  1577. * <code>false</code> to allow the column to shrink smaller
  1578. * than the contents
  1579. * @return the column itself
  1580. * @throws IllegalStateException
  1581. * if the column is no longer attached to any grid
  1582. * @see #setMinimumWidth(double)
  1583. * @since 8.1
  1584. */
  1585. public Column<T, V> setMinimumWidthFromContent(
  1586. boolean minimumWidthFromContent) throws IllegalStateException {
  1587. checkColumnIsAttached();
  1588. if (isMinimumWidthFromContent() != minimumWidthFromContent) {
  1589. getState().minimumWidthFromContent = minimumWidthFromContent;
  1590. getGrid().markAsDirty();
  1591. }
  1592. return this;
  1593. }
  1594. /**
  1595. * Gets whether the width of the contents in the column should be
  1596. * considered minimum width for this column.
  1597. *
  1598. * @return <code>true</code> to reserve space for all contents,
  1599. * <code>false</code> to allow the column to shrink smaller than
  1600. * the contents
  1601. * @see #setMinimumWidthFromContent(boolean)
  1602. * @since 8.1
  1603. */
  1604. public boolean isMinimumWidthFromContent() {
  1605. return getState(false).minimumWidthFromContent;
  1606. }
  1607. /**
  1608. * Sets the maximum width for this column.
  1609. * <p>
  1610. * This defines the maximum allowed pixel width of the column <em>when
  1611. * it is set to expand</em>.
  1612. *
  1613. * @param pixels
  1614. * the maximum width
  1615. * @throws IllegalStateException
  1616. * if the column is no longer attached to any grid
  1617. * @see #setExpandRatio(int)
  1618. */
  1619. public Column<T, V> setMaximumWidth(double pixels) {
  1620. checkColumnIsAttached();
  1621. final double minwidth = getMinimumWidth();
  1622. if (pixels >= 0 && pixels < minwidth && minwidth >= 0) {
  1623. throw new IllegalArgumentException("New maximum width ("
  1624. + pixels + ") was less than minimum width (" + minwidth
  1625. + ")");
  1626. }
  1627. getState().maxWidth = pixels;
  1628. getGrid().markAsDirty();
  1629. return this;
  1630. }
  1631. /**
  1632. * Returns the maximum width for this column.
  1633. *
  1634. * @return the maximum width for this column
  1635. * @see #setMaximumWidth(double)
  1636. */
  1637. public double getMaximumWidth() {
  1638. return getState(false).maxWidth;
  1639. }
  1640. /**
  1641. * Sets whether this column can be resized by the user.
  1642. *
  1643. * @since 7.6
  1644. * @param resizable
  1645. * {@code true} if this column should be resizable,
  1646. * {@code false} otherwise
  1647. * @throws IllegalStateException
  1648. * if the column is no longer attached to any grid
  1649. */
  1650. public Column<T, V> setResizable(boolean resizable) {
  1651. checkColumnIsAttached();
  1652. if (resizable != isResizable()) {
  1653. getState().resizable = resizable;
  1654. getGrid().markAsDirty();
  1655. }
  1656. return this;
  1657. }
  1658. /**
  1659. * Gets the caption of the hiding toggle for this column.
  1660. *
  1661. * @since 7.5.0
  1662. * @see #setHidingToggleCaption(String)
  1663. * @return the caption for the hiding toggle for this column
  1664. */
  1665. public String getHidingToggleCaption() {
  1666. return getState(false).hidingToggleCaption;
  1667. }
  1668. /**
  1669. * Sets the caption of the hiding toggle for this column. Shown in the
  1670. * toggle for this column in the grid's sidebar when the column is
  1671. * {@link #isHidable() hidable}.
  1672. * <p>
  1673. * The default value is <code>null</code>, and in that case the column's
  1674. * {@link #getCaption() header caption} is used.
  1675. * <p>
  1676. * <em>NOTE:</em> setting this to empty string might cause the hiding
  1677. * toggle to not render correctly.
  1678. *
  1679. * @since 7.5.0
  1680. * @param hidingToggleCaption
  1681. * the text to show in the column hiding toggle
  1682. * @return the column itself
  1683. */
  1684. public Column<T, V> setHidingToggleCaption(String hidingToggleCaption) {
  1685. if (hidingToggleCaption != getHidingToggleCaption()) {
  1686. getState().hidingToggleCaption = hidingToggleCaption;
  1687. }
  1688. return this;
  1689. }
  1690. /**
  1691. * Hides or shows the column. By default columns are visible before
  1692. * explicitly hiding them.
  1693. *
  1694. * @since 7.5.0
  1695. * @param hidden
  1696. * <code>true</code> to hide the column, <code>false</code>
  1697. * to show
  1698. * @return this column
  1699. * @throws IllegalStateException
  1700. * if the column is no longer attached to any grid
  1701. */
  1702. public Column<T, V> setHidden(boolean hidden) {
  1703. checkColumnIsAttached();
  1704. if (hidden != isHidden()) {
  1705. getState().hidden = hidden;
  1706. getGrid().fireColumnVisibilityChangeEvent(this, hidden, false);
  1707. }
  1708. return this;
  1709. }
  1710. /**
  1711. * Returns whether this column is hidden. Default is {@code false}.
  1712. *
  1713. * @since 7.5.0
  1714. * @return <code>true</code> if the column is currently hidden,
  1715. * <code>false</code> otherwise
  1716. */
  1717. public boolean isHidden() {
  1718. return getState(false).hidden;
  1719. }
  1720. /**
  1721. * Sets whether this column can be hidden by the user. Hidable columns
  1722. * can be hidden and shown via the sidebar menu.
  1723. *
  1724. * @since 7.5.0
  1725. * @param hidable
  1726. * <code>true</code> if the column may be hidable by the user
  1727. * via UI interaction
  1728. * @return this column
  1729. */
  1730. public Column<T, V> setHidable(boolean hidable) {
  1731. if (hidable != isHidable()) {
  1732. getState().hidable = hidable;
  1733. }
  1734. return this;
  1735. }
  1736. /**
  1737. * Returns whether this column can be hidden by the user. Default is
  1738. * {@code false}.
  1739. * <p>
  1740. * <em>Note:</em> the column can be programmatically hidden using
  1741. * {@link #setHidden(boolean)} regardless of the returned value.
  1742. *
  1743. * @since 7.5.0
  1744. * @return <code>true</code> if the user can hide the column,
  1745. * <code>false</code> if not
  1746. */
  1747. public boolean isHidable() {
  1748. return getState(false).hidable;
  1749. }
  1750. /**
  1751. * Returns whether this column can be resized by the user. Default is
  1752. * {@code true}.
  1753. * <p>
  1754. * <em>Note:</em> the column can be programmatically resized using
  1755. * {@link #setWidth(double)} and {@link #setWidthUndefined()} regardless
  1756. * of the returned value.
  1757. *
  1758. * @since 7.6
  1759. * @return {@code true} if this column is resizable, {@code false}
  1760. * otherwise
  1761. */
  1762. public boolean isResizable() {
  1763. return getState(false).resizable;
  1764. }
  1765. /**
  1766. * Sets whether this Column has a component displayed in Editor or not.
  1767. * A column can only be editable if an editor component or binding has
  1768. * been set.
  1769. *
  1770. * @param editable
  1771. * {@code true} if column is editable; {@code false} if not
  1772. * @return this column
  1773. * @throws IllegalStateException
  1774. * if editable is true and column has no editor binding or
  1775. * component defined
  1776. *
  1777. * @see #setEditorComponent(HasValue, Setter)
  1778. * @see #setEditorBinding(Binding)
  1779. */
  1780. public Column<T, V> setEditable(boolean editable)
  1781. throws IllegalStateException {
  1782. if (editable && editorBinding == null) {
  1783. throw new IllegalStateException(
  1784. "Column has no editor binding or component defined");
  1785. }
  1786. getState().editable = editable;
  1787. return this;
  1788. }
  1789. /**
  1790. * Gets whether this Column has a component displayed in Editor or not.
  1791. *
  1792. * @return {@code true} if the column displays an editor component;
  1793. * {@code false} if not
  1794. */
  1795. public boolean isEditable() {
  1796. return getState(false).editable;
  1797. }
  1798. /**
  1799. * Sets an editor binding for this column. The {@link Binding} is used
  1800. * when a row is in editor mode to define how to populate an editor
  1801. * component based on the edited row and how to update an item based on
  1802. * the value in the editor component.
  1803. * <p>
  1804. * To create a binding to use with a column, define a binding for the
  1805. * editor binder (<code>grid.getEditor().getBinder()</code>) using e.g.
  1806. * {@link Binder#forField(HasValue)}. You can also use
  1807. * {@link #setEditorComponent(HasValue, Setter)} if no validator or
  1808. * converter is needed for the binding.
  1809. * <p>
  1810. * The {@link HasValue} that the binding is defined to use must be a
  1811. * {@link Component}.
  1812. *
  1813. * @param binding
  1814. * the binding to use for this column
  1815. * @return this column
  1816. *
  1817. * @see #setEditorComponent(HasValue, Setter)
  1818. * @see Binding
  1819. * @see Grid#getEditor()
  1820. * @see Editor#getBinder()
  1821. */
  1822. public Column<T, V> setEditorBinding(Binding<T, ?> binding) {
  1823. Objects.requireNonNull(binding, "null is not a valid editor field");
  1824. if (!(binding.getField() instanceof Component)) {
  1825. throw new IllegalArgumentException(
  1826. "Binding target must be a component.");
  1827. }
  1828. this.editorBinding = binding;
  1829. return setEditable(true);
  1830. }
  1831. /**
  1832. * Gets the binder binding that is currently used for this column.
  1833. *
  1834. * @return the used binder binding, or <code>null</code> if no binding
  1835. * is configured
  1836. *
  1837. * @see #setEditorBinding(Binding)
  1838. */
  1839. public Binding<T, ?> getEditorBinding() {
  1840. return editorBinding;
  1841. }
  1842. /**
  1843. * Sets a component and setter to use for editing values of this column
  1844. * in the editor row. This is a shorthand for use in simple cases where
  1845. * no validator or converter is needed. Use
  1846. * {@link #setEditorBinding(Binding)} to support more complex cases.
  1847. * <p>
  1848. * <strong>Note:</strong> The same component cannot be used for multiple
  1849. * columns.
  1850. *
  1851. * @param editorComponent
  1852. * the editor component
  1853. * @param setter
  1854. * a setter that stores the component value in the row item
  1855. * @return this column
  1856. *
  1857. * @see #setEditorBinding(Binding)
  1858. * @see Grid#getEditor()
  1859. * @see Binder#bind(HasValue, ValueProvider, Setter)
  1860. */
  1861. public <C extends HasValue<V> & Component> Column<T, V> setEditorComponent(
  1862. C editorComponent, Setter<T, V> setter) {
  1863. Objects.requireNonNull(editorComponent,
  1864. "Editor component cannot be null");
  1865. Objects.requireNonNull(setter, "Setter cannot be null");
  1866. Binding<T, V> binding = getGrid().getEditor().getBinder()
  1867. .bind(editorComponent, valueProvider::apply, setter);
  1868. return setEditorBinding(binding);
  1869. }
  1870. /**
  1871. * Sets a component to use for editing values of this columns in the
  1872. * editor row. This method can only be used if the column has an
  1873. * {@link #setId(String) id} and the {@link Grid} has been created using
  1874. * {@link Grid#Grid(Class)} or some other way that allows finding
  1875. * properties based on property names.
  1876. * <p>
  1877. * This is a shorthand for use in simple cases where no validator or
  1878. * converter is needed. Use {@link #setEditorBinding(Binding)} to
  1879. * support more complex cases.
  1880. * <p>
  1881. * <strong>Note:</strong> The same component cannot be used for multiple
  1882. * columns.
  1883. *
  1884. * @param editorComponent
  1885. * the editor component
  1886. * @return this column
  1887. *
  1888. * @see #setEditorBinding(Binding)
  1889. * @see Grid#getEditor()
  1890. * @see Binder#bind(HasValue, String)
  1891. * @see Grid#Grid(Class)
  1892. */
  1893. public <F, C extends HasValue<F> & Component> Column<T, V> setEditorComponent(
  1894. C editorComponent) {
  1895. Objects.requireNonNull(editorComponent,
  1896. "Editor component cannot be null");
  1897. String propertyName = getId();
  1898. if (propertyName == null) {
  1899. throw new IllegalStateException(
  1900. "setEditorComponent without a setter can only be used if the column has an id. "
  1901. + "Use another setEditorComponent(Component, Setter) or setEditorBinding(Binding) instead.");
  1902. }
  1903. Binding<T, F> binding = getGrid().getEditor().getBinder()
  1904. .bind(editorComponent, propertyName);
  1905. return setEditorBinding(binding);
  1906. }
  1907. /**
  1908. * Sets the Renderer for this Column. Setting the renderer will cause
  1909. * all currently available row data to be recreated and sent to the
  1910. * client.
  1911. *
  1912. * Note: Setting a new renderer will reset presentation provider if
  1913. * it exists.
  1914. *
  1915. * @param renderer
  1916. * the new renderer
  1917. * @return this column
  1918. *
  1919. * @since 8.0.3
  1920. */
  1921. public Column<T, V> setRenderer(Renderer<? super V> renderer) {
  1922. return setRenderer(ValueProvider.identity(), renderer);
  1923. }
  1924. /**
  1925. * Sets the Renderer for this Column. Setting the renderer will cause
  1926. * all currently available row data to be recreated and sent to the
  1927. * client.
  1928. * <p>
  1929. * The presentation provider is a method that takes the value of this
  1930. * column on a single row, and maps that to a value that the renderer
  1931. * accepts. This feature can be used for storing a complex value in a
  1932. * column for editing, but providing a simplified presentation for the
  1933. * user when not editing.
  1934. *
  1935. * @param presentationProvider
  1936. * the function to get presentations from the value of this
  1937. * column, not {@code null}
  1938. * @param renderer
  1939. * the new renderer, not {@code null}
  1940. *
  1941. * @param <P>
  1942. * the presentation type
  1943. *
  1944. * @return this column
  1945. *
  1946. * @since 8.1
  1947. */
  1948. public <P> Column<T, V> setRenderer(
  1949. ValueProvider<V, P> presentationProvider,
  1950. Renderer<? super P> renderer) {
  1951. Objects.requireNonNull(renderer, "Renderer can not be null");
  1952. Objects.requireNonNull(presentationProvider,
  1953. "Presentation provider can not be null");
  1954. // Remove old renderer
  1955. Connector oldRenderer = getState().renderer;
  1956. if (oldRenderer instanceof Extension) {
  1957. removeExtension((Extension) oldRenderer);
  1958. }
  1959. // Set new renderer
  1960. getState().renderer = renderer;
  1961. addExtension(renderer);
  1962. this.presentationProvider = presentationProvider;
  1963. // Trigger redraw
  1964. getGrid().getDataCommunicator().reset();
  1965. return this;
  1966. }
  1967. /**
  1968. * Gets the Renderer for this Column.
  1969. *
  1970. * @return the renderer
  1971. * @since 8.1
  1972. */
  1973. public Renderer<?> getRenderer() {
  1974. return (Renderer<?>) getState().renderer;
  1975. }
  1976. /**
  1977. * Sets whether Grid should handle events in this Column from Components
  1978. * and Widgets rendered by certain Renderers. By default the events are
  1979. * not handled.
  1980. * <p>
  1981. * <strong>Note:</strong> Enabling this feature will for example select
  1982. * a row when a component is clicked. For example in the case of a
  1983. * {@link ComboBox} or {@link TextField} it might be problematic as the
  1984. * component gets re-rendered and might lose focus.
  1985. *
  1986. * @param handleWidgetEvents
  1987. * {@code true} to handle events; {@code false} to not
  1988. * @return this column
  1989. * @since 8.3
  1990. */
  1991. public Column<T, V> setHandleWidgetEvents(boolean handleWidgetEvents) {
  1992. getState().handleWidgetEvents = handleWidgetEvents;
  1993. return this;
  1994. }
  1995. /**
  1996. * Gets whether Grid is handling the events in this Column from
  1997. * Component and Widgets.
  1998. *
  1999. * @see #setHandleWidgetEvents(boolean)
  2000. *
  2001. * @return {@code true} if handling events; {@code false} if not
  2002. * @since 8.3
  2003. */
  2004. public boolean isHandleWidgetEvents() {
  2005. return getState(false).handleWidgetEvents;
  2006. }
  2007. /**
  2008. * Gets the grid that this column belongs to.
  2009. *
  2010. * @return the grid that this column belongs to, or <code>null</code> if
  2011. * this column has not yet been associated with any grid
  2012. */
  2013. @SuppressWarnings("unchecked")
  2014. protected Grid<T> getGrid() {
  2015. return (Grid<T>) getParent();
  2016. }
  2017. /**
  2018. * Checks if column is attached and throws an
  2019. * {@link IllegalStateException} if it is not.
  2020. *
  2021. * @throws IllegalStateException
  2022. * if the column is no longer attached to any grid
  2023. */
  2024. protected void checkColumnIsAttached() throws IllegalStateException {
  2025. if (getGrid() == null) {
  2026. throw new IllegalStateException(
  2027. "Column is no longer attached to a grid.");
  2028. }
  2029. }
  2030. /**
  2031. * Writes the design attributes for this column into given element.
  2032. *
  2033. * @since 7.5.0
  2034. *
  2035. * @param element
  2036. * Element to write attributes into
  2037. *
  2038. * @param designContext
  2039. * the design context
  2040. */
  2041. protected void writeDesign(Element element,
  2042. DesignContext designContext) {
  2043. Attributes attributes = element.attributes();
  2044. ColumnState defaultState = new ColumnState();
  2045. if (getId() == null) {
  2046. setId("column" + getGrid().getColumns().indexOf(this));
  2047. }
  2048. DesignAttributeHandler.writeAttribute("column-id", attributes,
  2049. getId(), null, String.class, designContext);
  2050. // Sortable is a special attribute that depends on the data
  2051. // provider.
  2052. DesignAttributeHandler.writeAttribute("sortable", attributes,
  2053. isSortable(), null, boolean.class, designContext);
  2054. DesignAttributeHandler.writeAttribute("editable", attributes,
  2055. isEditable(), defaultState.editable, boolean.class,
  2056. designContext);
  2057. DesignAttributeHandler.writeAttribute("resizable", attributes,
  2058. isResizable(), defaultState.resizable, boolean.class,
  2059. designContext);
  2060. DesignAttributeHandler.writeAttribute("hidable", attributes,
  2061. isHidable(), defaultState.hidable, boolean.class,
  2062. designContext);
  2063. DesignAttributeHandler.writeAttribute("hidden", attributes,
  2064. isHidden(), defaultState.hidden, boolean.class,
  2065. designContext);
  2066. DesignAttributeHandler.writeAttribute("hiding-toggle-caption",
  2067. attributes, getHidingToggleCaption(),
  2068. defaultState.hidingToggleCaption, String.class,
  2069. designContext);
  2070. DesignAttributeHandler.writeAttribute("width", attributes,
  2071. getWidth(), defaultState.width, Double.class,
  2072. designContext);
  2073. DesignAttributeHandler.writeAttribute("min-width", attributes,
  2074. getMinimumWidth(), defaultState.minWidth, Double.class,
  2075. designContext);
  2076. DesignAttributeHandler.writeAttribute("max-width", attributes,
  2077. getMaximumWidth(), defaultState.maxWidth, Double.class,
  2078. designContext);
  2079. DesignAttributeHandler.writeAttribute("expand", attributes,
  2080. getExpandRatio(), defaultState.expandRatio, Integer.class,
  2081. designContext);
  2082. }
  2083. /**
  2084. * Reads the design attributes for this column from given element.
  2085. *
  2086. * @since 7.5.0
  2087. * @param design
  2088. * Element to read attributes from
  2089. * @param designContext
  2090. * the design context
  2091. */
  2092. @SuppressWarnings("unchecked")
  2093. protected void readDesign(Element design, DesignContext designContext) {
  2094. Attributes attributes = design.attributes();
  2095. if (design.hasAttr("sortable")) {
  2096. setSortable(DesignAttributeHandler.readAttribute("sortable",
  2097. attributes, boolean.class));
  2098. } else {
  2099. setSortable(false);
  2100. }
  2101. if (design.hasAttr("editable")) {
  2102. /**
  2103. * This is a fake editor just to have something (otherwise
  2104. * "setEditable" throws an exception.
  2105. *
  2106. * Let's use TextField here because we support only Strings as
  2107. * inline data type. It will work incorrectly for other types
  2108. * but we don't support them anyway.
  2109. */
  2110. setEditorComponent((HasValue<V> & Component) new TextField(),
  2111. (item, value) -> {
  2112. // Ignore user value since we don't know the setter
  2113. });
  2114. setEditable(DesignAttributeHandler.readAttribute("editable",
  2115. attributes, boolean.class));
  2116. }
  2117. if (design.hasAttr("resizable")) {
  2118. setResizable(DesignAttributeHandler.readAttribute("resizable",
  2119. attributes, boolean.class));
  2120. }
  2121. if (design.hasAttr("hidable")) {
  2122. setHidable(DesignAttributeHandler.readAttribute("hidable",
  2123. attributes, boolean.class));
  2124. }
  2125. if (design.hasAttr("hidden")) {
  2126. setHidden(DesignAttributeHandler.readAttribute("hidden",
  2127. attributes, boolean.class));
  2128. }
  2129. if (design.hasAttr("hiding-toggle-caption")) {
  2130. setHidingToggleCaption(DesignAttributeHandler.readAttribute(
  2131. "hiding-toggle-caption", attributes, String.class));
  2132. }
  2133. if (design.hasAttr("assistive-caption")) {
  2134. setAssistiveCaption(DesignAttributeHandler.readAttribute(
  2135. "assistive-caption", attributes, String.class));
  2136. }
  2137. // Read size info where necessary.
  2138. if (design.hasAttr("width")) {
  2139. setWidth(DesignAttributeHandler.readAttribute("width",
  2140. attributes, Double.class));
  2141. }
  2142. if (design.hasAttr("min-width")) {
  2143. setMinimumWidth(DesignAttributeHandler
  2144. .readAttribute("min-width", attributes, Double.class));
  2145. }
  2146. if (design.hasAttr("max-width")) {
  2147. setMaximumWidth(DesignAttributeHandler
  2148. .readAttribute("max-width", attributes, Double.class));
  2149. }
  2150. if (design.hasAttr("expand")) {
  2151. if (design.attr("expand").isEmpty()) {
  2152. setExpandRatio(1);
  2153. } else {
  2154. setExpandRatio(DesignAttributeHandler.readAttribute(
  2155. "expand", attributes, Integer.class));
  2156. }
  2157. }
  2158. }
  2159. /**
  2160. * Gets the DataGenerator for this Column.
  2161. *
  2162. * @return data generator
  2163. */
  2164. private DataGenerator<T> getDataGenerator() {
  2165. return dataGenerator;
  2166. }
  2167. }
  2168. private class HeaderImpl extends Header {
  2169. @Override
  2170. protected Grid<T> getGrid() {
  2171. return Grid.this;
  2172. }
  2173. @Override
  2174. protected SectionState getState(boolean markAsDirty) {
  2175. return Grid.this.getState(markAsDirty).header;
  2176. }
  2177. @Override
  2178. protected Column<?, ?> getColumnByInternalId(String internalId) {
  2179. return getGrid().getColumnByInternalId(internalId);
  2180. }
  2181. @Override
  2182. @SuppressWarnings("unchecked")
  2183. protected String getInternalIdForColumn(Column<?, ?> column) {
  2184. return getGrid().getInternalIdForColumn((Column<T, ?>) column);
  2185. }
  2186. };
  2187. private class FooterImpl extends Footer {
  2188. @Override
  2189. protected Grid<T> getGrid() {
  2190. return Grid.this;
  2191. }
  2192. @Override
  2193. protected SectionState getState(boolean markAsDirty) {
  2194. return Grid.this.getState(markAsDirty).footer;
  2195. }
  2196. @Override
  2197. protected Column<?, ?> getColumnByInternalId(String internalId) {
  2198. return getGrid().getColumnByInternalId(internalId);
  2199. }
  2200. @Override
  2201. @SuppressWarnings("unchecked")
  2202. protected String getInternalIdForColumn(Column<?, ?> column) {
  2203. return getGrid().getInternalIdForColumn((Column<T, ?>) column);
  2204. }
  2205. };
  2206. private final Set<Column<T, ?>> columnSet = new LinkedHashSet<>();
  2207. private final Map<String, Column<T, ?>> columnKeys = new HashMap<>();
  2208. private final Map<String, Column<T, ?>> columnIds = new HashMap<>();
  2209. private final List<GridSortOrder<T>> sortOrder = new ArrayList<>();
  2210. private final DetailsManager<T> detailsManager;
  2211. private final Set<Component> extensionComponents = new HashSet<>();
  2212. private StyleGenerator<T> styleGenerator = item -> null;
  2213. private DescriptionGenerator<T> descriptionGenerator;
  2214. private final Header header = new HeaderImpl();
  2215. private final Footer footer = new FooterImpl();
  2216. private int counter = 0;
  2217. private GridSelectionModel<T> selectionModel;
  2218. private Editor<T> editor;
  2219. private PropertySet<T> propertySet;
  2220. private Class<T> beanType = null;
  2221. /**
  2222. * Creates a new grid without support for creating columns based on property
  2223. * names. Use an alternative constructor, such as {@link Grid#Grid(Class)},
  2224. * to create a grid that automatically sets up columns based on the type of
  2225. * presented data.
  2226. *
  2227. * @see #Grid(Class)
  2228. * @see #withPropertySet(PropertySet)
  2229. */
  2230. public Grid() {
  2231. this(new DataCommunicator<>());
  2232. }
  2233. /**
  2234. * Creates a new grid that uses reflection based on the provided bean type
  2235. * to automatically set up an initial set of columns. All columns will be
  2236. * configured using the same {@link Object#toString()} renderer that is used
  2237. * by {@link #addColumn(ValueProvider)}.
  2238. *
  2239. * @param beanType
  2240. * the bean type to use, not <code>null</code>
  2241. * @see #Grid()
  2242. * @see #withPropertySet(PropertySet)
  2243. */
  2244. public Grid(Class<T> beanType) {
  2245. this(beanType, new DataCommunicator<>());
  2246. }
  2247. /**
  2248. * Creates a new grid that uses custom data communicator and provided bean
  2249. * type
  2250. *
  2251. * It uses reflection of the provided bean type to automatically set up an
  2252. * initial set of columns. All columns will be configured using the same
  2253. * {@link Object#toString()} renderer that is used by
  2254. * {@link #addColumn(ValueProvider)}.
  2255. *
  2256. * @param beanType
  2257. * the bean type to use, not <code>null</code>
  2258. * @param dataCommunicator
  2259. * the data communicator to use, not<code>null</code>
  2260. * @since 8.0.7
  2261. */
  2262. protected Grid(Class<T> beanType, DataCommunicator<T> dataCommunicator) {
  2263. this(BeanPropertySet.get(beanType), dataCommunicator);
  2264. this.beanType = beanType;
  2265. }
  2266. /**
  2267. * Creates a new grid with the given data communicator and without support
  2268. * for creating columns based on property names.
  2269. *
  2270. * @param dataCommunicator
  2271. * the custom data communicator to set
  2272. * @see #Grid()
  2273. * @see #Grid(PropertySet, DataCommunicator)
  2274. * @since 8.0.7
  2275. */
  2276. protected Grid(DataCommunicator<T> dataCommunicator) {
  2277. this(new PropertySet<T>() {
  2278. @Override
  2279. public Stream<PropertyDefinition<T, ?>> getProperties() {
  2280. // No columns configured by default
  2281. return Stream.empty();
  2282. }
  2283. @Override
  2284. public Optional<PropertyDefinition<T, ?>> getProperty(String name) {
  2285. throw new IllegalStateException(
  2286. "A Grid created without a bean type class literal or a custom property set"
  2287. + " doesn't support finding properties by name.");
  2288. }
  2289. }, dataCommunicator);
  2290. }
  2291. /**
  2292. * Creates a grid using a custom {@link PropertySet} implementation for
  2293. * configuring the initial columns and resolving property names for
  2294. * {@link #addColumn(String)} and
  2295. * {@link Column#setEditorComponent(HasValue)}.
  2296. *
  2297. * @see #withPropertySet(PropertySet)
  2298. *
  2299. * @param propertySet
  2300. * the property set implementation to use, not <code>null</code>.
  2301. */
  2302. protected Grid(PropertySet<T> propertySet) {
  2303. this(propertySet, new DataCommunicator<>());
  2304. }
  2305. /**
  2306. * Creates a grid using a custom {@link PropertySet} implementation and
  2307. * custom data communicator.
  2308. * <p>
  2309. * Property set is used for configuring the initial columns and resolving
  2310. * property names for {@link #addColumn(String)} and
  2311. * {@link Column#setEditorComponent(HasValue)}.
  2312. *
  2313. * @see #withPropertySet(PropertySet)
  2314. *
  2315. * @param propertySet
  2316. * the property set implementation to use, not <code>null</code>.
  2317. * @param dataCommunicator
  2318. * the data communicator to use, not<code>null</code>
  2319. * @since 8.0.7
  2320. */
  2321. protected Grid(PropertySet<T> propertySet,
  2322. DataCommunicator<T> dataCommunicator) {
  2323. super(dataCommunicator);
  2324. registerRpc(new GridServerRpcImpl());
  2325. setDefaultHeaderRow(appendHeaderRow());
  2326. setSelectionModel(new SingleSelectionModelImpl<>());
  2327. detailsManager = new DetailsManager<>();
  2328. addExtension(detailsManager);
  2329. addDataGenerator(detailsManager);
  2330. addDataGenerator((item, json) -> {
  2331. String styleName = styleGenerator.apply(item);
  2332. if (styleName != null && !styleName.isEmpty()) {
  2333. json.put(GridState.JSONKEY_ROWSTYLE, styleName);
  2334. }
  2335. if (descriptionGenerator != null) {
  2336. String description = descriptionGenerator.apply(item);
  2337. if (description != null && !description.isEmpty()) {
  2338. json.put(GridState.JSONKEY_ROWDESCRIPTION, description);
  2339. }
  2340. }
  2341. });
  2342. setPropertySet(propertySet);
  2343. // Automatically add columns for all available properties
  2344. propertySet.getProperties().map(PropertyDefinition::getName)
  2345. .forEach(this::addColumn);
  2346. }
  2347. @Override
  2348. public void beforeClientResponse(boolean initial) {
  2349. super.beforeClientResponse(initial);
  2350. if (initial && editor.isOpen()) {
  2351. // Re-attaching grid. Any old editor should be closed.
  2352. editor.cancel();
  2353. }
  2354. }
  2355. /**
  2356. * Sets the property set to use for this grid. Does not create or update
  2357. * columns in any way but will delete and re-create the editor.
  2358. * <p>
  2359. * This is only meant to be called from constructors and readDesign, at a
  2360. * stage where it does not matter if you throw away the editor.
  2361. *
  2362. * @param propertySet
  2363. * the property set to use
  2364. *
  2365. * @since 8.0.3
  2366. */
  2367. protected void setPropertySet(PropertySet<T> propertySet) {
  2368. Objects.requireNonNull(propertySet, "propertySet cannot be null");
  2369. this.propertySet = propertySet;
  2370. if (editor instanceof Extension) {
  2371. removeExtension((Extension) editor);
  2372. }
  2373. editor = createEditor();
  2374. if (editor instanceof Extension) {
  2375. addExtension((Extension) editor);
  2376. }
  2377. }
  2378. /**
  2379. * Returns the property set used by this grid.
  2380. *
  2381. * @return propertySet the property set to return
  2382. * @since 8.4
  2383. */
  2384. protected PropertySet<T> getPropertySet() {
  2385. return propertySet;
  2386. }
  2387. /**
  2388. * Creates a grid using a custom {@link PropertySet} implementation for
  2389. * creating a default set of columns and for resolving property names with
  2390. * {@link #addColumn(String)} and
  2391. * {@link Column#setEditorComponent(HasValue)}.
  2392. * <p>
  2393. * This functionality is provided as static method instead of as a public
  2394. * constructor in order to make it possible to use a custom property set
  2395. * without creating a subclass while still leaving the public constructors
  2396. * focused on the common use cases.
  2397. *
  2398. * @see Grid#Grid()
  2399. * @see Grid#Grid(Class)
  2400. *
  2401. * @param propertySet
  2402. * the property set implementation to use, not <code>null</code>.
  2403. * @return a new grid using the provided property set, not <code>null</code>
  2404. */
  2405. public static <BEAN> Grid<BEAN> withPropertySet(
  2406. PropertySet<BEAN> propertySet) {
  2407. return new Grid<>(propertySet);
  2408. }
  2409. /**
  2410. * Creates a new {@code Grid} using the given caption.
  2411. *
  2412. * @param caption
  2413. * the caption of the grid
  2414. */
  2415. public Grid(String caption) {
  2416. this();
  2417. setCaption(caption);
  2418. }
  2419. /**
  2420. * Creates a new {@code Grid} using the given caption and
  2421. * {@code DataProvider}.
  2422. *
  2423. * @param caption
  2424. * the caption of the grid
  2425. * @param dataProvider
  2426. * the data provider, not {@code null}
  2427. */
  2428. public Grid(String caption, DataProvider<T, ?> dataProvider) {
  2429. this(caption);
  2430. setDataProvider(dataProvider);
  2431. }
  2432. /**
  2433. * Creates a new {@code Grid} using the given {@code DataProvider}.
  2434. *
  2435. * @param dataProvider
  2436. * the data provider, not {@code null}
  2437. */
  2438. public Grid(DataProvider<T, ?> dataProvider) {
  2439. this();
  2440. setDataProvider(dataProvider);
  2441. }
  2442. /**
  2443. * Creates a new {@code Grid} using the given caption and collection of
  2444. * items.
  2445. *
  2446. * @param caption
  2447. * the caption of the grid
  2448. * @param items
  2449. * the data items to use, not {@çode null}
  2450. */
  2451. public Grid(String caption, Collection<T> items) {
  2452. this(caption, DataProvider.ofCollection(items));
  2453. }
  2454. /**
  2455. * Gets the bean type used by this grid.
  2456. * <p>
  2457. * The bean type is used to automatically set up a column added using a
  2458. * property name.
  2459. *
  2460. * @return the used bean type or <code>null</code> if no bean type has been
  2461. * defined
  2462. *
  2463. * @since 8.0.3
  2464. */
  2465. public Class<T> getBeanType() {
  2466. return beanType;
  2467. }
  2468. public <V> void fireColumnVisibilityChangeEvent(Column<T, V> column,
  2469. boolean hidden, boolean userOriginated) {
  2470. fireEvent(new ColumnVisibilityChangeEvent(this, column, hidden,
  2471. userOriginated));
  2472. }
  2473. /**
  2474. * Adds a new column with the given property name. The column will use a
  2475. * {@link TextRenderer}. The value is converted to a String using
  2476. * {@link Object#toString()}. The property name will be used as the
  2477. * {@link Column#getId() column id} and the {@link Column#getCaption()
  2478. * column caption} will be set based on the property definition.
  2479. * <p>
  2480. * This method can only be used for a <code>Grid</code> created using
  2481. * {@link Grid#Grid(Class)} or {@link #withPropertySet(PropertySet)}.
  2482. * <p>
  2483. * You can add columns for nested properties with dot notation, eg.
  2484. * <code>"property.nestedProperty"</code>
  2485. *
  2486. * @param propertyName
  2487. * the property name of the new column, not <code>null</code>
  2488. * @return the newly added column, not <code>null</code>
  2489. */
  2490. public Column<T, ?> addColumn(String propertyName) {
  2491. return addColumn(propertyName, new TextRenderer());
  2492. }
  2493. /**
  2494. * Adds a new column with the given property name and renderer. The property
  2495. * name will be used as the {@link Column#getId() column id} and the
  2496. * {@link Column#getCaption() column caption} will be set based on the
  2497. * property definition.
  2498. * <p>
  2499. * This method can only be used for a <code>Grid</code> created using
  2500. * {@link Grid#Grid(Class)} or {@link #withPropertySet(PropertySet)}.
  2501. * <p>
  2502. * You can add columns for nested properties with dot notation, eg.
  2503. * <code>"property.nestedProperty"</code>
  2504. *
  2505. *
  2506. * @param propertyName
  2507. * the property name of the new column, not <code>null</code>
  2508. * @param renderer
  2509. * the renderer to use, not <code>null</code>
  2510. * @return the newly added column, not <code>null</code>
  2511. */
  2512. public Column<T, ?> addColumn(String propertyName,
  2513. AbstractRenderer<? super T, ?> renderer) {
  2514. Objects.requireNonNull(propertyName, "Property name cannot be null");
  2515. Objects.requireNonNull(renderer, "Renderer cannot be null");
  2516. if (getColumn(propertyName) != null) {
  2517. throw new IllegalStateException(
  2518. "There is already a column for " + propertyName);
  2519. }
  2520. PropertyDefinition<T, ?> definition = propertySet
  2521. .getProperty(propertyName)
  2522. .orElseThrow(() -> new IllegalArgumentException(
  2523. "Could not resolve property name " + propertyName
  2524. + " from " + propertySet));
  2525. if (!renderer.getPresentationType()
  2526. .isAssignableFrom(definition.getType())) {
  2527. throw new IllegalArgumentException(
  2528. renderer + " cannot be used with a property of type "
  2529. + definition.getType().getName());
  2530. }
  2531. @SuppressWarnings({ "unchecked", "rawtypes" })
  2532. Column<T, ?> column = addColumn(definition.getGetter(),
  2533. (AbstractRenderer) renderer).setId(definition.getName())
  2534. .setCaption(definition.getCaption());
  2535. return column;
  2536. }
  2537. /**
  2538. * Adds a new column with the given property name and renderer. The property
  2539. * name will be used as the {@link Column#getId() column id} and the
  2540. * {@link Column#getCaption() column caption} will be set based on the
  2541. * property definition.
  2542. * <p>
  2543. * This method can only be used for a <code>Grid</code> created using
  2544. * {@link Grid#Grid(Class)} or {@link #withPropertySet(PropertySet)}.
  2545. * <p>
  2546. * You can add columns for nested properties with dot notation, eg.
  2547. * <code>"property.nestedProperty"</code>
  2548. *
  2549. * @param propertyName
  2550. * the property name of the new column, not <code>null</code>
  2551. * @param renderer
  2552. * the renderer to use, not <code>null</code>
  2553. * @param nestedNullBehavior
  2554. * the behavior when
  2555. * @return the newly added column, not <code>null</code>
  2556. *
  2557. * @since 8.8
  2558. */
  2559. public Column<T, ?> addColumn(String propertyName,
  2560. AbstractRenderer<? super T, ?> renderer,
  2561. Column.NestedNullBehavior nestedNullBehavior) {
  2562. Objects.requireNonNull(propertyName, "Property name cannot be null");
  2563. Objects.requireNonNull(renderer, "Renderer cannot be null");
  2564. if (getColumn(propertyName) != null) {
  2565. throw new IllegalStateException(
  2566. "There is already a column for " + propertyName);
  2567. }
  2568. PropertyDefinition<T, ?> definition = propertySet
  2569. .getProperty(propertyName)
  2570. .orElseThrow(() -> new IllegalArgumentException(
  2571. "Could not resolve property name " + propertyName
  2572. + " from " + propertySet));
  2573. if (!renderer.getPresentationType()
  2574. .isAssignableFrom(definition.getType())) {
  2575. throw new IllegalArgumentException(
  2576. renderer + " cannot be used with a property of type "
  2577. + definition.getType().getName());
  2578. }
  2579. @SuppressWarnings({ "unchecked", "rawtypes" })
  2580. Column<T, ?> column = createColumn(definition.getGetter(),
  2581. ValueProvider.identity(), (AbstractRenderer) renderer,
  2582. nestedNullBehavior);
  2583. String generatedIdentifier = getGeneratedIdentifier();
  2584. addColumn(generatedIdentifier, column);
  2585. column.setId(definition.getName()).setCaption(definition.getCaption());
  2586. return column;
  2587. }
  2588. /**
  2589. * Adds a new text column to this {@link Grid} with a value provider. The
  2590. * column will use a {@link TextRenderer}. The value is converted to a
  2591. * String using {@link Object#toString()}. In-memory sorting will use the
  2592. * natural ordering of elements if they are mutually comparable and
  2593. * otherwise fall back to comparing the string representations of the
  2594. * values.
  2595. *
  2596. * @param valueProvider
  2597. * the value provider
  2598. *
  2599. * @return the new column
  2600. */
  2601. public <V> Column<T, V> addColumn(ValueProvider<T, V> valueProvider) {
  2602. return addColumn(valueProvider, new TextRenderer());
  2603. }
  2604. /**
  2605. * Adds a new column to this {@link Grid} with typed renderer and value
  2606. * provider.
  2607. *
  2608. * @param valueProvider
  2609. * the value provider
  2610. * @param renderer
  2611. * the column value renderer
  2612. * @param <V>
  2613. * the column value type
  2614. *
  2615. * @return the new column
  2616. *
  2617. * @see AbstractRenderer
  2618. */
  2619. public <V> Column<T, V> addColumn(ValueProvider<T, V> valueProvider,
  2620. AbstractRenderer<? super T, ? super V> renderer) {
  2621. return addColumn(valueProvider, ValueProvider.identity(), renderer);
  2622. }
  2623. /**
  2624. * Adds a new column to this {@link Grid} with value provider and
  2625. * presentation provider.
  2626. * <p>
  2627. * <strong>Note:</strong> The presentation type for this method is set to be
  2628. * String. To use any custom renderer with the presentation provider, use
  2629. * {@link #addColumn(ValueProvider, ValueProvider, AbstractRenderer)}.
  2630. *
  2631. * @param valueProvider
  2632. * the value provider
  2633. * @param presentationProvider
  2634. * the value presentation provider
  2635. * @param <V>
  2636. * the column value type
  2637. *
  2638. * @see #addColumn(ValueProvider, ValueProvider, AbstractRenderer)
  2639. *
  2640. * @return the new column
  2641. * @since 8.1
  2642. */
  2643. public <V> Column<T, V> addColumn(ValueProvider<T, V> valueProvider,
  2644. ValueProvider<V, String> presentationProvider) {
  2645. return addColumn(valueProvider, presentationProvider,
  2646. new TextRenderer());
  2647. }
  2648. /**
  2649. * Adds a new column to this {@link Grid} with value provider, presentation
  2650. * provider and typed renderer.
  2651. *
  2652. * <p>
  2653. * The presentation provider is a method that takes the value from the value
  2654. * provider, and maps that to a value that the renderer accepts. This
  2655. * feature can be used for storing a complex value in a column for editing,
  2656. * but providing a simplified presentation for the user when not editing.
  2657. *
  2658. * @param valueProvider
  2659. * the value provider
  2660. * @param presentationProvider
  2661. * the value presentation provider
  2662. * @param renderer
  2663. * the column value renderer
  2664. * @param <V>
  2665. * the column value type
  2666. * @param <P>
  2667. * the column presentation type
  2668. *
  2669. * @return the new column
  2670. *
  2671. * @see AbstractRenderer
  2672. * @since 8.1
  2673. */
  2674. public <V, P> Column<T, V> addColumn(ValueProvider<T, V> valueProvider,
  2675. ValueProvider<V, P> presentationProvider,
  2676. AbstractRenderer<? super T, ? super P> renderer) {
  2677. String generatedIdentifier = getGeneratedIdentifier();
  2678. Column<T, V> column = createColumn(valueProvider, presentationProvider,
  2679. renderer);
  2680. addColumn(generatedIdentifier, column);
  2681. return column;
  2682. }
  2683. /**
  2684. * Adds a column that shows components.
  2685. * <p>
  2686. * This is a shorthand for {@link #addColumn()} with a
  2687. * {@link ComponentRenderer}.
  2688. *
  2689. * @param componentProvider
  2690. * a value provider that will return a component for the given
  2691. * item
  2692. * @return the new column
  2693. * @param <V>
  2694. * the column value type, extends component
  2695. * @since 8.1
  2696. */
  2697. public <V extends Component> Column<T, V> addComponentColumn(
  2698. ValueProvider<T, V> componentProvider) {
  2699. return addColumn(componentProvider, new ComponentRenderer());
  2700. }
  2701. /**
  2702. * Creates a column instance from a value provider, presentation provider
  2703. * and a renderer.
  2704. *
  2705. * @param valueProvider
  2706. * the value provider
  2707. * @param presentationProvider
  2708. * the presentation provider
  2709. * @param renderer
  2710. * the renderer
  2711. * @return a new column instance
  2712. * @param <V>
  2713. * the column value type
  2714. * @param <P>
  2715. * the column presentation type
  2716. *
  2717. * @since 8.1
  2718. */
  2719. protected <V, P> Column<T, V> createColumn(
  2720. ValueProvider<T, V> valueProvider,
  2721. ValueProvider<V, P> presentationProvider,
  2722. AbstractRenderer<? super T, ? super P> renderer) {
  2723. return new Column<>(valueProvider, presentationProvider, renderer);
  2724. }
  2725. /**
  2726. * Creates a column instance from a value provider, presentation provider
  2727. * and a renderer.
  2728. *
  2729. * @param valueProvider
  2730. * the value provider
  2731. * @param presentationProvider
  2732. * the presentation provider
  2733. * @param renderer
  2734. * the renderer
  2735. * @param nestedNullBehavior
  2736. * the behavior when facing nested <code>null</code> values
  2737. * @return a new column instance
  2738. * @param <V>
  2739. * the column value type
  2740. * @param <P>
  2741. * the column presentation type
  2742. *
  2743. * @since 8.8
  2744. */
  2745. private <V, P> Column<T, V> createColumn(ValueProvider<T, V> valueProvider,
  2746. ValueProvider<V, P> presentationProvider,
  2747. AbstractRenderer<? super T, ? super P> renderer,
  2748. Column.NestedNullBehavior nestedNullBehavior) {
  2749. return new Column<>(valueProvider, presentationProvider, renderer,
  2750. nestedNullBehavior);
  2751. }
  2752. private void addColumn(String identifier, Column<T, ?> column) {
  2753. if (getColumns().contains(column)) {
  2754. return;
  2755. }
  2756. column.extend(this);
  2757. columnSet.add(column);
  2758. columnKeys.put(identifier, column);
  2759. column.setInternalId(identifier);
  2760. addDataGenerator(column.getDataGenerator());
  2761. getState().columnOrder.add(identifier);
  2762. getHeader().addColumn(identifier);
  2763. getFooter().addColumn(identifier);
  2764. if (getDefaultHeaderRow() != null) {
  2765. getDefaultHeaderRow().getCell(column).setText(column.getCaption());
  2766. }
  2767. column.updateSortable();
  2768. }
  2769. /**
  2770. * Removes the given column from this {@link Grid}.
  2771. *
  2772. * Note: If you have Editor with binding in this Grid to this property, you need to remove that
  2773. * using removeBinding method provided by Binder.
  2774. *
  2775. * @param column
  2776. * the column to remove
  2777. *
  2778. * @throws IllegalArgumentException
  2779. * if the column is not a valid one
  2780. */
  2781. public void removeColumn(Column<T, ?> column) {
  2782. if (columnSet.remove(column)) {
  2783. String columnId = column.getInternalId();
  2784. int displayIndex = getState(false).columnOrder.indexOf(columnId);
  2785. assert displayIndex != -1 : "Tried to remove a column which is not included in columnOrder. This should not be possible as all columns should be in columnOrder.";
  2786. columnKeys.remove(columnId);
  2787. columnIds.remove(column.getId());
  2788. column.remove();
  2789. removeDataGenerator(column.getDataGenerator());
  2790. getHeader().removeColumn(columnId);
  2791. getFooter().removeColumn(columnId);
  2792. getState(true).columnOrder.remove(columnId);
  2793. // Remove column from sorted columns.
  2794. List<GridSortOrder<T>> filteredSortOrder = sortOrder.stream()
  2795. .filter(order -> !order.getSorted().equals(column))
  2796. .collect(Collectors.toList());
  2797. if (filteredSortOrder.size() < sortOrder.size()) {
  2798. setSortOrder(filteredSortOrder);
  2799. }
  2800. if (displayIndex < getFrozenColumnCount()) {
  2801. setFrozenColumnCount(getFrozenColumnCount() - 1);
  2802. }
  2803. } else {
  2804. throw new IllegalArgumentException("Column with id "
  2805. + column.getId() + " cannot be removed from the grid");
  2806. }
  2807. }
  2808. /**
  2809. * Removes the column with the given column id.
  2810. *
  2811. * @see #removeColumn(Column)
  2812. * @see Column#setId(String)
  2813. *
  2814. * @param columnId
  2815. * the id of the column to remove, not <code>null</code>
  2816. */
  2817. public void removeColumn(String columnId) {
  2818. removeColumn(getColumnOrThrow(columnId));
  2819. }
  2820. /**
  2821. * Removes all columns from this Grid.
  2822. *
  2823. * @since 8.0.2
  2824. */
  2825. public void removeAllColumns() {
  2826. for (Column<T, ?> column : getColumns()) {
  2827. removeColumn(column);
  2828. }
  2829. }
  2830. /**
  2831. * Requests that the column widths should be recalculated.
  2832. * <p>
  2833. * In most cases Grid will know when column widths need to be recalculated
  2834. * but this method can be used to force recalculation in situations when
  2835. * grid does not recalculate automatically.
  2836. *
  2837. * @since 8.1.1
  2838. */
  2839. public void recalculateColumnWidths() {
  2840. getRpcProxy(GridClientRpc.class).recalculateColumnWidths();
  2841. }
  2842. /**
  2843. * Sets the details component generator.
  2844. *
  2845. * @param generator
  2846. * the generator for details components
  2847. */
  2848. public void setDetailsGenerator(DetailsGenerator<T> generator) {
  2849. this.detailsManager.setDetailsGenerator(generator);
  2850. }
  2851. /**
  2852. * Sets the visibility of details component for given item.
  2853. *
  2854. * @param item
  2855. * the item to show details for
  2856. * @param visible
  2857. * {@code true} if details component should be visible;
  2858. * {@code false} if it should be hidden
  2859. */
  2860. public void setDetailsVisible(T item, boolean visible) {
  2861. detailsManager.setDetailsVisible(item, visible);
  2862. }
  2863. /**
  2864. * Returns the visibility of details component for given item.
  2865. *
  2866. * @param item
  2867. * the item to show details for
  2868. *
  2869. * @return {@code true} if details component should be visible;
  2870. * {@code false} if it should be hidden
  2871. */
  2872. public boolean isDetailsVisible(T item) {
  2873. return detailsManager.isDetailsVisible(item);
  2874. }
  2875. /**
  2876. * Gets an unmodifiable collection of all columns currently in this
  2877. * {@link Grid}.
  2878. *
  2879. * @return unmodifiable collection of columns
  2880. */
  2881. public List<Column<T, ?>> getColumns() {
  2882. return Collections.unmodifiableList(getState(false).columnOrder.stream()
  2883. .map(columnKeys::get).collect(Collectors.toList()));
  2884. }
  2885. /**
  2886. * Gets a {@link Column} of this grid by its identifying string.
  2887. *
  2888. * When you use the Grid constructor with bean class, the columns are
  2889. * initialised with columnId being the property name.
  2890. *
  2891. * @see Column#setId(String)
  2892. *
  2893. * @param columnId
  2894. * the identifier of the column to get
  2895. * @return the column corresponding to the given column identifier, or
  2896. * <code>null</code> if there is no such column
  2897. */
  2898. public Column<T, ?> getColumn(String columnId) {
  2899. return columnIds.get(columnId);
  2900. }
  2901. private Column<T, ?> getColumnOrThrow(String columnId) {
  2902. Objects.requireNonNull(columnId, "Column id cannot be null");
  2903. Column<T, ?> column = getColumn(columnId);
  2904. if (column == null) {
  2905. throw new IllegalStateException(
  2906. "There is no column with the id " + columnId);
  2907. }
  2908. return column;
  2909. }
  2910. /**
  2911. * {@inheritDoc}
  2912. * <p>
  2913. * Note that the order of the returned components it not specified.
  2914. */
  2915. @Override
  2916. public Iterator<Component> iterator() {
  2917. Set<Component> componentSet = new LinkedHashSet<>(extensionComponents);
  2918. Header header = getHeader();
  2919. for (int i = 0; i < header.getRowCount(); ++i) {
  2920. HeaderRow row = header.getRow(i);
  2921. componentSet.addAll(row.getComponents());
  2922. }
  2923. Footer footer = getFooter();
  2924. for (int i = 0; i < footer.getRowCount(); ++i) {
  2925. FooterRow row = footer.getRow(i);
  2926. componentSet.addAll(row.getComponents());
  2927. }
  2928. return Collections.unmodifiableSet(componentSet).iterator();
  2929. }
  2930. /**
  2931. * Sets the number of frozen columns in this grid. Setting the count to 0
  2932. * means that no data columns will be frozen, but the built-in selection
  2933. * checkbox column will still be frozen if it's in use. Setting the count to
  2934. * -1 will also disable the selection column.
  2935. * <p>
  2936. * <em>NOTE:</em> this count includes {@link Column#isHidden() hidden
  2937. * columns} in the count.
  2938. * <p>
  2939. * The default value is 0.
  2940. *
  2941. * @param numberOfColumns
  2942. * the number of columns that should be frozen
  2943. *
  2944. * @throws IllegalArgumentException
  2945. * if the column count is less than -1 or greater than the
  2946. * number of visible columns
  2947. */
  2948. public void setFrozenColumnCount(int numberOfColumns) {
  2949. if (numberOfColumns < -1 || numberOfColumns > columnSet.size()) {
  2950. throw new IllegalArgumentException(
  2951. "count must be between -1 and the current number of columns ("
  2952. + columnSet.size() + "): " + numberOfColumns);
  2953. }
  2954. int currentFrozenColumnState = getState(false).frozenColumnCount;
  2955. /*
  2956. * we remove the current value from the state so that setting frozen
  2957. * columns will always happen after this call. This is so that the value
  2958. * will be set also in the widget even if it happens to seem to be the
  2959. * same as this current value we're setting.
  2960. */
  2961. if (currentFrozenColumnState != numberOfColumns) {
  2962. final String diffStateKey = "frozenColumnCount";
  2963. UI ui = getUI();
  2964. if (ui != null) {
  2965. JsonObject diffState = ui.getConnectorTracker()
  2966. .getDiffState(Grid.this);
  2967. // if diffState is not present, there's nothing for us to clean
  2968. if (diffState != null) {
  2969. diffState.remove(diffStateKey);
  2970. }
  2971. }
  2972. }
  2973. getState().frozenColumnCount = numberOfColumns;
  2974. }
  2975. /**
  2976. * Gets the number of frozen columns in this grid. 0 means that no data
  2977. * columns will be frozen, but the built-in selection checkbox column will
  2978. * still be frozen if it's in use. -1 means that not even the selection
  2979. * column is frozen.
  2980. * <p>
  2981. * <em>NOTE:</em> this count includes {@link Column#isHidden() hidden
  2982. * columns} in the count.
  2983. *
  2984. * @see #setFrozenColumnCount(int)
  2985. *
  2986. * @return the number of frozen columns
  2987. */
  2988. public int getFrozenColumnCount() {
  2989. return getState(false).frozenColumnCount;
  2990. }
  2991. /**
  2992. * Sets the number of rows that should be visible in Grid's body. This
  2993. * method will set the height mode to be {@link HeightMode#ROW}.
  2994. *
  2995. * @param rows
  2996. * The height in terms of number of rows displayed in Grid's
  2997. * body. If Grid doesn't contain enough rows, white space is
  2998. * displayed instead.
  2999. * @throws IllegalArgumentException
  3000. * if {@code rows} is zero or less
  3001. * @throws IllegalArgumentException
  3002. * if {@code rows} is {@link Double#isInfinite(double) infinite}
  3003. * @throws IllegalArgumentException
  3004. * if {@code rows} is {@link Double#isNaN(double) NaN}
  3005. */
  3006. public void setHeightByRows(double rows) {
  3007. if (rows <= 0.0d) {
  3008. throw new IllegalArgumentException(
  3009. "More than zero rows must be shown.");
  3010. }
  3011. if (Double.isInfinite(rows)) {
  3012. throw new IllegalArgumentException(
  3013. "Grid doesn't support infinite heights");
  3014. }
  3015. if (Double.isNaN(rows)) {
  3016. throw new IllegalArgumentException("NaN is not a valid row count");
  3017. }
  3018. getState().heightMode = HeightMode.ROW;
  3019. getState().heightByRows = rows;
  3020. }
  3021. /**
  3022. * Gets the amount of rows in Grid's body that are shown, while
  3023. * {@link #getHeightMode()} is {@link HeightMode#ROW}.
  3024. *
  3025. * @return the amount of rows that are being shown in Grid's body
  3026. * @see #setHeightByRows(double)
  3027. */
  3028. public double getHeightByRows() {
  3029. return getState(false).heightByRows;
  3030. }
  3031. /**
  3032. * {@inheritDoc}
  3033. * <p>
  3034. * <em>Note:</em> This method will set the height mode to be
  3035. * {@link HeightMode#CSS}.
  3036. *
  3037. * @see #setHeightMode(HeightMode)
  3038. */
  3039. @Override
  3040. public void setHeight(float height, Unit unit) {
  3041. getState().heightMode = HeightMode.CSS;
  3042. super.setHeight(height, unit);
  3043. }
  3044. /**
  3045. * Defines the mode in which the Grid widget's height is calculated.
  3046. * <p>
  3047. * If {@link HeightMode#CSS} is given, Grid will respect the values given
  3048. * via a {@code setHeight}-method, and behave as a traditional Component.
  3049. * <p>
  3050. * If {@link HeightMode#ROW} is given, Grid will make sure that the body
  3051. * will display as many rows as {@link #getHeightByRows()} defines.
  3052. * <em>Note:</em> If headers/footers are inserted or removed, the widget
  3053. * will resize itself to still display the required amount of rows in its
  3054. * body. It also takes the horizontal scrollbar into account.
  3055. *
  3056. * @param heightMode
  3057. * the mode in to which Grid should be set
  3058. */
  3059. public void setHeightMode(HeightMode heightMode) {
  3060. /**
  3061. * This method is a workaround for the fact that Vaadin re-applies
  3062. * widget dimensions (height/width) on each state change event. The
  3063. * original design was to have setHeight and setHeightByRow be equals,
  3064. * and whichever was called the latest was considered in effect.
  3065. *
  3066. * But, because of Vaadin always calling setHeight on the widget, this
  3067. * approach doesn't work.
  3068. */
  3069. getState().heightMode = heightMode;
  3070. }
  3071. /**
  3072. * Returns the current {@link HeightMode} the Grid is in.
  3073. * <p>
  3074. * Defaults to {@link HeightMode#CSS}.
  3075. *
  3076. * @return the current HeightMode
  3077. */
  3078. public HeightMode getHeightMode() {
  3079. return getState(false).heightMode;
  3080. }
  3081. /**
  3082. * Sets the height of body, header and footer rows. If -1 (default), the row
  3083. * height is calculated based on the theme for an empty row before the Grid
  3084. * is displayed.
  3085. * <p>
  3086. * Note that all header, body and footer rows get the same height if
  3087. * explicitly set. In automatic mode, each section is calculated separately
  3088. * based on an empty row of that type.
  3089. *
  3090. * @see #setBodyRowHeight(double)
  3091. * @see #setHeaderRowHeight(double)
  3092. * @see #setFooterRowHeight(double)
  3093. *
  3094. * @param rowHeight
  3095. * The height of a row in pixels or -1 for automatic calculation
  3096. */
  3097. public void setRowHeight(double rowHeight) {
  3098. setBodyRowHeight(rowHeight);
  3099. setHeaderRowHeight(rowHeight);
  3100. setFooterRowHeight(rowHeight);
  3101. }
  3102. /**
  3103. * Sets the height of a body row. If -1 (default), the row height is
  3104. * calculated based on the theme for an empty row before the Grid is
  3105. * displayed.
  3106. *
  3107. * @param rowHeight
  3108. * The height of a row in pixels or -1 for automatic calculation
  3109. * @since 8.2
  3110. */
  3111. public void setBodyRowHeight(double rowHeight) {
  3112. getState().bodyRowHeight = rowHeight;
  3113. }
  3114. /**
  3115. * Sets the height of a header row. If -1 (default), the row height is
  3116. * calculated based on the theme for an empty row before the Grid is
  3117. * displayed.
  3118. *
  3119. * @param rowHeight
  3120. * The height of a row in pixels or -1 for automatic calculation
  3121. * @since 8.2
  3122. */
  3123. public void setHeaderRowHeight(double rowHeight) {
  3124. getState().headerRowHeight = rowHeight;
  3125. }
  3126. /**
  3127. * Sets the height of a footer row. If -1 (default), the row height is
  3128. * calculated based on the theme for an empty row before the Grid is
  3129. * displayed.
  3130. *
  3131. * @param rowHeight
  3132. * The height of a row in pixels or -1 for automatic calculation
  3133. * @since 8.2
  3134. */
  3135. public void setFooterRowHeight(double rowHeight) {
  3136. getState().footerRowHeight = rowHeight;
  3137. }
  3138. /**
  3139. * Returns the current body row height.-1 if row height is in automatic
  3140. * calculation mode.
  3141. *
  3142. * @see #getBodyRowHeight()
  3143. * @see #getHeaderRowHeight()
  3144. * @see #getFooterRowHeight()
  3145. *
  3146. * @return body row height
  3147. * @deprecated replaced by three separate row height controls
  3148. */
  3149. @Deprecated
  3150. public double getRowHeight() {
  3151. return getBodyRowHeight();
  3152. }
  3153. /**
  3154. * Returns the current body row height. -1 if row height is in automatic
  3155. * calculation mode.
  3156. *
  3157. * @return body row height
  3158. * @since 8.2
  3159. */
  3160. public double getBodyRowHeight() {
  3161. return getState(false).bodyRowHeight;
  3162. }
  3163. /**
  3164. * Returns the current header row height. -1 if row height is in automatic
  3165. * calculation mode.
  3166. *
  3167. * @return header row height
  3168. * @since 8.2
  3169. */
  3170. public double getHeaderRowHeight() {
  3171. return getState(false).headerRowHeight;
  3172. }
  3173. /**
  3174. * Returns the current footer row height. -1 if row height is in automatic
  3175. * calculation mode.
  3176. *
  3177. * @return footer row height
  3178. * @since 8.2
  3179. */
  3180. public double getFooterRowHeight() {
  3181. return getState(false).footerRowHeight;
  3182. }
  3183. /**
  3184. * Sets the style generator that is used for generating class names for rows
  3185. * in this grid. Returning null from the generator results in no custom
  3186. * style name being set.
  3187. *
  3188. * Note: The style generator is applied only to the body cells, not to the
  3189. * Editor.
  3190. *
  3191. * @see StyleGenerator
  3192. *
  3193. * @param styleGenerator
  3194. * the row style generator to set, not null
  3195. * @throws NullPointerException
  3196. * if {@code styleGenerator} is {@code null}
  3197. */
  3198. public void setStyleGenerator(StyleGenerator<T> styleGenerator) {
  3199. Objects.requireNonNull(styleGenerator,
  3200. "Style generator must not be null");
  3201. this.styleGenerator = styleGenerator;
  3202. getDataCommunicator().reset();
  3203. }
  3204. /**
  3205. * Gets the style generator that is used for generating class names for
  3206. * rows.
  3207. *
  3208. * @see StyleGenerator
  3209. *
  3210. * @return the row style generator
  3211. */
  3212. public StyleGenerator<T> getStyleGenerator() {
  3213. return styleGenerator;
  3214. }
  3215. /**
  3216. * Sets the description generator that is used for generating descriptions
  3217. * for rows. This method uses the {@link ContentMode#PREFORMATTED} content
  3218. * mode.
  3219. *
  3220. * @see #setDescriptionGenerator(DescriptionGenerator, ContentMode)
  3221. *
  3222. * @param descriptionGenerator
  3223. * the row description generator to set, or <code>null</code> to
  3224. * remove a previously set generator
  3225. */
  3226. public void setDescriptionGenerator(
  3227. DescriptionGenerator<T> descriptionGenerator) {
  3228. setDescriptionGenerator(descriptionGenerator, ContentMode.PREFORMATTED);
  3229. }
  3230. /**
  3231. * Sets the description generator that is used for generating descriptions
  3232. * for rows. This method uses the given content mode.
  3233. *
  3234. * @see #setDescriptionGenerator(DescriptionGenerator)
  3235. *
  3236. * @param descriptionGenerator
  3237. * the row description generator to set, or {@code null} to
  3238. * remove a previously set generator
  3239. * @param contentMode
  3240. * the content mode for row tooltips
  3241. *
  3242. * @since 8.2
  3243. */
  3244. public void setDescriptionGenerator(
  3245. DescriptionGenerator<T> descriptionGenerator,
  3246. ContentMode contentMode) {
  3247. Objects.requireNonNull(contentMode, "contentMode cannot be null");
  3248. this.descriptionGenerator = descriptionGenerator;
  3249. getState().rowDescriptionContentMode = contentMode;
  3250. getDataCommunicator().reset();
  3251. }
  3252. /**
  3253. * Gets the description generator that is used for generating descriptions
  3254. * for rows.
  3255. *
  3256. * @return the row description generator, or <code>null</code> if no
  3257. * generator is set
  3258. */
  3259. public DescriptionGenerator<T> getDescriptionGenerator() {
  3260. return descriptionGenerator;
  3261. }
  3262. //
  3263. // HEADER AND FOOTER
  3264. //
  3265. /**
  3266. * Returns the header row at the given index.
  3267. *
  3268. * @param index
  3269. * the index of the row, where the topmost row has index zero
  3270. * @return the header row at the index
  3271. * @throws IndexOutOfBoundsException
  3272. * if {@code rowIndex < 0 || rowIndex >= getHeaderRowCount()}
  3273. */
  3274. public HeaderRow getHeaderRow(int index) {
  3275. return getHeader().getRow(index);
  3276. }
  3277. /**
  3278. * Gets the number of rows in the header section.
  3279. *
  3280. * @return the number of header rows
  3281. */
  3282. public int getHeaderRowCount() {
  3283. return header.getRowCount();
  3284. }
  3285. /**
  3286. * Inserts a new row at the given position to the header section. Shifts the
  3287. * row currently at that position and any subsequent rows down (adds one to
  3288. * their indices). Inserting at {@link #getHeaderRowCount()} appends the row
  3289. * at the bottom of the header.
  3290. *
  3291. * @param index
  3292. * the index at which to insert the row, where the topmost row
  3293. * has index zero
  3294. * @return the inserted header row
  3295. *
  3296. * @throws IndexOutOfBoundsException
  3297. * if {@code rowIndex < 0 || rowIndex > getHeaderRowCount()}
  3298. *
  3299. * @see #appendHeaderRow()
  3300. * @see #prependHeaderRow()
  3301. * @see #removeHeaderRow(HeaderRow)
  3302. * @see #removeHeaderRow(int)
  3303. */
  3304. public HeaderRow addHeaderRowAt(int index) {
  3305. return getHeader().addRowAt(index);
  3306. }
  3307. /**
  3308. * Adds a new row at the bottom of the header section.
  3309. *
  3310. * @return the appended header row
  3311. *
  3312. * @see #prependHeaderRow()
  3313. * @see #addHeaderRowAt(int)
  3314. * @see #removeHeaderRow(HeaderRow)
  3315. * @see #removeHeaderRow(int)
  3316. */
  3317. public HeaderRow appendHeaderRow() {
  3318. return addHeaderRowAt(getHeaderRowCount());
  3319. }
  3320. /**
  3321. * Adds a new row at the top of the header section.
  3322. *
  3323. * @return the prepended header row
  3324. *
  3325. * @see #appendHeaderRow()
  3326. * @see #addHeaderRowAt(int)
  3327. * @see #removeHeaderRow(HeaderRow)
  3328. * @see #removeHeaderRow(int)
  3329. */
  3330. public HeaderRow prependHeaderRow() {
  3331. return addHeaderRowAt(0);
  3332. }
  3333. /**
  3334. * Removes the given row from the header section. Removing a default row
  3335. * sets the Grid to have no default row.
  3336. *
  3337. * @param row
  3338. * the header row to be removed, not null
  3339. *
  3340. * @throws IllegalArgumentException
  3341. * if the header does not contain the row
  3342. *
  3343. * @see #removeHeaderRow(int)
  3344. * @see #addHeaderRowAt(int)
  3345. * @see #appendHeaderRow()
  3346. * @see #prependHeaderRow()
  3347. */
  3348. public void removeHeaderRow(HeaderRow row) {
  3349. getHeader().removeRow(row);
  3350. }
  3351. /**
  3352. * Removes the row at the given position from the header section.
  3353. *
  3354. * @param index
  3355. * the index of the row to remove, where the topmost row has
  3356. * index zero
  3357. *
  3358. * @throws IndexOutOfBoundsException
  3359. * if {@code index < 0 || index >= getHeaderRowCount()}
  3360. *
  3361. * @see #removeHeaderRow(HeaderRow)
  3362. * @see #addHeaderRowAt(int)
  3363. * @see #appendHeaderRow()
  3364. * @see #prependHeaderRow()
  3365. */
  3366. public void removeHeaderRow(int index) {
  3367. getHeader().removeRow(index);
  3368. }
  3369. /**
  3370. * Sets the visibility of the Header in this Grid.
  3371. *
  3372. * @param headerVisible
  3373. * {@code true} if visible; {@code false} if not
  3374. *
  3375. * @since 8.1.1
  3376. */
  3377. public void setHeaderVisible(boolean headerVisible) {
  3378. getHeader().setVisible(headerVisible);
  3379. }
  3380. /**
  3381. * Gets the visibility of the Header in this Grid.
  3382. *
  3383. * @return {@code true} if visible; {@code false} if not
  3384. *
  3385. * @since 8.1.1
  3386. */
  3387. public boolean isHeaderVisible() {
  3388. return getHeader().isVisible();
  3389. }
  3390. /**
  3391. * Returns the current default row of the header.
  3392. *
  3393. * @return the default row or null if no default row set
  3394. *
  3395. * @see #setDefaultHeaderRow(HeaderRow)
  3396. */
  3397. public HeaderRow getDefaultHeaderRow() {
  3398. return header.getDefaultRow();
  3399. }
  3400. /**
  3401. * Sets the default row of the header. The default row is a special header
  3402. * row that displays column captions and sort indicators. By default Grid
  3403. * has a single row which is also the default row. When a header row is set
  3404. * as the default row, any existing cell content is replaced by the column
  3405. * captions.
  3406. *
  3407. * @param row
  3408. * the new default row, or null for no default row
  3409. *
  3410. * @throws IllegalArgumentException
  3411. * if the header does not contain the row
  3412. */
  3413. public void setDefaultHeaderRow(HeaderRow row) {
  3414. header.setDefaultRow((Row) row);
  3415. }
  3416. /**
  3417. * Returns the header section of this grid. The default header contains a
  3418. * single row, set as the {@linkplain #setDefaultHeaderRow(HeaderRow)
  3419. * default row}.
  3420. *
  3421. * @return the header section
  3422. */
  3423. protected Header getHeader() {
  3424. return header;
  3425. }
  3426. /**
  3427. * Returns the footer row at the given index.
  3428. *
  3429. * @param index
  3430. * the index of the row, where the topmost row has index zero
  3431. * @return the footer row at the index
  3432. * @throws IndexOutOfBoundsException
  3433. * if {@code rowIndex < 0 || rowIndex >= getFooterRowCount()}
  3434. */
  3435. public FooterRow getFooterRow(int index) {
  3436. return getFooter().getRow(index);
  3437. }
  3438. /**
  3439. * Gets the number of rows in the footer section.
  3440. *
  3441. * @return the number of footer rows
  3442. */
  3443. public int getFooterRowCount() {
  3444. return getFooter().getRowCount();
  3445. }
  3446. /**
  3447. * Inserts a new row at the given position to the footer section. Shifts the
  3448. * row currently at that position and any subsequent rows down (adds one to
  3449. * their indices). Inserting at {@link #getFooterRowCount()} appends the row
  3450. * at the bottom of the footer.
  3451. *
  3452. * @param index
  3453. * the index at which to insert the row, where the topmost row
  3454. * has index zero
  3455. * @return the inserted footer row
  3456. *
  3457. * @throws IndexOutOfBoundsException
  3458. * if {@code rowIndex < 0 || rowIndex > getFooterRowCount()}
  3459. *
  3460. * @see #appendFooterRow()
  3461. * @see #prependFooterRow()
  3462. * @see #removeFooterRow(FooterRow)
  3463. * @see #removeFooterRow(int)
  3464. */
  3465. public FooterRow addFooterRowAt(int index) {
  3466. return getFooter().addRowAt(index);
  3467. }
  3468. /**
  3469. * Adds a new row at the bottom of the footer section.
  3470. *
  3471. * @return the appended footer row
  3472. *
  3473. * @see #prependFooterRow()
  3474. * @see #addFooterRowAt(int)
  3475. * @see #removeFooterRow(FooterRow)
  3476. * @see #removeFooterRow(int)
  3477. */
  3478. public FooterRow appendFooterRow() {
  3479. return addFooterRowAt(getFooterRowCount());
  3480. }
  3481. /**
  3482. * Adds a new row at the top of the footer section.
  3483. *
  3484. * @return the prepended footer row
  3485. *
  3486. * @see #appendFooterRow()
  3487. * @see #addFooterRowAt(int)
  3488. * @see #removeFooterRow(FooterRow)
  3489. * @see #removeFooterRow(int)
  3490. */
  3491. public FooterRow prependFooterRow() {
  3492. return addFooterRowAt(0);
  3493. }
  3494. /**
  3495. * Removes the given row from the footer section. Removing a default row
  3496. * sets the Grid to have no default row.
  3497. *
  3498. * @param row
  3499. * the footer row to be removed, not null
  3500. *
  3501. * @throws IllegalArgumentException
  3502. * if the footer does not contain the row
  3503. *
  3504. * @see #removeFooterRow(int)
  3505. * @see #addFooterRowAt(int)
  3506. * @see #appendFooterRow()
  3507. * @see #prependFooterRow()
  3508. */
  3509. public void removeFooterRow(FooterRow row) {
  3510. getFooter().removeRow(row);
  3511. }
  3512. /**
  3513. * Removes the row at the given position from the footer section.
  3514. *
  3515. * @param index
  3516. * the index of the row to remove, where the topmost row has
  3517. * index zero
  3518. *
  3519. * @throws IndexOutOfBoundsException
  3520. * if {@code index < 0 || index >= getFooterRowCount()}
  3521. *
  3522. * @see #removeFooterRow(FooterRow)
  3523. * @see #addFooterRowAt(int)
  3524. * @see #appendFooterRow()
  3525. * @see #prependFooterRow()
  3526. */
  3527. public void removeFooterRow(int index) {
  3528. getFooter().removeRow(index);
  3529. }
  3530. /**
  3531. * Sets the visibility of the Footer in this Grid.
  3532. *
  3533. * @param footerVisible
  3534. * {@code true} if visible; {@code false} if not
  3535. *
  3536. * @since 8.1.1
  3537. */
  3538. public void setFooterVisible(boolean footerVisible) {
  3539. getFooter().setVisible(footerVisible);
  3540. }
  3541. /**
  3542. * Gets the visibility of the Footer in this Grid.
  3543. *
  3544. * @return {@code true} if visible; {@code false} if not
  3545. *
  3546. * @since 8.1.1
  3547. */
  3548. public boolean isFooterVisible() {
  3549. return getFooter().isVisible();
  3550. }
  3551. /**
  3552. * Returns the footer section of this grid.
  3553. *
  3554. * @return the footer section
  3555. */
  3556. protected Footer getFooter() {
  3557. return footer;
  3558. }
  3559. /**
  3560. * Registers a new column reorder listener.
  3561. *
  3562. * @param listener
  3563. * the listener to register, not null
  3564. * @return a registration for the listener
  3565. */
  3566. public Registration addColumnReorderListener(
  3567. ColumnReorderListener listener) {
  3568. return addListener(ColumnReorderEvent.class, listener,
  3569. COLUMN_REORDER_METHOD);
  3570. }
  3571. /**
  3572. * Registers a new column resize listener.
  3573. *
  3574. * @param listener
  3575. * the listener to register, not null
  3576. * @return a registration for the listener
  3577. */
  3578. public Registration addColumnResizeListener(ColumnResizeListener listener) {
  3579. return addListener(ColumnResizeEvent.class, listener,
  3580. COLUMN_RESIZE_METHOD);
  3581. }
  3582. /**
  3583. * Adds an item click listener. The listener is called when an item of this
  3584. * {@code Grid} is clicked.
  3585. *
  3586. * @param listener
  3587. * the item click listener, not null
  3588. * @return a registration for the listener
  3589. * @see #addContextClickListener
  3590. */
  3591. public Registration addItemClickListener(
  3592. ItemClickListener<? super T> listener) {
  3593. return addListener(GridConstants.ITEM_CLICK_EVENT_ID, ItemClick.class,
  3594. listener, ITEM_CLICK_METHOD);
  3595. }
  3596. /**
  3597. * Adds a context click listener that gets notified when a context click
  3598. * happens.
  3599. *
  3600. * @param listener
  3601. * the context click listener to add, not null actual event
  3602. * provided to the listener is {@link GridContextClickEvent}
  3603. * @return a registration object for removing the listener
  3604. *
  3605. * @since 8.1
  3606. * @see #addItemClickListener
  3607. * @see Registration
  3608. */
  3609. @Override
  3610. public Registration addContextClickListener(
  3611. ContextClickEvent.ContextClickListener listener) {
  3612. return super.addContextClickListener(listener);
  3613. }
  3614. /**
  3615. * Registers a new column visibility change listener.
  3616. *
  3617. * @param listener
  3618. * the listener to register, not null
  3619. * @return a registration for the listener
  3620. */
  3621. public Registration addColumnVisibilityChangeListener(
  3622. ColumnVisibilityChangeListener listener) {
  3623. return addListener(ColumnVisibilityChangeEvent.class, listener,
  3624. COLUMN_VISIBILITY_METHOD);
  3625. }
  3626. /**
  3627. * Returns whether column reordering is allowed. Default value is
  3628. * <code>false</code>.
  3629. *
  3630. * @return true if reordering is allowed
  3631. */
  3632. public boolean isColumnReorderingAllowed() {
  3633. return getState(false).columnReorderingAllowed;
  3634. }
  3635. /**
  3636. * Sets whether or not column reordering is allowed. Default value is
  3637. * <code>false</code>.
  3638. *
  3639. * @param columnReorderingAllowed
  3640. * specifies whether column reordering is allowed
  3641. */
  3642. public void setColumnReorderingAllowed(boolean columnReorderingAllowed) {
  3643. if (isColumnReorderingAllowed() != columnReorderingAllowed) {
  3644. getState().columnReorderingAllowed = columnReorderingAllowed;
  3645. }
  3646. }
  3647. /**
  3648. * Sets the columns and their order based on their column ids. Columns
  3649. * currently in this grid that are not present in the list of column ids are
  3650. * removed. This includes any column that has no id. Similarly, any new
  3651. * column in columns will be added to this grid. New columns can only be
  3652. * added for a <code>Grid</code> created using {@link Grid#Grid(Class)} or
  3653. * {@link #withPropertySet(PropertySet)}.
  3654. *
  3655. *
  3656. * @param columnIds
  3657. * the column ids to set
  3658. *
  3659. * @see Column#setId(String)
  3660. */
  3661. public void setColumns(String... columnIds) {
  3662. // Must extract to an explicitly typed variable because otherwise javac
  3663. // cannot determine which overload of setColumnOrder to use
  3664. Column<T, ?>[] newColumnOrder = Stream.of(columnIds)
  3665. .map((Function<String, Column<T, ?>>) id -> {
  3666. Column<T, ?> column = getColumn(id);
  3667. if (column == null) {
  3668. column = addColumn(id);
  3669. }
  3670. return column;
  3671. }).toArray(Column[]::new);
  3672. setColumnOrder(newColumnOrder);
  3673. // The columns to remove are now at the end of the column list
  3674. getColumns().stream().skip(columnIds.length)
  3675. .forEach(this::removeColumn);
  3676. }
  3677. private String getGeneratedIdentifier() {
  3678. String columnId = "" + counter;
  3679. counter++;
  3680. return columnId;
  3681. }
  3682. /**
  3683. * Sets a new column order for the grid. All columns which are not ordered
  3684. * here will remain in the order they were before as the last columns of
  3685. * grid.
  3686. *
  3687. * @param columns
  3688. * the columns in the order they should be
  3689. */
  3690. public void setColumnOrder(Column<T, ?>... columns) {
  3691. setColumnOrder(Stream.of(columns));
  3692. }
  3693. private void setColumnOrder(Stream<Column<T, ?>> columns) {
  3694. List<String> columnOrder = new ArrayList<>();
  3695. columns.forEach(column -> {
  3696. if (columnSet.contains(column)) {
  3697. columnOrder.add(column.getInternalId());
  3698. } else {
  3699. throw new IllegalStateException(
  3700. "setColumnOrder should not be called "
  3701. + "with columns that are not in the grid.");
  3702. }
  3703. });
  3704. List<String> stateColumnOrder = getState().columnOrder;
  3705. if (stateColumnOrder.size() != columnOrder.size()) {
  3706. stateColumnOrder.removeAll(columnOrder);
  3707. columnOrder.addAll(stateColumnOrder);
  3708. }
  3709. getState().columnOrder = columnOrder;
  3710. fireColumnReorderEvent(false);
  3711. }
  3712. /**
  3713. * Sets a new column order for the grid based on their column ids. All
  3714. * columns which are not ordered here will remain in the order they were
  3715. * before as the last columns of grid.
  3716. *
  3717. * @param columnIds
  3718. * the column ids in the order they should be
  3719. *
  3720. * @see Column#setId(String)
  3721. */
  3722. public void setColumnOrder(String... columnIds) {
  3723. setColumnOrder(Stream.of(columnIds).map(this::getColumnOrThrow));
  3724. }
  3725. /**
  3726. * Returns the selection model for this grid.
  3727. *
  3728. * @return the selection model, not null
  3729. */
  3730. public GridSelectionModel<T> getSelectionModel() {
  3731. assert selectionModel != null : "No selection model set by "
  3732. + getClass().getName() + " constructor";
  3733. return selectionModel;
  3734. }
  3735. /**
  3736. * Use this grid as a single select in {@link Binder}.
  3737. * <p>
  3738. * Throws {@link IllegalStateException} if the grid is not using a
  3739. * {@link SingleSelectionModel}.
  3740. *
  3741. * @return the single select wrapper that can be used in binder
  3742. * @throws IllegalStateException
  3743. * if not using a single selection model
  3744. */
  3745. public GridSingleSelect<T> asSingleSelect() {
  3746. return new GridSingleSelect<>(this);
  3747. }
  3748. public Editor<T> getEditor() {
  3749. return editor;
  3750. }
  3751. /**
  3752. * User this grid as a multiselect in {@link Binder}.
  3753. * <p>
  3754. * Throws {@link IllegalStateException} if the grid is not using a
  3755. * {@link MultiSelectionModel}.
  3756. *
  3757. * @return the multiselect wrapper that can be used in binder
  3758. * @throws IllegalStateException
  3759. * if not using a multiselection model
  3760. */
  3761. public GridMultiSelect<T> asMultiSelect() {
  3762. return new GridMultiSelect<>(this);
  3763. }
  3764. /**
  3765. * Sets the selection model for the grid.
  3766. * <p>
  3767. * This method is for setting a custom selection model, and is
  3768. * {@code protected} because {@link #setSelectionMode(SelectionMode)} should
  3769. * be used for easy switching between built-in selection models.
  3770. * <p>
  3771. * The default selection model is {@link SingleSelectionModelImpl}.
  3772. * <p>
  3773. * To use a custom selection model, you can e.g. extend the grid call this
  3774. * method with your custom selection model.
  3775. *
  3776. * @param model
  3777. * the selection model to use, not {@code null}
  3778. *
  3779. * @see #setSelectionMode(SelectionMode)
  3780. */
  3781. @SuppressWarnings("unchecked")
  3782. protected void setSelectionModel(GridSelectionModel<T> model) {
  3783. Objects.requireNonNull(model, "selection model cannot be null");
  3784. if (selectionModel != null) { // null when called from constructor
  3785. selectionModel.remove();
  3786. }
  3787. selectionModel = model;
  3788. if (selectionModel instanceof AbstractListingExtension) {
  3789. ((AbstractListingExtension<T>) selectionModel).extend(this);
  3790. } else {
  3791. addExtension(selectionModel);
  3792. }
  3793. }
  3794. /**
  3795. * Sets the grid's selection mode.
  3796. * <p>
  3797. * The built-in selection models are:
  3798. * <ul>
  3799. * <li>{@link SelectionMode#SINGLE} -> {@link SingleSelectionModelImpl},
  3800. * <b>the default model</b></li>
  3801. * <li>{@link SelectionMode#MULTI} -> {@link MultiSelectionModelImpl}, with
  3802. * checkboxes in the first column for selection</li>
  3803. * <li>{@link SelectionMode#NONE} -> {@link NoSelectionModel}, preventing
  3804. * selection</li>
  3805. * </ul>
  3806. * <p>
  3807. * To use your custom selection model, you can use
  3808. * {@link #setSelectionModel(GridSelectionModel)}, see existing selection
  3809. * model implementations for example.
  3810. *
  3811. * @param selectionMode
  3812. * the selection mode to switch to, not {@code null}
  3813. * @return the used selection model
  3814. *
  3815. * @see SelectionMode
  3816. * @see GridSelectionModel
  3817. * @see #setSelectionModel(GridSelectionModel)
  3818. */
  3819. public GridSelectionModel<T> setSelectionMode(SelectionMode selectionMode) {
  3820. Objects.requireNonNull(selectionMode, "Selection mode cannot be null.");
  3821. GridSelectionModel<T> model = selectionMode.createModel();
  3822. setSelectionModel(model);
  3823. return model;
  3824. }
  3825. /**
  3826. * This method is a shorthand that delegates to the currently set selection
  3827. * model.
  3828. *
  3829. * @see #getSelectionModel()
  3830. * @see GridSelectionModel
  3831. */
  3832. public Set<T> getSelectedItems() {
  3833. return getSelectionModel().getSelectedItems();
  3834. }
  3835. /**
  3836. * This method is a shorthand that delegates to the currently set selection
  3837. * model.
  3838. *
  3839. * @see #getSelectionModel()
  3840. * @see GridSelectionModel
  3841. */
  3842. public void select(T item) {
  3843. getSelectionModel().select(item);
  3844. }
  3845. /**
  3846. * This method is a shorthand that delegates to the currently set selection
  3847. * model.
  3848. *
  3849. * @see #getSelectionModel()
  3850. * @see GridSelectionModel
  3851. */
  3852. public void deselect(T item) {
  3853. getSelectionModel().deselect(item);
  3854. }
  3855. /**
  3856. * This method is a shorthand that delegates to the currently set selection
  3857. * model.
  3858. *
  3859. * @see #getSelectionModel()
  3860. * @see GridSelectionModel
  3861. */
  3862. public void deselectAll() {
  3863. getSelectionModel().deselectAll();
  3864. }
  3865. /**
  3866. * Adds a selection listener to the current selection model.
  3867. * <p>
  3868. * <em>NOTE:</em> If selection mode is switched with
  3869. * {@link #setSelectionMode(SelectionMode)}, then this listener is not
  3870. * triggered anymore when selection changes!
  3871. * <p>
  3872. * This is a shorthand for
  3873. * {@code grid.getSelectionModel().addSelectionListener()}. To get more
  3874. * detailed selection events, use {@link #getSelectionModel()} and either
  3875. * {@link SingleSelectionModel#addSingleSelectionListener(SingleSelectionListener)}
  3876. * or
  3877. * {@link MultiSelectionModel#addMultiSelectionListener(MultiSelectionListener)}
  3878. * depending on the used selection mode.
  3879. *
  3880. * @param listener
  3881. * the listener to add
  3882. * @return a registration handle to remove the listener
  3883. * @throws UnsupportedOperationException
  3884. * if selection has been disabled with
  3885. * {@link SelectionMode#NONE}
  3886. */
  3887. public Registration addSelectionListener(SelectionListener<T> listener)
  3888. throws UnsupportedOperationException {
  3889. return getSelectionModel().addSelectionListener(listener);
  3890. }
  3891. /**
  3892. * Sort this Grid in ascending order by a specified column.
  3893. *
  3894. * @param column
  3895. * a column to sort against
  3896. *
  3897. */
  3898. public void sort(Column<T, ?> column) {
  3899. sort(column, SortDirection.ASCENDING);
  3900. }
  3901. /**
  3902. * Sort this Grid in user-specified direction by a column.
  3903. *
  3904. * @param column
  3905. * a column to sort against
  3906. * @param direction
  3907. * a sort order value (ascending/descending)
  3908. *
  3909. */
  3910. public void sort(Column<T, ?> column, SortDirection direction) {
  3911. setSortOrder(Collections
  3912. .singletonList(new GridSortOrder<>(column, direction)));
  3913. }
  3914. /**
  3915. * Sort this Grid in ascending order by a specified column defined by id.
  3916. *
  3917. * @param columnId
  3918. * the id of the column to sort against
  3919. *
  3920. * @see Column#setId(String)
  3921. */
  3922. public void sort(String columnId) {
  3923. sort(columnId, SortDirection.ASCENDING);
  3924. }
  3925. /**
  3926. * Sort this Grid in a user-specified direction by a column defined by id.
  3927. *
  3928. * @param columnId
  3929. * the id of the column to sort against
  3930. * @param direction
  3931. * a sort order value (ascending/descending)
  3932. *
  3933. * @see Column#setId(String)
  3934. */
  3935. public void sort(String columnId, SortDirection direction) {
  3936. sort(getColumnOrThrow(columnId), direction);
  3937. }
  3938. /**
  3939. * Clear the current sort order, and re-sort the grid.
  3940. */
  3941. public void clearSortOrder() {
  3942. setSortOrder(Collections.emptyList());
  3943. }
  3944. /**
  3945. * Sets the sort order to use.
  3946. *
  3947. * @param order
  3948. * a sort order list.
  3949. *
  3950. * @throws IllegalArgumentException
  3951. * if order is null
  3952. */
  3953. public void setSortOrder(List<GridSortOrder<T>> order) {
  3954. setSortOrder(order, false);
  3955. }
  3956. /**
  3957. * Sets the sort order to use, given a {@link GridSortOrderBuilder}.
  3958. * Shorthand for {@code setSortOrder(builder.build())}.
  3959. *
  3960. * @see GridSortOrderBuilder
  3961. *
  3962. * @param builder
  3963. * the sort builder to retrieve the sort order from
  3964. * @throws NullPointerException
  3965. * if builder is null
  3966. */
  3967. public void setSortOrder(GridSortOrderBuilder<T> builder) {
  3968. Objects.requireNonNull(builder, "Sort builder cannot be null");
  3969. setSortOrder(builder.build());
  3970. }
  3971. /**
  3972. * Adds a sort order change listener that gets notified when the sort order
  3973. * changes.
  3974. *
  3975. * @param listener
  3976. * the sort order change listener to add
  3977. */
  3978. @Override
  3979. public Registration addSortListener(
  3980. SortListener<GridSortOrder<T>> listener) {
  3981. return addListener(SortEvent.class, listener, SORT_ORDER_CHANGE_METHOD);
  3982. }
  3983. /**
  3984. * Get the current sort order list.
  3985. *
  3986. * @return a sort order list
  3987. */
  3988. public List<GridSortOrder<T>> getSortOrder() {
  3989. return Collections.unmodifiableList(sortOrder);
  3990. }
  3991. /**
  3992. * Scrolls to a certain item, using {@link ScrollDestination#ANY}.
  3993. * <p>
  3994. * If the item has an open details row, its size will also be taken into
  3995. * account.
  3996. *
  3997. * @param row
  3998. * zero based index of the item to scroll to in the current view.
  3999. * @throws IllegalArgumentException
  4000. * if the provided row is outside the item range
  4001. */
  4002. public void scrollTo(int row) throws IllegalArgumentException {
  4003. scrollTo(row, ScrollDestination.ANY);
  4004. }
  4005. /**
  4006. * Scrolls to a certain item, using user-specified scroll destination.
  4007. * <p>
  4008. * If the item has an open details row, its size will also be taken into
  4009. * account.
  4010. *
  4011. * @param row
  4012. * zero based index of the item to scroll to in the current view.
  4013. * @param destination
  4014. * value specifying desired position of scrolled-to row, not
  4015. * {@code null}
  4016. * @throws IllegalArgumentException
  4017. * if the provided row is outside the item range
  4018. */
  4019. public void scrollTo(int row, ScrollDestination destination) {
  4020. Objects.requireNonNull(destination,
  4021. "ScrollDestination can not be null");
  4022. if (row >= getDataCommunicator().getDataProviderSize()) {
  4023. throw new IllegalArgumentException("Row outside dataProvider size");
  4024. }
  4025. getRpcProxy(GridClientRpc.class).scrollToRow(row, destination);
  4026. }
  4027. /**
  4028. * Scrolls to the beginning of the first data row.
  4029. */
  4030. public void scrollToStart() {
  4031. getRpcProxy(GridClientRpc.class).scrollToStart();
  4032. }
  4033. /**
  4034. * Scrolls to the end of the last data row.
  4035. */
  4036. public void scrollToEnd() {
  4037. getRpcProxy(GridClientRpc.class).scrollToEnd();
  4038. }
  4039. @Override
  4040. protected GridState getState() {
  4041. return getState(true);
  4042. }
  4043. @Override
  4044. protected GridState getState(boolean markAsDirty) {
  4045. return (GridState) super.getState(markAsDirty);
  4046. }
  4047. /**
  4048. * Sets the column resize mode to use. The default mode is
  4049. * {@link ColumnResizeMode#ANIMATED}.
  4050. *
  4051. * @param mode
  4052. * a ColumnResizeMode value
  4053. * @since 7.7.5
  4054. */
  4055. public void setColumnResizeMode(ColumnResizeMode mode) {
  4056. getState().columnResizeMode = mode;
  4057. }
  4058. /**
  4059. * Returns the current column resize mode. The default mode is
  4060. * {@link ColumnResizeMode#ANIMATED}.
  4061. *
  4062. * @return a ColumnResizeMode value
  4063. * @since 7.7.5
  4064. */
  4065. public ColumnResizeMode getColumnResizeMode() {
  4066. return getState(false).columnResizeMode;
  4067. }
  4068. /**
  4069. * Creates a new Editor instance. Can be overridden to create a custom
  4070. * Editor. If the Editor is a {@link AbstractGridExtension}, it will be
  4071. * automatically added to {@link DataCommunicator}.
  4072. *
  4073. * @return editor
  4074. */
  4075. protected Editor<T> createEditor() {
  4076. return new EditorImpl<>(propertySet);
  4077. }
  4078. private void addExtensionComponent(Component c) {
  4079. if (extensionComponents.add(c)) {
  4080. c.setParent(this);
  4081. markAsDirty();
  4082. }
  4083. }
  4084. private void removeExtensionComponent(Component c) {
  4085. if (extensionComponents.remove(c)) {
  4086. c.setParent(null);
  4087. markAsDirty();
  4088. }
  4089. }
  4090. private void fireColumnReorderEvent(boolean userOriginated) {
  4091. fireEvent(new ColumnReorderEvent(this, userOriginated));
  4092. }
  4093. private void fireColumnResizeEvent(Column<?, ?> column,
  4094. boolean userOriginated) {
  4095. fireEvent(new ColumnResizeEvent(this, column, userOriginated));
  4096. }
  4097. @Override
  4098. protected void readItems(Element design, DesignContext context) {
  4099. // Grid handles reading of items in Grid#readData
  4100. }
  4101. @Override
  4102. public DataProvider<T, ?> getDataProvider() {
  4103. return internalGetDataProvider();
  4104. }
  4105. @Override
  4106. public void setDataProvider(DataProvider<T, ?> dataProvider) {
  4107. internalSetDataProvider(dataProvider);
  4108. }
  4109. /**
  4110. * Sets a CallbackDataProvider using the given fetch items callback and a
  4111. * size callback.
  4112. * <p>
  4113. * This method is a shorthand for making a {@link CallbackDataProvider} that
  4114. * handles a partial {@link Query} object.
  4115. *
  4116. * @param fetchItems
  4117. * a callback for fetching items
  4118. * @param sizeCallback
  4119. * a callback for getting the count of items
  4120. *
  4121. * @see CallbackDataProvider
  4122. * @see #setDataProvider(DataProvider)
  4123. */
  4124. public void setDataProvider(FetchItemsCallback<T> fetchItems,
  4125. SerializableSupplier<Integer> sizeCallback) {
  4126. internalSetDataProvider(
  4127. new CallbackDataProvider<>(
  4128. q -> fetchItems.fetchItems(q.getSortOrders(),
  4129. q.getOffset(), q.getLimit()),
  4130. q -> sizeCallback.get()));
  4131. }
  4132. @Override
  4133. protected void doReadDesign(Element design, DesignContext context) {
  4134. Attributes attrs = design.attributes();
  4135. if (design.hasAttr(DECLARATIVE_DATA_ITEM_TYPE)) {
  4136. String itemType = design.attr(DECLARATIVE_DATA_ITEM_TYPE);
  4137. setBeanType(itemType);
  4138. }
  4139. if (attrs.hasKey("selection-mode")) {
  4140. setSelectionMode(DesignAttributeHandler.readAttribute(
  4141. "selection-mode", attrs, SelectionMode.class));
  4142. }
  4143. Attributes attr = design.attributes();
  4144. if (attr.hasKey("selection-allowed")) {
  4145. setReadOnly(DesignAttributeHandler
  4146. .readAttribute("selection-allowed", attr, Boolean.class));
  4147. }
  4148. if (attrs.hasKey("rows")) {
  4149. setHeightByRows(DesignAttributeHandler.readAttribute("rows", attrs,
  4150. double.class));
  4151. }
  4152. readStructure(design, context);
  4153. // Read frozen columns after columns are read.
  4154. if (attrs.hasKey("frozen-columns")) {
  4155. setFrozenColumnCount(DesignAttributeHandler
  4156. .readAttribute("frozen-columns", attrs, int.class));
  4157. }
  4158. }
  4159. /**
  4160. * Sets the bean type to use for property mapping.
  4161. * <p>
  4162. * This method is responsible also for setting or updating the property set
  4163. * so that it matches the given bean type.
  4164. * <p>
  4165. * Protected mostly for Designer needs, typically should not be overridden
  4166. * or even called.
  4167. *
  4168. * @param beanTypeClassName
  4169. * the fully qualified class name of the bean type
  4170. *
  4171. * @since 8.0.3
  4172. */
  4173. @SuppressWarnings("unchecked")
  4174. protected void setBeanType(String beanTypeClassName) {
  4175. setBeanType((Class<T>) resolveClass(beanTypeClassName));
  4176. }
  4177. /**
  4178. * Sets the bean type to use for property mapping.
  4179. * <p>
  4180. * This method is responsible also for setting or updating the property set
  4181. * so that it matches the given bean type.
  4182. * <p>
  4183. * Protected mostly for Designer needs, typically should not be overridden
  4184. * or even called.
  4185. *
  4186. * @param beanType
  4187. * the bean type class
  4188. *
  4189. * @since 8.0.3
  4190. */
  4191. protected void setBeanType(Class<T> beanType) {
  4192. this.beanType = beanType;
  4193. setPropertySet(BeanPropertySet.get(beanType));
  4194. }
  4195. private Class<?> resolveClass(String qualifiedClassName) {
  4196. try {
  4197. Class<?> resolvedClass = Class.forName(qualifiedClassName, true,
  4198. VaadinServiceClassLoaderUtil.findDefaultClassLoader());
  4199. return resolvedClass;
  4200. } catch (ClassNotFoundException | SecurityException e) {
  4201. throw new IllegalArgumentException(
  4202. "Unable to find class " + qualifiedClassName, e);
  4203. }
  4204. }
  4205. @Override
  4206. protected void doWriteDesign(Element design, DesignContext designContext) {
  4207. Attributes attr = design.attributes();
  4208. if (this.beanType != null) {
  4209. design.attr(DECLARATIVE_DATA_ITEM_TYPE,
  4210. this.beanType.getCanonicalName());
  4211. }
  4212. DesignAttributeHandler.writeAttribute("selection-allowed", attr,
  4213. isReadOnly(), false, Boolean.class, designContext);
  4214. Attributes attrs = design.attributes();
  4215. Grid<?> defaultInstance = designContext.getDefaultInstance(this);
  4216. DesignAttributeHandler.writeAttribute("frozen-columns", attrs,
  4217. getFrozenColumnCount(), defaultInstance.getFrozenColumnCount(),
  4218. int.class, designContext);
  4219. if (HeightMode.ROW.equals(getHeightMode())) {
  4220. DesignAttributeHandler.writeAttribute("rows", attrs,
  4221. getHeightByRows(), defaultInstance.getHeightByRows(),
  4222. double.class, designContext);
  4223. }
  4224. SelectionMode mode = getSelectionMode();
  4225. if (mode != null) {
  4226. DesignAttributeHandler.writeAttribute("selection-mode", attrs, mode,
  4227. SelectionMode.SINGLE, SelectionMode.class, designContext);
  4228. }
  4229. writeStructure(design, designContext);
  4230. }
  4231. @Override
  4232. protected T deserializeDeclarativeRepresentation(String item) {
  4233. if (item == null) {
  4234. return super.deserializeDeclarativeRepresentation(
  4235. UUID.randomUUID().toString());
  4236. }
  4237. return super.deserializeDeclarativeRepresentation(new String(item));
  4238. }
  4239. @Override
  4240. protected boolean isReadOnly() {
  4241. SelectionMode selectionMode = getSelectionMode();
  4242. if (SelectionMode.SINGLE.equals(selectionMode)) {
  4243. return asSingleSelect().isReadOnly();
  4244. }
  4245. if (SelectionMode.MULTI.equals(selectionMode)) {
  4246. return asMultiSelect().isReadOnly();
  4247. }
  4248. return false;
  4249. }
  4250. @Override
  4251. protected void setReadOnly(boolean readOnly) {
  4252. SelectionMode selectionMode = getSelectionMode();
  4253. if (SelectionMode.SINGLE.equals(selectionMode)) {
  4254. asSingleSelect().setReadOnly(readOnly);
  4255. } else if (SelectionMode.MULTI.equals(selectionMode)) {
  4256. asMultiSelect().setReadOnly(readOnly);
  4257. }
  4258. }
  4259. private void readStructure(Element design, DesignContext context) {
  4260. if (design.children().isEmpty()) {
  4261. return;
  4262. }
  4263. if (design.children().size() > 1
  4264. || !design.child(0).tagName().equals("table")) {
  4265. throw new DesignException(
  4266. "Grid needs to have a table element as its only child");
  4267. }
  4268. Element table = design.child(0);
  4269. Elements colgroups = table.getElementsByTag("colgroup");
  4270. if (colgroups.size() != 1) {
  4271. throw new DesignException(
  4272. "Table element in declarative Grid needs to have a"
  4273. + " colgroup defining the columns used in Grid");
  4274. }
  4275. List<DeclarativeValueProvider<T>> providers = new ArrayList<>();
  4276. for (Element col : colgroups.get(0).getElementsByTag("col")) {
  4277. String id = DesignAttributeHandler.readAttribute("column-id",
  4278. col.attributes(), null, String.class);
  4279. // If there is a property with a matching name available,
  4280. // map to that
  4281. Optional<PropertyDefinition<T, ?>> property = propertySet
  4282. .getProperties().filter(p -> p.getName().equals(id))
  4283. .findFirst();
  4284. Column<T, ?> column;
  4285. if (property.isPresent()) {
  4286. column = addColumn(id);
  4287. } else {
  4288. DeclarativeValueProvider<T> provider = new DeclarativeValueProvider<>();
  4289. column = createColumn(provider, ValueProvider.identity(),
  4290. new HtmlRenderer());
  4291. addColumn(getGeneratedIdentifier(), column);
  4292. if (id != null) {
  4293. column.setId(id);
  4294. }
  4295. providers.add(provider);
  4296. }
  4297. column.readDesign(col, context);
  4298. }
  4299. for (Element child : table.children()) {
  4300. if (child.tagName().equals("thead")) {
  4301. getHeader().readDesign(child, context);
  4302. } else if (child.tagName().equals("tbody")) {
  4303. readData(child, providers);
  4304. } else if (child.tagName().equals("tfoot")) {
  4305. getFooter().readDesign(child, context);
  4306. }
  4307. }
  4308. // Sync default header captions to column captions
  4309. if (getDefaultHeaderRow() != null) {
  4310. for (Column<T, ?> c : getColumns()) {
  4311. HeaderCell headerCell = getDefaultHeaderRow().getCell(c);
  4312. if (headerCell.getCellType() == GridStaticCellType.TEXT) {
  4313. c.setCaption(headerCell.getText());
  4314. }
  4315. }
  4316. }
  4317. }
  4318. /**
  4319. * Reads the declarative representation of a grid's data from the given
  4320. * element and stores it in the given {@link DeclarativeValueProvider}s.
  4321. * Each member in the list of value providers corresponds to a column in the
  4322. * grid.
  4323. *
  4324. * @since 8.1
  4325. *
  4326. * @param body
  4327. * the element to read data from
  4328. * @param providers
  4329. * list of {@link DeclarativeValueProvider}s to store the data of
  4330. * each column to
  4331. */
  4332. protected void readData(Element body,
  4333. List<DeclarativeValueProvider<T>> providers) {
  4334. getSelectionModel().deselectAll();
  4335. List<T> items = new ArrayList<>();
  4336. List<T> selectedItems = new ArrayList<>();
  4337. for (Element row : body.children()) {
  4338. T item = deserializeDeclarativeRepresentation(row.attr("item"));
  4339. items.add(item);
  4340. if (row.hasAttr("selected")) {
  4341. selectedItems.add(item);
  4342. }
  4343. Elements cells = row.children();
  4344. int i = 0;
  4345. for (Element cell : cells) {
  4346. providers.get(i).addValue(item, cell.html());
  4347. i++;
  4348. }
  4349. }
  4350. setItems(items);
  4351. selectedItems.forEach(getSelectionModel()::select);
  4352. }
  4353. private void writeStructure(Element design, DesignContext designContext) {
  4354. if (getColumns().isEmpty()) {
  4355. return;
  4356. }
  4357. Element tableElement = design.appendElement("table");
  4358. Element colGroup = tableElement.appendElement("colgroup");
  4359. getColumns().forEach(column -> column
  4360. .writeDesign(colGroup.appendElement("col"), designContext));
  4361. // Always write thead. Reads correctly when there no header rows
  4362. getHeader().writeDesign(tableElement.appendElement("thead"),
  4363. designContext);
  4364. if (designContext.shouldWriteData(this)) {
  4365. Element bodyElement = tableElement.appendElement("tbody");
  4366. writeData(bodyElement, designContext);
  4367. }
  4368. if (getFooter().getRowCount() > 0) {
  4369. getFooter().writeDesign(tableElement.appendElement("tfoot"),
  4370. designContext);
  4371. }
  4372. }
  4373. /**
  4374. * Writes the data contained in this grid. Used when serializing a grid to
  4375. * its declarative representation, if
  4376. * {@link DesignContext#shouldWriteData(Component)} returns {@code true} for
  4377. * the grid that is being written.
  4378. *
  4379. * @since 8.1
  4380. *
  4381. * @param body
  4382. * the body element to write the declarative representation of
  4383. * data to
  4384. * @param designContext
  4385. * the design context
  4386. *
  4387. * @since 8.1
  4388. */
  4389. protected void writeData(Element body, DesignContext designContext) {
  4390. getDataProvider().fetch(new Query<>())
  4391. .forEach(item -> writeRow(body, item, designContext));
  4392. }
  4393. private void writeRow(Element container, T item, DesignContext context) {
  4394. Element tableRow = container.appendElement("tr");
  4395. tableRow.attr("item", serializeDeclarativeRepresentation(item));
  4396. if (getSelectionModel().isSelected(item)) {
  4397. tableRow.attr("selected", true);
  4398. }
  4399. for (Column<T, ?> column : getColumns()) {
  4400. Object value = column.valueProvider.apply(item);
  4401. tableRow.appendElement("td")
  4402. .append(Optional.ofNullable(value).map(Object::toString)
  4403. .map(DesignFormatter::encodeForTextNode)
  4404. .orElse(""));
  4405. }
  4406. }
  4407. private SelectionMode getSelectionMode() {
  4408. GridSelectionModel<T> selectionModel = getSelectionModel();
  4409. SelectionMode mode = null;
  4410. if (selectionModel.getClass().equals(SingleSelectionModelImpl.class)) {
  4411. mode = SelectionMode.SINGLE;
  4412. } else if (selectionModel.getClass()
  4413. .equals(MultiSelectionModelImpl.class)) {
  4414. mode = SelectionMode.MULTI;
  4415. } else if (selectionModel.getClass().equals(NoSelectionModel.class)) {
  4416. mode = SelectionMode.NONE;
  4417. }
  4418. return mode;
  4419. }
  4420. /**
  4421. * Sets a user-defined identifier for given column.
  4422. *
  4423. * @see Column#setId(String)
  4424. *
  4425. * @param column
  4426. * the column
  4427. * @param id
  4428. * the user-defined identifier
  4429. */
  4430. protected void setColumnId(String id, Column<T, ?> column) {
  4431. if (columnIds.containsKey(id)) {
  4432. throw new IllegalArgumentException("Duplicate ID for columns");
  4433. }
  4434. columnIds.put(id, column);
  4435. }
  4436. @Override
  4437. protected Collection<String> getCustomAttributes() {
  4438. Collection<String> result = super.getCustomAttributes();
  4439. // "rename" for frozen column count
  4440. result.add("frozen-column-count");
  4441. result.add("frozen-columns");
  4442. // "rename" for height-mode
  4443. result.add("height-by-rows");
  4444. result.add("rows");
  4445. // add a selection-mode attribute
  4446. result.add("selection-mode");
  4447. return result;
  4448. }
  4449. /**
  4450. * Returns a column identified by its internal id. This id should not be
  4451. * confused with the user-defined identifier.
  4452. *
  4453. * @param columnId
  4454. * the internal id of column
  4455. * @return column identified by internal id
  4456. */
  4457. protected Column<T, ?> getColumnByInternalId(String columnId) {
  4458. return columnKeys.get(columnId);
  4459. }
  4460. /**
  4461. * Returns the internal id for given column. This id should not be confused
  4462. * with the user-defined identifier.
  4463. *
  4464. * @param column
  4465. * the column
  4466. * @return internal id of given column
  4467. */
  4468. protected String getInternalIdForColumn(Column<T, ?> column) {
  4469. return column.getInternalId();
  4470. }
  4471. private void setSortOrder(List<GridSortOrder<T>> order,
  4472. boolean userOriginated) {
  4473. Objects.requireNonNull(order, "Sort order list cannot be null");
  4474. // Update client state to display sort order.
  4475. List<String> sortColumns = new ArrayList<>();
  4476. List<SortDirection> directions = new ArrayList<>();
  4477. order.stream().forEach(sortOrder -> {
  4478. sortColumns.add(sortOrder.getSorted().getInternalId());
  4479. directions.add(sortOrder.getDirection());
  4480. });
  4481. getState().sortColumns = sortColumns.toArray(new String[0]);
  4482. getState().sortDirs = directions.toArray(new SortDirection[0]);
  4483. sortOrder.clear();
  4484. sortOrder.addAll(order);
  4485. sort(userOriginated);
  4486. }
  4487. private void sort(boolean userOriginated) {
  4488. // Set sort orders
  4489. // In-memory comparator
  4490. getDataCommunicator().setInMemorySorting(createSortingComparator(),
  4491. false);
  4492. // Back-end sort properties
  4493. List<QuerySortOrder> sortProperties = new ArrayList<>();
  4494. sortOrder.stream().map(
  4495. order -> order.getSorted().getSortOrder(order.getDirection()))
  4496. .forEach(s -> s.forEach(sortProperties::add));
  4497. getDataCommunicator().setBackEndSorting(sortProperties, true);
  4498. // Close grid editor if it's open.
  4499. if (getEditor().isOpen()) {
  4500. getEditor().cancel();
  4501. }
  4502. fireEvent(new SortEvent<>(this, new ArrayList<>(sortOrder),
  4503. userOriginated));
  4504. }
  4505. /**
  4506. * Creates a comparator for grid to sort rows.
  4507. *
  4508. * @return the comparator based on column sorting information.
  4509. */
  4510. protected SerializableComparator<T> createSortingComparator() {
  4511. /*
  4512. * thenComparing is defined to return a serializable comparator as long
  4513. * as both original comparators are also serializable
  4514. */
  4515. BinaryOperator<SerializableComparator<T>> operator = (comparator1,
  4516. comparator2) -> comparator1.thenComparing(comparator2)::compare;
  4517. return sortOrder.stream().map(
  4518. order -> order.getSorted().getComparator(order.getDirection()))
  4519. .reduce((x, y) -> 0, operator);
  4520. }
  4521. @Override
  4522. protected void internalSetDataProvider(DataProvider<T, ?> dataProvider) {
  4523. super.internalSetDataProvider(dataProvider);
  4524. for (Column<T, ?> column : getColumns()) {
  4525. column.updateSortable();
  4526. }
  4527. }
  4528. }