001: /*
002: * Copyright (c) 2002-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.binding.tutorial.basics;
032:
033: import java.awt.event.ActionEvent;
034:
035: import javax.swing.*;
036: import javax.swing.text.JTextComponent;
037:
038: import com.jgoodies.binding.PresentationModel;
039: import com.jgoodies.binding.adapter.BasicComponentFactory;
040: import com.jgoodies.binding.beans.Model;
041: import com.jgoodies.binding.tutorial.TutorialUtils;
042: import com.jgoodies.forms.builder.PanelBuilder;
043: import com.jgoodies.forms.factories.ButtonBarFactory;
044: import com.jgoodies.forms.layout.CellConstraints;
045: import com.jgoodies.forms.layout.FormLayout;
046:
047: /**
048: * Demonstrates three different styles when to commit changes:
049: * on key typed, on focus lost, on OK/Apply pressed.
050: * Therefore we bind 3 JTextFields to 3 String typed ValueModels
051: * that honor the commit style. And we bind 3 JLabels directly
052: * to these ValueModels that display the current value.<p>
053: *
054: * The ValueModels used in this example are requested from a
055: * PresentationModel that adapts text properties of a TextBean.
056: * This is just to demonstrate a consistent binding style.
057: * The same techniques work with any ValueModel.
058: *
059: * @author Karsten Lentzsch
060: * @version $Revision: 1.10 $
061: *
062: * @see com.jgoodies.binding.PresentationModel
063: */
064: public final class CommitStylesExample {
065:
066: /**
067: * Holds a TextBean and vends ValueModels that adapt TextBean properties.
068: * As an alternative to this PresentationModel we could use 3 ValueModels,
069: * for example 3 ValueHolders, or any other ValueModel implementation.
070: */
071: private final PresentationModel<TextBean> presentationModel;
072:
073: private JTextComponent onKeyTypedField;
074: private JTextComponent onFocusLostField;
075: private JTextComponent onApplyField;
076: private JLabel onKeyTypedLabel;
077: private JLabel onFocusLostLabel;
078: private JLabel onApplyLabel;
079: private JButton applyButton;
080:
081: // Launching **************************************************************
082:
083: public static void main(String[] args) {
084: try {
085: UIManager
086: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
087: } catch (Exception e) {
088: // Likely PlasticXP is not in the class path; ignore.
089: }
090: JFrame frame = new JFrame();
091: frame.setTitle("Binding Tutorial :: Commit Styles");
092: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
093: CommitStylesExample example = new CommitStylesExample();
094: JComponent panel = example.buildPanel();
095: frame.getContentPane().add(panel);
096: frame.pack();
097: TutorialUtils.locateOnOpticalScreenCenter(frame);
098: frame.setVisible(true);
099: }
100:
101: // Instance Creation ******************************************************
102:
103: /**
104: * Constructs the example with a PresentationModel on the a TextBean.
105: */
106: public CommitStylesExample() {
107: this .presentationModel = new PresentationModel<TextBean>(
108: new TextBean());
109: }
110:
111: // Component Creation and Initialization **********************************
112:
113: /**
114: * Creates,binds and configures the UI components.<p>
115: */
116: private void initComponents() {
117: onKeyTypedField = BasicComponentFactory
118: .createTextField(presentationModel
119: .getModel(TextBean.PROPERTYNAME_TEXT1), false);
120: onKeyTypedLabel = BasicComponentFactory
121: .createLabel(presentationModel
122: .getModel(TextBean.PROPERTYNAME_TEXT1));
123:
124: onFocusLostField = BasicComponentFactory
125: .createTextField(presentationModel
126: .getModel(TextBean.PROPERTYNAME_TEXT2));
127: onFocusLostLabel = BasicComponentFactory
128: .createLabel(presentationModel
129: .getModel(TextBean.PROPERTYNAME_TEXT2));
130:
131: onApplyField = BasicComponentFactory
132: .createTextField(presentationModel
133: .getBufferedModel(TextBean.PROPERTYNAME_TEXT3));
134: onApplyLabel = BasicComponentFactory
135: .createLabel(presentationModel
136: .getModel(TextBean.PROPERTYNAME_TEXT3));
137:
138: applyButton = new JButton(new ApplyAction());
139: }
140:
141: // Building ***************************************************************
142:
143: /**
144: * Builds and returns the panel.
145: *
146: * @return the built panel
147: */
148: public JComponent buildPanel() {
149: initComponents();
150:
151: FormLayout layout = new FormLayout(
152: "right:max(50dlu;pref), 3dlu, 50dlu, 9dlu, 50dlu",
153: "p, 6dlu, p, 3dlu, p, 3dlu, p, 17dlu, p");
154:
155: PanelBuilder builder = new PanelBuilder(layout);
156: builder.setDefaultDialogBorder();
157: CellConstraints cc = new CellConstraints();
158:
159: builder.addTitle("Commit Style", cc.xy(1, 1));
160: builder.addTitle("Value", cc.xy(5, 1));
161: builder.addLabel("Key Typed", cc.xy(1, 3));
162: builder.add(onKeyTypedField, cc.xy(3, 3));
163: builder.add(onKeyTypedLabel, cc.xy(5, 3));
164: builder.addLabel("Focus Lost", cc.xy(1, 5));
165: builder.add(onFocusLostField, cc.xy(3, 5));
166: builder.add(onFocusLostLabel, cc.xy(5, 5));
167: builder.addLabel("Apply Pressed", cc.xy(1, 7));
168: builder.add(onApplyField, cc.xy(3, 7));
169: builder.add(onApplyLabel, cc.xy(5, 7));
170: builder.add(buildButtonBar(), cc.xyw(1, 9, 5));
171:
172: return builder.getPanel();
173: }
174:
175: private JComponent buildButtonBar() {
176: return ButtonBarFactory.buildRightAlignedBar(applyButton);
177: }
178:
179: // Actions ****************************************************************
180:
181: private final class ApplyAction extends AbstractAction {
182:
183: private ApplyAction() {
184: super ("Apply");
185: }
186:
187: public void actionPerformed(ActionEvent e) {
188: presentationModel.triggerCommit();
189: }
190: }
191:
192: // Helper Class ***********************************************************
193:
194: /**
195: * A simple bean that just provides three bound read-write text properties.
196: */
197: public static final class TextBean extends Model {
198:
199: // Names for the Bound Bean Properties ---------------------
200:
201: public static final String PROPERTYNAME_TEXT1 = "text1";
202: public static final String PROPERTYNAME_TEXT2 = "text2";
203: public static final String PROPERTYNAME_TEXT3 = "text3";
204:
205: private String text1;
206: private String text2;
207: private String text3;
208:
209: // Instance Creation ---------------------------------------
210:
211: private TextBean() {
212: text1 = "Text1";
213: text2 = "Text2";
214: text3 = "Text3";
215: }
216:
217: // Accessors -----------------------------------------------
218:
219: public String getText1() {
220: return text1;
221: }
222:
223: public void setText1(String newText1) {
224: String oldText1 = getText1();
225: text1 = newText1;
226: firePropertyChange(PROPERTYNAME_TEXT1, oldText1, newText1);
227: }
228:
229: public String getText2() {
230: return text2;
231: }
232:
233: public void setText2(String newText2) {
234: String oldText2 = getText2();
235: text2 = newText2;
236: firePropertyChange(PROPERTYNAME_TEXT2, oldText2, newText2);
237: }
238:
239: public String getText3() {
240: return text3;
241: }
242:
243: public void setText3(String newText3) {
244: String oldText3 = getText3();
245: text3 = newText3;
246: firePropertyChange(PROPERTYNAME_TEXT3, oldText3, newText3);
247: }
248:
249: }
250:
251: }
|