Source Code Cross Referenced for DefaultTableModel.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » 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 » Apache Harmony Java SE » javax package » javax.swing.table 
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:         * @author Anton Avtamonov
020:         * @version $Revision$
021:         */package javax.swing.table;
022:
023:        import java.util.Vector;
024:
025:        import javax.swing.event.TableModelEvent;
026:
027:        public class DefaultTableModel extends AbstractTableModel {
028:            protected Vector dataVector;
029:            protected Vector columnIdentifiers;
030:
031:            public DefaultTableModel() {
032:                setDataVector(new Vector(), new Vector());
033:            }
034:
035:            public DefaultTableModel(final int rowCount, final int columnCount) {
036:                setDataVector(new Vector(), new Vector());
037:                setColumnCount(columnCount);
038:                setRowCount(rowCount);
039:            }
040:
041:            public DefaultTableModel(final Vector columnNames,
042:                    final int rowCount) {
043:                setDataVector(new Vector(), columnNames);
044:                setRowCount(rowCount);
045:            }
046:
047:            public DefaultTableModel(final Object[] columnNames,
048:                    final int rowCount) {
049:                this (convertToVector(columnNames), rowCount);
050:            }
051:
052:            public DefaultTableModel(final Vector data, final Vector columnNames) {
053:                setDataVector(data, columnNames);
054:            }
055:
056:            public DefaultTableModel(final Object[][] data,
057:                    final Object[] columnNames) {
058:                setDataVector(data, columnNames);
059:            }
060:
061:            public Vector getDataVector() {
062:                return dataVector;
063:            }
064:
065:            public void setDataVector(final Vector data,
066:                    final Vector identifiers) {
067:                columnIdentifiers = identifiers != null ? identifiers
068:                        : new Vector();
069:                dataVector = data != null ? data : new Vector();
070:                alignRowsLengthAndCount();
071:                for (int i = 0; i < getRowCount(); i++) {
072:                    Vector row = (Vector) dataVector.get(i);
073:                    for (int j = 0; j < getColumnCount(); j++) {
074:                        Vector dataRow = (Vector) data.get(i);
075:                        if (dataRow.size() < j) {
076:                            row.setElementAt(dataRow.get(j), i);
077:                        }
078:                    }
079:                }
080:
081:                fireTableStructureChanged();
082:            }
083:
084:            public void setDataVector(final Object[][] dataVector,
085:                    final Object[] columnIdentifiers) {
086:                setDataVector(convertToVector(dataVector),
087:                        convertToVector(columnIdentifiers));
088:            }
089:
090:            public void newDataAvailable(final TableModelEvent e) {
091:                fireTableChanged(e);
092:            }
093:
094:            public void newRowsAdded(final TableModelEvent e) {
095:                if (e.getFirstRow() >= 0 && e.getLastRow() >= e.getFirstRow()) {
096:                    for (int i = e.getFirstRow(); i <= e.getLastRow(); i++) {
097:                        ((Vector) dataVector.get(i)).setSize(getColumnCount());
098:                    }
099:                }
100:                fireTableChanged(e);
101:            }
102:
103:            public void rowsRemoved(final TableModelEvent e) {
104:                fireTableChanged(e);
105:            }
106:
107:            public void setNumRows(final int rowCount) {
108:                setRowCount(rowCount);
109:            }
110:
111:            public void setRowCount(final int rowCount) {
112:                int oldSize = dataVector.size();
113:                if (oldSize < rowCount) {
114:                    for (int i = oldSize; i < rowCount; i++) {
115:                        dataVector.add(new Vector());
116:                    }
117:                    newRowsAdded(new TableModelEvent(this , oldSize,
118:                            rowCount - 1, TableModelEvent.HEADER_ROW,
119:                            TableModelEvent.INSERT));
120:                } else if (oldSize > rowCount) {
121:                    dataVector.setSize(rowCount);
122:                    rowsRemoved(new TableModelEvent(this , rowCount,
123:                            oldSize - 1, TableModelEvent.HEADER_ROW,
124:                            TableModelEvent.DELETE));
125:                }
126:            }
127:
128:            public void addRow(final Vector rowData) {
129:                dataVector.add(rowData != null ? rowData : new Vector());
130:                newRowsAdded(new TableModelEvent(this , dataVector.size() - 1,
131:                        dataVector.size() - 1, TableModelEvent.HEADER_ROW,
132:                        TableModelEvent.INSERT));
133:            }
134:
135:            public void addRow(final Object[] rowData) {
136:                addRow(convertToVector(rowData));
137:            }
138:
139:            public void insertRow(final int row, final Vector rowData) {
140:                dataVector.add(row, rowData != null ? rowData : new Vector());
141:                newRowsAdded(new TableModelEvent(this , row, row,
142:                        TableModelEvent.HEADER_ROW, TableModelEvent.INSERT));
143:            }
144:
145:            public void insertRow(final int row, final Object[] rowData) {
146:                insertRow(row, convertToVector(rowData));
147:            }
148:
149:            public void moveRow(final int start, final int end, final int to) {
150:                int intervalLength = end - start + 1;
151:                if (start < to) {
152:                    for (int i = 0; i < intervalLength; i++) {
153:                        Object row = dataVector.remove(start);
154:                        dataVector.add(to + intervalLength - 1, row);
155:                    }
156:                    fireTableRowsUpdated(start, to + intervalLength - 1);
157:                } else {
158:                    for (int i = 0; i < intervalLength; i++) {
159:                        Object row = dataVector.remove(start + i);
160:                        dataVector.add(to + i, row);
161:                    }
162:                    fireTableRowsUpdated(to, end);
163:                }
164:            }
165:
166:            public void removeRow(final int row) {
167:                dataVector.remove(row);
168:                rowsRemoved(new TableModelEvent(this , row, row,
169:                        TableModelEvent.HEADER_ROW, TableModelEvent.DELETE));
170:            }
171:
172:            public void setColumnIdentifiers(final Vector identifiers) {
173:                columnIdentifiers = identifiers;
174:                alignRowsLengthAndCount();
175:                fireTableStructureChanged();
176:            }
177:
178:            public void setColumnIdentifiers(final Object[] identifiers) {
179:                setColumnIdentifiers(convertToVector(identifiers));
180:            }
181:
182:            public void setColumnCount(final int columnCount) {
183:                columnIdentifiers.setSize(columnCount);
184:                alignRowsLengthAndCount();
185:                fireTableStructureChanged();
186:            }
187:
188:            public void addColumn(final Object columnName) {
189:                addColumn(columnName, (Vector) null);
190:            }
191:
192:            public void addColumn(final Object columnName,
193:                    final Vector columnData) {
194:                columnIdentifiers.add(columnName);
195:                alignRowsLengthAndCount();
196:
197:                if (columnData != null) {
198:                    for (int i = 0; i < dataVector.size(); i++) {
199:                        Vector row = (Vector) dataVector.get(i);
200:                        if (columnData.size() > i) {
201:                            row.setElementAt(columnData.get(i),
202:                                    columnIdentifiers.size() - 1);
203:                        } else {
204:                            break;
205:                        }
206:                    }
207:                }
208:                fireTableStructureChanged();
209:            }
210:
211:            public void addColumn(final Object columnName,
212:                    final Object[] columnData) {
213:                addColumn(columnName, convertToVector(columnData));
214:            }
215:
216:            public int getRowCount() {
217:                return dataVector.size();
218:            }
219:
220:            public int getColumnCount() {
221:                return columnIdentifiers.size();
222:            }
223:
224:            public String getColumnName(final int column) {
225:                return columnIdentifiers.size() > column
226:                        && columnIdentifiers.get(column) != null ? columnIdentifiers
227:                        .get(column).toString()
228:                        : super .getColumnName(column);
229:            }
230:
231:            public boolean isCellEditable(final int row, final int column) {
232:                return true;
233:            }
234:
235:            public Object getValueAt(final int row, final int column) {
236:                return ((Vector) dataVector.get(row)).get(column);
237:            }
238:
239:            public void setValueAt(final Object value, final int row,
240:                    final int column) {
241:                ((Vector) dataVector.get(row)).setElementAt(value, column);
242:                fireTableCellUpdated(row, column);
243:            }
244:
245:            protected static Vector convertToVector(final Object[] data) {
246:                if (data == null) {
247:                    return null;
248:                }
249:                Vector result = new Vector();
250:                for (int i = 0; i < data.length; i++) {
251:                    result.add(data[i]);
252:                }
253:
254:                return result;
255:            }
256:
257:            protected static Vector convertToVector(final Object[][] data) {
258:                if (data == null) {
259:                    return null;
260:                }
261:                Vector result = new Vector();
262:                for (int i = 0; i < data.length; i++) {
263:                    result.add(convertToVector(data[i]));
264:                }
265:
266:                return result;
267:            }
268:
269:            private void alignRowsLengthAndCount() {
270:                for (int i = 0; i < getRowCount(); i++) {
271:                    if (dataVector.size() == i) {
272:                        dataVector.add(new Vector());
273:                    }
274:                    Vector next = (Vector) dataVector.get(i);
275:                    if (next == null) {
276:                        next = new Vector();
277:                        dataVector.setElementAt(next, i);
278:                    }
279:                    next.setSize(getColumnCount());
280:                }
281:            }
282:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.