Java tutorial
// Description: Java 7 Swing Double 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 Double Text Field Display Widget. */ public class CFJDoubleEditor extends CFJFormattedTextField { protected static Format defaultFormat = null; protected Double minValue = Double.MIN_VALUE; protected Double maxValue = Double.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.0######################################################################################################################################################################################################################################################################################################################"); } return (defaultFormat); } public CFJDoubleEditor() { super(getDefaultFormat()); setHorizontalAlignment(JTextField.RIGHT); Dimension min = new Dimension(16 * getColumnWidth(), 25); setMinimumSize(min); addFocusListener(new EditorFocusListener()); } public Double getMinValue() { return (minValue); } public void setMinValue(Double value) { final String S_ProcName = "setMinValue"; if (value == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1, "value"); } if (value.compareTo(Double.MIN_VALUE) < 0) { throw CFLib.getDefaultExceptionFactory().newArgumentUnderflowException(getClass(), S_ProcName, 1, "value", value, Float.MIN_VALUE); } minValue = value; } public Double getMaxValue() { return (maxValue); } public void setMaxValue(Double value) { final String S_ProcName = "setMaxValue"; if (value == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 1, "value"); } if (value.compareTo(Double.MAX_VALUE) > 0) { throw CFLib.getDefaultExceptionFactory().newArgumentOverflowException(getClass(), S_ProcName, 1, "value", value, Float.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 Float) { Float f = (Float) obj; Double v = new Double(f.doubleValue()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { retval = false; } } else if (obj instanceof Double) { Double v = (Double) obj; if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { retval = false; } } else if (obj instanceof Short) { Short s = (Short) obj; Double v = new Double(s.doubleValue()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { retval = false; } } else if (obj instanceof Integer) { Integer i = (Integer) obj; Double v = new Double(i.doubleValue()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { retval = false; } } else if (obj instanceof Long) { Long l = (Long) obj; Double v = new Double(l.doubleValue()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { retval = false; } } else if (obj instanceof BigDecimal) { BigDecimal b = (BigDecimal) obj; Double v = new Double(b.toString()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { retval = false; } } else if (obj instanceof Number) { Number n = (Number) obj; Double v = new Double(n.toString()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { retval = false; } } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "EditedValue", obj, "Short, Integer, Long, BigDecimal, Float, Double or Number"); } } return (retval); } public void setDoubleValue(Double value) { super.setValue(value); if (value == null) { super.setText(""); } } public Double getDoubleValue() { final String S_ProcName = "getDoubleValue"; Double 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 Float) { Float f = (Float) obj; Double v = new Double(f.doubleValue()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0, "EditedValue", v, minValue, maxValue); } retval = v; } else if (obj instanceof Double) { Double v = (Double) obj; if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0, "EditedValue", v, minValue, maxValue); } retval = v; } else if (obj instanceof Short) { Short s = (Short) obj; Double v = new Double(s.doubleValue()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0, "EditedValue", v, minValue, maxValue); } retval = v; } else if (obj instanceof Integer) { Integer i = (Integer) obj; Double v = new Double(i.doubleValue()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0, "EditedValue", v, minValue, maxValue); } retval = v; } else if (obj instanceof Long) { Long l = (Long) obj; Double v = new Double(l.doubleValue()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0, "EditedValue", v, minValue, maxValue); } retval = v; } else if (obj instanceof BigDecimal) { BigDecimal b = (BigDecimal) obj; Double v = new Double(b.toString()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0, "EditedValue", v, minValue, maxValue); } retval = v; } else if (obj instanceof Number) { Number n = (Number) obj; Double v = new Double(n.toString()); if ((v.compareTo(minValue) < 0) || (v.compareTo(maxValue) > 0)) { throw CFLib.getDefaultExceptionFactory().newArgumentRangeException(getClass(), S_ProcName, 0, "EditedValue", v, minValue, maxValue); } retval = v; } else { throw CFLib.getDefaultExceptionFactory().newUnsupportedClassException(getClass(), S_ProcName, "EditedValue", obj, "Short, Integer, Long, BigDecimal or Number"); } } return (retval); } }