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