Parent: [r11] (diff)

Child: [r25] (diff)

Download this file

Table.java    210 lines (182 with data), 5.9 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* 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;
/**
*
* @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 = "";
/**
* Creates a simplified table where the two elements are centered
* @param value1
* @param size1
* @param value2
* @param size2
* @return
*/
public static String simple(String value1, int size1, String value2, int size2){
return "<table style=\"text-align: left; \" border=\"0\"\n" +
" cellpadding=\"0\" cellspacing=\"0\">\n" +
" <tbody>\n" +
" <tr>\n" +
" <td style=\"width: " + size1 + "px;\">"
+ value1
+ "</td>\n" +
" <td style=\"width: " + size2 + "px;\">"
+ value2
+ "</td>\n" +
" </tr>\n" +
" </tbody>\n" +
"</table>";
}
/**
* 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>";
}
/**
* Public constructor, important to define the array
* @param header
* @param length
*/
public Table(String[] header, int[] length){
size = header.length;
String result = "";
int i = -1;
for(String item : header){
i++;
result += //"<th>"
"<th style=\"text-align: left; color: rgb(31, 73, 125); "
+ "width: " + length[i] + "px; "
+ "font-family: Arial;\">"
+ item
+ "</th>";
}
// do the header
headerData = "<tr>"
+ result
+ "</tr>";
}
/**
* Creates single table fully aligned horizontally with a single line
* @param params an array with text values
* @param size the size for each column
* @return the text in HTML format
*/
public static String alignedTable(String[] params, int[] size){
// add the header
String result = "<table style=\"text-align: left;\" border=\"0\"\n" +
" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>";
int i = -1;
for(String param : params){
i++;
result += "<td style=\"width: "
+ size[i]
+ "px;\">"
+ param
+ "</td>"
;
}
// add the footer
result += "</tr></tbody></table>";
return result;
}
/**
* 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;
}
}