View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.monitoring.jmx.display;
18  
19  import java.util.LinkedList;
20  import java.util.List;
21  
22  /**
23   * Creates HTML tables for displaying data.
24   *
25   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/jmx/src/main/java/ch/elca/el4j/services/monitoring/jmx/display/HtmlTabulator.java $
26   *
27   * @author David Bernhard (DBD)
28   */
29  public class HtmlTabulator {
30  	
31  	/**
32  	 * The number of columns this table has.
33  	 */
34  	int m_numColumns;
35  	
36  	/**
37  	 * The title row.
38  	 */
39  	String[] m_title;
40  	
41  	/**
42  	 * The table data.
43  	 */
44  	List<String[]> m_table;
45  	
46  	/**
47  	 * The colour to use for the title background.
48  	 */
49  	String m_titleColor = "#FFFF80";
50  	
51  	/**
52  	 * The colour to use for marked rows.
53  	 * Every (m_markColor)'th row is marked.
54  	 */
55  	String m_markColor = "#FFFFC0";
56  	
57  	//Checkstyle: MagicNumber off
58  	
59  	/**
60  	 * The interval to mark rows at.
61  	 */
62  	int m_markInterval = 3;
63  	
64  	//Checkstyle: MagicNumber on
65  	
66  	/**
67  	 * @param titles The titles of the columns.
68  	 */
69  	public HtmlTabulator(String... titles) {
70  		m_numColumns = titles.length;
71  		m_table = new LinkedList<String[]>();
72  		m_title = titles;
73  	}
74  	
75  	/**
76  	 * @param args A row of data.
77  	 * @throws RuntimeException If the number of arguments does not match
78  	 * the number of columns.
79  	 */
80  	public void addRow(String ... args) throws RuntimeException {
81  		if (args.length != m_numColumns) {
82  			throw new RuntimeException("Got " + args.length
83  				+ " arguments but expected " + m_numColumns + ".");
84  		}
85  		m_table.add(args);
86  	}
87  	
88  	/**
89  	 * @return The table.
90  	 */
91  	public String tabulate() {
92  		String table = "<table>\n";
93  		
94  		// Title
95  		table += "<tr bgcolor=\"" + m_titleColor + "\"> ";
96  		for (String element : m_title) {
97  			table += "<td><b>" + element + "</b></td> ";
98  		}
99  		table += "</tr>\n";
100 		
101 		// Rows
102 		int rowCounter = 0;
103 		for (String[] currentRow : m_table) {
104 			rowCounter++;
105 			if (rowCounter == m_markInterval) {
106 				table += "<tr bgcolor=\"" + m_markColor + "\"> ";
107 				rowCounter = 0;
108 			} else {
109 				table += "<tr> ";
110 			}
111 			
112 			for (String element : currentRow) {
113 				table += "<td>" + element + "</td> ";
114 			}
115 			table += "</tr>\n";
116 		}
117 		table += "</table>\n";
118 		return table;
119 	}
120 	
121 }