001    /*
002     * Copyright 2005 Sun Microsystems, Inc., 4150 Network Circle,
003     * Santa Clara, California 95054, U.S.A. All rights reserved.
004     *
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     *
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     */
019    package org.jdesktop.swingx;
020    
021    import javax.swing.JFormattedTextField;
022    import javax.swing.UIManager;
023    import java.text.DateFormat;
024    import java.text.SimpleDateFormat;
025    import java.text.ParseException;
026    
027    /**
028     * Default formatter for the JXDatePicker component.  This factory
029     * creates and returns a formatter that can handle a variety of date
030     * formats.
031     *
032     * @author Joshua Outwater
033     */
034    public class JXDatePickerFormatter extends
035            JFormattedTextField.AbstractFormatter {
036        private DateFormat _formats[] = null;
037    
038        public JXDatePickerFormatter() {
039            _formats = new DateFormat[3];
040            _formats[0] = new SimpleDateFormat(UIManager.getString("JXDatePicker.longFormat"));
041            _formats[1] = new SimpleDateFormat(UIManager.getString("JXDatePicker.mediumFormat"));
042            _formats[2] = new SimpleDateFormat(UIManager.getString("JXDatePicker.shortFormat"));
043        }
044    
045        public JXDatePickerFormatter(DateFormat formats[]) {
046            _formats = formats;
047        }
048    
049        public DateFormat[] getFormats() {
050            return _formats;
051        }
052    
053        /**
054         * {@inheritDoc}
055         */
056        public Object stringToValue(String text) throws ParseException {
057            Object result = null;
058            ParseException pex = null;
059    
060            if (text == null || text.trim().length() == 0) {
061                return null;
062            }
063    
064            // If the current formatter did not work loop through the other
065            // formatters and see if any of them can parse the string passed
066            // in.
067            for (DateFormat _format : _formats) {
068                try {
069                    result = (_format).parse(text);
070                    pex = null;
071                    break;
072                } catch (ParseException ex) {
073                    pex = ex;
074                }
075            }
076    
077            if (pex != null) {
078                throw pex;
079            }
080    
081            return result;
082        }
083    
084        /**
085         * {@inheritDoc}
086         */
087        public String valueToString(Object value) throws ParseException {
088            if (value != null) {
089                return _formats[0].format(value);
090            }
091            return null;
092        }
093    }