KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > field > FormattedValueCache


1 package jimm.datavision.field;
2 import jimm.util.StringUtils;
3 import java.awt.FontMetrics JavaDoc;
4 import java.util.*;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.text.DecimalFormat JavaDoc;
7 import javax.swing.JLabel JavaDoc;
8
9 /**
10  * Helps avoid multiple expensive formatting and font size calculations. Each
11  * {@link Field} holds on to one of these and asks it for the formatted
12  * (wrapped) version of its value or the height needed to output the formatted
13  * value.
14  *
15  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
16  */

17 class FormattedValueCache implements Observer {
18
19 protected static final double LINE_SIZE_FUDGE_FACTOR = 1.2;
20 protected static final HashMap decimalFormatters = new HashMap();
21 protected static final HashMap dateFormatters = new HashMap();
22 protected static JLabel JavaDoc wrappingCalculationsLabel;
23
24 protected Field field;
25 protected Object JavaDoc value;
26 protected String JavaDoc formatted;
27 protected double height;
28
29 FormattedValueCache(Field f) {
30     field = f;
31     if (field != null) {
32     field.addObserver(this);
33     if (field.getFormat() != null)
34         field.getFormat().addObserver(this);
35     }
36 }
37
38 protected void finalize() throws Throwable JavaDoc {
39     if (field != null) {
40     field.deleteObserver(this);
41     if (field.getFormat() != null)
42         field.getFormat().deleteObserver(this);
43     }
44 }
45
46 /** When format changes, erase value so we recalculate it */
47 public void update(Observable o, Object JavaDoc arg) {
48     if (field != null && field.getFormat() != null)
49     value = null;
50 }
51
52 String JavaDoc getFormattedString(Object JavaDoc val) {
53     if (notSameAs(val)) {
54     value = val;
55     calcValues();
56     }
57     return formatted;
58 }
59
60 double getOutputHeight(Object JavaDoc val) {
61     if (notSameAs(val)) {
62     value = val;
63     calcValues();
64     }
65     return height;
66 }
67
68 protected boolean notSameAs(Object JavaDoc otherValue) {
69     if (value == null)
70     return otherValue != null;
71     else
72     return !value.equals(otherValue);
73 }
74
75 /**
76  * Cacluates formatted (wrapped) string and output height.
77  */

78 void calcValues() {
79     if (value == null) {
80     formatted = null;
81     height = 0;
82     return;
83     }
84
85     Format format = field.getFormat();
86     String JavaDoc fmtStr = format.getFormat();
87
88     if (value instanceof Number JavaDoc) {
89     if (fmtStr == null) formatted = value.toString();
90     else formatted = getNumberFormatterFor(fmtStr).format((Number JavaDoc)value);
91     }
92     else if (value instanceof Date) {
93     if (fmtStr == null) formatted = value.toString();
94     else formatted = getDateFormatterFor(fmtStr).format((Date)value);
95     }
96     else {
97     formatted = value.toString();
98     if (format.isWrap()) {
99         FontMetrics JavaDoc fm =
100           getWrappingCalcsLabel().getFontMetrics(format.getFont());
101         formatted =
102         StringUtils.join(StringUtils.wrap(formatted, fm,
103                           (int)field.getBounds().width),
104                  "\n");
105     }
106     }
107
108     // Height calculation. Field's bounds height is the minimum height.
109
height = field.bounds.height;
110     if (formatted.length() != 0) {
111     List lines = StringUtils.splitIntoLines(formatted);
112     double h = lines.size() * format.getSize() * LINE_SIZE_FUDGE_FACTOR;
113     if (h > height)
114         height = h;
115     }
116 }
117
118 protected DecimalFormat JavaDoc getNumberFormatterFor(String JavaDoc formatString) {
119     DecimalFormat JavaDoc formatter =
120     (DecimalFormat JavaDoc)decimalFormatters.get(formatString);
121     if (formatter == null) {
122     formatter = new DecimalFormat JavaDoc(formatString);
123     decimalFormatters.put(formatString, formatter);
124     }
125     return formatter;
126 }
127
128 protected SimpleDateFormat JavaDoc getDateFormatterFor(String JavaDoc formatString) {
129     SimpleDateFormat JavaDoc formatter =
130     (SimpleDateFormat JavaDoc)dateFormatters.get(formatString);
131     if (formatter == null) {
132     formatter = new SimpleDateFormat JavaDoc(formatString);
133     dateFormatters.put(formatString, formatter);
134     }
135     return formatter;
136 }
137
138 protected static JLabel JavaDoc getWrappingCalcsLabel() {
139     if (wrappingCalculationsLabel == null) // Lazy instantiation
140
wrappingCalculationsLabel = new JLabel JavaDoc();
141     return wrappingCalculationsLabel;
142 }
143 }
144
Popular Tags