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.forms.builder.PanelBuilder;
038: import com.jgoodies.forms.factories.ButtonBarFactory;
039: import com.jgoodies.forms.layout.CellConstraints;
040: import com.jgoodies.forms.layout.FormLayout;
041: import com.jgoodies.validation.ValidationResult;
042: import com.jgoodies.validation.ValidationResultModel;
043: import com.jgoodies.validation.tutorial.util.TutorialUtils;
044: import com.jgoodies.validation.util.DefaultValidationResultModel;
045: import com.jgoodies.validation.view.ValidationResultViewFactory;
046:
047: /**
048: * Demonstrates the different validation result views created by the
049: * {@link com.jgoodies.validation.view.ValidationResultViewFactory}.
050: * Provides buttons to set a couple of valid and invalid results.
051: *
052: * @author Karsten Lentzsch
053: * @version $Revision: 1.12 $
054: */
055:
056: public final class ValidationResultViewExample {
057:
058: private final ValidationResultModel resultModel;
059:
060: private JButton validButton;
061: private JButton errorsButton;
062: private JButton warningsButton;
063: private JButton mixedButton;
064:
065: public static void main(String[] args) {
066: try {
067: UIManager
068: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
069: } catch (Exception e) {
070: // Likely Plastic is not in the classpath; ignore it.
071: }
072: JFrame frame = new JFrame();
073: frame.setTitle("Basics :: Validation Result Views");
074: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
075: JComponent panel = new ValidationResultViewExample()
076: .buildPanel();
077: frame.getContentPane().add(panel);
078: frame.pack();
079: TutorialUtils.locateOnOpticalScreenCenter(frame);
080: frame.setVisible(true);
081: }
082:
083: // Instance Creation ******************************************************
084:
085: public ValidationResultViewExample() {
086: resultModel = new DefaultValidationResultModel();
087: resultModel.setResult(TutorialUtils.getMixedResult());
088: }
089:
090: // Component Creation and Initialization **********************************
091:
092: private void initComponents() {
093: validButton = new JButton(new SetResultAction("Valid",
094: ValidationResult.EMPTY));
095: errorsButton = new JButton(new SetResultAction("Errors",
096: TutorialUtils.getErrorsResult()));
097: warningsButton = new JButton(new SetResultAction("Warnings",
098: TutorialUtils.getWarningsResult()));
099: mixedButton = new JButton(new SetResultAction("Mixed",
100: TutorialUtils.getMixedResult()));
101: }
102:
103: // Building ***************************************************************
104:
105: /**
106: * Builds and returns the whole editor with 3 sections
107: * for the different validation times.
108: */
109: public JComponent buildPanel() {
110: initComponents();
111:
112: FormLayout layout = new FormLayout(
113: "right:max(65dlu;pref), 4dlu, 160dlu:grow",
114: "p, 9dlu, p, 9dlu, fill:max(34dlu;p), 9dlu, fill:34dlu, 9dlu, fill:34dlu, 9dlu:grow, p");
115:
116: PanelBuilder builder = new PanelBuilder(layout);
117: builder.setDefaultDialogBorder();
118: CellConstraints cc = new CellConstraints();
119:
120: builder.addLabel("Icon Summary", cc.xy(1, 1));
121: builder.add(ValidationResultViewFactory
122: .createReportIconLabel(resultModel), cc.xy(3, 1));
123:
124: builder.addLabel("Icon and Text Summary", cc.xy(1, 3));
125: builder
126: .add(ValidationResultViewFactory
127: .createReportIconAndTextLabel(resultModel), cc
128: .xy(3, 3));
129:
130: builder.addLabel("Texts and Summary Icon", cc.xy(1, 5, "r, t"));
131: builder.add(ValidationResultViewFactory
132: .createReportIconAndTextPane(resultModel), cc.xy(3, 5));
133:
134: builder.addLabel("List", cc.xy(1, 7, "r, t"));
135: builder.add(ValidationResultViewFactory
136: .createReportList(resultModel), cc.xy(3, 7));
137:
138: builder.addLabel("Text Pane", cc.xy(1, 9, "r, t"));
139: builder.add(ValidationResultViewFactory
140: .createReportTextPane(resultModel), cc.xy(3, 9));
141:
142: builder.add(buildButtonBar(), cc.xyw(1, 11, 3));
143: return builder.getPanel();
144: }
145:
146: private JComponent buildButtonBar() {
147: return ButtonBarFactory.buildRightAlignedBar(validButton,
148: errorsButton, warningsButton, mixedButton);
149: }
150:
151: // Helper Code ************************************************************
152:
153: /**
154: * Sets the given ValidationResult as the resultModel's result
155: * when performed. Used to create the 4 command buttons.
156: */
157: private final class SetResultAction extends AbstractAction {
158:
159: private final ValidationResult result;
160:
161: SetResultAction(String name, ValidationResult result) {
162: super (name);
163: this .result = result;
164: }
165:
166: public void actionPerformed(ActionEvent e) {
167: resultModel.setResult(result);
168: }
169:
170: }
171:
172: }
|