001: /*
002: * $Id: DataTable.java 465472 2006-10-19 04:19:43Z ivaynberg $
003: * $Revision: 465472 $ $Date: 2006-10-19 06:19:43 +0200 (Thu, 19 Oct 2006) $
004: *
005: * ==================================================================== Licensed
006: * under the Apache License, Version 2.0 (the "License"); you may not use this
007: * file except in compliance with the License. You may obtain a copy of the
008: * License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.extensions.markup.html.repeater.data.table;
019:
020: import wicket.extensions.markup.html.repeater.RepeatingView;
021: import wicket.extensions.markup.html.repeater.data.IDataProvider;
022: import wicket.extensions.markup.html.repeater.data.grid.DataGridView;
023: import wicket.extensions.markup.html.repeater.refreshing.IItemReuseStrategy;
024: import wicket.extensions.markup.html.repeater.refreshing.Item;
025: import wicket.extensions.markup.html.repeater.refreshing.RefreshingView;
026: import wicket.markup.html.WebMarkupContainer;
027: import wicket.markup.html.navigation.paging.IPageable;
028: import wicket.markup.html.panel.Panel;
029: import wicket.model.IModel;
030:
031: /**
032: * A data table builds on data grid view to introduce toolbars. Toolbars can be
033: * used to display sortable column headers, paging information, filter controls,
034: * and other information.
035: * <p>
036: * Data table also provides its own markup for an html table so the user does
037: * not need to provide it himself. This makes it very simple to add a datatable
038: * to the markup, however, some flexibility.
039: * <p>
040: * Example
041: *
042: * <pre>
043: * <table wicket:id="datatable"></table>
044: * </pre>
045: *
046: * And the related Java code: ( the first column will be sortable because its
047: * sort property is specified, the second column will not )
048: *
049: * <pre>
050: *
051: * IColumn[] columns = new IColumn[2];
052: *
053: * columns[0] = new PropertyColumn(new Model("First Name"), "name.first", "name.first");
054: * columns[1] = new PropertyColumn(new Model("Last Name"), "name.last");
055: *
056: * DataTable table = new DataTable("datatable", columns, new UserProvider(), 10);
057: * table.add(new NavigationToolbar(table));
058: * table.add(new HeadersToolbar(table));
059: * add(table);
060: *
061: * </pre>
062: *
063: * @see DefaultDataTable
064: *
065: * @author Igor Vaynberg (ivaynberg)
066: *
067: */
068: public class DataTable extends Panel implements IPageable {
069: /**
070: * The component id that toolbars must be created with in order to be added
071: * to the data table
072: */
073: public static final String TOOLBAR_COMPONENT_ID = "toolbar";
074:
075: private static final long serialVersionUID = 1L;
076:
077: private final DataGridView datagrid;
078:
079: private IColumn[] columns;
080:
081: private final RepeatingView topToolbars;
082: private final RepeatingView bottomToolbars;
083:
084: /**
085: * Constructor
086: *
087: * @param id
088: * component id
089: * @param columns
090: * list of IColumn objects
091: * @param dataProvider
092: * imodel for data provider
093: * @param rowsPerPage
094: * number of rows per page
095: */
096: public DataTable(String id, IColumn[] columns,
097: IDataProvider dataProvider, int rowsPerPage) {
098: super (id);
099:
100: this .columns = columns;
101:
102: datagrid = new DataGridView("rows", columns, dataProvider) {
103: private static final long serialVersionUID = 1L;
104:
105: protected Item newRowItem(String id, int index, IModel model) {
106: return DataTable.this .newRowItem(id, index, model);
107: }
108:
109: protected Item newCellItem(String id, int index,
110: IModel model) {
111: return DataTable.this .newCellItem(id, index, model);
112: }
113: };
114: datagrid.setRowsPerPage(rowsPerPage);
115: add(datagrid);
116:
117: topToolbars = new RepeatingView("topToolbars") {
118: private static final long serialVersionUID = 1L;
119:
120: public boolean isVisible() {
121: return size() > 0;
122: }
123:
124: };
125:
126: bottomToolbars = new RepeatingView("bottomToolbars") {
127:
128: private static final long serialVersionUID = 1L;
129:
130: public boolean isVisible() {
131: return size() > 0;
132: }
133: };
134:
135: add(topToolbars);
136: add(bottomToolbars);
137: }
138:
139: /**
140: * @return array of column objects this table displays
141: */
142: public final IColumn[] getColumns() {
143: return columns;
144: }
145:
146: /**
147: * Adds a toolbar to the datatable that will be displayed before the data
148: *
149: * @param toolbar
150: * toolbar to be added
151: *
152: * @see AbstractToolbar
153: */
154: public void addTopToolbar(AbstractToolbar toolbar) {
155: addToolbar(toolbar, topToolbars);
156: }
157:
158: /**
159: * Adds a toolbar to the datatable that will be displayed after the data
160: *
161: * @param toolbar
162: * toolbar to be added
163: *
164: * @see AbstractToolbar
165: */
166: public void addBottomToolbar(AbstractToolbar toolbar) {
167: addToolbar(toolbar, bottomToolbars);
168: }
169:
170: private void addToolbar(AbstractToolbar toolbar,
171: RepeatingView container) {
172: if (toolbar == null) {
173: throw new IllegalArgumentException(
174: "argument [toolbar] cannot be null");
175: }
176:
177: if (!toolbar.getId().equals(TOOLBAR_COMPONENT_ID)) {
178: throw new IllegalArgumentException(
179: "Toolbar must have component id equal to AbstractDataTable.TOOLBAR_COMPONENT_ID");
180: }
181:
182: toolbar.setRenderBodyOnly(true);
183:
184: // create a container item for the toolbar (required by repeating view)
185: WebMarkupContainer item = new WebMarkupContainer(container
186: .newChildId());
187: item.setRenderBodyOnly(true);
188: item.add(toolbar);
189:
190: container.add(item);
191: }
192:
193: /**
194: * @see wicket.markup.html.navigation.paging.IPageable#getCurrentPage()
195: */
196: public final int getCurrentPage() {
197: return datagrid.getCurrentPage();
198: }
199:
200: /**
201: * @see wicket.markup.html.navigation.paging.IPageable#setCurrentPage(int)
202: */
203: public final void setCurrentPage(int page) {
204: datagrid.setCurrentPage(page);
205: onPageChanged();
206: }
207:
208: /**
209: * Event listener for page-changed event
210: */
211: protected void onPageChanged() {
212: // noop
213: }
214:
215: /**
216: * @see wicket.markup.html.navigation.paging.IPageable#getPageCount()
217: */
218: public final int getPageCount() {
219: return datagrid.getPageCount();
220: }
221:
222: /**
223: * @return total number of rows in this table
224: */
225: public final int getRowCount() {
226: return datagrid.getRowCount();
227: }
228:
229: /**
230: * Sets the number of items to be displayed per page
231: *
232: * @param items
233: * number of items to display per page
234: *
235: */
236: public void setRowsPerPage(int items) {
237: datagrid.setRowsPerPage(items);
238: }
239:
240: /**
241: * @return number of rows per page
242: */
243: public final int getRowsPerPage() {
244: return datagrid.getRowsPerPage();
245: }
246:
247: /**
248: * Factory method for Item container that represents a row in the underlying
249: * DataGridView
250: *
251: * @see Item
252: *
253: * @param id
254: * component id for the new data item
255: * @param index
256: * the index of the new data item
257: * @param model
258: * the model for the new data item.
259: *
260: * @return DataItem created DataItem
261: */
262: protected Item newRowItem(final String id, int index,
263: final IModel model) {
264: return new Item(id, index, model);
265: }
266:
267: /**
268: * Factory method for Item container that represents a cell in the
269: * underlying DataGridView
270: *
271: * @see Item
272: *
273: * @param id
274: * component id for the new data item
275: * @param index
276: * the index of the new data item
277: * @param model
278: * the model for the new data item
279: *
280: * @return DataItem created DataItem
281: */
282: protected Item newCellItem(final String id, int index,
283: final IModel model) {
284: return new Item(id, index, model);
285: }
286:
287: /**
288: * Sets the item reuse strategy. This strategy controls the creation of
289: * {@link Item}s.
290: *
291: * @see RefreshingView#setItemReuseStrategy(IItemReuseStrategy)
292: * @see IItemReuseStrategy
293: *
294: * @param strategy
295: * item reuse strategy
296: * @return this for chaining
297: */
298: public final DataTable setItemReuseStrategy(
299: IItemReuseStrategy strategy) {
300: datagrid.setItemReuseStrategy(strategy);
301: return this;
302: }
303:
304: }
|