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    import echopoint.internal.TextField;
022    
023    /**
024     * A text field that enforces a user supplied regular expression on a key
025     * press.  Allows creation of serial number field, character only field etc.
026     *
027     * <p>The following shows sample use of this class.</p>
028     * <pre>
029     *  import nextapp.echo.app.TextField;
030     *  import echopoint.RegexTextField;
031     *
032     *    ...
033     *    // Create a field that allow real numbers with two digit fractional part
034     *    final TextField tf = new RegexTextField( "^[\\d]+[.]{0,1}[\\d]{1,2}$" );
035     * </pre>
036     *
037     * <p><b>Note:</b> It is recommended that you test the regular expression
038     * using a simple test html page in your browser of choice before you set
039     * it for the component.  This is to ensure that the expression you specify
040     * is valid and behaves the way you want it to work.</p>
041     *
042     * <pre>
043     * &lt;html&gt;
044     *   &lt;head&gt;
045     *     &lt;title&gt;RegExp Test&lt;/title&gt;
046     *     &lt;script type='text/javascript'&gt;
047     *       function check( event )
048     *       {
049     *         event = (event) ? event : window.event;
050     *         var charCode = (event.which) ? event.which : event.keyCode;
051     *         var status = true;
052     *
053     *         if ( charCode &lt;= 31 )
054     *         {
055     *           status = true;
056     *           return status;
057     *         }
058     *
059     *         var regexString = "^[A-Z]{1,1}[a-z]*$";
060     *         var regex = new RegExp( regexString );
061     *         var input = document.getElementById( 'textField' );
062     *         var value = input.value + String.fromCharCode( charCode );
063     *         status = regex.test( value );
064     *
065     *         return status;
066     *       }
067     *     &lt;/script&gt;
068     *   &lt;/head&gt;
069     *   &lt;body&gt;
070     *     &lt;input id='textField' type='text' onkeypress='return check(event)' /&gt;
071     *   &lt;/body&gt;
072     * &lt;/html&gt;
073     * </pre>
074     *
075     * @author Rakesh 2009-03-08
076     * @version $Id: RegexTextField.java 258 2009-12-07 16:18:24Z sptrakesh $
077     */
078    public class RegexTextField extends TextField
079    {
080      private static final long serialVersionUID = 1l;
081    
082      /**
083       * The regular expression to specify.  Note that the string value is used
084       * to create a new {@code RegExp} JavaScript object and hence the expression
085       * must be escaped as you would when compiling a Java regex pattern.
086       * This property may also be styled.
087       */
088      public static final String PROPERTY_REGEX = "regex";
089    
090      /** Default constructor.  Not particularly useful. */
091      public RegexTextField() {}
092    
093      /**
094       * Create a new text field with the specified regex pattern.
095       *
096       * @param regex The regular expression to use.
097       */
098      public RegexTextField( final String regex )
099      {
100        setRegex( regex );
101      }
102    
103      /**
104       * Return the regular expression in use for the field ({@link
105       * #PROPERTY_REGEX}).
106       *
107       * @return The regular expression in use.
108       */
109      public String getRegex()
110      {
111        return (String) get( PROPERTY_REGEX );
112      }
113    
114      /**
115       * Set the regular expression pattern ({@link #PROPERTY_REGEX}) to use to
116       * restrict input into this * field.  This value may also be styled.
117       *
118       * @param regex The regular expression to use.
119       */
120      public void setRegex( final String regex )
121      {
122        final String old_regex = getRegex();
123        set( PROPERTY_REGEX, regex );
124        firePropertyChange(PROPERTY_REGEX, old_regex, regex);
125      }
126    }