Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: myComponents/JIntegerField.java


1   /* Evolvo - Image Generator
2    * Copyright (C) 2000 Andrew Molloy
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU General Public License
6    * as published by the Free Software Foundation; either version 2
7    * of the License, or (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  
19  /*
20   * @(#)JIntegerField.java   0.1   08/19/2000
21   */
22  package myComponents;
23  
24  import javax.swing.JTextField;
25  import java.awt.event.*;
26  import javax.swing.text.*;
27  import javax.swing.event.*;
28  
29  /**
30   * Extension of JTextField that only accepts positive Integer values.
31   *
32   * @version 1.1 08/19/2000
33   * @author Andy Molloy
34   */
35  public class JIntegerField extends JTextField implements ActionListener
36  {
37     /** The value of the text field. */
38     Integer value;
39     
40     /** Default constructor. */
41     public JIntegerField()
42     {
43        this(6, 0);
44     }
45  
46     /** Class constructor.  Creates a TextField made up of cols columns, whose value is v. */
47     public JIntegerField(int cols, int v)
48     {
49        super(cols);
50        value = new Integer(v);
51        addActionListener(this); // We listen to our own actionEvents so we can make sure the values being entered
52                                 // are actually positive integers.
53        setText(value.toString());
54     }
55  
56     /** Creates a IntegerDocument model for the text field. */
57     protected Document createDefaultModel()
58     {
59        return new IntegerDocument();
60     }
61  
62     /** Handles ActionEvents from this. */
63     public void actionPerformed(ActionEvent e)
64     {
65        try
66        {
67          setValue(new Integer(Integer.parseInt(getText()))); // Set our value to what's been entered in the text field
68        }
69        catch (NumberFormatException ex)
70        {
71           select(0, getText().length());  // If the value of the text field is not a valid integer, refuse to accept it and select
72                                           // the entire field.
73        }
74     }
75  
76     /** Handles ChangeEvents. */
77     public void stateChanged(ChangeEvent e)
78     {
79        setValue(value);
80        setText(value.toString());
81     }
82  
83     /** Returns the value of the text field as an int. */
84     public int getValue()
85     {
86        return value.intValue();
87     }
88  
89     /** Sets value from the text field, selects all text in the field, and generates a ChangeEvent. */
90     public void setValue(Integer v)
91     {
92        Integer oldValue = value;
93        value = v;
94        setText(value.toString());
95        select(0, getText().length());
96        firePropertyChange("value", oldValue, v);
97     }
98  
99     /** Extends PlainDocument.  Restricts user input to numbers. */
100    static class IntegerDocument extends PlainDocument
101    {
102       /** Characters that are acceptable in input. */
103       byte[] validChars = new String("0123456789").getBytes();
104       /** The number of acceptable characters. */
105       int vcLength = validChars.length;
106 
107       /** Checks the validity of a String, and inserts it if deemed valid. */
108       public void insertString(int offset, String str, AttributeSet a) throws BadLocationException
109       {
110          byte[] strArray = str.getBytes();
111          boolean valid = true;
112 
113          for (int strIndex = 0; strIndex < strArray.length ;  strIndex++)
114          {
115             boolean internalValid = false;
116             for (int vcIndex = 0; vcIndex < vcLength; vcIndex++ )
117             {
118                if ( strArray[strIndex] == validChars[vcIndex] )
119                {
120                   internalValid = true;
121                   vcIndex = vcLength;
122                }
123             }
124             if (internalValid == false)
125             {
126                valid = false;
127                strIndex = strArray.length;
128             }
129          }
130          if ( !valid )
131          {
132             return;
133          }
134          super.insertString(offset, str, a);
135       }
136    };
137 }