001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.tutorial.util;
032:
033: import java.text.DateFormat;
034:
035: import javax.swing.JFormattedTextField;
036: import javax.swing.text.DefaultFormatter;
037: import javax.swing.text.DefaultFormatterFactory;
038:
039: import com.jgoodies.binding.adapter.BasicComponentFactory;
040: import com.jgoodies.binding.adapter.Bindings;
041: import com.jgoodies.binding.value.ValueModel;
042: import com.jgoodies.validation.formatter.RelativeDateFormatter;
043:
044: /**
045: * Consists only of static methods that vend formatted text fields used
046: * to edit dates that are bound to an underlying ValueModel.
047: * Extends the Binding library's BasicComponentFactory to inherit
048: * all factory methods from that class.
049: *
050: * @author Karsten Lentzsch
051: * @version $Revision: 1.10 $
052: *
053: * @see com.jgoodies.binding.adapter.BasicComponentFactory
054: * @see com.jgoodies.binding.adapter.Bindings
055: */
056: public final class ExampleComponentFactory extends
057: BasicComponentFactory {
058:
059: private ExampleComponentFactory() {
060: // Suppresses default constructor, ensuring non-instantiability.
061: }
062:
063: /**
064: * Creates and returns a formatted text field that is bound
065: * to the Date value of the given <code>ValueModel</code>.<p>
066: *
067: * The JFormattedTextField is configured with an AbstractFormatter
068: * that uses two different DateFormats to edit and display the Date.
069: * A <code>SHORT</code> DateFormat with strict checking is used to edit
070: * (parse) a date; the DateFormatter's default DateFormat is used to
071: * display (format) a date. In both cases <code>null</code> Dates are
072: * mapped to the empty String.<p>
073: *
074: * In addition to formatted Dates, the parser accepts positive and
075: * negative integers and interprets them as Dates relative to today.
076: * For example -1 is yesterday, 1 is tomorrow, and 7 is "in a week".<p>
077: *
078: * Yesterday, today, and tomorrow are displayed as these Strings,
079: * not as formatted Dates.
080: *
081: * @param valueModel the model that holds the value to be edited
082: * @return a formatted text field for Date instances that is bound
083: * @throws NullPointerException if the model is <code>null</code>
084: */
085: public static JFormattedTextField createDateField(
086: ValueModel valueModel) {
087: return createDateField(valueModel, true);
088: }
089:
090: /**
091: * Creates and returns a formatted text field that is bound
092: * to the Date value of the given <code>ValueModel</code>.<p>
093: *
094: * The JFormattedTextField is configured with an AbstractFormatter
095: * that uses two different DateFormats to edit and display the Date.
096: * A <code>SHORT</code> DateFormat with strict checking is used to edit
097: * (parse) a date; the DateFormatter's default DateFormat is used to
098: * display (format) a date. In both cases <code>null</code> Dates are
099: * mapped to the empty String.<p>
100: *
101: * In addition to formatted Dates, the parser accepts positive and
102: * negative integers and interprets them as Dates relative to today.
103: * For example -1 is yesterday, 1 is tomorrow, and 7 is "in a week".<p>
104: *
105: * If <code>enableShortcuts</code> is set to <code>true</code>,
106: * yesterday, today, and tomorrow are displayed as these Strings,
107: * not as formatted Dates.
108: *
109: * @param valueModel the model that holds the value to be edited
110: * @param enableShortcuts true to display yesterday, today, and tomorrow
111: * with natural language strings
112: * @return a formatted text field for Date instances that is bound
113: * @throws NullPointerException if the model is <code>null</code>
114: */
115: public static JFormattedTextField createDateField(
116: ValueModel valueModel, boolean enableShortcuts) {
117: return createDateField(valueModel, enableShortcuts, false);
118: }
119:
120: /**
121: * Creates and returns a formatted text field that is bound
122: * to the Date value of the given <code>ValueModel</code>.<p>
123: *
124: * The JFormattedTextField is configured with an AbstractFormatter
125: * that uses two different DateFormats to edit and display the Date.
126: * A <code>SHORT</code> DateFormat with strict checking is used to edit
127: * (parse) a date; the DateFormatter's default DateFormat is used to
128: * display (format) a date. In both cases <code>null</code> Dates are
129: * mapped to the empty String.<p>
130: *
131: * In addition to formatted Dates, the parser accepts positive and
132: * negative integers and interprets them as Dates relative to today.
133: * For example -1 is yesterday, 1 is tomorrow, and 7 is "in a week".<p>
134: *
135: * If <code>enableShortcuts</code> is set to <code>true</code>,
136: * yesterday, today, and tomorrow are displayed as these Strings,
137: * not as formatted Dates.
138: *
139: * @param valueModel the model that holds the value to be edited
140: * @param enableShortcuts true to display yesterday, today, and tomorrow
141: * with natural language strings
142: * @param commitsOnValidEdit true to commit on valid edit,
143: * false to commit on focus lost
144: * @return a formatted text field for Date instances that is bound
145: * @throws NullPointerException if the model is <code>null</code>
146: */
147: public static JFormattedTextField createDateField(
148: ValueModel valueModel, boolean enableShortcuts,
149: boolean commitsOnValidEdit) {
150: DateFormat shortFormat = DateFormat
151: .getDateInstance(DateFormat.SHORT);
152: shortFormat.setLenient(false);
153:
154: DefaultFormatter defaultFormatter = new RelativeDateFormatter(
155: shortFormat, false, true);
156: defaultFormatter.setCommitsOnValidEdit(commitsOnValidEdit);
157:
158: JFormattedTextField.AbstractFormatter displayFormatter = new RelativeDateFormatter(
159: enableShortcuts, true);
160:
161: DefaultFormatterFactory formatterFactory = new DefaultFormatterFactory(
162: defaultFormatter, displayFormatter);
163:
164: JFormattedTextField textField = new JFormattedTextField(
165: formatterFactory);
166: Bindings.bind(textField, valueModel);
167: return textField;
168: }
169:
170: }
|