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

Java tutorial

Introduction

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

Source

// Description: Java 7 Swing UInt64 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 UInt64 Text Field Display Widget.
 */
public class CFJUInt64Editor extends CFJFormattedTextField {
    protected static Format defaultFormat = null;
    public final static BigDecimal MIN_VALUE = new BigDecimal("0");
    public final static BigDecimal MAX_VALUE = new BigDecimal("18446744073709551616");

    protected BigDecimal minValue = MIN_VALUE;
    protected BigDecimal maxValue = MAX_VALUE;

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

        public void focusGained(FocusEvent e) {
        }

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

    public static Format getDefaultFormat() {
        if (defaultFormat == null) {
            defaultFormat = new DecimalFormat("###################0");
        }
        return (defaultFormat);
    }

    public CFJUInt64Editor() {
        super(getDefaultFormat());
        setHorizontalAlignment(JTextField.RIGHT);
        Dimension min = new Dimension(21 * getColumnWidth(), 25);
        setMinimumSize(min);
        setMaximumSize(min);
        addFocusListener(new EditorFocusListener());
    }

    public BigDecimal getMinValue() {
        return (minValue);
    }

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

    public BigDecimal getMaxValue() {
        return (maxValue);
    }

    public void setMaxValue(BigDecimal value) {
        final String S_ProcName = "setMaxValue";
        if (value == null) {
            throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1, "value");
        }
        if (value.compareTo(MAX_VALUE) > 0) {
            throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName, 1,
                    "value", value, MAX_VALUE);
        }
        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 = 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 = 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 = 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 setUInt64Value(BigDecimal value) {
        super.setValue(value);
        if (value == null) {
            super.setText("");
        }
    }

    public BigDecimal getUInt64Value() {
        final String S_ProcName = "getUInt64Value";
        BigDecimal retval;
        String text = getText();
        if ((text == null) || (text.length() <= 0)) {
            retval = null;
        } else {
            if (!super.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 = 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 = 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 = 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);
    }
}