001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: PropertyRefValue.java,v 1.17 2005/12/04 13:46:06 jesper Exp $
023: package net.infonode.properties.propertymap.value;
024:
025: import net.infonode.properties.base.Property;
026: import net.infonode.properties.base.exception.InvalidPropertyTypeException;
027: import net.infonode.properties.propertymap.PropertyMapImpl;
028: import net.infonode.properties.propertymap.ref.PropertyMapRef;
029: import net.infonode.properties.propertymap.ref.PropertyMapRefDecoder;
030: import net.infonode.util.Printer;
031: import net.infonode.util.ValueChange;
032: import net.infonode.util.collection.map.base.ConstMap;
033: import net.infonode.util.signal.Signal;
034: import net.infonode.util.signal.SignalListener;
035:
036: import java.io.IOException;
037: import java.io.ObjectInputStream;
038: import java.io.ObjectOutputStream;
039:
040: /**
041: * @author $Author: jesper $
042: * @version $Revision: 1.17 $
043: */
044: public class PropertyRefValue implements PropertyValue, SignalListener {
045: private PropertyMapImpl map;
046: private Property property;
047: private PropertyMapRef propertyObjectRef;
048: private Property propertyRef;
049: private PropertyRefValue parentRef;
050:
051: public PropertyRefValue(PropertyMapImpl map, Property property,
052: PropertyMapRef propertyObjectRef, Property propertyRef,
053: PropertyRefValue parentRef) {
054: if (!property.getType().isAssignableFrom(propertyRef.getType()))
055: throw new InvalidPropertyTypeException(
056: property,
057: propertyRef,
058: "Can't create reference from Property '"
059: + property
060: + "' to property '"
061: + propertyRef
062: + "' because they are of incompatible types!");
063:
064: this .map = map;
065: this .property = property;
066: this .propertyObjectRef = propertyObjectRef;
067: this .propertyRef = propertyRef;
068: this .parentRef = parentRef;
069: }
070:
071: public Property getProperty() {
072: return property;
073: }
074:
075: public PropertyMapImpl getMap() {
076: return map;
077: }
078:
079: public void updateListener(boolean enable) {
080: if (enable)
081: propertyObjectRef.getMap(map).getMap().getChangeSignal()
082: .add(this );
083: else
084: propertyObjectRef.getMap(map).getMap().getChangeSignal()
085: .remove(this );
086: }
087:
088: public PropertyValue getParent() {
089: return parentRef;
090: }
091:
092: public Object get(PropertyMapImpl object) {
093: PropertyMapImpl o = propertyObjectRef.getMap(object);
094: PropertyValue v = (o == null ? propertyObjectRef.getMap(map)
095: : o).getValue(propertyRef);
096: return v == null ? null : v.get(o);
097: }
098:
099: public Object getWithDefault(PropertyMapImpl object) {
100: PropertyMapImpl o = propertyObjectRef.getMap(object);
101: PropertyValue v = (o == null ? propertyObjectRef.getMap(map)
102: : o).getValueWithDefault(propertyRef);
103: return v == null ? null : v.getWithDefault(o);
104: }
105:
106: public PropertyValue getSubValue(PropertyMapImpl object) {
107: PropertyMapImpl newObject = propertyObjectRef.getMap(object);
108:
109: if (newObject == null)
110: return null;
111:
112: if (!newObject.getPropertyGroup().hasProperty(propertyRef))
113: return null;
114:
115: return new PropertyRefValue(object, property,
116: propertyObjectRef, propertyRef, this );
117: }
118:
119: public void unset() {
120: propertyObjectRef.getMap(map).getMap().getChangeSignal()
121: .remove(this );
122: }
123:
124: public void signalEmitted(Signal signal, Object object) {
125: ConstMap changes = (ConstMap) object;
126: ValueChange vc = (ValueChange) changes.get(propertyRef);
127:
128: if (vc != null)
129: map.firePropertyValueChanged(property, new ValueChange(vc
130: .getOldValue(), this ));
131: }
132:
133: public String toString() {
134: return "ref -> " + propertyObjectRef + '.' + propertyRef;
135: }
136:
137: public void dump(Printer printer) {
138: printer.println(toString());
139: }
140:
141: public void write(ObjectOutputStream out) throws IOException {
142: out.writeInt(ValueDecoder.REF);
143: propertyObjectRef.write(out);
144: out.writeUTF(propertyRef.getName());
145: }
146:
147: public boolean isSerializable() {
148: return true;
149: }
150:
151: public static PropertyValue decode(ObjectInputStream in,
152: PropertyMapImpl propertyObject, Property property)
153: throws IOException {
154: PropertyMapRef ref = PropertyMapRefDecoder.decode(in);
155: String propertyName = in.readUTF();
156:
157: if (property == null || ref == null)
158: return null;
159:
160: Property refProperty = ref.getMap(propertyObject)
161: .getPropertyGroup().getProperty(propertyName);
162:
163: if (refProperty == null)
164: return null;
165:
166: return new PropertyRefValue(propertyObject, property, ref,
167: refProperty, null);
168: }
169:
170: public static void skip(ObjectInputStream in) throws IOException {
171: PropertyMapRefDecoder.decode(in);
172: in.readUTF();
173: }
174:
175: public PropertyValue copyTo(PropertyMapImpl propertyMap) {
176: return new PropertyRefValue(propertyMap, property,
177: propertyObjectRef, propertyRef, null);
178: }
179:
180: }
|