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: IntegerColumn.java,v $
21  	$Date: 2008/04/02 11:22:14 $
22  */
23  package net.sf.statcvs.reportmodel;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import net.sf.statcvs.renderer.TableCellRenderer;
29  
30  /**
31   * A column of integer values. The column's total is the sum of all values. 
32   * 
33   * @author Richard Cyganiak <rcyg@gmx.de>
34   * @version $Id: IntegerColumn.java,v 1.2 2008/04/02 11:22:14 benoitx Exp $
35   */
36  public class IntegerColumn extends Column {
37  
38      private final String title;
39      private final List values = new ArrayList();
40      private int sum = 0;
41      private boolean showValues = true;
42      private boolean showPercentages = true;
43  
44      /**
45       * Creates a new <tt>SimpleTextColumn</tt> with the given head
46       * @param title the head of the column
47       */
48      public IntegerColumn(final String title) {
49          this.title = title;
50      }
51  
52      /**
53       * Set if the actual integer values should be shown
54       * @param enable show values?
55       */
56      public void setShowValues(final boolean enable) {
57          showValues = enable;
58      }
59  
60      /**
61       * Set if the values should be shown as percentages
62       * @param enable show percentages?
63       */
64      public void setShowPercentages(final boolean enable) {
65          showPercentages = enable;
66      }
67  
68      /**
69       * Adds a value to this column (in a new row)
70       * @param value the new value
71       */
72      public void addValue(final int value) {
73          values.add(new Integer(value));
74          sum += value;
75      }
76  
77      /**
78       * Returns a value in the column
79       * @param rowIndex the row to get, starting at 0
80       * @return the value of this row
81       */
82      public int getValue(final int rowIndex) {
83          return ((Integer) values.get(rowIndex)).intValue();
84      }
85  
86      /**
87       * Returns the sum of all values in the column
88       * @return sum
89       */
90      public int getSum() {
91          return sum;
92      }
93  
94      /**
95       * Sets the sum of the column. Useful if, for example, the
96       * column contains only the top 10 values of more values, but
97       * the column total should reflect all values.
98       * @param sum the column's total
99       */
100     public void setSum(final int sum) {
101         this.sum = sum;
102     }
103 
104     /**
105      * @see net.sf.statcvs.reportmodel.Column#getRows()
106      */
107     public int getRows() {
108         return values.size();
109     }
110 
111     /**
112      * @see net.sf.statcvs.reportmodel.Column#renderHead(net.sf.statcvs.renderer.TableCellRenderer)
113      */
114     public void renderHead(final TableCellRenderer renderer) {
115         renderer.renderCell(title);
116     }
117 
118     /**
119      * @see net.sf.statcvs.reportmodel.Column#renderCell
120      */
121     public void renderCell(final int rowIndex, final TableCellRenderer renderer) {
122         callRenderer(renderer, getValue(rowIndex));
123     }
124 
125     /**
126      * @see net.sf.statcvs.reportmodel.Column#renderTotal(net.sf.statcvs.renderer.TableCellRenderer)
127      */
128     public void renderTotal(final TableCellRenderer renderer) {
129         callRenderer(renderer, sum);
130     }
131 
132     private void callRenderer(final TableCellRenderer renderer, final int value) {
133         if (showValues && showPercentages) {
134             renderer.renderIntegerCell(value, sum);
135         } else if (showValues) {
136             renderer.renderIntegerCell(value);
137         } else if (showPercentages) {
138             renderer.renderPercentageCell((double) value / (double) sum);
139         } else {
140             renderer.renderEmptyCell();
141         }
142     }
143 }