1 package net.sf.statcvs.renderer;
2
3 import net.sf.statcvs.model.Author;
4 import net.sf.statcvs.model.Directory;
5 import net.sf.statcvs.model.VersionedFile;
6 import net.sf.statcvs.output.WebRepositoryIntegration;
7 import net.sf.statcvs.pages.MarkupSyntax;
8 import net.sf.statcvs.pages.xml.XML;
9
10 /**
11 * Helper class for rendering different types of table cells and table heads
12 * to XML
13 *
14 * @author Nilendra Weerasinghe <nilendraw@gmail.com>
15 * @version $Id: XMLRenderer.java,v 1.3 2008/04/02 11:22:15 benoitx Exp $
16 */
17 public class XMLRenderer implements TableCellRenderer {
18 private String xml = null;
19 private MarkupSyntax output = null;
20
21 /**
22 * Render a generic table cell to XML
23 * @param content the cell's content
24 */
25 public void renderCell(final String content) {
26 xml = content;
27 }
28
29 /**
30 * Render an empty cell to XML
31 */
32 public void renderEmptyCell() {
33 xml = null;
34 }
35
36 /**
37 * Render an integer cell to XML
38 * @param value the cell's content
39 */
40 public void renderIntegerCell(final int value) {
41 xml = Integer.toString(value);
42 }
43
44 /**
45 * Render an integer cell to XML, showing both the integer value and
46 * a percentage of a total
47 * @param value the cell's content
48 * @param total the total, worth 100%
49 */
50 public void renderIntegerCell(final int value, final int total) {
51 xml = Integer.toString(value) + " (" + getPercentage((double) value / (double) total) + ")";
52 }
53
54 /**
55 * Render a percentage cell to XML
56 * @param ratio the cell's content
57 */
58 public void renderPercentageCell(final double ratio) {
59 xml = getPercentage(ratio);
60 }
61
62 /**
63 * Render a cell containing an author to XML
64 * @param author the author
65 */
66 public void renderAuthorCell(final Author author) {
67 xml = XML.escape(author.getName());
68 }
69
70 /**
71 * Render a cell containing a directory to XML
72 * @param directory the directory
73 */
74 public void renderDirectoryCell(final Directory directory) {
75 xml = XML.getDirectoryLink(directory);
76 }
77
78 /**
79 * Render a cell containing a file to XML
80 * @param file the file
81 * @param withIcon display an icon in front of the filename?
82 * @param webRepository for creating links; might be <tt>null</tt>
83 */
84 public void renderFileCell(final VersionedFile file, final boolean withIcon, final WebRepositoryIntegration webRepository) {
85
86 xml = file.getFilenameWithPath();
87
88
89
90
91
92
93
94
95
96
97
98 }
99
100 /**
101 * Render a cell containing a repository tag.
102 */
103 public void renderLinkCell(final String url, final String label) {
104 this.xml = XML.getLink(url, label);
105 }
106
107 /**
108 * Return the results of the last <tt>renderCell</tt> call
109 * @return XML
110 */
111 public String getColumnHead() {
112 return getHtml();
113 }
114
115 /**
116 * Return the results of the last <tt>renderCell</tt> call
117 * as a row head
118 * @return XML
119 */
120 public String getRowHead() {
121 return getHtml();
122 }
123
124 /**
125 * Return the results of the last <tt>renderCell</tt> call
126 * as an ordinary table cell
127 * @return XML
128 */
129 public String getTableCell() {
130 return xml;
131 }
132
133 private String getPercentage(final double ratio) {
134 if (Double.isNaN(ratio)) {
135 return "-";
136 }
137 final int percentTimes10 = (int) Math.round(ratio * 1000);
138 final double percent = percentTimes10 / 10.0;
139 return Double.toString(percent) + "%";
140 }
141
142 private String getHtml() {
143 return xml.replaceAll(" ", "");
144 }
145
146 /**
147 * @return the output
148 */
149 public MarkupSyntax getOutput() {
150 return output;
151 }
152
153 /**
154 * @param output the output to set
155 */
156 public void setOutput(final MarkupSyntax output) {
157 this.output = output;
158 }
159
160 public String getOddRowFormat() {
161 return " class=\"even\"";
162 }
163
164 public String getEvenRowFormat() {
165 return " class=\"odd\"";
166 }
167
168 public void renderAuthorIdCell(final Author author) {
169 xml = XML.getAuthorIdLink(author);
170 }
171 }