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


001:        /*
002:         * Created on 23/04/2004
003:         * 
004:         * ============================================================================
005:         * GNU Lesser General Public License
006:         * ============================================================================
007:         *
008:         * Swing Components - visit http://sf.net/projects/gfd
009:         * 
010:         * Copyright (C) 2004  Igor Regis da Silva Simões
011:         * 
012:         * This library is free software; you can redistribute it and/or
013:         * modify it under the terms of the GNU Lesser General Public
014:         * License as published by the Free Software Foundation; either
015:         * version 2.1 of the License, or (at your option) any later version.
016:         * 
017:         * This library is distributed in the hope that it will be useful,
018:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
019:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
020:         * Lesser General Public License for more details.
021:         * 
022:         * You should have received a copy of the GNU Lesser General Public
023:         * License along with this library; if not, write to the Free Software
024:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
025:         */
026:
027:        package br.com.gfpshare.beans;
028:
029:        import java.math.BigDecimal;
030:        import java.sql.Date;
031:        import java.sql.ResultSet;
032:        import java.sql.ResultSetMetaData;
033:        import java.sql.SQLException;
034:        import java.sql.Timestamp;
035:        import java.util.HashMap;
036:        import java.util.Map;
037:
038:        import javax.swing.AbstractListModel;
039:        import javax.swing.ComboBoxModel;
040:
041:        import br.com.gfpshare.db.DAO;
042:        import br.com.gfpshare.db.DAOCreationException;
043:        import br.com.gfpshare.db.DAOEvent;
044:        import br.com.gfpshare.db.DAOListener;
045:        import br.com.gfpshare.db.DAOProvider;
046:        import br.com.gfpshare.db.PersistentObject;
047:
048:        /**
049:         * Modelo de dados que deverão ser representados através de um DBComboBox 
050:         * @author Igor Regis da Silva Simoes
051:         */
052:        public class DBComboBoxModel extends AbstractListModel implements 
053:                ComboBoxModel, DBCollectionModel, DAOListener {
054:            /**
055:             *	Dados que compoem o modelo
056:             */
057:            private ResultSet dados = null;
058:
059:            /**
060:             *	Objeto atualmente selecionado
061:             */
062:            private Object selectedObject = null;
063:
064:            /**
065:             *	Tipo de dados carregados no modelo
066:             */
067:            private PersistentObject dataType = null;
068:
069:            public static final String INSERT_NEW = "< INSERT NEW >";
070:
071:            /**
072:             * Gerencia o cache de dados deste componente
073:             */
074:            private CacheManager cacheManager = new CacheManager();
075:
076:            /**
077:             * Controller usado para acessar a tabela de dados
078:             */
079:            private DAO dAO = null;
080:
081:            /**
082:             * Cria um modelo com um determinado tipo de dados
083:             * @param dataType Tipo de dados do modelo
084:             */
085:            public DBComboBoxModel(PersistentObject dataType) {
086:                this .dataType = dataType;
087:            }
088:
089:            /**
090:             * @see br.com.gfpshare.beans.DBCollectionModel#loadDados()
091:             */
092:            public void loadDados() throws DAOCreationException, SQLException {
093:                cacheManager.resetCache();
094:                if (getDataType() != null) {
095:                    if (dAO != null) {
096:                        dAO.removeControllerListener(this );
097:                    }
098:                    String classe = dataType.getClass().getName();
099:                    dAO = DAOProvider.getControllerProvider().getController(
100:                            classe.substring(classe.lastIndexOf('.') + 1));
101:                    dAO.addControllerListener(this );
102:                    dados = dAO.filterBy(dados, dataType);
103:                    fireContentsChanged(this , -1, -1);
104:                } else
105:                    dados = null;
106:            }
107:
108:            /**
109:             * @see br.com.gfpshare.beans.DBCollectionModel#getDados()
110:             */
111:            public ResultSet getDados() {
112:                try {
113:                    if (dados != null)
114:                        dados.isFirst();
115:                } catch (SQLException e) {
116:                    //Fazemos o tratamento do erro pos do contrário a tela pode
117:                    //tentar exibir o conteúdo de um ResultSet que esteja fechado
118:                    if (e.getErrorCode() == -158)//ResultSet fechado
119:                        this .dados = null;
120:                    try {
121:                        loadDados();
122:                    } catch (DAOCreationException e1) {
123:                        // //TODO Auto-generated catch block
124:                        e1.printStackTrace();
125:                    } catch (SQLException e1) {
126:                        // //TODO Auto-generated catch block
127:                        e1.printStackTrace();
128:                    }
129:                }
130:                return this .dados;
131:            }
132:
133:            /**
134:             * @see br.com.gfpshare.beans.DBCollectionModel#getDataAt(int)
135:             */
136:            public Map<String, Object> getDataAt(int index) throws SQLException {
137:                index--;
138:                HashMap<String, Object> data = new HashMap<String, Object>(1);
139:
140:                if (index == 0 || getDados() == null)
141:                    return null;
142:
143:                Map<String, Object> c = cacheManager.getMap(index);
144:                if (c != null)
145:                    return c;
146:
147:                ResultSetMetaData metaData = dados.getMetaData();
148:                dados.beforeFirst();
149:                for (int i = 0; i < index; i++) {
150:                    dados.next();
151:                }
152:
153:                for (int i = 1; i <= metaData.getColumnCount(); i++) {
154:                    Object campo = dados.getObject(i) == null ? null : dados
155:                            .getObject(i);
156:
157:                    if (campo instanceof  Date)
158:                        campo = new java.util.Date(((Date) campo).getTime());
159:                    else if (campo instanceof  Timestamp)
160:                        campo = new java.util.Date(((Timestamp) campo)
161:                                .getTime());
162:                    else if (campo instanceof  BigDecimal)
163:                        campo = new Double(((BigDecimal) campo).doubleValue());
164:
165:                    data.put(metaData.getColumnName(i), campo);
166:                }
167:                cacheManager.add(index, data);
168:                return data;
169:
170:            }
171:
172:            /**
173:             * @see br.com.gfpshare.beans.DBCollectionModel#getPersistentObject(int)
174:             */
175:            public PersistentObject getPersistentObject(int index)
176:                    throws SQLException {
177:                if (index == 0 || getDados() == null)
178:                    return null;
179:
180:                PersistentObject dado = (PersistentObject) cacheManager
181:                        .get(index);
182:                if (dado != null)
183:                    return dado;
184:                try {
185:                    dado = getDataType().getClass().newInstance();
186:                } catch (InstantiationException ie) {
187:                    //TODO Excecao
188:                    ie.printStackTrace();
189:                } catch (IllegalAccessException iae) {
190:                    //TODO Excecao
191:                    iae.printStackTrace();
192:                }
193:
194:                Map dados = getDataAt(index);
195:                if (dados == null)
196:                    return null;
197:
198:                dado.setDados(dados);
199:                cacheManager.add(index, dado);
200:                return dado;
201:            }
202:
203:            /**
204:             * @see javax.swing.ListModel#getElementAt(int)
205:             */
206:            public Object getElementAt(int index) {
207:                if (index == 1) {
208:                    return INSERT_NEW;
209:                }
210:                try {
211:                    return getPersistentObject(index);
212:                } catch (SQLException sqle) {
213:                    //TODO Excecao
214:                }
215:                return null;
216:            }
217:
218:            /**
219:             * @see javax.swing.ListModel#getSize()
220:             */
221:            public int getSize() {
222:                if (getDados() == null)
223:                    return 0;
224:
225:                if (cacheManager.isCachedSizeClear())
226:                    try {
227:                        dados.last();
228:                        cacheManager.setSizeCached(dados.getRow() + 2);
229:                    } catch (SQLException sqle) {
230:                        //TODO Exceção
231:                        sqle.printStackTrace();
232:                    }
233:                return cacheManager.getSizeCached();
234:            }
235:
236:            /**
237:             * @see javax.swing.ComboBoxModel#getSelectedItem()
238:             */
239:            public Object getSelectedItem() {
240:                return selectedObject;
241:            }
242:
243:            /**
244:             * Retorna o indice selecionado deste data model
245:             * @return
246:             */
247:            public int getSelectedIndex() {
248:                return cacheManager.getSelectedIndex();
249:            }
250:
251:            /**
252:             * Seleciona um determinado PersistentObject da combobox
253:             * @param item Item a ser selecionado
254:             * @throws SQLException
255:             */
256:            public void setSelectedItem(PersistentObject item)
257:                    throws SQLException {
258:                for (int i = getSize() - 1; i > 0; i--) {
259:                    if (getPersistentObject(i) != null
260:                            && getPersistentObject(i).toString().equals(
261:                                    item.toString())) {
262:                        cacheManager.setSelectedIndex(i);
263:                        selectedObject = item;
264:                        fireContentsChanged(this , -1, -1);
265:                        return;
266:                    }
267:                }
268:                cacheManager.setSelectedIndex(-1);
269:                selectedObject = null;
270:                fireContentsChanged(this , -1, -1);
271:                return;
272:            }
273:
274:            /**
275:             * Determina o item selecionado na combobox
276:             * @param anObject Objeto que deve ser selecionado
277:             * @param key Chave usada parafazer a comparação de citério de seleção
278:             * @throws SQLException
279:             */
280:            public void setSelectedItem(Object anObject, String key)
281:                    throws SQLException {
282:                if (getDados() == null || anObject == null)
283:                    return;
284:
285:                if (selectedObject != null
286:                        && ((PersistentObject) selectedObject).getAsMap().get(
287:                                key).equals(anObject))
288:                    return;
289:                for (int i = 2; i < getSize(); i++) {
290:                    if (getDataAt(i) != null
291:                            && getDataAt(i).get(key) != null
292:                            && getDataAt(i).get(key).toString().equals(
293:                                    anObject.toString())) {
294:                        cacheManager.setSelectedIndex(i);
295:                        selectedObject = getPersistentObject(i);
296:                        fireContentsChanged(this , -1, -1);
297:                        return;
298:                    }
299:                }
300:
301:                cacheManager.setSelectedIndex(-1);
302:                selectedObject = null;
303:                fireContentsChanged(this , -1, -1);
304:                return;
305:            }
306:
307:            /**
308:             * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
309:             */
310:            public void setSelectedItem(Object anObject) {
311:                if (anObject instanceof  PersistentObject) {
312:                    try {
313:                        setSelectedItem((PersistentObject) anObject);
314:                    } catch (SQLException sqle) {
315:                        //TODO Excecao
316:                    }
317:                    return;
318:                }
319:
320:                cacheManager.setSelectedIndex(-1);
321:                selectedObject = null;
322:                fireContentsChanged(this , -1, -1);
323:            }
324:
325:            /**
326:             * Seta o indice selecionado neste model
327:             * @param index
328:             */
329:            public void setSelectedIndex(int index) {
330:                selectedObject = getElementAt(index);
331:                cacheManager.setSelectedIndex(index);
332:                cacheManager.add(index, selectedObject);
333:                fireContentsChanged(this , -1, -1);
334:            }
335:
336:            /**
337:             * @see br.com.gfpshare.beans.DBCollectionModel#setDataType(br.com.gfpshare.db.PersistentObject)
338:             */
339:            public void setDataType(PersistentObject dataType) {
340:                this .dataType = dataType;
341:                try {
342:                    loadDados();
343:                } catch (DAOCreationException cce) {
344:                    throw new RuntimeException(cce);
345:                } catch (SQLException sqle) {
346:                    throw new RuntimeException(sqle);
347:                }
348:
349:            }
350:
351:            /**
352:             * @see br.com.gfpshare.beans.DBCollectionModel#getDataType()
353:             */
354:            public PersistentObject getDataType() {
355:                return dataType;
356:            }
357:
358:            /**
359:             * @see br.com.gfpshare.beans.DBCollectionModel#filter()
360:             */
361:            public void filter() throws DAOCreationException, SQLException {
362:                cacheManager.resetCache();
363:                if (getDataType() != null) {
364:                    String classe = dataType.getClass().getName();
365:                    DAO dAO = DAOProvider
366:                            .getControllerProvider()
367:                            .getController(
368:                                    classe
369:                                            .substring(classe.lastIndexOf('.') + 1));
370:                    dados = dAO.filterBy(dados, dataType);
371:                    fireContentsChanged(this , -1, -1);
372:                } else
373:                    dados = null;
374:
375:            }
376:
377:            /**
378:             * @see java.lang.Object#finalize()
379:             */
380:            @Override
381:            protected void finalize() throws Throwable {
382:                super .finalize();
383:                if (dados != null) {
384:                    String classe = dataType.getClass().getName();
385:                    DAO dAO = DAOProvider
386:                            .getControllerProvider()
387:                            .getController(
388:                                    classe
389:                                            .substring(classe.lastIndexOf('.') + 1));
390:                    dAO.freeResource(dados);
391:                }
392:            }
393:
394:            private class CacheManager {
395:                private Map<Integer, Object> cacheBO = new HashMap<Integer, Object>();
396:
397:                private Map<Integer, Map<String, Object>> cacheMapToColumn = new HashMap<Integer, Map<String, Object>>();
398:
399:                private int sizeCache = -1;
400:
401:                private int selectedIndex = -1;
402:
403:                void resetCache() {
404:                    cacheBO.clear();
405:                    cacheMapToColumn.clear();
406:                    sizeCache = -1;
407:                }
408:
409:                void add(Integer i, Object bo) {
410:                    cacheBO.put(i, bo);
411:                }
412:
413:                Object get(Integer i) {
414:                    return cacheBO.get(i);
415:                }
416:
417:                void add(Integer i, Map<String, Object> bo) {
418:                    cacheMapToColumn.put(i, bo);
419:                }
420:
421:                Map<String, Object> getMap(Integer i) {
422:                    return cacheMapToColumn.get(i);
423:                }
424:
425:                void setSizeCached(int i) {
426:                    sizeCache = i;
427:                }
428:
429:                int getSizeCached() {
430:                    return sizeCache;
431:                }
432:
433:                boolean isCachedSizeClear() {
434:                    return sizeCache == -1;
435:                }
436:
437:                public int getSelectedIndex() {
438:                    return selectedIndex;
439:                }
440:
441:                public void setSelectedIndex(int selectedIndex) {
442:                    this .selectedIndex = selectedIndex;
443:                }
444:            }
445:
446:            public void adicionadoNovo(DAOEvent e) {
447:                try {
448:                    loadDados();
449:                } catch (DAOCreationException e1) {
450:                    // TODO Auto-generated catch block
451:                    e1.printStackTrace();
452:                } catch (SQLException e1) {
453:                    // TODO Auto-generated catch block
454:                    e1.printStackTrace();
455:                }
456:            }
457:
458:            public void atualizado(DAOEvent e) {
459:                try {
460:                    loadDados();
461:                } catch (DAOCreationException e1) {
462:                    // TODO Auto-generated catch block
463:                    e1.printStackTrace();
464:                } catch (SQLException e1) {
465:                    // TODO Auto-generated catch block
466:                    e1.printStackTrace();
467:                }
468:            }
469:
470:            public void deletado(DAOEvent e) {
471:                try {
472:                    loadDados();
473:                } catch (DAOCreationException e1) {
474:                    // TODO Auto-generated catch block
475:                    e1.printStackTrace();
476:                } catch (SQLException e1) {
477:                    // TODO Auto-generated catch block
478:                    e1.printStackTrace();
479:                }
480:            }
481:
482:            public void filtrado(DAOEvent e) {
483:                //------------------------------------------------
484:                // TODO Auto-generated method stub
485:
486:            }
487:
488:            public void selecionado(DAOEvent e) {
489:                //------------------------------------------------
490:                // TODO Auto-generated method stub
491:
492:            }
493:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.