Source Code Cross Referenced for ObjectTableModel.java in  » Testing » jakarta-jmeter » org » apache » jorphan » gui » 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 » Testing » jakarta jmeter » org.apache.jorphan.gui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *   http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         * 
017:         */
018:
019:        package org.apache.jorphan.gui;
020:
021:        import java.util.ArrayList;
022:        import java.util.Arrays;
023:        import java.util.Iterator;
024:        import java.util.List;
025:
026:        import javax.swing.event.TableModelEvent;
027:        import javax.swing.table.DefaultTableModel;
028:
029:        import org.apache.jorphan.logging.LoggingManager;
030:        import org.apache.jorphan.reflect.Functor;
031:        import org.apache.log.Logger;
032:
033:        /**
034:         * @version $Revision: 571988 $
035:         */
036:        public class ObjectTableModel extends DefaultTableModel {
037:            private static Logger log = LoggingManager.getLoggerForClass();
038:
039:            private transient ArrayList objects = new ArrayList();
040:
041:            private transient List headers = new ArrayList();
042:
043:            private transient ArrayList classes = new ArrayList();
044:
045:            private transient ArrayList readFunctors = new ArrayList();
046:
047:            private transient ArrayList writeFunctors = new ArrayList();
048:
049:            private transient Class objectClass = null; // if provided
050:
051:            /**
052:             * The ObjectTableModel is a TableModel whose rows are objects;
053:             * columns are defined as Functors on the object.
054:             * 
055:             * @param headers - Column names
056:             * @param _objClass - Object class that will be used
057:             * @param readFunctors - used to get the values
058:             * @param writeFunctors - used to set the values
059:             * @param editorClasses - class for each column
060:             */
061:            public ObjectTableModel(String[] headers, Class _objClass,
062:                    Functor[] readFunctors, Functor[] writeFunctors,
063:                    Class[] editorClasses) {
064:                this (headers, readFunctors, writeFunctors, editorClasses);
065:                this .objectClass = _objClass;
066:            }
067:
068:            /**
069:             * The ObjectTableModel is a TableModel whose rows are objects;
070:             * columns are defined as Functors on the object.
071:             * 
072:             * @param headers - Column names
073:             * @param readFunctors - used to get the values
074:             * @param writeFunctors - used to set the values
075:             * @param editorClasses - class for each column
076:             */
077:            public ObjectTableModel(String[] headers, Functor[] readFunctors,
078:                    Functor[] writeFunctors, Class[] editorClasses) {
079:                this .headers.addAll(Arrays.asList(headers));
080:                this .classes.addAll(Arrays.asList(editorClasses));
081:                this .readFunctors = new ArrayList(Arrays.asList(readFunctors));
082:                this .writeFunctors = new ArrayList(Arrays.asList(writeFunctors));
083:
084:                int numHeaders = headers.length;
085:
086:                int numClasses = classes.size();
087:                if (numClasses != numHeaders) {
088:                    log.warn("Header count=" + numHeaders
089:                            + " but classes count=" + numClasses);
090:                }
091:
092:                // Functor count = 0 is handled specially 
093:                int numWrite = writeFunctors.length;
094:                if (numWrite > 0 && numWrite != numHeaders) {
095:                    log.warn("Header count=" + numHeaders
096:                            + " but writeFunctor count=" + numWrite);
097:                }
098:
099:                int numRead = readFunctors.length;
100:                if (numRead > 0 && numRead != numHeaders) {
101:                    log.warn("Header count=" + numHeaders
102:                            + " but readFunctor count=" + numRead);
103:                }
104:            }
105:
106:            public Iterator iterator() {
107:                return objects.iterator();
108:            }
109:
110:            public void clearData() {
111:                int size = getRowCount();
112:                objects.clear();
113:                super .fireTableRowsDeleted(0, size);
114:            }
115:
116:            public void addRow(Object value) {
117:                log.debug("Adding row value: " + value);
118:                if (objectClass != null) {
119:                    final Class valueClass = value.getClass();
120:                    if (!objectClass.isAssignableFrom(valueClass)) {
121:                        throw new IllegalArgumentException(
122:                                "Trying to add class: " + valueClass.getName()
123:                                        + "; expecting class: "
124:                                        + objectClass.getName());
125:                    }
126:                }
127:                objects.add(value);
128:                super .fireTableRowsInserted(objects.size() - 1, objects.size());
129:            }
130:
131:            public void insertRow(Object value, int index) {
132:                objects.add(index, value);
133:                super .fireTableRowsInserted(index, index + 1);
134:            }
135:
136:            /**
137:             * @see javax.swing.table.TableModel#getColumnCount()
138:             */
139:            public int getColumnCount() {
140:                return headers.size();
141:            }
142:
143:            /**
144:             * @see javax.swing.table.TableModel#getColumnName(int)
145:             */
146:            public String getColumnName(int col) {
147:                return (String) headers.get(col);
148:            }
149:
150:            /**
151:             * @see javax.swing.table.TableModel#getRowCount()
152:             */
153:            public int getRowCount() {
154:                if (objects == null) {
155:                    return 0;
156:                }
157:                return objects.size();
158:            }
159:
160:            /**
161:             * @see javax.swing.table.TableModel#getValueAt(int, int)
162:             */
163:            public Object getValueAt(int row, int col) {
164:                log.debug("Getting row value");
165:                Object value = objects.get(row);
166:                if (headers.size() == 1 && col >= readFunctors.size())
167:                    return value;
168:                Functor getMethod = (Functor) readFunctors.get(col);
169:                if (getMethod != null && value != null) {
170:                    return getMethod.invoke(value);
171:                }
172:                return null;
173:            }
174:
175:            /**
176:             * @see javax.swing.table.TableModel#isCellEditable(int, int)
177:             */
178:            public boolean isCellEditable(int arg0, int arg1) {
179:                return true;
180:            }
181:
182:            /**
183:             * @see javax.swing.table.DefaultTableModel#moveRow(int, int, int)
184:             */
185:            public void moveRow(int start, int end, int to) {
186:                List subList = objects.subList(start, end);
187:                for (int x = end - 1; x >= start; x--) {
188:                    objects.remove(x);
189:                }
190:                objects.addAll(to, subList);
191:                super .fireTableChanged(new TableModelEvent(this ));
192:            }
193:
194:            /**
195:             * @see javax.swing.table.DefaultTableModel#removeRow(int)
196:             */
197:            public void removeRow(int row) {
198:                objects.remove(row);
199:                super .fireTableRowsDeleted(row, row);
200:            }
201:
202:            /**
203:             * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
204:             */
205:            public void setValueAt(Object cellValue, int row, int col) {
206:                if (row < objects.size()) {
207:                    Object value = objects.get(row);
208:                    if (col < writeFunctors.size()) {
209:                        Functor setMethod = (Functor) writeFunctors.get(col);
210:                        if (setMethod != null) {
211:                            setMethod.invoke(value, new Object[] { cellValue });
212:                            super .fireTableDataChanged();
213:                        }
214:                    } else if (headers.size() == 1) {
215:                        objects.set(row, cellValue);
216:                    }
217:                }
218:            }
219:
220:            /**
221:             * @see javax.swing.table.TableModel#getColumnClass(int)
222:             */
223:            public Class getColumnClass(int arg0) {
224:                return (Class) classes.get(arg0);
225:            }
226:
227:            /**
228:             * Check all registered functors.
229:             * <p>
230:             * <b>** only for use in unit test code **</b>
231:             * </p>
232:             * 
233:             * @param _value - an instance of the table model row data item 
234:             * (if null, use the class passed to the constructor).
235:             * 
236:             * @param caller - class of caller.
237:             * 
238:             * @return false if at least one Functor cannot be found.
239:             */
240:            public boolean checkFunctors(Object _value, Class caller) {
241:                Object value;
242:                if (_value == null && objectClass != null) {
243:                    try {
244:                        value = objectClass.newInstance();
245:                    } catch (InstantiationException e) {
246:                        log.error("Cannot create instance of class "
247:                                + objectClass.getName(), e);
248:                        return false;
249:                    } catch (IllegalAccessException e) {
250:                        log.error("Cannot create instance of class "
251:                                + objectClass.getName(), e);
252:                        return false;
253:                    }
254:                } else {
255:                    value = _value;
256:                }
257:                boolean status = true;
258:                for (int i = 0; i < getColumnCount(); i++) {
259:                    Functor setMethod = (Functor) writeFunctors.get(i);
260:                    if (setMethod != null) {
261:                        if (!setMethod.checkMethod(value, getColumnClass(i))) {
262:                            status = false;
263:                            log.warn(caller.getName()
264:                                    + " is attempting to use nonexistent "
265:                                    + setMethod.toString());
266:                        }
267:                    }
268:                    Functor getMethod = (Functor) readFunctors.get(i);
269:                    if (getMethod != null) {
270:                        if (!getMethod.checkMethod(value)) {
271:                            status = false;
272:                            log.warn(caller.getName()
273:                                    + " is attempting to use nonexistent "
274:                                    + getMethod.toString());
275:                        }
276:                    }
277:
278:                }
279:                return status;
280:            }
281:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.