KickJava   Java API By Example, From Geeks To Geeks.

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


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.RenderingHints JavaDoc;
21 import java.awt.Shape JavaDoc;
22 import java.awt.geom.Rectangle2D JavaDoc;
23 import java.awt.image.RenderedImage JavaDoc;
24 import java.awt.image.renderable.RenderContext JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  * This class allows for the return of a proxy object quickly, while a
30  * heavy weight object is constrcuted in a background Thread. This
31  * proxy object will then block if any methods are called on it that
32  * require talking to the source object.
33  *
34  * This is actually a particular instance of a very general pattern
35  * this is probably best represented using the Proxy class in the
36  * Reflection APIs.
37  */

38
39 public class DeferRable implements Filter {
40     Filter src;
41     Rectangle2D JavaDoc bounds;
42     Map JavaDoc props;
43     /**
44      * Constructor takes nothing
45      */

46     public DeferRable() {
47     }
48
49     /**
50      * Key method that blocks if the src has not yet been provided.
51      */

52     public synchronized Filter getSource() {
53         while (src == null) {
54             try {
55                 // Wait for someone to set src.
56
wait();
57             }
58             catch(InterruptedException JavaDoc ie) {
59                 // Loop around again see if src is set now...
60
}
61         }
62         return src;
63     }
64
65     /**
66      * Key method that sets the src. The source can only
67      * be set once (this makes sense given the intent of the
68      * class is to stand in for a real object, so swaping that
69      * object isn't a good idea.
70      *
71      * This will wake all the threads that might be waiting for
72      * the source to be set.
73      */

74     public synchronized void setSource(Filter src) {
75         // Only let them set Source once.
76
if (this.src != null) return;
77         this.src = src;
78         this.bounds = src.getBounds2D();
79         notifyAll();
80     }
81
82     public synchronized void setBounds(Rectangle2D JavaDoc bounds) {
83         if (this.bounds != null) return;
84         this.bounds = bounds;
85         notifyAll();
86     }
87
88     public synchronized void setProperties(Map JavaDoc props) {
89         this.props = props;
90         notifyAll();
91     }
92
93     public long getTimeStamp() {
94         return getSource().getTimeStamp();
95     }
96
97     public Vector JavaDoc getSources() {
98         return getSource().getSources();
99     }
100
101     /**
102      * Forward the call (blocking until source is set if need be).
103      */

104     public boolean isDynamic() {
105         return getSource().isDynamic();
106     }
107
108     /**
109      * Implement the baseclass method to call getSource() so
110      * it will block until we have a real source.
111      */

112     public Rectangle2D JavaDoc getBounds2D() {
113         synchronized(this) {
114             while ((src == null) && (bounds == null)) {
115                 try {
116                     // Wait for someone to set bounds.
117
wait();
118                 }
119                 catch(InterruptedException JavaDoc ie) {
120                     // Loop around again see if src is set now...
121
}
122             }
123         }
124         if (src != null)
125             return src.getBounds2D();
126         return bounds;
127     }
128
129     public float getMinX() {
130         return (float)getBounds2D().getX();
131     }
132     public float getMinY() {
133         return (float)getBounds2D().getY();
134     }
135     public float getWidth() {
136         return (float)getBounds2D().getWidth();
137     }
138     public float getHeight() {
139         return (float)getBounds2D().getHeight();
140     }
141
142     /**
143      * Forward the call (blocking until source is set if need be).
144      */

145     public Object JavaDoc getProperty(String JavaDoc name) {
146         synchronized (this) {
147             while ((src == null) && (props == null)) {
148                 try {
149                     // Wait for someone to set src | props
150
wait();
151                 } catch(InterruptedException JavaDoc ie) { }
152             }
153         }
154         if (src != null)
155             return src.getProperty(name);
156         return props.get(name);
157     }
158
159     /**
160      * Forward the call (blocking until source is set if need be).
161      */

162     public String JavaDoc [] getPropertyNames() {
163         synchronized (this) {
164             while ((src == null) && (props == null)) {
165                 try {
166                     // Wait for someone to set src | props
167
wait();
168                 } catch(InterruptedException JavaDoc ie) { }
169             }
170         }
171         if (src != null)
172             return src.getPropertyNames();
173
174         String JavaDoc [] ret = new String JavaDoc[props.size()];
175         props.keySet().toArray(ret);
176         return ret;
177     }
178
179     /**
180      * Forward the call (blocking until source is set if need be).
181      */

182     public RenderedImage JavaDoc createDefaultRendering() {
183         return getSource().createDefaultRendering();
184     }
185
186     /**
187      * Forward the call (blocking until source is set if need be).
188      */

189     public RenderedImage JavaDoc createScaledRendering(int w, int h,
190                                                RenderingHints JavaDoc hints) {
191         return getSource().createScaledRendering(w, h, hints);
192     }
193
194     /**
195      * Forward the call (blocking until source is set if need be).
196      */

197     public RenderedImage JavaDoc createRendering(RenderContext JavaDoc rc) {
198         return getSource().createRendering(rc);
199     }
200
201     /**
202      * Forward the call (blocking until source is set if need be).
203      */

204     public Shape JavaDoc getDependencyRegion(int srcIndex,
205                                      Rectangle2D JavaDoc outputRgn) {
206         return getSource().getDependencyRegion(srcIndex, outputRgn);
207     }
208
209     /**
210      * Forward the call (blocking until source is set if need be).
211      */

212     public Shape JavaDoc getDirtyRegion(int srcIndex,
213                                 Rectangle2D JavaDoc inputRgn) {
214         return getSource().getDirtyRegion(srcIndex, inputRgn);
215     }
216 }
217
Popular Tags