KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > SVGGElementBridge


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge;
19
20 import java.awt.RenderingHints JavaDoc;
21 import java.awt.geom.Rectangle2D JavaDoc;
22
23 import org.apache.batik.gvt.CompositeGraphicsNode;
24 import org.apache.batik.gvt.GraphicsNode;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.events.MutationEvent JavaDoc;
28
29 /**
30  * Bridge class for the <g> element.
31  *
32  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
33  * @version $Id: SVGGElementBridge.java,v 1.26 2004/08/20 19:29:46 deweese Exp $
34  */

35 public class SVGGElementBridge extends AbstractGraphicsNodeBridge {
36
37     /**
38      * Constructs a new bridge for the &lt;g> element.
39      */

40     public SVGGElementBridge() {}
41
42     /**
43      * Returns 'g'.
44      */

45     public String JavaDoc getLocalName() {
46         return SVG_G_TAG;
47     }
48
49     /**
50      * Returns a new instance of this bridge.
51      */

52     public Bridge getInstance() {
53         return new SVGGElementBridge();
54     }
55
56     /**
57      * Creates a <tt>GraphicsNode</tt> according to the specified parameters.
58      *
59      * @param ctx the bridge context to use
60      * @param e the element that describes the graphics node to build
61      * @return a graphics node that represents the specified element
62      */

63     public GraphicsNode createGraphicsNode(BridgeContext ctx, Element JavaDoc e) {
64         CompositeGraphicsNode gn =
65             (CompositeGraphicsNode)super.createGraphicsNode(ctx, e);
66     if (gn == null)
67         return null;
68
69         // 'color-rendering'
70
RenderingHints JavaDoc hints = null;
71         hints = CSSUtilities.convertColorRendering(e, hints);
72         if (hints != null)
73             gn.setRenderingHints(hints);
74
75         // 'enable-background'
76
Rectangle2D JavaDoc r = CSSUtilities.convertEnableBackground(e);
77         if (r != null)
78             gn.setBackgroundEnable(r);
79
80         return gn;
81     }
82
83     /**
84      * Creates a <tt>CompositeGraphicsNode</tt>.
85      */

86     protected GraphicsNode instantiateGraphicsNode() {
87         return new CompositeGraphicsNode();
88     }
89
90     /**
91      * Returns true as the &lt;g> element is a container.
92      */

93     public boolean isComposite() {
94         return true;
95     }
96
97     // BridgeUpdateHandler implementation //////////////////////////////////
98

99     /**
100      * Invoked when an MutationEvent of type 'DOMNodeInserted' is fired.
101      */

102     public void handleDOMNodeInsertedEvent(MutationEvent JavaDoc evt) {
103         if ( evt.getTarget() instanceof Element JavaDoc ){
104             handleElementAdded((CompositeGraphicsNode)node,
105                                e,
106                                (Element JavaDoc)evt.getTarget());
107         }
108     }
109
110     /**
111      * Invoked when an MutationEvent of type 'DOMNodeInserted' is fired.
112      */

113     public void handleElementAdded(CompositeGraphicsNode gn,
114                                    Node parent,
115                                    Element JavaDoc childElt) {
116         // build the graphics node
117
GVTBuilder builder = ctx.getGVTBuilder();
118         GraphicsNode childNode = builder.build(ctx, childElt);
119         if (childNode == null) {
120             return; // the added element is not a graphic element
121
}
122         
123         // Find the index where the GraphicsNode should be added
124
int idx = -1;
125         for(Node ps = childElt.getPreviousSibling(); ps != null;
126             ps = ps.getPreviousSibling()) {
127             if (ps.getNodeType() != Node.ELEMENT_NODE)
128                 continue;
129             Element JavaDoc pse = (Element JavaDoc)ps;
130             GraphicsNode psgn = ctx.getGraphicsNode(pse);
131             while ((psgn != null) && (psgn.getParent() != gn)) {
132                 // In some cases the GN linked is
133
// a child (in particular for images).
134
psgn = psgn.getParent();
135             }
136             if (psgn == null)
137                 continue;
138             idx = gn.indexOf(psgn);
139             if (idx == -1)
140                 continue;
141             break;
142         }
143         // insert after prevSibling, if
144
// it was -1 this becomes 0 (first slot)
145
idx++;
146         gn.add(idx, childNode);
147     }
148 }
149
Popular Tags