1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35 private String append = "";
36 private int decimal;
37 private long minValue;
38
39
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
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 }