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 javax.swing.*;
034:
035: import com.jgoodies.forms.builder.PanelBuilder;
036: import com.jgoodies.forms.layout.CellConstraints;
037: import com.jgoodies.forms.layout.FormLayout;
038: import com.jgoodies.validation.tutorial.util.TutorialUtils;
039: import com.jgoodies.validation.view.ValidationComponentUtils;
040:
041: /**
042: * Demonstrates different styles how to mark mandatory fields.
043: * The fields in this demo are not bound and not validated.
044: *
045: * @author Karsten Lentzsch
046: * @version $Revision: 1.9 $
047: */
048: public final class MandatoryMarkerExample {
049:
050: private JLabel orderNoLabel;
051: private JLabel orderDateLabel;
052:
053: private JTextField backgroundOrderNoField;
054: private JTextField backgroundOrderDateField;
055: private JTextField backgroundDeliveryDateField;
056:
057: private JTextField borderOrderNoField;
058: private JTextField borderOrderDateField;
059: private JTextField borderDeliveryDateField;
060:
061: public static void main(String[] args) {
062: try {
063: UIManager
064: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
065: } catch (Exception e) {
066: // Likely Plastic is not in the classpath; ignore it.
067: }
068: JFrame frame = new JFrame();
069: frame.setTitle("Basics :: Mandatory Marker");
070: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
071: JComponent panel = new MandatoryMarkerExample().buildPanel();
072: frame.getContentPane().add(panel);
073: frame.pack();
074: TutorialUtils.locateOnOpticalScreenCenter(frame);
075: frame.setVisible(true);
076: }
077:
078: // Component Creation and Initialization **********************************
079:
080: private void initComponents() {
081: orderNoLabel = createMandatoryLabel("Order No");
082: orderDateLabel = createMandatoryLabel("Order-");
083:
084: backgroundOrderNoField = new JTextField();
085: backgroundOrderDateField = new JTextField();
086: backgroundDeliveryDateField = new JTextField();
087:
088: borderOrderNoField = new JTextField();
089: borderOrderDateField = new JTextField();
090: borderDeliveryDateField = new JTextField();
091: }
092:
093: private void initComponentAnnotations() {
094: ValidationComponentUtils.setMandatory(backgroundOrderNoField,
095: true);
096: ValidationComponentUtils.setMandatory(backgroundOrderDateField,
097: true);
098:
099: ValidationComponentUtils.setMandatory(borderOrderNoField, true);
100: ValidationComponentUtils.setMandatory(borderOrderDateField,
101: true);
102: }
103:
104: // Building ***************************************************************
105:
106: /**
107: * Builds and returns the whole editor with 3 sections
108: * for the different validation times.
109: */
110: public JComponent buildPanel() {
111: initComponents();
112: initComponentAnnotations();
113:
114: FormLayout layout = new FormLayout("fill:pref:grow",
115: "p, 3dlu, p, 17dlu, p, 3dlu, p, 17dlu, p, 3dlu, p, 17dlu, p, 3dlu, p");
116:
117: PanelBuilder builder = new PanelBuilder(layout);
118: builder.setDefaultDialogBorder();
119: CellConstraints cc = new CellConstraints();
120:
121: builder.addSeparator("Label with *", cc.xy(1, 1));
122: builder.add(buildLabelWithStarPanel(), cc.xy(1, 3));
123: builder.addSeparator("Label Foreground", cc.xy(1, 5));
124: builder.add(buildLabelForegroundPanel(), cc.xy(1, 7));
125: builder.addSeparator("Component Background", cc.xy(1, 9));
126: builder.add(buildComponentBackgroundPanel(), cc.xy(1, 11));
127: builder.addSeparator("Component Border", cc.xy(1, 13));
128: builder.add(buildComponentBorderPanel(), cc.xy(1, 15));
129: return builder.getPanel();
130: }
131:
132: private JComponent buildLabelWithStarPanel() {
133: FormLayout layout = new FormLayout(
134: "right:max(65dlu;pref), 4dlu, 44dlu, 2dlu, 44dlu",
135: "p, 3dlu, p");
136:
137: PanelBuilder builder = new PanelBuilder(layout);
138: CellConstraints cc = new CellConstraints();
139:
140: builder.addLabel("Order No*", cc.xy(1, 1));
141: builder.add(new JTextField(), cc.xyw(3, 1, 3));
142: builder.addLabel("Order-*/Delivery Date", cc.xy(1, 3));
143: builder.add(new JTextField(), cc.xy(3, 3));
144: builder.add(new JTextField(), cc.xy(5, 3));
145: return builder.getPanel();
146: }
147:
148: private JComponent buildLabelForegroundPanel() {
149: FormLayout layout = new FormLayout(
150: "right:max(65dlu;pref), 4dlu, 44dlu, 2dlu, 44dlu",
151: "p, 3dlu, p");
152:
153: PanelBuilder builder = new PanelBuilder(layout);
154: CellConstraints cc = new CellConstraints();
155:
156: builder.add(orderNoLabel, cc.xy(1, 1));
157: builder.add(new JTextField(), cc.xyw(3, 1, 3));
158: builder.add(buildDateLabelBar(), cc.xy(1, 3));
159: builder.add(new JTextField(), cc.xy(3, 3));
160: builder.add(new JTextField(), cc.xy(5, 3));
161: return builder.getPanel();
162: }
163:
164: private JComponent buildDateLabelBar() {
165: FormLayout layout = new FormLayout("pref, pref", "pref");
166: PanelBuilder builder = new PanelBuilder(layout);
167: builder.add(orderDateLabel);
168: builder.nextColumn();
169: builder.addLabel("/Delivery Date");
170: return builder.getPanel();
171: }
172:
173: private JComponent buildComponentBackgroundPanel() {
174: FormLayout layout = new FormLayout(
175: "right:max(65dlu;pref), 4dlu, 44dlu, 2dlu, 44dlu",
176: "p, 3dlu, p");
177:
178: PanelBuilder builder = new PanelBuilder(layout);
179: CellConstraints cc = new CellConstraints();
180:
181: builder.addLabel("Order No", cc.xy(1, 1));
182: builder.add(backgroundOrderNoField, cc.xyw(3, 1, 3));
183: builder.addLabel("Order-/Delivery Date", cc.xy(1, 3));
184: builder.add(backgroundOrderDateField, cc.xy(3, 3));
185: builder.add(backgroundDeliveryDateField, cc.xy(5, 3));
186:
187: ValidationComponentUtils
188: .updateComponentTreeMandatoryBackground(builder
189: .getPanel());
190:
191: return builder.getPanel();
192: }
193:
194: private JComponent buildComponentBorderPanel() {
195: FormLayout layout = new FormLayout(
196: "right:max(65dlu;pref), 4dlu, 44dlu, 2dlu, 44dlu",
197: "p, 3dlu, p");
198:
199: PanelBuilder builder = new PanelBuilder(layout);
200: CellConstraints cc = new CellConstraints();
201:
202: builder.addLabel("Order No", cc.xy(1, 1));
203: builder.add(borderOrderNoField, cc.xyw(3, 1, 3));
204: builder.addLabel("Order-/Delivery Date", cc.xy(1, 3));
205: builder.add(borderOrderDateField, cc.xy(3, 3));
206: builder.add(borderDeliveryDateField, cc.xy(5, 3));
207:
208: ValidationComponentUtils
209: .updateComponentTreeMandatoryBorder(builder.getPanel());
210:
211: return builder.getPanel();
212: }
213:
214: // Helper Code ************************************************************
215:
216: private static JLabel createMandatoryLabel(String text) {
217: JLabel label = new JLabel(text);
218: label.setForeground(ValidationComponentUtils
219: .getMandatoryForeground());
220: return label;
221: }
222:
223: }
|