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.table; 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 * 037 * @deprecated (pre-1.6.2) moved to org.jdesktop.swingx.text 038 */ 039@Deprecated 040class NumberFormatExt extends NumberFormat { 041 042 private NumberFormat childFormat; 043 044 public NumberFormatExt() { 045 this(null); 046 } 047 048 public NumberFormatExt(NumberFormat childFormat) { 049 if (childFormat == null) { 050 childFormat = NumberFormat.getInstance(); 051 } 052 this.childFormat = childFormat; 053 } 054 055 @Override 056 public AttributedCharacterIterator formatToCharacterIterator(Object obj) { 057 if (obj == null) 058 return new AttributedString("").getIterator(); 059 return childFormat.formatToCharacterIterator(obj); 060 } 061 062 @Override 063 public StringBuffer format(Object obj, StringBuffer toAppendTo, 064 FieldPosition pos) { 065 if (obj == null) 066 return new StringBuffer(""); 067 return childFormat.format(obj, toAppendTo, pos); 068 } 069 070 @Override 071 public Number parse(String source, ParsePosition pos) { 072 if (source == null) { 073 pos.setIndex(1); // otherwise Format thinks parse failed 074 return null; 075 } 076 if (source.trim().equals("")) { 077 pos.setIndex(1); // otherwise Format thinks parse failed 078 return null; 079 } 080 Number val = childFormat.parse(source, pos); 081 /* 082 * The default behaviour of Format objects is to keep parsing as long as 083 * they encounter valid data. By for table editing we don't want 084 * trailing bad data to be considered a "valid value". So set the index 085 * to 0 so that the parse(Object) method knows that we had an error. 086 */ 087 if (pos.getIndex() != source.length()) { 088 pos.setErrorIndex(pos.getIndex()); 089 pos.setIndex(0); 090 } 091 return val; 092 } 093 094 @Override 095 public StringBuffer format(double number, StringBuffer toAppendTo, 096 FieldPosition pos) { 097 return childFormat.format(number, toAppendTo, pos); 098 } 099 100 @Override 101 public StringBuffer format(long number, StringBuffer toAppendTo, 102 FieldPosition pos) { 103 return childFormat.format(number, toAppendTo, pos); 104 } 105 106}