001/* 002 * $Id: NumberEditorNumberFormat.java 3781 2010-09-15 08:33:52Z kleopatra $ 003 * 004 * Copyright 2008 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 */ 021package org.jdesktop.swingx.table; 022 023import java.text.AttributedCharacterIterator; 024import java.text.AttributedString; 025import java.text.FieldPosition; 026import java.text.Format; 027import java.text.NumberFormat; 028import java.text.ParsePosition; 029 030/** 031 * A specialised Format for the NumberEditor that returns a null for empty 032 * strings. 033 * 034 * @author Noel Grandin 035 * 036 * @deprecated (pre-1.6.2) replaced by NumberEditorExt, no longer used internally 037 */ 038@Deprecated 039class NumberEditorNumberFormat extends Format { 040 private final NumberFormat childFormat; 041 042 public NumberEditorNumberFormat(NumberFormat childFormat) { 043 if (childFormat == null) { 044 childFormat = NumberFormat.getInstance(); 045 } 046 this.childFormat = childFormat; 047 } 048 049 @Override 050 public AttributedCharacterIterator formatToCharacterIterator(Object obj) { 051 if (obj == null) 052 return new AttributedString("").getIterator(); 053 return childFormat.formatToCharacterIterator(obj); 054 } 055 056 @Override 057 public StringBuffer format(Object obj, StringBuffer toAppendTo, 058 FieldPosition pos) { 059 if (obj == null) 060 return new StringBuffer(""); 061 return childFormat.format(obj, toAppendTo, pos); 062 } 063 064 @Override 065 public Object parseObject(String source, ParsePosition pos) { 066 if (source == null) { 067 pos.setIndex(1); // otherwise Format thinks parse failed 068 return null; 069 } 070 if (source.trim().equals("")) { 071 pos.setIndex(1); // otherwise Format thinks parse failed 072 return null; 073 } 074 Object val = childFormat.parseObject(source, pos); 075 /* 076 * The default behaviour of Format objects is to keep parsing as long as 077 * they encounter valid data. By for table editing we don't want 078 * trailing bad data to be considered a "valid value". So set the index 079 * to 0 so that the parse(Object) method knows that we had an error. 080 */ 081 if (pos.getIndex() != source.length()) { 082 pos.setErrorIndex(pos.getIndex()); 083 pos.setIndex(0); 084 } 085 return val; 086 } 087}