Source Code Cross Referenced for DBTableModel.java in  » ERP-CRM-Financial » Personal-Finance-Manager » br » com » gfpshare » beans » 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 » ERP CRM Financial » Personal Finance Manager » br.com.gfpshare.beans.table 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on 11/04/2004
003:         * 
004:         * Swing Components - visit http://sf.net/projects/gfd
005:         * 
006:         * Copyright (C) 2004  Igor Regis da Silva Simões
007:         * 
008:         * This program is free software; you can redistribute it and/or
009:         * modify it under the terms of the GNU General Public License
010:         * as published by the Free Software Foundation; either version 2
011:         * of the License, or (at your option) any later version.
012:         *
013:         * This program is distributed in the hope that it will be useful,
014:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
015:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016:         * GNU General Public License for more details.
017:         *
018:         * You should have received a copy of the GNU General Public License
019:         * along with this program; if not, write to the Free Software
020:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
021:         * 
022:         */
023:
024:        package br.com.gfpshare.beans.table;
025:
026:        import java.math.BigDecimal;
027:        import java.sql.Date;
028:        import java.sql.ResultSet;
029:        import java.sql.ResultSetMetaData;
030:        import java.sql.SQLException;
031:        import java.sql.Timestamp;
032:        import java.util.HashMap;
033:        import java.util.Map;
034:
035:        import javax.swing.table.AbstractTableModel;
036:
037:        import br.com.gfpshare.beans.DBCollectionModel;
038:        import br.com.gfpshare.db.DAO;
039:        import br.com.gfpshare.db.DAOCreationException;
040:        import br.com.gfpshare.db.DAOProvider;
041:        import br.com.gfpshare.db.PersistentObject;
042:        import br.com.gfpshare.plugin.core.DynamicClassLoader;
043:
044:        /**
045:         * Modelo de dados que deverão ser representados através de um DBTable
046:         * 
047:         * @author Igor Regis da Silva Simões
048:         */
049:        public class DBTableModel<E extends PersistentObject> extends
050:                AbstractTableModel implements  DBCollectionModel<E> {
051:            /**
052:             * Dados que compoem o modelo
053:             */
054:            private ResultSet dados = null;
055:
056:            /**
057:             * Tipo de dados carregados no modelo
058:             */
059:            private E dataType = null;
060:
061:            private int rowCount;
062:
063:            /**
064:             * Creates a new instance of DataBaseTableModel
065:             * 
066:             * @param dataType Tipo de dados do modelo
067:             */
068:            public DBTableModel(E dataType) {
069:                setDataType(dataType);
070:            }
071:
072:            /**
073:             * @see br.com.gfpshare.beans.DBCollectionModel#loadDados()
074:             */
075:            public void loadDados() throws DAOCreationException, SQLException {
076:                if (getDataType() != null) {
077:                    String classe = dataType.getClass().getSimpleName();
078:                    DAO dAO = DAOProvider.getControllerProvider()
079:                            .getController(classe);
080:                    dados = dAO.getAll(dados, dataType);
081:                    rowCount = -1;
082:                    fireTableDataChanged();
083:                }
084:            }
085:
086:            /**
087:             * @see br.com.gfpshare.beans.DBCollectionModel#getDados()
088:             */
089:            public ResultSet getDados() {
090:                try {
091:                    if (dados != null)
092:                        dados.isFirst();
093:                } catch (SQLException e) {
094:                    //Fazemos o tratamento do erro pos do contrário a tela pode
095:                    //tentar exibir o conteúdo de um ResultSet que esteja fechado
096:                    if (e.getErrorCode() == -158)//ResultSet fechado
097:                        this .dados = null;
098:                    try {
099:                        loadDados();
100:                    } catch (DAOCreationException e1) {
101:                        // //TODO Auto-generated catch block
102:                        e1.printStackTrace();
103:                    } catch (SQLException e1) {
104:                        // //TODO Auto-generated catch block
105:                        e1.printStackTrace();
106:                    }
107:                }
108:                return this .dados;
109:            }
110:
111:            /**
112:             * @see br.com.gfpshare.beans.DBCollectionModel#getDataAt(int)
113:             */
114:            public Map<String, Object> getDataAt(int index) throws SQLException {
115:                if (getRowCount() == 0)
116:                    return null;
117:                HashMap<String, Object> data = new HashMap<String, Object>(1);
118:
119:                ResultSetMetaData metaData = dados.getMetaData();
120:
121:                int coluna;
122:                for (int i = 0; i < metaData.getColumnCount(); i++)
123:                    if ((coluna = findColumn(metaData.getColumnName(i + 1)) - 1) != -2)
124:                        data.put(metaData.getColumnName(i + 1), getValueAt(
125:                                index, coluna) == null ? null : getValueAt(
126:                                index, coluna));
127:                return data;
128:            }
129:
130:            /**
131:             * @see br.com.gfpshare.beans.DBCollectionModel#getPersistentObject(int)
132:             */
133:            public E getPersistentObject(int index) throws SQLException {
134:                if (getRowCount() == 0)
135:                    return null;
136:                E dado = null;
137:
138:                try {
139:                    dado = (E) getDataType().getClass().newInstance();
140:                } catch (InstantiationException ie) {
141:                    //TODO Excecao
142:                    ie.printStackTrace();
143:                } catch (IllegalAccessException iae) {
144:                    //TODO Excecao
145:                    iae.printStackTrace();
146:                }
147:
148:                dado.setDados(getDataAt(index));
149:                return dado;
150:            }
151:
152:            /**
153:             * @see javax.swing.table.TableModel#getColumnCount()
154:             */
155:            public int getColumnCount() {
156:                if (getDados() == null)
157:                    return 0;
158:
159:                try {
160:                    return dados.getMetaData().getColumnCount();
161:                } catch (SQLException sqle) {
162:                    //TODO Exceção
163:                    sqle.printStackTrace();
164:                }
165:                return -1;
166:            }
167:
168:            /**
169:             * @see javax.swing.table.TableModel#getRowCount()
170:             */
171:            public int getRowCount() {
172:                if (getDados() == null)
173:                    return 0;
174:                if (rowCount == -1)
175:                    try {
176:                        dados.last();
177:                        rowCount = dados.getRow();
178:                    } catch (SQLException sqle) {
179:                        //TODO Exceção
180:                    }
181:                return rowCount;
182:            }
183:
184:            /**
185:             * @see javax.swing.table.TableModel#getValueAt(int, int)
186:             */
187:            public Object getValueAt(int rowIndex, int columnIndex) {
188:                if (getDados() == null || getRowCount() == 0)
189:                    return null;
190:                try {
191:                    dados.absolute(rowIndex + 1);
192:                    Object dado = dados.getObject(++columnIndex);
193:
194:                    if (dado instanceof  Date)
195:                        return new java.util.Date(((Date) dado).getTime());
196:                    else if (dado instanceof  Timestamp)
197:                        return new java.util.Date(((Timestamp) dado).getTime());
198:                    else if (dado instanceof  BigDecimal)
199:                        return new Double(((BigDecimal) dado).doubleValue());
200:
201:                    return dado;
202:                } catch (SQLException sqle) {
203:                    //TODO Exceção
204:                    sqle.printStackTrace();
205:                    return null;
206:                }
207:            }
208:
209:            /**
210:             * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
211:             */
212:            @Override
213:            public Class getColumnClass(int columnIndex) {
214:                int x = columnIndex;
215:
216:                if (getDados() == null)
217:                    return null;
218:
219:                try {
220:                    dados.first();
221:                    Class classe = DynamicClassLoader.getClassLoader()
222:                            .loadClass(
223:                                    dados.getMetaData().getColumnClassName(
224:                                            ++columnIndex));
225:
226:                    if (classe.getName().equals(Date.class.getName())
227:                            || classe.getName().equals(
228:                                    Timestamp.class.getName()))
229:                        return java.util.Date.class;
230:                    else if (classe.getName()
231:                            .equals(BigDecimal.class.getName()))
232:                        return Double.class;
233:
234:                    return classe;
235:                } catch (SQLException sqle) {
236:                    //TODO Exceção
237:                    sqle.printStackTrace();
238:                } catch (ClassNotFoundException cnfe) {
239:                    //TODO Exceção
240:                    cnfe.printStackTrace();
241:                }
242:
243:                return null;
244:            }
245:
246:            /**
247:             * @see javax.swing.table.AbstractTableModel#getColumnName(int)
248:             */
249:            @Override
250:            public String getColumnName(int columnIndex) {
251:                int x = columnIndex;
252:
253:                try {
254:                    dados.first();
255:                    return dados.getMetaData().getColumnName(++columnIndex);
256:                } catch (SQLException sqle) {
257:                    //TODO Exceção
258:                    sqle.printStackTrace();
259:                }
260:                return null;
261:            }
262:
263:            /**
264:             * @see javax.swing.table.AbstractTableModel#findColumn(java.lang.String)
265:             */
266:            @Override
267:            public int findColumn(String columnName) {
268:                if (getDados() == null)
269:                    return -1;
270:
271:                try {
272:                    dados.first();
273:                    for (int i = 1; i <= dados.getMetaData().getColumnCount(); i++) {
274:                        if (dados.getMetaData().getColumnName(i).equals(
275:                                columnName))
276:                            return i;
277:                    }
278:                } catch (SQLException sqle) {
279:                    //TODO Exceção
280:                    sqle.printStackTrace();
281:                }
282:                return -1;
283:            }
284:
285:            /**
286:             * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
287:             */
288:            @Override
289:            public boolean isCellEditable(int rowIndex, int columnIndex) {
290:                return false;
291:            }
292:
293:            /**
294:             * @see br.com.gfpshare.beans.DBCollectionModel#setDataType(br.com.gfpshare.db.PersistentObject)
295:             */
296:            public void setDataType(E dataType) {
297:                this .dataType = dataType;
298:            }
299:
300:            /**
301:             * @see br.com.gfpshare.beans.DBCollectionModel#getDataType()
302:             */
303:            public E getDataType() {
304:                return dataType;
305:            }
306:
307:            /**
308:             * @see br.com.gfpshare.beans.DBCollectionModel#filter()
309:             */
310:            public void filter() throws DAOCreationException, SQLException {
311:                if (getDataType() != null) {
312:                    String classe = dataType.getClass().getName();
313:                    DAO dAO = DAOProvider
314:                            .getControllerProvider()
315:                            .getController(
316:                                    classe
317:                                            .substring(classe.lastIndexOf('.') + 1));
318:                    dados = dAO.filterBy(dados, dataType);
319:                    rowCount = -1;
320:                    fireTableDataChanged();
321:                }
322:            }
323:
324:            /**
325:             * @see java.lang.Object#finalize()
326:             */
327:            @Override
328:            protected void finalize() throws Throwable {
329:                super .finalize();
330:                fecharConexaoDB();
331:            }
332:
333:            /**
334:             * 
335:             *
336:             */
337:            private void fecharConexaoDB() {
338:                if (getDados() != null) {
339:                    String classe = dataType.getClass().getName();
340:                    DAO dAO;
341:                    try {
342:                        dAO = DAOProvider.getControllerProvider()
343:                                .getController(
344:                                        classe.substring(classe
345:                                                .lastIndexOf('.') + 1));
346:                        dAO.freeResource(dados);
347:                    } catch (DAOCreationException e) {
348:                        // //TODO Auto-generated catch block
349:                        e.printStackTrace();
350:                    } catch (SQLException e1) {
351:                        // //TODO Auto-generated catch block
352:                        e1.printStackTrace();
353:                    }
354:                }
355:            }
356:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.