Child: [r11] (diff)

Download this file

Table.java    133 lines (113 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-17T00:00:00Z
* LicenseName: NOASSERTION
* FileName: Table.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Eases the creation of HTML tables </text>
*/
package www;
import java.util.ArrayList;
/**
*
* @author Nuno Brito, 17th of November 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class Table {
String
headerOutput,
footerOutput;
int size = 0;
String
color1 = "<td style=\"font-family: Arial;\">",
color2 = "<td style=\"font-family: Arial; background-color: rgb(219, 229, 241);\">";
Boolean alternate = true;
//ArrayList<String[]> items = new ArrayList();
String
itemData = "",
headerData = "";
/**
* Public constructor, important to define the array
* @param header
*/
public Table(String[] header){
size = header.length;
String result = "";
for(String item : header){
result += //"<th>"
"<th style=\"text-align: left; color: rgb(31, 73, 125); "
+ "font-weight: bold; font-family: Arial;\">"
+ item
+ "</th>";
}
// do the header
headerData = "<tr>"
+ result
+ "</tr>";
}
/**
* Add this item to our table list
* @param item the data that we want to fill our list with
*/
public void add(String[] item){
// we need items with the same size as available columns
if(item.length != size){
System.err.println("TB001 - Column sizes don't match: ["
+ utils.text.arrayToString(item, "], [")+ "]");
return;
}
// all good, add up this item
//items.add(item);
itemData += "<tr>";
String color;// = color1;
// do the color switching
if(alternate){
color = color2;
alternate = false;
}else{
color = color1;
alternate = true;
}
// go through each column
for(String text : item){
itemData += color
//+ "<small>"
+ text
//+ "</small>"
+ "</td>";
}
itemData += "</tr>";
}
/**
* Provides the output of this table along with its data
* @return
*/
public String output(){
String result;
// do the header
result = "<table style=\"text-align: left; width: 5px;\" border=\"0\""
+ "cellpadding=\"2\" cellspacing=\"2\">"
+ "<tbody>";
result +=
headerData
+ itemData;
// // now add up our items
// for(String[] item : items){
// result += "<tr>";
// // go through each column
// for(String text : item){
// result += "<td align=\"undefined\" valign=\"undefined\">"
// + text
// + "</td>";
// }
// result += "</tr>";
// }
// add the closing part of the code
result += "</tbody></table>";
// all done
return result;
}
}