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.adapter.BasicComponentFactory;
039: import com.jgoodies.binding.beans.PropertyConnector;
040: import com.jgoodies.binding.tutorial.Album;
041: import com.jgoodies.binding.tutorial.AlbumPresentationModel;
042: import com.jgoodies.binding.tutorial.TutorialUtils;
043: import com.jgoodies.forms.builder.PanelBuilder;
044: import com.jgoodies.forms.factories.ButtonBarFactory;
045: import com.jgoodies.forms.layout.CellConstraints;
046: import com.jgoodies.forms.layout.FormLayout;
047:
048: /**
049: * Builds an editor with components bound to the domain object properties
050: * using adapting ValueModels created by a PresentationModel.
051: *
052: * @author Karsten Lentzsch
053: * @version $Revision: 1.13 $
054: *
055: * @see com.jgoodies.binding.PresentationModel
056: */
057:
058: public final class EditorBoundExample {
059:
060: /**
061: * Holds the edited Album and vends ValueModels that adapt Album properties.
062: */
063: private final AlbumPresentationModel presentationModel;
064:
065: private JTextComponent titleField;
066: private JTextComponent artistField;
067: private JCheckBox classicalBox;
068: private JTextComponent composerField;
069: private JButton closeButton;
070:
071: // Launching **************************************************************
072:
073: public static void main(String[] args) {
074: try {
075: UIManager
076: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
077: } catch (Exception e) {
078: // Likely PlasticXP is not in the class path; ignore.
079: }
080: JFrame frame = new JFrame();
081: frame.setTitle("Binding Tutorial :: Editor (Bound)");
082: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
083: EditorBoundExample example = new EditorBoundExample();
084: JComponent panel = example.buildPanel();
085: frame.getContentPane().add(panel);
086: frame.pack();
087: TutorialUtils.locateOnOpticalScreenCenter(frame);
088: frame.setVisible(true);
089: }
090:
091: // Instance Creation ******************************************************
092:
093: /**
094: * Constructs an editor on an Album example instance.
095: */
096: public EditorBoundExample() {
097: this (Album.ALBUM1);
098: }
099:
100: /**
101: * Constructs an editor for an Album to be edited.
102: *
103: * @param album the Album to be edited
104: */
105: public EditorBoundExample(Album album) {
106: this .presentationModel = new AlbumPresentationModel(album);
107: }
108:
109: // Initialization *********************************************************
110:
111: /**
112: * Creates, binds and configures the UI components.
113: * Changes are committed to the value models on focus lost.<p>
114: *
115: * The coding style used here is based on standard Swing components.
116: * Therefore we can create and bind the components in one step.
117: * And that's the purpose of the BasicComponentFactory class.<p>
118: *
119: * If you need to bind custom components, for example MyTextField,
120: * MyCheckBox, MyComboBox, you can use the more basic Bindings class.
121: * The code would then read:<pre>
122: * titleField = new MyTextField();
123: * Bindings.bind(titleField,
124: * presentationModel.getModel(Album.PROPERTYNAME_TITLE));
125: * </pre><p>
126: *
127: * I strongly recommend to use a custom ComponentFactory,
128: * the BasicComponentFactory or the Bindings class. These classes
129: * hide details of the binding.
130: * So you better <em>not</em> write the following code:<pre>
131: * titleField = new JTextField();
132: * titleField.setDocument(new DocumentAdapter(
133: * presentationModel.getModel(Album.PROPERTYNAME_TITLE)));
134: * </pre>
135: */
136: private void initComponents() {
137: titleField = BasicComponentFactory
138: .createTextField(presentationModel
139: .getModel(Album.PROPERTYNAME_TITLE));
140: artistField = BasicComponentFactory
141: .createTextField(presentationModel
142: .getModel(Album.PROPERTYNAME_ARTIST));
143: classicalBox = BasicComponentFactory.createCheckBox(
144: presentationModel
145: .getModel(Album.PROPERTYNAME_CLASSICAL),
146: "Classical");
147: composerField = BasicComponentFactory
148: .createTextField(presentationModel
149: .getModel(Album.PROPERTYNAME_COMPOSER));
150: closeButton = new JButton(new CloseAction());
151:
152: boolean composerEnabled = presentationModel.isComposerEnabled();
153: composerField.setEnabled(composerEnabled);
154: }
155:
156: /**
157: * Registers a listener with the presentation model's "composerEnabled"
158: * property to switch the composer field's enablement. For demonstration
159: * purposes a listener is registered that writes changes to the console.
160: */
161: private void initEventHandling() {
162: // Synchronize the composer field enablement with 'composerEnabled'.
163: PropertyConnector.connect(composerField, "enabled",
164: presentationModel,
165: AlbumPresentationModel.PROPERTYNAME_COMPOSER_ENABLED);
166:
167: // Report changes in all bound Album properties.
168: presentationModel.addBeanPropertyChangeListener(TutorialUtils
169: .createDebugPropertyChangeListener());
170: }
171:
172: // Building ***************************************************************
173:
174: /**
175: * Builds and returns the panel.
176: *
177: * @return the built panel
178: */
179: public JComponent buildPanel() {
180: initComponents();
181: initEventHandling();
182:
183: FormLayout layout = new FormLayout(
184: "right:pref, 3dlu, 150dlu:grow",
185: "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, p");
186:
187: PanelBuilder builder = new PanelBuilder(layout);
188: builder.setDefaultDialogBorder();
189: CellConstraints cc = new CellConstraints();
190:
191: builder.addLabel("Artist", cc.xy(1, 1));
192: builder.add(artistField, cc.xy(3, 1));
193: builder.addLabel("Title", cc.xy(1, 3));
194: builder.add(titleField, cc.xy(3, 3));
195: builder.add(classicalBox, cc.xy(3, 5));
196: builder.addLabel("Composer", cc.xy(1, 7));
197: builder.add(composerField, cc.xy(3, 7));
198: builder.add(buildButtonBar(), cc.xyw(1, 9, 3));
199:
200: return builder.getPanel();
201: }
202:
203: private JComponent buildButtonBar() {
204: return ButtonBarFactory.buildRightAlignedBar(closeButton);
205: }
206:
207: // Presentation Model *****************************************************
208:
209: /**
210: * An Action that prints the current bean to the System console
211: * before it exists the System.
212: *
213: * Actions belong to the presentation model. However, to keep
214: * this tutorial small I've chosen to reuse a single presentation model
215: * for all album examples and so, couldn't put in different close actions.
216: */
217: private final class CloseAction extends AbstractAction {
218:
219: private CloseAction() {
220: super ("Close");
221: }
222:
223: public void actionPerformed(ActionEvent e) {
224: System.out.println(presentationModel.getBean());
225: System.exit(0);
226: }
227: }
228:
229: }
|