PageRenderTime 63ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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