001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.bridge;
020:
021: import org.apache.batik.gvt.CompositeGraphicsNode;
022: import org.apache.batik.gvt.GraphicsNode;
023:
024: import org.w3c.dom.Element;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.svg.SVGTests;
027:
028: /**
029: * Bridge class for the <switch> element.
030: *
031: * @author <a href="mailto:tkormann@apache.org">Thierry Kormann</a>
032: * @version $Id: SVGSwitchElementBridge.java 491178 2006-12-30 06:18:34Z cam $
033: */
034: public class SVGSwitchElementBridge extends SVGGElementBridge {
035:
036: /**
037: * The child element that was chosen for rendering according to the
038: * test attributes.
039: */
040: protected Element selectedChild;
041:
042: /**
043: * Constructs a new bridge for the <switch> element.
044: */
045: public SVGSwitchElementBridge() {
046: }
047:
048: /**
049: * Returns 'switch'.
050: */
051: public String getLocalName() {
052: return SVG_SWITCH_TAG;
053: }
054:
055: /**
056: * Returns a new instance of this bridge.
057: */
058: public Bridge getInstance() {
059: return new SVGSwitchElementBridge();
060: }
061:
062: /**
063: * Creates a <tt>GraphicsNode</tt> according to the specified parameters.
064: *
065: * @param ctx the bridge context to use
066: * @param e the element that describes the graphics node to build
067: * @return a graphics node that represents the specified element
068: */
069: public GraphicsNode createGraphicsNode(BridgeContext ctx, Element e) {
070: GraphicsNode refNode = null;
071: GVTBuilder builder = ctx.getGVTBuilder();
072: selectedChild = null;
073: for (Node n = e.getFirstChild(); n != null; n = n
074: .getNextSibling()) {
075: if (n.getNodeType() == Node.ELEMENT_NODE) {
076: Element ref = (Element) n;
077: if (n instanceof SVGTests
078: && SVGUtilities.matchUserAgent(ref, ctx
079: .getUserAgent())) {
080: selectedChild = ref;
081: refNode = builder.build(ctx, ref);
082: break;
083: }
084: }
085: }
086:
087: if (refNode == null) {
088: return null;
089: }
090:
091: CompositeGraphicsNode group = (CompositeGraphicsNode) super
092: .createGraphicsNode(ctx, e);
093: if (group == null) {
094: return null;
095: }
096:
097: group.add(refNode);
098:
099: return group;
100: }
101:
102: /**
103: * Returns true as the <switch> element is not a container.
104: */
105: public boolean isComposite() {
106: return false;
107: }
108:
109: // BridgeUpdateHandler implementation //////////////////////////////////
110:
111: /**
112: * Disposes this BridgeUpdateHandler and releases all resources.
113: */
114: public void dispose() {
115: selectedChild = null;
116: super .dispose();
117: }
118:
119: /**
120: * Responds to the insertion of a child element by re-evaluating the
121: * test attributes.
122: */
123: protected void handleElementAdded(CompositeGraphicsNode gn,
124: Node parent, Element childElt) {
125: for (Node n = childElt.getPreviousSibling(); n != null; n = n
126: .getPreviousSibling()) {
127: if (n == childElt) {
128: return;
129: }
130: }
131: if (childElt instanceof SVGTests
132: && SVGUtilities.matchUserAgent(childElt, ctx
133: .getUserAgent())) {
134: if (selectedChild != null) {
135: gn.remove(0);
136: disposeTree(selectedChild);
137: }
138: selectedChild = childElt;
139: GVTBuilder builder = ctx.getGVTBuilder();
140: GraphicsNode refNode = builder.build(ctx, childElt);
141: if (refNode != null) {
142: gn.add(refNode);
143: }
144: }
145: }
146:
147: /**
148: * Responds to the removal of a child element by re-evaluating the
149: * test attributes.
150: */
151: protected void handleChildElementRemoved(Element e) {
152: CompositeGraphicsNode gn = (CompositeGraphicsNode) node;
153: if (selectedChild == e) {
154: gn.remove(0);
155: disposeTree(selectedChild);
156: selectedChild = null;
157: GraphicsNode refNode = null;
158: GVTBuilder builder = ctx.getGVTBuilder();
159: for (Node n = e.getNextSibling(); n != null; n = n
160: .getNextSibling()) {
161: if (n.getNodeType() == Node.ELEMENT_NODE) {
162: Element ref = (Element) n;
163: if (n instanceof SVGTests
164: && SVGUtilities.matchUserAgent(ref, ctx
165: .getUserAgent())) {
166: refNode = builder.build(ctx, ref);
167: selectedChild = ref;
168: break;
169: }
170: }
171: }
172:
173: if (refNode != null) {
174: gn.add(refNode);
175: }
176: }
177: }
178: }
|