PageRenderTime 167ms CodeModel.GetById 80ms app.highlight 55ms RepoModel.GetById 25ms app.codeStats 1ms

/src/mpv5/utils/files/TableHtmlWriter.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 480 lines | 280 code | 44 blank | 156 comment | 58 complexity | f92d65e979286182437d16827dd45aee MD5 | raw file
  1/*
  2 * 
  3 *  *  This file is part of YaBS.
  4 *  *  
  5 *  *      YaBS is free software: you can redistribute it and/or modify
  6 *  *      it under the terms of the GNU General Public License as published by
  7 *  *      the Free Software Foundation, either version 3 of the License, or
  8 *  *      (at your option) any later version.
  9 *  *  
 10 *  *      YaBS is distributed in the hope that it will be useful,
 11 *  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 *  *      GNU General Public License for more details.
 14 *  *  
 15 *  *      You should have received a copy of the GNU General Public License
 16 *  *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17 *  
 18 */
 19package mpv5.utils.files;
 20
 21import java.awt.Color;
 22import java.io.BufferedWriter;
 23import java.io.File;
 24import java.io.FileWriter;
 25import java.io.IOException;
 26import java.util.Date;
 27import java.util.logging.Level;
 28import java.util.logging.Logger;
 29import javax.swing.JTable;
 30import javax.swing.table.DefaultTableModel;
 31import mpv5.logging.Log;
 32import mpv5.utils.date.DateConverter;
 33
 34/**
 35 *
 36 * This class writes a HTML file with a 
 37 * table containing the data of the given data model 
 38 * (DefaultTableModel or String[][] representation)
 39 * 
 40 * 
 41 */
 42public class TableHtmlWriter {
 43
 44    private Object[][] model = null;
 45    private String[] header = null;
 46    private File file = FileDirectoryHandler.getTempFile();
 47    private String prefix = null;
 48    private Color borderColor = Color.black;
 49    private boolean showHorizontalLines = true;
 50    private boolean showVerticalLines = true;
 51
 52    /**
 53     * This constructor initializes the HTMLWriter with the
 54     * given datamodel and the given output file.
 55     * The table header will be set to header
 56     * and the header of the created file will be the prefix
 57     * @param model
 58     * @param file
 59     * @param showHorizontalLines
 60     * @param showVerticalLines
 61     */
 62    public TableHtmlWriter(JTable model, File file, boolean showHorizontalLines, boolean showVerticalLines) {
 63        Object[][] obj = new Object[model.getRowCount()][model.getColumnCount()-1];
 64
 65        for (int k = 0; k < obj.length; k++) {
 66            for (int l = 1; l < obj[k].length; l++) {
 67                if (model.getValueAt(k, l) instanceof Date) {
 68                    obj[k][l - 1] = DateConverter.getDefDateString((Date) model.getValueAt(k, l));
 69                } else {
 70                    obj[k][l - 1] = model.getValueAt(k, l);
 71                }
 72            }
 73        }
 74        String[] head = new String[model.getColumnCount()-1];
 75        for (int i = 1; i < head.length; i++) {
 76            head[i - 1] = model.getColumnName(i);
 77        }
 78        this.header = head;
 79        this.model = obj;
 80        this.file = file;
 81        this.showHorizontalLines = showHorizontalLines;
 82        this.showVerticalLines = showVerticalLines;
 83
 84    }
 85
 86    /**
 87     * This constructor initializes the HTMLWriter with the
 88     * given datamodel and the given output file.
 89     * The table header will be set to header 
 90     * and the header of the created file will be the prefix
 91     * 
 92     * @param model A Default Table Model 
 93     * @param file A file to write the output
 94     * @param header The table header {column1, coloumn2,...} 
 95     * @param prefix The page header
 96     */
 97    public TableHtmlWriter(DefaultTableModel model, File file, String[] header, String prefix) {
 98        Object[][] obj = new Object[model.getRowCount()][model.getColumnCount()];
 99        for (int k = 0; k < obj.length; k++) {
100            for (int l = 0; l < obj[k].length; l++) {
101                obj[k][l] = model.getValueAt(k, l);
102            }
103        }
104
105        this.model = obj;
106        this.file = file;
107        this.header = header;
108        this.prefix = prefix;
109    }
110
111    /**
112     * This constructor initializes the HTMLWriter with the
113     * given datamodel and the given output file.
114     * 
115     * @param model A Default Table Model 
116     * @param file A file to write the output
117     */
118    public TableHtmlWriter(DefaultTableModel model, File file) {
119        Object[][] obj = new Object[model.getRowCount()][model.getColumnCount()];
120        for (int k = 0; k < obj.length; k++) {
121            for (int l = 0; l < obj[k].length; l++) {
122                obj[k][l] = model.getValueAt(k, l);
123            }
124        }
125        this.model = obj;
126        this.file = file;
127        String[] head = new String[model.getColumnCount()];
128        for (int i = 0; i < head.length; i++) {
129            head[i] = model.getColumnName(i);
130        }
131        this.header = head;
132    }
133
134    /**
135     * This constructor initializes the HTMLWriter with the
136     * given datamodel and the given output file.
137     * 
138     * @param model A 2-dimensional array presenting the table-data. 
139     * @param file A file to write the output
140     */
141    public TableHtmlWriter(Object[][] model, File file) {
142        this.model = model;
143        this.file = file;
144    }
145
146    /**
147     * This constructor initializes the HTMLWriter with the
148     * given datamodel and the given output file.
149     * The table header will be set to header 
150     * and the header of the created file will be the prefix
151     * 
152     * @param model A Default Table Model 
153     * @param file A file to write the output
154     * @param header The table header {column1, coloumn2,...} 
155     * @param prefix The page header
156     */
157    public TableHtmlWriter(Object[][] model, File file, String[] header, String prefix) {
158        this.model = model;
159        this.file = file;
160        this.header = header;
161        this.prefix = prefix;
162
163    }
164
165    /**
166     * This constructor initializes the HTMLWriter with the
167     * given datamodele.
168     * 
169     * @param model A Default Table Model 
170     */
171    public TableHtmlWriter(DefaultTableModel model) {
172
173        Object[][] obj = new Object[model.getRowCount()][model.getColumnCount()];
174        for (int k = 0; k < obj.length; k++) {
175            for (int l = 0; l < obj[k].length; l++) {
176                obj[k][l] = model.getValueAt(k, l);
177            }
178        }
179        this.model = obj;
180        String[] head = new String[model.getColumnCount()];
181        for (int i = 0; i < head.length; i++) {
182            head[i] = model.getColumnName(i);
183        }
184        this.header = head;
185    }
186
187    /**
188     * This constructor initializes the HTMLWriter with the
189     * given datamodel and the given output file.
190     * 
191     * @param model A 2-dimensional array presenting the table-data
192     */
193    public TableHtmlWriter(Object[][] model) {
194        this.model = model;
195    }
196
197    /**
198     * This method creates the HTML file
199     * 
200     * @param border The border thickness of the created HTML table
201     * @param bordercolor The bordercolor of the created HTML table
202     * @return The HTML file
203     */
204    public File createHtml(Integer border, Color bordercolor) {
205
206        if (getModel() != null) {
207            try {
208                write(border, bordercolor);
209            } catch (IOException ex) {
210                mpv5.logging.Log.Debug(ex);//Logger.getLogger(TableHtmlWriter.class.getName()).log(Level.SEVERE, null, ex);
211            }
212        } else {
213            Log.Print("No datamodel given.");
214        }
215
216        return getFile();
217    }
218
219    /**
220     * This method creates the HTML file with standard values
221     * 
222     * @return The created HTML file
223     */
224    public File createHtml() {
225        if (getModel() != null) {
226            try {
227                write(0, Color.BLACK);
228            } catch (IOException ex) {
229                mpv5.logging.Log.Debug(ex);//Logger.getLogger(TableHtmlWriter.class.getName()).log(Level.SEVERE, null, ex);
230            }
231        } else {
232            Log.Print("No datamodel given.");
233        }
234        return getFile();
235    }
236
237    private void write(Integer border, Color borderc) throws IOException {
238
239        String bo = "";
240        if (!showHorizontalLines || !showVerticalLines) {
241            if (!showHorizontalLines && !showVerticalLines) {
242                border = 0;
243            } else if (showHorizontalLines) {
244                bo = "RULES=COLS";
245            } else if (showHorizontalLines) {
246                bo = " RULES=ROWS";
247            }
248        }
249
250
251        BufferedWriter out = new BufferedWriter(new FileWriter(getFile()));
252        String rgb = Integer.toHexString(borderc.getRGB());
253        rgb = rgb.substring(2, rgb.length());
254        out.write(" <meta http-equiv='Content-Type' content='text/html;'> <HTML>");
255        out.write("<head><STYLE TYPE='text/css'><!--TD{font-family: Arial; font-size: 10pt;}"
256                + "table {border: " + border
257                + "px solid #" + rgb + ";border-collapse: collapse;}"
258                + "td { border: " + border + "px solid #" + rgb + "; }"
259                + "th { border: " + border + "px solid #" + rgb + "; }"
260                + "-->"
261                + "</STYLE></head>");
262        if (getPrefix() != null) {
263            out.write("<P><H2>" + getPrefix() + "</H2></P><BR>");
264        }
265        out.write("<TABLE style='empty-cells:show' WIDTH = 100%   CELLPADDING=1px CELLSPACING=0px BORDERCOLOR = #" + rgb + " " + bo + ">\n");
266        if (getHeader() != null) {
267            out.write("<THEAD>\n<TR VALIGN=TOP>\n");
268            for (int k = 0; k < getHeader().length; k++) {
269                out.write("<TH WIDTH=*>	<P>" + getHeader()[k] + "</P></TH>\n");
270            }
271            out.write("</TR></THEAD>\n");
272        }
273        out.write("	<TBODY>\n");
274        for (int k = 0; k < getModel().length; k++) {
275            out.write("<TR VALIGN=TOP>\n");
276            for (int l = 0; l < getModel()[k].length; l++) {
277                out.write("<TD WIDTH=*><P>");
278                if (getModel()[k][l] != null && !String.valueOf(getModel()[k][l]).equals("null")) {
279                    out.write(String.valueOf(getModel()[k][l]).replaceAll("<html>|<pre>|&nbsp;", "") + "&nbsp;");
280                }
281                out.write("</P></TD>\n");
282            }
283            out.write("</TR>\n");
284        }
285        out.write("</TBODY></TABLE>\n");
286        out.write("<BR><BR>");
287        out.write(DateConverter.getDefDateString(new Date()));
288        out.write("</HTML>");
289        out.close();
290    }
291
292    /**
293     * 
294     * @return The datamodel
295     */
296    public Object[][] getModel() {
297        return model;
298    }
299
300    /**
301     * 
302     * @param model The data model
303     */
304    public void setModel(Object[][] model) {
305        this.model = model;
306    }
307
308    /**
309     * 
310     * @return The tableheader
311     */
312    public String[] getHeader() {
313        return header;
314    }
315
316    /**
317     * 
318     * @param header The table header
319     */
320    public void setHeader(String[] header) {
321        this.header = header;
322    }
323
324    /**
325     * 
326     * @return The output file
327     */
328    public File getFile() {
329        return file;
330    }
331
332    /**
333     * The output file
334     * 
335     * @param file
336     */
337    public void setFile(File file) {
338        this.file = file;
339    }
340
341    /**
342     * 
343     * @return The page header
344     */
345    public String getPrefix() {
346        return prefix;
347    }
348
349    /**
350     * 
351     * @param prefix The page header
352     */
353    public void setPrefix(String prefix) {
354        this.prefix = prefix;
355    }
356
357    /**
358     * @return the showHorizontalLines
359     */
360    public boolean isShowHorizontalLines() {
361        return showHorizontalLines;
362    }
363
364    /**
365     * @param showHorizontalLines the showHorizontalLines to set
366     */
367    public void setShowHorizontalLines(boolean showHorizontalLines) {
368        this.showHorizontalLines = showHorizontalLines;
369    }
370
371    /**
372     * @return the showVerticalLines
373     */
374    public boolean isShowVerticalLines() {
375        return showVerticalLines;
376    }
377
378    /**
379     * @param showVerticalLines the showVerticalLines to set
380     */
381    public void setShowVerticalLines(boolean showVerticalLines) {
382        this.showVerticalLines = showVerticalLines;
383    }
384
385    /**
386     * @return the borderColor
387     */
388    public Color getBorderColor() {
389        return borderColor;
390    }
391
392    /**
393     * @param borderColor the borderColor to set
394     */
395    public void setBorderColor(Color borderColor) {
396        this.borderColor = borderColor;
397    }
398
399///////////////////////////////////// xhtml-writer start
400    /**
401     * This method creates the HTML file
402     *
403     * @param border The border thickness of the created HTML table
404     * @param bordercolor The bordercolor of the created HTML table
405     * @return The HTML file
406     */
407    public File createHtml2(Integer border, Color bordercolor) {
408
409        if (getModel() != null) {
410            try {
411                write2(border, bordercolor);
412            } catch (IOException ex) {
413                mpv5.logging.Log.Debug(ex);//Logger.getLogger(TableHtmlWriter.class.getName()).log(Level.SEVERE, null, ex);
414            }
415        } else {
416            Log.Print("No datamodel given.");
417        }
418
419        return getFile();
420    }
421
422    private void write2(Integer border, Color borderc) throws IOException {
423
424        String bo = "";
425        if (!showHorizontalLines || !showVerticalLines) {
426            if (!showHorizontalLines && !showVerticalLines) {
427                border = 0;
428            } else if (showHorizontalLines) {
429                bo = "RULES=COLS";
430            } else if (showHorizontalLines) {
431                bo = " RULES=ROWS";
432            }
433        }
434
435
436        BufferedWriter out = new BufferedWriter(new FileWriter(getFile()));
437        String rgb = Integer.toHexString(borderc.getRGB());
438        rgb = rgb.substring(2, rgb.length());
439        out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
440        out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
441        out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta http-equiv='Content-Type' content='text/html;charset=UTF-8' />\n<title />\n");
442        out.write("<style type='text/css'>\ntd{font-family: Arial; font-size:10pt;}\n"
443                + "table {border:" + border + "px solid #" + rgb + "; border-collapse:collapse;}\n"
444                + "td {border:" + border + "px solid #" + rgb + "; padding:7pt 5pt;}\n"
445                + "th {border:" + border + "px solid #" + rgb + "; padding:10pt;}\n"
446                + "h2 {margin:10pt 0 20pt;}"
447                + "</style>\n</head><body>\n");
448        if (getPrefix() != null) {
449            out.write("<h2>" + getPrefix() + "</h2>\n");
450        }
451        out.write("<table style='empty-cells:show;border-color:#" + rgb + " " + bo
452                + ";' \nwidth = '100%' cellpadding = '1' cellspacing = '0'>\n");
453        if (getHeader() != null) {
454            out.write("<thead>\n<tr valign='top'>\n");
455            for (int k = 0; k < getHeader().length; k++) {
456                out.write("<th>" + getHeader()[k] + "</th>\n");
457            }
458            out.write("</tr></thead>\n");
459        }
460        out.write("<tbody>\n");
461        for (int k = 0; k < getModel().length; k++) {
462            out.write("<tr valign = 'top'>\n");
463            for (int l = 0; l < getModel()[k].length; l++) {
464                out.write("<td>");
465                if (getModel()[k][l] != null && !String.valueOf(getModel()[k][l]).equals("null")) {
466                    out.write(String.valueOf(getModel()[k][l]).replaceAll("<html>|<pre>|&nbsp;", "") + "&nbsp;");
467                }
468                out.write("</td>\n");
469            }
470            out.write("</tr>\n");
471        }
472        out.write("</tbody></table>\n");
473        out.write("<p style='margin-top:20pt;font-size:small;'>");
474        out.write(DateConverter.getDefDateString(new Date()));
475        out.write("</p>\n</body></html>");
476        out.close();
477        Log.Debug(this, getFile());
478    }
479//////////////////////////////// xhtml-writer end
480}