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.PropertyChangeListener;
034:
035: import com.jgoodies.binding.beans.Model;
036:
037: /**
038: * An abstract class that minimizes the effort required to implement
039: * the {@link ValueModel} interface. It provides convenience methods
040: * to convert boolean, double, float, int, and long to their
041: * corresponding Object values and vice versa.<p>
042: *
043: * Subclasses must implement <code>getValue()</code> and
044: * <code>setValue(Object)</code> to get and set this model's value.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.6 $
048: *
049: * @see com.jgoodies.binding.beans.ExtendedPropertyChangeSupport
050: */
051:
052: public abstract class AbstractValueModel extends Model implements
053: ValueModel {
054:
055: /**
056: * The name of the bound property <em>value</em>.
057: */
058: public static final String PROPERTYNAME_VALUE = "value";
059:
060: // Change Management ****************************************************
061:
062: /**
063: * Registers the given PropertyChangeListener with this model.
064: * The listener will be notified if the value has changed.<p>
065: *
066: * The PropertyChangeEvents delivered to the listener have the name
067: * set to "value". In other words, the listeners won't get notified
068: * when a PropertyChangeEvent is fired that has a null object as
069: * the name to indicate an arbitrary set of the event source's
070: * properties have changed.<p>
071: *
072: * In the rare case, where you want to notify a PropertyChangeListener
073: * even with PropertyChangeEvents that have no property name set,
074: * you can register the listener with #addPropertyChangeListener,
075: * not #addValueChangeListener.
076: *
077: * @param l the listener to add
078: *
079: * @see ValueModel
080: */
081: public final void addValueChangeListener(PropertyChangeListener l) {
082: addPropertyChangeListener(PROPERTYNAME_VALUE, l);
083: }
084:
085: /**
086: * Removes the given PropertyChangeListener from the model.
087: *
088: * @param l the listener to remove
089: */
090: public final void removeValueChangeListener(PropertyChangeListener l) {
091: removePropertyChangeListener(PROPERTYNAME_VALUE, l);
092: }
093:
094: /**
095: * Notifies all listeners that have registered interest for
096: * notification on this event type. The event instance
097: * is lazily created using the parameters passed into
098: * the fire method.
099: *
100: * @param oldValue the value before the change
101: * @param newValue the value after the change
102: *
103: * @see java.beans.PropertyChangeSupport
104: */
105: public final void fireValueChange(Object oldValue, Object newValue) {
106: firePropertyChange(PROPERTYNAME_VALUE, oldValue, newValue);
107: }
108:
109: /**
110: * Notifies all listeners that have registered interest for
111: * notification on this event type. The event instance
112: * is lazily created using the parameters passed into
113: * the fire method.<p>
114: *
115: * The boolean parameter specifies whether differences between the old
116: * and new value are tested using <code>==</code> or <code>#equals</code>.
117: *
118: * @param oldValue the value before the change
119: * @param newValue the value after the change
120: * @param checkIdentity true to compare the old and new value using
121: * <code>==</code>, false to use <code>#equals</code>
122: *
123: * @see java.beans.PropertyChangeSupport
124: */
125: public final void fireValueChange(Object oldValue, Object newValue,
126: boolean checkIdentity) {
127: firePropertyChange(PROPERTYNAME_VALUE, oldValue, newValue,
128: checkIdentity);
129: }
130:
131: /**
132: * Notifies all listeners that have registered interest for
133: * notification on this event type. The event instance
134: * is lazily created using the parameters passed into
135: * the fire method.
136: *
137: * @param oldValue the boolean value before the change
138: * @param newValue the boolean value after the change
139: *
140: * @see java.beans.PropertyChangeSupport
141: */
142: public final void fireValueChange(boolean oldValue, boolean newValue) {
143: fireValueChange(Boolean.valueOf(oldValue), Boolean
144: .valueOf(newValue));
145: }
146:
147: /**
148: * Notifies all listeners that have registered interest for
149: * notification on this event type. The event instance
150: * is lazily created using the parameters passed into
151: * the fire method.
152: *
153: * @param oldValue the int value before the change
154: * @param newValue the int value after the change
155: *
156: * @see java.beans.PropertyChangeSupport
157: */
158: public final void fireValueChange(int oldValue, int newValue) {
159: fireValueChange(Integer.valueOf(oldValue), Integer
160: .valueOf(newValue));
161: }
162:
163: /**
164: * Notifies all listeners that have registered interest for
165: * notification on this event type. The event instance
166: * is lazily created using the parameters passed into
167: * the fire method.
168: *
169: * @param oldValue the long value before the change
170: * @param newValue the long value after the change
171: *
172: * @see java.beans.PropertyChangeSupport
173: */
174: public final void fireValueChange(long oldValue, long newValue) {
175: fireValueChange(Long.valueOf(oldValue), Long.valueOf(newValue));
176: }
177:
178: /**
179: * Notifies all listeners that have registered interest for
180: * notification on this event type. The event instance
181: * is lazily created using the parameters passed into
182: * the fire method.
183: *
184: * @param oldValue the double value before the change
185: * @param newValue the double value after the change
186: *
187: * @see java.beans.PropertyChangeSupport
188: */
189: public final void fireValueChange(double oldValue, double newValue) {
190: fireValueChange(Double.valueOf(oldValue), Double
191: .valueOf(newValue));
192: }
193:
194: /**
195: * Notifies all listeners that have registered interest for
196: * notification on this event type. The event instance
197: * is lazily created using the parameters passed into
198: * the fire method.
199: *
200: * @param oldValue the float value before the change
201: * @param newValue the float value after the change
202: *
203: * @see java.beans.PropertyChangeSupport
204: */
205: public final void fireValueChange(float oldValue, float newValue) {
206: fireValueChange(Float.valueOf(oldValue), Float
207: .valueOf(newValue));
208: }
209:
210: // Type Conversion ******************************************************
211:
212: /**
213: * Converts this model's value and returns it as a <code>boolean</code>.
214: *
215: * @return the <code>boolean</code> value
216: * @throws ClassCastException if the observed value is not of type
217: * <code>Boolean</code>
218: * @throws NullPointerException if the value is <code>null</code>
219: */
220: public final boolean booleanValue() {
221: return ((Boolean) getValue()).booleanValue();
222: }
223:
224: /**
225: * Converts this model's value and returns it as a <code>double</code>.
226: *
227: * @return the <code>double</code> value
228: * @throws ClassCastException if the observed value is not of type
229: * <code>Double</code>
230: * @throws NullPointerException if the value is <code>null</code>
231: */
232: public final double doubleValue() {
233: return ((Double) getValue()).doubleValue();
234: }
235:
236: /**
237: * Converts this model's value and returns it as a <code>float</code>.
238: *
239: * @return the <code>float</code> value
240: * @throws ClassCastException if the observed value is not of type
241: * <code>Float</code>
242: * @throws NullPointerException if the value is <code>null</code>
243: */
244: public final float floatValue() {
245: return ((Float) getValue()).floatValue();
246: }
247:
248: /**
249: * Converts this model's value and returns it as an <code>int</code>.
250: *
251: * @return the <code>int</code> value
252: * @throws ClassCastException if the observed value is not of type
253: * <code>Integer</code>
254: * @throws NullPointerException if the value is <code>null</code>
255: */
256: public final int intValue() {
257: return ((Integer) getValue()).intValue();
258: }
259:
260: /**
261: * Converts this model's value and returns it as a <code>long</code>.
262: *
263: * @return the <code>long</code> value
264: * @throws ClassCastException if the observed value is not of type
265: * <code>Long</code>
266: * @throws NullPointerException if the value is <code>null</code>
267: */
268: public final long longValue() {
269: return ((Long) getValue()).longValue();
270: }
271:
272: /**
273: * Converts this model's value and returns it as a <code>String</code>.
274: *
275: * @return this model's value as String
276: * @throws ClassCastException if the observed value is not of type
277: * <code>String</code>
278: */
279: public String getString() {
280: return (String) getValue();
281: }
282:
283: /**
284: * Converts the given <code>boolean</code> to a <code>Boolean</code> and
285: * sets it as new value.
286: *
287: * @param b the value to be converted and set as new value
288: */
289: public final void setValue(boolean b) {
290: setValue(Boolean.valueOf(b));
291: }
292:
293: /**
294: * Converts the given <code>double</code> to a <code>Double</code> and
295: * sets it as new value.
296: *
297: * @param d the value to be converted and set as new value
298: */
299: public final void setValue(double d) {
300: setValue(Double.valueOf(d));
301: }
302:
303: /**
304: * Converts the given <code>float</code> to a <code>Float</code> and
305: * sets it as new value.
306: *
307: * @param f the value to be converted and set as new value
308: */
309: public final void setValue(float f) {
310: setValue(Float.valueOf(f));
311: }
312:
313: /**
314: * Converts the given <code>int</code> to an <code>Integer</code> and
315: * sets it as new value.
316: *
317: * @param i the value to be converted and set as new value
318: */
319: public final void setValue(int i) {
320: setValue(Integer.valueOf(i));
321: }
322:
323: /**
324: * Converts the given <code>long</code> to a <code>Long</code> and
325: * sets it as new value.
326: *
327: * @param l the value to be converted and set as new value
328: */
329: public final void setValue(long l) {
330: setValue(Long.valueOf(l));
331: }
332:
333: /**
334: * Returns a string representation of this value model.
335: * Answers the print string of the observed value.
336: *
337: * @return a string representation of this value model
338: */
339: @Override
340: public String toString() {
341: try {
342: Object value = getValue();
343: return value == null ? "null" : value.toString();
344: } catch (Exception e) {
345: return "Can't read";
346: }
347: }
348:
349: }
|