001/* 002 * $Id$ 003 * 004 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 * 021 */ 022package org.jdesktop.swingx.text; 023 024import java.text.AttributedCharacterIterator; 025import java.text.AttributedString; 026import java.text.FieldPosition; 027import java.text.NumberFormat; 028import java.text.ParsePosition; 029 030/** 031 * A specialised NumberFormat which handles null values and empty Strings. 032 * This is useful in cell editors and used in StrictNumberFormatter. 033 * 034 * @author Noel Grandin 035 * @author Jeanette Winzenburg 036 */ 037public class NumberFormatExt extends NumberFormat { 038 039 private NumberFormat childFormat; 040 041 public NumberFormatExt() { 042 this(null); 043 } 044 045 public NumberFormatExt(NumberFormat childFormat) { 046 if (childFormat == null) { 047 childFormat = NumberFormat.getInstance(); 048 } 049 this.childFormat = childFormat; 050 } 051 052 @Override 053 public AttributedCharacterIterator formatToCharacterIterator(Object obj) { 054 if (obj == null) 055 return new AttributedString("").getIterator(); 056 return childFormat.formatToCharacterIterator(obj); 057 } 058 059 @Override 060 public StringBuffer format(Object obj, StringBuffer toAppendTo, 061 FieldPosition pos) { 062 if (obj == null) 063 return new StringBuffer(""); 064 return childFormat.format(obj, toAppendTo, pos); 065 } 066 067 @Override 068 public Number parse(String source, ParsePosition pos) { 069 if (source == null) { 070 pos.setIndex(1); // otherwise Format thinks parse failed 071 return null; 072 } 073 if (source.trim().equals("")) { 074 pos.setIndex(1); // otherwise Format thinks parse failed 075 return null; 076 } 077 Number val = childFormat.parse(source, pos); 078 /* 079 * The default behaviour of Format objects is to keep parsing as long as 080 * they encounter valid data. By for table editing we don't want 081 * trailing bad data to be considered a "valid value". So set the index 082 * to 0 so that the parse(Object) method knows that we had an error. 083 */ 084 if (pos.getIndex() != source.length()) { 085 pos.setErrorIndex(pos.getIndex()); 086 pos.setIndex(0); 087 } 088 return val; 089 } 090 091 @Override 092 public StringBuffer format(double number, StringBuffer toAppendTo, 093 FieldPosition pos) { 094 return childFormat.format(number, toAppendTo, pos); 095 } 096 097 @Override 098 public StringBuffer format(long number, StringBuffer toAppendTo, 099 FieldPosition pos) { 100 return childFormat.format(number, toAppendTo, pos); 101 } 102 103}