001: /**********************************************************************
002: Copyright (c) 2005 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.sco;
019:
020: import java.awt.geom.Point2D;
021: import java.io.ObjectStreamException;
022:
023: import org.jpox.StateManager;
024: import org.jpox.state.FetchPlanState;
025:
026: /**
027: * A mutable second-class java.awt.Point object.
028: *
029: * @version $Revision: 1.26 $
030: */
031: public class Point extends java.awt.Point implements SCO {
032: private transient StateManager ownerSM;
033: private transient Object owner;
034: private transient String fieldName;
035:
036: /**
037: * Creates a <tt>Point</tt> object. Assigns owning object and field name.
038: * @param ownerSM the owning object
039: * @param fieldName the owning field name
040: */
041: public Point(StateManager ownerSM, String fieldName) {
042: super ();
043:
044: if (ownerSM != null) {
045: this .ownerSM = ownerSM;
046: this .owner = ownerSM.getObject();
047: }
048: this .fieldName = fieldName;
049: }
050:
051: /**
052: * Method to initialise the SCO for use.
053: */
054: public void initialise() {
055: }
056:
057: /**
058: * Method to initialise the SCO from an existing value.
059: * @param o The Object
060: * @param forInsert Whether the object needs inserting in the datastore with this value
061: * @param forUpdate Whether to update the datastore with this value
062: */
063: public void initialise(Object o, boolean forInsert,
064: boolean forUpdate) {
065: super .setLocation((java.awt.Point) o);
066: }
067:
068: /**
069: * Accessor for the unwrapped value that we are wrapping.
070: * @return The unwrapped value
071: */
072: public Object getValue() {
073: return new java.awt.Point(this );
074: }
075:
076: /**
077: * Creates and returns a copy of this object.
078: *
079: * <p>Mutable second-class Objects are required to provide a public
080: * clone method in order to allow for copying PersistenceCapable
081: * objects. In contrast to Object.clone(), this method must not throw a
082: * CloneNotSupportedException.
083: * @return A clone of the object
084: */
085: public Object clone() {
086: Object obj = super .clone();
087:
088: ((Point) obj).unsetOwner();
089:
090: return obj;
091: }
092:
093: /**
094: * Mutator for the location.
095: * @param x The location x
096: * @param y The location y
097: **/
098: public void setLocation(double x, double y) {
099: super .setLocation(x, y);
100: makeDirty();
101: }
102:
103: /**
104: * Mutator for the location.
105: * @param x The location x
106: * @param y The location y
107: **/
108: public void setLocation(int x, int y) {
109: super .setLocation(x, y);
110: makeDirty();
111: }
112:
113: /**
114: * Mutator for the location.
115: * @param point The location
116: **/
117: public void setLocation(java.awt.Point point) {
118: super .setLocation(point);
119: makeDirty();
120: }
121:
122: /**
123: * Mutator for the location.
124: * @param point The location
125: **/
126: public void setLocation(Point2D point) {
127: super .setLocation(point);
128: makeDirty();
129: }
130:
131: /* (non-Javadoc)
132: * @see java.awt.Point#move(int, int)
133: */
134: public void move(int x, int y) {
135: super .move(x, y);
136: makeDirty();
137: }
138:
139: /* (non-Javadoc)
140: * @see java.awt.Point#translate(int, int)
141: */
142: public void translate(int dx, int dy) {
143: super .translate(dx, dy);
144: makeDirty();
145: }
146:
147: /**
148: * Utility to unset the owner.
149: **/
150: public void unsetOwner() {
151: owner = null;
152: ownerSM = null;
153: fieldName = null;
154: }
155:
156: /**
157: * Accessor for the owner.
158: * @return The owner
159: **/
160: public Object getOwner() {
161: return owner;
162: }
163:
164: /**
165: * Accessor for the field name
166: * @return The field name
167: **/
168: public String getFieldName() {
169: return this .fieldName;
170: }
171:
172: /**
173: * Utility to mark the object as dirty
174: **/
175: public void makeDirty() {
176: if (ownerSM != null) {
177: ownerSM.getObjectManager().getApiAdapter().makeFieldDirty(
178: owner, fieldName);
179: }
180: }
181:
182: /**
183: * Method to detach a copy of this object.
184: * @param state State for detachment process
185: * @return The detached object
186: */
187: public Object detachCopy(FetchPlanState state) {
188: return new java.awt.Point(this );
189: }
190:
191: /**
192: * Method to attach the passed value.
193: * @param value The new value
194: */
195: public void attachCopy(Object value) {
196: double oldX = getX();
197: double oldY = getY();
198: initialise(value, false, true);
199:
200: // Check if the field has changed, and set the owner field as dirty if necessary
201: double newX = ((java.awt.Point) value).getX();
202: double newY = ((java.awt.Point) value).getY();
203: if (oldX != newX || oldY != newY) {
204: makeDirty();
205: }
206: }
207:
208: /**
209: * The writeReplace method is called when ObjectOutputStream is preparing to write the object to the stream. The
210: * ObjectOutputStream checks whether the class defines the writeReplace method. If the method is defined, the
211: * writeReplace method is called to allow the object to designate its replacement in the stream. The object returned
212: * should be either of the same type as the object passed in or an object that when read and resolved will result in
213: * an object of a type that is compatible with all references to the object.
214: *
215: * @return the replaced object
216: * @throws ObjectStreamException
217: */
218: protected Object writeReplace() throws ObjectStreamException {
219: return new java.awt.Point(this.getLocation());
220: }
221: }
|