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: /**
037: * A ValueModel that allows to accept or reject proposed value changes.
038: * Useful to request information from the user or to perform operations
039: * before a value is changed.<p>
040: *
041: * Wraps a given subject ValueModel and always returns the subject value
042: * as this model's value. Observes subject value changes and forwards
043: * them to listeners of this model. If a value is set to this model,
044: * the abstract method <code>#proposedChange</code> is invoked. In this method
045: * implementors define how to accept or reject value changes.<p>
046: *
047: * Implementors may veto against a proposed change based on the application
048: * state or by asking the user, and may also perform additional operations
049: * during the check, for example to save editor contents. Here's an example:
050: * <pre>
051: * public class CheckPendingEditValueModel extends AbstractVetoableValueModel {
052: *
053: * public CheckPendingEditValueModel(ValueModel subject) {
054: * super(subject);
055: * }
056: *
057: * public boolean proposedChange(Object oldValue, Object proposedNewValue) {
058: * if (equals(oldValue, proposedNewValue))
059: * return true;
060: * int option = JOptionPane.showConfirmDialog(
061: * Application.getDefaultParentFrame(),
062: * "Do you want to save the editor contents.");
063: * if (option == JOptionPane.YES_OPTION)
064: * model.save();
065: * return option != JOptionPane.CANCEL_OPTION;
066: * }
067: * }
068: * </pre>
069: *
070: * @author Karsten Lentzsch
071: * @version $Revision: 1.7 $
072: *
073: * @since 1.1
074: */
075: public abstract class AbstractVetoableValueModel extends
076: AbstractValueModel {
077:
078: /**
079: * Holds the wrapped subject ValueModel that is used to read values from
080: * and commit accepted changes to.
081: */
082: private final ValueModel subject;
083:
084: // Instance Creation ******************************************************
085:
086: /**
087: * Constructs an AbstractVetoableValueModel for the given ValueModel.
088: *
089: * @param subject the underlying (or wrapped) ValueModel
090: *
091: * @throws NullPointerException if the subject is <code>null</code>
092: */
093: protected AbstractVetoableValueModel(ValueModel subject) {
094: this .subject = subject;
095: subject.addValueChangeListener(new SubjectValueChangeHandler());
096: }
097:
098: // Abstract Behavior ******************************************************
099:
100: /**
101: * Checks and answers whether the proposed value change shall be
102: * accepted or rejected. Implementors may perform additional
103: * operations, for example to save a pending editor content.
104: *
105: * @param oldValue the value before the change
106: * @param proposedNewValue the new value if the change is accepted
107: * @return true to accept the proposed value, false to veto against it.
108: */
109: public abstract boolean proposedChange(Object oldValue,
110: Object proposedNewValue);
111:
112: // ValueModel Implementation **********************************************
113:
114: /**
115: * Returns this model's current subject value.
116: *
117: * @return this model's current subject value.
118: */
119: public final Object getValue() {
120: return subject.getValue();
121: }
122:
123: /**
124: * Sets the given value as new subject value if and only if
125: * 1) the new value differs from the old value and
126: * 2) the proposed change is accepted as checked by
127: * <code>proposedChange(oldValue, newValue)</code>.
128: *
129: * @param newValue the value to set
130: */
131: public final void setValue(Object newValue) {
132: Object oldValue = getValue();
133: if (oldValue == newValue)
134: return;
135: if (proposedChange(oldValue, newValue))
136: subject.setValue(newValue);
137: }
138:
139: // Event Handling *****************************************************
140:
141: /**
142: * Forwards value changes in the subject to listeners of this model.
143: */
144: private final class SubjectValueChangeHandler implements
145: PropertyChangeListener {
146:
147: public void propertyChange(PropertyChangeEvent evt) {
148: fireValueChange(evt.getOldValue(), evt.getNewValue(), true);
149: }
150: }
151:
152: }
|