001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.form;
017    
018    import java.util.Date;
019    
020    import javax.swing.JFormattedTextField;
021    import javax.swing.JFormattedTextField.AbstractFormatter;
022    import javax.swing.JFormattedTextField.AbstractFormatterFactory;
023    import javax.swing.text.DateFormatter;
024    import javax.swing.text.DefaultFormatter;
025    import javax.swing.text.NumberFormatter;
026    
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.springframework.binding.value.swing.ValueCommitPolicy;
030    import org.springframework.util.Assert;
031    
032    /**
033     * 
034     * @author Keith Donald
035     */
036    public class FormatterFactory extends AbstractFormatterFactory {
037    
038        private static final Log logger = LogFactory.getLog(FormatterFactory.class);
039    
040        private ValueCommitPolicy valueCommitPolicy = ValueCommitPolicy.AS_YOU_TYPE;
041    
042        private Class valueClass;
043    
044        public FormatterFactory(Class valueClass) {
045            this(valueClass, ValueCommitPolicy.AS_YOU_TYPE);
046        }
047    
048        public FormatterFactory(Class valueClass, ValueCommitPolicy policy) {
049            this.valueClass = valueClass;
050            setValueCommitPolicy(policy);
051        }
052    
053        public void setValueCommitPolicy(ValueCommitPolicy policy) {
054            Assert.notNull(policy);
055            this.valueCommitPolicy = policy;
056        }
057    
058        public AbstractFormatter getFormatter(JFormattedTextField source) {
059            Object value = source.getValue();
060            DefaultFormatter formatter;
061            if (value instanceof Date) {
062                formatter = new DateFormatter();
063            }
064            else if (value instanceof Number) {
065                formatter = new NumberFormatter();
066            }
067            else {
068                formatter = new DefaultFormatter();
069            }
070            if (logger.isDebugEnabled()) {
071                logger.debug("Factory returning new formatter " + formatter + " for text field " + source);
072            }
073            valueCommitPolicy.configure(source, formatter);
074            if (valueClass != null) {
075                formatter.setValueClass(valueClass);
076            }
077            else if (value != null) {
078                formatter.setValueClass(value.getClass());
079            }
080            return formatter;
081        }
082    }