1 package org.simantics.document.linking.report.html;
3 import java.io.PrintStream;
4 import java.util.ArrayList;
7 import org.simantics.document.linking.report.Table;
8 import org.simantics.document.linking.report.TableColumn;
9 import org.simantics.document.linking.report.TextItem;
10 import org.simantics.document.linking.report.TableColumn.Alignment;
11 import org.simantics.document.linking.report.TableRow;
14 public class HTMLTable extends HTMLTextElement implements Table {
17 List<TableColumn> columns = new ArrayList<TableColumn>();
19 boolean linesVisible = true;
20 boolean headerVisible = true;
21 TextItem title = null;
27 boolean copyStyle = false;
29 public HTMLTable(HTMLDocument writer, boolean id) {
30 super(id == false ? null : writer.getUniqueId(HTMLTable.class));
33 this.classID = writer.getUniqueId(this);
36 public HTMLTable(HTMLDocument writer, PrintStream os, boolean id) {
37 super(id == false ? null : writer.getUniqueId(HTMLTable.class));
40 this.classID = writer.getUniqueId(this);
43 public HTMLTable(HTMLTable table, boolean id) {
44 super(id == false ? null : table.writer.getUniqueId(HTMLTable.class));
45 this.writer = table.writer;
47 this.columns.addAll(table.columns);
48 this.classID = table.classID;
49 this.copyStyle = true;
53 public TableColumn addColumn(String name, double width) {
54 TableColumn tc = new TableColumn(name, width);
60 public List<TableColumn> getColumns() {
65 public void setTitle(String title) {
67 this.title = writer.newItem(TextItem.class);
68 this.title.setText(title);
69 } catch (Exception e) {
75 public void setTitle(TextItem title){
80 public TableRow writeRow(String... line) throws Exception {
81 List<String> list = new ArrayList<String>(line.length);
84 return writeRow(list);
88 public TableRow writeRow(List<String> line) throws Exception {
89 for (int i = 0; i < line.size(); i++) {
90 line.set(i, escape(line.get(i)));
92 return writeRowRaw(line);
96 public TableRow writeRowItem(List<TextItem> line) throws Exception {
97 for (int i = 0; i < line.size(); i++) {
98 TextItem text = line.get(i);
100 text.setText(escape(text.getText()));
103 return writeRowRaw2(line);
107 public TableRow writeRowItem(TextItem... line) throws Exception {
108 List<TextItem> list = new ArrayList<TextItem>(line.length);
109 for (TextItem s : line)
111 return writeRowItem(list);
115 public TableRow writeRowRaw(List<String> line) throws Exception {
116 if (currentLine == 0)
119 clz += currentLine % 2 == 0 ? "even" : "odd";
120 clz += " "+currentTextSize;
122 os.println(" <tr class=" + clz+ ">");
123 if (line.size() > 1) {
124 for (int i = 0; i < line.size(); i++) {
125 String s = line.get(i);
126 Alignment a = columns.get(i).getAlignment();
127 String tdClass = "class=\"" +a.toString()+"\"";
130 os.println(" <td "+tdClass+">" + s + "</td>");
132 os.println(" <td> </td>");
134 } else if (line.size() == 1){
135 String s = line.get(0);
136 Alignment a = columns.get(0).getAlignment();
137 String tdClass = "class=\"" +a.toString()+"\"";
139 os.println(" <td "+tdClass + " colspan=\"" + columns.size()+"\">" + s + "</td>");
141 os.println(" <td colspan=\"" + columns.size()+"\"> </td>");
144 os.println(" <td colspan=\"" + columns.size()+"\"> </td>");
146 os.println(" </tr>");
148 writer.currentLine++;
149 return new HTMLTableRow(null);
152 public TableRow writeRowRaw2(List<TextItem> line) throws Exception {
153 if (currentLine == 0)
156 clz += currentLine % 2 == 0 ? "even" : "odd";
157 clz += " "+currentTextSize;
159 os.println(" <tr class=" + clz+ ">");
160 if (line.size() > 1) {
161 for (int i = 0; i < line.size(); i++) {
162 TextItem item = line.get(i);
163 Alignment a = columns.get(i).getAlignment();
164 String tdClass = "class=\"" +a.toString()+"\"";
166 if (item != null && item.getText() != null)
167 os.println(" <td "+tdClass+">" + item.toString() + "</td>");
169 os.println(" <td> </td>");
171 } else if (line.size() == 1){
172 String s = line.get(0).toString();
173 Alignment a = columns.get(0).getAlignment();
174 String tdClass = "class=\"" +a.toString()+"\"";
176 os.println(" <td "+tdClass + " colspan=\"" + columns.size()+"\">" + s + "</td>");
178 os.println(" <td colspan=\"" + columns.size()+"\"> </td>");
181 os.println(" <td colspan=\"" + columns.size()+"\"> </td>");
183 os.println(" </tr>");
185 writer.currentLine++;
186 return new HTMLTableRow(null);
190 public boolean isLinesVisible() {
195 public void setLinesVisible(boolean b) {
196 this.linesVisible = b;
200 public boolean isHeaderVisible() {
201 return headerVisible;
205 public void setHeaderVisible(boolean b) {
206 this.headerVisible = b;
210 for (int i = 0; i < columns.size(); i++) {
211 os.println("table."+classID+" th.column"+i+" {");
212 os.println(" width: " + ((int)(columns.get(i).getWidth()*100.0)) + "%;");
220 os.println("<a id=\"" + id + "\"></a>");
222 String clz = classID + " " + (linesVisible ? "lines" : "nolines");
223 os.println("<table class=\"" + clz +"\" >");
227 os.println(" <caption>"+title+"</caption>");
228 os.println(" <thead>");
229 os.println(" <tr class=MEDIUM>");
231 for (TableColumn c : columns) {
232 os.println(" <th class=\"column" + ci +"\">" + escape(c.getName()) + "</th>");
235 os.println(" </tr>");
236 os.println(" </thead>");
238 os.println(" <tbody>");
241 void endTable() throws Exception{
242 if (currentLine > 0) {
243 os.println(" </tbody>");
244 os.println("</table>");
247 os.println("<style>");
249 os.println("</style>");
254 private String escape(String s) {
255 // TODO : escape string