1 package jimm.datavision.field; 2 import jimm.util.StringUtils; 3 import java.awt.FontMetrics ; 4 import java.util.*; 5 import java.text.SimpleDateFormat ; 6 import java.text.DecimalFormat ; 7 import javax.swing.JLabel ; 8 9 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 wrappingCalculationsLabel; 23 24 protected Field field; 25 protected Object value; 26 protected String 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 { 39 if (field != null) { 40 field.deleteObserver(this); 41 if (field.getFormat() != null) 42 field.getFormat().deleteObserver(this); 43 } 44 } 45 46 47 public void update(Observable o, Object arg) { 48 if (field != null && field.getFormat() != null) 49 value = null; 50 } 51 52 String getFormattedString(Object val) { 53 if (notSameAs(val)) { 54 value = val; 55 calcValues(); 56 } 57 return formatted; 58 } 59 60 double getOutputHeight(Object val) { 61 if (notSameAs(val)) { 62 value = val; 63 calcValues(); 64 } 65 return height; 66 } 67 68 protected boolean notSameAs(Object otherValue) { 69 if (value == null) 70 return otherValue != null; 71 else 72 return !value.equals(otherValue); 73 } 74 75 78 void calcValues() { 79 if (value == null) { 80 formatted = null; 81 height = 0; 82 return; 83 } 84 85 Format format = field.getFormat(); 86 String fmtStr = format.getFormat(); 87 88 if (value instanceof Number ) { 89 if (fmtStr == null) formatted = value.toString(); 90 else formatted = getNumberFormatterFor(fmtStr).format((Number )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 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 = 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 getNumberFormatterFor(String formatString) { 119 DecimalFormat formatter = 120 (DecimalFormat )decimalFormatters.get(formatString); 121 if (formatter == null) { 122 formatter = new DecimalFormat (formatString); 123 decimalFormatters.put(formatString, formatter); 124 } 125 return formatter; 126 } 127 128 protected SimpleDateFormat getDateFormatterFor(String formatString) { 129 SimpleDateFormat formatter = 130 (SimpleDateFormat )dateFormatters.get(formatString); 131 if (formatter == null) { 132 formatter = new SimpleDateFormat (formatString); 133 dateFormatters.put(formatString, formatter); 134 } 135 return formatter; 136 } 137 138 protected static JLabel getWrappingCalcsLabel() { 139 if (wrappingCalculationsLabel == null) wrappingCalculationsLabel = new JLabel (); 141 return wrappingCalculationsLabel; 142 } 143 } 144 | Popular Tags |