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)
118 String clz = "\""; //$NON-NLS-1$
119 clz += currentLine % 2 == 0 ? "even" : "odd"; //$NON-NLS-1$ //$NON-NLS-2$
120 clz += " "+currentTextSize; //$NON-NLS-1$
121 clz += "\""; //$NON-NLS-1$
122 os.println(" <tr class=" + clz+ ">"); //$NON-NLS-1$ //$NON-NLS-2$
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()+"\""; //$NON-NLS-1$ //$NON-NLS-2$
130 os.println(" <td "+tdClass+">" + s + "</td>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
132 os.println(" <td> </td>"); //$NON-NLS-1$
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()+"\""; //$NON-NLS-1$ //$NON-NLS-2$
139 os.println(" <td "+tdClass + " colspan=\"" + columns.size()+"\">" + s + "</td>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
141 os.println(" <td colspan=\"" + columns.size()+"\"> </td>"); //$NON-NLS-1$ //$NON-NLS-2$
144 os.println(" <td colspan=\"" + columns.size()+"\"> </td>"); //$NON-NLS-1$ //$NON-NLS-2$
146 os.println(" </tr>"); //$NON-NLS-1$
148 writer.currentLine++;
149 return new HTMLTableRow(null);
152 public TableRow writeRowRaw2(List<TextItem> line) throws Exception {
153 if (currentLine == 0)
155 String clz = "\""; //$NON-NLS-1$
156 clz += currentLine % 2 == 0 ? "even" : "odd"; //$NON-NLS-1$ //$NON-NLS-2$
157 clz += " "+currentTextSize; //$NON-NLS-1$
158 clz += "\""; //$NON-NLS-1$
159 os.println(" <tr class=" + clz+ ">"); //$NON-NLS-1$ //$NON-NLS-2$
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()+"\""; //$NON-NLS-1$ //$NON-NLS-2$
166 if (item != null && item.getText() != null)
167 os.println(" <td "+tdClass+">" + item.toString() + "</td>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
169 os.println(" <td> </td>"); //$NON-NLS-1$
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()+"\""; //$NON-NLS-1$ //$NON-NLS-2$
176 os.println(" <td "+tdClass + " colspan=\"" + columns.size()+"\">" + s + "</td>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
178 os.println(" <td colspan=\"" + columns.size()+"\"> </td>"); //$NON-NLS-1$ //$NON-NLS-2$
181 os.println(" <td colspan=\"" + columns.size()+"\"> </td>"); //$NON-NLS-1$ //$NON-NLS-2$
183 os.println(" </tr>"); //$NON-NLS-1$
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+" {"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
212 os.println(" width: " + ((int)(columns.get(i).getWidth()*100.0)) + "%;"); //$NON-NLS-1$ //$NON-NLS-2$
213 os.println("}"); //$NON-NLS-1$
220 os.println("<a id=\"" + id + "\"></a>"); //$NON-NLS-1$ //$NON-NLS-2$
222 String clz = classID + " " + (linesVisible ? "lines" : "nolines"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
223 os.println("<table class=\"" + clz +"\" >"); //$NON-NLS-1$ //$NON-NLS-2$
227 os.println(" <caption>"+title+"</caption>"); //$NON-NLS-1$ //$NON-NLS-2$
228 os.println(" <thead>"); //$NON-NLS-1$
229 os.println(" <tr class=MEDIUM>"); //$NON-NLS-1$
231 for (TableColumn c : columns) {
232 os.println(" <th class=\"column" + ci +"\">" + escape(c.getName()) + "</th>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
235 os.println(" </tr>"); //$NON-NLS-1$
236 os.println(" </thead>"); //$NON-NLS-1$
238 os.println(" <tbody>"); //$NON-NLS-1$
241 void endTable() throws Exception{
242 if (currentLine > 0) {
243 os.println(" </tbody>"); //$NON-NLS-1$
244 os.println("</table>"); //$NON-NLS-1$
245 os.println("<br>"); //$NON-NLS-1$
247 os.println("<style>"); //$NON-NLS-1$
249 os.println("</style>"); //$NON-NLS-1$
254 private String escape(String s) {
255 // TODO : escape string