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


001:        /*
002:         * Created on 07/03/2002
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:        package br.com.gfpshare.util.html;
024:
025:        import java.io.PrintWriter;
026:        import java.sql.ResultSet;
027:        import java.sql.SQLException;
028:        import java.text.DateFormat;
029:        import java.text.NumberFormat;
030:        import java.text.ParsePosition;
031:        import java.text.SimpleDateFormat;
032:        import java.util.Date;
033:
034:        /**
035:         * Esta classe cria uma Tebala com taga HTML e escreve o resultado em um PrintWriter
036:         * 
037:         * @author Igor Regis Da Silva Simoes
038:         * @created 05/08/2001
039:         */
040:        public class HTMLTable {
041:            /**
042:             * Constante representando o formato de data
043:             */
044:            public static String DATE = "D";
045:
046:            /**
047:             * Constante representando o formato de moeda
048:             */
049:            public static String CURRENCY = "C";
050:
051:            /**
052:             * Constante representando o formato de texto
053:             */
054:            public static String STRING = "S";
055:
056:            private PrintWriter out = null;
057:
058:            private String[] headers = null;
059:
060:            private String[][][] fields = null;
061:
062:            private boolean opened = false;
063:
064:            protected ResultSet dados = null;
065:
066:            /**
067:             * Cria uma nova isntamncia de HTML
068:             * 
069:             * @param out O PrintWriter no qual será escrita a saída.
070:             */
071:            public HTMLTable(PrintWriter out) {
072:                this .out = out;
073:            }
074:
075:            /**
076:             * Cria uma nova isntamncia de HTML
077:             * 
078:             * @param out O PrintWriter no qual será escrita a saída.
079:             * @param borda Indica se o tabela gerada deverá apresentar borda
080:             * @param width Indica a largura da tebela gerada
081:             */
082:            public HTMLTable(PrintWriter out, boolean borda, int width) {
083:                this .out = out;
084:                open(borda, width);
085:            }
086:
087:            /**
088:             * Cria uma nova isntamncia de HTML
089:             * 
090:             * @param out O PrintWriter no qual será escrita a saída.
091:             * @param borda Indica se o tabela gerada deverá apresentar borda
092:             * @param width Indica a largura da tebela gerada
093:             * @param headers Indica os cabeçalhos da tabela gerada
094:             * @param fields Indica os dados que serão apresentados na tabela
095:             */
096:            public HTMLTable(PrintWriter out, boolean borda, int width,
097:                    String[] headers, String[][][] fields) {
098:                this .out = out;
099:                this .headers = headers;
100:                this .fields = fields;
101:                open(borda, width);
102:            }
103:
104:            /**
105:             * Cria uma nova isntamncia de HTML
106:             * 
107:             * @param out O PrintWriter no qual será escrita a saída.
108:             * @param borda Indica se o tabela gerada deverá apresentar borda
109:             * @param width Indica a largura da tebela gerada
110:             * @param dados
111:             */
112:            public HTMLTable(PrintWriter out, boolean borda, int width,
113:                    ResultSet dados) {
114:                this .out = out;
115:                this .dados = dados;
116:                open(borda, width);
117:            }
118:
119:            /**
120:             * Cria uma nova isntamncia de HTML
121:             * 
122:             * @param out O PrintWriter no qual será escrita a saída.
123:             * @param headers Indica os cabeçalhos da tabela gerada
124:             * @param fields Indica os dados que serão apresentados na tabela
125:             */
126:            public HTMLTable(PrintWriter out, String[] headers,
127:                    String[][][] fields) {
128:                this .out = out;
129:                this .headers = headers;
130:                this .fields = fields;
131:            }
132:
133:            /**
134:             * Determina os headers para a tabela do relatório
135:             * 
136:             * @param headers Indica os cabeçalhos da tabela gerada
137:             */
138:            public void setHeaders(String[] headers) {
139:                this .headers = headers;
140:            }
141:
142:            /**
143:             * Retorna os headers para a tabela do relatório
144:             * 
145:             * @return Array de string com os headers para a tabela do relatório
146:             */
147:            public String[] getHeaders() {
148:                return headers;
149:            }
150:
151:            /**
152:             * Determina os campos para a tabela do relatório
153:             * 
154:             * @param fields Indica os dados que serão apresentados na tabela
155:             */
156:            public void setFields(String[][][] fields) {
157:                this .fields = fields;
158:            }
159:
160:            /**
161:             * Retorna os campos para a tabela do relatório
162:             * 
163:             * @return Array de strings com os dados que serão exibidos pela tabela
164:             */
165:            public String[][][] getFields() {
166:                return fields;
167:            }
168:
169:            /**
170:             * Indica se a tabela está aberta para a inserçao de novas linhas
171:             * 
172:             * @return boolean indicando o descrito acima
173:             */
174:            public boolean isOpen() {
175:                return opened;
176:            }
177:
178:            /**
179:             * Abre a tabela para inserção de linhas
180:             * 
181:             * @param borda Indica se a tabela deverá ou não apresentar bordas
182:             * @param width Indica a largura a tabela gerada
183:             */
184:            public void open(boolean borda, int width) {
185:                if (borda)
186:                    out.println("<table border=\"1\" width=\"" + width
187:                            + "\" height=\"20\">");
188:                else
189:                    out.println("<table border=\"0\" width=\"" + width
190:                            + "\" height=\"20\">");
191:                opened = true;
192:            }
193:
194:            /**
195:             * Fecha a tabela, impedindo a inserção de novas linhas
196:             */
197:            public void close() {
198:                out.println("</table>");
199:                opened = false;
200:            }
201:
202:            /**
203:             * Abre uma nova linha na tabela
204:             */
205:            public void openLine() {
206:                out.println("<tr>");
207:            }
208:
209:            /**
210:             * Fecha uma linha na tabela
211:             */
212:            public void closeLine() {
213:                out.println("</tr>");
214:            }
215:
216:            /**
217:             * Escreve um novo campo, com formatação de cabeçalho, na tabela
218:             * 
219:             * @param texto Texto que será inserido no campo
220:             */
221:            public void writeHeader(String texto) {
222:                out
223:                        .println("<td valign=\"bottom\" height=\"16\"><font size=\"1\" face=\"Arial\" COLOR=\"#ff0000\"><b>"
224:                                + texto + "</b></font></td>");
225:            }
226:
227:            /**
228:             * Escreve um novo, campo com formatação de dado, na tabela
229:             * 
230:             * @param texto Texto que será contido neste campo
231:             * @param negrito Indica se o texto deverá ou não estar em negrito
232:             */
233:            public void writeField(String texto, boolean negrito) {
234:                if (negrito)
235:                    out
236:                            .println("<td valign=\"bottom\" height=\"16\"><font size=\"1\" face=\"Arial\"><b>"
237:                                    + texto + "</b></font></td>");
238:                else
239:                    out
240:                            .println("<td valign=\"bottom\" height=\"16\"><font size=\"1\" face=\"Arial\">"
241:                                    + texto + "</font></td>");
242:            }
243:
244:            /**
245:             * Constrói o relatório
246:             * 
247:             * @param borda Indica se a tebela irá ou não possuir borda
248:             * @param width Indica a largura da tabela
249:             * @return Retorna true se o relatório for gerado com sucesso e false se ocorrer um erro na geração
250:             */
251:            public boolean build(boolean borda, int width) {
252:                if (dados == null)
253:                    if (fields == null | headers == null)
254:                        return false;
255:
256:                if (!opened)
257:                    open(borda, width);
258:
259:                openLine();
260:                if (dados == null)
261:                    buildFromArray();
262:                else
263:                    buildFromResultSet();
264:                close();
265:                return true;
266:            }
267:
268:            protected void buildFromArray() {
269:                for (int i = 0; i < headers.length; i++)
270:                    writeHeader(headers[i]);
271:                closeLine();
272:
273:                for (int i = 0; i < fields.length; i++) {
274:                    if (fields[i][0][0] != null) {
275:                        openLine();
276:                        for (int n = 0; n < fields[i].length; n++) {
277:                            if (fields[i][n][1].equals(DATE)) {
278:                                SimpleDateFormat sdf = new SimpleDateFormat(
279:                                        "ddMMyyyy");
280:                                sdf.setLenient(false);
281:                                Date d = null;
282:                                try {
283:                                    d = sdf.parse(fields[i][n][0],
284:                                            new ParsePosition(0));
285:                                } catch (Exception e) {
286:                                    //Não deveria ocorrer exceção aqui
287:                                }
288:
289:                                writeField(DateFormat.getDateInstance(
290:                                        DateFormat.MEDIUM).format(d), false);
291:                            } else if (fields[i][n][1].equals(CURRENCY)) {
292:                                String valor = NumberFormat
293:                                        .getCurrencyInstance()
294:                                        .format(
295:                                                Double
296:                                                        .valueOf(fields[i][n][0]
297:                                                                .substring(
298:                                                                        0,
299:                                                                        fields[i][n][0]
300:                                                                                .length() - 2)
301:                                                                + "."
302:                                                                + fields[i][n][0]
303:                                                                        .substring(
304:                                                                                fields[i][n][0]
305:                                                                                        .length() - 2,
306:                                                                                fields[i][n][0]
307:                                                                                        .length())));
308:                                if (valor.indexOf(",") == -1)
309:                                    valor = valor + ",00";
310:                                else if (valor.indexOf(",") == valor.length() - 2)
311:                                    valor = valor + "0";
312:                                writeField(valor, false);
313:                            } else
314:                                writeField(fields[i][n][0], false);
315:                        }
316:                        closeLine();
317:                    }
318:                }
319:            }
320:
321:            protected void buildFromResultSet() {
322:                try {
323:                    int columnCount = dados.getMetaData().getColumnCount();
324:                    for (int i = 0; i < columnCount; i++)
325:                        writeHeader(dados.getMetaData().getColumnName(i + 1));
326:                    closeLine();
327:
328:                    dados.first();
329:                    boolean loop = true;
330:                    while (loop) {
331:                        openLine();
332:                        for (int i = 0; i < columnCount; i++) {
333:                            writeField(dados.getObject(i + 1).toString(), false);
334:                        }
335:                        closeLine();
336:                        loop = dados.next();
337:                    }
338:
339:                } catch (SQLException e) {
340:                    // TODO Auto-generated catch block
341:                    e.printStackTrace();
342:                }
343:            }
344:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.