// 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:   CurrencyEngine.java

package com.symantec.itools.javax.swing;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.PrintStream;
import java.math.BigDecimal;
import java.text.*;

public class CurrencyEngine
{

    public CurrencyEngine()
    {
        this(getDefaultDecimalSeparator(), getDefaultGroupingSeparator());
    }

    public CurrencyEngine(char c, char c1)
    {
        _commas = true;
        _currencySymbolLeads = true;
        _ATMmode = false;
        _currencySymbol = "$";
        _decimalPoint = getDefaultDecimalSeparator();
        _separator = getDefaultGroupingSeparator();
        _digitsAfterDecimal = 2;
        setDecimalPoint(c);
        setSeparator(c1);
    }

    public void setCommas(boolean flag)
    {
        _commas = flag;
    }

    public void setCurrencyLeading(boolean flag)
    {
        _currencySymbolLeads = flag;
    }

    public void setATMmode(boolean flag)
    {
        _ATMmode = flag;
    }

    public void setCurrencySymbol(String s)
    {
        _currencySymbol = s;
    }

    public void setDecimalPoint(char c)
    {
        _decimalPoint = c;
    }

    public void setSeparator(char c)
    {
        _separator = c;
    }

    public void setDigitsAfterDecimal(int i)
    {
        _digitsAfterDecimal = i;
    }

    public boolean getCommas()
    {
        return _commas;
    }

    public boolean getCurrencyLeading()
    {
        return _currencySymbolLeads;
    }

    public boolean getATMmode()
    {
        return _ATMmode;
    }

    public String getCurrencySymbol()
    {
        return _currencySymbol;
    }

    public char getDecimalPoint()
    {
        return _decimalPoint;
    }

    public char getSeparator()
    {
        return _separator;
    }

    public int getDigitsAfterDecimal()
    {
        return _digitsAfterDecimal;
    }

    public int initDisplay(String s, StringBuffer stringbuffer)
    {
        if(s.length() == 0)
        {
            normalize(stringbuffer);
        }
        else
        {
            stringbuffer.append(s);
            scale(stringbuffer);
        }
        if(_ATMmode)
            return stringbuffer.length();
        else
            return 0;
    }

    public int processKey(KeyEvent keyevent, int i, String s, StringBuffer stringbuffer, int j, int k)
    {
        char c = keyevent.getKeyChar();
        stringbuffer.append(s);
        int l = s.indexOf(_decimalPoint);
        int i1 = s.length();
        int j1 = keyevent.getKeyCode();
        if(_ATMmode)
            switch(j1)
            {
            case 37: // '%'
                return i - (i != 0 ? i != l + 1 ? 1 : 2 : i);

            case 39: // '\''
                return i + (i != i1 ? i != l - 1 ? 1 : 2 : 0);

            case 8: // '\b'
            case 127: // '\177'
                if(i == l)
                    return -1;
                if(j < k)
                {
                    clearSelectedText(j, k, stringbuffer, true);
                    return j;
                }
                deleteChar(stringbuffer, i != i1 ? i : i - 1);
                normalize(stringbuffer);
                int k1 = stringbuffer.length() - i1;
                if(i == l - 1)
                    return l + (k1 != 0 ? 0 : 1);
                else
                    return i + k1;

            }
        else
            switch(j1)
            {
            case 8: // '\b'
                if(k == j && i > 0)
                {
                    clearSelectedText(i - 1, i, stringbuffer, false);
                    return i - 1;
                }
                else
                {
                    clearSelectedText(j, k, stringbuffer, false);
                    return i;
                }

            case 127: // '\177'
                if(k == j && i < i1)
                    clearSelectedText(i, i + 1, stringbuffer, false);
                else
                    clearSelectedText(j, k, stringbuffer, false);
                return i;

            }
        if(!Character.isDigit(c))
        {
            if(c != _decimalPoint || _ATMmode)
                return -1;
            if(l != -1)
                if(i <= l)
                    return l + 1;
                else
                    return -1;
        }
        clearSelectedText(j, k, stringbuffer, true);
        if(_ATMmode)
        {
            if(j < k)
                stringbuffer.insert(j + 1, c);
            else
                stringbuffer.insert(i, c);
            normalize(stringbuffer);
            if(j < k)
                return j + 1;
            else
                return (i + stringbuffer.length()) - i1;
        }
        if(j < k)
        {
            i = j;
            stringbuffer.insert(j, c);
        }
        else
        {
            stringbuffer.insert(i, c);
        }
        return i + 1;
    }

    public boolean isHandledKey(KeyEvent keyevent)
    {
        if(!Character.isISOControl(keyevent.getKeyChar()))
            return true;
        switch(keyevent.getKeyCode())
        {
        case 8: // '\b'
        case 127: // '\177'
            return true;

        case 37: // '%'
        case 39: // '\''
            return _ATMmode && !keyevent.isShiftDown();

        }
        return false;
    }

    public String compress(String s)
    {
        StringBuffer stringbuffer = new StringBuffer(s);
        scale(stringbuffer);
        String s1 = stringbuffer.toString();
        s1 = s1.replace(getDecimalPoint(), getDefaultDecimalSeparator());
        for(int i = _digitsAfterDecimal; i >= 1; i--)
        {
            boolean flag = true;
            for(int j = s1.length() - i; j < s1.length(); j++)
            {
                if(s1.charAt(j) == '0')
                    continue;
                flag = false;
                break;
            }

            if(flag)
                return s1.substring(0, s1.length() - i - (i != _digitsAfterDecimal ? 0 : 1));
        }

        return s1;
    }

    public void postFormat(String s, StringBuffer stringbuffer)
    {
        stringbuffer.append(s);
        scale(stringbuffer);
        s = stringbuffer.toString();
        stringbuffer.setLength(0);
        if(_currencySymbolLeads)
            stringbuffer.append(_currencySymbol);
        int i = stringbuffer.length();
        stringbuffer.append(s);
        if(!_currencySymbolLeads)
            stringbuffer.append(_currencySymbol);
        int j = s.indexOf(_decimalPoint);
        if(j == -1)
            j = s.length();
        int k = 0;
        for(int l = stringbuffer.length(); k < l && !Character.isDigit(stringbuffer.charAt(k)); k++);
        for(int i1 = 0; i1 < j;)
        {
            if(_commas && i1 >= k && i1 % defDecimalFormat.getGroupingSize() == j % defDecimalFormat.getGroupingSize())
                stringbuffer.insert(i++, _separator);
            i1++;
            i++;
        }

        if(debug)
            System.out.println("postformat : " + stringbuffer);
    }

    public void cut(StringBuffer stringbuffer, int i, int j)
    {
        clearSelectedText(i, j, stringbuffer, true);
    }

    public void paste(StringBuffer stringbuffer, String s, int i, int j, int k)
    {
        clearSelectedText(j, k, stringbuffer, false);
        String s1 = stringbuffer.toString();
        int l = s1.indexOf(_decimalPoint);
        stringbuffer.setLength(0);
        StringBuffer stringbuffer1 = new StringBuffer();
        int i1 = s.length();
        for(int j1 = 0; j1 < i1; j1++)
        {
            char c = s.charAt(j1);
            if(Character.isDigit(c) || l == -1 && _decimalPoint == c)
                stringbuffer1.append(c);
        }

        stringbuffer.append(s1.substring(0, i) + stringbuffer1.toString() + s1.substring(i, s1.length()));
        if(_ATMmode)
            normalize(stringbuffer);
    }

    void clearSelectedText(int i, int j, StringBuffer stringbuffer, boolean flag)
    {
        int k = j - i;
        if(k > 0)
        {
            String s = stringbuffer.toString();
            int l = stringbuffer.length();
            stringbuffer.insert(0, s.substring(0, i) + s.substring(j));
            stringbuffer.setLength(l - k);
            if(_ATMmode && flag)
                normalize(stringbuffer);
        }
    }

    private void normalize(StringBuffer stringbuffer)
    {
        int i = stringbuffer.toString().indexOf(_decimalPoint);
        int j = stringbuffer.toString().length() - 1 - _digitsAfterDecimal;
        if(i != j || i < 1)
        {
            if(i != -1)
            {
                deleteChar(stringbuffer, i);
                j--;
            }
            while(j++ < 0) 
                stringbuffer.insert(0, '0');

            stringbuffer.insert(stringbuffer.length() - _digitsAfterDecimal, _decimalPoint);
        }
        for(; stringbuffer.charAt(0) == '0' && stringbuffer.charAt(1) != _decimalPoint; deleteChar(stringbuffer, 0));
    }

    private void scale(StringBuffer stringbuffer)
    {
        if(stringbuffer.length() == 0)
            stringbuffer.append('0');
        String s = stringbuffer.toString();
        char c = '~';
        if(_separator != getDefaultGroupingSeparator())
            s = s.replace(_separator, c);
        if(_decimalPoint != getDefaultDecimalSeparator())
            s = s.replace(_decimalPoint, getDefaultDecimalSeparator());
        if(_separator != getDefaultGroupingSeparator())
            s = s.replace(c, getDefaultGroupingSeparator());
        Number number = null;
        try
        {
            number = defDecimalFormat.parse(s);
        }
        catch(Exception exception)
        {
            throw new NumberFormatException(exception.getMessage());
        }
        s = numberToString(number);
        BigDecimal bigdecimal = new BigDecimal(s);
        bigdecimal.setScale(_digitsAfterDecimal, 4);
        s = bigdecimal.toString();
        int i = s.indexOf(46);
        if(i == -1)
        {
            s = s + '.';
            i = s.indexOf(46);
        }
        for(; s.length() - i <= _digitsAfterDecimal; s = s + String.valueOf(0));
        if(_decimalPoint != '.')
            s = s.replace('.', _decimalPoint);
        if(debug)
            System.out.println("scale : " + s);
        stringbuffer.setLength(0);
        stringbuffer.append(s);
    }

    public static String numberToString(Number number)
    {
        String s = number.toString().toUpperCase();
        int i = s.indexOf(69);
        if(i != -1)
        {
            int j = s.indexOf(46);
            String s1 = s.substring(i + 1);
            int k = (new Integer(s1)).intValue();
            StringBuffer stringbuffer = new StringBuffer();
            if(k >= 0)
            {
                stringbuffer.append(s.substring(0, j));
                String s2;
                for(s2 = s.substring(j + 1, i); k > s2.length(); s2 = s2 + String.valueOf(0));
                stringbuffer.append(s2.substring(0, k));
                if(s2.length() > k)
                {
                    stringbuffer.append('.');
                    stringbuffer.append(s2.substring(k));
                }
            }
            else
            {
                stringbuffer.append(0);
                stringbuffer.append('.');
                k = Math.abs(k);
                StringBuffer stringbuffer1;
                for(stringbuffer1 = new StringBuffer(); k - 1 > stringbuffer1.length(); stringbuffer1.append(0));
                stringbuffer.append(stringbuffer1);
                stringbuffer.append(s.substring(0, j));
                String s3 = s.substring(j + 1, i);
                stringbuffer.append(s3);
            }
            s = stringbuffer.toString();
        }
        return s;
    }

    void deleteChar(StringBuffer stringbuffer, int i)
    {
        int j = stringbuffer.length();
        if(i >= 0 && i < j)
        {
            String s = stringbuffer.toString();
            stringbuffer.insert(0, s.substring(0, i) + (i >= j - 1 ? "" : s.substring(i + 1)));
            stringbuffer.setLength(j - 1);
        }
    }

    public static char getDefaultDecimalSeparator()
    {
        return defDecimalFormat.getDecimalFormatSymbols().getDecimalSeparator();
    }

    public static char getDefaultGroupingSeparator()
    {
        return defDecimalFormat.getDecimalFormatSymbols().getGroupingSeparator();
    }

    boolean _commas;
    boolean _currencySymbolLeads;
    boolean _ATMmode;
    String _currencySymbol;
    char _decimalPoint;
    char _separator;
    int _digitsAfterDecimal;
    private static DecimalFormat defDecimalFormat = (DecimalFormat)NumberFormat.getNumberInstance();
    private static boolean debug;

}
