1 6 7 20 package org.wings.text; 21 22 import org.wings.SFormattedTextField; 23 import org.wings.SFrame; 24 import org.wings.event.SParentFrameEvent; 25 import org.wings.event.SParentFrameListener; 26 import org.wings.externalizer.ExternalizeManager; 27 import org.wings.header.Script; 28 import org.wings.resource.ClasspathResource; 29 import org.wings.resource.DefaultURLResource; 30 import org.wings.session.SessionManager; 31 import org.wings.session.Session; 32 import org.wings.script.JavaScriptListener; 33 34 import java.text.DecimalFormat ; 35 import java.text.DecimalFormatSymbols ; 36 import java.text.NumberFormat ; 37 38 39 47 public class SNumberFormatter extends SAbstractFormatter implements SParentFrameListener { 48 49 private NumberFormat numberFormat; 50 private double maxVal = Double.MAX_VALUE; 51 private double minVal = Double.MIN_VALUE; 52 53 private boolean alert = false; 54 55 private JavaScriptListener javaScriptListener; 56 57 private static String functStr = "saveOld(this)"; 58 59 private static final JavaScriptListener SCRIPT_SAVEOLD = new JavaScriptListener("onfocus", functStr); 60 61 private static final String SNUMBERFORMATTER_JS = "org/wings/text/SNumberFormatter.js"; 62 65 public SNumberFormatter() { 66 Session session = SessionManager.getSession(); 67 numberFormat = session != null ? NumberFormat.getNumberInstance(session.getLocale()) : NumberFormat.getNumberInstance(); 68 } 69 70 74 public SNumberFormatter(NumberFormat nf) { 75 numberFormat = nf; 76 } 77 78 public NumberFormat getNumberFormat() { 79 return numberFormat; 80 } 81 82 87 public void setNumberFormat(NumberFormat nf) { 88 numberFormat = nf; 89 updateFormatter(); 90 } 91 92 public String valueToString(Object value) { 93 return numberFormat.format(value); 94 } 95 96 102 public Object stringToValue(String text) { 103 Object obj = null; 104 try { 105 obj = numberFormat.parseObject(text); 106 } catch (Exception e) { 107 e.printStackTrace(); 108 } 109 return obj; 110 } 111 112 116 public void setMaxVal(double maxVal) { 117 this.maxVal = maxVal; 118 updateFormatter(); 119 } 120 121 public double getMaxVal() { 122 return maxVal; 123 } 124 125 129 public void setMinVal(double minVal) { 130 this.minVal = minVal; 131 updateFormatter(); 132 } 133 134 public double getMinVal() { 135 return minVal; 136 } 137 138 private char getDecimalSeparator() { 139 DecimalFormatSymbols dfs = new DecimalFormatSymbols (); 140 if (numberFormat instanceof DecimalFormat ) { 141 dfs = ((DecimalFormat ) numberFormat).getDecimalFormatSymbols(); 142 } 143 return dfs.getDecimalSeparator(); 144 } 145 146 private char getGroupingSeparator() { 147 DecimalFormatSymbols dfs = new DecimalFormatSymbols (); 148 if (numberFormat instanceof DecimalFormat ) { 149 dfs = ((DecimalFormat ) numberFormat).getDecimalFormatSymbols(); 150 } 151 return dfs.getGroupingSeparator(); 152 } 153 154 159 public JavaScriptListener generateJavaScript(SFormattedTextField field, boolean b) { 160 161 if (field.getParentFrame() != null) { 162 addExternalizedHeader(field.getParentFrame(), SNUMBERFORMATTER_JS, "text/javascript"); 163 } 164 field.addParentFrameListener(this); 165 166 field.removeScriptListener(SCRIPT_SAVEOLD); 167 field.addScriptListener(SCRIPT_SAVEOLD); 168 169 if (b) { 170 String funct = "if (numberFormatter(" + numberFormat.getMinimumIntegerDigits() + "," + numberFormat.getMaximumIntegerDigits() + "," + numberFormat.getMinimumFractionDigits() + "," + numberFormat.getMaximumFractionDigits() + "," + minVal + "," + maxVal + "," + numberFormat.isGroupingUsed() + "," + numberFormat.isParseIntegerOnly() + ",'" + this.getDecimalSeparator() + "','" + this.getGroupingSeparator() + "',this)) submit();"; 171 javaScriptListener = new JavaScriptListener("onchange", funct); 172 } else { 173 javaScriptListener = new JavaScriptListener("onchange", "numberFormatter(" + numberFormat.getMinimumIntegerDigits() + "," + numberFormat.getMaximumIntegerDigits() + "," + numberFormat.getMinimumFractionDigits() + "," + numberFormat.getMaximumFractionDigits() + "," + minVal + "," + maxVal + "," + numberFormat.isGroupingUsed() + "," + numberFormat.isParseIntegerOnly() + ",'" + this.getDecimalSeparator() + "','" + this.getGroupingSeparator() + "',this)"); 174 } 175 return javaScriptListener; 176 } 177 178 public void setAlert(boolean alert) { 179 this.alert = alert; 180 } 181 182 public boolean isAlert() { 183 return alert; 184 } 185 186 public void parentFrameAdded(SParentFrameEvent e) { 187 addExternalizedHeader(e.getParentFrame(), SNUMBERFORMATTER_JS, "text/javascript"); 188 } 189 190 private void addExternalizedHeader(SFrame parentFrame, String classPath, String mimeType) { 191 ClasspathResource res = new ClasspathResource(classPath, mimeType); 192 String jScriptUrl = SessionManager.getSession().getExternalizeManager().externalize(res, ExternalizeManager.GLOBAL); 193 parentFrame.addHeader(new Script(mimeType, new DefaultURLResource(jScriptUrl))); 194 } 195 196 public void parentFrameRemoved(SParentFrameEvent e) { 197 199 } 200 } 201 | Popular Tags |