View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  
23  
24  package zeus.gui.fields;
25  
26  import java.awt.Toolkit;
27  import java.awt.event.*;
28  import javax.swing.*;
29  import javax.swing.text.*;
30  import javax.swing.event.*;
31  
32  
33  public class WholeNumberField extends JTextField
34                            implements FocusListener, DocumentListener  {
35  
36     protected static char MINUS_CHAR = '-';
37     protected EventListenerList changeListeners = new EventListenerList();
38     protected long min;
39     protected long max;
40     protected boolean range_check = false;
41     protected boolean range_checked = false;
42  
43     public WholeNumberField() {
44        super();
45     }
46     public WholeNumberField(long min, long max) {
47        this();
48        this.min = min;
49        this.max = max;
50        range_check = true;
51        this.addFocusListener(this);
52     }
53     public WholeNumberField(int min, int max) {
54        this();
55        this.min = (long)min;
56        this.max = (long)max;
57        range_check = true;
58        this.addFocusListener(this);
59     }
60  
61     public void focusGained(FocusEvent evt) {
62     }
63     public void focusLost(FocusEvent evt) {
64        if ( range_check && !range_checked ) {
65           range_checked = true;
66           try {
67              long value = (Long.valueOf(getText())).longValue();
68              if ( value < min || value > max ) {
69                 errorMsg();
70                 return;
71              }
72           }
73           catch(NumberFormatException e) {
74              errorMsg();
75              return;
76           }
77        }
78     }
79  
80     public void setText(Integer obj) {
81        setText(obj.toString());
82     }
83     public void setText(Long obj) {
84        setText(obj.toString());
85     }
86  
87     protected void errorMsg() {
88        JOptionPane.showMessageDialog(this,
89           "Illegal entry\nValue must be between " + min + " and " +
90           max + " inclusive","Error", JOptionPane.ERROR_MESSAGE);
91     }
92  
93     public void setValue(int value) {
94        setValue((long)value);
95     }
96     public void setValue(long value) {
97        if ( range_check ) {
98           if ( value < min || value > max ) {
99              errorMsg();
100             return;
101          }
102       }
103       setText(Long.toString(value));
104    }
105 
106    public Long getValue() {
107       try {
108          return new Long(getText());
109       }
110       catch(NumberFormatException e) {
111          return null;
112       }
113    }
114 
115    public Long getValue(int default_value) {
116       return getValue((long)default_value);
117    }
118 
119    public Long getValue(long default_value) {
120       Long value = getValue();
121       if ( value == null )
122          return new Long(default_value);
123       else
124          return value;
125    }
126 
127    protected Document createDefaultModel() {
128       Document doc = new WholeNumberFieldDocument();
129       doc.addDocumentListener(this);
130       return doc;
131    }
132 
133    public void insertUpdate(DocumentEvent e) {
134       range_checked = false;
135       fireChanged();
136    }
137    public void removeUpdate(DocumentEvent e) {
138       range_checked = false;
139       fireChanged();
140    }
141    public void changedUpdate(DocumentEvent e) {
142       range_checked = false;
143       fireChanged();
144    }
145 
146    static char[] numberSet = {
147     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
148    };
149 
150    class WholeNumberFieldDocument extends PlainDocument {
151       public void insertString(int offs, String str, AttributeSet a)
152          throws BadLocationException {
153 
154          if ( str == null ) return;
155          str = str.trim();
156 
157          String buf = getText(0,offs) + str;
158          char[] array = buf.toCharArray();
159 
160          if ( array.length > 0 ) {
161             if ( array[0] != MINUS_CHAR && !member(array[0],numberSet) ) {
162                Toolkit.getDefaultToolkit().beep();
163                return;
164             }
165          }
166 
167          for(int i = 1; i < array.length; i++ ) {
168             if ( !member(array[i],numberSet) ) {
169                Toolkit.getDefaultToolkit().beep();
170                return;
171             }
172          }
173          super.insertString(offs,str,a);
174       }
175    }
176    static boolean member(char item, char[] array) {
177       for(int i = 0; i < array.length; i++ )
178          if ( array[i] == item ) return true;
179       return false;
180    }
181    //------------------------------------------------------------------------
182    // Event Methods
183    //------------------------------------------------------------------------
184 
185    public void addChangeListener(ChangeListener x) {
186       changeListeners.add(ChangeListener.class, x);
187    }
188 
189    public void removeChangeListener(ChangeListener x) {
190       changeListeners.remove(ChangeListener.class, x);
191    }
192 
193    protected void fireChanged() {
194       ChangeEvent c = new ChangeEvent(this);
195       Object[] listeners = changeListeners.getListenerList();
196       for(int i= listeners.length-2; i >= 0; i -=2) {
197          if (listeners[i] == ChangeListener.class) {
198             ChangeListener cl = (ChangeListener)listeners[i+1];
199             cl.stateChanged(c);
200          }
201       }
202    }
203 }