home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
CurrencyTextField.java
< prev
next >
Wrap
Text File
|
1998-09-28
|
9KB
|
252 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.CurrencyEngine;
public class CurrencyTextField extends java.awt.TextField {
public CurrencyTextField( ) { this("" , 0 ); }
public CurrencyTextField(int numberOfColumns) { this("" , numberOfColumns); }
public CurrencyTextField(String initialText) { this(initialText, 256 ); }
// All other constructors call this one.
public CurrencyTextField(String initialText, int numberOfColumns) {
super(initialText, numberOfColumns);
}
public synchronized void setFormattedText(String data) {
StringBuffer s = new StringBuffer();
_dataText = data;
int pos = _engine.initDisplay(data, s);
if (!_haveFocus) {
StringBuffer newData = new StringBuffer();
_engine.postFormat(s.toString(), newData);
s = newData;
_isFormatted = true;
if (_keyListener == null) {
setText(s.toString());
return; // don't call peer if it doesn't exist yet
}
}
rightJustify(s);
setText(s.toString());
setCaretPosition(s.length());
if (isEditable() && _haveFocus) {
setCaretPosition(pos + _padCount);
if (data.length() == 0 && !_engine.getATMmode())
select(_padCount, s.length());
_isFormatted = false;
}
}
public synchronized void format() {
StringBuffer newData = new StringBuffer();
_engine.postFormat(getDataText(), newData);
rightJustify(newData);
setText(newData.toString());
setCaretPosition(newData.length());
_isFormatted = true;
}
public synchronized String getDataText() {
if (_dataText == null)
_dataText = getText().trim();
return _dataText;
}
// Get/Set properties: all are properties of the currency engine
public boolean isCurrencyLeading ( ) { return _engine.getCurrencyLeading ( ); }
public boolean isSeparatorEnabled ( ) { return _engine.getCommas ( ); }
public boolean getATMmode ( ) { return _engine.getATMmode ( ); }
public String getCurrencySymbol ( ) { return _engine.getCurrencySymbol ( ); }
public char getDecimalPoint ( ) { return _engine.getDecimalPoint ( ); }
public char getSeparator ( ) { return _engine.getSeparator ( ); }
public int getDigitsAfterDecimal( ) { return _engine.getDigitsAfterDecimal( ); }
public void setCurrencyLeading (boolean b) { _engine.setCurrencyLeading (b); }
public void setSeparatorEnabled (boolean b) { _engine.setCommas (b); }
public void setATMmode (boolean b) { _engine.setATMmode (b); }
public void setCurrencySymbol (String s) { _engine.setCurrencySymbol (s); }
public void setDecimalPoint (char c) { _engine.setDecimalPoint (c); }
public void setSeparator (char c) { _engine.setSeparator (c); }
public void setDigitsAfterDecimal(int n) { _engine.setDigitsAfterDecimal(n); }
public synchronized void cut(Clipboard clipboard) {
if (!isEditable())
return;
String s = getText().trim();
int selStart = getSelectionStart() - _padCount;
int selEnd = getSelectionEnd() - _padCount;
StringSelection ss = new StringSelection(s.substring(selStart, selEnd));
clipboard.setContents(ss, ss);
StringBuffer newData = new StringBuffer(s);
_engine.cut(newData, selStart, selEnd);
rightJustify(newData);
setText(newData.toString());
select(0,0);
setCaretPosition(getText().length());
setCaretPosition(selStart + _padCount);
}
public synchronized void paste(Clipboard clipboard) {
if (!isEditable())
return;
String pasteData = "";
try {
pasteData = (String)clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor);
} catch (Exception e) {}
int selStart = getSelectionStart() - _padCount;
int selEnd = getSelectionEnd() - _padCount;
int pos = getCaretPosition() - _padCount;
StringBuffer data = new StringBuffer(getText().trim());
_engine.paste(data, pasteData, pos, selStart, selEnd);
rightJustify(data);
setText(data.toString());
select(0,0);
setCaretPosition(getText().length());
setCaretPosition(pos + _padCount);
}
/**
* This 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 call addKeyListener.
*
* @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 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 call removeKeyListener.
*
* @see #addNotify
*/
public synchronized void removeNotify() {
if (_focusListener != null) {
if (isEditable())
removeKeyListener(_keyListener);
removeFocusListener(_focusListener);
_keyListener = null;
_focusListener = null;
}
super.removeNotify();
}
void gotFocus(FocusEvent e) {
_haveFocus = true;
if (_isFormatted || getText().length() == 0)
setFormattedText(getDataText());
_dataText = null;
}
void lostFocus(FocusEvent e) {
format();
_haveFocus = false;
}
void keyPress(KeyEvent e) {
int selStart = getSelectionStart() - _padCount;
int selEnd = getSelectionEnd() - _padCount;
int pos = getCaretPosition() - _padCount;
if (pos < 0) {
setCaretPosition(_padCount);
e.consume();
if (selEnd > selStart)
select(_padCount, _padCount + Math.max(selEnd, 0));
return;
} else if (pos == 0) {
switch (e.getKeyCode()) {
case e.VK_LEFT:
if (selStart == pos && selStart < selEnd)
return; // leading substring is selected, so let left work
case e.VK_HOME:
e.consume();
}
}
if (_engine.isHandledKey(e)) {
e.consume(); // prevent echo of keys we'll handle
if (_keyPressed)
processKey(e);
else
_keyPressed = true;
}
}
protected void keyType(KeyEvent e) {
}
protected void keyRelease(KeyEvent e) {
if (e.getKeyCode() == e.VK_HOME) {
setCaretPosition(getText().length());
setCaretPosition(_padCount);
_keyPressed = false;
return;
}
if (!_engine.isHandledKey(e))
return;
_keyPressed = false;
processKey(e);
}
void processKey(KeyEvent e) {
int selStart = getSelectionStart() - _padCount;
int selEnd = getSelectionEnd() - _padCount;
int pos = getCaretPosition() - _padCount;
if (selStart < 0) // selection starts in ozone?
selStart = 0;
if (selEnd < 0) // entire selection was in ozone?
selEnd = 0;
if (pos < 0) { // allow no operations in the ozone
if (selEnd <= selStart)
return;
pos = selStart; // selected text started in ozone but includes data
}
StringBuffer newData = new StringBuffer("");
StringBuffer data = new StringBuffer(getText().trim());
int newpos = _engine.processKey(e, pos, data.toString(), newData, selStart, selEnd);
if (newpos >= 0) { // if good new caret position
rightJustify(newData);
setText(newData.toString());
select(0, 0); // remove text selection, if any
setCaretPosition(newData.length());
setCaretPosition(newpos + _padCount);
} else if (newpos == -1)
setCaretPosition(pos + _padCount);
}
void rightJustify(StringBuffer s) {
for (int i = 1; i <= _padCount; i++)
s.insert(0, ' ');
}
private CurrencyEngine _engine = new CurrencyEngine();
private KeyAdapter _keyListener = null;
private FocusAdapter _focusListener = null;
private final int _padCount = 128; // padding spaces in currency field
private String _dataText = null;
private boolean _haveFocus = false;
private boolean _isFormatted = false;
private boolean _keyPressed = false;
}