View Javadoc

1   /*
2   	StatCvs - CVS statistics generation
3   	Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
4   	http://statcvs.sf.net/
5   
6   	This library is free software; you can redistribute it and/or
7   	modify it under the terms of the GNU Lesser General Public
8   	License as published by the Free Software Foundation; either
9   	version 2.1 of the License, or (at your option) any later version.
10  
11  	This library is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  	Lesser General Public License for more details.
15  
16  	You should have received a copy of the GNU Lesser General Public
17  	License along with this library; if not, write to the Free Software
18  	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  
20  	$RCSfile: HTMLTableCellRenderer.java,v $
21  	$Date: 2008/04/02 11:22:15 $
22  */
23  package net.sf.statcvs.renderer;
24  
25  import net.sf.statcvs.Messages;
26  import net.sf.statcvs.model.Author;
27  import net.sf.statcvs.model.Directory;
28  import net.sf.statcvs.model.VersionedFile;
29  import net.sf.statcvs.output.WebRepositoryIntegration;
30  import net.sf.statcvs.pages.HTML;
31  import net.sf.statcvs.pages.MarkupSyntax;
32  import net.sf.statcvs.pages.ReportSuiteMaker;
33  
34  /**
35   * Helper class for rendering different types of table cells and table heads
36   * to HTML
37   *
38   * @author Richard Cyganiak <rcyg@gmx.de>
39   * @version $Id: HTMLTableCellRenderer.java,v 1.10 2008/04/02 11:22:15 benoitx Exp $
40   */
41  public class HTMLTableCellRenderer implements TableCellRenderer {
42      private String html = null;
43      private MarkupSyntax output = null;
44  
45      /**
46       * Render a generic table cell to HTML
47       * @param content the cell's content
48       */
49      public void renderCell(final String content) {
50          html = content;
51      }
52  
53      /**
54       * Render an empty cell to HTML
55       */
56      public void renderEmptyCell() {
57          html = null;
58      }
59  
60      /**
61       * Render an integer cell to HTML
62       * @param value the cell's content
63       */
64      public void renderIntegerCell(final int value) {
65          html = Integer.toString(value);
66      }
67  
68      /**
69       * Render an integer cell to HTML, showing both the integer value and
70       * a percentage of a total
71       * @param value the cell's content
72       * @param total the total, worth 100%
73       */
74      public void renderIntegerCell(final int value, final int total) {
75          html = Integer.toString(value) + " (" + getPercentage((double) value / (double) total) + ")";
76      }
77  
78      /**
79       * Render a percentage cell to HTML
80       * @param ratio the cell's content
81       */
82      public void renderPercentageCell(final double ratio) {
83          html = getPercentage(ratio);
84      }
85  
86      /**
87       * Render a cell containing an author to HTML
88       * @param author the author
89       */
90      public void renderAuthorCell(final Author author) {
91          html = HTML.getAuthorLink(author);
92      }
93  
94      /**
95       * Render a cell containing an author Id to HTML
96       * @param author the author
97       */
98      public void renderAuthorIdCell(final Author author) {
99          html = HTML.getAuthorIdLink(author);
100     }
101 
102     /**
103      * Render a cell containing a directory to HTML
104      * @param directory the directory
105      */
106     public void renderDirectoryCell(final Directory directory) {
107         html = HTML.getDirectoryLink(directory);
108     }
109 
110     /**
111      * Render a cell containing a file to HTML
112      * @param file the file
113      * @param withIcon display an icon in front of the filename?
114      * @param webRepository for creating links; might be <tt>null</tt>
115      */
116     public void renderFileCell(final VersionedFile file, final boolean withIcon, final WebRepositoryIntegration webRepository) {
117         if (webRepository == null) {
118             html = file.getFilenameWithPath();
119         } else {
120             html = HTML.getLink(webRepository.getFileViewUrl(file), file.getFilenameWithPath());
121         }
122         if (withIcon) {
123             if (file.isDead()) {
124                 html = HTML.getIcon(ReportSuiteMaker.DELETED_FILE_ICON, Messages.getString("DELETED_FILE_ICON")) + " " + html;
125             } else {
126                 html = HTML.getIcon(ReportSuiteMaker.FILE_ICON, Messages.getString("FILE_ICON")) + " " + html;
127             }
128         }
129     }
130 
131     /**
132      * Render a cell containing a repository tag.
133      */
134     public void renderLinkCell(final String url, final String label) {
135         this.html = HTML.getLink(url, label);
136     }
137 
138     /**
139      * Return the results of the last <tt>renderCell</tt> call
140      * @return HTML
141      */
142     public String getColumnHead() {
143         return getHtml("th");
144     }
145 
146     /**
147      * Return the results of the last <tt>renderCell</tt> call
148      * as a row head
149      * @return HTML
150      */
151     public String getRowHead() {
152         return getHtml("th");
153     }
154 
155     /**
156      * Return the results of the last <tt>renderCell</tt> call
157      * as an ordinary table cell
158      * @return HTML
159      */
160     public String getTableCell() {
161         return getHtml("td");
162     }
163 
164     private String getPercentage(final double ratio) {
165         if (Double.isNaN(ratio)) {
166             return "-";
167         }
168         final int percentTimes10 = (int) Math.round(ratio * 1000);
169         final double percent = percentTimes10 / 10.0;
170         return Double.toString(percent) + "%";
171     }
172 
173     private String getHtml(final String tag) {
174         if (html == null) {
175             return "<" + tag + "></" + tag + ">";
176         }
177         return "<" + tag + ">" + html + "</" + tag + ">";
178     }
179 
180     /**
181      * @return the output
182      */
183     public MarkupSyntax getOutput() {
184         return output;
185     }
186 
187     /**
188      * @param output the output to set
189      */
190     public void setOutput(final MarkupSyntax output) {
191         this.output = output;
192     }
193 
194     public String getOddRowFormat() {
195         return " class=\"even\"";
196     }
197 
198     public String getEvenRowFormat() {
199         return " class=\"odd\"";
200     }
201 }