home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
JDateMaskedField.java
< prev
next >
Wrap
Text File
|
1998-12-09
|
10KB
|
326 lines
/*
* @(#JDateMaskedField.java
*
* Copyright (c) 1998 Symantec Corporation. All Rights Reserved.
*
*/
package com.symantec.itools.swing;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.Serializable;
/**
* JDateMaskedField is a masked text component that automatically assumes a mask
* based on the current locale and the type and formatting style specified by
* the user.
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*
* @author Vasudev J. Rao
* @version 1.0
* @see com.symantec.itools.swing.JMaskedTextField
*/
public class JDateMaskedField
extends com.symantec.itools.swing.JMaskedTextField
implements Serializable, DateMaskedFieldConstants
{
protected int type = DATE_TYPE ;
protected int formattingStyle = DateFormat.DEFAULT ;
protected boolean includeLiterals = true;
protected DatePatternToMaskXLator xlator = new DatePatternToMaskXLator();
/**
* Default contsructor. Constructs a date masked field of LONG style.
*/
public JDateMaskedField ( ) {
this ( DATE_TYPE , DateFormat.LONG );
}
/**
* Constructs a masked field with the specified date type and style.
* Type can be one of DATE_TYPE, TIME_TYPE or TIMESTAMP_TYPE. Style
* can be one of DateFormat.DEFAULT, FULL, LONG, MEDIUM or SHORT.
*/
public JDateMaskedField ( int type , int style ) {
super ( );
setType ( type );
setFormattingStyle ( style );
}
/**
* Set the style of the date, time or timestamp. Should be one of DateFormat.DEFAULT,
* FULL, LONG, MEDIUM or SHORT. If the type is invalid, DEFAULT is used.
*/
public void setFormattingStyle ( int newStyle) {
int oldStyle = this.formattingStyle;
if ( newStyle == DateFormat.DEFAULT ||
newStyle == DateFormat.FULL ||
newStyle == DateFormat.LONG ||
newStyle == DateFormat.MEDIUM ||
newStyle == DateFormat.SHORT ) {
formattingStyle = newStyle ;
String tempMask = getMaskForDateFormat ( getFormatter ( getType() , getFormattingStyle() ) );
setMask ( tempMask );
firePropertyChange("formattingStyle", oldStyle , newStyle );
}
else{
throw new IllegalArgumentException ( INVALID_STYLE );
}
}
/**
* Returns the style of date, time or timestamp. Would be one of DateFormat.DEFAULT,
* FULL, LONG, MEDIUM or SHORT.
*/
public int getFormattingStyle ( ){
return this.formattingStyle ;
}
/**
* Set the type of date. Should be one of DATE_TYPE, TIME_TYPE or TIMESTAMP_TYPE.
* If the type supplied is invalid DATE_TYPE is used.
*/
public void setType ( int newType){
int oldType = type;
if ( newType == DATE_TYPE ||
newType == TIME_TYPE ||
newType == TIMESTAMP_TYPE ) {
type = newType ;
String tempMask = getMaskForDateFormat ( getFormatter ( getType() , getFormattingStyle() ) );
setMask ( tempMask );
firePropertyChange("type", oldType , newType );
}
else{
throw new IllegalArgumentException ( INVALID_TYPE );
}
}
/**
* Returns the type of date. Would be one of DATE_TYPE, TIME_TYPE or TIMESTAMP_TYPE.
*/
public int getType ( ) {
return this.type;
}
/**
* Returns the current mask.
*/
public String getDateMask ( ) {
return super.getMask() ;
}
/**
* Get the date/time formatter pattern for the current type and formatting style.
*/
public String getPattern () {
return getFormatterPattern ( getFormatter ( getType() , getFormattingStyle() ) );
}
/**
* If set to true, the method setMaskedText would expect
* the dates to have separators and all other literals. Also
* the method getUnmaskedText would return all the separators
* and literals. If false, the behavior would default to the behavior of
* JMaskedTextField.
* <p>
* For eg., in case of SHORT date,
* when set to true, setMaskedText expects dates "10-Jun-1998" and when set
* to false it expects 10Jun1998. Similarly, getUnmaskedText would return
* "10-Jun-1998" or "10Jun1998" based on ths setting.
*/
public void setIncludeLiterals ( boolean b ) {
includeLiterals = b;
}
/**
* Returns whether or not literals would be included in the masked and Unmasked
* texts.
*/
public boolean isIncludeLiterals ( ) {
return includeLiterals ;
}
//over-ride
public void setMaskedText ( String s ) {
if ( s == null || s.length() == 0 ) {
super.setMaskedText ( "" );
return;
}
String matchedInput = "";
try {
if ( isIncludeLiterals () ) {
java.util.Date dt = parseDateValue ( s );
if ( dt == null ) {
throw new IllegalAccessException ( INVALID_DATE );
}
SimpleDateFormat sdf = ( SimpleDateFormat ) getFormatter ( getType (), getFormattingStyle () );
String s1 = sdf.format ( dt ) ;
if ( debug ) {
System.out.println ( "S1 : " + s1 );
}
matchedInput = getMatchedText ( s1 );
}
else {
matchedInput = s ;
}
}
catch (Exception e ) {
if ( java.beans.Beans.isDesignTime() ) {
//at design time if column-name or other literals are set.
setMask ("");
super.setText ( s );
}
else {
throw new IllegalArgumentException ( e.getMessage() );
}
}
super.setMaskedText ( matchedInput ) ;
}
//over-ride
public synchronized String getUnmaskedText() {
String retString = "";
if ( isIncludeLiterals () ) {
retString = super.getUnmaskedText();
if ( debug ) {
System.out.println ( "getUnmaskedText : " + retString );
}
if ( retString.length() > 0 ) {
//user has entered something in the field
retString = super.getText();
//retString = retString.replace ( '_' , " ".charAt (0) );
retString = retString.replace ( '_' , ' ' );
}
}
else {
retString = super.getUnmaskedText () ;
}
return retString;
}
// PROTECTED SECTION
protected String getMatchedText ( String s ) {
return DateMaskHelper.getMatchedText(
s ,
getMask() ,
xlator.getMaskNumericCharacter(),
xlator.getMaskAlphaCharacter(),
xlator.getMaskEscapeCharacter()
);
}
/**
* Parse a string to a date using the current type.
*/
protected java.util.Date parseDateValue ( String s ) {
return parseDateValue ( s , getType() );
}
/**
* Parse a string to a date using the given type.
*/
protected java.util.Date parseDateValue ( String s , int type ) {
return DateMaskHelper.parseDateValue ( s , type );
}
/**
* Get a date/time formatter for the given type and formatting style.
*/
protected DateFormat getFormatter ( int type , int style ) {
DateMaskFormatter myFormatter = new DateMaskFormatter();
return myFormatter.getInstance ( type , style );
}
/**
* Get the date pattern string for a DateFormat.
*/
protected String getFormatterPattern ( DateFormat dateFormat ) {
return ( (SimpleDateFormat ) dateFormat ) .toPattern();
}
/**
* Get the localized date pattern string for a DateFormat.
*/
protected String getFormatterLocalizedPattern ( DateFormat dateFormat ) {
return ( (SimpleDateFormat ) dateFormat ) .toLocalizedPattern();
}
/**
* Get the mask representation of a DateFormat.
*/
protected String getMaskForDateFormat ( DateFormat dateFormat ) {
return xlator.getMaskForDatePattern ( getFormatterPattern ( dateFormat ) );
}
//private section
private boolean debug = false ;
}// end class JDateMaskedField