home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
MaskedTextField.java
< prev
next >
Wrap
Text File
|
1998-12-11
|
10KB
|
253 lines
package com.symantec.itools.awt;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusAdapter;
import java.awt.datatransfer.*;
import com.symantec.itools.swing.MaskEngine;
public class MaskedTextField extends java.awt.TextField {
/* These constants are used to specify the data type information needed
to specialize input time behavior based on data type.
For now, the only such difference is between text and numeric:
In numeric fields, an empty field is initialzed to zero, and input of
a decimal point when the mask contains one and the cartet is to its left
causes the caret to move to the right of the decimal point position.
*/
public static final int texttype = MaskEngine.texttype;
public static final int numbtype = MaskEngine.numbtype;
public static final int datetype = MaskEngine.datetype;
public static final int timetype = MaskEngine.timetype;
// ctors
public MaskedTextField ( ) { this("" , 0 ); }
public MaskedTextField (int numberOfColumns) { this("" , numberOfColumns); }
public MaskedTextField (String initialText ) { this(initialText, 256 ); }
// All other constructors call this one
public MaskedTextField(String initialText, int numberOfColumns) {
super(initialText, numberOfColumns);
}
//over-ride
public synchronized void setText ( String t ) {
if ( t != null && t.length() > 0 ) {
super.setText ( getMatchedText ( t ) ) ;
}
else {
super.setText( t );
}
}
/**
Initialize the field
*/
public synchronized void setMaskedText(String t) {
StringBuffer newData = new StringBuffer();
_firstInputPos = _maskEngine.initDisplay(t, newData);
super.setText(newData.toString());
if (_haveFocus && isEditable())
select(_firstInputPos, _firstInputPos + 1);
_dataComplete = false;
}
// Returns current text with mask characters removed
public synchronized String getUnmaskedText() {
StringBuffer newData = new StringBuffer();
_dataComplete = _maskEngine.stripMask(getText(), newData);
return newData.toString();
}
public boolean isDataComplete() {
if (!_dataComplete) {
if (!_activity)
return true;
_dataComplete = _maskEngine.stripMask(getText(), new StringBuffer());
}
return _dataComplete;
}
protected String getMatchedText ( String t ) {
return _maskEngine.getMatchedText ( t );
}
/**
* This is a standard AWT method which gets called when
* this component is added to a container. Typically, it is used to
* create this component's peer.
* It has been overridden to add listener(s).
*
* @see #removeNotify
*/
public synchronized void addNotify() {
if (_focusListener == null) {
_focusListener = new FocusAdapter() {
public void focusGained(FocusEvent e) { gotFocus (e); }
public void focusLost (FocusEvent e) { lostFocus(e); }
};
addFocusListener(_focusListener);
if (isEditable()) {
_keyListener = new KeyAdapter() {
public void keyPressed (KeyEvent e) { keyPress (e); }
public void keyTyped (KeyEvent e) { keyType (e); }
public void keyReleased(KeyEvent e) { keyRelease(e); }
};
addKeyListener(_keyListener);
}
}
super.addNotify();
}
/**
* This method gets called when this component is removed from a
* container. Typically, it is used to destroy the peers of this
* component and all its subcomponents.
* It has been overridden here to remove listener(s).
*
* @see #addNotify
*/
public synchronized void removeNotify() {
if (_focusListener != null) {
removeFocusListener(_focusListener);
_focusListener = null;
if (_keyListener != null)
removeKeyListener(_keyListener);
_keyListener = null;
}
super.removeNotify();
}
protected void gotFocus(FocusEvent e) {
_haveFocus = true;
_activity = false;
if (getText().length() == 0) {
setMaskedText(""); // show the mask literals if field is empty
} else {
if (isEditable() && _lastContents.compareTo(getText()) != 0)
select(_firstInputPos, _firstInputPos + 1);
_dataComplete = false;
}
}
protected void lostFocus(FocusEvent e) {
_haveFocus = false;
_lastContents = getText();
}
protected void keyPress(KeyEvent e) {
_activity = true;
if (_maskEngine.isHandledKey(e) && isEditable()) {
e.consume();
if (_keyPressed) { // key must be auto-repeating
if (Character.isISOControl(e.getKeyChar()))
processKey(e);
} else
_keyPressed = true;
}
}
protected void keyType(KeyEvent e) {
_activity = true;
if (_maskEngine.isHandledKey(e) && isEditable())
processKey(e);
}
protected void keyRelease(KeyEvent e) {
_keyPressed = false;
_activity = true;
if (_maskEngine.isHandledKey(e) && isEditable()) {
if (Character.isISOControl(e.getKeyChar()))
processKey(e);
}
}
protected void processKey(KeyEvent e) {
e.consume();
StringBuffer newData = new StringBuffer("");
int pos = getCaretPosition();
String data = getText();
int newpos = _maskEngine.processKey(e, pos, data, newData,
getSelectionStart(), getSelectionEnd());
if (newpos == -2) // quit if filter mismatch
return;
super.setText(newData.toString());
if (newpos >= 0) { // if good new caret position
select(newpos, newpos + 1); // select this position
} else if (newpos == -1) {
java.awt.Toolkit.getDefaultToolkit().beep();
select(pos, pos);
} else { // cursor just moved out of range
select(0,0); // turn off selection
setCaretPosition(newpos + 1000); // move one past last filter
}
}
public synchronized void cut(Clipboard clipboard) {
if (!isEditable())
return;
_activity = true;
StringBuffer newData = new StringBuffer();
int selStart = getSelectionStart();
String clipboardData = _maskEngine.cut(getText(), selStart,
getSelectionEnd(),
newData);
StringSelection ss = new StringSelection(clipboardData);
clipboard.setContents(ss, ss);
super.setText(newData.toString());
setCaretPosition(selStart);
}
public synchronized boolean paste(Clipboard clipboard) {
if (!isEditable())
return true;
_activity = true;
StringBuffer newData = new StringBuffer();
String data = "";
try {
data = (String)clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) { return false; }
int newpos = _maskEngine.paste(getText(), data, getCaretPosition(), newData,
getSelectionStart(), getSelectionEnd());
if (newpos < 0) // beep if paste failed
getToolkit().beep();
if (newpos == -2) // quit if filter mismatch
return false;
super.setText(newData.toString());
if (newpos >= 0) { // if good new caret position
select(newpos, newpos + 1); // select this position
} else {
select(0, 0); // turn off selection
if (newpos != -1) // cursor just moved out of range
setCaretPosition(newpos + 1000); // move one past last filter
}
return true;
}
// Property accessors
public void setMask (String mask) {
_maskEngine.setMask (mask);
setText ( getText() );
}
public String getMask ( ) { return _maskEngine.getMask ( ); }
public void setDatatype(int type) { _maskEngine.setDatatype(type); }
public int getDatatype( ) { return _maskEngine.getDatatype( ); }
// Variables
private FocusAdapter _focusListener = null;
private MaskEngine _maskEngine = new MaskEngine();
private KeyAdapter _keyListener = null;
private boolean _dataComplete = false; // true iff no mandatory filters are empty
private boolean _haveFocus = false; // true if this component has input focus
private boolean _activity = false;
private boolean _keyPressed = false;
private String _lastContents = ""; // contents on last loss of focus
private int _firstInputPos = 0; // position of first filter
}