KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > SimpleImageHandler


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.svggen;
19
20 import java.awt.Image JavaDoc;
21 import java.awt.geom.AffineTransform JavaDoc;
22 import java.awt.image.RenderedImage JavaDoc;
23 import java.awt.image.renderable.RenderableImage JavaDoc;
24
25 import org.w3c.dom.Element JavaDoc;
26
27 /**
28  * Implements the <tt>GenericImageHandler</tt> interface and only
29  * uses &lt;image&gt; elements. This class delegates to the
30  * <tt>ImageHandler</tt> interface for handling the xlink:href
31  * attribute on the elements it creates.
32  *
33  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
34  * @version $Id: SimpleImageHandler.java,v 1.6 2004/08/18 07:15:09 vhardy Exp $
35  */

36 public class SimpleImageHandler implements GenericImageHandler, SVGSyntax, ErrorConstants {
37     // duplicate the string here to remove dependencies on
38
// org.apache.batik.dom.util.XLinkSupport
39
static final String JavaDoc XLINK_NAMESPACE_URI =
40         "http://www.w3.org/1999/xlink";
41
42     /**
43      * <tt>ImageHandler</tt> which handles xlink:href attribute setting
44      */

45     protected ImageHandler imageHandler;
46
47     /**
48      * @param imageHandler ImageHandler handling the xlink:href on the
49      * &lt;image&gt; elements this GenericImageHandler implementation
50      * creates.
51      */

52     public SimpleImageHandler(ImageHandler imageHandler){
53         if (imageHandler == null){
54             throw new IllegalArgumentException JavaDoc();
55         }
56
57         this.imageHandler = imageHandler;
58     }
59
60     /**
61      * This <tt>GenericImageHandler</tt> implementation does not
62      * need to interact with the DOMTreeManager.
63      */

64     public void setDOMTreeManager(DOMTreeManager domTreeManager){
65     }
66
67     /**
68      * Creates an Element which can refer to an image.
69      * Note that no assumptions should be made by the caller about the
70      * corresponding SVG tag.
71      */

72     public Element JavaDoc createElement(SVGGeneratorContext generatorContext) {
73         // Create a DOM Element in SVG namespace to refer to an image
74
Element JavaDoc imageElement =
75             generatorContext.getDOMFactory().createElementNS
76             (SVG_NAMESPACE_URI, SVG_IMAGE_TAG);
77
78         return imageElement;
79     }
80
81     /**
82      * The handler sets the xlink:href tag and returns a transform
83      */

84     public AffineTransform JavaDoc handleImage(Image JavaDoc image,
85                                        Element JavaDoc imageElement,
86                                        int x, int y,
87                                        int width, int height,
88                                        SVGGeneratorContext generatorContext) {
89
90         int imageWidth = image.getWidth(null);
91         int imageHeight = image.getHeight(null);
92         if(imageWidth == 0 || imageHeight == 0 ||
93            width == 0 || height == 0) {
94
95             // Forget about it
96
handleEmptyImage(imageElement);
97
98         } else {
99             imageHandler.handleImage(image, imageElement, generatorContext);
100             setImageAttributes(imageElement, x, y, width, height,
101                                generatorContext);
102         }
103         return null;
104     }
105
106     /**
107      * The handler sets the xlink:href tag and returns a transform
108      */

109     public AffineTransform JavaDoc handleImage(RenderedImage JavaDoc image,
110                                        Element JavaDoc imageElement,
111                                        int x, int y,
112                                        int width, int height,
113                                        SVGGeneratorContext generatorContext) {
114
115         int imageWidth = image.getWidth();
116         int imageHeight = image.getHeight();
117
118         if(imageWidth == 0 || imageHeight == 0 ||
119            width == 0 || height == 0) {
120
121             // Forget about it
122
handleEmptyImage(imageElement);
123
124         } else {
125             imageHandler.handleImage(image, imageElement, generatorContext);
126             setImageAttributes(imageElement, x, y, width, height,
127                                generatorContext);
128         }
129         return null;
130     }
131
132     /**
133      * The handler sets the xlink:href tag and returns a transform
134      */

135     public AffineTransform JavaDoc handleImage(RenderableImage JavaDoc image,
136                                        Element JavaDoc imageElement,
137                                        double x, double y,
138                                        double width, double height,
139                                        SVGGeneratorContext generatorContext) {
140
141         double imageWidth = image.getWidth();
142         double imageHeight = image.getHeight();
143
144         if(imageWidth == 0 || imageHeight == 0 ||
145            width == 0 || height == 0) {
146
147             // Forget about it
148
handleEmptyImage(imageElement);
149
150         } else {
151             imageHandler.handleImage(image, imageElement, generatorContext);
152             setImageAttributes(imageElement, x, y, width, height, generatorContext);
153         }
154         return null;
155     }
156
157     /**
158      * Sets the x/y/width/height attributes on the &lt;image&gt;
159      * element.
160      */

161     protected void setImageAttributes(Element JavaDoc imageElement,
162                                       double x,
163                                       double y,
164                                       double width,
165                                       double height,
166                                       SVGGeneratorContext generatorContext) {
167         imageElement.setAttributeNS(null,
168                                     SVG_X_ATTRIBUTE,
169                                     generatorContext.doubleString(x));
170         imageElement.setAttributeNS(null,
171                                     SVG_Y_ATTRIBUTE,
172                                     generatorContext.doubleString(y));
173         imageElement.setAttributeNS(null,
174                                     SVG_WIDTH_ATTRIBUTE,
175                                     generatorContext.doubleString(width));
176         imageElement.setAttributeNS(null,
177                                     SVG_HEIGHT_ATTRIBUTE,
178                                     generatorContext.doubleString(height));
179         imageElement.setAttributeNS(null,
180                                     SVG_PRESERVE_ASPECT_RATIO_ATTRIBUTE,
181                                     SVG_NONE_VALUE);
182     }
183               
184     protected void handleEmptyImage(Element JavaDoc imageElement) {
185         imageElement.setAttributeNS(XLINK_NAMESPACE_URI,
186                                     ATTR_XLINK_HREF, "");
187         imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE, "0");
188         imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE, "0");
189     }
190
191 }
192
Popular Tags