001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.formatter;
032:
033: import java.text.NumberFormat;
034: import java.text.ParseException;
035:
036: import javax.swing.text.NumberFormatter;
037:
038: import com.jgoodies.validation.util.ValidationUtils;
039:
040: /**
041: * In addition to its superclass NumberFormatter, this class converts
042: * to/from the empty string. Therefore it holds an <em>empty value</em>
043: * that is the counterpart of the empty string.
044: * The Method <code>#valueToString</code> converts the empty value to the
045: * empty string. And <code>#stringToValue</code> converts blank strings
046: * to the empty value. In all other cases the conversion is delegated
047: * to its superclass.<p>
048: *
049: * Often the empty value is <code>null</code> or <code>-1</code>.
050: *
051: * <strong>Examples:</strong><pre>
052: * new EmptyNumberFormatter();
053: * new EmptyNumberFormatter(-1);
054: * </pre>
055: *
056: * @author Karsten Lentzsch
057: * @version $Revision: 1.5 $
058: *
059: * @see java.text.Format
060: */
061: public class EmptyNumberFormatter extends NumberFormatter {
062:
063: /**
064: * Holds the Number that is converted to an empty string and
065: * that is the result of converting blank strings to a value.
066: *
067: * @see #stringToValue(String)
068: * @see #valueToString(Object)
069: */
070: private final Number emptyValue;
071:
072: // Instance Creation ****************************************************
073:
074: /**
075: * Constructs an EmptyNumberFormatter that converts <code>null</code>
076: * to the empty string and vice versa.
077: */
078: public EmptyNumberFormatter() {
079: this ((Number) null);
080: }
081:
082: /**
083: * Constructs an EmptyNumberFormatter configured with the specified
084: * Format; converts <code>null</code> to the empty string and vice versa.
085: *
086: * @param format Format used to dictate legal values
087: */
088: public EmptyNumberFormatter(NumberFormat format) {
089: this (format, null);
090: }
091:
092: /**
093: * Constructs an EmptyNumberFormatter that converts the given
094: * <code>emptyValue</code> to the empty string and vice versa.
095: *
096: * @param emptyValue the representation of the empty string
097: */
098: public EmptyNumberFormatter(Number emptyValue) {
099: this .emptyValue = emptyValue;
100: }
101:
102: /**
103: * Constructs an EmptyNumberFormatter configured with the specified
104: * Format; converts <code>null</code> to the given <code>emptyValue</code>
105: * and vice versa.
106: *
107: * @param format Format used to dictate legal values
108: * @param emptyValue the representation of the empty string
109: */
110: public EmptyNumberFormatter(NumberFormat format, Number emptyValue) {
111: super (format);
112: this .emptyValue = emptyValue;
113: }
114:
115: // Overriding Superclass Behavior *****************************************
116:
117: /**
118: * Returns the <code>Object</code> representation of the
119: * <code>String</code> <code>text</code>.<p>
120: *
121: * Unlike its superclass, this class converts blank strings
122: * to the empty value.
123: *
124: * @param text <code>String</code> to convert
125: * @return <code>Object</code> representation of text
126: * @throws ParseException if there is an error in the conversion
127: */
128: @Override
129: public Object stringToValue(String text) throws ParseException {
130: return ValidationUtils.isBlank(text) ? emptyValue : super
131: .stringToValue(text);
132: }
133:
134: /**
135: * Returns a String representation of the Object <code>value</code>.
136: * This invokes <code>format</code> on the current <code>Format</code>.<p>
137: *
138: * Unlike its superclass, this class converts the empty value
139: * to the empty string.
140: *
141: * @param value the value to convert
142: * @return a String representation of value
143: * @throws ParseException if there is an error in the conversion
144: */
145: @Override
146: public String valueToString(Object value) throws ParseException {
147: return ValidationUtils.equals(value, emptyValue) ? "" : super
148: .valueToString(value);
149: }
150:
151: }
|