net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFJNumberEditor.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFJNumberEditor.java

Source

// Description: Java 7 Swing Number Text Display Widget.

/*
 *   CFLib
 *
 *   Copyright (c) 2014 Mark Sobkow
 *   
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *   
 *          http://www.apache.org/licenses/LICENSE-2.0
 *   
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 *   
 * ***********************************************************************
 *
 *   Code manufactured by MSS Code Factory
 *
 *   $Revision: 1408 $
 */

package net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing;

import java.math.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.*;

import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.*;
import net.sourceforge.msscodefactory.cflib.v1_11.CFLib.Swing.CFJInt16Editor.EditorFocusListener;

import org.apache.commons.codec.binary.Base64;

/**
 *   CFJEditor Swing Number Text Field Display Widget.
 */
public class CFJNumberEditor extends CFJFormattedTextField {
    final static String S_Hashes = "####################################################################################################";
    final static String S_Zeroes = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
    final static String S_Nines = "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";

    protected int digits;
    protected int precis;
    protected BigDecimal minValue = null;
    protected BigDecimal maxValue = null;

    protected class EditorFocusListener implements FocusListener {
        public EditorFocusListener() {
        }

        public void focusGained(FocusEvent e) {
        }

        public void focusLost(FocusEvent e) {
            isEditValid();
        }
    }

    public static Format getNumberFormat(int argDigits, int argPrecis) {
        final String S_ProcName = "getNumberFormat";
        if (argDigits < 1) {
            throw CFLib.getDefaultExceptionFactory().newArgumentUnderflowException(CFJNumberEditor.class,
                    S_ProcName, 1, "argDigits", argDigits, 1);
        } else if (argDigits > S_Hashes.length()) {
            throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(CFJNumberEditor.class, S_ProcName,
                    1, "argDigits", argDigits, S_Hashes.length());
        }

        String usePrefixHash = S_Hashes.substring(0, argDigits - 1);

        if (argPrecis < 0) {
            throw CFLib.getDefaultExceptionFactory().newArgumentUnderflowException(CFJNumberEditor.class,
                    S_ProcName, 2, "argPrecis", argPrecis, 0);
        } else if (argPrecis > S_Zeroes.length()) {
            throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(CFJNumberEditor.class, S_ProcName,
                    2, "argPrecis", argPrecis, S_Zeroes.length());
        }

        String useSuffix;
        if (argPrecis > 0) {
            useSuffix = "." + S_Zeroes.substring(0, argPrecis);
        } else {
            useSuffix = "";
        }

        Format numberFormat = new DecimalFormat(
                usePrefixHash + "0" + useSuffix + ";-" + usePrefixHash + "0" + useSuffix);

        return (numberFormat);
    }

    public CFJNumberEditor(int argDigits, int argPrecis) {
        super(getNumberFormat(argDigits, argPrecis));
        setDigits(argDigits);
        setPrecision(argPrecis);
        setHorizontalAlignment(JTextField.RIGHT);
        Dimension min = new Dimension((argDigits + argPrecis + 2) * getColumnWidth(), 25);
        setMinimumSize(min);
        setMaximumSize(min);
        addFocusListener(new EditorFocusListener());
    }

    public int getDigits() {
        return (digits);
    }

    public void setDigits(int argDigits) {
        final String S_ProcName = "setDigits";
        if (argDigits < 1) {
            throw CFLib.getDefaultExceptionFactory().newArgumentUnderflowException(getClass(), S_ProcName, 1,
                    "argDigits", argDigits, 1);
        } else if (argDigits > S_Hashes.length()) {
            throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName, 1,
                    "argDigits", argDigits, S_Hashes.length());
        }
        digits = argDigits;
        minValue = null;
        maxValue = null;
    }

    public int getPrecision() {
        return (precis);
    }

    public void setPrecision(int argPrecis) {
        final String S_ProcName = "setPrecision";
        if (argPrecis < 0) {
            throw CFLib.getDefaultExceptionFactory().newArgumentUnderflowException(getClass(), S_ProcName, 2,
                    "argPrecis", argPrecis, 0);
        } else if (argPrecis > S_Zeroes.length()) {
            throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName, 2,
                    "argPrecis", argPrecis, S_Zeroes.length());
        }
        precis = argPrecis;
        minValue = null;
        maxValue = null;
    }

    public BigDecimal getAbsoluteMinValue() {
        String strval;
        if (precis <= 0) {
            strval = "-" + S_Nines.substring(0, digits);
        } else {
            strval = "-" + S_Nines.substring(0, digits) + "." + S_Nines.substring(0, precis);
        }
        BigDecimal absoluteMinValue = new BigDecimal(strval);
        return (absoluteMinValue);
    }

    public BigDecimal getAbsoluteMaxValue() {
        String strval;
        if (precis <= 0) {
            strval = S_Nines.substring(0, digits);
        } else {
            strval = S_Nines.substring(0, digits) + "." + S_Nines.substring(0, precis);
        }
        BigDecimal absoluteMaxValue = new BigDecimal(strval);
        return (absoluteMaxValue);
    }

    public BigDecimal getMinValue() {
        if (minValue == null) {
            minValue = getAbsoluteMinValue();
        }
        return (minValue);
    }

    public void setMinValue(BigDecimal value) {
        final String S_ProcName = "setMinValue";
        if (value == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1, "value");
        }
        BigDecimal absoluteMinValue = getAbsoluteMinValue();
        if (value.compareTo(absoluteMinValue) < 0) {
            throw CFLib.getDefaultExceptionFactory().newArgumentUnderflowException(getClass(), S_ProcName, 1,
                    "value", value, absoluteMinValue);
        }
        minValue = value;
    }

    public BigDecimal getMaxValue() {
        if (maxValue == null) {
            maxValue = getAbsoluteMaxValue();
        }
        return (maxValue);
    }

    public void setMaxValue(BigDecimal value) {
        final String S_ProcName = "setMaxValue";
        if (value == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1, "value");
        }
        BigDecimal absoluteMaxValue = getAbsoluteMaxValue();
        if (value.compareTo(absoluteMaxValue) > 0) {
            throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName, 1,
                    "value", value, absoluteMaxValue);
        }
        maxValue = value;
    }

    public boolean hasValue() {
        boolean retval;
        String text = getText();
        if ((text == null) || (text.length() <= 0)) {
            retval = false;
        } else {
            retval = true;
        }
        return (retval);
    }

    public boolean isEditValid() {
        final String S_ProcName = "isEditValid";
        if (!hasValue()) {
            setValue(null);
            return (true);
        }

        boolean retval = super.isEditValid();
        if (retval) {
            try {
                commitEdit();
            } catch (ParseException e) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Field is not valid - " + e.getMessage(), e);

            }
            Object obj = getValue();
            if (obj == null) {
                retval = false;
            } else if (obj instanceof Short) {
                Short s = (Short) obj;
                BigDecimal v;
                if (precis > 0) {
                    v = new BigDecimal(s.toString() + "." + S_Zeroes.substring(0, precis));
                } else {
                    v = new BigDecimal(s.toString());
                }
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    retval = false;
                }
            } else if (obj instanceof Integer) {
                Integer i = (Integer) obj;
                BigDecimal v;
                if (precis > 0) {
                    v = new BigDecimal(i.toString() + "." + S_Zeroes.substring(0, precis));
                } else {
                    v = new BigDecimal(i.toString());
                }
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    retval = false;
                }
            } else if (obj instanceof Long) {
                Long l = (Long) obj;
                BigDecimal v;
                if (precis > 0) {
                    v = new BigDecimal(l.toString() + "." + S_Zeroes.substring(0, precis));
                } else {
                    v = new BigDecimal(l.toString());
                }
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    retval = false;
                }
            } else if (obj instanceof BigDecimal) {
                BigDecimal v = (BigDecimal) obj;
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    retval = false;
                }
            } else if (obj instanceof Number) {
                Number n = (Number) obj;
                BigDecimal v = new BigDecimal(n.toString());
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    retval = false;
                }
            } else {
                throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                        "EditedValue", obj, "Short, Integer, Long, BigDecimal or Number");
            }
        }
        return (retval);
    }

    public void setNumberValue(BigDecimal value) {
        super.setValue(value);
        if (value == null) {
            super.setText("");
        }
    }

    public BigDecimal getNumberValue() {
        final String S_ProcName = "getNumberValue";
        BigDecimal retval;
        String text = getText();
        if ((text == null) || (text.length() <= 0)) {
            retval = null;
        } else {
            if (!isEditValid()) {
                throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName,
                        "Field is not valid");
            }
            try {
                commitEdit();
            } catch (ParseException e) {
                throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName,
                        "Field is not valid - " + e.getMessage(), e);

            }
            Object obj = getValue();
            if (obj == null) {
                retval = null;
            } else if (obj instanceof Short) {
                Short s = (Short) obj;
                BigDecimal v;
                if (precis > 0) {
                    v = new BigDecimal(s.toString() + "." + S_Zeroes.substring(0, precis));
                } else {
                    v = new BigDecimal(s.toString());
                }
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                            "EditedValue", v, min, max);
                }
                retval = v;
            } else if (obj instanceof Integer) {
                Integer i = (Integer) obj;
                BigDecimal v;
                if (precis > 0) {
                    v = new BigDecimal(i.toString() + "." + S_Zeroes.substring(0, precis));
                } else {
                    v = new BigDecimal(i.toString());
                }
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                            "EditedValue", v, min, max);
                }
                retval = v;
            } else if (obj instanceof Long) {
                Long l = (Long) obj;
                BigDecimal v;
                if (precis > 0) {
                    v = new BigDecimal(l.toString() + "." + S_Zeroes.substring(0, precis));
                } else {
                    v = new BigDecimal(l.toString());
                }
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                            "EditedValue", v, min, max);
                }
                retval = v;
            } else if (obj instanceof BigDecimal) {
                BigDecimal v = (BigDecimal) obj;
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                            "EditedValue", v, min, max);
                }
                retval = v;
            } else if (obj instanceof Number) {
                Number n = (Number) obj;
                BigDecimal v = new BigDecimal(n.toString());
                BigDecimal min = getMinValue();
                BigDecimal max = getMaxValue();
                if ((v.compareTo(min) < 0) || (v.compareTo(max) > 0)) {
                    throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0,
                            "EditedValue", v, min, max);
                }
                retval = v;
            } else {
                throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName,
                        "EditedValue", obj, "Short, Integer, Long, BigDecimal or Number");
            }
        }
        return (retval);
    }
}