您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Table.java 75KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.ui;
  19. import java.util.Collection;
  20. import java.util.HashMap;
  21. import java.util.HashSet;
  22. import java.util.Iterator;
  23. import java.util.LinkedHashSet;
  24. import java.util.LinkedList;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import java.util.StringTokenizer;
  28. import com.itmill.toolkit.data.Container;
  29. import com.itmill.toolkit.data.Item;
  30. import com.itmill.toolkit.data.Property;
  31. import com.itmill.toolkit.data.util.ContainerOrderedWrapper;
  32. import com.itmill.toolkit.data.util.IndexedContainer;
  33. import com.itmill.toolkit.event.Action;
  34. import com.itmill.toolkit.terminal.KeyMapper;
  35. import com.itmill.toolkit.terminal.PaintException;
  36. import com.itmill.toolkit.terminal.PaintTarget;
  37. import com.itmill.toolkit.terminal.Resource;
  38. import com.itmill.toolkit.terminal.Sizeable;
  39. /**
  40. * Table component is used for representing data or components in pageable and
  41. * selectable table.
  42. *
  43. * @author IT Mill Ltd.
  44. * @version @VERSION@
  45. * @since 3.0
  46. */
  47. public class Table extends Select implements Action.Container,
  48. Container.Ordered, Container.Sortable, Sizeable {
  49. private static final int CELL_KEY = 0;
  50. private static final int CELL_HEADER = 1;
  51. private static final int CELL_ICON = 2;
  52. private static final int CELL_ITEMID = 3;
  53. private static final int CELL_FIRSTCOL = 4;
  54. /** Width of the table or -1 if unspecified */
  55. private int width = -1;
  56. /** Height of the table or -1 if unspecified */
  57. private int height = -1;
  58. /** Width unit */
  59. private int widthUnit = Sizeable.UNITS_PIXELS;
  60. /** Height unit */
  61. private int heightUnit = Sizeable.UNITS_PIXELS;
  62. /**
  63. * Left column alignment. <b>This is the default behaviour. </b>
  64. */
  65. public static final String ALIGN_LEFT = "b";
  66. /** Center column alignment. */
  67. public static final String ALIGN_CENTER = "c";
  68. /** Right column alignment. */
  69. public static final String ALIGN_RIGHT = "e";
  70. /**
  71. * Column header mode: Column headers are hidden. <b>This is the default
  72. * behaviour. </b>
  73. */
  74. public static final int COLUMN_HEADER_MODE_HIDDEN = -1;
  75. /**
  76. * Column header mode: Property ID:s are used as column headers.
  77. */
  78. public static final int COLUMN_HEADER_MODE_ID = 0;
  79. /**
  80. * Column header mode: Column headers are explicitly specified with
  81. * <code>setColumnHeaders()</code>
  82. */
  83. public static final int COLUMN_HEADER_MODE_EXPLICIT = 1;
  84. /**
  85. * Column header mode: Column headers are explicitly specified with
  86. * <code>setColumnHeaders()</code>
  87. */
  88. public static final int COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID = 2;
  89. /**
  90. * Row caption mode: The row headers are hidden. <b>This is the default
  91. * mode. </b>
  92. */
  93. public static final int ROW_HEADER_MODE_HIDDEN = -1;
  94. /**
  95. * Row caption mode: Items Id-objects toString() is used as row caption.
  96. */
  97. public static final int ROW_HEADER_MODE_ID = Select.ITEM_CAPTION_MODE_ID;
  98. /**
  99. * Row caption mode: Item-objects toString() is used as row caption.
  100. */
  101. public static final int ROW_HEADER_MODE_ITEM = Select.ITEM_CAPTION_MODE_ITEM;
  102. /**
  103. * Row caption mode: Index of the item is used as item caption. * The index
  104. * mode can only be used with the containers implementing Container.Indexed
  105. * interface.
  106. */
  107. public static final int ROW_HEADER_MODE_INDEX = Select.ITEM_CAPTION_MODE_INDEX;
  108. /**
  109. * Row caption mode: Item captions are explicitly specified.
  110. */
  111. public static final int ROW_HEADER_MODE_EXPLICIT = Select.ITEM_CAPTION_MODE_EXPLICIT;
  112. /**
  113. * Row caption mode: Item captions are read from property specified with
  114. * <code>setItemCaptionPropertyId</code>.
  115. */
  116. public static final int ROW_HEADER_MODE_PROPERTY = Select.ITEM_CAPTION_MODE_PROPERTY;
  117. /**
  118. * Row caption mode: Only icons are shown, the captions are hidden.
  119. */
  120. public static final int ROW_HEADER_MODE_ICON_ONLY = Select.ITEM_CAPTION_MODE_ICON_ONLY;
  121. /**
  122. * Row caption mode: Item captions are explicitly specified, but if the
  123. * caption is missing, the item id objects <code>toString()</code> is used
  124. * instead.
  125. */
  126. public static final int ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID = Select.ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID;
  127. /* Private table extensions to Select *********************************** */
  128. /** True if column collapsing is allowed */
  129. private boolean columnCollapsingAllowed = false;
  130. /** True if reordering of columns is allowed on the client side */
  131. private boolean columnReorderingAllowed = false;
  132. /** Keymapper for column ids */
  133. private KeyMapper columnIdMap = new KeyMapper();
  134. /** Holds visible column propertyIds - in order */
  135. private LinkedList visibleColumns = new LinkedList();
  136. /** Holds propertyIds of currently collapsed columns */
  137. private HashSet collapsedColumns = new HashSet();
  138. /** Holds headers for visible columns (by propertyId) */
  139. private HashMap columnHeaders = new HashMap();
  140. /** Holds icons for visible columns (by propertyId) */
  141. private HashMap columnIcons = new HashMap();
  142. /** Holds alignments for visible columns (by propertyId) */
  143. private HashMap columnAlignments = new HashMap();
  144. /** Holds value of property pageLength. 0 disables paging. */
  145. private int pageLength = 0;
  146. /** Id the first item on the current page. */
  147. private Object currentPageFirstItemId = null;
  148. /** Index of the first item on the current page. */
  149. private int currentPageFirstItemIndex = 0;
  150. /** Holds value of property pageBuffering. */
  151. private boolean pageBuffering = false;
  152. /** Holds value of property selectable. */
  153. private boolean selectable = false;
  154. /** Holds value of property columnHeaderMode. */
  155. private int columnHeaderMode = COLUMN_HEADER_MODE_HIDDEN;
  156. /** True iff the row captions are hidden. */
  157. private boolean rowCaptionsAreHidden = true;
  158. /** Page contents buffer used in buffered mode */
  159. private Object[][] pageBuffer = null;
  160. /**
  161. * List of properties listened - the list is kept to release the listeners
  162. * later.
  163. */
  164. private LinkedList listenedProperties = null;
  165. /** List of visible components - the is used for needsRepaint calculation. */
  166. private LinkedList visibleComponents = null;
  167. /** List of action handlers */
  168. private LinkedList actionHandlers = null;
  169. /** Action mapper */
  170. private KeyMapper actionMapper = null;
  171. /** Table cell editor factory */
  172. private FieldFactory fieldFactory = new BaseFieldFactory();
  173. /** Is table editable */
  174. private boolean editable = false;
  175. /** Current sorting direction */
  176. private boolean sortAscending = true;
  177. /** Currently table is sorted on this propertyId */
  178. private Object sortContainerPropertyId = null;
  179. /** Is table sorting disabled alltogether; even if some of the properties would be
  180. * sortable. */
  181. private boolean sortDisabled = false;
  182. /** Number of rows explicitly requested by the client to be painted on next paint.
  183. * This is -1 if no request by the client is made. Painting the component will automatically
  184. * reset this to -1.
  185. */
  186. private int reqRowsToPaint = -1;
  187. /** Index of the first rows explicitly requested by the client to be painted.
  188. * This is -1 if no request by the client is made. Painting the component will automatically
  189. * reset this to -1.
  190. */
  191. private int reqFirstRowToPaint = -1;
  192. /* Table constructors *************************************************** */
  193. /** Create new empty table */
  194. public Table() {
  195. setRowHeaderMode(ROW_HEADER_MODE_HIDDEN);
  196. }
  197. /** Create new empty table with caption. */
  198. public Table(String caption) {
  199. this();
  200. setCaption(caption);
  201. }
  202. /** Create new table with caption and connect it to a Container. */
  203. public Table(String caption, Container dataSource) {
  204. this();
  205. setCaption(caption);
  206. setContainerDataSource(dataSource);
  207. }
  208. /* Table functionality ************************************************** */
  209. /**
  210. * Get the array of visible column property id:s.
  211. *
  212. * <p>
  213. * The columns are show in the order of their appearance in this array
  214. * </p>
  215. *
  216. * @return Value of property availableColumns.
  217. */
  218. public Object[] getVisibleColumns() {
  219. if (this.visibleColumns == null) {
  220. return null;
  221. }
  222. return this.visibleColumns.toArray();
  223. }
  224. /**
  225. * Set the array of visible column property id:s.
  226. *
  227. * <p>
  228. * The columns are show in the order of their appearance in this array
  229. * </p>
  230. *
  231. * @param availableColumns
  232. * Array of shown property id:s.
  233. */
  234. public void setVisibleColumns(Object[] visibleColumns) {
  235. // Visible columns must exist
  236. if (visibleColumns == null)
  237. throw new NullPointerException(
  238. "Can not set visible columns to null value");
  239. // Check that the new visible columns contains no nulls and properties
  240. // exist
  241. Collection properties = getContainerPropertyIds();
  242. for (int i = 0; i < visibleColumns.length; i++) {
  243. if (visibleColumns[i] == null)
  244. throw new NullPointerException("Properties must be non-nulls");
  245. else if (!properties.contains(visibleColumns[i]))
  246. throw new IllegalArgumentException(
  247. "Properties must exist in the Container, missing property: "
  248. + visibleColumns[i]);
  249. }
  250. // If this is called befor the constructor is finished, it might be
  251. // uninitialized
  252. LinkedList newVC = new LinkedList();
  253. for (int i = 0; i < visibleColumns.length; i++) {
  254. newVC.add(visibleColumns[i]);
  255. }
  256. // Remove alignments, icons and headers from hidden columns
  257. if (this.visibleColumns != null)
  258. for (Iterator i = this.visibleColumns.iterator(); i.hasNext();) {
  259. Object col = i.next();
  260. if (!newVC.contains(col)) {
  261. setColumnHeader(col, null);
  262. setColumnAlignment(col, null);
  263. setColumnIcon(col, null);
  264. }
  265. }
  266. this.visibleColumns = newVC;
  267. // Assure visual refresh
  268. refreshCurrentPage();
  269. }
  270. /**
  271. * Get the headers of the columns.
  272. *
  273. * <p>
  274. * The headers match the property id:s given my the set visible column
  275. * headers. The table must be set in either
  276. * <code>ROW_HEADER_MODE_EXPLICIT</code> or
  277. * <code>ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID</code> mode to show the
  278. * headers. In the defaults mode any nulls in the headers array are replaced
  279. * with id.toString() outputs when rendering.
  280. * </p>
  281. *
  282. * @return Array of column headers.
  283. */
  284. public String[] getColumnHeaders() {
  285. if (this.columnHeaders == null) {
  286. return null;
  287. }
  288. String[] headers = new String[this.visibleColumns.size()];
  289. int i = 0;
  290. for (Iterator it = this.visibleColumns.iterator(); it.hasNext(); i++) {
  291. headers[i] = (String) this.columnHeaders.get(it.next());
  292. }
  293. return headers;
  294. }
  295. /**
  296. * Set the headers of the columns.
  297. *
  298. * <p>
  299. * The headers match the property id:s given my the set visible column
  300. * headers. The table must be set in either
  301. * <code>ROW_HEADER_MODE_EXPLICIT</code> or
  302. * <code>ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID</code> mode to show the
  303. * headers. In the defaults mode any nulls in the headers array are replaced
  304. * with id.toString() outputs when rendering.
  305. * </p>
  306. *
  307. * @param columnHeaders
  308. * Array of column headers that match the
  309. * <code>getVisibleColumns()</code>.
  310. */
  311. public void setColumnHeaders(String[] columnHeaders) {
  312. if (columnHeaders.length != this.visibleColumns.size())
  313. throw new IllegalArgumentException(
  314. "The length of the headers array must match the number of visible columns");
  315. this.columnHeaders.clear();
  316. int i = 0;
  317. for (Iterator it = this.visibleColumns.iterator(); it.hasNext()
  318. && i < columnHeaders.length; i++) {
  319. this.columnHeaders.put(it.next(), columnHeaders[i]);
  320. }
  321. // Assure visual refresh
  322. refreshCurrentPage();
  323. }
  324. /**
  325. * Get the icons of the columns.
  326. *
  327. * <p>
  328. * The icons in headers match the property id:s given my the set visible
  329. * column headers. The table must be set in either
  330. * <code>ROW_HEADER_MODE_EXPLICIT</code> or
  331. * <code>ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID</code> mode to show the
  332. * headers with icons.
  333. * </p>
  334. *
  335. * @return Array of icons that match the <code>getVisibleColumns()</code>.
  336. */
  337. public Resource[] getColumnIcons() {
  338. if (this.columnIcons == null) {
  339. return null;
  340. }
  341. Resource[] icons = new Resource[this.visibleColumns.size()];
  342. int i = 0;
  343. for (Iterator it = this.visibleColumns.iterator(); it.hasNext(); i++) {
  344. icons[i] = (Resource) this.columnIcons.get(it.next());
  345. }
  346. return icons;
  347. }
  348. /**
  349. * Set the icons of the columns.
  350. *
  351. * <p>
  352. * The icons in headers match the property id:s given my the set visible
  353. * column headers. The table must be set in either
  354. * <code>ROW_HEADER_MODE_EXPLICIT</code> or
  355. * <code>ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID</code> mode to show the
  356. * headers with icons.
  357. * </p>
  358. *
  359. * @param columnIcons
  360. * Array of icons that match the <code>getVisibleColumns()</code>.
  361. */
  362. public void setColumnIcons(Resource[] columnIcons) {
  363. if (columnIcons.length != this.visibleColumns.size())
  364. throw new IllegalArgumentException(
  365. "The length of the icons array must match the number of visible columns");
  366. this.columnIcons.clear();
  367. int i = 0;
  368. for (Iterator it = this.visibleColumns.iterator(); it.hasNext()
  369. && i < columnIcons.length; i++) {
  370. this.columnIcons.put(it.next(), columnIcons[i]);
  371. }
  372. // Assure visual refresh
  373. refreshCurrentPage();
  374. }
  375. /**
  376. * Get array of column alignments.
  377. *
  378. * <p>
  379. * The items in the array must match the properties identified by
  380. * <code>getVisibleColumns()</code>. The possible values for the
  381. * alignments include:
  382. * <ul>
  383. * <li><code>ALIGN_LEFT</code>: Left alignment</li>
  384. * <li><code>ALIGN_CENTER</code>: Centered</li>
  385. * <li><code>ALIGN_RIGHT</code>: Right alignment</li>
  386. * </ul>
  387. * The alignments default to <code>ALIGN_LEFT</code>: any null values are
  388. * rendered as align lefts.
  389. * </p>
  390. *
  391. * @return Column alignments array.
  392. */
  393. public String[] getColumnAlignments() {
  394. if (this.columnAlignments == null) {
  395. return null;
  396. }
  397. String[] alignments = new String[this.visibleColumns.size()];
  398. int i = 0;
  399. for (Iterator it = this.visibleColumns.iterator(); it.hasNext(); i++) {
  400. alignments[i++] = getColumnAlignment(it.next());
  401. }
  402. return alignments;
  403. }
  404. /**
  405. * Set the column alignments.
  406. *
  407. * <p>
  408. * The items in the array must match the properties identified by
  409. * <code>getVisibleColumns()</code>. The possible values for the
  410. * alignments include:
  411. * <ul>
  412. * <li><code>ALIGN_LEFT</code>: Left alignment</li>
  413. * <li><code>ALIGN_CENTER</code>: Centered</li>
  414. * <li><code>ALIGN_RIGHT</code>: Right alignment</li>
  415. * </ul>
  416. * The alignments default to <code>ALIGN_LEFT</code>
  417. * </p>
  418. *
  419. * @param columnAlignments
  420. * Column alignments array.
  421. */
  422. public void setColumnAlignments(String[] columnAlignments) {
  423. if (columnAlignments.length != this.visibleColumns.size())
  424. throw new IllegalArgumentException(
  425. "The length of the alignments array must match the number of visible columns");
  426. // Check all alignments
  427. for (int i = 0; i < columnAlignments.length; i++) {
  428. String a = columnAlignments[i];
  429. if (a != null && !a.equals(ALIGN_LEFT) && !a.equals(ALIGN_CENTER)
  430. && !a.equals(ALIGN_RIGHT))
  431. throw new IllegalArgumentException("Column " + i
  432. + " aligment '" + a + "' is invalid");
  433. }
  434. // Reset alignments
  435. HashMap newCA = new HashMap();
  436. int i = 0;
  437. for (Iterator it = this.visibleColumns.iterator(); it.hasNext()
  438. && i < columnAlignments.length; i++) {
  439. newCA.put(it.next(), columnAlignments[i]);
  440. }
  441. this.columnAlignments = newCA;
  442. // Assure visual refresh
  443. refreshCurrentPage();
  444. }
  445. /**
  446. * Get the page length.
  447. *
  448. * <p>
  449. * Setting page length 0 disables paging.
  450. * </p>
  451. *
  452. * @return Lenght of one page.
  453. */
  454. public int getPageLength() {
  455. return this.pageLength;
  456. }
  457. /**
  458. * Set the page length.
  459. *
  460. * <p>
  461. * Setting page length 0 disables paging. The page length defaults to 0 (no
  462. * paging).
  463. * </p>
  464. *
  465. * @param Lenght
  466. * of one page.
  467. */
  468. public void setPageLength(int pageLength) {
  469. if (pageLength >= 0 && this.pageLength != pageLength) {
  470. this.pageLength = pageLength;
  471. // Assure visual refresh
  472. refreshCurrentPage();
  473. }
  474. }
  475. /**
  476. * Getter for property currentPageFirstItem.
  477. *
  478. * @return Value of property currentPageFirstItem.
  479. */
  480. public Object getCurrentPageFirstItemId() {
  481. // Priorise index over id if indexes are supported
  482. if (items instanceof Container.Indexed) {
  483. int index = getCurrentPageFirstItemIndex();
  484. Object id = null;
  485. if (index >= 0 && index < size())
  486. id = ((Container.Indexed) items).getIdByIndex(index);
  487. if (id != null && !id.equals(currentPageFirstItemId))
  488. currentPageFirstItemId = id;
  489. }
  490. // If there is no item id at all, use the first one
  491. if (currentPageFirstItemId == null)
  492. currentPageFirstItemId = ((Container.Ordered) items).firstItemId();
  493. return currentPageFirstItemId;
  494. }
  495. /**
  496. * Setter for property currentPageFirstItem.
  497. *
  498. * @param currentPageFirstItem
  499. * New value of property currentPageFirstItem.
  500. */
  501. public void setCurrentPageFirstItemId(Object currentPageFirstItemId) {
  502. // Get the corresponding index
  503. int index = -1;
  504. try {
  505. index = ((Container.Indexed) items)
  506. .indexOfId(currentPageFirstItemId);
  507. } catch (ClassCastException e) {
  508. // If the table item container does not have index, we have to
  509. // calculate the index by hand
  510. Object id = ((Container.Ordered) items).firstItemId();
  511. while (id != null && !id.equals(currentPageFirstItemId)) {
  512. index++;
  513. id = ((Container.Ordered) items).nextItemId(id);
  514. }
  515. if (id == null)
  516. index = -1;
  517. }
  518. // If the search for item index was successfull
  519. if (index >= 0) {
  520. this.currentPageFirstItemId = currentPageFirstItemId;
  521. this.currentPageFirstItemIndex = index;
  522. }
  523. // Assure visual refresh
  524. refreshCurrentPage();
  525. }
  526. /**
  527. * Gets the icon Resource for the specified column.
  528. *
  529. * @param propertyId
  530. * the propertyId indentifying the column.
  531. * @return the icon for the specified column; null if the column has no icon
  532. * set, or if the column is not visible.
  533. */
  534. public Resource getColumnIcon(Object propertyId) {
  535. return (Resource) this.columnIcons.get(propertyId);
  536. }
  537. /**
  538. * Sets the icon Resource for the specified column.
  539. * <p>
  540. * Throws IllegalArgumentException if the specified column is not visible.
  541. * </p>
  542. *
  543. * @param propertyId
  544. * the propertyId identifying the column.
  545. * @param icon
  546. * the icon Resource to set.
  547. */
  548. public void setColumnIcon(Object propertyId, Resource icon) {
  549. if (icon == null)
  550. this.columnIcons.remove(propertyId);
  551. else
  552. this.columnIcons.put(propertyId, icon);
  553. // Assure visual refresh
  554. refreshCurrentPage();
  555. }
  556. /**
  557. * Gets the header for the specified column.
  558. *
  559. * @param propertyId
  560. * the propertyId indentifying the column.
  561. * @return the header for the specifed column if it has one.
  562. */
  563. public String getColumnHeader(Object propertyId) {
  564. if (getColumnHeaderMode() == COLUMN_HEADER_MODE_HIDDEN)
  565. return null;
  566. String header = (String) this.columnHeaders.get(propertyId);
  567. if ((header == null && this.getColumnHeaderMode() == COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID)
  568. || this.getColumnHeaderMode() == COLUMN_HEADER_MODE_ID) {
  569. header = propertyId.toString();
  570. }
  571. return header;
  572. }
  573. /**
  574. * Sets column header for the specified column;
  575. *
  576. * @param propertyId
  577. * the propertyId indentifying the column.
  578. * @param header
  579. * the header to set.
  580. */
  581. public void setColumnHeader(Object propertyId, String header) {
  582. if (header == null) {
  583. this.columnHeaders.remove(propertyId);
  584. return;
  585. }
  586. this.columnHeaders.put(propertyId, header);
  587. // Assure visual refresh
  588. refreshCurrentPage();
  589. }
  590. /**
  591. * Gets the specified column's alignment.
  592. *
  593. * @param propertyId
  594. * the propertyID identifying the column.
  595. * @return the specified column's alignment if it as one; null otherwise.
  596. */
  597. public String getColumnAlignment(Object propertyId) {
  598. String a = (String) this.columnAlignments.get(propertyId);
  599. return a == null ? ALIGN_LEFT : a;
  600. }
  601. /**
  602. * Sets the specified column's alignment.
  603. *
  604. * <p>
  605. * Throws IllegalArgumentException if the alignment is not one of the
  606. * following: ALIGN_LEFT, ALIGN_CENTER or ALIGN_RIGHT
  607. * </p>
  608. *
  609. * @param propertyId
  610. * the propertyID identifying the column.
  611. * @param alignment
  612. * the desired alignment.
  613. */
  614. public void setColumnAlignment(Object propertyId, String alignment) {
  615. // Check for valid alignments
  616. if (alignment != null && !alignment.equals(ALIGN_LEFT)
  617. && !alignment.equals(ALIGN_CENTER)
  618. && !alignment.equals(ALIGN_RIGHT))
  619. throw new IllegalArgumentException("Column alignment '" + alignment
  620. + "' is not supported.");
  621. if (alignment == null || alignment.equals(ALIGN_LEFT)) {
  622. this.columnAlignments.remove(propertyId);
  623. return;
  624. }
  625. this.columnAlignments.put(propertyId, alignment);
  626. // Assure visual refresh
  627. refreshCurrentPage();
  628. }
  629. /**
  630. * Checks if the specified column is collapsed.
  631. *
  632. * @param propertyId
  633. * the propertyID identifying the column.
  634. * @return true if the column is collapsed; false otherwise;
  635. */
  636. public boolean isColumnCollapsed(Object propertyId) {
  637. return collapsedColumns != null
  638. && collapsedColumns.contains(propertyId);
  639. }
  640. /**
  641. * Sets whether the specified column is collapsed or not.
  642. *
  643. *
  644. * @param propertyId
  645. * the propertyID identifying the column.
  646. * @param collapsed
  647. * the desired collapsedness.
  648. */
  649. public void setColumnCollapsed(Object propertyId, boolean collapsed)
  650. throws IllegalAccessException {
  651. if (!this.isColumnCollapsingAllowed()) {
  652. throw new IllegalAccessException("Column collapsing not allowed!");
  653. }
  654. if (collapsed)
  655. this.collapsedColumns.add(propertyId);
  656. else
  657. this.collapsedColumns.remove(propertyId);
  658. // Assure visual refresh
  659. refreshCurrentPage();
  660. }
  661. /**
  662. * Check if column collapsing is allowed.
  663. *
  664. * @return true if columns can be collapsed; false otherwise.
  665. */
  666. public boolean isColumnCollapsingAllowed() {
  667. return this.columnCollapsingAllowed;
  668. }
  669. /**
  670. * Sets whether column collapsing is allowed or not.
  671. *
  672. * @param collapsingAllowed
  673. * specifies whether column collapsing is allowed.
  674. */
  675. public void setColumnCollapsingAllowed(boolean collapsingAllowed) {
  676. this.columnCollapsingAllowed = collapsingAllowed;
  677. if (!collapsingAllowed)
  678. collapsedColumns.clear();
  679. // Assure visual refresh
  680. refreshCurrentPage();
  681. }
  682. /**
  683. * Check if column reordering is allowed.
  684. *
  685. * @return true if columns can be reordered; false otherwise.
  686. */
  687. public boolean isColumnReorderingAllowed() {
  688. return this.columnReorderingAllowed;
  689. }
  690. /**
  691. * Sets whether column reordering is allowed or not.
  692. *
  693. * @param reorderingAllowed
  694. * specifies whether column reordering is allowed.
  695. */
  696. public void setColumnReorderingAllowed(boolean reorderingAllowed) {
  697. this.columnReorderingAllowed = reorderingAllowed;
  698. // Assure visual refresh
  699. refreshCurrentPage();
  700. }
  701. /*
  702. * Arranges visible columns according to given columnOrder. Silently ignores
  703. * colimnId:s that are not visible columns, and keeps the internal order of
  704. * visible columns left out of the ordering (trailing). Silently does
  705. * nothing if columnReordering is not allowed.
  706. */
  707. private void setColumnOrder(Object[] columnOrder) {
  708. if (columnOrder == null || !this.isColumnReorderingAllowed()) {
  709. return;
  710. }
  711. LinkedList newOrder = new LinkedList();
  712. for (int i = 0; i < columnOrder.length; i++) {
  713. if (columnOrder[i] != null
  714. && this.visibleColumns.contains(columnOrder[i])) {
  715. this.visibleColumns.remove(columnOrder[i]);
  716. newOrder.add(columnOrder[i]);
  717. }
  718. }
  719. for (Iterator it = this.visibleColumns.iterator(); it.hasNext();) {
  720. Object columnId = it.next();
  721. if (!newOrder.contains(columnId))
  722. newOrder.add(columnId);
  723. }
  724. this.visibleColumns = newOrder;
  725. // Assure visual refresh
  726. refreshCurrentPage();
  727. }
  728. /**
  729. * Getter for property currentPageFirstItem.
  730. *
  731. * @return Value of property currentPageFirstItem.
  732. */
  733. public int getCurrentPageFirstItemIndex() {
  734. return this.currentPageFirstItemIndex;
  735. }
  736. /**
  737. * Setter for property currentPageFirstItem.
  738. *
  739. * @param newIndex
  740. * New value of property currentPageFirstItem.
  741. */
  742. public void setCurrentPageFirstItemIndex(int newIndex) {
  743. // Ensure that the new value is valid
  744. if (newIndex < 0)
  745. newIndex = 0;
  746. if (newIndex >= size())
  747. newIndex = size() - 1;
  748. // Refresh first item id
  749. if (items instanceof Container.Indexed) {
  750. try {
  751. currentPageFirstItemId = ((Container.Indexed) items)
  752. .getIdByIndex(newIndex);
  753. } catch (IndexOutOfBoundsException e) {
  754. currentPageFirstItemId = null;
  755. }
  756. this.currentPageFirstItemIndex = newIndex;
  757. } else {
  758. // For containers not supporting indexes, we must iterate the
  759. // container forwards / backwards
  760. // next available item forward or backward
  761. this.currentPageFirstItemId = ((Container.Ordered) items).firstItemId();
  762. // Go forwards in the middle of the list (respect borders)
  763. while (this.currentPageFirstItemIndex < newIndex
  764. && !((Container.Ordered) items)
  765. .isLastId(currentPageFirstItemId)) {
  766. this.currentPageFirstItemIndex++;
  767. currentPageFirstItemId = ((Container.Ordered) items)
  768. .nextItemId(currentPageFirstItemId);
  769. }
  770. // If we did hit the border
  771. if (((Container.Ordered) items).isLastId(currentPageFirstItemId)) {
  772. this.currentPageFirstItemIndex = size() - 1;
  773. }
  774. // Go backwards in the middle of the list (respect borders)
  775. while (this.currentPageFirstItemIndex > newIndex
  776. && !((Container.Ordered) items)
  777. .isFirstId(currentPageFirstItemId)) {
  778. this.currentPageFirstItemIndex--;
  779. currentPageFirstItemId = ((Container.Ordered) items)
  780. .prevItemId(currentPageFirstItemId);
  781. }
  782. // If we did hit the border
  783. if (((Container.Ordered) items).isFirstId(currentPageFirstItemId)) {
  784. this.currentPageFirstItemIndex = 0;
  785. }
  786. // Go forwards once more
  787. while (this.currentPageFirstItemIndex < newIndex
  788. && !((Container.Ordered) items)
  789. .isLastId(currentPageFirstItemId)) {
  790. this.currentPageFirstItemIndex++;
  791. currentPageFirstItemId = ((Container.Ordered) items)
  792. .nextItemId(currentPageFirstItemId);
  793. }
  794. // If for some reason we do hit border again, override
  795. // the user index request
  796. if (((Container.Ordered) items).isLastId(currentPageFirstItemId)) {
  797. newIndex = this.currentPageFirstItemIndex = size() - 1;
  798. }
  799. }
  800. // Assure visual refresh
  801. refreshCurrentPage();
  802. }
  803. /**
  804. * Getter for property pageBuffering.
  805. *
  806. * @return Value of property pageBuffering.
  807. */
  808. public boolean isPageBufferingEnabled() {
  809. return this.pageBuffering;
  810. }
  811. /**
  812. * Setter for property pageBuffering.
  813. *
  814. * @param pageBuffering
  815. * New value of property pageBuffering.
  816. */
  817. public void setPageBufferingEnabled(boolean pageBuffering) {
  818. this.pageBuffering = pageBuffering;
  819. // If page buffering is disabled, clear the buffer
  820. if (!pageBuffering)
  821. pageBuffer = null;
  822. }
  823. /**
  824. * Getter for property selectable.
  825. *
  826. * <p>
  827. * The table is not selectable by default.
  828. * </p>
  829. *
  830. * @return Value of property selectable.
  831. */
  832. public boolean isSelectable() {
  833. return this.selectable;
  834. }
  835. /**
  836. * Setter for property selectable.
  837. *
  838. * <p>
  839. * The table is not selectable by default.
  840. * </p>
  841. *
  842. * @param selectable
  843. * New value of property selectable.
  844. */
  845. public void setSelectable(boolean selectable) {
  846. if (this.selectable != selectable) {
  847. this.selectable = selectable;
  848. requestRepaint();
  849. }
  850. }
  851. /**
  852. * Getter for property columnHeaderMode.
  853. *
  854. * @return Value of property columnHeaderMode.
  855. */
  856. public int getColumnHeaderMode() {
  857. return this.columnHeaderMode;
  858. }
  859. /**
  860. * Setter for property columnHeaderMode.
  861. *
  862. * @param columnHeaderMode
  863. * New value of property columnHeaderMode.
  864. */
  865. public void setColumnHeaderMode(int columnHeaderMode) {
  866. if (columnHeaderMode >= COLUMN_HEADER_MODE_HIDDEN
  867. && columnHeaderMode <= COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID)
  868. this.columnHeaderMode = columnHeaderMode;
  869. // Assure visual refresh
  870. refreshCurrentPage();
  871. }
  872. /**
  873. * Refresh the current page contents. If the page buffering is turned off,
  874. * it is not necessary to call this explicitely.
  875. */
  876. public void refreshCurrentPage() {
  877. // Clear page buffer and notify about the change
  878. pageBuffer = null;
  879. requestRepaint();
  880. }
  881. /**
  882. * Set the row header mode.
  883. * <p>
  884. * The mode can be one of the following ones:
  885. * <ul>
  886. * <li><code>ROW_HEADER_MODE_HIDDEN</code>: The row captions are hidden.
  887. * </li>
  888. * <li><code>ROW_HEADER_MODE_ID</code>: Items Id-objects
  889. * <code>toString()</code> is used as row caption.
  890. * <li><code>ROW_HEADER_MODE_ITEM</code>: Item-objects
  891. * <code>toString()</code> is used as row caption.
  892. * <li><code>ROW_HEADER_MODE_PROPERTY</code>: Property set with
  893. * <code>setItemCaptionPropertyId()</code> is used as row header.
  894. * <li><code>ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID</code>: Items
  895. * Id-objects <code>toString()</code> is used as row header. If caption is
  896. * explicitly specified, it overrides the id-caption.
  897. * <li><code>ROW_HEADER_MODE_EXPLICIT</code>: The row headers must be
  898. * explicitly specified.</li>
  899. * <li><code>ROW_HEADER_MODE_INDEX</code>: The index of the item is used
  900. * as row caption. The index mode can only be used with the containers
  901. * implementing <code>Container.Indexed</code> interface.</li>
  902. * </ul>
  903. * The default value is <code>ROW_HEADER_MODE_HIDDEN</code>
  904. * </p>
  905. *
  906. * @param mode
  907. * One of the modes listed above.
  908. */
  909. public void setRowHeaderMode(int mode) {
  910. if (ROW_HEADER_MODE_HIDDEN == mode)
  911. rowCaptionsAreHidden = true;
  912. else {
  913. rowCaptionsAreHidden = false;
  914. setItemCaptionMode(mode);
  915. }
  916. // Assure visual refresh
  917. refreshCurrentPage();
  918. }
  919. /**
  920. * Get the row header mode.
  921. *
  922. * @return Row header mode.
  923. * @see #setRowHeaderMode(int)
  924. */
  925. public int getRowHeaderMode() {
  926. return rowCaptionsAreHidden ? ROW_HEADER_MODE_HIDDEN
  927. : getItemCaptionMode();
  928. }
  929. /**
  930. * Add new row to table and fill the visible cells with given values.
  931. *
  932. * @param cells
  933. * Object array that is used for filling the visible cells new
  934. * row. The types must be settable to visible column property
  935. * types.
  936. * @param itemId
  937. * Id the new row. If null, a new id is automatically assigned.
  938. * If given, the table cant already have a item with given id.
  939. * @return Returns item id for the new row. Returns null if operation fails.
  940. */
  941. public Object addItem(Object[] cells, Object itemId)
  942. throws UnsupportedOperationException {
  943. Object[] cols = getVisibleColumns();
  944. // Check that a correct number of cells are given
  945. if (cells.length != cols.length)
  946. return null;
  947. // Create new item
  948. Item item;
  949. if (itemId == null) {
  950. itemId = items.addItem();
  951. if (itemId == null)
  952. return null;
  953. item = items.getItem(itemId);
  954. } else
  955. item = items.addItem(itemId);
  956. if (item == null)
  957. return null;
  958. // Fill the item properties
  959. for (int i = 0; i < cols.length; i++)
  960. item.getItemProperty(cols[i]).setValue(cells[i]);
  961. return itemId;
  962. }
  963. /* Overriding select behavior******************************************** */
  964. /**
  965. * @see com.itmill.toolkit.data.Container.Viewer#setContainerDataSource(Container)
  966. */
  967. public void setContainerDataSource(Container newDataSource) {
  968. if (newDataSource == null)
  969. newDataSource = new IndexedContainer();
  970. // Assure that the data source is ordered by making unordered
  971. // containers ordered by wrapping them
  972. if (newDataSource instanceof Container.Ordered)
  973. super.setContainerDataSource(newDataSource);
  974. else
  975. super.setContainerDataSource(new ContainerOrderedWrapper(
  976. newDataSource));
  977. // Reset page position
  978. currentPageFirstItemId = null;
  979. currentPageFirstItemIndex = 0;
  980. // Reset column properties
  981. if (this.collapsedColumns != null)
  982. this.collapsedColumns.clear();
  983. setVisibleColumns(getContainerPropertyIds().toArray());
  984. // Assure visual refresh
  985. refreshCurrentPage();
  986. }
  987. /* Component basics ***************************************************** */
  988. /**
  989. * Invoked when the value of a variable has changed.
  990. *
  991. * @param event
  992. * Variable change event containing the information about the
  993. * changed variable.
  994. */
  995. public void changeVariables(Object source, Map variables) {
  996. super.changeVariables(source, variables);
  997. // Page start index
  998. if (variables.containsKey("firstvisible")) {
  999. Integer value = (Integer) variables.get("firstvisible");
  1000. if (value != null)
  1001. setCurrentPageFirstItemIndex(value.intValue() - 1);
  1002. }
  1003. // Set requested firstrow and rows for the next paint
  1004. if (variables.containsKey("reqfirstrow") || variables.containsKey("reqrows")) {
  1005. Integer value = (Integer) variables.get("reqfirstrow");
  1006. if (value != null)
  1007. reqFirstRowToPaint = value.intValue() -1;
  1008. value = (Integer) variables.get("reqrows");
  1009. if (value != null)
  1010. reqRowsToPaint = value.intValue();
  1011. pageBuffer = null;
  1012. requestRepaint();
  1013. }
  1014. // Actions
  1015. if (variables.containsKey("action")) {
  1016. StringTokenizer st = new StringTokenizer((String) variables
  1017. .get("action"), ",");
  1018. if (st.countTokens() == 2) {
  1019. Object itemId = itemIdMapper.get(st.nextToken());
  1020. Action action = (Action) actionMapper.get(st.nextToken());
  1021. if (action != null && containsId(itemId)
  1022. && actionHandlers != null)
  1023. for (Iterator i = actionHandlers.iterator(); i.hasNext();)
  1024. ((Action.Handler) i.next()).handleAction(action, this,
  1025. itemId);
  1026. }
  1027. }
  1028. // Sorting
  1029. boolean doSort = false;
  1030. if (variables.containsKey("sortcolumn")) {
  1031. String colId = (String) variables.get("sortcolumn");
  1032. if (colId != null && !"".equals(colId) && !"null".equals(colId)) {
  1033. Object id = this.columnIdMap.get(colId);
  1034. setSortContainerPropertyId(id);
  1035. doSort = true;
  1036. }
  1037. }
  1038. if (variables.containsKey("sortascending")) {
  1039. boolean state = ((Boolean) variables.get("sortascending"))
  1040. .booleanValue();
  1041. if (state != this.sortAscending) {
  1042. setSortAscending(state);
  1043. doSort = true;
  1044. }
  1045. }
  1046. if (doSort)
  1047. this.sort();
  1048. // Dynamic column hide/show and order
  1049. // Update visible columns
  1050. if (this.isColumnCollapsingAllowed()) {
  1051. if (variables.containsKey("collapsedcolumns")) {
  1052. try {
  1053. Object[] ids = (Object[]) variables.get("collapsedcolumns");
  1054. for (Iterator it = this.visibleColumns.iterator(); it
  1055. .hasNext();) {
  1056. this.setColumnCollapsed(it.next(), false);
  1057. }
  1058. for (int i = 0; i < ids.length; i++) {
  1059. this.setColumnCollapsed(columnIdMap.get(ids[i]
  1060. .toString()), true);
  1061. }
  1062. } catch (Exception ignored) {
  1063. }
  1064. }
  1065. }
  1066. if (this.isColumnReorderingAllowed()) {
  1067. if (variables.containsKey("columnorder")) {
  1068. try {
  1069. Object[] ids = (Object[]) variables.get("columnorder");
  1070. for (int i = 0; i < ids.length; i++) {
  1071. ids[i] = columnIdMap.get(ids[i].toString());
  1072. }
  1073. this.setColumnOrder(ids);
  1074. } catch (Exception ignored) {
  1075. }
  1076. }
  1077. }
  1078. }
  1079. /**
  1080. * Paint the content of this component.
  1081. *
  1082. * @param target
  1083. * Paint target.
  1084. * @throws PaintException
  1085. * The paint operation failed.
  1086. */
  1087. public void paintContent(PaintTarget target) throws PaintException {
  1088. // Focus control id
  1089. if (this.getFocusableId() > 0) {
  1090. target.addAttribute("focusid", this.getFocusableId());
  1091. }
  1092. // The tab ordering number
  1093. if (this.getTabIndex() > 0)
  1094. target.addAttribute("tabindex", this.getTabIndex());
  1095. // Size
  1096. if (getHeight() >= 0)
  1097. target.addAttribute("height", "" + getHeight() + Sizeable.UNIT_SYMBOLS[getHeightUnits()]);
  1098. if (getWidth() >= 0)
  1099. target.addAttribute("width", "" + getWidth()+ Sizeable.UNIT_SYMBOLS[getWidthUnits()]);
  1100. // Initialize temps
  1101. Object[] colids = getVisibleColumns();
  1102. int cols = colids.length;
  1103. int first = getCurrentPageFirstItemIndex();
  1104. int total = size();
  1105. int pagelen = getPageLength();
  1106. int colHeadMode = getColumnHeaderMode();
  1107. boolean colheads = colHeadMode != COLUMN_HEADER_MODE_HIDDEN;
  1108. boolean rowheads = getRowHeaderMode() != ROW_HEADER_MODE_HIDDEN;
  1109. Object[][] cells = getVisibleCells();
  1110. boolean iseditable = this.isEditable();
  1111. // selection support
  1112. String[] selectedKeys;
  1113. if (isMultiSelect())
  1114. selectedKeys = new String[((Set) getValue()).size()];
  1115. else
  1116. selectedKeys = new String[(getValue() == null
  1117. && getNullSelectionItemId() == null ? 0 : 1)];
  1118. int keyIndex = 0;
  1119. // Table attributes
  1120. if (isSelectable())
  1121. target.addAttribute("selectmode", (isMultiSelect() ? "multi"
  1122. : "single"));
  1123. else
  1124. target.addAttribute("selectmode", "none");
  1125. target.addAttribute("cols", cols);
  1126. target.addAttribute("rows", cells[0].length);
  1127. target.addAttribute("firstrow", (reqFirstRowToPaint >= 0 ? reqFirstRowToPaint : first) + 1);
  1128. target.addAttribute("totalrows", total);
  1129. if (pagelen != 0)
  1130. target.addAttribute("pagelength", pagelen);
  1131. if (colheads)
  1132. target.addAttribute("colheaders", true);
  1133. if (rowheads)
  1134. target.addAttribute("rowheaders", true);
  1135. // Columns
  1136. target.startTag("cols");
  1137. Collection sortables = getSortableContainerPropertyIds();
  1138. for (Iterator it = this.visibleColumns.iterator(); it.hasNext();) {
  1139. Object columnId = it.next();
  1140. if (!isColumnCollapsed(columnId)) {
  1141. target.startTag("ch");
  1142. if (colheads) {
  1143. if (this.getColumnIcon(columnId) != null)
  1144. target.addAttribute("icon", this
  1145. .getColumnIcon(columnId));
  1146. if (sortables.contains(columnId))
  1147. target.addAttribute("sortable", true);
  1148. String header = (String) this.getColumnHeader(columnId);
  1149. target.addAttribute("caption", (header != null ? header
  1150. : ""));
  1151. }
  1152. target.addAttribute("cid", this.columnIdMap.key(columnId));
  1153. if (!ALIGN_LEFT.equals(this.getColumnAlignment(columnId)))
  1154. target.addAttribute("align", this
  1155. .getColumnAlignment(columnId));
  1156. target.endTag("ch");
  1157. }
  1158. }
  1159. target.endTag("cols");
  1160. // Rows
  1161. Set actionSet = new LinkedHashSet();
  1162. boolean selectable = isSelectable();
  1163. boolean[] iscomponent = new boolean[this.visibleColumns.size()];
  1164. int iscomponentIndex = 0;
  1165. for (Iterator it = this.visibleColumns.iterator(); it.hasNext()
  1166. && iscomponentIndex < iscomponent.length;) {
  1167. Object columnId = it.next();
  1168. Class colType = getType(columnId);
  1169. iscomponent[iscomponentIndex++] = colType != null
  1170. && Component.class.isAssignableFrom(colType);
  1171. }
  1172. target.startTag("rows");
  1173. for (int i = 0; i < cells[0].length; i++) {
  1174. target.startTag("tr");
  1175. Object itemId = cells[CELL_ITEMID][i];
  1176. // tr attributes
  1177. if (rowheads) {
  1178. if (cells[CELL_ICON][i] != null)
  1179. target.addAttribute("icon", (Resource) cells[CELL_ICON][i]);
  1180. if (cells[CELL_HEADER][i] != null)
  1181. target.addAttribute("caption",
  1182. (String) cells[CELL_HEADER][i]);
  1183. }
  1184. if (actionHandlers != null || isSelectable()) {
  1185. target.addAttribute("key", (String) cells[CELL_KEY][i]);
  1186. if (isSelected(itemId) && keyIndex < selectedKeys.length) {
  1187. target.addAttribute("selected", true);
  1188. selectedKeys[keyIndex++] = (String) cells[CELL_KEY][i];
  1189. }
  1190. }
  1191. // Actions
  1192. if (actionHandlers != null) {
  1193. target.startTag("al");
  1194. for (Iterator ahi = actionHandlers.iterator(); ahi.hasNext();) {
  1195. Action[] aa = ((Action.Handler) ahi.next()).getActions(
  1196. itemId, this);
  1197. if (aa != null)
  1198. for (int ai = 0; ai < aa.length; ai++) {
  1199. String key = actionMapper.key(aa[ai]);
  1200. actionSet.add(aa[ai]);
  1201. target.addSection("ak", key);
  1202. }
  1203. }
  1204. target.endTag("al");
  1205. }
  1206. // cells
  1207. int currentColumn = 0;
  1208. for (Iterator it = this.visibleColumns.iterator(); it.hasNext(); currentColumn++) {
  1209. Object columnId = it.next();
  1210. if (columnId == null || this.isColumnCollapsed(columnId))
  1211. continue;
  1212. if ((iscomponent[currentColumn] || iseditable)
  1213. && Component.class.isInstance(cells[CELL_FIRSTCOL
  1214. + currentColumn][i])) {
  1215. Component c = (Component) cells[CELL_FIRSTCOL
  1216. + currentColumn][i];
  1217. if (c == null)
  1218. target.addSection("label", "");
  1219. else
  1220. c.paint(target);
  1221. } else
  1222. target.addSection("label", (String) cells[CELL_FIRSTCOL
  1223. + currentColumn][i]);
  1224. }
  1225. target.endTag("tr");
  1226. }
  1227. target.endTag("rows");
  1228. // The select variable is only enabled if selectable
  1229. if (selectable)
  1230. target.addVariable(this, "selected", selectedKeys);
  1231. // The cursors are only shown on pageable table
  1232. if (first != 0 || getPageLength() > 0)
  1233. target.addVariable(this, "firstvisible", first + 1);
  1234. // Sorting
  1235. if (getContainerDataSource() instanceof Container.Sortable) {
  1236. target.addVariable(this, "sortcolumn", this.columnIdMap.key(this.sortContainerPropertyId));
  1237. target.addVariable(this, "sortascending", this.sortAscending);
  1238. }
  1239. // Reset and paint "to be painted next" variables. Also reset pageBuffer
  1240. reqFirstRowToPaint = -1;
  1241. reqRowsToPaint = -1;
  1242. pageBuffer = null;
  1243. target.addVariable(this, "reqrows", reqRowsToPaint);
  1244. target.addVariable(this, "reqfirstrow", reqFirstRowToPaint);
  1245. // Actions
  1246. if (!actionSet.isEmpty()) {
  1247. target.startTag("actions");
  1248. target.addVariable(this, "action", "");
  1249. for (Iterator it = actionSet.iterator(); it.hasNext();) {
  1250. Action a = (Action) it.next();
  1251. target.startTag("action");
  1252. if (a.getCaption() != null)
  1253. target.addAttribute("caption", a.getCaption());
  1254. if (a.getIcon() != null)
  1255. target.addAttribute("icon", a.getIcon());
  1256. target.addAttribute("key", actionMapper.key(a));
  1257. target.endTag("action");
  1258. }
  1259. target.endTag("actions");
  1260. }
  1261. if (this.columnReorderingAllowed) {
  1262. String[] colorder = new String[this.visibleColumns.size()];
  1263. int i = 0;
  1264. for (Iterator it = this.visibleColumns.iterator(); it.hasNext()
  1265. && i < colorder.length;) {
  1266. colorder[i++] = this.columnIdMap.key(it.next());
  1267. }
  1268. target.addVariable(this, "columnorder", colorder);
  1269. }
  1270. // Available columns
  1271. if (this.columnCollapsingAllowed) {
  1272. HashSet ccs = new HashSet();
  1273. for (Iterator i = visibleColumns.iterator(); i.hasNext();) {
  1274. Object o = i.next();
  1275. if (isColumnCollapsed(o))
  1276. ccs.add(o);
  1277. }
  1278. String[] collapsedkeys = new String[ccs.size()];
  1279. int nextColumn = 0;
  1280. for (Iterator it = this.visibleColumns.iterator(); it.hasNext()
  1281. && nextColumn < collapsedkeys.length;) {
  1282. Object columnId = it.next();
  1283. if (this.isColumnCollapsed(columnId)) {
  1284. collapsedkeys[nextColumn++] = this.columnIdMap
  1285. .key(columnId);
  1286. }
  1287. }
  1288. target.addVariable(this, "collapsedcolumns", collapsedkeys);
  1289. target.startTag("visiblecolumns");
  1290. int i = 0;
  1291. for (Iterator it = this.visibleColumns.iterator(); it.hasNext(); i++) {
  1292. Object columnId = it.next();
  1293. if (columnId != null) {
  1294. target.startTag("column");
  1295. target.addAttribute("cid", this.columnIdMap.key(columnId));
  1296. String head = getColumnHeader(columnId);
  1297. target.addAttribute("caption", (head != null ? head : ""));
  1298. if (this.isColumnCollapsed(columnId)) {
  1299. target.addAttribute("collapsed", true);
  1300. }
  1301. target.endTag("column");
  1302. }
  1303. }
  1304. target.endTag("visiblecolumns");
  1305. }
  1306. }
  1307. /**
  1308. * Get UIDL tag corresponding to component.
  1309. *
  1310. * @return UIDL tag as string.
  1311. */
  1312. public String getTag() {
  1313. return "table";
  1314. }
  1315. /** Return cached visible table contents */
  1316. private Object[][] getVisibleCells() {
  1317. // Return a buffered value if possible
  1318. if (pageBuffer != null && isPageBufferingEnabled())
  1319. return pageBuffer;
  1320. // Stop listening the old properties and initialise the list
  1321. if (listenedProperties == null)
  1322. listenedProperties = new LinkedList();
  1323. else
  1324. for (Iterator i = listenedProperties.iterator(); i.hasNext();) {
  1325. ((Property.ValueChangeNotifier) i.next()).removeListener(this);
  1326. }
  1327. // Detach old visible component from the table
  1328. if (visibleComponents == null)
  1329. visibleComponents = new LinkedList();
  1330. else
  1331. for (Iterator i = visibleComponents.iterator(); i.hasNext();) {
  1332. ((Component) i.next()).setParent(null);
  1333. }
  1334. // Collect basic facts about the table page
  1335. Object[] colids = getVisibleColumns();
  1336. int cols = colids.length;
  1337. int pagelen = getPageLength();
  1338. int firstIndex = getCurrentPageFirstItemIndex();
  1339. int rows = size();
  1340. if (rows > 0 && firstIndex >= 0)
  1341. rows -= firstIndex;
  1342. if (pagelen > 0 && pagelen < rows)
  1343. rows = pagelen;
  1344. // If "to be painted next" variables are set, use them
  1345. if (reqRowsToPaint >= 0) rows = reqRowsToPaint;
  1346. Object id;
  1347. if (reqFirstRowToPaint >= 0 && reqFirstRowToPaint < size())
  1348. firstIndex = reqFirstRowToPaint;
  1349. if (rows + firstIndex > size()) rows = size() - firstIndex;
  1350. // Get first item id
  1351. if (items instanceof Container.Indexed)
  1352. id = ((Container.Indexed) items).getIdByIndex(firstIndex);
  1353. else {
  1354. id = ((Container.Ordered) items).firstItemId();
  1355. for (int i=0; i<firstIndex; i++) id = ((Container.Ordered) items).nextItemId(id);
  1356. }
  1357. Object[][] cells = new Object[cols + CELL_FIRSTCOL][rows];
  1358. if (rows == 0)
  1359. return cells;
  1360. int headmode = getRowHeaderMode();
  1361. boolean[] iscomponent = new boolean[cols];
  1362. for (int i = 0; i < cols; i++)
  1363. iscomponent[i] = Component.class
  1364. .isAssignableFrom(getType(colids[i]));
  1365. // Create page contents
  1366. int filledRows = 0;
  1367. for (int i = 0; i < rows && id != null; i++) {
  1368. cells[CELL_ITEMID][i] = id;
  1369. cells[CELL_KEY][i] = itemIdMapper.key(id);
  1370. if (headmode != ROW_HEADER_MODE_HIDDEN) {
  1371. switch (headmode) {
  1372. case ROW_HEADER_MODE_INDEX:
  1373. cells[CELL_HEADER][i] = String.valueOf(i + firstIndex + 1);
  1374. break;
  1375. default:
  1376. cells[CELL_HEADER][i] = getItemCaption(id);
  1377. }
  1378. cells[CELL_ICON][i] = getItemIcon(id);
  1379. }
  1380. if (cols > 0) {
  1381. for (int j = 0; j < cols; j++) {
  1382. Object value = null;
  1383. Property p = getContainerProperty(id, colids[j]);
  1384. if (p != null) {
  1385. if (p instanceof Property.ValueChangeNotifier) {
  1386. ((Property.ValueChangeNotifier) p)
  1387. .addListener(this);
  1388. listenedProperties.add(p);
  1389. }
  1390. if (iscomponent[j]) {
  1391. value = p.getValue();
  1392. } else if (p != null) {
  1393. value = getPropertyValue(id, colids[j], p);
  1394. } else {
  1395. value = getPropertyValue(id, colids[j], null);
  1396. }
  1397. } else {
  1398. value = "";
  1399. }
  1400. if (value instanceof Component) {
  1401. ((Component) value).setParent(this);
  1402. visibleComponents.add((Component) value);
  1403. }
  1404. cells[CELL_FIRSTCOL + j][i] = value;
  1405. }
  1406. }
  1407. id = ((Container.Ordered) items).nextItemId(id);
  1408. filledRows++;
  1409. }
  1410. // Assure that all the rows of the cell-buffer are valid
  1411. if (filledRows != cells[0].length) {
  1412. Object[][] temp = new Object[cells.length][filledRows];
  1413. for (int i = 0; i < cells.length; i++)
  1414. for (int j = 0; j < filledRows; j++)
  1415. temp[i][j] = cells[i][j];
  1416. cells = temp;
  1417. }
  1418. // Save the results to internal buffer iff in buffering mode
  1419. // to possible conserve memory from large non-buffered pages
  1420. if (isPageBufferingEnabled())
  1421. pageBuffer = cells;
  1422. return cells;
  1423. }
  1424. /**
  1425. * Get value of property.
  1426. *
  1427. * By default if the table is editable the fieldFactory is used to create
  1428. * editors for table cells. Otherwise formatPropertyValue is used to format
  1429. * the value representation.
  1430. *
  1431. * @see #setFieldFactory(FieldFactory)
  1432. * @param rowId
  1433. * Id of the row (same as item Id)
  1434. * @param colId
  1435. * Id of the column
  1436. * @param property
  1437. * Property to be presented
  1438. * @return Object Either formatted value or Component for field.
  1439. */
  1440. protected Object getPropertyValue(Object rowId, Object colId,
  1441. Property property) {
  1442. if (this.isEditable() && this.fieldFactory != null) {
  1443. Field f = this.fieldFactory.createField(getContainerDataSource(),
  1444. rowId, colId, this);
  1445. if (f != null) {
  1446. f.setPropertyDataSource(property);
  1447. return f;
  1448. }
  1449. }
  1450. return formatPropertyValue(rowId, colId, property);
  1451. }
  1452. /**
  1453. * Formats table cell property values. By default the property.toString()
  1454. * and return a empty string for null properties.
  1455. *
  1456. * @param itemId
  1457. * @param property
  1458. * Property to be formatted
  1459. * @return String representation of property and its value.
  1460. * @since 3.1
  1461. */
  1462. protected String formatPropertyValue(Object rowId, Object colId,
  1463. Property property) {
  1464. if (property == null) {
  1465. return "";
  1466. }
  1467. return property.toString();
  1468. }
  1469. /* Action container *************************************************** */
  1470. /**
  1471. * @see com.itmill.toolkit.event.Action.Container#addActionHandler(Action.Handler)
  1472. */
  1473. public void addActionHandler(Action.Handler actionHandler) {
  1474. if (actionHandler != null) {
  1475. if (actionHandlers == null) {
  1476. actionHandlers = new LinkedList();
  1477. actionMapper = new KeyMapper();
  1478. }
  1479. if(!actionHandlers.contains(actionHandler)){
  1480. actionHandlers.add(actionHandler);
  1481. requestRepaint();
  1482. }
  1483. }
  1484. }
  1485. /**
  1486. * @see com.itmill.toolkit.event.Action.Container#removeActionHandler(Action.Handler)
  1487. */
  1488. public void removeActionHandler(Action.Handler actionHandler) {
  1489. if (actionHandlers != null && actionHandlers.contains(actionHandler)) {
  1490. actionHandlers.remove(actionHandler);
  1491. if (actionHandlers.isEmpty()) {
  1492. actionHandlers = null;
  1493. actionMapper = null;
  1494. }
  1495. requestRepaint();
  1496. }
  1497. }
  1498. /* Property value change listening support **************************** */
  1499. /**
  1500. * @see com.itmill.toolkit.data.Property.ValueChangeListener#valueChange(Property.ValueChangeEvent)
  1501. */
  1502. public void valueChange(Property.ValueChangeEvent event) {
  1503. super.valueChange(event);
  1504. requestRepaint();
  1505. }
  1506. /**
  1507. * @see com.itmill.toolkit.ui.Component#attach()
  1508. */
  1509. public void attach() {
  1510. super.attach();
  1511. if (visibleComponents != null)
  1512. for (Iterator i = visibleComponents.iterator(); i.hasNext();)
  1513. ((Component) i.next()).attach();
  1514. }
  1515. /**
  1516. * @see com.itmill.toolkit.ui.Component#attach()
  1517. */
  1518. public void detach() {
  1519. super.detach();
  1520. if (visibleComponents != null)
  1521. for (Iterator i = visibleComponents.iterator(); i.hasNext();)
  1522. ((Component) i.next()).detach();
  1523. }
  1524. /**
  1525. * @see com.itmill.toolkit.data.Container#removeAllItems()
  1526. */
  1527. public boolean removeAllItems() {
  1528. this.currentPageFirstItemId = null;
  1529. this.currentPageFirstItemIndex = 0;
  1530. return super.removeAllItems();
  1531. }
  1532. /**
  1533. * @see com.itmill.toolkit.data.Container#removeItem(Object)
  1534. */
  1535. public boolean removeItem(Object itemId) {
  1536. Object nextItemId = ((Container.Ordered) items).nextItemId(itemId);
  1537. boolean ret = super.removeItem(itemId);
  1538. if (ret && (itemId != null)
  1539. && (itemId.equals(this.currentPageFirstItemId))) {
  1540. this.currentPageFirstItemId = nextItemId;
  1541. }
  1542. return ret;
  1543. }
  1544. /**
  1545. * @see com.itmill.toolkit.data.Container#removeContainerProperty(Object)
  1546. */
  1547. public boolean removeContainerProperty(Object propertyId)
  1548. throws UnsupportedOperationException {
  1549. // If a visible property is removed, remove the corresponding column
  1550. this.visibleColumns.remove(propertyId);
  1551. this.columnAlignments.remove(propertyId);
  1552. this.columnIcons.remove(propertyId);
  1553. this.columnHeaders.remove(propertyId);
  1554. return super.removeContainerProperty(propertyId);
  1555. }
  1556. /**
  1557. * Adds a new property to the table and show it as a visible column.
  1558. *
  1559. * @see com.itmill.toolkit.data.Container#addContainerProperty(Object,
  1560. * Class, Object)
  1561. *
  1562. * @param propertyId
  1563. * Id of the proprty
  1564. * @param type
  1565. * The class of the property
  1566. * @param defaultValue
  1567. * The default value given for all existing items
  1568. */
  1569. public boolean addContainerProperty(Object propertyId, Class type,
  1570. Object defaultValue) throws UnsupportedOperationException {
  1571. if (!super.addContainerProperty(propertyId, type, defaultValue))
  1572. return false;
  1573. if (!this.visibleColumns.contains(propertyId))
  1574. this.visibleColumns.add(propertyId);
  1575. return true;
  1576. }
  1577. /**
  1578. * Adds a new property to the table and show it as a visible column.
  1579. *
  1580. * @see com.itmill.toolkit.data.Container#addContainerProperty(Object,
  1581. * Class, Object)
  1582. *
  1583. * @param propertyId
  1584. * Id of the proprty
  1585. * @param type
  1586. * The class of the property
  1587. * @param defaultValue
  1588. * The default value given for all existing items
  1589. * @param columnHeader
  1590. * Explicit header of the column. If explicit header is not
  1591. * needed, this should be set null.
  1592. * @param columnIcon
  1593. * Icon of the column. If icon is not needed, this should be set
  1594. * null.
  1595. * @param columnAlignment
  1596. * Alignment of the column. Null implies align left.
  1597. */
  1598. public boolean addContainerProperty(Object propertyId, Class type,
  1599. Object defaultValue, String columnHeader, Resource columnIcon,
  1600. String columnAlignment) throws UnsupportedOperationException {
  1601. if (!this.addContainerProperty(propertyId, type, defaultValue))
  1602. return false;
  1603. this.setColumnAlignment(propertyId, columnAlignment);
  1604. this.setColumnHeader(propertyId, columnHeader);
  1605. this.setColumnIcon(propertyId, columnIcon);
  1606. return true;
  1607. }
  1608. /**
  1609. * Return list of items on the current page
  1610. *
  1611. * @see com.itmill.toolkit.ui.Select#getVisibleItemIds()
  1612. */
  1613. public Collection getVisibleItemIds() {
  1614. LinkedList visible = new LinkedList();
  1615. Object[][] cells = getVisibleCells();
  1616. for (int i = 0; i < cells[CELL_ITEMID].length; i++)
  1617. visible.add(cells[CELL_ITEMID][i]);
  1618. return visible;
  1619. }
  1620. /**
  1621. * Container datasource item set change. Table must flush its buffers on
  1622. * change.
  1623. *
  1624. * @see com.itmill.toolkit.data.Container.ItemSetChangeListener#containerItemSetChange(com.itmill.toolkit.data.Container.ItemSetChangeEvent)
  1625. */
  1626. public void containerItemSetChange(Container.ItemSetChangeEvent event) {
  1627. pageBuffer = null;
  1628. super.containerItemSetChange(event);
  1629. setCurrentPageFirstItemIndex(this.getCurrentPageFirstItemIndex());
  1630. }
  1631. /**
  1632. * Container datasource property set change. Table must flush its buffers on
  1633. * change.
  1634. *
  1635. * @see com.itmill.toolkit.data.Container.PropertySetChangeListener#containerPropertySetChange(com.itmill.toolkit.data.Container.PropertySetChangeEvent)
  1636. */
  1637. public void containerPropertySetChange(
  1638. Container.PropertySetChangeEvent event) {
  1639. pageBuffer = null;
  1640. super.containerPropertySetChange(event);
  1641. }
  1642. /**
  1643. * Adding new items is not supported.
  1644. *
  1645. * @see com.itmill.toolkit.ui.Select#setNewItemsAllowed(boolean)
  1646. * @throws UnsupportedOperationException
  1647. * if set to true.
  1648. */
  1649. public void setNewItemsAllowed(boolean allowNewOptions)
  1650. throws UnsupportedOperationException {
  1651. if (allowNewOptions)
  1652. throw new UnsupportedOperationException();
  1653. }
  1654. /**
  1655. * Focusing to this component is not supported.
  1656. *
  1657. * @see com.itmill.toolkit.ui.AbstractField#focus()
  1658. * @throws UnsupportedOperationException
  1659. * if invoked.
  1660. */
  1661. public void focus() throws UnsupportedOperationException {
  1662. throw new UnsupportedOperationException();
  1663. }
  1664. /**
  1665. * @see com.itmill.toolkit.data.Container.Ordered#nextItemId(java.lang.Object)
  1666. */
  1667. public Object nextItemId(Object itemId) {
  1668. return ((Container.Ordered) items).nextItemId(itemId);
  1669. }
  1670. /**
  1671. * @see com.itmill.toolkit.data.Container.Ordered#prevItemId(java.lang.Object)
  1672. */
  1673. public Object prevItemId(Object itemId) {
  1674. return ((Container.Ordered) items).prevItemId(itemId);
  1675. }
  1676. /**
  1677. * @see com.itmill.toolkit.data.Container.Ordered#firstItemId()
  1678. */
  1679. public Object firstItemId() {
  1680. return ((Container.Ordered) items).firstItemId();
  1681. }
  1682. /**
  1683. * @see com.itmill.toolkit.data.Container.Ordered#lastItemId()
  1684. */
  1685. public Object lastItemId() {
  1686. return ((Container.Ordered) items).lastItemId();
  1687. }
  1688. /**
  1689. * @see com.itmill.toolkit.data.Container.Ordered#isFirstId(java.lang.Object)
  1690. */
  1691. public boolean isFirstId(Object itemId) {
  1692. return ((Container.Ordered) items).isFirstId(itemId);
  1693. }
  1694. /**
  1695. * @see com.itmill.toolkit.data.Container.Ordered#isLastId(java.lang.Object)
  1696. */
  1697. public boolean isLastId(Object itemId) {
  1698. return ((Container.Ordered) items).isLastId(itemId);
  1699. }
  1700. /**
  1701. * @see com.itmill.toolkit.data.Container.Ordered#addItemAfter(java.lang.Object)
  1702. */
  1703. public Object addItemAfter(Object previousItemId)
  1704. throws UnsupportedOperationException {
  1705. return ((Container.Ordered) items).addItemAfter(previousItemId);
  1706. }
  1707. /**
  1708. * @see com.itmill.toolkit.data.Container.Ordered#addItemAfter(java.lang.Object,
  1709. * java.lang.Object)
  1710. */
  1711. public Item addItemAfter(Object previousItemId, Object newItemId)
  1712. throws UnsupportedOperationException {
  1713. return ((Container.Ordered) items).addItemAfter(previousItemId,
  1714. newItemId);
  1715. }
  1716. /**
  1717. * Get the FieldFactory that is used to create editor for table cells.
  1718. *
  1719. * The FieldFactory is only used if the Table is editable.
  1720. *
  1721. * @see #isEditable
  1722. * @return FieldFactory used to create the Field instances.
  1723. */
  1724. public FieldFactory getFieldFactory() {
  1725. return fieldFactory;
  1726. }
  1727. /**
  1728. * Set the FieldFactory that is used to create editor for table cells.
  1729. *
  1730. * The FieldFactory is only used if the Table is editable. By default the
  1731. * BaseFieldFactory is used.
  1732. *
  1733. * @see #isEditable
  1734. * @see BaseFieldFactory
  1735. * @param fieldFactory
  1736. * The field factory to set
  1737. */
  1738. public void setFieldFactory(FieldFactory fieldFactory) {
  1739. this.fieldFactory = fieldFactory;
  1740. // Assure visual refresh
  1741. refreshCurrentPage();
  1742. }
  1743. /**
  1744. * Is table editable.
  1745. *
  1746. * If table is editable a editor of type Field is created for each table
  1747. * cell. The assigned FieldFactory is used to create the instances.
  1748. *
  1749. * To provide custom editors for table cells create a class implementins the
  1750. * FieldFactory interface, and assign it to table, and set the editable
  1751. * property to true.
  1752. *
  1753. * @see Field
  1754. * @see FieldFactory
  1755. * @return true if table is editable, false oterwise.
  1756. */
  1757. public boolean isEditable() {
  1758. return editable;
  1759. }
  1760. /**
  1761. * Set the editable property.
  1762. *
  1763. * If table is editable a editor of type Field is created for each table
  1764. * cell. The assigned FieldFactory is used to create the instances.
  1765. *
  1766. * To provide custom editors for table cells create a class implementins the
  1767. * FieldFactory interface, and assign it to table, and set the editable
  1768. * property to true.
  1769. *
  1770. * @see Field
  1771. * @see FieldFactory
  1772. * @param editable
  1773. * true if table should be editable by user.
  1774. */
  1775. public void setEditable(boolean editable) {
  1776. this.editable = editable;
  1777. // Assure visual refresh
  1778. refreshCurrentPage();
  1779. }
  1780. /**
  1781. * Sort table.
  1782. *
  1783. * @see com.itmill.toolkit.data.Container.Sortable#sort(java.lang.Object[],
  1784. * boolean[])
  1785. *
  1786. * @throws UnsupportedOperationException
  1787. * if the container data source does not implement
  1788. * Container.Sortable
  1789. */
  1790. public void sort(Object[] propertyId, boolean[] ascending)
  1791. throws UnsupportedOperationException {
  1792. Container c = getContainerDataSource();
  1793. if (c instanceof Container.Sortable) {
  1794. int pageIndex = this.getCurrentPageFirstItemIndex();
  1795. ((Container.Sortable) c).sort(propertyId, ascending);
  1796. setCurrentPageFirstItemIndex(pageIndex);
  1797. } else if (c != null) {
  1798. throw new UnsupportedOperationException(
  1799. "Underlying Data does not allow sorting");
  1800. }
  1801. }
  1802. /**
  1803. * Sort table by currently selected sorting column.
  1804. *
  1805. * @throws UnsupportedOperationException
  1806. * if the container data source does not implement
  1807. * Container.Sortable
  1808. */
  1809. public void sort() {
  1810. if (getSortContainerPropertyId() == null)
  1811. return;
  1812. sort(new Object[] { this.sortContainerPropertyId },
  1813. new boolean[] { this.sortAscending });
  1814. }
  1815. /*
  1816. * (non-Javadoc)
  1817. *
  1818. * @see com.itmill.toolkit.data.Container.Sortable#getSortableContainerPropertyIds()
  1819. */
  1820. public Collection getSortableContainerPropertyIds() {
  1821. Container c = getContainerDataSource();
  1822. if (c instanceof Container.Sortable && !isSortDisabled()) {
  1823. return ((Container.Sortable) c).getSortableContainerPropertyIds();
  1824. } else {
  1825. return new LinkedList();
  1826. }
  1827. }
  1828. /**
  1829. * Get the currently sorted column property ID.
  1830. *
  1831. * @return Container property id of the currently sorted column.
  1832. */
  1833. public Object getSortContainerPropertyId() {
  1834. return this.sortContainerPropertyId;
  1835. }
  1836. /**
  1837. * Set the currently sorted column property id.
  1838. *
  1839. * @param propertyId
  1840. * Container property id of the currently sorted column.
  1841. */
  1842. public void setSortContainerPropertyId(Object propertyId) {
  1843. if ((this.sortContainerPropertyId != null && !this.sortContainerPropertyId
  1844. .equals(propertyId))
  1845. || (this.sortContainerPropertyId == null && propertyId != null)) {
  1846. this.sortContainerPropertyId = propertyId;
  1847. sort();
  1848. }
  1849. // Assure visual refresh
  1850. refreshCurrentPage();
  1851. }
  1852. /**
  1853. * Is the table currently sorted in ascending order.
  1854. *
  1855. * @return <code>true</code> if ascending, <code>false</code> if
  1856. * descending
  1857. */
  1858. public boolean isSortAscending() {
  1859. return this.sortAscending;
  1860. }
  1861. /**
  1862. * Set the table in ascending order.
  1863. *
  1864. * @param ascending
  1865. * <code>true</code> if ascending, <code>false</code> if
  1866. * descending
  1867. */
  1868. public void setSortAscending(boolean ascending) {
  1869. if (this.sortAscending != ascending) {
  1870. this.sortAscending = ascending;
  1871. sort();
  1872. }
  1873. // Assure visual refresh
  1874. refreshCurrentPage();
  1875. }
  1876. /** Is sorting disabled alltogether.
  1877. *
  1878. * True iff no sortable columns are given even in the case where datasource would support this.
  1879. *
  1880. * @return True iff sorting is disabled.
  1881. */
  1882. public boolean isSortDisabled() {
  1883. return sortDisabled;
  1884. }
  1885. /** Disable sorting alltogether.
  1886. *
  1887. * To disable sorting alltogether, set to true. In this case no
  1888. * sortable columns are given even in the case where datasource would support this.
  1889. *
  1890. * @param sortDisabled True iff sorting is disabled
  1891. */
  1892. public void setSortDisabled(boolean sortDisabled) {
  1893. if (this.sortDisabled != sortDisabled) {
  1894. this.sortDisabled = sortDisabled;
  1895. refreshCurrentPage();
  1896. }
  1897. }
  1898. /**
  1899. * @see com.itmill.toolkit.terminal.Sizeable#getHeightUnits()
  1900. */
  1901. public int getHeightUnits() {
  1902. return heightUnit;
  1903. }
  1904. /**
  1905. * @see com.itmill.toolkit.terminal.Sizeable#getWidthUnits()
  1906. */
  1907. public int getWidthUnits() {
  1908. return widthUnit;
  1909. }
  1910. /** Set height units.
  1911. * Table supports only Sizeable.UNITS_PIXELS. Setting to any other throws
  1912. * IllegalArgumentException.
  1913. * @see com.itmill.toolkit.terminal.Sizeable#setHeightUnits(int)
  1914. */
  1915. public void setHeightUnits(int units) {
  1916. if (units != Sizeable.UNITS_PIXELS)
  1917. throw new IllegalArgumentException();
  1918. this.heightUnit = units;
  1919. }
  1920. /** Set width units.
  1921. * Tabel supports only Sizeable.UNITS_PIXELS and Sizeable.UNITS_PERCENTAGE. Setting to any other throws
  1922. * IllegalArgumentException.
  1923. * @see com.itmill.toolkit.terminal.Sizeable#setWidthUnits(int)
  1924. */
  1925. public void setWidthUnits(int units) {
  1926. if (units != Sizeable.UNITS_PIXELS && units != Sizeable.UNITS_PERCENTAGE)
  1927. throw new IllegalArgumentException();
  1928. this.widthUnit = units;
  1929. }
  1930. /**
  1931. * @return The height in pixels or negative value if not assigned.
  1932. */
  1933. public int getHeight() {
  1934. return height;
  1935. }
  1936. /**
  1937. * @return The width in pixels or negative value if not assigned.
  1938. */
  1939. public int getWidth() {
  1940. return width;
  1941. }
  1942. /** Sets the height in pixels.
  1943. * Use negative value to let the client decide the height.
  1944. * @param height The height to set
  1945. */
  1946. public void setHeight(int height) {
  1947. this.height = height;
  1948. requestRepaint();
  1949. }
  1950. /** Sets the width in pixels.
  1951. * Use negative value to allow the client decide the width.
  1952. * @param width The width to set
  1953. */
  1954. public void setWidth(int width) {
  1955. this.width = width;
  1956. requestRepaint();
  1957. }
  1958. /** Table does not support lazy options loading mode.
  1959. * Setting this true will throw UnsupportedOperationException.
  1960. */
  1961. public void setLazyLoading(boolean useLazyLoading) {
  1962. if (useLazyLoading)
  1963. throw new UnsupportedOperationException("Lazy options loading is not supported by Table.");
  1964. }
  1965. }