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 171KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992
  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. /**
  3678. * Sets the columns and their order based on their column ids provided that
  3679. * collection supports preserving of the order. Columns currently in this
  3680. * grid that are not present in the collection of column ids are removed.
  3681. * This includes any column that has no id. Similarly, any new column in
  3682. * columns will be added to this grid. New columns can only be added for a
  3683. * <code>Grid</code> created using {@link Grid#Grid(Class)} or
  3684. * {@link #withPropertySet(PropertySet)}.
  3685. *
  3686. *
  3687. * @param columnIds
  3688. * the column ids to set
  3689. *
  3690. * @see Column#setId(String)
  3691. * @see #setColumns(String...)
  3692. */
  3693. public void setColumns(Collection<String> columnIds) {
  3694. Objects.requireNonNull(columnIds, "columnIds can't be null");
  3695. String[] columns = columnIds.toArray(new String[columnIds.size()]);
  3696. setColumns(columns);
  3697. }
  3698. private String getGeneratedIdentifier() {
  3699. String columnId = "" + counter;
  3700. counter++;
  3701. return columnId;
  3702. }
  3703. /**
  3704. * Sets a new column order for the grid. All columns which are not ordered
  3705. * here will remain in the order they were before as the last columns of
  3706. * grid.
  3707. *
  3708. * @param columns
  3709. * the columns in the order they should be
  3710. */
  3711. public void setColumnOrder(Column<T, ?>... columns) {
  3712. setColumnOrder(Stream.of(columns));
  3713. }
  3714. private void setColumnOrder(Stream<Column<T, ?>> columns) {
  3715. List<String> columnOrder = new ArrayList<>();
  3716. columns.forEach(column -> {
  3717. if (columnSet.contains(column)) {
  3718. columnOrder.add(column.getInternalId());
  3719. } else {
  3720. throw new IllegalStateException(
  3721. "setColumnOrder should not be called "
  3722. + "with columns that are not in the grid.");
  3723. }
  3724. });
  3725. List<String> stateColumnOrder = getState().columnOrder;
  3726. if (stateColumnOrder.size() != columnOrder.size()) {
  3727. stateColumnOrder.removeAll(columnOrder);
  3728. columnOrder.addAll(stateColumnOrder);
  3729. }
  3730. getState().columnOrder = columnOrder;
  3731. fireColumnReorderEvent(false);
  3732. }
  3733. /**
  3734. * Sets a new column order for the grid based on their column ids. All
  3735. * columns which are not ordered here will remain in the order they were
  3736. * before as the last columns of grid.
  3737. *
  3738. * @param columnIds
  3739. * the column ids in the order they should be
  3740. *
  3741. * @see Column#setId(String)
  3742. */
  3743. public void setColumnOrder(String... columnIds) {
  3744. setColumnOrder(Stream.of(columnIds).map(this::getColumnOrThrow));
  3745. }
  3746. /**
  3747. * Returns the selection model for this grid.
  3748. *
  3749. * @return the selection model, not null
  3750. */
  3751. public GridSelectionModel<T> getSelectionModel() {
  3752. assert selectionModel != null : "No selection model set by "
  3753. + getClass().getName() + " constructor";
  3754. return selectionModel;
  3755. }
  3756. /**
  3757. * Use this grid as a single select in {@link Binder}.
  3758. * <p>
  3759. * Throws {@link IllegalStateException} if the grid is not using a
  3760. * {@link SingleSelectionModel}.
  3761. *
  3762. * @return the single select wrapper that can be used in binder
  3763. * @throws IllegalStateException
  3764. * if not using a single selection model
  3765. */
  3766. public GridSingleSelect<T> asSingleSelect() {
  3767. return new GridSingleSelect<>(this);
  3768. }
  3769. public Editor<T> getEditor() {
  3770. return editor;
  3771. }
  3772. /**
  3773. * User this grid as a multiselect in {@link Binder}.
  3774. * <p>
  3775. * Throws {@link IllegalStateException} if the grid is not using a
  3776. * {@link MultiSelectionModel}.
  3777. *
  3778. * @return the multiselect wrapper that can be used in binder
  3779. * @throws IllegalStateException
  3780. * if not using a multiselection model
  3781. */
  3782. public GridMultiSelect<T> asMultiSelect() {
  3783. return new GridMultiSelect<>(this);
  3784. }
  3785. /**
  3786. * Sets the selection model for the grid.
  3787. * <p>
  3788. * This method is for setting a custom selection model, and is
  3789. * {@code protected} because {@link #setSelectionMode(SelectionMode)} should
  3790. * be used for easy switching between built-in selection models.
  3791. * <p>
  3792. * The default selection model is {@link SingleSelectionModelImpl}.
  3793. * <p>
  3794. * To use a custom selection model, you can e.g. extend the grid call this
  3795. * method with your custom selection model.
  3796. *
  3797. * @param model
  3798. * the selection model to use, not {@code null}
  3799. *
  3800. * @see #setSelectionMode(SelectionMode)
  3801. */
  3802. @SuppressWarnings("unchecked")
  3803. protected void setSelectionModel(GridSelectionModel<T> model) {
  3804. Objects.requireNonNull(model, "selection model cannot be null");
  3805. if (selectionModel != null) { // null when called from constructor
  3806. selectionModel.remove();
  3807. }
  3808. selectionModel = model;
  3809. if (selectionModel instanceof AbstractListingExtension) {
  3810. ((AbstractListingExtension<T>) selectionModel).extend(this);
  3811. } else {
  3812. addExtension(selectionModel);
  3813. }
  3814. }
  3815. /**
  3816. * Sets the grid's selection mode.
  3817. * <p>
  3818. * The built-in selection models are:
  3819. * <ul>
  3820. * <li>{@link SelectionMode#SINGLE} -> {@link SingleSelectionModelImpl},
  3821. * <b>the default model</b></li>
  3822. * <li>{@link SelectionMode#MULTI} -> {@link MultiSelectionModelImpl}, with
  3823. * checkboxes in the first column for selection</li>
  3824. * <li>{@link SelectionMode#NONE} -> {@link NoSelectionModel}, preventing
  3825. * selection</li>
  3826. * </ul>
  3827. * <p>
  3828. * To use your custom selection model, you can use
  3829. * {@link #setSelectionModel(GridSelectionModel)}, see existing selection
  3830. * model implementations for example.
  3831. *
  3832. * @param selectionMode
  3833. * the selection mode to switch to, not {@code null}
  3834. * @return the used selection model
  3835. *
  3836. * @see SelectionMode
  3837. * @see GridSelectionModel
  3838. * @see #setSelectionModel(GridSelectionModel)
  3839. */
  3840. public GridSelectionModel<T> setSelectionMode(SelectionMode selectionMode) {
  3841. Objects.requireNonNull(selectionMode, "Selection mode cannot be null.");
  3842. GridSelectionModel<T> model = selectionMode.createModel();
  3843. setSelectionModel(model);
  3844. return model;
  3845. }
  3846. /**
  3847. * This method is a shorthand that delegates to the currently set selection
  3848. * model.
  3849. *
  3850. * @see #getSelectionModel()
  3851. * @see GridSelectionModel
  3852. */
  3853. public Set<T> getSelectedItems() {
  3854. return getSelectionModel().getSelectedItems();
  3855. }
  3856. /**
  3857. * This method is a shorthand that delegates to the currently set selection
  3858. * model.
  3859. *
  3860. * @see #getSelectionModel()
  3861. * @see GridSelectionModel
  3862. */
  3863. public void select(T item) {
  3864. getSelectionModel().select(item);
  3865. }
  3866. /**
  3867. * This method is a shorthand that delegates to the currently set selection
  3868. * model.
  3869. *
  3870. * @see #getSelectionModel()
  3871. * @see GridSelectionModel
  3872. */
  3873. public void deselect(T item) {
  3874. getSelectionModel().deselect(item);
  3875. }
  3876. /**
  3877. * This method is a shorthand that delegates to the currently set selection
  3878. * model.
  3879. *
  3880. * @see #getSelectionModel()
  3881. * @see GridSelectionModel
  3882. */
  3883. public void deselectAll() {
  3884. getSelectionModel().deselectAll();
  3885. }
  3886. /**
  3887. * Adds a selection listener to the current selection model.
  3888. * <p>
  3889. * <em>NOTE:</em> If selection mode is switched with
  3890. * {@link #setSelectionMode(SelectionMode)}, then this listener is not
  3891. * triggered anymore when selection changes!
  3892. * <p>
  3893. * This is a shorthand for
  3894. * {@code grid.getSelectionModel().addSelectionListener()}. To get more
  3895. * detailed selection events, use {@link #getSelectionModel()} and either
  3896. * {@link SingleSelectionModel#addSingleSelectionListener(SingleSelectionListener)}
  3897. * or
  3898. * {@link MultiSelectionModel#addMultiSelectionListener(MultiSelectionListener)}
  3899. * depending on the used selection mode.
  3900. *
  3901. * @param listener
  3902. * the listener to add
  3903. * @return a registration handle to remove the listener
  3904. * @throws UnsupportedOperationException
  3905. * if selection has been disabled with
  3906. * {@link SelectionMode#NONE}
  3907. */
  3908. public Registration addSelectionListener(SelectionListener<T> listener)
  3909. throws UnsupportedOperationException {
  3910. return getSelectionModel().addSelectionListener(listener);
  3911. }
  3912. /**
  3913. * Sort this Grid in ascending order by a specified column.
  3914. *
  3915. * @param column
  3916. * a column to sort against
  3917. *
  3918. */
  3919. public void sort(Column<T, ?> column) {
  3920. sort(column, SortDirection.ASCENDING);
  3921. }
  3922. /**
  3923. * Sort this Grid in user-specified direction by a column.
  3924. *
  3925. * @param column
  3926. * a column to sort against
  3927. * @param direction
  3928. * a sort order value (ascending/descending)
  3929. *
  3930. */
  3931. public void sort(Column<T, ?> column, SortDirection direction) {
  3932. setSortOrder(Collections
  3933. .singletonList(new GridSortOrder<>(column, direction)));
  3934. }
  3935. /**
  3936. * Sort this Grid in ascending order by a specified column defined by id.
  3937. *
  3938. * @param columnId
  3939. * the id of the column to sort against
  3940. *
  3941. * @see Column#setId(String)
  3942. */
  3943. public void sort(String columnId) {
  3944. sort(columnId, SortDirection.ASCENDING);
  3945. }
  3946. /**
  3947. * Sort this Grid in a user-specified direction by a column defined by id.
  3948. *
  3949. * @param columnId
  3950. * the id of the column to sort against
  3951. * @param direction
  3952. * a sort order value (ascending/descending)
  3953. *
  3954. * @see Column#setId(String)
  3955. */
  3956. public void sort(String columnId, SortDirection direction) {
  3957. sort(getColumnOrThrow(columnId), direction);
  3958. }
  3959. /**
  3960. * Clear the current sort order, and re-sort the grid.
  3961. */
  3962. public void clearSortOrder() {
  3963. setSortOrder(Collections.emptyList());
  3964. }
  3965. /**
  3966. * Sets the sort order to use.
  3967. *
  3968. * @param order
  3969. * a sort order list.
  3970. *
  3971. * @throws IllegalArgumentException
  3972. * if order is null
  3973. */
  3974. public void setSortOrder(List<GridSortOrder<T>> order) {
  3975. setSortOrder(order, false);
  3976. }
  3977. /**
  3978. * Sets the sort order to use, given a {@link GridSortOrderBuilder}.
  3979. * Shorthand for {@code setSortOrder(builder.build())}.
  3980. *
  3981. * @see GridSortOrderBuilder
  3982. *
  3983. * @param builder
  3984. * the sort builder to retrieve the sort order from
  3985. * @throws NullPointerException
  3986. * if builder is null
  3987. */
  3988. public void setSortOrder(GridSortOrderBuilder<T> builder) {
  3989. Objects.requireNonNull(builder, "Sort builder cannot be null");
  3990. setSortOrder(builder.build());
  3991. }
  3992. /**
  3993. * Adds a sort order change listener that gets notified when the sort order
  3994. * changes.
  3995. *
  3996. * @param listener
  3997. * the sort order change listener to add
  3998. */
  3999. @Override
  4000. public Registration addSortListener(
  4001. SortListener<GridSortOrder<T>> listener) {
  4002. return addListener(SortEvent.class, listener, SORT_ORDER_CHANGE_METHOD);
  4003. }
  4004. /**
  4005. * Get the current sort order list.
  4006. *
  4007. * @return a sort order list
  4008. */
  4009. public List<GridSortOrder<T>> getSortOrder() {
  4010. return Collections.unmodifiableList(sortOrder);
  4011. }
  4012. /**
  4013. * Scrolls to a certain item, using {@link ScrollDestination#ANY}.
  4014. * <p>
  4015. * If the item has an open details row, its size will also be taken into
  4016. * account.
  4017. *
  4018. * @param row
  4019. * zero based index of the item to scroll to in the current view.
  4020. * @throws IllegalArgumentException
  4021. * if the provided row is outside the item range
  4022. */
  4023. public void scrollTo(int row) throws IllegalArgumentException {
  4024. scrollTo(row, ScrollDestination.ANY);
  4025. }
  4026. /**
  4027. * Scrolls to a certain item, using user-specified scroll destination.
  4028. * <p>
  4029. * If the item has an open details row, its size will also be taken into
  4030. * account.
  4031. *
  4032. * @param row
  4033. * zero based index of the item to scroll to in the current view.
  4034. * @param destination
  4035. * value specifying desired position of scrolled-to row, not
  4036. * {@code null}
  4037. * @throws IllegalArgumentException
  4038. * if the provided row is outside the item range
  4039. */
  4040. public void scrollTo(int row, ScrollDestination destination) {
  4041. Objects.requireNonNull(destination,
  4042. "ScrollDestination can not be null");
  4043. if (row >= getDataCommunicator().getDataProviderSize()) {
  4044. throw new IllegalArgumentException("Row outside dataProvider size");
  4045. }
  4046. getRpcProxy(GridClientRpc.class).scrollToRow(row, destination);
  4047. }
  4048. /**
  4049. * Scrolls to the beginning of the first data row.
  4050. */
  4051. public void scrollToStart() {
  4052. getRpcProxy(GridClientRpc.class).scrollToStart();
  4053. }
  4054. /**
  4055. * Scrolls to the end of the last data row.
  4056. */
  4057. public void scrollToEnd() {
  4058. getRpcProxy(GridClientRpc.class).scrollToEnd();
  4059. }
  4060. @Override
  4061. protected GridState getState() {
  4062. return getState(true);
  4063. }
  4064. @Override
  4065. protected GridState getState(boolean markAsDirty) {
  4066. return (GridState) super.getState(markAsDirty);
  4067. }
  4068. /**
  4069. * Sets the column resize mode to use. The default mode is
  4070. * {@link ColumnResizeMode#ANIMATED}.
  4071. *
  4072. * @param mode
  4073. * a ColumnResizeMode value
  4074. * @since 7.7.5
  4075. */
  4076. public void setColumnResizeMode(ColumnResizeMode mode) {
  4077. getState().columnResizeMode = mode;
  4078. }
  4079. /**
  4080. * Returns the current column resize mode. The default mode is
  4081. * {@link ColumnResizeMode#ANIMATED}.
  4082. *
  4083. * @return a ColumnResizeMode value
  4084. * @since 7.7.5
  4085. */
  4086. public ColumnResizeMode getColumnResizeMode() {
  4087. return getState(false).columnResizeMode;
  4088. }
  4089. /**
  4090. * Creates a new Editor instance. Can be overridden to create a custom
  4091. * Editor. If the Editor is a {@link AbstractGridExtension}, it will be
  4092. * automatically added to {@link DataCommunicator}.
  4093. *
  4094. * @return editor
  4095. */
  4096. protected Editor<T> createEditor() {
  4097. return new EditorImpl<>(propertySet);
  4098. }
  4099. private void addExtensionComponent(Component c) {
  4100. if (extensionComponents.add(c)) {
  4101. c.setParent(this);
  4102. markAsDirty();
  4103. }
  4104. }
  4105. private void removeExtensionComponent(Component c) {
  4106. if (extensionComponents.remove(c)) {
  4107. c.setParent(null);
  4108. markAsDirty();
  4109. }
  4110. }
  4111. private void fireColumnReorderEvent(boolean userOriginated) {
  4112. fireEvent(new ColumnReorderEvent(this, userOriginated));
  4113. }
  4114. private void fireColumnResizeEvent(Column<?, ?> column,
  4115. boolean userOriginated) {
  4116. fireEvent(new ColumnResizeEvent(this, column, userOriginated));
  4117. }
  4118. @Override
  4119. protected void readItems(Element design, DesignContext context) {
  4120. // Grid handles reading of items in Grid#readData
  4121. }
  4122. @Override
  4123. public DataProvider<T, ?> getDataProvider() {
  4124. return internalGetDataProvider();
  4125. }
  4126. @Override
  4127. public void setDataProvider(DataProvider<T, ?> dataProvider) {
  4128. internalSetDataProvider(dataProvider);
  4129. }
  4130. /**
  4131. * Sets a CallbackDataProvider using the given fetch items callback and a
  4132. * size callback.
  4133. * <p>
  4134. * This method is a shorthand for making a {@link CallbackDataProvider} that
  4135. * handles a partial {@link Query} object.
  4136. *
  4137. * @param fetchItems
  4138. * a callback for fetching items
  4139. * @param sizeCallback
  4140. * a callback for getting the count of items
  4141. *
  4142. * @see CallbackDataProvider
  4143. * @see #setDataProvider(DataProvider)
  4144. */
  4145. public void setDataProvider(FetchItemsCallback<T> fetchItems,
  4146. SerializableSupplier<Integer> sizeCallback) {
  4147. internalSetDataProvider(
  4148. new CallbackDataProvider<>(
  4149. q -> fetchItems.fetchItems(q.getSortOrders(),
  4150. q.getOffset(), q.getLimit()),
  4151. q -> sizeCallback.get()));
  4152. }
  4153. @Override
  4154. protected void doReadDesign(Element design, DesignContext context) {
  4155. Attributes attrs = design.attributes();
  4156. if (design.hasAttr(DECLARATIVE_DATA_ITEM_TYPE)) {
  4157. String itemType = design.attr(DECLARATIVE_DATA_ITEM_TYPE);
  4158. setBeanType(itemType);
  4159. }
  4160. if (attrs.hasKey("selection-mode")) {
  4161. setSelectionMode(DesignAttributeHandler.readAttribute(
  4162. "selection-mode", attrs, SelectionMode.class));
  4163. }
  4164. Attributes attr = design.attributes();
  4165. if (attr.hasKey("selection-allowed")) {
  4166. setReadOnly(DesignAttributeHandler
  4167. .readAttribute("selection-allowed", attr, Boolean.class));
  4168. }
  4169. if (attrs.hasKey("rows")) {
  4170. setHeightByRows(DesignAttributeHandler.readAttribute("rows", attrs,
  4171. double.class));
  4172. }
  4173. readStructure(design, context);
  4174. // Read frozen columns after columns are read.
  4175. if (attrs.hasKey("frozen-columns")) {
  4176. setFrozenColumnCount(DesignAttributeHandler
  4177. .readAttribute("frozen-columns", attrs, int.class));
  4178. }
  4179. }
  4180. /**
  4181. * Sets the bean type to use for property mapping.
  4182. * <p>
  4183. * This method is responsible also for setting or updating the property set
  4184. * so that it matches the given bean type.
  4185. * <p>
  4186. * Protected mostly for Designer needs, typically should not be overridden
  4187. * or even called.
  4188. *
  4189. * @param beanTypeClassName
  4190. * the fully qualified class name of the bean type
  4191. *
  4192. * @since 8.0.3
  4193. */
  4194. @SuppressWarnings("unchecked")
  4195. protected void setBeanType(String beanTypeClassName) {
  4196. setBeanType((Class<T>) resolveClass(beanTypeClassName));
  4197. }
  4198. /**
  4199. * Sets the bean type to use for property mapping.
  4200. * <p>
  4201. * This method is responsible also for setting or updating the property set
  4202. * so that it matches the given bean type.
  4203. * <p>
  4204. * Protected mostly for Designer needs, typically should not be overridden
  4205. * or even called.
  4206. *
  4207. * @param beanType
  4208. * the bean type class
  4209. *
  4210. * @since 8.0.3
  4211. */
  4212. protected void setBeanType(Class<T> beanType) {
  4213. this.beanType = beanType;
  4214. setPropertySet(BeanPropertySet.get(beanType));
  4215. }
  4216. private Class<?> resolveClass(String qualifiedClassName) {
  4217. try {
  4218. Class<?> resolvedClass = Class.forName(qualifiedClassName, true,
  4219. VaadinServiceClassLoaderUtil.findDefaultClassLoader());
  4220. return resolvedClass;
  4221. } catch (ClassNotFoundException | SecurityException e) {
  4222. throw new IllegalArgumentException(
  4223. "Unable to find class " + qualifiedClassName, e);
  4224. }
  4225. }
  4226. @Override
  4227. protected void doWriteDesign(Element design, DesignContext designContext) {
  4228. Attributes attr = design.attributes();
  4229. if (this.beanType != null) {
  4230. design.attr(DECLARATIVE_DATA_ITEM_TYPE,
  4231. this.beanType.getCanonicalName());
  4232. }
  4233. DesignAttributeHandler.writeAttribute("selection-allowed", attr,
  4234. isReadOnly(), false, Boolean.class, designContext);
  4235. Attributes attrs = design.attributes();
  4236. Grid<?> defaultInstance = designContext.getDefaultInstance(this);
  4237. DesignAttributeHandler.writeAttribute("frozen-columns", attrs,
  4238. getFrozenColumnCount(), defaultInstance.getFrozenColumnCount(),
  4239. int.class, designContext);
  4240. if (HeightMode.ROW.equals(getHeightMode())) {
  4241. DesignAttributeHandler.writeAttribute("rows", attrs,
  4242. getHeightByRows(), defaultInstance.getHeightByRows(),
  4243. double.class, designContext);
  4244. }
  4245. SelectionMode mode = getSelectionMode();
  4246. if (mode != null) {
  4247. DesignAttributeHandler.writeAttribute("selection-mode", attrs, mode,
  4248. SelectionMode.SINGLE, SelectionMode.class, designContext);
  4249. }
  4250. writeStructure(design, designContext);
  4251. }
  4252. @Override
  4253. protected T deserializeDeclarativeRepresentation(String item) {
  4254. if (item == null) {
  4255. return super.deserializeDeclarativeRepresentation(
  4256. UUID.randomUUID().toString());
  4257. }
  4258. return super.deserializeDeclarativeRepresentation(new String(item));
  4259. }
  4260. @Override
  4261. protected boolean isReadOnly() {
  4262. SelectionMode selectionMode = getSelectionMode();
  4263. if (SelectionMode.SINGLE.equals(selectionMode)) {
  4264. return asSingleSelect().isReadOnly();
  4265. }
  4266. if (SelectionMode.MULTI.equals(selectionMode)) {
  4267. return asMultiSelect().isReadOnly();
  4268. }
  4269. return false;
  4270. }
  4271. @Override
  4272. protected void setReadOnly(boolean readOnly) {
  4273. SelectionMode selectionMode = getSelectionMode();
  4274. if (SelectionMode.SINGLE.equals(selectionMode)) {
  4275. asSingleSelect().setReadOnly(readOnly);
  4276. } else if (SelectionMode.MULTI.equals(selectionMode)) {
  4277. asMultiSelect().setReadOnly(readOnly);
  4278. }
  4279. }
  4280. private void readStructure(Element design, DesignContext context) {
  4281. if (design.children().isEmpty()) {
  4282. return;
  4283. }
  4284. if (design.children().size() > 1
  4285. || !design.child(0).tagName().equals("table")) {
  4286. throw new DesignException(
  4287. "Grid needs to have a table element as its only child");
  4288. }
  4289. Element table = design.child(0);
  4290. Elements colgroups = table.getElementsByTag("colgroup");
  4291. if (colgroups.size() != 1) {
  4292. throw new DesignException(
  4293. "Table element in declarative Grid needs to have a"
  4294. + " colgroup defining the columns used in Grid");
  4295. }
  4296. List<DeclarativeValueProvider<T>> providers = new ArrayList<>();
  4297. for (Element col : colgroups.get(0).getElementsByTag("col")) {
  4298. String id = DesignAttributeHandler.readAttribute("column-id",
  4299. col.attributes(), null, String.class);
  4300. // If there is a property with a matching name available,
  4301. // map to that
  4302. Optional<PropertyDefinition<T, ?>> property = propertySet
  4303. .getProperties().filter(p -> p.getName().equals(id))
  4304. .findFirst();
  4305. Column<T, ?> column;
  4306. if (property.isPresent()) {
  4307. column = addColumn(id);
  4308. } else {
  4309. DeclarativeValueProvider<T> provider = new DeclarativeValueProvider<>();
  4310. column = createColumn(provider, ValueProvider.identity(),
  4311. new HtmlRenderer());
  4312. addColumn(getGeneratedIdentifier(), column);
  4313. if (id != null) {
  4314. column.setId(id);
  4315. }
  4316. providers.add(provider);
  4317. }
  4318. column.readDesign(col, context);
  4319. }
  4320. for (Element child : table.children()) {
  4321. if (child.tagName().equals("thead")) {
  4322. getHeader().readDesign(child, context);
  4323. } else if (child.tagName().equals("tbody")) {
  4324. readData(child, providers);
  4325. } else if (child.tagName().equals("tfoot")) {
  4326. getFooter().readDesign(child, context);
  4327. }
  4328. }
  4329. // Sync default header captions to column captions
  4330. if (getDefaultHeaderRow() != null) {
  4331. for (Column<T, ?> c : getColumns()) {
  4332. HeaderCell headerCell = getDefaultHeaderRow().getCell(c);
  4333. if (headerCell.getCellType() == GridStaticCellType.TEXT) {
  4334. c.setCaption(headerCell.getText());
  4335. }
  4336. }
  4337. }
  4338. }
  4339. /**
  4340. * Reads the declarative representation of a grid's data from the given
  4341. * element and stores it in the given {@link DeclarativeValueProvider}s.
  4342. * Each member in the list of value providers corresponds to a column in the
  4343. * grid.
  4344. *
  4345. * @since 8.1
  4346. *
  4347. * @param body
  4348. * the element to read data from
  4349. * @param providers
  4350. * list of {@link DeclarativeValueProvider}s to store the data of
  4351. * each column to
  4352. */
  4353. protected void readData(Element body,
  4354. List<DeclarativeValueProvider<T>> providers) {
  4355. getSelectionModel().deselectAll();
  4356. List<T> items = new ArrayList<>();
  4357. List<T> selectedItems = new ArrayList<>();
  4358. for (Element row : body.children()) {
  4359. T item = deserializeDeclarativeRepresentation(row.attr("item"));
  4360. items.add(item);
  4361. if (row.hasAttr("selected")) {
  4362. selectedItems.add(item);
  4363. }
  4364. Elements cells = row.children();
  4365. int i = 0;
  4366. for (Element cell : cells) {
  4367. providers.get(i).addValue(item, cell.html());
  4368. i++;
  4369. }
  4370. }
  4371. setItems(items);
  4372. selectedItems.forEach(getSelectionModel()::select);
  4373. }
  4374. private void writeStructure(Element design, DesignContext designContext) {
  4375. if (getColumns().isEmpty()) {
  4376. return;
  4377. }
  4378. Element tableElement = design.appendElement("table");
  4379. Element colGroup = tableElement.appendElement("colgroup");
  4380. getColumns().forEach(column -> column
  4381. .writeDesign(colGroup.appendElement("col"), designContext));
  4382. // Always write thead. Reads correctly when there no header rows
  4383. getHeader().writeDesign(tableElement.appendElement("thead"),
  4384. designContext);
  4385. if (designContext.shouldWriteData(this)) {
  4386. Element bodyElement = tableElement.appendElement("tbody");
  4387. writeData(bodyElement, designContext);
  4388. }
  4389. if (getFooter().getRowCount() > 0) {
  4390. getFooter().writeDesign(tableElement.appendElement("tfoot"),
  4391. designContext);
  4392. }
  4393. }
  4394. /**
  4395. * Writes the data contained in this grid. Used when serializing a grid to
  4396. * its declarative representation, if
  4397. * {@link DesignContext#shouldWriteData(Component)} returns {@code true} for
  4398. * the grid that is being written.
  4399. *
  4400. * @since 8.1
  4401. *
  4402. * @param body
  4403. * the body element to write the declarative representation of
  4404. * data to
  4405. * @param designContext
  4406. * the design context
  4407. *
  4408. * @since 8.1
  4409. */
  4410. protected void writeData(Element body, DesignContext designContext) {
  4411. getDataProvider().fetch(new Query<>())
  4412. .forEach(item -> writeRow(body, item, designContext));
  4413. }
  4414. private void writeRow(Element container, T item, DesignContext context) {
  4415. Element tableRow = container.appendElement("tr");
  4416. tableRow.attr("item", serializeDeclarativeRepresentation(item));
  4417. if (getSelectionModel().isSelected(item)) {
  4418. tableRow.attr("selected", true);
  4419. }
  4420. for (Column<T, ?> column : getColumns()) {
  4421. Object value = column.valueProvider.apply(item);
  4422. tableRow.appendElement("td")
  4423. .append(Optional.ofNullable(value).map(Object::toString)
  4424. .map(DesignFormatter::encodeForTextNode)
  4425. .orElse(""));
  4426. }
  4427. }
  4428. private SelectionMode getSelectionMode() {
  4429. GridSelectionModel<T> selectionModel = getSelectionModel();
  4430. SelectionMode mode = null;
  4431. if (selectionModel.getClass().equals(SingleSelectionModelImpl.class)) {
  4432. mode = SelectionMode.SINGLE;
  4433. } else if (selectionModel.getClass()
  4434. .equals(MultiSelectionModelImpl.class)) {
  4435. mode = SelectionMode.MULTI;
  4436. } else if (selectionModel.getClass().equals(NoSelectionModel.class)) {
  4437. mode = SelectionMode.NONE;
  4438. }
  4439. return mode;
  4440. }
  4441. /**
  4442. * Sets a user-defined identifier for given column.
  4443. *
  4444. * @see Column#setId(String)
  4445. *
  4446. * @param column
  4447. * the column
  4448. * @param id
  4449. * the user-defined identifier
  4450. */
  4451. protected void setColumnId(String id, Column<T, ?> column) {
  4452. if (columnIds.containsKey(id)) {
  4453. throw new IllegalArgumentException("Duplicate ID for columns");
  4454. }
  4455. columnIds.put(id, column);
  4456. }
  4457. @Override
  4458. protected Collection<String> getCustomAttributes() {
  4459. Collection<String> result = super.getCustomAttributes();
  4460. // "rename" for frozen column count
  4461. result.add("frozen-column-count");
  4462. result.add("frozen-columns");
  4463. // "rename" for height-mode
  4464. result.add("height-by-rows");
  4465. result.add("rows");
  4466. // add a selection-mode attribute
  4467. result.add("selection-mode");
  4468. return result;
  4469. }
  4470. /**
  4471. * Returns a column identified by its internal id. This id should not be
  4472. * confused with the user-defined identifier.
  4473. *
  4474. * @param columnId
  4475. * the internal id of column
  4476. * @return column identified by internal id
  4477. */
  4478. protected Column<T, ?> getColumnByInternalId(String columnId) {
  4479. return columnKeys.get(columnId);
  4480. }
  4481. /**
  4482. * Returns the internal id for given column. This id should not be confused
  4483. * with the user-defined identifier.
  4484. *
  4485. * @param column
  4486. * the column
  4487. * @return internal id of given column
  4488. */
  4489. protected String getInternalIdForColumn(Column<T, ?> column) {
  4490. return column.getInternalId();
  4491. }
  4492. private void setSortOrder(List<GridSortOrder<T>> order,
  4493. boolean userOriginated) {
  4494. Objects.requireNonNull(order, "Sort order list cannot be null");
  4495. // Update client state to display sort order.
  4496. List<String> sortColumns = new ArrayList<>();
  4497. List<SortDirection> directions = new ArrayList<>();
  4498. order.stream().forEach(sortOrder -> {
  4499. sortColumns.add(sortOrder.getSorted().getInternalId());
  4500. directions.add(sortOrder.getDirection());
  4501. });
  4502. getState().sortColumns = sortColumns.toArray(new String[0]);
  4503. getState().sortDirs = directions.toArray(new SortDirection[0]);
  4504. sortOrder.clear();
  4505. sortOrder.addAll(order);
  4506. sort(userOriginated);
  4507. }
  4508. private void sort(boolean userOriginated) {
  4509. // Set sort orders
  4510. // In-memory comparator
  4511. getDataCommunicator().setInMemorySorting(createSortingComparator(),
  4512. false);
  4513. // Back-end sort properties
  4514. List<QuerySortOrder> sortProperties = new ArrayList<>();
  4515. sortOrder.stream().map(
  4516. order -> order.getSorted().getSortOrder(order.getDirection()))
  4517. .forEach(s -> s.forEach(sortProperties::add));
  4518. getDataCommunicator().setBackEndSorting(sortProperties, true);
  4519. // Close grid editor if it's open.
  4520. if (getEditor().isOpen()) {
  4521. getEditor().cancel();
  4522. }
  4523. fireEvent(new SortEvent<>(this, new ArrayList<>(sortOrder),
  4524. userOriginated));
  4525. }
  4526. /**
  4527. * Creates a comparator for grid to sort rows.
  4528. *
  4529. * @return the comparator based on column sorting information.
  4530. */
  4531. protected SerializableComparator<T> createSortingComparator() {
  4532. /*
  4533. * thenComparing is defined to return a serializable comparator as long
  4534. * as both original comparators are also serializable
  4535. */
  4536. BinaryOperator<SerializableComparator<T>> operator = (comparator1,
  4537. comparator2) -> comparator1.thenComparing(comparator2)::compare;
  4538. return sortOrder.stream().map(
  4539. order -> order.getSorted().getComparator(order.getDirection()))
  4540. .reduce((x, y) -> 0, operator);
  4541. }
  4542. @Override
  4543. protected void internalSetDataProvider(DataProvider<T, ?> dataProvider) {
  4544. super.internalSetDataProvider(dataProvider);
  4545. for (Column<T, ?> column : getColumns()) {
  4546. column.updateSortable();
  4547. }
  4548. }
  4549. }