KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > text > SNumberFormatter


1 /*
2  * SNumberFormatter.java
3  *
4  * Created on 9. September 2003, 12:15
5  */

6
7 /*
8  * $Id: SNumberFormatter.java,v 1.10 2005/06/07 12:55:59 neurolabs Exp $
9  * Copyright 2000,2005 wingS development team.
10  *
11  * This file is part of wingS (http://www.j-wings.org).
12  *
13  * wingS is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License
15  * as published by the Free Software Foundation; either version 2.1
16  * of the License, or (at your option) any later version.
17  *
18  * Please see COPYING for the complete licence.
19  */

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 JavaDoc;
35 import java.text.DecimalFormatSymbols JavaDoc;
36 import java.text.NumberFormat JavaDoc;
37
38
39 /**
40  * SNumberFormater supports NumberFormat.parseIntegerOnly,
41  * NumberFormat.maxIntegerDigits, NumberFormat.minIntegerDigits, NumberFormat.maxFractionDigits,
42  * NumberFormat.minIntegerDigits.
43  * Additional it's possible to set a maximal or a minimal valid value.
44  *
45  * @author theresia & erik
46  */

47 public class SNumberFormatter extends SAbstractFormatter implements SParentFrameListener {
48
49     private NumberFormat JavaDoc 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 JavaDoc functStr = "saveOld(this)";
58
59     private static final JavaScriptListener SCRIPT_SAVEOLD = new JavaScriptListener("onfocus", functStr);
60
61     private static final String JavaDoc SNUMBERFORMATTER_JS = "org/wings/text/SNumberFormatter.js";
62     /*
63      * Creates a new instance of SNumberFormatter.
64      */

65     public SNumberFormatter() {
66         Session session = SessionManager.getSession();
67         numberFormat = session != null ? NumberFormat.getNumberInstance(session.getLocale()) : NumberFormat.getNumberInstance();
68     }
69
70     /*
71      * Creates a new instance of SNumberFormatter.
72      * @param NumberFormat nf the NumberFormat for the SFormattedTextField
73      */

74     public SNumberFormatter(NumberFormat JavaDoc nf) {
75         numberFormat = nf;
76     }
77
78     public NumberFormat JavaDoc getNumberFormat() {
79         return numberFormat;
80     }
81
82     /*
83      * sets the NumberFormat. Doesn't work after the Frame is installed, because
84      * until now it's not possible to add a JavaScriptFunction correctly after that.
85      * @param NumberFormat nf the NumberFormat for the SFormattedTextField.
86      */

87     public void setNumberFormat(NumberFormat JavaDoc nf) {
88         numberFormat = nf;
89         updateFormatter();
90     }
91
92     public String JavaDoc valueToString(Object JavaDoc value) {
93         return numberFormat.format(value);
94     }
95
96     /**
97      * Specified by stringToValue in class AbstractFormatter
98      *
99      * @param text text - String to convert
100      * @return Object representation of text
101      */

102     public Object JavaDoc stringToValue(String JavaDoc text) {
103         Object JavaDoc obj = null;
104         try {
105             obj = numberFormat.parseObject(text);
106         } catch (Exception JavaDoc e) {
107             e.printStackTrace();
108         }
109         return obj;
110     }
111
112     /*
113      * sets the maximal possible value.
114      * @param double maxVal the maximal possible value for the SFormattedTextField.
115      */

116     public void setMaxVal(double maxVal) {
117         this.maxVal = maxVal;
118         updateFormatter();
119     }
120
121     public double getMaxVal() {
122         return maxVal;
123     }
124
125     /*
126     * sets the minimal possible value.
127     * @param double minVal the minimal possible value for the SFormattedTextField.
128     */

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 JavaDoc dfs = new DecimalFormatSymbols JavaDoc();
140         if (numberFormat instanceof DecimalFormat JavaDoc) {
141             dfs = ((DecimalFormat JavaDoc) numberFormat).getDecimalFormatSymbols();
142         }
143         return dfs.getDecimalSeparator();
144     }
145
146     private char getGroupingSeparator() {
147         DecimalFormatSymbols JavaDoc dfs = new DecimalFormatSymbols JavaDoc();
148         if (numberFormat instanceof DecimalFormat JavaDoc) {
149             dfs = ((DecimalFormat JavaDoc) numberFormat).getDecimalFormatSymbols();
150         }
151         return dfs.getGroupingSeparator();
152     }
153
154     /*
155      * Generates the JavaScript-function based on numberFormat, MinVal and MaxVal
156      * @param String name Name of the function.
157      * @param boolean b true if an ActionListener is added to the SFormattedTextField
158      **/

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 JavaDoc 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 JavaDoc classPath, String JavaDoc mimeType) {
191         ClasspathResource res = new ClasspathResource(classPath, mimeType);
192         String JavaDoc 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         // TODO Auto-generated method stub
198

199     }
200 }
201
Popular Tags