KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > dom > svg > SVGLocatableSupport


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.dom.svg;
19
20 import java.awt.geom.AffineTransform JavaDoc;
21 import java.awt.geom.NoninvertibleTransformException JavaDoc;
22
23 import org.apache.batik.css.engine.SVGCSSEngine;
24 import org.w3c.dom.DOMException JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.svg.SVGElement;
27 import org.w3c.dom.svg.SVGException;
28 import org.w3c.dom.svg.SVGFitToViewBox;
29 import org.w3c.dom.svg.SVGMatrix;
30 import org.w3c.dom.svg.SVGRect;
31
32 /**
33  * This class provides support for the SVGLocatable interface.
34  *
35  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
36  * @version $Id: SVGLocatableSupport.java,v 1.11 2005/02/27 02:08:51 deweese Exp $
37  */

38 public class SVGLocatableSupport {
39     /**
40      * Creates a new SVGLocatable element.
41      */

42     public SVGLocatableSupport() {
43     }
44     
45     /**
46      * To implement {@link
47      * org.w3c.dom.svg.SVGLocatable#getNearestViewportElement()}.
48      */

49     public static SVGElement getNearestViewportElement(Element JavaDoc e) {
50         Element JavaDoc elt = e;
51         while (elt != null) {
52             elt = SVGCSSEngine.getParentCSSStylableElement(elt);
53             if (elt instanceof SVGFitToViewBox) {
54                 break;
55             }
56         }
57         return (SVGElement)elt;
58     }
59
60     /**
61      * To implement {@link
62      * org.w3c.dom.svg.SVGLocatable#getFarthestViewportElement()}.
63      */

64     public static SVGElement getFarthestViewportElement(Element JavaDoc elt) {
65         return (SVGElement)elt.getOwnerDocument().getDocumentElement();
66     }
67
68     /**
69      * To implement {@link org.w3c.dom.svg.SVGLocatable#getBBox()}.
70      */

71     public static SVGRect getBBox(Element JavaDoc elt) {
72         final SVGOMElement svgelt = (SVGOMElement)elt;
73         SVGContext svgctx = svgelt.getSVGContext();
74         if (svgctx == null) return null;
75         if (svgctx.getBBox() == null) return null;
76
77         return new SVGRect() {
78                 public float getX() {
79                     return (float)svgelt.getSVGContext().getBBox().getX();
80                 }
81                 public void setX(float x) throws DOMException JavaDoc {
82                     throw svgelt.createDOMException
83                         (DOMException.NO_MODIFICATION_ALLOWED_ERR,
84                          "readonly.rect", null);
85                 }
86                 public float getY() {
87                     return (float)svgelt.getSVGContext().getBBox().getY();
88                 }
89                 public void setY(float y) throws DOMException JavaDoc {
90                     throw svgelt.createDOMException
91                         (DOMException.NO_MODIFICATION_ALLOWED_ERR,
92                          "readonly.rect", null);
93                 }
94                 public float getWidth() {
95                     return (float)svgelt.getSVGContext().getBBox().getWidth();
96                 }
97                 public void setWidth(float width) throws DOMException JavaDoc {
98                     throw svgelt.createDOMException
99                         (DOMException.NO_MODIFICATION_ALLOWED_ERR,
100                          "readonly.rect", null);
101                 }
102                 public float getHeight() {
103                     return (float)svgelt.getSVGContext().getBBox().getHeight();
104                 }
105                 public void setHeight(float height) throws DOMException JavaDoc {
106                     throw svgelt.createDOMException
107                         (DOMException.NO_MODIFICATION_ALLOWED_ERR,
108                          "readonly.rect", null);
109                 }
110             };
111     }
112
113     /**
114      * To implement {@link org.w3c.dom.svg.SVGLocatable#getCTM()}.
115      */

116     public static SVGMatrix getCTM(Element JavaDoc elt) {
117         final SVGOMElement svgelt = (SVGOMElement)elt;
118         return new AbstractSVGMatrix() {
119                 protected AffineTransform JavaDoc getAffineTransform() {
120                     return svgelt.getSVGContext().getCTM();
121             }
122         };
123     }
124
125     /**
126      * To implement {@link org.w3c.dom.svg.SVGLocatable#getScreenCTM()}.
127      */

128     public static SVGMatrix getScreenCTM(Element JavaDoc elt) {
129         final SVGOMElement svgelt = (SVGOMElement)elt;
130         return new AbstractSVGMatrix() {
131                 protected AffineTransform JavaDoc getAffineTransform() {
132                     SVGContext context = svgelt.getSVGContext();
133                     AffineTransform JavaDoc ret = context.getGlobalTransform();
134                     AffineTransform JavaDoc scrnTrans = context.getScreenTransform();
135                     if (scrnTrans != null)
136                         ret.preConcatenate(scrnTrans);
137                     return ret;
138                 }
139             };
140     }
141
142     /**
143      * To implement {@link
144      * org.w3c.dom.svg.SVGLocatable#getTransformToElement(SVGElement)}.
145      */

146     public static SVGMatrix getTransformToElement(Element JavaDoc elt,
147                                                   SVGElement element)
148     throws SVGException {
149         final SVGOMElement currentElt = (SVGOMElement)elt;
150         final SVGOMElement targetElt = (SVGOMElement)element;
151         return new AbstractSVGMatrix() {
152                 protected AffineTransform JavaDoc getAffineTransform() {
153                     AffineTransform JavaDoc cat =
154                         currentElt.getSVGContext().getGlobalTransform();
155                     if (cat == null) {
156                         cat = new AffineTransform JavaDoc();
157                     }
158                     AffineTransform JavaDoc tat =
159                         targetElt.getSVGContext().getGlobalTransform();
160                     if (tat == null) {
161                         tat = new AffineTransform JavaDoc();
162                     }
163                     AffineTransform JavaDoc at = new AffineTransform JavaDoc(cat);
164                     try {
165                         at.preConcatenate(tat.createInverse());
166                         return at;
167                     } catch (NoninvertibleTransformException JavaDoc ex) {
168                         throw currentElt.createSVGException
169                             (SVGException.SVG_MATRIX_NOT_INVERTABLE,
170                              "noninvertiblematrix",
171                              null);
172                     }
173                 }
174             };
175     }
176 }
177
Popular Tags