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 com.sun.perseus.j2d.Transform;
029:
030: import com.sun.perseus.j2d.Box;
031: import com.sun.perseus.j2d.PaintDef;
032: import com.sun.perseus.j2d.PaintServer;
033: import com.sun.perseus.j2d.PaintTarget;
034:
035: import java.util.Enumeration;
036: import java.util.Hashtable;
037: import java.util.Vector;
038:
039: /**
040: * Abstract base class for all elements which are paint server implementations.
041: *
042: * @version $Id: PaintElement.java,v 1.7 2006/06/29 10:47:33 ln156897 Exp $
043: */
044: public abstract class PaintElement extends ElementNode {
045: /**
046: * Defines whether the paint is in objectBoundingBox
047: * space or in userSpaceOnUse space.
048: */
049: boolean isObjectBBox = false;
050:
051: /**
052: * An array holding all current references (References instances) to this
053: * PaintElement.
054: */
055: Vector references = new Vector(1);
056:
057: /**
058: * @ownerDocument the DocumentNode this element belongs to.
059: */
060: public PaintElement(final DocumentNode ownerDocument) {
061: super (ownerDocument);
062: }
063:
064: /**
065: * @param paintType the type of paint for which a PaintServer should be
066: * computed. (e.g., "fill" or "stroke");
067: * @param paintTarget the PaintTarget for which the PaintServer should be
068: * computed.
069: * @return the PaintDef generated by the server.
070: */
071: public PaintServer getPaintServer(final String paintType,
072: final PaintTarget paintTarget) {
073: Reference ref = new Reference();
074: ref.setPaintTarget(paintType, paintTarget);
075: return ref;
076: }
077:
078: /**
079: * Notifies all references that the Paint definitions has been modified.
080: */
081: protected void notifyPaintChange() {
082: final int n = references.size();
083: for (int i = 0; i < n; i++) {
084: Reference ref = (Reference) references.elementAt(i);
085: ref.paintTarget.onPaintServerUpdate(ref.paintType, ref);
086: }
087: }
088:
089: /**
090: * Computes the paint in user space on use.
091: *
092: * @return the computed PaintDef.
093: */
094: protected abstract PaintDef computePaint();
095:
096: /**
097: * The Reference class is used to bind a paint property (e.g., fill or
098: * stroke) to its related PaintElement.
099: */
100: class Reference implements PaintServer {
101: /**
102: * The referencing node.
103: */
104: protected PaintTarget paintTarget;
105:
106: /**
107: * The referencing paintType.
108: */
109: protected String paintType;
110:
111: /**
112: * @return the PaintDef generated by the server.
113: */
114: public PaintDef getPaintDef() {
115: return computePaint();
116: }
117:
118: /**
119: * @param paintType a key that the PaintTarget can use to characterize
120: * its interest in the PaintServer. For example, a PaintTarget
121: * may be interested in the Paint both for stroking and filling
122: * purposes.
123: * @param paintTarget the PaintServerTarget listening to changes to the
124: * paint generated by this PaintServer.
125: */
126: public void setPaintTarget(final String paintType,
127: final PaintTarget paintTarget) {
128: this .paintType = paintType;
129: this .paintTarget = paintTarget;
130: references.addElement(this );
131: }
132:
133: /**
134: * Called when the PaintServer is no longer used.
135: */
136: public void dispose() {
137: references.removeElement(this);
138: }
139: }
140: }
|