KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > renderable > AbstractRable


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.ext.awt.image.renderable;
19
20 import java.awt.Rectangle JavaDoc;
21 import java.awt.RenderingHints JavaDoc;
22 import java.awt.Shape JavaDoc;
23 import java.awt.geom.AffineTransform JavaDoc;
24 import java.awt.geom.Rectangle2D JavaDoc;
25 import java.awt.image.RenderedImage JavaDoc;
26 import java.awt.image.renderable.RenderContext JavaDoc;
27 import java.awt.image.renderable.RenderableImage JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import org.apache.batik.ext.awt.image.PadMode;
36 import org.apache.batik.ext.awt.image.rendered.CachableRed;
37 import org.apache.batik.ext.awt.image.rendered.PadRed;
38 import org.apache.batik.ext.awt.image.rendered.RenderedImageCachableRed;
39
40 /**
41  * This is an abstract base class that takes care of most of the
42  * normal issues surrounding the implementation of the RenderableImage
43  * interface. It tries to make no assumptions about the subclass
44  * implementation.
45  *
46  * @author <a HREF="mailto:Thomas.DeWeeese@Kodak.com">Thomas DeWeese</a>
47  * @version $Id: AbstractRable.java,v 1.8 2005/03/27 08:58:33 cam Exp $
48  */

49 public abstract class AbstractRable implements Filter {
50
51     protected Vector JavaDoc srcs;
52     protected Map JavaDoc props = new HashMap JavaDoc();
53     protected long stamp = 0;
54
55     /**
56      * void constructor. The subclass must call one of the
57      * flavors of init before the object becomes usable.
58      * This is useful when the proper parameters to the init
59      * method need to be computed in the subclasses constructor.
60      */

61     protected AbstractRable() {
62         srcs = new Vector JavaDoc();
63     }
64
65     /**
66      * Construct an Abstract Rable from src.
67      * @param src will be the first (and only) member of the srcs
68      * Vector. The bounds of src are also used to set the bounds of
69      * this renderable.
70      */

71     protected AbstractRable(Filter src) {
72         init(src, null);
73     }
74
75     /**
76      * Construct an Abstract Rable from src and props.
77      * @param src will also be set as the first (and only) member of
78      * the srcs Vector.
79      * @param props use to initialize the properties on this renderable image.
80      */

81     protected AbstractRable(Filter src, Map JavaDoc props) {
82         init(src, props);
83     }
84
85     /**
86      * Construct an Abstract Rable from a list of sources.
87      * @param srcs This is used to initialize the srcs Vector.
88      * The bounds of this renderable will be the union of the bounds
89      * of all the sources in srcs. All the members of srcs must be
90      * CacheableRable otherwise an error will be thrown.
91      */

92     protected AbstractRable(List JavaDoc srcs) {
93         this(srcs, null);
94     }
95
96     /**
97      * Construct an Abstract Rable from a list of sources, and bounds.
98      * @param srcs This is used to initialize the srcs Vector. All
99      * the members of srcs must be CacheableRable otherwise an error
100      * will be thrown.
101      * @param props use to initialize the properties on this renderable image.
102      */

103     protected AbstractRable(List JavaDoc srcs, Map JavaDoc props) {
104         init(srcs, props);
105     }
106
107     /**
108      * Increments the time stamp. This should be called when ever
109      * the image changes in such a way that cached output should be
110      * discarded.
111      */

112     public final void touch() { stamp++; }
113
114       /**
115        * Returns the current modification timestamp on this Renderable
116        * node. This value will change whenever cached output data becomes
117        * invalid.
118        * @return Current modification timestamp value.
119        */

120     public long getTimeStamp() { return stamp; }
121
122     /**
123      * Initialize an Abstract Rable from src, bounds and props. This
124      * can be called long after the object is constructed to reset the
125      * state of the Renderable.
126      * @param src will become the first (and only) member of the srcs Vector.
127      */

128     protected void init(Filter src) {
129         touch();
130
131         this.srcs = new Vector JavaDoc(1);
132         if (src != null) {
133             this.srcs.add(src);
134         }
135     }
136
137     /**
138      * Initialize an Abstract Rable from src, bounds and props. This
139      * can be called long after the object is constructed to reset the
140      * state of the Renderable.
141      * @param src will also be set as the first (and only) member of
142      * the srcs Vector.
143      * @param props use to set the properties on this renderable image.
144      * Always clears the current properties (even if null).
145      */

146     protected void init(Filter src, Map JavaDoc props) {
147         init (src);
148         if(props != null){
149             this.props.putAll(props);
150         }
151     }
152
153     /**
154      * Initialize an Abstract Rable from a list of sources, and
155      * possibly a bounds. This can be called long after the object is
156      * constructed to reset the state of the Renderable.
157      * @param srcs Used the create a new srcs Vector (old sources are dropped).
158      */

159     protected void init(List JavaDoc srcs) {
160         touch();
161         this.srcs = new Vector JavaDoc(srcs);
162     }
163
164     /**
165      * Initialize an Abstract Rable from a list of sources, and
166      * possibly a bounds. This can be called long after the object is
167      * constructed to reset the state of the Renderable.
168      * @param srcs Used the create a new srcs Vector (old sources are dropped).
169      * @param props use to set the properties on this renderable image.
170      * Always clears the current properties (even if null).
171      */

172     protected void init(List JavaDoc srcs, Map JavaDoc props) {
173         init (srcs);
174         if(props != null)
175             this.props.putAll(props);
176     }
177
178     public Rectangle2D JavaDoc getBounds2D() {
179         Rectangle2D JavaDoc bounds = null;
180         if (this.srcs.size() != 0) {
181             Iterator JavaDoc i = srcs.iterator();
182             Filter src = (Filter)i.next();
183             bounds = (Rectangle2D JavaDoc)src.getBounds2D().clone();
184             Rectangle2D JavaDoc r;
185             while (i.hasNext()) {
186                 src = (Filter)i.next();
187                 r = src.getBounds2D();
188                 Rectangle2D.union(bounds, r, bounds);
189             }
190         }
191         return bounds;
192     }
193
194     public Vector JavaDoc getSources() {
195         return srcs;
196     }
197
198     public RenderedImage JavaDoc createDefaultRendering() {
199         return createScaledRendering(100, 100, null);
200     }
201
202     public RenderedImage JavaDoc createScaledRendering(int w, int h,
203                                            RenderingHints JavaDoc hints) {
204         float sX = w/getWidth();
205         float sY = h/getHeight();
206         float scale = Math.min(sX, sY);
207
208         AffineTransform JavaDoc at = AffineTransform.getScaleInstance(scale, scale);
209         RenderContext JavaDoc rc = new RenderContext JavaDoc(at, hints);
210
211         float dX = (getWidth()*scale)-w;
212         float dY = (getHeight()*scale)-h;
213
214         RenderedImage JavaDoc ri = createRendering(rc);
215         CachableRed cr = RenderedImageCachableRed.wrap(ri);
216         return new PadRed(cr, new Rectangle JavaDoc((int)(dX/2), (int)(dY/2), w, h),
217                           PadMode.ZERO_PAD, null);
218     }
219
220     public float getMinX() {
221         return (float)getBounds2D().getX();
222     }
223     public float getMinY() {
224         return (float)getBounds2D().getY();
225     }
226     public float getWidth() {
227         return (float)getBounds2D().getWidth();
228     }
229     public float getHeight() {
230         return (float)getBounds2D().getHeight();
231     }
232
233     public Object JavaDoc getProperty(String JavaDoc name) {
234         Object JavaDoc ret = props.get(name);
235         if (ret != null) return ret;
236         Iterator JavaDoc i = srcs.iterator();
237         while (i.hasNext()) {
238             RenderableImage JavaDoc ri = (RenderableImage JavaDoc)i.next();
239             ret = ri.getProperty(name);
240             if (ret != null) return ret;
241         }
242         return null;
243     }
244
245     public String JavaDoc [] getPropertyNames() {
246         Set JavaDoc keys = props.keySet();
247         Iterator JavaDoc iter = keys.iterator();
248         String JavaDoc [] ret = new String JavaDoc[keys.size()];
249         int i=0;
250         while (iter.hasNext()) {
251             ret[i++] = (String JavaDoc)iter.next();
252         }
253
254         iter = srcs.iterator();
255         while (iter.hasNext()) {
256             RenderableImage JavaDoc ri = (RenderableImage JavaDoc)iter.next();
257             String JavaDoc [] srcProps = ri.getPropertyNames();
258             if (srcProps.length != 0) {
259                 String JavaDoc [] tmp = new String JavaDoc[ret.length+srcProps.length];
260                 System.arraycopy(tmp,0,tmp,0,ret.length);
261                 System.arraycopy(tmp,ret.length,srcProps,0,srcProps.length);
262                 ret = tmp;
263             }
264         }
265
266         return ret;
267     }
268
269     public boolean isDynamic() { return false; }
270
271     public Shape JavaDoc getDependencyRegion(int srcIndex,
272                                      Rectangle2D JavaDoc outputRgn) {
273         if ((srcIndex < 0) || (srcIndex > srcs.size()))
274             throw new IndexOutOfBoundsException JavaDoc
275                 ("Nonexistant source requested.");
276
277         // We only depend on our source for stuff that is inside
278
// our bounds...
279
Rectangle2D JavaDoc srect = (Rectangle2D JavaDoc)outputRgn.clone();
280         Rectangle2D JavaDoc bounds = getBounds2D();
281
282         // Return empty rect if they don't intersect.
283
if (bounds.intersects(srect) == false)
284             return new Rectangle2D.Float JavaDoc();
285             
286         Rectangle2D.intersect(srect, bounds, srect);
287         return srect;
288     }
289
290     public Shape JavaDoc getDirtyRegion(int srcIndex,
291                                 Rectangle2D JavaDoc inputRgn) {
292         if ((srcIndex < 0) || (srcIndex > srcs.size()))
293             throw new IndexOutOfBoundsException JavaDoc
294                 ("Nonexistant source requested.");
295
296           // Changes in the input region don't propogate outside our
297
// bounds.
298
Rectangle2D JavaDoc drect = (Rectangle2D JavaDoc)inputRgn.clone();
299         Rectangle2D JavaDoc bounds = getBounds2D();
300
301         // Return empty rect if they don't intersect.
302
if (bounds.intersects(drect) == false)
303             return new Rectangle2D.Float JavaDoc();
304
305         Rectangle2D.intersect(drect, bounds, drect);
306         return drect;
307     }
308     
309     
310     /* left for subclass:
311        public RenderedImage createRendering(RenderContext rc);
312     */

313 }
314
Popular Tags