001    /*
002     * This file is part of the Echo Point Project.  This project is a
003     * collection of Components that have extended the Echo Web Application
004     * Framework Version 3.
005     *
006     * Version: MPL 1.1
007     *
008     * The contents of this file are subject to the Mozilla Public License Version
009     * 1.1 (the "License"); you may not use this file except in compliance with
010     * the License. You may obtain a copy of the License at
011     * http://www.mozilla.org/MPL/
012     *
013     * Software distributed under the License is distributed on an "AS IS" basis,
014     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
015     * for the specific language governing rights and limitations under the
016     * License.
017     */
018    
019    package echopoint;
020    
021    /**
022     * A simple extension of {@link RegexTextField} that allows only
023     * numeric characters and one period ({@code .}) to be entered.  The  precision
024     * (number of fractional digits may be controlled using the {@link
025     * #setPrecision} method.
026     *
027     * <p>The following shows sample use of this component:</p>
028     * <pre>
029     *  import nextapp.echo.app.TextField;
030     *  import echopoint.NumberTextField;
031     *
032     *    ...
033     *    final TextField tf = new NumberTextField( 2 );
034     *    grid.add( tf );
035     * </pre>
036     *
037     * @author Rakesh 2009-03-07
038     * @version $Id: NumberTextField.java 258 2009-12-07 16:18:24Z sptrakesh $
039     */
040    public class NumberTextField extends RegexTextField
041    {
042      private static final long serialVersionUID = 1L;
043    
044      public static final String PROPERTY_PRECISION = "precision";
045    
046      /** Default constructor.  No actions required. */
047      public NumberTextField() {}
048    
049      /**
050       * Create a new instance with the specified precision value.
051       *
052       * @param precision The maximum number of fractional digits allowed.
053       */
054      public NumberTextField( final int precision )
055      {
056        setPrecision( precision );
057      }
058    
059      /**
060       * Return the value of the {@link #PROPERTY_PRECISION} property.  This
061       * indicates the number of fractional digits allowed in the number.
062       *
063       * @return The precision or {@code -1} if not set.
064       */
065      public int getPrecision()
066      {
067        int precision = -1;
068    
069        final Object value = get( PROPERTY_PRECISION );
070        if ( value != null )
071        {
072          precision = (Integer) value;
073        }
074    
075        return precision;
076      }
077    
078      /**
079       * Set the value of the {@link #PROPERTY_PRECISION} property.
080       *
081       * @param precision The maximum number of fractional digits.
082       */
083      public void setPrecision( final int precision )
084      {
085        final int old_prec  = getPrecision();
086        set( PROPERTY_PRECISION, precision );
087        firePropertyChange(PROPERTY_REGEX, old_prec, precision);
088      }
089    
090      /**
091       * Over-ridden to do nothing.  This component does not allow custom
092       * regular expressions.
093       *
094       * @param regex The regular expression to use.
095       */
096      @Override
097      public void setRegex( final String regex )
098      {
099        // No-op
100      }
101    }