View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
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 General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.table;
21  
22  import javax.swing.table.DefaultTableCellRenderer;
23  
24  import org.xnap.util.Formatter;
25  
26  /***
27   * A cell renderer for {@link Number} objects. 
28   *
29   * @see xnap.util.Formatter#formatNumber(double)
30   */
31  public class NumberCellRenderer extends DefaultTableCellRenderer {
32  
33      //--- Data Field(s) ---
34  
35      private String append = "";
36      private int decimal;
37  	private long minValue;
38  
39      //--- Constructor(s) ---
40  
41      public NumberCellRenderer(long minValue, int decimal) 
42      {
43  		this.minValue = minValue;
44  		this.decimal = decimal;
45  	
46  		setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
47      }
48  
49      public NumberCellRenderer(long minValue)
50      {
51  		this(minValue, 0);
52      }
53  
54      public NumberCellRenderer()
55      {
56  		this(0, 0);
57      }
58  
59      //--- Method(s) ---
60  
61      /***
62       * Sets the text that is appended to the value.
63       *
64       * @param newValue the text to append
65       */
66      public void setAppend(String newValue) 
67      {
68  		append = newValue;
69      }
70  
71      /***
72       * Sets the position of the decimal separator.
73       */
74      public void setDecimal(int newValue) 
75      {
76  		decimal = newValue;
77      }
78  
79      public void setValue(Object value) 
80      {
81  		if (value instanceof Number) {
82  			double d = ((Number)value).doubleValue();
83  			if (d >= minValue) {
84  				String s = Formatter.formatNumber(d, decimal);
85  				super.setValue(s + append);
86  				return;
87  			}
88  		}
89  
90  		super.setValue(null);
91      } 
92  }