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.formatted;
032:
033: import java.text.Format;
034: import java.text.NumberFormat;
035: import java.util.LinkedList;
036: import java.util.List;
037:
038: import javax.swing.*;
039: import javax.swing.text.DefaultFormatter;
040: import javax.swing.text.DefaultFormatterFactory;
041: import javax.swing.text.NumberFormatter;
042:
043: import com.jgoodies.forms.builder.DefaultFormBuilder;
044: import com.jgoodies.forms.layout.FormLayout;
045: import com.jgoodies.validation.formatter.EmptyNumberFormatter;
046: import com.jgoodies.validation.tutorial.formatted.format.DisplayFormat;
047: import com.jgoodies.validation.tutorial.formatted.formatter.CustomNumberFormatter;
048: import com.jgoodies.validation.tutorial.util.MyFocusTraversalPolicy;
049: import com.jgoodies.validation.tutorial.util.TutorialUtils;
050:
051: /**
052: * Demonstrates different configurations of <code>JFormattedTextField</code>
053: * to display and edit numbers. Shows<ul>
054: * <li>how to use a custom NumberFormat
055: * <li>how to use a custom NumberFormatter
056: * <li>how to use a custom FormatterFactory
057: * <li>how to reset a number to <code>null</code>
058: * <li>how to map the empty string to a special number
059: * <li>how to commit values on valid texts
060: * <li>how to set the class of the result
061: * </ul><p>
062: *
063: * To look under the hood of this component, this class exposes
064: * the bound properties <em>text</em>, <em>value</em> and <em>editValid</em>.
065: *
066: * @author Karsten Lentzsch
067: * @version $Revision: 1.18 $
068: *
069: * @see JFormattedTextField
070: * @see JFormattedTextField.AbstractFormatter
071: */
072: public final class NumberExample {
073:
074: public static void main(String[] args) {
075: try {
076: UIManager
077: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
078: } catch (Exception e) {
079: // Likely Plastic is not in the classpath; ignore it.
080: }
081: JFrame frame = new JFrame();
082: frame.setTitle("Formatted :: Numbers");
083: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
084: JComponent panel = new NumberExample().buildPanel();
085: frame.getContentPane().add(panel);
086: frame.pack();
087: TutorialUtils.locateOnOpticalScreenCenter(frame);
088: frame.setVisible(true);
089: }
090:
091: /**
092: * Builds and returns a panel with a header row and the sample rows.
093: *
094: * @return the example panel with a header and the sample rows
095: */
096: public JComponent buildPanel() {
097: FormLayout layout = new FormLayout(
098: "r:max(80dlu;pref), 4dlu, 45dlu, 1dlu, pref, 4dlu, pref, 0:grow");
099:
100: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
101: builder.setDefaultDialogBorder();
102: builder.getPanel().setFocusTraversalPolicy(
103: MyFocusTraversalPolicy.INSTANCE);
104:
105: Utils.appendTitleRow(builder, "Description");
106: List<JFormattedTextField> fields = appendDemoRows(builder);
107: Utils.appendButtonBar(builder, fields, "42", "aa42");
108: return builder.getPanel();
109: }
110:
111: /**
112: * Appends the demo rows to the given builder and returns the List of
113: * formatted text fields.
114: *
115: * @param builder the builder used to add components to
116: * @return the List of formatted text fields
117: */
118: private List<JFormattedTextField> appendDemoRows(
119: DefaultFormBuilder builder) {
120: // The Formatter is chosen by the initial value.
121: JFormattedTextField defaultNumberField = new JFormattedTextField(
122: Long.valueOf(42));
123:
124: // The Formatter is chosen by the given Format.
125: JFormattedTextField noInitialValueField = new JFormattedTextField(
126: NumberFormat.getIntegerInstance());
127:
128: // Uses a custom NumberFormat.
129: NumberFormat customFormat = NumberFormat.getIntegerInstance();
130: customFormat.setMinimumIntegerDigits(3);
131: JFormattedTextField customFormatField = new JFormattedTextField(
132: new NumberFormatter(customFormat));
133:
134: // Uses a custom NumberFormatter that prints natural language strings.
135: JFormattedTextField customFormatterField = new JFormattedTextField(
136: new CustomNumberFormatter());
137:
138: // Uses a custom FormatterFactory that used different formatters
139: // for the display and while editing.
140: DefaultFormatterFactory formatterFactory = new DefaultFormatterFactory(
141: new NumberFormatter(), new CustomNumberFormatter());
142: JFormattedTextField formatterFactoryField = new JFormattedTextField(
143: formatterFactory);
144:
145: // Wraps a NumberFormatter to map empty strings to null and vice versa.
146: JFormattedTextField numberOrNullField = new JFormattedTextField(
147: new EmptyNumberFormatter());
148:
149: // Wraps a NumberFormatter to map empty strings to -1 and vice versa.
150: Integer emptyValue = Integer.valueOf(-1);
151: JFormattedTextField numberOrEmptyValueField = new JFormattedTextField(
152: new EmptyNumberFormatter(emptyValue));
153: numberOrEmptyValueField.setValue(emptyValue);
154:
155: // Commits values on valid edit texts.
156: DefaultFormatter formatter = new NumberFormatter();
157: formatter.setCommitsOnValidEdit(true);
158: JFormattedTextField commitOnValidEditField = new JFormattedTextField(
159: formatter);
160:
161: // Returns number values of type Integer
162: NumberFormatter numberFormatter = new NumberFormatter();
163: numberFormatter.setValueClass(Integer.class);
164: JFormattedTextField integerField = new JFormattedTextField(
165: numberFormatter);
166:
167: Format displayFormat = new DisplayFormat(NumberFormat
168: .getIntegerInstance());
169: Format typedDisplayFormat = new DisplayFormat(NumberFormat
170: .getIntegerInstance(), true);
171: List<JFormattedTextField> fields = new LinkedList<JFormattedTextField>();
172: fields.add(Utils.appendRow(builder, "Default",
173: defaultNumberField, typedDisplayFormat));
174: fields.add(Utils.appendRow(builder, "No initial value",
175: noInitialValueField, displayFormat));
176: fields.add(Utils.appendRow(builder, "Empty <-> null",
177: numberOrNullField, displayFormat));
178: fields.add(Utils.appendRow(builder, "Empty <-> -1",
179: numberOrEmptyValueField, displayFormat));
180: fields.add(Utils.appendRow(builder, "Custom format",
181: customFormatField, displayFormat));
182: fields.add(Utils.appendRow(builder, "Custom formatter",
183: customFormatterField, displayFormat));
184: fields.add(Utils.appendRow(builder, "Formatter factory",
185: formatterFactoryField, displayFormat));
186: fields.add(Utils.appendRow(builder, "Commits on valid edit",
187: commitOnValidEditField, displayFormat));
188: fields.add(Utils.appendRow(builder, "Integer Result",
189: integerField, typedDisplayFormat));
190:
191: return fields;
192: }
193:
194: }
|