KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > table > WebTable


1 package com.opensymphony.webwork.components.table;
2
3 import com.opensymphony.webwork.components.GenericUIBean;
4 import com.opensymphony.webwork.components.table.renderer.CellRenderer;
5 import com.opensymphony.xwork.util.OgnlValueStack;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11 import javax.swing.table.TableModel JavaDoc;
12 import java.io.Writer JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.NoSuchElementException JavaDoc;
16
17 /**
18  * User: plightbo
19  * Date: Jul 31, 2005
20  * Time: 9:40:11 PM
21  */

22 public class WebTable extends GenericUIBean {
23     private static final Log LOG = LogFactory.getLog(WebTable.class);
24
25     final public static String JavaDoc TEMPLATE = "table";
26
27     protected String JavaDoc sortOrder = SortableTableModel.NONE;
28     protected String JavaDoc modelName = null;
29     protected TableModel JavaDoc model = null;
30     protected WebTableColumn[] columns = null;
31     protected boolean sortableAttr = false;
32     protected int sortColumn = -1;
33     protected int curRow = 0;
34
35     public WebTable(OgnlValueStack stack, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
36         super(stack, request, response);
37     }
38
39     protected String JavaDoc getDefaultTemplate() {
40         return TEMPLATE;
41     }
42
43     public void end(Writer JavaDoc writer) {
44         if (sortableAttr && model instanceof SortableTableModel) {
45             LOG.debug("we are looking for " + getSortColumnLinkName());
46
47             String JavaDoc sortColumn = request.getParameter(getSortColumnLinkName());
48             String JavaDoc sortOrder = request.getParameter(getSortOrderLinkName());
49
50             try {
51                 if ((sortColumn != null) || (sortOrder != null)) {
52                     if (sortColumn != null) {
53                         try {
54                             this.sortColumn = Integer.parseInt(sortColumn);
55                         } catch (Exception JavaDoc ex) {
56                             if (LOG.isDebugEnabled()) {
57                                 LOG.debug("coudn't convert column, take default");
58                             }
59                         }
60                     }
61
62                     if (sortOrder != null) {
63                         this.sortOrder = sortOrder;
64                     }
65                 } else {
66                     LOG.debug("no sorting info in the request");
67                 }
68
69                 if (this.sortColumn >= 0) {
70                     LOG.debug("we have the sortColumn " + Integer.toString(this.sortColumn));
71                     LOG.debug("we have the sortOrder " + this.sortOrder);
72
73                     try {
74                         ((SortableTableModel) model).sort(this.sortColumn, this.sortOrder);
75                     } catch (Exception JavaDoc ex) {
76                         if (LOG.isDebugEnabled()) {
77                             LOG.debug("couldn't sort the data");
78                         }
79                     }
80
81                     LOG.debug("we just sorted the data");
82                 }
83             } catch (Exception JavaDoc e) {
84                 LOG.error(e);
85                 throw new RuntimeException JavaDoc("Error with WebTable: " + toString(e));
86             }
87         }
88
89         super.end(writer);
90     }
91
92     public WebTableColumn getColumn(int index) {
93         try {
94             return (columns[index]);
95         } catch (Exception JavaDoc E) {
96             //blank
97
}
98
99         return null;
100     }
101
102     protected void evaluateExtraParams() {
103         if (modelName != null) {
104             modelName = findString(modelName);
105
106             Object JavaDoc obj = stack.findValue(this.modelName);
107
108             if (obj instanceof TableModel JavaDoc) {
109                 setModel((TableModel JavaDoc) obj);
110             }
111         }
112
113         super.evaluateExtraParams();
114     }
115
116     protected int getNumberOfVisibleColumns() {
117         int count = 0;
118
119         for (int i = 0; i < columns.length; ++i) {
120             if (!columns[i].isHidden()) {
121                 ++count;
122             }
123         }
124
125         return count;
126     }
127
128     public int getColumnCount() {
129         return (columns.length);
130     }
131
132     public void setColumnDisplayName(int column, String JavaDoc displayName) {
133         columns[column].setDisplayName(displayName);
134     }
135
136     public void getColumnDisplayName(int column) {
137         columns[column].getDisplayName();
138     }
139
140     public void setColumnHidden(int column, boolean hide) {
141         columns[column].setHidden(hide);
142     }
143
144     public boolean isColumnHidden(int column) {
145         return columns[column].isHidden();
146     }
147
148     public void setColumnRenderer(int column, CellRenderer renderer) {
149         columns[column].setRenderer(renderer);
150     }
151
152     public CellRenderer getColumnRenderer(int column) {
153         return columns[column].getRenderer();
154     }
155
156     public WebTableColumn[] getColumns() {
157         return columns;
158     }
159
160     public String JavaDoc[] getFormattedRow(int row) {
161         ArrayList JavaDoc data = new ArrayList JavaDoc(getNumberOfVisibleColumns());
162
163         for (int i = 0; i < getColumnCount(); ++i) {
164             if (columns[i].isVisible()) {
165                 data.add(columns[i].getRenderer().renderCell(this, model.getValueAt(row, i), row, i));
166             }
167         }
168
169         return (String JavaDoc[]) data.toArray(new String JavaDoc[0]);
170     }
171
172     public void setModel(TableModel JavaDoc model) {
173         this.model = model;
174         columns = new WebTableColumn[this.model.getColumnCount()];
175
176         for (int i = 0; i < columns.length; ++i) {
177             columns[i] = new WebTableColumn(this.model.getColumnName(i), i);
178         }
179
180         if ((sortableAttr) && !(this.model instanceof SortableTableModel)) {
181             this.model = new SortFilterModel(this.model);
182         }
183     }
184
185     public TableModel JavaDoc getModel() {
186         return (model);
187     }
188
189     public void setModelName(String JavaDoc modelName) {
190         this.modelName = modelName;
191     }
192
193     public String JavaDoc getModelName() {
194         return modelName;
195     }
196
197     public Object JavaDoc getRawData(int row, int column) {
198         return model.getValueAt(row, column);
199     }
200
201     public Iterator JavaDoc getRawDataRowIterator() {
202         return new WebTableRowIterator(this, WebTableRowIterator.RAW_DATA);
203     }
204
205     public Object JavaDoc[] getRow(int row) {
206         ArrayList JavaDoc data = new ArrayList JavaDoc(getNumberOfVisibleColumns());
207
208         for (int i = 0; i < getColumnCount(); ++i) {
209             if (columns[i].isVisible()) {
210                 data.add(model.getValueAt(row, i));
211             }
212         }
213
214         return data.toArray(new Object JavaDoc[0]);
215     }
216
217     public int getRowCount() {
218         return model.getRowCount();
219     }
220
221     public Iterator JavaDoc getRowIterator() {
222         return new WebTableRowIterator(this);
223     }
224
225     public void setSortColumn(int sortColumn) {
226         this.sortColumn = sortColumn;
227     }
228
229     public int getSortColumn() {
230         if (model instanceof SortableTableModel) {
231             return ((SortableTableModel) model).getSortedColumnNumber();
232         }
233
234         return -1;
235     }
236
237     public String JavaDoc getSortColumnLinkName() {
238         return "WEBTABLE_" + modelName + "_SORT_COLUMN";
239     }
240
241     public void setSortOrder(String JavaDoc sortOrder) {
242         if (sortOrder.equals(SortableTableModel.NONE)) {
243             this.sortOrder = SortableTableModel.NONE;
244         } else if (sortOrder.equals(SortableTableModel.DESC)) {
245             this.sortOrder = SortableTableModel.DESC;
246         } else if (sortOrder.equals(SortableTableModel.ASC)) {
247             this.sortOrder = SortableTableModel.ASC;
248         } else {
249             this.sortOrder = SortableTableModel.NONE;
250         }
251     }
252
253     public String JavaDoc getSortOrder() {
254         if ((model instanceof SortableTableModel) && (getSortColumn() >= 0)) {
255             return ((SortableTableModel) model).getSortedDirection(getSortColumn());
256         }
257
258         return SortableTableModel.NONE;
259     }
260
261     public String JavaDoc getSortOrderLinkName() {
262         return "WEBTABLE_" + modelName + "_SORT_ORDER";
263     }
264
265     public void setSortable(boolean sortable) {
266         sortableAttr = sortable;
267
268         if ((sortableAttr) && (model != null) && !(model instanceof SortableTableModel)) {
269             model = new SortFilterModel(model);
270         }
271     }
272
273     public boolean isSortable() {
274         return sortableAttr;
275     }
276
277     /**
278      * inner class to iteratoe over a row of the table.
279      * It can return formatted data, using the columnRenderer
280      * for the column or it can return the raw data.
281      */

282     public class WebTableRowIterator implements Iterator JavaDoc {
283         public static final int FORMATTED_DATA = 0;
284         public static final int RAW_DATA = 1;
285         protected WebTable _table;
286         protected int _curRow = 0;
287         protected int _mode = 0;
288
289         protected WebTableRowIterator(WebTable table) {
290             this(table, FORMATTED_DATA);
291         }
292
293         protected WebTableRowIterator(WebTable table, int mode) {
294             _table = table;
295             _mode = mode;
296         }
297
298         public boolean hasNext() {
299             if (_table == null) {
300                 return false;
301             }
302
303             return (_table.getRowCount() > _curRow);
304         }
305
306         public Object JavaDoc next() throws NoSuchElementException JavaDoc {
307             if (_table == null) {
308                 throw new NoSuchElementException JavaDoc("WebTable is null");
309             }
310
311             if (!hasNext()) {
312                 throw new NoSuchElementException JavaDoc("Beyond end of WebTable");
313             }
314
315             if (_mode == RAW_DATA) {
316                 return _table.getRow(_curRow++);
317             }
318
319             return _table.getFormattedRow(_curRow++);
320         }
321
322         public void remove() throws UnsupportedOperationException JavaDoc, IllegalStateException JavaDoc {
323             throw new UnsupportedOperationException JavaDoc("Remove not supported in WebTable");
324         }
325     }
326 }
327
Popular Tags