// Decompiled by Jad v1.5.7. Copyright 1997-99 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   JCurrencyTextField.java

package com.symantec.itools.javax.swing;

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.PrintStream;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.*;

// Referenced classes of package com.symantec.itools.javax.swing:
//            CurrencyEngine

public class JCurrencyTextField extends JTextField
{

    public JCurrencyTextField()
    {
        this("", 0);
    }

    public JCurrencyTextField(int i)
    {
        this("", i);
    }

    public JCurrencyTextField(String s)
    {
        this(s, 256);
    }

    public JCurrencyTextField(String s, int i)
    {
        this(null, s, i);
    }

    public JCurrencyTextField(Document document, String s, int i)
    {
        super(document, s, i);
        _docListenerDisabled = false;
        _engine = new CurrencyEngine();
        _haveFocus = false;
        _isFormatted = false;
        _keyPressed = false;
        _activity = false;
        _listeningToFocus = true;
        setHorizontalAlignment(4);
    }

    public synchronized void setFormattedText(String s)
    {
        StringBuffer stringbuffer = new StringBuffer();
        _dataText = s;
        int i = _engine.initDisplay(s, stringbuffer);
        if(!_haveFocus)
        {
            StringBuffer stringbuffer1 = new StringBuffer();
            _engine.postFormat(stringbuffer.toString(), stringbuffer1);
            stringbuffer = stringbuffer1;
            _isFormatted = true;
            if(_focusListener == null)
            {
                setText(stringbuffer.toString());
                return;
            }
        }
        setText(stringbuffer.toString());
        if(_haveFocus)
        {
            setCaretPosition(i);
            if(s.length() == 0 && !_engine.getATMmode())
                select(0, stringbuffer.length());
            _isFormatted = false;
        }
    }

    public synchronized void format()
    {
        StringBuffer stringbuffer = new StringBuffer();
        _engine.postFormat(getDataText(), stringbuffer);
        setText(stringbuffer.toString());
        _isFormatted = true;
    }

    public synchronized String getDataText()
    {
        if(_dataText == null)
            _dataText = getText();
        return _dataText;
    }

    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 flag)
    {
        _engine.setCurrencyLeading(flag);
    }

    public void setSeparatorEnabled(boolean flag)
    {
        _engine.setCommas(flag);
    }

    public void setATMmode(boolean flag)
    {
        _engine.setATMmode(flag);
    }

    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 i)
    {
        _engine.setDigitsAfterDecimal(i);
    }

    public synchronized void cut(Clipboard clipboard)
    {
        _activity = true;
        String s = getText();
        int i = getSelectionStart();
        int j = getSelectionEnd();
        StringSelection stringselection = new StringSelection(s.substring(i, j));
        clipboard.setContents(stringselection, stringselection);
        StringBuffer stringbuffer = new StringBuffer(s);
        _engine.cut(stringbuffer, i, j);
        setText(stringbuffer.toString());
        select(0, 0);
        setCaretPosition(i);
    }

    public synchronized void paste(Clipboard clipboard)
    {
        _activity = true;
        String s = "";
        try
        {
            s = (String)clipboard.getContents(this).getTransferData(DataFlavor.stringFlavor);
        }
        catch(Exception _ex) { }
        int i = getSelectionStart();
        int j = getSelectionEnd();
        int k = getCaretPosition();
        StringBuffer stringbuffer = new StringBuffer(getText());
        _engine.paste(stringbuffer, s, k, i, j);
        setText(stringbuffer.toString());
        select(0, 0);
        setCaretPosition(k);
    }

    public void cut()
    {
        cut(Toolkit.getDefaultToolkit().getSystemClipboard());
    }

    public void paste()
    {
        paste(Toolkit.getDefaultToolkit().getSystemClipboard());
    }

    public synchronized void addNotify()
    {
        if(_focusListener == null)
        {
            _focusListener = new FocusAdapter() {

                public void focusGained(FocusEvent focusevent)
                {
                    gotFocus(focusevent);
                }

                public void focusLost(FocusEvent focusevent)
                {
                    lostFocus(focusevent);
                }

            };
            addFocusListener(_focusListener);
        }
        super.addNotify();
    }

    public synchronized void removeNotify()
    {
        if(_focusListener != null)
        {
            removeFocusListener(_focusListener);
            _focusListener = null;
        }
        super.removeNotify();
    }

    public String getValue()
    {
        format();
        return _engine.compress(_dataText);
    }

    public void editingStarted()
    {
        _activity = false;
        _haveFocus = true;
        if(_isFormatted || getText().length() == 0)
            setFormattedText(getDataText());
        _dataText = null;
    }

    void gotFocus(FocusEvent focusevent)
    {
        if(isListeningToFocus())
            editingStarted();
    }

    public void editingStopped()
    {
        if(_haveFocus)
        {
            format();
            _haveFocus = false;
            if(isEditable() && _activity)
            {
                try
                {
                    _docListenerDisabled = true;
                    _document.remove(0, _document.getLength());
                    _document.insertString(0, _engine.compress(_dataText), null);
                }
                catch(BadLocationException _ex)
                {
                    badLocException();
                }
                finally
                {
                    _docListenerDisabled = false;
                }
                return;
            }
        }
    }

    public synchronized void lostFocus(FocusEvent focusevent)
    {
        if(_haveFocus && isListeningToFocus())
        {
            editingStopped();
            processFocusEvent(focusevent);
        }
    }

    protected void processComponentKeyEvent(KeyEvent keyevent)
    {
        switch(keyevent.getID())
        {
        case 400: 
            keyevent.consume();
            return;

        case 401: 
            if(_engine.isHandledKey(keyevent) && isEditable())
            {
                if(_keyPressed)
                {
                    processKey(keyevent);
                    return;
                }
                else
                {
                    keyevent.consume();
                    _keyPressed = true;
                    return;
                }
            }
            else
            {
                super.processComponentKeyEvent(keyevent);
                return;
            }

        case 402: 
            if(_engine.isHandledKey(keyevent) && isEditable())
            {
                _keyPressed = false;
                processKey(keyevent);
                return;
            }
            else
            {
                super.processComponentKeyEvent(keyevent);
                return;
            }

        }
    }

    void processKey(KeyEvent keyevent)
    {
        _activity = true;
        keyevent.consume();
        int i = getCaretPosition();
        StringBuffer stringbuffer = new StringBuffer("");
        StringBuffer stringbuffer1 = new StringBuffer(getText());
        int j = getSelectionStart();
        int k = getSelectionEnd();
        if(k >= j)
            i = j;
        int l = _engine.processKey(keyevent, i, stringbuffer1.toString(), stringbuffer, j, k);
        if(l >= 0)
        {
            setText(stringbuffer.toString());
            select(0, 0);
            setCaretPosition(l);
            return;
        }
        if(l == -1)
            setCaretPosition(i);
    }

    public void setDocument(Document document)
    {
        if(_docListener == null)
            _docListener = new DocumentListener() {

                public void changedUpdate(DocumentEvent documentevent)
                {
                    docChange(documentevent);
                }

                public void removeUpdate(DocumentEvent documentevent)
                {
                    docRemove(documentevent);
                }

                public void insertUpdate(DocumentEvent documentevent)
                {
                    docInsert(documentevent);
                }

            };
        if(_document != null)
            _document.removeDocumentListener(_docListener);
        _document = document;
        _document.addDocumentListener(_docListener);
        if(_myDoc == null)
            _myDoc = new PlainDocument();
        super.setDocument(_myDoc);
    }

    protected void docChange(DocumentEvent documentevent)
    {
        if(_docListenerDisabled)
            return;
        else
            return;
    }

    protected void docRemove(DocumentEvent documentevent)
    {
        if(_docListenerDisabled)
        {
            return;
        }
        else
        {
            setText(_dataText = "");
            return;
        }
    }

    protected void docInsert(DocumentEvent documentevent)
    {
        if(_docListenerDisabled)
            return;
        int i = documentevent.getLength();
        String s = "";
        try
        {
            s = _document.getText(0, i);
        }
        catch(BadLocationException _ex)
        {
            badLocException();
        }
        setFormattedText(s);
    }

    protected void badLocException()
    {
        getToolkit().beep();
        System.out.println("Bad Location Exception in JCurrencyTextField");
    }

    public boolean isListeningToFocus()
    {
        return _listeningToFocus;
    }

    public void setListeningToFocus(boolean flag)
    {
        _listeningToFocus = flag;
    }

    private PlainDocument _myDoc;
    private DocumentListener _docListener;
    private Document _document;
    private boolean _docListenerDisabled;
    private CurrencyEngine _engine;
    private FocusAdapter _focusListener;
    private String _dataText;
    private boolean _haveFocus;
    private boolean _isFormatted;
    private boolean _keyPressed;
    private boolean _activity;
    private boolean _listeningToFocus;
}
