Save This Page
Home » openjdk-7 » net.bereza.money » gui » [javadoc | source]
    1   /*
    2    *  Copyright (C) 2000  Bill Bereza
    3    *
    4    *  This program is free software; you can redistribute it and/or modify
    5    *  it under the terms of the GNU General Public License as published by
    6    *  the Free Software Foundation; either version 2 of the License, or
    7    *  (at your option) any later version.
    8    *
    9    *  This program is distributed in the hope that it will be useful,
   10    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
   11    *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12    *  GNU General Public License for more details.
   13    *
   14    *  You should have received a copy of the GNU General Public License
   15    *  along with this program; if not, write to the Free Software
   16    *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   17    *
   18    *  Author: Bill Bereza
   19    *  email : bereza@pobox.com
   20    *  url   : http://www.pobox.com/~bereza/
   21    */
   22   package net.bereza.money.gui;
   23   
   24   import javax.swing;
   25   import javax.swing.text;
   26   
   27   public class NumberField extends JTextField
   28   {
   29     protected boolean decimals=true;
   30     
   31     public NumberField(String in, int cols)
   32     {
   33       super(in, cols);
   34     }
   35     
   36     public NumberField(int cols)
   37     {
   38       super(cols);
   39     }
   40   
   41     public NumberField(int cols, boolean decimals)
   42     {
   43       super(cols);
   44       this.decimals=decimals;
   45     }
   46     
   47     protected Document createDefaultModel()
   48     {
   49       return new NumberDocument();
   50     }
   51   
   52     class NumberDocument extends PlainDocument
   53     {
   54       public void insertString(int offs, String str, AttributeSet a) 
   55         throws BadLocationException
   56       {
   57   
   58         if (str == null)
   59         {
   60           return;
   61         }
   62         char[] upper = str.toCharArray();
   63         char[] nums = new char[upper.length];
   64         int numi=0;
   65         String text=this.getText(0, getLength());
   66         boolean decimalPoint=(text.indexOf('.')>=0) || !decimals;
   67         
   68         for (int i = 0; i < upper.length;i++)
   69         {
   70           if(Character.isDigit(upper[i])
   71              || (upper[i] == '.' && !decimalPoint)
   72              || (offs == 0 && i == 0 && upper[i] == '-'))
   73           {
   74             if(upper[i] == '.')
   75             {
   76               decimalPoint=true;
   77             }
   78             
   79             nums[numi++] = upper[i];
   80           }
   81         }
   82         super.insertString(offs, new String(nums, 0, numi), a);
   83       }
   84     }
   85   }
   86   

Save This Page
Home » openjdk-7 » net.bereza.money » gui » [javadoc | source]