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.basics;
032:
033: import java.awt.event.ActionEvent;
034: import java.text.DateFormat;
035: import java.util.Date;
036:
037: import javax.swing.*;
038:
039: import com.jgoodies.forms.builder.DefaultFormBuilder;
040: import com.jgoodies.forms.builder.PanelBuilder;
041: import com.jgoodies.forms.factories.ButtonBarFactory;
042: import com.jgoodies.forms.layout.CellConstraints;
043: import com.jgoodies.forms.layout.FormLayout;
044: import com.jgoodies.forms.layout.RowSpec;
045: import com.jgoodies.validation.ValidationResult;
046: import com.jgoodies.validation.tutorial.shared.Order;
047: import com.jgoodies.validation.tutorial.util.TutorialUtils;
048: import com.jgoodies.validation.tutorial.validator.OrderValidator;
049:
050: /**
051: * A simple validation example that uses a "copying" binding,
052: * not the JGoodies Binding. The domain Order object is validated
053: * using an OrderValidator. On OK pressed, a modal dialog shows
054: * the validation errors and warnings - if any.
055: *
056: * @author Karsten Lentzsch
057: * @version $Revision: 1.6 $
058: */
059: public final class SimpleDomainValidationExample {
060:
061: private Order order;
062:
063: private JTextField orderNoField;
064: private JFormattedTextField orderDateField;
065: private JFormattedTextField deliveryDateField;
066: private JTextArea deliveryNotesArea;
067: private JButton okButton;
068: private JButton closeButton;
069:
070: /**
071: * Launches this example: creates a JFrame, adds the built
072: * example panel, packs the frame and finally makes it visible.
073: *
074: * @param args runtime arguments (ignored)
075: */
076: public static void main(String[] args) {
077: try {
078: UIManager
079: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
080: } catch (Exception e) {
081: // Likely Plastic is not in the classpath; ignore it.
082: }
083: JFrame frame = new JFrame();
084: frame.setTitle("Basic :: Simple Domain Validation");
085: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
086: JComponent panel = new SimpleDomainValidationExample()
087: .buildPanel();
088: frame.getContentPane().add(panel);
089: frame.pack();
090: TutorialUtils.locateOnOpticalScreenCenter(frame);
091: frame.setVisible(true);
092: }
093:
094: // Instance Creation ******************************************************
095:
096: private void init() {
097: order = new Order();
098: }
099:
100: // Component Creation and Initialization **********************************
101:
102: /**
103: * Creates and initializes the UI components.
104: */
105: protected void initComponents() {
106: orderNoField = new JTextField();
107: DateFormat dateFormat = DateFormat
108: .getDateInstance(DateFormat.SHORT);
109: orderDateField = new JFormattedTextField(dateFormat);
110: deliveryDateField = new JFormattedTextField(dateFormat);
111: deliveryNotesArea = new JTextArea();
112:
113: okButton = new JButton(new OKAction());
114: closeButton = new JButton(TutorialUtils.getCloseAction());
115: }
116:
117: // Building ***************************************************************
118:
119: /**
120: * Builds the whole editor.
121: *
122: * @return the editor panel with a report area at the bottom
123: */
124: public JComponent buildPanel() {
125: init();
126: initComponents();
127: updateView();
128: return buildPanelWithReportInBottom();
129: }
130:
131: /**
132: * Builds the whole editor.
133: *
134: * @return the editor panel with a report area at the bottom
135: */
136: private JComponent buildPanelWithReportInBottom() {
137: FormLayout layout = new FormLayout("fill:default:grow",
138: "pref, 6dlu:grow, pref");
139:
140: PanelBuilder builder = new PanelBuilder(layout);
141: builder.setDefaultDialogBorder();
142:
143: CellConstraints cc = new CellConstraints();
144: builder.add(buildEditorPanel(), cc.xy(1, 1));
145: builder.add(buildButtonBar(), cc.xy(1, 3));
146: return builder.getPanel();
147: }
148:
149: private JComponent buildEditorPanel() {
150: FormLayout layout = new FormLayout(
151: "right:pref, 4dlu, 40dlu, 2dlu, 40dlu, 82dlu:grow");
152:
153: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
154: builder.setRowGroupingEnabled(true);
155:
156: CellConstraints cc = new CellConstraints();
157:
158: builder.appendSeparator("Order");
159: builder.append("Order No", orderNoField, 3);
160: builder.append("Order-/Delivery Date", orderDateField,
161: deliveryDateField);
162: builder.nextLine();
163:
164: builder.append("Notes");
165: builder.appendRow(new RowSpec("17dlu")); // Assumes line is 14, gap is 3
166: builder.add(new JScrollPane(deliveryNotesArea), cc.xywh(builder
167: .getColumn(), builder.getRow(), 4, 2));
168:
169: // Append a small gap at the bottom so the overlay icon is visible
170: builder.nextLine();
171: builder.appendRow(new RowSpec("2px"));
172:
173: return builder.getPanel();
174: }
175:
176: private JComponent buildButtonBar() {
177: return ButtonBarFactory.buildOKCancelBar(okButton, closeButton);
178: }
179:
180: // Synchronizing Model and View *******************************************
181:
182: private void updateView() {
183: orderNoField.setText(getOrder().getOrderNo());
184: orderDateField.setValue(getOrder().getOrderDate());
185: deliveryDateField.setValue(getOrder().getDeliveryDate());
186: deliveryNotesArea.setText(getOrder().getDeliveryNotes());
187: }
188:
189: private void updateModel() {
190: getOrder().setOrderNo(orderNoField.getText());
191: getOrder().setOrderDate((Date) orderDateField.getValue());
192: getOrder().setDeliveryDate((Date) deliveryDateField.getValue());
193: getOrder().setDeliveryNotes(deliveryNotesArea.getText());
194: }
195:
196: private Order getOrder() {
197: return order;
198: }
199:
200: // Event Handling *********************************************************
201:
202: /**
203: * Validates the GUI state and updates the model only,
204: * if the GUI state has no errors (it may have warnings).
205: */
206: private final class OKAction extends AbstractAction {
207:
208: OKAction() {
209: super ("OK");
210: }
211:
212: public void actionPerformed(ActionEvent e) {
213: updateModel();
214: ValidationResult validationResult = new OrderValidator()
215: .validate(order);
216: if (validationResult.hasErrors()) {
217: TutorialUtils.showValidationMessage(e,
218: "To save the order, fix the following errors:",
219: validationResult);
220: return;
221: }
222: if (validationResult.hasWarnings()) {
223: TutorialUtils.showValidationMessage(e,
224: "Note: some order fields are invalid.",
225: validationResult);
226: }
227: System.out.println("Data saved");
228: }
229:
230: }
231:
232: }
|