]> gerrit.simantics Code Review - simantics/platform.git/blob
2137dbb54a1f622e907a7961e603409dcc5bf198
[simantics/platform.git] /
1 package org.simantics.document.linking.report.html;
2
3 import java.io.PrintStream;
4 import java.util.ArrayList;
5 import java.util.List;
6
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;
12
13
14 public class HTMLTable extends HTMLTextElement implements Table {
15         HTMLDocument writer;
16         
17         List<TableColumn> columns = new ArrayList<TableColumn>();
18         
19         boolean linesVisible = true;
20         boolean headerVisible = true;
21         TextItem title = null;
22         int currentLine = 0;
23         PrintStream os;
24         
25         String classID;
26         
27         boolean copyStyle = false;
28         
29         public HTMLTable(HTMLDocument writer, boolean id) {
30                 super(id == false ? null : writer.getUniqueId(HTMLTable.class));
31                 this.writer = writer;
32                 this.os = writer.os;
33                 this.classID = writer.getUniqueId(this);
34         }
35         
36         public HTMLTable(HTMLDocument writer, PrintStream os, boolean id) {
37                 super(id == false ? null : writer.getUniqueId(HTMLTable.class));
38                 this.writer = writer;
39                 this.os = os;
40                 this.classID = writer.getUniqueId(this);
41         }
42         
43         public HTMLTable(HTMLTable table, boolean id) {
44                 super(id == false ? null : table.writer.getUniqueId(HTMLTable.class));
45                 this.writer = table.writer;
46                 this.os = table.os;
47                 this.columns.addAll(table.columns);
48                 this.classID = table.classID;
49                 this.copyStyle = true;
50         }
51         
52         @Override
53         public TableColumn addColumn(String name, double width) {
54                 TableColumn tc = new TableColumn(name, width);
55                 columns.add(tc);
56                 return tc;
57         }
58         
59         @Override
60         public List<TableColumn> getColumns() {
61                 return columns;
62         }
63         
64         @Override
65         public void setTitle(String title) {
66                 try {
67                         this.title = writer.newItem(TextItem.class);
68                         this.title.setText(title);
69                 } catch (Exception e) {
70                         
71                 }
72         }
73         
74         @Override
75         public void setTitle(TextItem title){
76                 this.title = title;
77         }
78         
79         @Override
80         public TableRow writeRow(String... line) throws Exception {
81                 List<String> list = new ArrayList<String>(line.length);
82                 for (String s : line)
83                         list.add(s);
84                 return writeRow(list);
85         }
86         
87         @Override
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)));
91                 }
92                 return writeRowRaw(line);
93         }
94         
95         @Override
96         public TableRow writeRowItem(List<TextItem> line) throws Exception {
97                 for (int i = 0; i < line.size(); i++) {
98                         TextItem text = line.get(i);
99                         if (text != null) {
100                                 text.setText(escape(text.getText()));
101                         }
102                 }
103                 return writeRowRaw2(line);
104         }
105         
106         @Override
107         public TableRow writeRowItem(TextItem... line) throws Exception {
108                 List<TextItem> list = new ArrayList<TextItem>(line.length);
109                 for (TextItem s : line)
110                         list.add(s);
111                 return writeRowItem(list);
112         }
113         
114         
115         public TableRow writeRowRaw(List<String> line) throws Exception {
116                 if (currentLine == 0)
117                         startTable();
118                 String clz = "\"";
119                 clz += currentLine % 2 == 0 ? "even" : "odd";
120                 clz += " "+currentTextSize;
121                 clz += "\"";
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()+"\"";
128                                 
129                                 if (s != null)
130                                         os.println("      <td "+tdClass+">" + s + "</td>");
131                                 else
132                                         os.println("      <td> </td>");
133                         }
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()+"\"";
138                         if (s != null)
139                                 os.println("      <td "+tdClass + " colspan=\"" + columns.size()+"\">" + s + "</td>");
140                         else
141                                 os.println("      <td colspan=\"" + columns.size()+"\"> </td>");
142                         
143                 } else {
144                         os.println("      <td colspan=\"" + columns.size()+"\"> </td>");
145                 }
146                 os.println("    </tr>");
147                 currentLine++;
148                 writer.currentLine++;
149                 return new HTMLTableRow(null);
150         }
151         
152         public TableRow writeRowRaw2(List<TextItem> line) throws Exception {
153                 if (currentLine == 0)
154                         startTable();
155                 String clz = "\"";
156                 clz += currentLine % 2 == 0 ? "even" : "odd";
157                 clz += " "+currentTextSize;
158                 clz += "\"";
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()+"\"";
165                                 
166                                 if (item != null && item.getText() != null)
167                                         os.println("      <td "+tdClass+">" + item.toString() + "</td>");
168                                 else
169                                         os.println("      <td> </td>");
170                         }
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()+"\"";
175                         if (s != null)
176                                 os.println("      <td "+tdClass + " colspan=\"" + columns.size()+"\">" + s + "</td>");
177                         else
178                                 os.println("      <td colspan=\"" + columns.size()+"\"> </td>");
179                         
180                 } else {
181                         os.println("      <td colspan=\"" + columns.size()+"\"> </td>");
182                 }
183                 os.println("    </tr>");
184                 currentLine++;
185                 writer.currentLine++;
186                 return new HTMLTableRow(null);
187         }
188
189         @Override
190         public boolean isLinesVisible() {
191                 return linesVisible;
192         }
193         
194         @Override
195         public void setLinesVisible(boolean b) {
196                 this.linesVisible = b;
197         }
198         
199         @Override
200         public boolean isHeaderVisible() {
201                 return headerVisible;
202         }
203         
204         @Override
205         public void setHeaderVisible(boolean b) {
206                 this.headerVisible = b;
207         }
208         
209         void style() {
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)) + "%;");
213                         os.println("}");
214                 }
215         }
216         
217         void startTable() {
218                 
219                 if (id != null) {
220                         os.println("<a id=\"" + id + "\"></a>");
221                 }       
222                 String clz = classID + " " + (linesVisible ? "lines" : "nolines");
223                 os.println("<table class=\"" + clz +"\" >");
224                 
225                 if (headerVisible) {
226                         if (title != null)
227                                 os.println("  <caption>"+title+"</caption>");
228                         os.println("  <thead>");
229                         os.println("    <tr class=MEDIUM>");
230                         int ci = 0;
231                         for (TableColumn c : columns) {
232                            os.println("      <th class=\"column" + ci +"\">" + escape(c.getName()) + "</th>");
233                            ci++;
234                         }
235                         os.println("    </tr>");
236                         os.println("  </thead>");
237                 }
238                 os.println("  <tbody>");
239         }
240         
241         void endTable() throws Exception{
242                 if (currentLine > 0) {
243                         os.println("  </tbody>");
244                         os.println("</table>");
245                         os.println("<br>");
246                         if (!copyStyle) {
247                                 os.println("<style>");
248                                 style();
249                                 os.println("</style>");
250                         }
251                 }
252         }
253         
254         private String escape(String s) {
255                 // TODO : escape string
256                 return s;
257         }
258         
259         
260 }