Back to index

wims  3.65+svn20090927
AddressField.java
Go to the documentation of this file.
00001 /*
00002  * @(#)AddressField.java
00003  * 
00004  * $Id: AddressField.java,v 1.5 2000/12/06 05:58:01 huaz Exp $
00005  * 
00006  * Created Novenmber 25, 2000, 5:13 AM
00007  */
00008 package SharpTools; 
00009 import java.awt.*;
00010 import java.awt.event.*;
00011 import javax.swing.*;
00012 import javax.swing.text.*;
00013 
00024 public class AddressField extends JTextField {
00025 
00031     public CellPoint getAddress() {
00032        return Formula.parseAddress(getText());
00033     }
00034 
00040     public void setAddress(CellPoint point) {
00041        if (point == null)
00042            setText(null);
00043        else
00044            setText(point.toString());
00045     }
00046 
00047     
00048     //    public Object getValue() { return getAddress(); }
00049 
00050     // a class used to filter the user's input
00051     class NumberFilterDocument extends PlainDocument {
00052        private StringBuffer __scratchBuffer;
00053 
00054        public NumberFilterDocument() {
00055            __scratchBuffer = new StringBuffer();
00056        }
00057 
00058        // this is called when user inserts a string into the text field
00059        public void insertString(int offset, String text, AttributeSet aset)
00060            throws BadLocationException {
00061            if (text == null || text.length() == 0)
00062               return;           
00063 
00064            __scratchBuffer.setLength(0);
00065 
00066            // Reject all strings that cause the contents of the field not
00067            // to be a valid number (i.e., string representation of a double)
00068            try {
00069               __scratchBuffer.append(getText(0, getLength()));
00070               __scratchBuffer.insert(offset, text);
00071               // Kludge: Append a 0 so that leading decimal points
00072               // and signs will be accepted
00073            } catch(BadLocationException ble) {
00074               ble.printStackTrace();
00075               return;
00076            } catch(StringIndexOutOfBoundsException sioobe) {
00077               sioobe.printStackTrace();
00078               return;
00079            }
00080            
00081            String buf = __scratchBuffer.toString();
00082            boolean hasNumber = false;
00083            
00084            // only allow {letters}{numbers}
00085            for (int i=0; i<buf.length(); i++) {
00086               char c = buf.charAt(i);
00087 
00088               // is a letter?
00089               if (!Character.isLetter(c)) {
00090                   // no, is a digit?
00091                   if (Character.isDigit(c) && i>0)
00092                      // the first c has to be letter
00093                      // yes, following should be all digits
00094                      hasNumber = true;
00095                   else
00096                      // no, invalid
00097                      return;
00098               }
00099               else if (hasNumber) // should not be letter anymore
00100                   return;
00101            }             
00102            
00103            super.insertString(offset, text.toUpperCase(), aset);
00104        }
00105     }
00106 
00107     
00113     public AddressField(int columns) {
00114        super(columns);
00115        setDocument(new NumberFilterDocument());
00116     }
00117 }