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.value;
032:
033: import java.beans.PropertyChangeEvent;
034: import java.beans.PropertyChangeListener;
035:
036: import com.jgoodies.binding.PresentationModel;
037: import com.jgoodies.binding.adapter.BasicComponentFactory;
038: import com.jgoodies.binding.adapter.Bindings;
039:
040: /**
041: * A ValueModel that provides relevant GUI state in presentation models.
042: * It provides bound properties for the frequently used JComponent state
043: * <em>enabled</em>,<em>visible</em> and JTextComponent state <em>editable</em>.
044: * ComponentValueModels can be used to set these properties at the
045: * presentation model layer; any ComponentValueModel property change
046: * will be reflected by components bound to that ComponentValueModel.<p>
047: *
048: * The ComponentValueModel is similar to the Swing Action class.
049: * If you disable an Action, all buttons and menu items bound to that Action
050: * will be disabled. If you disable a ComponentValueModel, all components
051: * bound to that ComponentValueModel will be disabled. If you set the
052: * ComponentValueModel to invisible, the component bound to it will become
053: * invisible. If you set a ComponentValueModel to non-editable,
054: * the JTextComponents bound to it will become non-editable.<p>
055: *
056: * Since version 1.1, PresentationModels can vend ComponentValueModels
057: * using <code>#getComponentModel(String)</code> and
058: * <code>#getBufferedComponentModel(String)</code>. Multiple calls
059: * to these factory methods return the same ComponentValueModel.<p>
060: *
061: * The BasicComponentFactory and the Bindings class check if the ValueModel
062: * provided to create/bind a Swing component is a ComponentValueModel.
063: * If so, the ComponentValueModel properties will be synchronized
064: * with the associated Swing component properties.<p>
065: *
066: * It is recommended to use ComponentValueModels only for those models
067: * that are bound to view components that require GUI state changes.<p>
068: *
069: * <strong>Example Code:</strong><pre>
070: * final class AlbumView {
071: *
072: * ...
073: *
074: * private void initComponents() {
075: * // No state modifications required for the name field.
076: * nameField = BasicComponentFactory.createTextField(
077: * presentationModel.getModel(Album.PROPERTYNAME_NAME));
078: * ...
079: * // Enablement shall change for the composer field
080: * composerField = BasicComponentFactory.createTextField(
081: * presentationModel.getComponentModel(Album.PROPERTYNAME_COMPOSER));
082: * ...
083: * }
084: *
085: * ...
086: *
087: * }
088: *
089: *
090: * public final class AlbumPresentationModel extends PresentationModel {
091: *
092: * ...
093: *
094: * private void updateComposerEnablement(boolean enabled) {
095: * getComponentModel(Album.PROPERTYNAME_COMPOSER).setEnabled(enabled);
096: * }
097: *
098: * ...
099: *
100: * }
101: * </pre><p>
102: *
103: * As of the Binding version 2.0 the ComponentValueModel feature
104: * is implemented for text components, radio buttons, check boxes,
105: * combo boxes, and lists. JColorChoosers bound using the
106: * Bindings class will ignore ComponentValueModel state.
107: *
108: * @author Karsten Lentzsch
109: * @version $Revision: 1.11 $
110: *
111: * @see PresentationModel#getComponentModel(String)
112: * @see PresentationModel#getBufferedComponentModel(String)
113: * @see BasicComponentFactory
114: * @see Bindings
115: *
116: * @since 1.1
117: */
118: public final class ComponentValueModel extends AbstractValueModel {
119:
120: // Names of the Bean Properties *******************************************
121:
122: /**
123: * The name of the property used to synchronize
124: * this model with the <em>enabled</em> property of JComponents.
125: */
126: public static final String PROPERTYNAME_ENABLED = "enabled";
127:
128: /**
129: * The name of the property used to synchronize
130: * this model with the <em>visible</em> property of JComponents.
131: */
132: public static final String PROPERTYNAME_VISIBLE = "visible";
133:
134: /**
135: * The name of the property used to synchronize
136: * this model with the <em>editable</em> property of JTextComponents.
137: */
138: public static final String PROPERTYNAME_EDITABLE = "editable";
139:
140: // Instance Fields ********************************************************
141:
142: /**
143: * Holds the wrapped subject ValueModel that is used
144: * to read and write value.
145: */
146: private final ValueModel subject;
147:
148: private boolean enabled;
149: private boolean visible;
150: private boolean editable;
151:
152: // Instance Creation ******************************************************
153:
154: /**
155: * Constructs a ComponentValueModel for the given ValueModel.
156: *
157: * @param subject the underlying (or wrapped) ValueModel
158: */
159: public ComponentValueModel(ValueModel subject) {
160: this .subject = subject;
161: this .enabled = true;
162: this .editable = true;
163: this .visible = true;
164: subject.addValueChangeListener(new SubjectValueChangeHandler());
165: }
166:
167: // ValueModel Implementation **********************************************
168:
169: /**
170: * Returns this model's current subject value.
171: *
172: * @return this model's current subject value.
173: */
174: public Object getValue() {
175: return subject.getValue();
176: }
177:
178: /**
179: * Sets the given value as new subject value.
180: *
181: * @param newValue the value to set
182: */
183: public void setValue(Object newValue) {
184: subject.setValue(newValue);
185: }
186:
187: // Firing Component Property Change Events ********************************
188:
189: /**
190: * Returns if this model represents an enabled or disabled component state.
191: *
192: * @return true for enabled, false for disabled
193: */
194: public boolean isEnabled() {
195: return enabled;
196: }
197:
198: /**
199: * Enables or disabled this model, which in turn
200: * will enable or disable all Swing components bound to this model.
201: *
202: * @param b true to enable, false to disable.
203: */
204: public void setEnabled(boolean b) {
205: boolean oldEnabled = isEnabled();
206: enabled = b;
207: firePropertyChange(PROPERTYNAME_ENABLED, oldEnabled, b);
208: }
209:
210: /**
211: * Returns if this model represents the visible or invisible component state.
212: *
213: * @return true for visible, false for invisible
214: */
215: public boolean isVisible() {
216: return visible;
217: }
218:
219: /**
220: * Sets this model state to visible or invisible, which in turn
221: * will make all Swing components bound to this model visible or invisible.
222: *
223: * @param b true for visible, false for invisible
224: */
225: public void setVisible(boolean b) {
226: boolean oldVisible = isVisible();
227: visible = b;
228: firePropertyChange(PROPERTYNAME_VISIBLE, oldVisible, b);
229: }
230:
231: /**
232: * Returns if this model represents the editable or non-editable
233: * text component state.
234: *
235: * @return true for editable, false for non-editable
236: */
237: public boolean isEditable() {
238: return editable;
239: }
240:
241: /**
242: * Sets this model state to editable or non-editable, which in turn will
243: * make all text components bound to this model editable or non-editable.
244: *
245: * @param b true for editable, false for non-editable
246: */
247: public void setEditable(boolean b) {
248: boolean oldEditable = isEditable();
249: editable = b;
250: firePropertyChange(PROPERTYNAME_EDITABLE, oldEditable, b);
251: }
252:
253: // Event Handling *********************************************************
254:
255: /**
256: * Forwards value changes in the subject to listeners of this model.
257: */
258: private final class SubjectValueChangeHandler implements
259: PropertyChangeListener {
260:
261: public void propertyChange(PropertyChangeEvent evt) {
262: fireValueChange(evt.getOldValue(), evt.getNewValue(), true);
263: }
264: }
265:
266: }
|