home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / JDateMaskedField.java < prev    next >
Text File  |  1998-12-09  |  10KB  |  326 lines

  1. /*
  2.  * @(#JDateMaskedField.java
  3.  *
  4.  * Copyright (c) 1998 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7. package com.symantec.itools.swing;
  8.  
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.io.Serializable;
  12.  
  13. /**
  14.  * JDateMaskedField is a masked text component that automatically assumes a mask 
  15.  * based on the current locale and the type and formatting style specified by 
  16.  * the user.
  17.  * <p>
  18.  * Warning: serialized objects of this class will not be compatible with
  19.  * future swing releases.  The current serialization support is appropriate
  20.  * for short term storage or RMI between Swing1.0 applications.  It will
  21.  * not be possible to load serialized Swing1.0 objects with future releases
  22.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  23.  * baseline for the serialized form of Swing objects.
  24.  *
  25.  * @author Vasudev J. Rao
  26.  * @version 1.0 
  27.  * @see com.symantec.itools.swing.JMaskedTextField
  28.  */
  29.  
  30. public class JDateMaskedField 
  31. extends com.symantec.itools.swing.JMaskedTextField 
  32. implements Serializable, DateMaskedFieldConstants
  33. {
  34.     
  35.     
  36.     protected int type = DATE_TYPE ;
  37.     protected int formattingStyle = DateFormat.DEFAULT ;
  38.     protected boolean includeLiterals = true;
  39.     
  40.     protected DatePatternToMaskXLator xlator = new DatePatternToMaskXLator();
  41.     
  42.     
  43.     /**
  44.      * Default contsructor. Constructs a date masked field of LONG style.
  45.      */
  46.     public JDateMaskedField ( ) {
  47.         
  48.         this ( DATE_TYPE , DateFormat.LONG );
  49.         
  50.     }
  51.    
  52.     /**
  53.      * Constructs a masked field with the specified date type and style.
  54.      * Type can be one of DATE_TYPE, TIME_TYPE or TIMESTAMP_TYPE. Style 
  55.      * can be one of DateFormat.DEFAULT, FULL, LONG, MEDIUM or SHORT.
  56.      */
  57.     public JDateMaskedField ( int type , int style ) {
  58.         
  59.         super ( );
  60.         setType ( type );
  61.         setFormattingStyle ( style );
  62.         
  63.     }
  64.     
  65.     /**
  66.      * Set the style of the date, time or timestamp. Should be one of DateFormat.DEFAULT,
  67.      * FULL, LONG, MEDIUM or SHORT. If the type is invalid, DEFAULT is used.
  68.      */
  69.     
  70.     public void setFormattingStyle ( int newStyle) {
  71.         
  72.         int oldStyle = this.formattingStyle;
  73.         if ( newStyle == DateFormat.DEFAULT ||
  74.              newStyle == DateFormat.FULL    ||
  75.              newStyle == DateFormat.LONG    ||
  76.              newStyle == DateFormat.MEDIUM  ||
  77.              newStyle == DateFormat.SHORT     ) { 
  78.                 
  79.             formattingStyle = newStyle ;
  80.             String tempMask = getMaskForDateFormat ( getFormatter ( getType() , getFormattingStyle() ) );
  81.             setMask ( tempMask );
  82.           
  83.             firePropertyChange("formattingStyle", oldStyle , newStyle );
  84.         }
  85.         else{
  86.             throw new IllegalArgumentException ( INVALID_STYLE );
  87.  
  88.         }
  89.         
  90.         
  91.     }
  92.     
  93.     /**
  94.      * Returns the style of date, time or timestamp. Would be one of DateFormat.DEFAULT,
  95.      * FULL, LONG, MEDIUM or SHORT.
  96.      */
  97.     public int getFormattingStyle ( ){
  98.     
  99.         return this.formattingStyle ;
  100.         
  101.     }
  102.     
  103.     /**
  104.      * Set the type of date. Should be one of DATE_TYPE, TIME_TYPE or TIMESTAMP_TYPE.
  105.      * If the type supplied is invalid DATE_TYPE is used.
  106.      */
  107.     public void setType ( int newType){
  108.         
  109.         int oldType = type;
  110.         
  111.         if ( newType == DATE_TYPE || 
  112.              newType == TIME_TYPE || 
  113.              newType == TIMESTAMP_TYPE ) {
  114.                 
  115.             type = newType ;
  116.             
  117.             String tempMask = getMaskForDateFormat ( getFormatter ( getType() , getFormattingStyle() ) );
  118.             setMask ( tempMask );
  119.             
  120.             firePropertyChange("type", oldType , newType );
  121.         }
  122.         else{
  123.             throw new IllegalArgumentException ( INVALID_TYPE );
  124.  
  125.         }
  126.         
  127.         
  128.     }
  129.     
  130.     /**
  131.      * Returns the type of date. Would be one of DATE_TYPE, TIME_TYPE or TIMESTAMP_TYPE.
  132.      */
  133.     
  134.     public int getType ( ) {
  135.     
  136.         return this.type;
  137.     
  138.     }
  139.     
  140.     /**
  141.      * Returns the current mask.
  142.      */
  143.     
  144.     public String getDateMask ( ) {
  145.         
  146.         return super.getMask() ;
  147.     }
  148.     
  149.     /**
  150.      * Get the date/time formatter pattern for the current type and formatting style.
  151.      */
  152.     
  153.     public String getPattern () {
  154.         
  155.         return getFormatterPattern ( getFormatter ( getType() , getFormattingStyle() ) );
  156.         
  157.     }
  158.     
  159.     /**
  160.      * If set to true, the method setMaskedText would expect 
  161.      * the dates to have separators and all other literals. Also 
  162.      * the method getUnmaskedText would return all the separators
  163.      * and literals. If false, the behavior would default to the behavior of
  164.      * JMaskedTextField.
  165.      * <p>
  166.      * For eg., in case of SHORT date, 
  167.      * when set to true, setMaskedText expects dates "10-Jun-1998" and when set
  168.      * to false it expects 10Jun1998. Similarly, getUnmaskedText would return
  169.      * "10-Jun-1998" or "10Jun1998" based on ths setting.
  170.      */
  171.     
  172.     public void setIncludeLiterals ( boolean b ) {
  173.         
  174.         includeLiterals = b; 
  175.         
  176.     }
  177.     
  178.     /**
  179.      * Returns whether or not literals would be included in the masked and Unmasked 
  180.      * texts.
  181.      */
  182.     public boolean isIncludeLiterals (  ) {
  183.         
  184.         return includeLiterals ;
  185.         
  186.     }
  187.     
  188.     //over-ride
  189.     
  190.     public void setMaskedText ( String s ) {
  191.         if ( s == null || s.length() == 0 ) {
  192.             super.setMaskedText ( "" );
  193.             return;
  194.         }
  195.         
  196.         String matchedInput = "";
  197.         try {
  198.             if ( isIncludeLiterals () ) {
  199.                 java.util.Date dt = parseDateValue ( s );
  200.                 if ( dt == null ) {
  201.                     throw new IllegalAccessException ( INVALID_DATE );
  202.                 }
  203.                 SimpleDateFormat sdf = ( SimpleDateFormat ) getFormatter ( getType (), getFormattingStyle () );
  204.                 String s1 = sdf.format ( dt ) ;
  205.                 if ( debug ) {
  206.                     System.out.println ( "S1 : " + s1 );
  207.                 }
  208.                 matchedInput = getMatchedText ( s1 );
  209.             }
  210.             else {
  211.                 matchedInput = s ;
  212.             }
  213.         }
  214.         catch (Exception e ) {
  215.             if ( java.beans.Beans.isDesignTime() ) {
  216.                 //at design time if column-name or other literals are set.
  217.                 setMask ("");
  218.                 super.setText ( s );
  219.             }
  220.             else {
  221.                 throw new IllegalArgumentException ( e.getMessage() );
  222.             }
  223.         }
  224.         super.setMaskedText ( matchedInput ) ;
  225.         
  226.     }
  227.     
  228.     //over-ride
  229.     public synchronized String getUnmaskedText() {
  230.         
  231.         String retString = "";
  232.         
  233.         if ( isIncludeLiterals () ) {
  234.             retString = super.getUnmaskedText();
  235.             if ( debug ) {
  236.                 System.out.println ( "getUnmaskedText : " + retString );
  237.             }
  238.             if ( retString.length() > 0 ) {
  239.                 //user has entered something in the field
  240.                 retString = super.getText();
  241.                 //retString = retString.replace ( '_' , " ".charAt (0) );
  242.                 retString = retString.replace ( '_' , ' ' );
  243.             }
  244.         }
  245.         else {
  246.             retString = super.getUnmaskedText () ;
  247.         }
  248.         
  249.         return retString;
  250.   
  251.     }
  252.     
  253.     // PROTECTED SECTION
  254.     protected String getMatchedText ( String s ) {
  255.         
  256.         return DateMaskHelper.getMatchedText( 
  257.                                 s , 
  258.                                 getMask() , 
  259.                                 xlator.getMaskNumericCharacter(),
  260.                                 xlator.getMaskAlphaCharacter(),
  261.                                 xlator.getMaskEscapeCharacter() 
  262.                                 );
  263.     }
  264.     
  265.     /**
  266.      * Parse a string to a date using the current type.
  267.      */
  268.     protected java.util.Date parseDateValue ( String s ) {
  269.         
  270.         return parseDateValue ( s , getType() );
  271.         
  272.     }
  273.     /**
  274.      * Parse a string to a date using the given type.
  275.      */
  276.     protected java.util.Date parseDateValue ( String s , int type ) {
  277.         
  278.         return DateMaskHelper.parseDateValue ( s , type );
  279.         
  280.     }
  281.     
  282.     /**
  283.      * Get a date/time formatter for the given type and formatting style.
  284.      */
  285.     protected DateFormat getFormatter ( int type , int style  ) {
  286.         
  287.         DateMaskFormatter myFormatter = new DateMaskFormatter();
  288.         return myFormatter.getInstance ( type , style );
  289.         
  290.     }
  291.     
  292.     
  293.     
  294.     /**
  295.      * Get the date pattern string for a DateFormat.
  296.      */
  297.     protected String getFormatterPattern ( DateFormat dateFormat ) {
  298.         
  299.         return ( (SimpleDateFormat ) dateFormat ) .toPattern();
  300.         
  301.     }
  302.     
  303.     /**
  304.      * Get the localized date pattern string for a DateFormat.
  305.      */
  306.     protected String getFormatterLocalizedPattern ( DateFormat dateFormat ) {
  307.         
  308.         return ( (SimpleDateFormat ) dateFormat ) .toLocalizedPattern();
  309.         
  310.     }
  311.     
  312.     /**
  313.      * Get the mask representation of a DateFormat.
  314.      */
  315.     
  316.     protected String getMaskForDateFormat ( DateFormat dateFormat ) {
  317.         
  318.         return xlator.getMaskForDatePattern ( getFormatterPattern ( dateFormat ) );
  319.         
  320.     }
  321.     
  322.   
  323.     //private section
  324.     private boolean debug = false ;
  325.     
  326. }// end class JDateMaskedField