/Users/lyon/j4p/src/net/server/servlets/HtmlTable.java
|
1 package net.server.servlets;
2
3 /**
4 * The HtmlTable class encapsulates the functionality
5 * required to construct an HTML representation of a table.
6 *
7 * The number of rows and columns of the required table
8 * are specified as arguments to the constructor, as is the
9 * size of the border drawn around the table.
10 *
11 * The table can then be populated with data items using
12 * the setItem method.
13 *
14 * To retrieve the resulting table, the getHtml method is
15 * invoked.
16 *
17 * @author Robert Lysik
18 * @version 1.00
19 */
20 public class HtmlTable {
21 // Data members of the HtmlTable class
22 // which will be used to construct an
23 // HTML representation of a table.
24 private int rows;
25 private int cols;
26 private int border;
27 private String tableItem[][];
28
29 /**
30 * This function sets the value of an element in the
31 * table specified by the parameters r and c passed
32 * in as arguments.
33 *
34 */
35 public HtmlTable(int r,
36 int c,
37 int b) {
38 rows = r;
39 cols = c;
40 border = b;
41 tableItem = new String[rows][cols];
42 }
43
44 /**
45 * This function sets the value of an element in the
46 * table specified by the parameters r and c passed
47 * in as arguments.
48
49 */
50 public void setElement(int r,
51 int c,
52 String value) {
53 tableItem[r][c] = value;
54 }
55
56 /**
57 * This function returns the value of the element in
58 * the table as specified by the r and c parameters
59 * passed in as arguments.
60 *
61 */
62 public String getElement(int r,
63 int c) {
64 return tableItem[r][c];
65 }
66
67 /**
68 * This function returns the HTML script to generate
69 * the table as a string.
70 */
71 public String getHtml() {
72 String html = new String();
73
74 // Start the table definition with the opening HTML tag
75 html = "<table border = " + border + ">\r\n";
76
77 // Loop through each row and column index for the table
78 // Prefix each new row with the <tr> HTML tag, and
79 // each element in the row using the <td> tag. The
80 // value for each item is stored in the tableItem String
81 // array.
82 for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
83 html += "<tr>\r\n";
84 for (int colIndex = 0; colIndex < cols; colIndex++) {
85 html += "<td>" +
86 (tableItem[rowIndex][colIndex]) +
87 "\r\n";
88 }
89 }
90
91 // Add the closing tag for the table.
92 html += "</table>\r\n";
93
94 return html;
95 }
96 }
97