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.Component;
034: import java.awt.KeyboardFocusManager;
035: import java.beans.PropertyChangeEvent;
036: import java.beans.PropertyChangeListener;
037:
038: import javax.swing.*;
039:
040: import com.jgoodies.binding.value.ValueHolder;
041: import com.jgoodies.forms.builder.PanelBuilder;
042: import com.jgoodies.forms.layout.CellConstraints;
043: import com.jgoodies.forms.layout.FormLayout;
044: import com.jgoodies.validation.tutorial.util.ExampleComponentFactory;
045: import com.jgoodies.validation.tutorial.util.TutorialUtils;
046: import com.jgoodies.validation.view.ValidationComponentUtils;
047: import com.jgoodies.validation.view.ValidationResultViewFactory;
048:
049: /**
050: * Demonstrates a style how to provide input hints,
051: * so that users can avoid entering invalid data.
052: *
053: * @author Karsten Lentzsch
054: * @version $Revision: 1.10 $
055: */
056: public final class InfoOnFocusExample {
057:
058: private JLabel infoLabel;
059: private JTextArea infoArea;
060: private JComponent infoAreaPane;
061:
062: private JTextField orderNoField;
063: private JTextField orderDateField;
064: private JTextField deliveryDateField;
065: private JTextArea deliveryNotesArea;
066:
067: public static void main(String[] args) {
068: try {
069: UIManager
070: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
071: } catch (Exception e) {
072: // Likely Plastic is not in the classpath; ignore it.
073: }
074: JFrame frame = new JFrame();
075: frame.setTitle("Basics :: Hints on Focus");
076: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
077: JComponent panel = new InfoOnFocusExample().buildPanel();
078: frame.getContentPane().add(panel);
079: frame.pack();
080: TutorialUtils.locateOnOpticalScreenCenter(frame);
081: frame.setVisible(true);
082: }
083:
084: // Component Creation and Initialization **********************************
085:
086: /**
087: * Creates and initializes the UI components.
088: */
089: private void initComponents() {
090: orderNoField = new JTextField();
091: orderDateField = ExampleComponentFactory
092: .createDateField(new ValueHolder());
093: deliveryDateField = ExampleComponentFactory
094: .createDateField(new ValueHolder());
095: deliveryNotesArea = new JTextArea();
096: }
097:
098: private void initComponentAnnotations() {
099: ValidationComponentUtils.setInputHint(orderNoField,
100: "Mandatory, length in [5, 10]");
101: ValidationComponentUtils.setInputHint(orderDateField,
102: "Mandatory, before delivery date");
103: ValidationComponentUtils.setInputHint(deliveryDateField,
104: "After delivery date");
105: ValidationComponentUtils.setInputHint(deliveryNotesArea,
106: "Length <= 30");
107: }
108:
109: private void initEventHandling() {
110: KeyboardFocusManager.getCurrentKeyboardFocusManager()
111: .addPropertyChangeListener(new FocusChangeHandler());
112: }
113:
114: // Building ***************************************************************
115:
116: /**
117: * Builds and returns the whole editor.
118: */
119: public JComponent buildPanel() {
120: initComponents();
121: initComponentAnnotations();
122: initEventHandling();
123:
124: FormLayout layout = new FormLayout("fill:default:grow",
125: "max(14dlu;pref), 6dlu, pref, 3dlu, pref");
126:
127: PanelBuilder builder = new PanelBuilder(layout);
128: builder.setDefaultDialogBorder();
129:
130: CellConstraints cc = new CellConstraints();
131: builder.add(buildInfoAreaPane(), cc.xy(1, 1));
132: builder.addSeparator("Order", cc.xy(1, 3));
133: builder.add(buildEditorPanel(), cc.xy(1, 5));
134: return builder.getPanel();
135: }
136:
137: private JComponent buildEditorPanel() {
138: FormLayout layout = new FormLayout(
139: "right:max(65dlu;pref), 4dlu, 44dlu, 2dlu, 44dlu, 90dlu:grow",
140: "p, 3dlu, p, 3dlu, p, 3dlu, p, 2px"); // extra bottom space for icons
141:
142: layout.setRowGroups(new int[][] { { 1, 3, 5, 7 } });
143: PanelBuilder builder = new PanelBuilder(layout);
144: CellConstraints cc = new CellConstraints();
145:
146: builder.addLabel("Order No", cc.xy(1, 1));
147: builder.add(orderNoField, cc.xyw(3, 1, 3));
148: builder.addLabel("Order-/Delivery Date", cc.xy(1, 3));
149: builder.add(orderDateField, cc.xy(3, 3));
150: builder.add(deliveryDateField, cc.xy(5, 3));
151: builder.addLabel("Notes", cc.xy(1, 5));
152: builder.add(new JScrollPane(deliveryNotesArea), cc.xywh(3, 5,
153: 4, 3));
154: return builder.getPanel();
155: }
156:
157: private JComponent buildInfoAreaPane() {
158: infoLabel = new JLabel(ValidationResultViewFactory
159: .getInfoIcon());
160: infoArea = new JTextArea(1, 38);
161: infoArea.setEditable(false);
162: infoArea.setOpaque(false);
163:
164: FormLayout layout = new FormLayout("pref, 2dlu, default",
165: "pref");
166: PanelBuilder builder = new PanelBuilder(layout);
167: CellConstraints cc = new CellConstraints();
168: builder.add(infoLabel, cc.xy(1, 1));
169: builder.add(infoArea, cc.xy(3, 1));
170:
171: infoAreaPane = builder.getPanel();
172: infoAreaPane.setVisible(false);
173: return infoAreaPane;
174: }
175:
176: // Validation Handler *****************************************************
177:
178: /**
179: * Displays an input hint for components that get the focus permanently.
180: */
181: private final class FocusChangeHandler implements
182: PropertyChangeListener {
183:
184: public void propertyChange(PropertyChangeEvent evt) {
185: String propertyName = evt.getPropertyName();
186: if (!"permanentFocusOwner".equals(propertyName))
187: return;
188:
189: Component focusOwner = KeyboardFocusManager
190: .getCurrentKeyboardFocusManager().getFocusOwner();
191:
192: String focusHint = (focusOwner instanceof JComponent) ? (String) ValidationComponentUtils
193: .getInputHint((JComponent) focusOwner)
194: : null;
195:
196: infoArea.setText(focusHint);
197: infoAreaPane.setVisible(focusHint != null);
198: }
199: }
200:
201: }
|