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.Severity;
042: import com.jgoodies.validation.ValidationResult;
043: import com.jgoodies.validation.ValidationResultModel;
044: import com.jgoodies.validation.message.SimpleValidationMessage;
045: import com.jgoodies.validation.tutorial.util.IconFeedbackPanel;
046: import com.jgoodies.validation.tutorial.util.TutorialUtils;
047: import com.jgoodies.validation.util.DefaultValidationResultModel;
048: import com.jgoodies.validation.view.ValidationComponentUtils;
049: import com.jgoodies.validation.view.ValidationResultViewFactory;
050:
051: /**
052: * Demonstrates how to associate validation messages with UI components.
053: * Provides buttons to set a couple of valid and invalid results.
054: *
055: * @author Karsten Lentzsch
056: * @version $Revision: 1.2 $
057: */
058: public final class ValidationMessageKeysExample {
059:
060: private static final Object KEY1 = "key1";
061: private static final Object KEY2 = "key2";
062:
063: private final ValidationResultModel resultModel;
064:
065: private JComponent noKeyField;
066: private JComponent key1Field;
067: private JComponent key2Field;
068: private JComponent key1Key2Field;
069:
070: private JButton validButton;
071: private JButton errorsButton;
072: private JButton warningsButton;
073: private JButton mixedButton;
074:
075: public static void main(String[] args) {
076: try {
077: UIManager
078: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
079: } catch (Exception e) {
080: // Likely Plastic is not in the classpath; ignore it.
081: }
082: JFrame frame = new JFrame();
083: frame.setTitle("Basics :: Validation Message Keys");
084: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
085: JComponent panel = new ValidationMessageKeysExample()
086: .buildPanel();
087: frame.getContentPane().add(panel);
088: frame.pack();
089: TutorialUtils.locateOnOpticalScreenCenter(frame);
090: frame.setVisible(true);
091: }
092:
093: // Instance Creation ******************************************************
094:
095: public ValidationMessageKeysExample() {
096: resultModel = new DefaultValidationResultModel();
097: resultModel.setResult(createResult1());
098: }
099:
100: // Component Creation and Initialization **********************************
101:
102: private void initComponents() {
103: noKeyField = new JTextField();
104: key1Field = new JTextField();
105: key2Field = new JTextField();
106: key1Key2Field = new JTextField();
107:
108: validButton = new JButton(new SetResultAction("Empty",
109: ValidationResult.EMPTY));
110: errorsButton = new JButton(new SetResultAction("1 Only",
111: createResult1()));
112: warningsButton = new JButton(new SetResultAction("2 Only",
113: createResult2()));
114: mixedButton = new JButton(new SetResultAction("1 & 2",
115: createResult3()));
116: }
117:
118: private void initComponentAnnotations() {
119: ValidationComponentUtils.setMessageKey(key1Field, KEY1);
120: ValidationComponentUtils.setMessageKey(key2Field, KEY2);
121: ValidationComponentUtils.setMessageKeys(key1Key2Field,
122: new Object[] { KEY1, KEY2 });
123: }
124:
125: // Building ***************************************************************
126:
127: /**
128: * Builds and returns the whole editor.
129: */
130: public JComponent buildPanel() {
131: initComponents();
132: initComponentAnnotations();
133:
134: FormLayout layout = new FormLayout(
135: "right:max(65dlu;pref), 4dlu, 160dlu:grow",
136: "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu:grow, p");
137:
138: PanelBuilder builder = new PanelBuilder(layout);
139: builder.setDefaultDialogBorder();
140: CellConstraints cc = new CellConstraints();
141:
142: builder.addLabel("No key", cc.xy(1, 1));
143: builder.add(noKeyField, cc.xy(3, 1));
144: builder.addLabel("Key1", cc.xy(1, 3));
145: builder.add(key1Field, cc.xy(3, 3));
146: builder.addLabel("Key2", cc.xy(1, 5));
147: builder.add(key2Field, cc.xy(3, 5));
148: builder.addLabel("Key1, Key2", cc.xy(1, 7));
149: builder.add(key1Key2Field, cc.xy(3, 7));
150:
151: builder.addLabel("List", cc.xy(1, 9, "r, t"));
152: builder.add(ValidationResultViewFactory
153: .createReportList(resultModel), cc.xy(3, 9));
154:
155: builder.add(buildButtonBar(), cc.xyw(1, 11, 3));
156:
157: return new IconFeedbackPanel(resultModel, builder.getPanel());
158: }
159:
160: private JComponent buildButtonBar() {
161: return ButtonBarFactory.buildRightAlignedBar(validButton,
162: errorsButton, warningsButton, mixedButton);
163: }
164:
165: // Helper Code ************************************************************
166:
167: /**
168: * Sets the given ValidationResult as the resultModel's result
169: * when performed. Used to create the 4 command buttons.
170: */
171: private final class SetResultAction extends AbstractAction {
172:
173: private final ValidationResult result;
174:
175: SetResultAction(String name, ValidationResult result) {
176: super (name);
177: this .result = result;
178: }
179:
180: public void actionPerformed(ActionEvent e) {
181: resultModel.setResult(result);
182: }
183:
184: }
185:
186: private static ValidationResult createResult1() {
187: ValidationResult result = new ValidationResult();
188: result.add(new SimpleValidationMessage("Message1 for key1",
189: Severity.ERROR, KEY1));
190: result.add(new SimpleValidationMessage("Message2 for key1",
191: Severity.WARNING, KEY1));
192: return result;
193: }
194:
195: private static ValidationResult createResult2() {
196: ValidationResult result = new ValidationResult();
197: result.add(new SimpleValidationMessage("Message1 for key2",
198: Severity.ERROR, KEY2));
199: result.add(new SimpleValidationMessage("Message2 for key2",
200: Severity.WARNING, KEY2));
201: return result;
202: }
203:
204: private static ValidationResult createResult3() {
205: ValidationResult result = new ValidationResult();
206: result.add(new SimpleValidationMessage("Message1 for key1",
207: Severity.ERROR, KEY1));
208: result.add(new SimpleValidationMessage("Message2 for key1",
209: Severity.WARNING, KEY1));
210: result.add(new SimpleValidationMessage("Message1 for key2",
211: Severity.ERROR, KEY2));
212: result.add(new SimpleValidationMessage("Message2 for key2",
213: Severity.WARNING, KEY2));
214: return result;
215: }
216:
217: }
|