KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > thickclient > application > swt > NumericText


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: NumericText.java,v 1.2 2007/01/07 06:14:15 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package org.opensubsystems.patterns.thickclient.application.swt;
23
24 import java.text.DecimalFormat JavaDoc;
25 import java.text.DecimalFormatSymbols JavaDoc;
26 import java.text.NumberFormat JavaDoc;
27
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.VerifyEvent;
30 import org.eclipse.swt.events.VerifyListener;
31 import org.eclipse.swt.layout.FillLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Layout;
34 import org.eclipse.swt.widgets.Text;
35
36 /**
37  * Text field allowing to type only numbers.
38  *
39  * @version $Id: NumericText.java,v 1.2 2007/01/07 06:14:15 bastafidli Exp $
40  * @author Miro Halas
41  * @code.reviewer Miro Halas
42  * @code.reviewed Initial revision
43  */

44 public class NumericText extends Composite
45 {
46    // Attributes ///////////////////////////////////////////////////////////////
47

48    /**
49     * Decimal point character to use.
50     */

51    protected char m_cDecimalPoint;
52
53    /**
54     * Decimal point string to use.
55     */

56    protected String JavaDoc m_strDecimalPoint;
57    
58    /**
59     * Maximum number of digits allowed after the decimal point.
60     */

61    public static final int MAX_DECIMAL_DIGITS = 2;
62    
63    /**
64     * Text field used by this composite.
65     */

66    protected Text m_textField;
67    
68    /**
69     * If true then numbers with decimal point are allowed (there can be at most
70     * 1 decimal point character in the number).
71     */

72    protected boolean m_bDecimalPoint;
73    
74    // Constructors /////////////////////////////////////////////////////////////
75

76    /**
77     * Create text field, which accepts only numbers.
78     *
79     * @param parent - parent which owns this text field
80     * @param style - style to use for this text field
81     * @param bDecimalPoint - if true, number can contain decimal point
82     */

83    public NumericText(
84       Composite parent,
85       int style,
86       boolean bDecimalPoint
87    )
88    {
89       super(parent, style);
90
91       NumberFormat JavaDoc format = NumberFormat.getCurrencyInstance();
92       DecimalFormatSymbols JavaDoc decsym;
93       
94       if (format instanceof DecimalFormat JavaDoc)
95       {
96          decsym = ((DecimalFormat JavaDoc)format).getDecimalFormatSymbols();
97          m_cDecimalPoint = decsym.getDecimalSeparator();
98       }
99       else
100       {
101          m_cDecimalPoint = '.';
102       }
103       m_strDecimalPoint = Character.toString(m_cDecimalPoint);
104       
105       m_bDecimalPoint = bDecimalPoint;
106       setLayout(createLayout());
107
108       int iTextStyle;
109       
110       iTextStyle = SWT.RIGHT | SWT.SINGLE;
111       if ((style & SWT.READ_ONLY) != 0)
112       {
113          iTextStyle |= SWT.READ_ONLY;
114       }
115       m_textField = new Text(this, iTextStyle);
116       m_textField.addVerifyListener(new VerifyListener()
117       {
118          public void verifyText(VerifyEvent e)
119          {
120             // check for value
121
String JavaDoc text = e.text;
122             char[] chars = new char[text.length()];
123             text.getChars(0, chars.length, chars, 0);
124             for (int index = 0; index < chars.length; index++)
125             {
126                if (!((('0' <= chars[index]) && (chars[index] <= '9'))
127                   || ((chars[index] == m_cDecimalPoint) && (m_bDecimalPoint))))
128                {
129                   e.doit = false;
130                   return;
131                }
132             }
133             
134             // only one decimal point
135
if (m_bDecimalPoint)
136             {
137                if ((chars.length == 1) && (chars[0] == m_cDecimalPoint))
138                {
139                   // add decimal point
140
if (m_textField.getText().indexOf(m_cDecimalPoint) >= 0)
141                   {
142                      // there is another one
143
e.doit = false;
144                      return;
145                   }
146                }
147                else if ((chars.length == 1)
148                        && (chars[0] >= '0' && chars[0] <= '9'))
149                {
150                   int decIndex = m_textField.getText().indexOf(m_cDecimalPoint);
151                   if ((decIndex >= 0)
152                      && (m_textField.getText().length()
153                          - decIndex > MAX_DECIMAL_DIGITS))
154                   {
155                      e.doit = false;
156                      return;
157                   }
158                }
159             }
160             if (text.length() > 0)
161             {
162                if (m_bDecimalPoint)
163                {
164                   try
165                   {
166                      text = m_textField.getText();
167                      if ((text != null) && (text.length() > 0)
168                         && (!text.equals(m_strDecimalPoint)))
169                      {
170                         Double.parseDouble(text);
171                      }
172                   }
173                   catch (NumberFormatException JavaDoc exec)
174                   {
175                      e.doit = false;
176                   }
177                }
178                else
179                {
180                   try
181                   {
182                      // We cannot allow too big number
183
text = m_textField.getText();
184                      if ((text != null) && (text.length() > 0))
185                      {
186                         Integer.parseInt(text);
187                      }
188                   }
189                   catch (NumberFormatException JavaDoc nfe)
190                   {
191                      // The number is too big
192
e.doit = false;
193                   }
194                }
195             }
196          }
197       });
198    }
199    
200    /**
201     * Get text field used by this composite.
202     *
203     * @return Text
204     */

205    public Text getTextField(
206    )
207    {
208       return m_textField;
209    }
210    
211    /**
212     * Create layout which will be used by this control.
213     *
214     * @return Layout - layout to use by this control.
215     */

216    public Layout createLayout(
217    )
218    {
219       return new FillLayout();
220    }
221    
222    /**
223     * This function sets maximal number of characters typed. If decimal point
224     * is allowed then this count has to include the decimal point. This method
225     * has to be called BEFORE any text is present in the field.
226     *
227     * @param iLimit - maximal number of characters
228     * @return boolean - sucess flag
229     */

230    public boolean setTextLimit(
231       int iLimit
232    )
233    {
234       boolean bReturn = false;
235       
236       if (m_textField != null)
237       {
238          m_textField.setTextLimit(iLimit);
239          bReturn = true;
240       }
241       return bReturn;
242    }
243    
244    /**
245     * @return String - string representing decimal point
246     */

247    public String JavaDoc getDecimalPointText()
248    {
249       return m_strDecimalPoint;
250    }
251 }
252
Popular Tags