001: /*
002: * $Id: WebTable.java 497654 2007-01-19 00:21:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components.table;
022:
023: import java.io.Writer;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.NoSuchElementException;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import javax.swing.table.TableModel;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.struts2.views.annotations.StrutsTag;
035: import org.apache.struts2.views.annotations.StrutsTagAttribute;
036: import org.apache.struts2.StrutsException;
037: import org.apache.struts2.components.GenericUIBean;
038: import org.apache.struts2.components.table.renderer.CellRenderer;
039:
040: import com.opensymphony.xwork2.util.ValueStack;
041:
042: @StrutsTag(name="table",tldTagClass="org.apache.struts2.views.jsp.ui.table.WebTableTag",description="Instantiate a JavaBean and place it in the context")
043: public class WebTable extends GenericUIBean {
044: private static final Log LOG = LogFactory.getLog(WebTable.class);
045:
046: final public static String TEMPLATE = "table";
047:
048: protected String sortOrder = SortableTableModel.NONE;
049: protected String modelName = null;
050: protected TableModel model = null;
051: protected WebTableColumn[] columns = null;
052: protected boolean sortableAttr = false;
053: protected int sortColumn = -1;
054: protected int curRow = 0;
055:
056: public WebTable(ValueStack stack, HttpServletRequest request,
057: HttpServletResponse response) {
058: super (stack, request, response);
059: }
060:
061: protected String getDefaultTemplate() {
062: return TEMPLATE;
063: }
064:
065: public boolean end(Writer writer, String body) {
066: if (sortableAttr && model instanceof SortableTableModel) {
067: LOG.debug("we are looking for " + getSortColumnLinkName());
068:
069: String sortColumn = request
070: .getParameter(getSortColumnLinkName());
071: String sortOrder = request
072: .getParameter(getSortOrderLinkName());
073:
074: try {
075: if ((sortColumn != null) || (sortOrder != null)) {
076: if (sortColumn != null) {
077: try {
078: this .sortColumn = Integer
079: .parseInt(sortColumn);
080: } catch (Exception ex) {
081: if (LOG.isDebugEnabled()) {
082: LOG
083: .debug("coudn't convert column, take default");
084: }
085: }
086: }
087:
088: if (sortOrder != null) {
089: this .sortOrder = sortOrder;
090: }
091: } else {
092: LOG.debug("no sorting info in the request");
093: }
094:
095: if (this .sortColumn >= 0) {
096: LOG.debug("we have the sortColumn "
097: + Integer.toString(this .sortColumn));
098: LOG
099: .debug("we have the sortOrder "
100: + this .sortOrder);
101:
102: try {
103: ((SortableTableModel) model).sort(
104: this .sortColumn, this .sortOrder);
105: } catch (Exception ex) {
106: if (LOG.isDebugEnabled()) {
107: LOG.debug("couldn't sort the data");
108: }
109: }
110:
111: LOG.debug("we just sorted the data");
112: }
113: } catch (Exception e) {
114: throw new StrutsException("Error with WebTable: "
115: + toString(e), e);
116: }
117: }
118:
119: return super .end(writer, body);
120: }
121:
122: public WebTableColumn getColumn(int index) {
123: try {
124: return (columns[index]);
125: } catch (Exception E) {
126: //blank
127: }
128:
129: return null;
130: }
131:
132: protected void evaluateExtraParams() {
133: if (modelName != null) {
134: modelName = findString(modelName);
135:
136: Object obj = stack.findValue(this .modelName);
137:
138: if (obj instanceof TableModel) {
139: setModel((TableModel) obj);
140: }
141: }
142:
143: super .evaluateExtraParams();
144: }
145:
146: protected int getNumberOfVisibleColumns() {
147: int count = 0;
148:
149: for (int i = 0; i < columns.length; ++i) {
150: if (!columns[i].isHidden()) {
151: ++count;
152: }
153: }
154:
155: return count;
156: }
157:
158: public int getColumnCount() {
159: return (columns.length);
160: }
161:
162: public void setColumnDisplayName(int column, String displayName) {
163: columns[column].setDisplayName(displayName);
164: }
165:
166: public void getColumnDisplayName(int column) {
167: columns[column].getDisplayName();
168: }
169:
170: public void setColumnHidden(int column, boolean hide) {
171: columns[column].setHidden(hide);
172: }
173:
174: public boolean isColumnHidden(int column) {
175: return columns[column].isHidden();
176: }
177:
178: public void setColumnRenderer(int column, CellRenderer renderer) {
179: columns[column].setRenderer(renderer);
180: }
181:
182: public CellRenderer getColumnRenderer(int column) {
183: return columns[column].getRenderer();
184: }
185:
186: public WebTableColumn[] getColumns() {
187: return columns;
188: }
189:
190: public String[] getFormattedRow(int row) {
191: ArrayList data = new ArrayList(getNumberOfVisibleColumns());
192:
193: for (int i = 0; i < getColumnCount(); ++i) {
194: if (columns[i].isVisible()) {
195: data.add(columns[i].getRenderer().renderCell(this ,
196: model.getValueAt(row, i), row, i));
197: }
198: }
199:
200: return (String[]) data.toArray(new String[0]);
201: }
202:
203: public void setModel(TableModel model) {
204: this .model = model;
205: columns = new WebTableColumn[this .model.getColumnCount()];
206:
207: for (int i = 0; i < columns.length; ++i) {
208: columns[i] = new WebTableColumn(
209: this .model.getColumnName(i), i);
210: }
211:
212: if ((sortableAttr)
213: && !(this .model instanceof SortableTableModel)) {
214: this .model = new SortFilterModel(this .model);
215: }
216: }
217:
218: public TableModel getModel() {
219: return (model);
220: }
221:
222: /**
223: * The name of model to use
224: * @s.tagattribute required="true" type="String"
225: */
226: public void setModelName(String modelName) {
227: this .modelName = modelName;
228: }
229:
230: public String getModelName() {
231: return modelName;
232: }
233:
234: public Object getRawData(int row, int column) {
235: return model.getValueAt(row, column);
236: }
237:
238: public Iterator getRawDataRowIterator() {
239: return new WebTableRowIterator(this ,
240: WebTableRowIterator.RAW_DATA);
241: }
242:
243: public Object[] getRow(int row) {
244: ArrayList data = new ArrayList(getNumberOfVisibleColumns());
245:
246: for (int i = 0; i < getColumnCount(); ++i) {
247: if (columns[i].isVisible()) {
248: data.add(model.getValueAt(row, i));
249: }
250: }
251:
252: return data.toArray(new Object[0]);
253: }
254:
255: public int getRowCount() {
256: return model.getRowCount();
257: }
258:
259: public Iterator getRowIterator() {
260: return new WebTableRowIterator(this );
261: }
262:
263: @StrutsTagAttribute(description="Index of column to sort data by",type="Integer")
264: public void setSortColumn(int sortColumn) {
265: this .sortColumn = sortColumn;
266: }
267:
268: public int getSortColumn() {
269: if (model instanceof SortableTableModel) {
270: return ((SortableTableModel) model).getSortedColumnNumber();
271: }
272:
273: return -1;
274: }
275:
276: public String getSortColumnLinkName() {
277: return "WEBTABLE_" + modelName + "_SORT_COLUMN";
278: }
279:
280: @StrutsTagAttribute(description="Set sort order. Allowed values are NONE, ASC and DESC",defaultValue="NONE")
281: public void setSortOrder(String sortOrder) {
282: if (sortOrder.equals(SortableTableModel.NONE)) {
283: this .sortOrder = SortableTableModel.NONE;
284: } else if (sortOrder.equals(SortableTableModel.DESC)) {
285: this .sortOrder = SortableTableModel.DESC;
286: } else if (sortOrder.equals(SortableTableModel.ASC)) {
287: this .sortOrder = SortableTableModel.ASC;
288: } else {
289: this .sortOrder = SortableTableModel.NONE;
290: }
291: }
292:
293: public String getSortOrder() {
294: if ((model instanceof SortableTableModel)
295: && (getSortColumn() >= 0)) {
296: return ((SortableTableModel) model)
297: .getSortedDirection(getSortColumn());
298: }
299:
300: return SortableTableModel.NONE;
301: }
302:
303: public String getSortOrderLinkName() {
304: return "WEBTABLE_" + modelName + "_SORT_ORDER";
305: }
306:
307: @StrutsTagAttribute(description="Whether the table should be sortable. Requires that model implements " + "org.apache.struts2.components.table.SortableTableModel if set to true.",type="Boolean",defaultValue="false")
308: public void setSortable(boolean sortable) {
309: sortableAttr = sortable;
310:
311: if ((sortableAttr) && (model != null)
312: && !(model instanceof SortableTableModel)) {
313: model = new SortFilterModel(model);
314: }
315: }
316:
317: public boolean isSortable() {
318: return sortableAttr;
319: }
320:
321: /**
322: * inner class to iteratoe over a row of the table.
323: * It can return formatted data, using the columnRenderer
324: * for the column or it can return the raw data.
325: */
326: public class WebTableRowIterator implements Iterator {
327: public static final int FORMATTED_DATA = 0;
328: public static final int RAW_DATA = 1;
329: protected WebTable _table;
330: protected int _curRow = 0;
331: protected int _mode = 0;
332:
333: protected WebTableRowIterator(WebTable table) {
334: this (table, FORMATTED_DATA);
335: }
336:
337: protected WebTableRowIterator(WebTable table, int mode) {
338: _table = table;
339: _mode = mode;
340: }
341:
342: public boolean hasNext() {
343: if (_table == null) {
344: return false;
345: }
346:
347: return (_table.getRowCount() > _curRow);
348: }
349:
350: public Object next() throws NoSuchElementException {
351: if (_table == null) {
352: throw new NoSuchElementException("WebTable is null");
353: }
354:
355: if (!hasNext()) {
356: throw new NoSuchElementException(
357: "Beyond end of WebTable");
358: }
359:
360: if (_mode == RAW_DATA) {
361: return _table.getRow(_curRow++);
362: }
363:
364: return _table.getFormattedRow(_curRow++);
365: }
366:
367: public void remove() throws UnsupportedOperationException,
368: IllegalStateException {
369: throw new UnsupportedOperationException(
370: "Remove not supported in WebTable");
371: }
372: }
373: }
|