JXDatePickerFormatterFactory.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXDatePickerFormatterFactory.java,v 1.3 2005/10/10 18:02:45 rbair Exp $
  3.  * 
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.calendar;
  22. import java.text.ParseException;
  23. import java.text.SimpleDateFormat;
  24. import javax.swing.JFormattedTextField;
  25. import javax.swing.JFormattedTextField.AbstractFormatter;
  26. import javax.swing.JFormattedTextField.AbstractFormatterFactory;
  27. import javax.swing.UIManager;
  28. /**
  29.  * Default formatter factory for the JXDatePicker component.  This factory
  30.  * creates and returns a formatter that can handle a variety of date formats.
  31.  *
  32.  * @author Joshua Outwater
  33.  */
  34. public class JXDatePickerFormatterFactory extends AbstractFormatterFactory {
  35.     /** Cached formatter */
  36.     protected AbstractFormatter formatter = null;
  37.     /**
  38.      * {@inheritDoc}
  39.      */
  40.     @Override
  41.     public AbstractFormatter getFormatter(JFormattedTextField ftf) {
  42.         if (formatter == null) {
  43.             formatter = new JXDatePickerFormatter();
  44.         }
  45.         return formatter;
  46.     }
  47.     /**
  48.      * Default formatter class for the JXDatePicker component.  This formatter
  49.      * supports the following three default formats:
  50.      * <ul>
  51.      * <li>EEE MM/dd/yyyy (Fri 04/09/2004)
  52.      * <li>MM/dd/yyyy     (04/09/2004)
  53.      * <li>dd/yy          (04/09)
  54.      * </ul>
  55.      * These formats are localizable and fields may be re-arranged, such as
  56.      * swapping the month and day fields.  The keys for localizing these fields
  57.      * are:
  58.      * <ul>
  59.      * <li>JXDatePicker.longFormat
  60.      * <li>JXDatePicker.mediumFormat
  61.      * <li>JXDatePicker.shortFormat
  62.      * </ul>
  63.      * It is important to order the formats in the order of most complex to
  64.      * least complex as it is possible for less complex formats to match more
  65.      * complex strings.
  66.      */
  67.     private class JXDatePickerFormatter extends
  68.             JFormattedTextField.AbstractFormatter {
  69.         private SimpleDateFormat _formats[] = null;
  70.         private int _formatIndex = 0;
  71.         
  72.         public JXDatePickerFormatter() {
  73.             _formats = new SimpleDateFormat[3];
  74.             String format = UIManager.getString("JXDatePicker.longFormat");
  75.             if (format == null) {
  76.                 format = "EEE MM/dd/yyyy";
  77.             }
  78.             _formats[0] = new SimpleDateFormat(format);
  79.             format = UIManager.getString("JXDatePicker.mediumFormat");
  80.             if (format == null) {
  81.                 format = "MM/dd/yyyy";
  82.             }
  83.             _formats[1] = new SimpleDateFormat(format);
  84.             format = UIManager.getString("JXDatePicker.shortFormat");
  85.             if (format == null) {
  86.                 format = "MM/dd";
  87.             }
  88.             _formats[2] = new SimpleDateFormat(format);
  89.         }
  90.         /**
  91.          * {@inheritDoc}
  92.          */
  93.         @Override
  94.         public Object stringToValue(String text) throws ParseException {
  95.             Object result = null;
  96.             ParseException pex = null;
  97.             if (text == null || text.trim().length() == 0) {
  98.                 return null;
  99.             }
  100.             // If the current formatter did not work loop through the other
  101.             // formatters and see if any of them can parse the string passed
  102.             // in.
  103.             if (result == null) {
  104.                 for (int i = 0; i < _formats.length; i++) {
  105.                     try {
  106.                         result = _formats[i].parse(text);
  107.                         // We got a successful formatter.  Update the current
  108.                         // formatter index.
  109.                         _formatIndex = i;
  110.                         pex = null;
  111.                         break;
  112.                     } catch (ParseException ex) {
  113.                         pex = ex;
  114.                     }
  115.                 }
  116.             }
  117.             if (pex != null) {
  118.                 throw pex;
  119.             }
  120.             return result;
  121.         }
  122.         /**
  123.          * {@inheritDoc}
  124.          */
  125.         @Override
  126.         public String valueToString(Object value) {
  127.             if (value != null) {
  128.                 return _formats[_formatIndex].format(value);
  129.             }
  130.             return null;
  131.         }
  132.     }
  133. }