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.awt.Component;
034: import java.awt.event.ActionEvent;
035: import java.text.Format;
036: import java.util.List;
037:
038: import javax.swing.AbstractAction;
039: import javax.swing.JButton;
040: import javax.swing.JFormattedTextField;
041: import javax.swing.JLabel;
042: import javax.swing.text.JTextComponent;
043:
044: import com.jgoodies.binding.beans.PropertyAdapter;
045: import com.jgoodies.binding.beans.PropertyConnector;
046: import com.jgoodies.forms.builder.DefaultFormBuilder;
047: import com.jgoodies.forms.factories.ButtonBarFactory;
048: import com.jgoodies.forms.layout.CellConstraints;
049: import com.jgoodies.validation.tutorial.util.ExampleComponentFactory;
050: import com.jgoodies.validation.view.ValidationResultViewFactory;
051:
052: /**
053: * Provides helper code for the <em>Formatted</em> tutorial example series.
054: *
055: * @author Karsten Lentzsch
056: * @version $Revision: 1.13 $
057: *
058: * @see JFormattedTextField
059: * @see Format
060: * @see com.jgoodies.binding.beans.PropertyAdapter
061: * @see com.jgoodies.binding.beans.PropertyConnector
062: * @see com.jgoodies.validation.tutorial.util.ExampleComponentFactory
063: */
064:
065: public final class Utils {
066:
067: private Utils() {
068: // Override default constructor; prevents instantiation.
069: }
070:
071: //*************************************************************************
072:
073: /**
074: * Appends a row to the given builder that consists of the
075: * title labels used in the formatted example series.
076: *
077: * @param builder the builder used to add titles to
078: * @param title the text for the first column
079: */
080: static void appendTitleRow(DefaultFormBuilder builder, String title) {
081: CellConstraints cc = new CellConstraints();
082:
083: builder.appendTitle(title);
084: int row = builder.getRow();
085: builder.addTitle("text", cc.xy(3, row, "center, center"));
086: builder.addTitle("editValid", cc.xy(5, row, "center, center"));
087: builder.addTitle("value", cc.xy(7, row));
088: builder.appendRow("4dlu");
089: builder.nextLine(2);
090: }
091:
092: /**
093: * Appends a row to the given builder. The row consists of
094: * a label for the focus lost behavior mode name,
095: * the given JFormattedTextField,
096: * a check icon label that is visible iff the field's <em>editValid</em>
097: * property is <code>true</code>,
098: * and a text label that displays the field's <em>value</em> property.
099: *
100: * @param builder the builder used to append the components
101: * @param text the String that describes the added row
102: * @param field the formatted text field
103: * @param displayFormat the format used to format the field's value
104: * @return the JFormattedTextField added to the builder
105: */
106: static JFormattedTextField appendRow(DefaultFormBuilder builder,
107: String text, JFormattedTextField field, Format displayFormat) {
108: builder.append(text, field, Utils
109: .createEditValidCheckLabel(field), Utils
110: .createValueTextLabel(field, displayFormat));
111: return field;
112: }
113:
114: /**
115: * Creates and returns a label with a check icon, that is visible,
116: * if and only if the given field's <em>editValid</em> property is true.
117: *
118: * @param field the JFormattedTextField to be inspected
119: * @return a label with visibility bound to the field's editValid value
120: */
121: private static JLabel createEditValidCheckLabel(
122: JFormattedTextField field) {
123: JLabel label = new JLabel(ValidationResultViewFactory
124: .getCheckIcon());
125: PropertyConnector.connect(field, "editValid", label, "visible");
126: return label;
127: }
128:
129: /**
130: * Creates and returns a text label whose text value is bound
131: * to the <em>value</em> of the given <code>JFormattedTextField</code>.
132: * The value is converted to a String using the specified
133: * <code>format</code>.
134: *
135: * @param field the JFormattedTextField to be inspected
136: * @param format the Format used to convert values to Strings
137: * @return a text label that displays the field's value
138: */
139: private static JLabel createValueTextLabel(
140: JFormattedTextField field, Format format) {
141: PropertyAdapter<JFormattedTextField> valueAdapter = new PropertyAdapter<JFormattedTextField>(
142: field, "value", true);
143: return ExampleComponentFactory
144: .createLabel(valueAdapter, format);
145: }
146:
147: // Buttons ****************************************************************
148:
149: /**
150: * Appends a row with three buttons to the given builder.
151: * The buttons set valid, invalid and empty texts to the fields.
152: * Before the button bar, a gap is added that grows vertically.
153: *
154: * @param builder the builder used to append the button bar
155: */
156: static void appendButtonBar(DefaultFormBuilder builder,
157: List<? extends JTextComponent> textComponents,
158: String validText, String invalidText) {
159: Component buttonBar = ButtonBarFactory
160: .buildRightAlignedBar(new JButton[] {
161: createButton("Valid", textComponents, validText),
162: createButton("Invalid", textComponents,
163: invalidText),
164: createButton("Empty", textComponents, "") });
165: builder.appendRow("6dlu:grow");
166: builder.nextLine(2);
167: builder.append(buttonBar, 8);
168: }
169:
170: /**
171: * Creates and returns a button that sets a valid text to the fields.
172: *
173: * @param label the button's text label
174: * @param textComponents a List of text components to set a text
175: * @param text the text to be set
176: * @return a button that sets the specified text to the components
177: */
178: private static JButton createButton(String label,
179: List<? extends JTextComponent> textComponents, String text) {
180: return new JButton(new SetTextAction(label, textComponents,
181: text));
182: }
183:
184: /**
185: * An Action that sets a specified text to a list of given text components.
186: */
187: private static final class SetTextAction extends AbstractAction {
188:
189: /**
190: * The List of text components to be set a text.
191: */
192: private final List<? extends JTextComponent> textComponents;
193:
194: /**
195: * Used to set the field text.
196: */
197: private final String text;
198:
199: private SetTextAction(String label,
200: List<? extends JTextComponent> textComponents,
201: String text) {
202: super (label);
203: this .textComponents = textComponents;
204: this .text = text;
205: }
206:
207: public void actionPerformed(ActionEvent e) {
208: for (JTextComponent component : textComponents) {
209: component.setText(text);
210: }
211: }
212:
213: }
214:
215: }
|