001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.perseus.model;
027:
028: import org.w3c.dom.DOMException;
029:
030: import com.sun.perseus.j2d.RGB;
031: import com.sun.perseus.j2d.Transform;
032:
033: import com.sun.perseus.j2d.PaintDef;
034: import com.sun.perseus.j2d.PaintServer;
035: import com.sun.perseus.j2d.PaintTarget;
036:
037: /**
038: * The <code>PaintServerReference</code> object is used to handle
039: * references from a <code>GraphicsNode</code> to an actual
040: * <code>Paint</code> implementation. This is needed because
041: * paint references are not always resolved, for example when
042: * a document has forward references to paint servers.
043: *
044: * @version $Id: PaintServerReference.java,v 1.5 2006/06/29 10:47:33 ln156897 Exp $
045: */
046: public class PaintServerReference {
047: /**
048: * Paint used when the paint server reference has not been resolved
049: * yet. Fully transparent black.
050: */
051: static final RGB UNRESOLVED_PAINT = new RGB(0, 0, 0);
052:
053: static class Unresolved implements IDRef, PaintServer {
054: /**
055: * The referenced id.
056: */
057: String idRef;
058:
059: /**
060: * The CompositeGraphicsNode using this paint server reference.
061: */
062: PaintTarget paintTarget;
063:
064: /**
065: * The requested paintType, i.e., the paint traitName.
066: */
067: String paintType;
068:
069: /**
070: * @param ownerDocument the document this reference is part of.
071: * @param paintTarget the node which references the paint server.
072: * @param paintType the name of the paint trait (e.g., "fill" or
073: * "stroke")
074: * @param idRef the value of the referenced paint server id.
075: * @param IllegalArgumentException if ownerDocument, referencingNode,
076: * or idRef is null.
077: */
078: public Unresolved(final DocumentNode ownerDocument,
079: final PaintTarget paintTarget, final String paintType,
080: final String idRef) {
081: if (ownerDocument == null || paintTarget == null
082: || idRef == null || paintType == null) {
083: throw new IllegalArgumentException();
084: }
085:
086: this .paintTarget = paintTarget;
087: this .idRef = idRef;
088: this .paintType = paintType;
089: ownerDocument.resolveIDRef(this , idRef);
090: }
091:
092: /**
093: * <code>IDRef</code> implementation.
094: *
095: * @param ref the resolved referenced (mapped from the id passed to
096: * the setIdRef method).
097: */
098: public void resolveTo(final ElementNode ref) {
099: if (!(ref instanceof PaintServer)) {
100: throw new DOMException(
101: DOMException.INVALID_STATE_ERR,
102: Messages
103: .formatMessage(
104: Messages.ERROR_INVALID_PAINT_SERVER_REFERENCE,
105: new String[] { idRef,
106: ref.getNamespaceURI(),
107: ref.getLocalName() }));
108: }
109:
110: paintTarget.onPaintServerUpdate(paintType,
111: (PaintServer) ref);
112: }
113:
114: /**
115: * @return the PaintDef generated by the server.
116: */
117: public PaintDef getPaintDef() {
118: return UNRESOLVED_PAINT;
119: }
120:
121: /**
122: * @param paintType a key that the PaintTarget can use to characterize
123: * its interest in the PaintServer. For example, a PaintTarget
124: * may be interested in the Paint both for stroking and filling
125: * purposes.
126: * @param paintTarget the PaintServerTarget listening to changes to the
127: * paint generated by this PaintServer.
128: */
129: public void setPaintTarget(final String paintType,
130: final PaintTarget paintTarget) {
131: if (paintTarget != this .paintTarget) {
132: throw new Error();
133: }
134: }
135:
136: /**
137: * Called when the PaintServer is no longer used
138: */
139: public void dispose() {
140: }
141: }
142:
143: /**
144: * If the requested idRef is resolved, this method checks it is a reference
145: * to a PaintServer and returns that PaintServer.
146: *
147: * If the resquested idRef cannot be resolved, then the method returns and
148: * PaintServerReference.Unresolved instance.
149: *
150: * @param doc the Document scope.
151: * @param paintTarget the PaintServer observer.
152: * @param traitName the name of the trait for which the server is seeked.
153: * @param idRef the id of the paint server reference.
154: */
155: public static PaintServer resolve(final DocumentNode doc,
156: final PaintTarget paintTarget, final String traitName,
157: final String idRef) {
158: // Check if the paint server reference is already resolved.
159: ElementNode ref = (ElementNode) doc.getElementById(idRef);
160: if (ref != null) {
161: if (!(ref instanceof PaintElement)) {
162: throw new DOMException(
163: DOMException.INVALID_STATE_ERR,
164: Messages
165: .formatMessage(
166: Messages.ERROR_INVALID_PAINT_SERVER_REFERENCE,
167: new String[] { idRef,
168: ref.getNamespaceURI(),
169: ref.getLocalName() }));
170: } else {
171: return ((PaintElement) ref).getPaintServer(traitName,
172: paintTarget);
173: }
174: }
175:
176: // The paint server reference is not resolved yet. We create an
177: // unresolved PaintServer.
178: return new PaintServerReference.Unresolved(doc, paintTarget,
179: traitName, idRef);
180: }
181: }
|