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: import java.beans.PropertyChangeEvent;
035: import java.beans.PropertyChangeListener;
036:
037: import javax.swing.*;
038: import javax.swing.text.JTextComponent;
039:
040: import com.jgoodies.binding.PresentationModel;
041: import com.jgoodies.binding.adapter.BasicComponentFactory;
042: import com.jgoodies.binding.tutorial.Album;
043: import com.jgoodies.binding.tutorial.TutorialUtils;
044: import com.jgoodies.binding.value.ValueModel;
045: import com.jgoodies.forms.builder.PanelBuilder;
046: import com.jgoodies.forms.factories.ButtonBarFactory;
047: import com.jgoodies.forms.layout.CellConstraints;
048: import com.jgoodies.forms.layout.FormLayout;
049:
050: /**
051: * Builds an editor with components bound to the domain object properties
052: * using buffered adapting ValueModels created by a PresentationModel.
053: * These buffers can be committed on 'Apply' and flushed on 'Reset'.
054: * A second set of components displays the domain object properties.
055: *
056: * @author Karsten Lentzsch
057: * @version $Revision: 1.14 $
058: *
059: * @see com.jgoodies.binding.PresentationModel
060: * @see com.jgoodies.binding.value.BufferedValueModel
061: * @see com.jgoodies.binding.value.Trigger
062: */
063: public final class EditorBufferedExample {
064:
065: /**
066: * Holds the edited Album and vends ValueModels that adapt Album properties.
067: * In this example we will request BufferedValueModels for the editor.
068: */
069: private final PresentationModel<Album> presentationModel;
070:
071: private JTextComponent titleField;
072: private JTextComponent artistField;
073: private JCheckBox classicalBox;
074: private JTextComponent composerField;
075:
076: private JTextComponent unbufferedTitleField;
077: private JTextComponent unbufferedArtistField;
078: private JCheckBox unbufferedClassicalBox;
079: private JTextComponent unbufferedComposerField;
080:
081: private JButton applyButton;
082: private JButton resetButton;
083:
084: // Launching **************************************************************
085:
086: public static void main(String[] args) {
087: try {
088: UIManager
089: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
090: } catch (Exception e) {
091: // Likely PlasticXP is not in the class path; ignore.
092: }
093: JFrame frame = new JFrame();
094: frame.setTitle("Binding Tutorial :: Editor (Bound, Buffered)");
095: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
096: EditorBufferedExample example = new EditorBufferedExample();
097: JComponent panel = example.buildPanel();
098: frame.getContentPane().add(panel);
099: frame.pack();
100: TutorialUtils.locateOnOpticalScreenCenter(frame);
101: frame.setVisible(true);
102: }
103:
104: // Instance Creation ******************************************************
105:
106: /**
107: * Constructs a buffered editor on an example Album.
108: */
109: public EditorBufferedExample() {
110: this (Album.ALBUM1);
111: }
112:
113: /**
114: * Constructs a buffered editor for an Album to be edited.
115: *
116: * @param album the Album to be edited
117: */
118: public EditorBufferedExample(Album album) {
119: this .presentationModel = new PresentationModel<Album>(album);
120: }
121:
122: // Initialization *********************************************************
123:
124: /**
125: * Creates, binds and configures the UI components.
126: * Changes are committed to the value models on apply and are
127: * flushed on reset.
128: */
129: private void initComponents() {
130: titleField = BasicComponentFactory
131: .createTextField(presentationModel
132: .getBufferedModel(Album.PROPERTYNAME_TITLE));
133: artistField = BasicComponentFactory
134: .createTextField(presentationModel
135: .getBufferedModel(Album.PROPERTYNAME_ARTIST));
136: classicalBox = BasicComponentFactory
137: .createCheckBox(
138: presentationModel
139: .getBufferedModel(Album.PROPERTYNAME_CLASSICAL),
140: "Classical");
141: composerField = BasicComponentFactory
142: .createTextField(presentationModel
143: .getBufferedModel(Album.PROPERTYNAME_COMPOSER));
144:
145: unbufferedTitleField = BasicComponentFactory
146: .createTextField(presentationModel
147: .getModel(Album.PROPERTYNAME_TITLE));
148: unbufferedTitleField.setEditable(false);
149: unbufferedArtistField = BasicComponentFactory
150: .createTextField(presentationModel
151: .getModel(Album.PROPERTYNAME_ARTIST));
152: unbufferedArtistField.setEditable(false);
153: unbufferedClassicalBox = BasicComponentFactory.createCheckBox(
154: presentationModel
155: .getModel(Album.PROPERTYNAME_CLASSICAL),
156: "Classical");
157: unbufferedClassicalBox.setEnabled(false);
158: unbufferedComposerField = BasicComponentFactory
159: .createTextField(presentationModel
160: .getModel(Album.PROPERTYNAME_COMPOSER));
161: unbufferedComposerField.setEditable(false);
162:
163: applyButton = new JButton(new ApplyAction());
164: resetButton = new JButton(new ResetAction());
165:
166: updateComposerField();
167: }
168:
169: /**
170: * Observes the buffered <em>classical</em> property
171: * to update the composer field's enablement and contents.<p>
172: *
173: * If the AlbumPresentationModel would provide a property
174: * <em>bufferedComposerEnabled</em> we could use that instead.
175: * I've not added the latter property to the AlbumPresentationModel
176: * so it remains close to the example used in Martin Fowler's
177: * description of the
178: * <a href="http://martinfowler.com/eaaDev/PresentationModel.html">Presentation
179: * Model</a> pattern.
180: */
181: private void initEventHandling() {
182: ValueModel bufferedClassicalModel = presentationModel
183: .getBufferedModel(Album.PROPERTYNAME_CLASSICAL);
184: bufferedClassicalModel
185: .addValueChangeListener(new BufferedClassicalChangeHandler());
186: }
187:
188: // Building ***************************************************************
189:
190: /**
191: * Builds and returns a panel that consists of the editor and display.
192: *
193: * @return the built panel
194: */
195: public JComponent buildPanel() {
196: initComponents();
197: initEventHandling();
198:
199: FormLayout layout = new FormLayout(
200: "right:pref, 3dlu, 150dlu:grow",
201: "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 17dlu, "
202: + "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 17dlu, p");
203:
204: PanelBuilder builder = new PanelBuilder(layout);
205: builder.setDefaultDialogBorder();
206: CellConstraints cc = new CellConstraints();
207:
208: builder.addSeparator("Buffered", cc.xyw(1, 1, 3));
209: builder.addLabel("Artist", cc.xy(1, 3));
210: builder.add(artistField, cc.xy(3, 3));
211: builder.addLabel("Title", cc.xy(1, 5));
212: builder.add(titleField, cc.xy(3, 5));
213: builder.add(classicalBox, cc.xy(3, 7));
214: builder.addLabel("Composer", cc.xy(1, 9));
215: builder.add(composerField, cc.xy(3, 9));
216:
217: builder.addSeparator("Unbuffered", cc.xyw(1, 11, 3));
218: builder.addLabel("Artist", cc.xy(1, 13));
219: builder.add(unbufferedArtistField, cc.xy(3, 13));
220: builder.addLabel("Title", cc.xy(1, 15));
221: builder.add(unbufferedTitleField, cc.xy(3, 15));
222: builder.add(unbufferedClassicalBox, cc.xy(3, 17));
223: builder.addLabel("Composer", cc.xy(1, 19));
224: builder.add(unbufferedComposerField, cc.xy(3, 19));
225:
226: builder.add(buildButtonBar(), cc.xyw(1, 21, 3));
227:
228: return builder.getPanel();
229: }
230:
231: private JComponent buildButtonBar() {
232: return ButtonBarFactory.buildRightAlignedBar(applyButton,
233: resetButton);
234: }
235:
236: // Event Handling *********************************************************
237:
238: /**
239: * Updates the composer field's enablement and contents.
240: * Sets the enablement according to the boolean state of
241: * the buffered classical property. If the composer is not enabled,
242: * we copy the domain logic and set the composer to <code>null</code>.
243: */
244: private void updateComposerField() {
245: boolean composerEnabled = ((Boolean) presentationModel
246: .getBufferedValue(Album.PROPERTYNAME_CLASSICAL))
247: .booleanValue();
248: composerField.setEnabled(composerEnabled);
249: if (!composerEnabled) {
250: presentationModel.setBufferedValue(
251: Album.PROPERTYNAME_COMPOSER, null);
252: }
253: }
254:
255: /**
256: * Updates the composer field's enablement and the buffered composer name.
257: */
258: private final class BufferedClassicalChangeHandler implements
259: PropertyChangeListener {
260:
261: /**
262: * The buffered (Boolean) classical property has changed.
263: * Updates the enablement and contents of the composer field.
264: */
265: public void propertyChange(PropertyChangeEvent evt) {
266: updateComposerField();
267: }
268: }
269:
270: // Actions ****************************************************************
271:
272: /**
273: * Commits the Trigger used to buffer the editor contents.
274: */
275: private final class ApplyAction extends AbstractAction {
276:
277: private ApplyAction() {
278: super ("Apply");
279: }
280:
281: public void actionPerformed(ActionEvent e) {
282: presentationModel.triggerCommit();
283: }
284: }
285:
286: /**
287: * Flushed the Trigger used to buffer the editor contents.
288: */
289: private final class ResetAction extends AbstractAction {
290:
291: private ResetAction() {
292: super ("Reset");
293: }
294:
295: public void actionPerformed(ActionEvent e) {
296: presentationModel.triggerFlush();
297: }
298: }
299:
300: }
|