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.RenderGraphics;
029:
030: import com.sun.perseus.util.SVGConstants;
031:
032: import com.sun.perseus.j2d.Transform;
033: import com.sun.perseus.j2d.Box;
034:
035: /**
036: * The <code>Switch</code> class is a simple extension of the
037: * <code>Group</code> class which stops rendering its children
038: * after one as rendered (i.e., the child's canRender method
039: * returns true).
040: * This implements the behavior required by the SVG Tiny
041: * specification of the <code><switch></code> element.
042: *
043: * @version $Id: Switch.java,v 1.7 2006/06/29 10:47:35 ln156897 Exp $
044: */
045: public class Switch extends Group {
046: /**
047: * Constructor.
048: *
049: * @param ownerDocument this element's owner <code>DocumentNode</code>
050: */
051: public Switch(final DocumentNode ownerDocument) {
052: super (ownerDocument);
053: }
054:
055: /**
056: * @return the SVGConstants.SVG_SWITCH_TAG value
057: */
058: public String getLocalName() {
059: return SVGConstants.SVG_SWITCH_TAG;
060: }
061:
062: /**
063: * Used by <code>DocumentNode</code> to create a new instance from
064: * a prototype <code>Switch</code>.
065: *
066: * @param doc the <code>DocumentNode</code> for which a new node is
067: * should be created.
068: * @return a new <code>Switch</code> for the requested document.
069: */
070: public ElementNode newInstance(final DocumentNode doc) {
071: return new Switch(doc);
072: }
073:
074: /**
075: * Returns the <code>ModelNode</code>, if any, hit by the
076: * point at coordinate x/y.
077: *
078: * @param pt the x/y coordinate. Should never be null and be
079: * of size two. If not, the behavior is unspecified.
080: * The coordinates are in viewport space.
081: * @return the <tt>ModelNode</tt> hit at the given point or null
082: * if none was hit.
083: */
084: public ModelNode nodeHitAt(final float[] pt) {
085: // If a node does not render, it is never hit
086: if (canRenderState != 0) {
087: return null;
088: }
089:
090: // Check for a hit on children
091: ModelNode c = lastChild;
092: while (c != null) {
093: if ((c.canRenderState & CAN_RENDER_CONDITIONS_MET_BITS) == 0) {
094: return c.nodeHitAt(pt);
095: }
096: c = c.prevSibling;
097: }
098:
099: return null;
100: }
101:
102: /**
103: * Returns the <code>ModelNode</code>, if any, hit by the
104: * point at coordinate x/y in the proxy tree starting at
105: * proxy.
106: *
107: * @param pt the x/y coordinate. Should never be null and be
108: * of size two. If not, the behavior is unspecified.
109: * The coordinates are in viewport space.
110: * @param proxy the root of the proxy tree to test.
111: * @return the <tt>ModelNode</tt> hit at the given point or null
112: * if none was hit.
113: */
114: ModelNode proxyNodeHitAt(final float[] pt,
115: final ElementNodeProxy proxy) {
116: // If a node does not render, it is never hit
117: if (canRenderState != 0) {
118: return null;
119: }
120:
121: // Check for a hit on the proxy's expanded children
122: ElementNodeProxy c = (ElementNodeProxy) proxy
123: .getLastExpandedChild();
124: while (c != null) {
125: if ((c.proxied.canRenderState & CAN_RENDER_CONDITIONS_MET_BITS) == 0) {
126: return c.nodeHitAt(pt);
127: }
128: c = (ElementNodeProxy) c.prevSibling;
129: }
130:
131: return null;
132: }
133:
134: /**
135: * @param rg this method paints this node into the input
136: * <tt>RenderGraphics</tt>
137: */
138: public void paint(final RenderGraphics rg) {
139: if (canRenderState != 0) {
140: return;
141: }
142:
143: if (firstChild == null) {
144: return;
145: }
146:
147: //
148: // Only go up to the first child which
149: // renders
150: //
151: ElementNode c = firstChild;
152: while (c != null) {
153: // Paint child
154: c.paint(rg);
155:
156: // Exit the loop if the child rendered
157: if ((c.canRenderState & CAN_RENDER_CONDITIONS_MET_BITS) == 0) {
158: break;
159: }
160:
161: c = (ElementNode) c.nextSibling;
162: }
163: }
164:
165: /**
166: * @param bbox the bounding box to which this node's bounding box should be
167: * appended. That bounding box is in the target coordinate space. It
168: * may be null, in which case this node should create a new one.
169: * @param t the transform to apply from the node's coordinate space to
170: * the target coordinate space. May be null for the identity
171: * transform.
172: * @return the node's bounding box in the target coordinate space.
173: */
174: public Box addBBox(Box bbox, final Transform t) {
175: ModelNode c = getFirstChildNode();
176: while (c != null) {
177: if ((c.canRenderState & CAN_RENDER_CONDITIONS_MET_BITS) == 0) {
178: bbox = c.addBBox(bbox, c.appendTransform(t, null));
179: break;
180: }
181: c = c.nextSibling;
182: }
183:
184: return bbox;
185: }
186:
187: }
|