home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-11 | 10.0 KB | 286 lines |
- package com.symantec.itools.swing;
-
- 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.sun.java.swing.text.Document;
- import com.sun.java.swing.text.PlainDocument;
- import com.sun.java.swing.event.DocumentListener;
- import com.sun.java.swing.event.DocumentEvent;
-
- public class JCurrencyTextField extends com.sun.java.swing.JTextField {
- public JCurrencyTextField( ) { this("" , 0 ); }
- public JCurrencyTextField(int numberOfColumns) { this("" , numberOfColumns); }
- public JCurrencyTextField(String initialText) { this(initialText, 256 ); }
- public JCurrencyTextField(String initialText, int numberOfColumns) {
- this(null, initialText, numberOfColumns);
- }
-
- // All other constructors call this one.
- public JCurrencyTextField(Document doc, String initialText, int numberOfColumns) {
- super(doc, initialText, numberOfColumns);
- setHorizontalAlignment(RIGHT);
- }
-
- 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 (_focusListener == null) {
- setText(s.toString());
- return; // don't call peer if it doesn't exist yet
- }
- }
- setText(s.toString());
- if (_haveFocus) {
- setCaretPosition(pos);
- if (data.length() == 0 && !_engine.getATMmode())
- select(0, s.length());
- _isFormatted = false;
- }
- }
-
- public synchronized void format() {
- StringBuffer newData = new StringBuffer();
- _engine.postFormat(getDataText(), newData);
- setText(newData.toString());
- _isFormatted = true;
- }
-
- public synchronized String getDataText() {
- if (_dataText == null)
- _dataText = getText();
- 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) {
- _activity = true;
- String s = getText();
- int selStart = getSelectionStart();
- int selEnd = getSelectionEnd();
- StringSelection ss = new StringSelection(s.substring(selStart, selEnd));
- clipboard.setContents(ss, ss);
- StringBuffer newData = new StringBuffer(s);
- _engine.cut(newData, selStart, selEnd);
- setText(newData.toString());
- select(0,0);
- setCaretPosition(selStart);
- }
-
- public synchronized void paste(Clipboard clipboard) {
- _activity = true;
- String pasteData = "";
- try {
- pasteData = (String)clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor);
- } catch (Exception e) {}
- int selStart = getSelectionStart();
- int selEnd = getSelectionEnd();
- int pos = getCaretPosition();
- StringBuffer data = new StringBuffer(getText());
- _engine.paste(data, pasteData, pos, selStart, selEnd);
- setText(data.toString());
- select(0,0);
- setCaretPosition(pos);
- }
-
- // Override of JTextComponent
- public void cut() {
- cut(java.awt.Toolkit.getDefaultToolkit().getSystemClipboard());
- }
-
- // Override of JTextComponent
- public void paste() {
- paste(java.awt.Toolkit.getDefaultToolkit().getSystemClipboard());
- }
-
-
- /**
- * This is a standard AWT method which gets called when
- * this component is added to a container.
- *
- * @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);
- }
- 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 call removeKeyListener.
- *
- * @see #addNotify
- */
- public synchronized void removeNotify() {
- if (_focusListener != null) {
- removeFocusListener(_focusListener);
- _focusListener = null;
- }
- super.removeNotify();
- }
-
- void gotFocus(FocusEvent e) {
- _activity = false;
- _haveFocus = true;
- if (_isFormatted || getText().length() == 0)
- setFormattedText(getDataText());
- _dataText = null;
- }
-
- void lostFocus(FocusEvent e) {
- format();
- _haveFocus = false;
- if (isEditable() && _activity) {
- try {
- _docListenerDisabled = true;
- _document.remove(0, _document.getLength());
- _document.insertString(0, _dataText, null);
- } catch (com.sun.java.swing.text.BadLocationException ex) {
- badLocException();
- } finally {
- _docListenerDisabled = false;
- }
- }
- }
-
- // Override JTextComponent
- protected void processComponentKeyEvent(KeyEvent e) {
- switch(e.getID()) {
- case KeyEvent.KEY_TYPED:
- e.consume(); // prevents super component from writing
- break;
- case KeyEvent.KEY_PRESSED:
- if (_engine.isHandledKey(e) && isEditable()) {
- if (_keyPressed) // key must be auto-repeating
- processKey(e); // so process it
- else {
- e.consume();
- _keyPressed = true; // in case auto-repeat happens
- }
- } else
- super.processComponentKeyEvent(e);
- break;
- case KeyEvent.KEY_RELEASED:
- if (_engine.isHandledKey(e) && isEditable()) {
- _keyPressed = false;
- processKey(e);
- } else
- super.processComponentKeyEvent(e);
- break;
- }
- }
-
- void processKey(KeyEvent e) {
- _activity = true;
- e.consume();
- int pos = getCaretPosition();
- StringBuffer newData = new StringBuffer("");
- StringBuffer data = new StringBuffer(getText());
- int selStart = getSelectionStart();
- int selEnd = getSelectionEnd();
- if ( selEnd >= selStart ) {
- pos = selStart;
- }
- int newpos = _engine.processKey(e, pos, data.toString(), newData, selStart, selEnd);
- if (newpos >= 0) { // if good new caret position
- setText(newData.toString());
- select(0, 0); // remove text selection, if any
- setCaretPosition(newpos);
- } else if (newpos == -1)
- setCaretPosition(pos);
- }
-
- // Override
- // This gets called during superclass construction!
- public void setDocument(Document d) {
- if (_docListener == null)
- _docListener = new DocumentListener() {
- public void changedUpdate(DocumentEvent e) { docChange(e); }
- public void removeUpdate (DocumentEvent e) { docRemove(e); }
- public void insertUpdate (DocumentEvent e) { docInsert(e); }
- };
-
- if (_document != null)
- _document.removeDocumentListener(_docListener);
- _document = d;
- _document.addDocumentListener(_docListener);
- if (_myDoc == null)
- _myDoc = new PlainDocument();
- super.setDocument(_myDoc);
- }
-
- // This shouldn't get called except by this class
- protected void docChange(DocumentEvent e) {
- if (_docListenerDisabled)
- return;
- // System.out.println("Currency Doc Listener Change Called");
- }
-
- protected void docRemove(DocumentEvent e) {
- if (_docListenerDisabled)
- return;
- setText(_dataText = "");
- }
-
- // This gets called on data insertion into the field
- protected void docInsert(DocumentEvent e) {
- if (_docListenerDisabled)
- return;
- int len = e.getLength();
- String data = "";
- try {
- data = _document.getText(0, len);
- } catch(com.sun.java.swing.text.BadLocationException ex) {
- badLocException();
- }
- setFormattedText(data);
- }
-
- protected void badLocException() {
- getToolkit().beep();
- System.out.println("Bad Location Exception in JCurrencyTextField");
- }
-
- private PlainDocument _myDoc = null;
- private DocumentListener _docListener = null;
- private Document _document = null;
- private boolean _docListenerDisabled = false;
- private CurrencyEngine _engine = new CurrencyEngine();
- private FocusAdapter _focusListener = null;
- private String _dataText = null;
- private boolean _haveFocus = false;
- private boolean _isFormatted = false;
- private boolean _keyPressed = false; // true if a key is down
- private boolean _activity = false;
- }