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:
035: import javax.swing.*;
036:
037: import com.jgoodies.binding.PresentationModel;
038: import com.jgoodies.forms.builder.PanelBuilder;
039: import com.jgoodies.forms.factories.ButtonBarFactory;
040: import com.jgoodies.forms.layout.CellConstraints;
041: import com.jgoodies.forms.layout.FormLayout;
042: import com.jgoodies.validation.ValidationResult;
043: import com.jgoodies.validation.ValidationResultModel;
044: import com.jgoodies.validation.tutorial.shared.Order;
045: import com.jgoodies.validation.tutorial.shared.OrderModel;
046: import com.jgoodies.validation.tutorial.util.ExampleComponentFactory;
047: import com.jgoodies.validation.tutorial.util.TutorialUtils;
048: import com.jgoodies.validation.tutorial.validator.OrderValidator;
049: import com.jgoodies.validation.util.DefaultValidationResultModel;
050: import com.jgoodies.validation.view.ValidationResultViewFactory;
051:
052: /**
053: * Demonstrates and compares different validation times:
054: * key-typed, focus lost, commit (OK pressed).
055: *
056: * @author Karsten Lentzsch
057: * @version $Revision: 1.14 $
058: */
059:
060: public final class ValidationTimeExample {
061:
062: private final OrderModel keyTypedModel;
063: private JTextField keyTypedOrderNoField;
064: private JTextField keyTypedOrderDateField;
065: private JComponent keyTypedResultView;
066:
067: private final OrderModel focusLostModel;
068: private JTextField focusLostOrderNoField;
069: private JTextField focusLostOrderDateField;
070: private JComponent focusLostResultView;
071:
072: private final CommitOrderModel commitModel;
073: private JTextField commitOrderNoField;
074: private JTextField commitOrderDateField;
075: private JComponent commitResultView;
076: private JButton commitOKButton;
077: private JButton commitCancelButton;
078:
079: public static void main(String[] args) {
080: try {
081: UIManager
082: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
083: } catch (Exception e) {
084: // Likely Plastic is not in the classpath; ignore it.
085: }
086: JFrame frame = new JFrame();
087: frame.setTitle("Basics :: Validation Time");
088: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
089: JComponent panel = new ValidationTimeExample().buildPanel();
090: frame.getContentPane().add(panel);
091: frame.pack();
092: TutorialUtils.locateOnOpticalScreenCenter(frame);
093: frame.setVisible(true);
094: }
095:
096: // Instance Creation ******************************************************
097:
098: public ValidationTimeExample() {
099: keyTypedModel = new OrderModel(new Order());
100: focusLostModel = new OrderModel(new Order());
101: commitModel = new CommitOrderModel(new Order());
102: }
103:
104: // Component Creation and Initialization **********************************
105:
106: private void initComponents() {
107: // To commit on key typed we invoke #createTextField
108: // with the optional boolean parameter set to false.
109: keyTypedOrderNoField = ExampleComponentFactory.createTextField(
110: keyTypedModel.getModel(Order.PROPERTYNAME_ORDER_NO),
111: false);
112: // We can't commit Dates on key typed, only on valid edit.
113: keyTypedOrderDateField = ExampleComponentFactory
114: .createDateField(keyTypedModel
115: .getModel(Order.PROPERTYNAME_ORDER_DATE), true,
116: true);
117: keyTypedResultView = ValidationResultViewFactory
118: .createReportIconAndTextPane(keyTypedModel
119: .getValidationResultModel());
120:
121: // By default #createTextField commits on focus lost
122: focusLostOrderNoField = ExampleComponentFactory
123: .createTextField(focusLostModel
124: .getModel(Order.PROPERTYNAME_ORDER_NO));
125: focusLostOrderDateField = ExampleComponentFactory
126: .createDateField(focusLostModel
127: .getModel(Order.PROPERTYNAME_ORDER_DATE));
128: focusLostResultView = ValidationResultViewFactory
129: .createReportIconAndTextPane(focusLostModel
130: .getValidationResultModel());
131:
132: // Here we delay commits by using the Binding buffering support.
133: commitOrderNoField = ExampleComponentFactory
134: .createTextField(commitModel
135: .getBufferedModel(Order.PROPERTYNAME_ORDER_NO));
136: commitOrderDateField = ExampleComponentFactory
137: .createDateField(commitModel
138: .getBufferedModel(Order.PROPERTYNAME_ORDER_DATE));
139: commitResultView = ValidationResultViewFactory
140: .createReportIconAndTextPane(commitModel
141: .getValidationResultModel());
142: commitOKButton = new JButton(commitModel.getOKAction());
143: commitCancelButton = new JButton(commitModel.getCancelAction());
144: }
145:
146: // Building ***************************************************************
147:
148: /**
149: * Builds and returns the whole editor with 3 sections
150: * for the different validation times.
151: */
152: public JComponent buildPanel() {
153: initComponents();
154:
155: FormLayout layout = new FormLayout("fill:pref:grow",
156: "p, 3dlu, p, 6dlu, p, 3dlu, p, 6dlu, p, 3dlu, p");
157:
158: PanelBuilder builder = new PanelBuilder(layout);
159: builder.setDefaultDialogBorder();
160: CellConstraints cc = new CellConstraints();
161:
162: builder.addSeparator("Validate on Key Typed", cc.xy(1, 1));
163: builder.add(buildKeyTypedPanel(), cc.xy(1, 3));
164: builder.addSeparator("Validate on Focus Lost", cc.xy(1, 5));
165: builder.add(buildFocusLostPanel(), cc.xy(1, 7));
166: builder.addSeparator("Validate on Commit (OK)", cc.xy(1, 9));
167: builder.add(buildCommitPanel(), cc.xy(1, 11));
168: return builder.getPanel();
169: }
170:
171: private JComponent buildKeyTypedPanel() {
172: FormLayout layout = new FormLayout(
173: "right:max(65dlu;pref), 4dlu, 44dlu, 2dlu, 44dlu, 90dlu:grow",
174: "p, 3dlu, p, 9dlu, fill:max(22dlu;p)");
175:
176: PanelBuilder builder = new PanelBuilder(layout);
177: CellConstraints cc = new CellConstraints();
178:
179: builder.addLabel("Order No", cc.xy(1, 1));
180: builder.add(keyTypedOrderNoField, cc.xyw(3, 1, 3));
181: builder.addLabel("Order Date", cc.xy(1, 3));
182: builder.add(keyTypedOrderDateField, cc.xy(3, 3));
183: builder.addLabel("(commits on valid edit)", cc.xyw(5, 3, 2));
184: builder.add(keyTypedResultView, cc.xyw(3, 5, 4));
185: return builder.getPanel();
186: }
187:
188: private JComponent buildFocusLostPanel() {
189: FormLayout layout = new FormLayout(
190: "right:max(65dlu;pref), 4dlu, 44dlu, 2dlu, 44dlu, 90dlu:grow",
191: "p, 3dlu, p, 9dlu, fill:max(22dlu;p)");
192:
193: PanelBuilder builder = new PanelBuilder(layout);
194: CellConstraints cc = new CellConstraints();
195:
196: builder.addLabel("Order No", cc.xy(1, 1));
197: builder.add(focusLostOrderNoField, cc.xyw(3, 1, 3));
198: builder.addLabel("Order Date", cc.xy(1, 3));
199: builder.add(focusLostOrderDateField, cc.xy(3, 3));
200: builder.add(focusLostResultView, cc.xyw(3, 5, 4));
201: return builder.getPanel();
202: }
203:
204: private JComponent buildCommitPanel() {
205: FormLayout layout = new FormLayout(
206: "right:max(65dlu;pref), 4dlu, 44dlu, 2dlu, 44dlu, 90dlu:grow",
207: "p, 3dlu, p, 9dlu, fill:max(22dlu;p), 2dlu:grow, p");
208:
209: PanelBuilder builder = new PanelBuilder(layout);
210: CellConstraints cc = new CellConstraints();
211:
212: builder.addLabel("Order No", cc.xy(1, 1));
213: builder.add(commitOrderNoField, cc.xyw(3, 1, 3));
214: builder.addLabel("Order Date", cc.xy(1, 3));
215: builder.add(commitOrderDateField, cc.xy(3, 3));
216: builder.add(commitResultView, cc.xyw(3, 5, 4));
217: builder.add(buildCommitButtonBar(), cc.xyw(3, 7, 4));
218: return builder.getPanel();
219: }
220:
221: private JComponent buildCommitButtonBar() {
222: return ButtonBarFactory.buildRightAlignedBar(commitOKButton,
223: commitCancelButton);
224: }
225:
226: // Domain Classes *********************************************************
227:
228: /**
229: * Provides the validation result model and actions necessary to
230: * build a presentation.
231: */
232: private static final class CommitOrderModel extends
233: PresentationModel<Order> {
234:
235: private final ValidationResultModel validationResultModel;
236: private final Action okAction;
237: private final Action cancelAction;
238:
239: // Instance Creation -------------------------------------
240:
241: CommitOrderModel(Order order) {
242: super (order);
243: validationResultModel = new DefaultValidationResultModel();
244: okAction = new OKAction();
245: cancelAction = new CancelAction();
246: }
247:
248: // Exposing Models -------------------------------------------
249:
250: ValidationResultModel getValidationResultModel() {
251: return validationResultModel;
252: }
253:
254: Action getOKAction() {
255: return okAction;
256: }
257:
258: Action getCancelAction() {
259: return cancelAction;
260: }
261:
262: // Validation ------------------------------------------------
263:
264: private void updateValidationResult() {
265: Order order = getBean();
266: ValidationResult result = new OrderValidator()
267: .validate(order);
268: validationResultModel.setResult(result);
269: }
270:
271: // -------------------------------------------------------
272:
273: /**
274: * Commits buffered changes and updates the validation result.
275: */
276: private final class OKAction extends AbstractAction {
277:
278: private OKAction() {
279: super ("OK");
280: }
281:
282: public void actionPerformed(ActionEvent evt) {
283: triggerCommit();
284: updateValidationResult();
285: }
286: }
287:
288: /**
289: * Flushes buffered changes and updates the validation result.
290: */
291: private final class CancelAction extends AbstractAction {
292:
293: private CancelAction() {
294: super ("Cancel");
295: }
296:
297: public void actionPerformed(ActionEvent evt) {
298: triggerFlush();
299: updateValidationResult();
300: }
301: }
302:
303: }
304:
305: }
|