package org.simantics.document.linking.report.html;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import org.simantics.document.linking.report.Table;
import org.simantics.document.linking.report.TableColumn;
import org.simantics.document.linking.report.TextItem;
import org.simantics.document.linking.report.TableColumn.Alignment;
import org.simantics.document.linking.report.TableRow;
public class HTMLTable extends HTMLTextElement implements Table {
HTMLDocument writer;
List columns = new ArrayList();
boolean linesVisible = true;
boolean headerVisible = true;
TextItem title = null;
int currentLine = 0;
PrintStream os;
String classID;
boolean copyStyle = false;
public HTMLTable(HTMLDocument writer, boolean id) {
super(id == false ? null : writer.getUniqueId(HTMLTable.class));
this.writer = writer;
this.os = writer.os;
this.classID = writer.getUniqueId(this);
}
public HTMLTable(HTMLDocument writer, PrintStream os, boolean id) {
super(id == false ? null : writer.getUniqueId(HTMLTable.class));
this.writer = writer;
this.os = os;
this.classID = writer.getUniqueId(this);
}
public HTMLTable(HTMLTable table, boolean id) {
super(id == false ? null : table.writer.getUniqueId(HTMLTable.class));
this.writer = table.writer;
this.os = table.os;
this.columns.addAll(table.columns);
this.classID = table.classID;
this.copyStyle = true;
}
@Override
public TableColumn addColumn(String name, double width) {
TableColumn tc = new TableColumn(name, width);
columns.add(tc);
return tc;
}
@Override
public List getColumns() {
return columns;
}
@Override
public void setTitle(String title) {
try {
this.title = writer.newItem(TextItem.class);
this.title.setText(title);
} catch (Exception e) {
}
}
@Override
public void setTitle(TextItem title){
this.title = title;
}
@Override
public TableRow writeRow(String... line) throws Exception {
List list = new ArrayList(line.length);
for (String s : line)
list.add(s);
return writeRow(list);
}
@Override
public TableRow writeRow(List line) throws Exception {
for (int i = 0; i < line.size(); i++) {
line.set(i, escape(line.get(i)));
}
return writeRowRaw(line);
}
@Override
public TableRow writeRowItem(List line) throws Exception {
for (int i = 0; i < line.size(); i++) {
TextItem text = line.get(i);
if (text != null) {
text.setText(escape(text.getText()));
}
}
return writeRowRaw2(line);
}
@Override
public TableRow writeRowItem(TextItem... line) throws Exception {
List list = new ArrayList(line.length);
for (TextItem s : line)
list.add(s);
return writeRowItem(list);
}
public TableRow writeRowRaw(List line) throws Exception {
if (currentLine == 0)
startTable();
String clz = "\"";
clz += currentLine % 2 == 0 ? "even" : "odd";
clz += " "+currentTextSize;
clz += "\"";
os.println(" ");
if (line.size() > 1) {
for (int i = 0; i < line.size(); i++) {
String s = line.get(i);
Alignment a = columns.get(i).getAlignment();
String tdClass = "class=\"" +a.toString()+"\"";
if (s != null)
os.println(" " + s + " | ");
else
os.println(" | ");
}
} else if (line.size() == 1){
String s = line.get(0);
Alignment a = columns.get(0).getAlignment();
String tdClass = "class=\"" +a.toString()+"\"";
if (s != null)
os.println(" " + s + " | ");
else
os.println(" | ");
} else {
os.println(" | ");
}
os.println("
");
currentLine++;
writer.currentLine++;
return new HTMLTableRow(null);
}
public TableRow writeRowRaw2(List line) throws Exception {
if (currentLine == 0)
startTable();
String clz = "\"";
clz += currentLine % 2 == 0 ? "even" : "odd";
clz += " "+currentTextSize;
clz += "\"";
os.println(" ");
if (line.size() > 1) {
for (int i = 0; i < line.size(); i++) {
TextItem item = line.get(i);
Alignment a = columns.get(i).getAlignment();
String tdClass = "class=\"" +a.toString()+"\"";
if (item != null && item.getText() != null)
os.println(" " + item.toString() + " | ");
else
os.println(" | ");
}
} else if (line.size() == 1){
String s = line.get(0).toString();
Alignment a = columns.get(0).getAlignment();
String tdClass = "class=\"" +a.toString()+"\"";
if (s != null)
os.println(" " + s + " | ");
else
os.println(" | ");
} else {
os.println(" | ");
}
os.println("
");
currentLine++;
writer.currentLine++;
return new HTMLTableRow(null);
}
@Override
public boolean isLinesVisible() {
return linesVisible;
}
@Override
public void setLinesVisible(boolean b) {
this.linesVisible = b;
}
@Override
public boolean isHeaderVisible() {
return headerVisible;
}
@Override
public void setHeaderVisible(boolean b) {
this.headerVisible = b;
}
void style() {
for (int i = 0; i < columns.size(); i++) {
os.println("table."+classID+" th.column"+i+" {");
os.println(" width: " + ((int)(columns.get(i).getWidth()*100.0)) + "%;");
os.println("}");
}
}
void startTable() {
if (id != null) {
os.println("");
}
String clz = classID + " " + (linesVisible ? "lines" : "nolines");
os.println("");
if (headerVisible) {
if (title != null)
os.println(" "+title+"");
os.println(" ");
os.println(" ");
int ci = 0;
for (TableColumn c : columns) {
os.println(" " + escape(c.getName()) + " | ");
ci++;
}
os.println("
");
os.println(" ");
}
os.println(" ");
}
void endTable() throws Exception{
if (currentLine > 0) {
os.println(" ");
os.println("
");
os.println("
");
if (!copyStyle) {
os.println("");
}
}
}
private String escape(String s) {
// TODO : escape string
return s;
}
}