001: /*
002: * @(#)AbstractEditableAttribute.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.pmti.v1.defimpl;
030:
031: import net.sourceforge.groboutils.pmti.v1.IEditableAttribute;
032: import net.sourceforge.groboutils.pmti.v1.IAttributeInfo;
033: import net.sourceforge.groboutils.pmti.v1.IAttribute;
034:
035: /**
036: * This uses delegation to allow for maximum flexibility in creating new
037: * editable attributes based on this class through subclassing.
038: *
039: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
040: * @version $Date: 2003/02/10 22:51:57 $
041: * @since July 12, 2002
042: */
043: public abstract class AbstractEditableAttribute implements
044: IEditableAttribute {
045: private Object newvalue;
046: private boolean valueChanged = false;
047: private IAttribute baseAttrib;
048:
049: public AbstractEditableAttribute(IAttribute base) {
050: if (base == null) {
051: throw new IllegalArgumentException("no null arguments");
052: }
053: this .baseAttrib = base;
054: }
055:
056: /**
057: * Returns the current (possibly modified) value for this attribute.
058: */
059: public Object getValue() {
060: Object ret;
061: if (this .valueChanged) {
062: ret = this .newvalue;
063: } else {
064: ret = this .baseAttrib.getValue();
065: }
066: return ret;
067: }
068:
069: /**
070: * Returns the meta-information for this attribute.
071: */
072: public IAttributeInfo getInfo() {
073: return this .baseAttrib.getInfo();
074: }
075:
076: /**
077: * @exception IllegalArgumentException thrown if the value argument is
078: * invalid.
079: */
080: public void setValue(Object value) {
081: if (!isValidValue(value)) {
082: throw new IllegalArgumentException("invalid value " + value);
083: }
084: Object orig = this .baseAttrib.getValue();
085: if (value == orig || (value != null && value.equals(orig))) {
086: // no change from the original
087: this .valueChanged = false;
088: } else {
089: this .valueChanged = true;
090: this .newvalue = value;
091: }
092: }
093:
094: /**
095: * @return <tt>true</tt> if the <tt>setValue( Object )</tt> method has
096: * been called on this instance and the actual value has changed
097: * (via inspection with <tt>==</tt> and <tt>equals()</tt>),
098: * otherwise <tt>false</tt>.
099: */
100: public boolean hasValueChanged() {
101: return this .valueChanged;
102: }
103:
104: /**
105: *
106: */
107: public abstract boolean isValidValue(Object value);
108: }
|