001: /*
002: * $RCSfile: RenderContextProxy.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:56:53 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.rmi;
013:
014: import java.awt.Rectangle;
015: import java.awt.RenderingHints;
016: import java.awt.Shape;
017: import java.awt.geom.AffineTransform;
018: import java.awt.image.renderable.RenderContext;
019: import java.io.IOException;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.io.Serializable;
023: import javax.media.jai.ROIShape;
024:
025: /**
026: * This class is a serializable proxy for a RenderContext from which the
027: * RenderContext may be reconstituted.
028: *
029: *
030: * @since EA3
031: */
032: public class RenderContextProxy implements Serializable {
033: /** The RenderContext. */
034: private transient RenderContext renderContext;
035:
036: /**
037: * Constructs a <code>DatabufferProxy</code> from a
038: * <code>Databuffer</code>.
039: *
040: * @param source The <code>Databuffer</code> to be serialized.
041: */
042: public RenderContextProxy(RenderContext source) {
043: renderContext = source;
044: }
045:
046: /**
047: * Retrieves the associated <code>Databuffer</code>.
048: * @return The (perhaps reconstructed) <code>Databuffer</code>.
049: */
050: public RenderContext getRenderContext() {
051: return renderContext;
052: }
053:
054: /**
055: * Serialize the <code>RenderContextProxy</code>.
056: *
057: * @param out The <code>ObjectOutputStream</code>.
058: */
059: private void writeObject(ObjectOutputStream out) throws IOException {
060: boolean isNull = renderContext == null;
061: out.writeBoolean(isNull);
062: if (isNull)
063: return;
064:
065: // Extract the affine transform.
066: AffineTransform usr2dev = renderContext.getTransform();
067:
068: // Create serializable form of the hints.
069: RenderingHintsProxy rhp = new RenderingHintsProxy(renderContext
070: .getRenderingHints());
071:
072: Shape aoi = renderContext.getAreaOfInterest();
073:
074: // Write serialized form to the stream.
075: out.writeObject(usr2dev);
076:
077: out.writeBoolean(aoi != null);
078: if (aoi != null) {
079: if (aoi instanceof Serializable) {
080: out.writeObject(aoi);
081: } else {
082: out.writeObject(new ROIShape(aoi));
083: }
084: }
085:
086: out.writeObject(rhp);
087: }
088:
089: /**
090: * Deserialize the <code>RenderContextProxy</code>.
091: *
092: * @param out The <code>ObjectInputStream</code>.
093: */
094: private void readObject(ObjectInputStream in) throws IOException,
095: ClassNotFoundException {
096:
097: if (in.readBoolean()) {
098: renderContext = null;
099: return;
100: }
101:
102: // Read serialized form from the stream.
103: AffineTransform usr2dev = (AffineTransform) in.readObject();
104:
105: Shape shape = null;
106: Object aoi = in.readBoolean() ? (Object) in.readObject() : null;
107: RenderingHintsProxy rhp = (RenderingHintsProxy) in.readObject();
108:
109: RenderingHints hints = rhp.getRenderingHints();
110:
111: // Restore the transient RenderContext.
112: if (aoi != null) {
113: if (aoi instanceof ROIShape) {
114: shape = ((ROIShape) aoi).getAsShape();
115: } else {
116: shape = (Shape) aoi;
117: }
118: }
119:
120: if (aoi == null && hints.isEmpty()) {
121: renderContext = new RenderContext(usr2dev);
122: } else if (aoi == null) {
123: renderContext = new RenderContext(usr2dev, hints);
124: } else if (hints.isEmpty()) {
125: renderContext = new RenderContext(usr2dev, shape);
126: } else {
127: renderContext = new RenderContext(usr2dev, shape, hints);
128: }
129: }
130: }
|