Source Code Cross Referenced for WebTable.java in  » J2EE » webwork-2.2.6 » com » opensymphony » webwork » components » table » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » webwork 2.2.6 » com.opensymphony.webwork.components.table 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.