Revision 44453 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.mapcontext/org.gvsig.fmap.mapcontext.api/src/main/java/org/gvsig/fmap/mapcontext/impl/DefaultMapContextDrawer.java
DefaultMapContextDrawer.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.awt.Graphics2D; |
27 | 27 |
import java.awt.geom.AffineTransform; |
28 |
import java.awt.geom.Rectangle2D; |
|
29 | 28 |
import java.awt.image.BufferedImage; |
30 | 29 |
import java.util.ArrayList; |
31 | 30 |
import java.util.Iterator; |
... | ... | |
36 | 35 |
import org.gvsig.compat.print.PrintAttributes; |
37 | 36 |
import org.gvsig.fmap.dal.exception.ReadException; |
38 | 37 |
import org.gvsig.fmap.geom.primitive.Envelope; |
39 |
import org.gvsig.fmap.mapcontext.ExtentHistory; |
|
40 | 38 |
import org.gvsig.fmap.mapcontext.MapContext; |
41 | 39 |
import org.gvsig.fmap.mapcontext.MapContextDrawer; |
42 | 40 |
import org.gvsig.fmap.mapcontext.MapContextException; |
... | ... | |
53 | 51 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
54 | 52 |
import org.gvsig.fmap.mapcontext.layers.vectorial.VectorLayer; |
55 | 53 |
import org.gvsig.fmap.mapcontext.rendering.legend.styling.ILabelable; |
56 |
import org.gvsig.tools.ToolsLocator; |
|
57 |
import org.gvsig.tools.dynobject.DynStruct; |
|
58 |
import org.gvsig.tools.library.LibraryException; |
|
59 |
import org.gvsig.tools.persistence.PersistenceManager; |
|
60 | 54 |
import org.gvsig.tools.task.Cancellable; |
61 | 55 |
import org.gvsig.tools.util.Callable; |
62 | 56 |
import org.slf4j.Logger; |
... | ... | |
64 | 58 |
|
65 | 59 |
public class DefaultMapContextDrawer implements MapContextDrawer { |
66 | 60 |
|
67 |
private static final Logger LOG = LoggerFactory
|
|
68 |
.getLogger(DefaultMapContextDrawer.class);
|
|
61 |
private static final Logger LOG = LoggerFactory
|
|
62 |
.getLogger(DefaultMapContextDrawer.class);
|
|
69 | 63 |
|
70 |
private MapContext mapContext = null;
|
|
71 |
private ViewPort viewPort = null;
|
|
72 |
private CachedImage cachedImage = null;
|
|
73 |
private DrawList previousDrawList = null;
|
|
64 |
private MapContext mapContext = null;
|
|
65 |
private ViewPort viewPort = null;
|
|
66 |
private CachedImage cachedImage = null;
|
|
67 |
private DrawList previousDrawList = null;
|
|
74 | 68 |
|
75 |
protected void checkInitialized() {
|
|
76 |
if (mapContext == null || viewPort == null) {
|
|
77 |
throw new IllegalStateException(
|
|
78 |
"MapContext and ViewPort must be set");
|
|
79 |
}
|
|
80 |
}
|
|
69 |
protected void checkInitialized() {
|
|
70 |
if (mapContext == null || viewPort == null) {
|
|
71 |
throw new IllegalStateException(
|
|
72 |
"MapContext and ViewPort must be set");
|
|
73 |
}
|
|
74 |
}
|
|
81 | 75 |
|
82 |
public void draw(FLayers root, BufferedImage image, Graphics2D g,
|
|
83 |
Cancellable cancel, double scale) throws ReadException {
|
|
76 |
public void draw(FLayers root, BufferedImage image, Graphics2D g,
|
|
77 |
Cancellable cancel, double scale) throws ReadException {
|
|
84 | 78 |
|
85 |
this.checkInitialized();
|
|
79 |
this.checkInitialized();
|
|
86 | 80 |
|
87 |
// With viewport changes all layers must be redrawn, discard cache
|
|
88 |
if (cachedImage != null && cachedImage.hasChangedViewPortDrawVersion()) {
|
|
89 |
cachedImage = null;
|
|
90 |
}
|
|
81 |
// With viewport changes all layers must be redrawn, discard cache
|
|
82 |
if (cachedImage != null && cachedImage.hasChangedViewPortDrawVersion()) {
|
|
83 |
cachedImage = null;
|
|
84 |
}
|
|
91 | 85 |
|
92 |
AffineTransform aux_at = null; |
|
93 |
|
|
94 |
if (isValidFullCachedImage()) { |
|
95 |
|
|
96 |
aux_at = g.getTransform(); |
|
97 |
g.drawImage( |
|
98 |
cachedImage.getFullDrawnImage(), |
|
99 |
(int) -aux_at.getTranslateX(), |
|
100 |
(int) -aux_at.getTranslateY(), |
|
101 |
null); |
|
102 |
LOG.debug("Drawn full image from the cache, all layers cached"); |
|
103 |
return; |
|
104 |
} |
|
86 |
AffineTransform aux_at = null; |
|
105 | 87 |
|
106 |
DrawList drawList = this.createDrawList(root, cancel, scale); |
|
107 |
if (drawList == null || drawList.size() == 0) { |
|
108 |
return; |
|
109 |
} |
|
88 |
if (isValidFullCachedImage()) { |
|
110 | 89 |
|
111 |
if (cancel.isCanceled()) { |
|
112 |
cachedImage = null; |
|
113 |
return; |
|
114 |
} |
|
90 |
aux_at = g.getTransform(); |
|
91 |
g.drawImage( |
|
92 |
cachedImage.getFullDrawnImage(), |
|
93 |
(int) -aux_at.getTranslateX(), |
|
94 |
(int) -aux_at.getTranslateY(), |
|
95 |
null); |
|
96 |
LOG.debug("Drawn full image from the cache, all layers cached"); |
|
97 |
return; |
|
98 |
} |
|
115 | 99 |
|
116 |
int firstLayerToDraw; |
|
117 |
int lastLayerToDraw; |
|
118 |
if (isValidPartialCachedImage(drawList)) { |
|
119 |
firstLayerToDraw = 0; |
|
120 |
lastLayerToDraw = cachedImage.getLastDrawnLayerPosition(); |
|
121 |
|
|
122 |
aux_at = g.getTransform(); |
|
123 |
g.drawImage( |
|
124 |
cachedImage.getPartialDrawnImage(), |
|
125 |
(int) -aux_at.getTranslateX(), |
|
126 |
(int) -aux_at.getTranslateY(), |
|
127 |
null); |
|
128 |
|
|
129 |
cachedImage.updateVersions(mapContext, viewPort); |
|
130 |
LOG.debug("Reused image of cached layers from 0 to {}", |
|
131 |
new Integer(lastLayerToDraw)); |
|
132 |
} else { |
|
133 |
if (cachedImage == null) { |
|
134 |
cachedImage = new CachedImage(); |
|
135 |
// Draw all layers |
|
136 |
firstLayerToDraw = 0; |
|
137 |
//lastLayerToDraw = drawList.getLayerCount() - 1; |
|
138 |
lastLayerToDraw = drawList.getLastLayerVisible(viewPort); |
|
139 |
} else { |
|
140 |
// Draw the first group of layers without changes to be cached |
|
141 |
// next time |
|
142 |
firstLayerToDraw = 0; |
|
143 |
int firstChangedLayer = drawList.getFirstChangedLayer(); |
|
144 |
// If negative nothing has changed, so draw all the layers |
|
145 |
lastLayerToDraw = firstChangedLayer < 0 ? drawList |
|
146 |
.getLayerCount() - 1 : firstChangedLayer - 1; |
|
147 |
} |
|
148 |
drawList.drawLayers(image, g, firstLayerToDraw, lastLayerToDraw, |
|
149 |
cancel, scale); |
|
150 |
cachedImage.setPartialDrawnImage(image, mapContext, viewPort, |
|
151 |
lastLayerToDraw); |
|
152 |
} |
|
100 |
DrawList drawList = this.createDrawList(root, cancel, scale); |
|
101 |
if (drawList == null || drawList.size() == 0) { |
|
102 |
return; |
|
103 |
} |
|
153 | 104 |
|
154 |
if (cancel.isCanceled()) {
|
|
155 |
cachedImage = null;
|
|
156 |
return;
|
|
157 |
}
|
|
105 |
if (cancel.isCanceled()) {
|
|
106 |
cachedImage = null;
|
|
107 |
return;
|
|
108 |
}
|
|
158 | 109 |
|
159 |
// Draw the second group of layers not cached |
|
160 |
firstLayerToDraw = lastLayerToDraw + 1; |
|
161 |
lastLayerToDraw = drawList.getLayerCount() - 1; |
|
162 |
drawList.drawLayers(image, g, firstLayerToDraw, lastLayerToDraw, |
|
163 |
cancel, scale); |
|
164 |
cachedImage.setFullDrawnImage(image); |
|
110 |
int firstLayerToDraw; |
|
111 |
int lastLayerToDraw; |
|
112 |
if (isValidPartialCachedImage(drawList)) { |
|
113 |
firstLayerToDraw = 0; |
|
114 |
lastLayerToDraw = cachedImage.getLastDrawnLayerPosition(); |
|
165 | 115 |
|
166 |
this.previousDrawList = drawList; |
|
167 |
} |
|
116 |
aux_at = g.getTransform(); |
|
117 |
g.drawImage( |
|
118 |
cachedImage.getPartialDrawnImage(), |
|
119 |
(int) -aux_at.getTranslateX(), |
|
120 |
(int) -aux_at.getTranslateY(), |
|
121 |
null); |
|
168 | 122 |
|
169 |
private boolean isValidPartialCachedImage(DrawList drawList) { |
|
170 |
return cachedImage != null |
|
171 |
&& cachedImage.isValidPartialDrawnImage(mapContext, drawList); |
|
172 |
} |
|
123 |
cachedImage.updateVersions(mapContext, viewPort); |
|
124 |
LOG.debug("Reused image of cached layers from 0 to {}", |
|
125 |
new Integer(lastLayerToDraw)); |
|
126 |
} else { |
|
127 |
if (cachedImage == null) { |
|
128 |
cachedImage = new CachedImage(); |
|
129 |
// Draw all layers |
|
130 |
firstLayerToDraw = 0; |
|
131 |
//lastLayerToDraw = drawList.getLayerCount() - 1; |
|
132 |
lastLayerToDraw = drawList.getLastLayerVisible(viewPort); |
|
133 |
} else { |
|
134 |
// Draw the first group of layers without changes to be cached |
|
135 |
// next time |
|
136 |
firstLayerToDraw = 0; |
|
137 |
int firstChangedLayer = drawList.getFirstChangedLayer(); |
|
138 |
// If negative nothing has changed, so draw all the layers |
|
139 |
lastLayerToDraw = firstChangedLayer < 0 ? drawList |
|
140 |
.getLayerCount() - 1 : firstChangedLayer - 1; |
|
141 |
} |
|
142 |
drawList.drawLayers(image, g, firstLayerToDraw, lastLayerToDraw, |
|
143 |
cancel, scale); |
|
144 |
cachedImage.setPartialDrawnImage(image, mapContext, viewPort, |
|
145 |
lastLayerToDraw); |
|
146 |
} |
|
173 | 147 |
|
174 |
private boolean isValidFullCachedImage() {
|
|
175 |
return cachedImage != null
|
|
176 |
&& cachedImage.isValidFullDrawnImage(mapContext);
|
|
177 |
}
|
|
148 |
if (cancel.isCanceled()) {
|
|
149 |
cachedImage = null;
|
|
150 |
return;
|
|
151 |
}
|
|
178 | 152 |
|
179 |
private void print(Object layerOrComposed, Graphics2D g,
|
|
180 |
Cancellable cancel, double scale, PrintAttributes properties)
|
|
181 |
throws ReadException {
|
|
182 |
ILabelable labelable = null;
|
|
183 |
ILabelable tmp = null;
|
|
184 |
if (layerOrComposed instanceof ILabelable) {
|
|
153 |
// Draw the second group of layers not cached
|
|
154 |
firstLayerToDraw = lastLayerToDraw + 1;
|
|
155 |
lastLayerToDraw = drawList.getLayerCount() - 1;
|
|
156 |
drawList.drawLayers(image, g, firstLayerToDraw, lastLayerToDraw,
|
|
157 |
cancel, scale);
|
|
158 |
cachedImage.setFullDrawnImage(image);
|
|
185 | 159 |
|
186 |
tmp = (ILabelable) layerOrComposed; |
|
160 |
this.previousDrawList = drawList; |
|
161 |
} |
|
187 | 162 |
|
188 |
if (tmp.isLabeled() && tmp.getLabelingStrategy() != null |
|
189 |
&& tmp.getLabelingStrategy().shouldDrawLabels(scale)) { |
|
190 |
labelable = tmp; |
|
191 |
} |
|
192 |
} |
|
163 |
private boolean isValidPartialCachedImage(DrawList drawList) { |
|
164 |
return cachedImage != null |
|
165 |
&& cachedImage.isValidPartialDrawnImage(mapContext, drawList); |
|
166 |
} |
|
193 | 167 |
|
194 |
if (layerOrComposed instanceof FLayer) { |
|
195 |
FLayer layer = (FLayer) layerOrComposed; |
|
196 |
layer.print(g, viewPort, cancel, scale, properties); |
|
197 |
} else { |
|
198 |
ComposedLayer composed = (ComposedLayer) layerOrComposed; |
|
199 |
composed.print(g, viewPort, cancel, scale, properties); |
|
200 |
} |
|
201 |
if (labelable != null) { |
|
202 |
labelable.printLabels(g, viewPort, cancel, scale, properties); |
|
203 |
} |
|
168 |
private boolean isValidFullCachedImage() { |
|
169 |
return cachedImage != null |
|
170 |
&& cachedImage.isValidFullDrawnImage(mapContext); |
|
171 |
} |
|
204 | 172 |
|
205 |
} |
|
173 |
private void print(Object layerOrComposed, Graphics2D g, |
|
174 |
Cancellable cancel, double scale, PrintAttributes properties) |
|
175 |
throws ReadException { |
|
176 |
ILabelable labelable = null; |
|
177 |
ILabelable tmp = null; |
|
178 |
if (layerOrComposed instanceof ILabelable) { |
|
206 | 179 |
|
207 |
public void setMapContext(MapContext mapContext) { |
|
208 |
if (this.mapContext == mapContext) { |
|
209 |
return; |
|
210 |
} |
|
211 |
this.clean(); |
|
212 |
this.mapContext = mapContext; |
|
180 |
tmp = (ILabelable) layerOrComposed; |
|
213 | 181 |
|
214 |
} |
|
182 |
if (tmp.isLabeled() && tmp.getLabelingStrategy() != null |
|
183 |
&& tmp.getLabelingStrategy().shouldDrawLabels(scale)) { |
|
184 |
labelable = tmp; |
|
185 |
} |
|
186 |
} |
|
215 | 187 |
|
216 |
public void setViewPort(ViewPort viewPort) { |
|
217 |
if (this.viewPort == viewPort) { |
|
218 |
return; |
|
219 |
} |
|
220 |
this.clean(); |
|
221 |
this.viewPort = viewPort; |
|
188 |
if (layerOrComposed instanceof FLayer) { |
|
189 |
FLayer layer = (FLayer) layerOrComposed; |
|
190 |
layer.print(g, viewPort, cancel, scale, properties); |
|
191 |
} else { |
|
192 |
ComposedLayer composed = (ComposedLayer) layerOrComposed; |
|
193 |
composed.print(g, viewPort, cancel, scale, properties); |
|
194 |
} |
|
195 |
if (labelable != null) { |
|
196 |
labelable.printLabels(g, viewPort, cancel, scale, properties); |
|
197 |
} |
|
222 | 198 |
|
223 |
}
|
|
199 |
}
|
|
224 | 200 |
|
225 |
protected void clean() { |
|
226 |
this.cachedImage = null; |
|
227 |
} |
|
201 |
public void setMapContext(MapContext mapContext) { |
|
202 |
if (this.mapContext == mapContext) { |
|
203 |
return; |
|
204 |
} |
|
205 |
this.clean(); |
|
206 |
this.mapContext = mapContext; |
|
228 | 207 |
|
229 |
public class CachedImage { |
|
230 |
private BufferedImage partialDrawnImage; |
|
231 |
private BufferedImage fullDrawnImage; |
|
232 |
private long lastMapContextVersion; |
|
233 |
private long lastViewPortVersion; |
|
234 |
private int lastDrawnLayerPosition; |
|
208 |
} |
|
235 | 209 |
|
236 |
public void setPartialDrawnImage(BufferedImage partialDrawnImage, |
|
237 |
MapContext mapContext, ViewPort viewPort, |
|
238 |
int lastDrawnLayerPosition) { |
|
239 |
this.partialDrawnImage = CompatLocator.getGraphicsUtils() |
|
240 |
.copyBufferedImage(partialDrawnImage); |
|
241 |
this.lastDrawnLayerPosition = lastDrawnLayerPosition; |
|
242 |
updateVersions(mapContext, viewPort); |
|
243 |
} |
|
244 |
|
|
245 |
public void updateVersions(MapContext mapContext, ViewPort viewPort) { |
|
246 |
this.lastMapContextVersion = mapContext.getDrawVersion(); |
|
247 |
this.lastViewPortVersion = viewPort.getDrawVersion(); |
|
248 |
} |
|
210 |
public void setViewPort(ViewPort viewPort) { |
|
211 |
if (this.viewPort == viewPort) { |
|
212 |
return; |
|
213 |
} |
|
214 |
this.clean(); |
|
215 |
this.viewPort = viewPort; |
|
249 | 216 |
|
250 |
public void setFullDrawnImage(BufferedImage fullDrawnImage) { |
|
251 |
this.fullDrawnImage = CompatLocator.getGraphicsUtils() |
|
252 |
.copyBufferedImage(fullDrawnImage); |
|
253 |
} |
|
217 |
} |
|
254 | 218 |
|
255 |
public BufferedImage getPartialDrawnImage() {
|
|
256 |
return partialDrawnImage;
|
|
257 |
}
|
|
219 |
protected void clean() {
|
|
220 |
this.cachedImage = null;
|
|
221 |
}
|
|
258 | 222 |
|
259 |
public BufferedImage getFullDrawnImage() { |
|
260 |
return fullDrawnImage; |
|
261 |
} |
|
223 |
public class CachedImage { |
|
262 | 224 |
|
263 |
public long getMapContextVersion() { |
|
264 |
return lastMapContextVersion; |
|
265 |
} |
|
225 |
private BufferedImage partialDrawnImage; |
|
226 |
private BufferedImage fullDrawnImage; |
|
227 |
private long lastMapContextVersion; |
|
228 |
private long lastViewPortVersion; |
|
229 |
private int lastDrawnLayerPosition; |
|
266 | 230 |
|
267 |
public int getLastDrawnLayerPosition() { |
|
268 |
return this.lastDrawnLayerPosition; |
|
269 |
} |
|
231 |
public void setPartialDrawnImage(BufferedImage partialDrawnImage, |
|
232 |
MapContext mapContext, ViewPort viewPort, |
|
233 |
int lastDrawnLayerPosition) { |
|
234 |
this.partialDrawnImage = CompatLocator.getGraphicsUtils() |
|
235 |
.copyBufferedImage(partialDrawnImage); |
|
236 |
this.lastDrawnLayerPosition = lastDrawnLayerPosition; |
|
237 |
updateVersions(mapContext, viewPort); |
|
238 |
} |
|
270 | 239 |
|
271 |
public boolean isValidFullDrawnImage(MapContext context) { |
|
272 |
// If the MapContext version has not changed, there are not any |
|
273 |
// changes that require redrawing any of the layers. |
|
274 |
return fullDrawnImage != null && !hasChangedMapContextDrawVersion(); |
|
275 |
} |
|
240 |
public void updateVersions(MapContext mapContext, ViewPort viewPort) { |
|
241 |
this.lastMapContextVersion = mapContext.getDrawVersion(); |
|
242 |
this.lastViewPortVersion = viewPort.getDrawVersion(); |
|
243 |
} |
|
276 | 244 |
|
277 |
public boolean hasChangedMapContextDrawVersion() {
|
|
278 |
// This change detects changes in layers and the viewport also
|
|
279 |
return mapContext.getDrawVersion() != this.lastMapContextVersion;
|
|
280 |
}
|
|
245 |
public void setFullDrawnImage(BufferedImage fullDrawnImage) {
|
|
246 |
this.fullDrawnImage = CompatLocator.getGraphicsUtils()
|
|
247 |
.copyBufferedImage(fullDrawnImage);
|
|
248 |
}
|
|
281 | 249 |
|
282 |
public boolean hasChangedViewPortDrawVersion() { |
|
283 |
// This change detects changes in the viewport |
|
284 |
return viewPort.getDrawVersion() != this.lastViewPortVersion; |
|
285 |
} |
|
250 |
public BufferedImage getPartialDrawnImage() { |
|
251 |
return partialDrawnImage; |
|
252 |
} |
|
286 | 253 |
|
287 |
public boolean isValidPartialDrawnImage(MapContext context, |
|
288 |
DrawList drawList) { |
|
289 |
if (!hasChangedMapContextDrawVersion()) { |
|
290 |
// Nothing has changed |
|
291 |
return true; |
|
292 |
} |
|
254 |
public BufferedImage getFullDrawnImage() { |
|
255 |
return fullDrawnImage; |
|
256 |
} |
|
293 | 257 |
|
294 |
if (partialDrawnImage == null || hasChangedViewPortDrawVersion()) { |
|
295 |
// No image available or changes in view port |
|
296 |
return false; |
|
297 |
} |
|
258 |
public long getMapContextVersion() { |
|
259 |
return lastMapContextVersion; |
|
260 |
} |
|
298 | 261 |
|
299 |
if (drawList.size() < lastDrawnLayerPosition + 1) { |
|
300 |
// New list has fewer layers than before |
|
301 |
return false; |
|
302 |
} |
|
262 |
public int getLastDrawnLayerPosition() { |
|
263 |
return this.lastDrawnLayerPosition; |
|
264 |
} |
|
303 | 265 |
|
304 |
// There is any change in the layers drawn in the partial drawn
|
|
305 |
// image?
|
|
306 |
return drawList.getFirstChangedLayer() > lastDrawnLayerPosition;
|
|
307 |
}
|
|
308 |
}
|
|
266 |
public boolean isValidFullDrawnImage(MapContext context) {
|
|
267 |
// If the MapContext version has not changed, there are not any
|
|
268 |
// changes that require redrawing any of the layers.
|
|
269 |
return fullDrawnImage != null && !hasChangedMapContextDrawVersion();
|
|
270 |
}
|
|
309 | 271 |
|
310 |
public class DrawList { |
|
311 |
private List layers = new ArrayList(); |
|
312 |
private List all = new ArrayList(); |
|
313 |
private List versions = new ArrayList(); |
|
314 |
private DrawList previosList = null; |
|
315 |
private int firstLayerChanged = -1; |
|
272 |
public boolean hasChangedMapContextDrawVersion() { |
|
273 |
// This change detects changes in layers and the viewport also |
|
274 |
return mapContext.getDrawVersion() != this.lastMapContextVersion; |
|
275 |
} |
|
316 | 276 |
|
317 |
public DrawList() { |
|
318 |
} |
|
277 |
public boolean hasChangedViewPortDrawVersion() { |
|
278 |
// This change detects changes in the viewport |
|
279 |
return viewPort.getDrawVersion() != this.lastViewPortVersion; |
|
280 |
} |
|
319 | 281 |
|
320 |
public DrawList(DrawList previousList) {
|
|
321 |
if (previousList != null) {
|
|
322 |
this.firstLayerChanged = previousList.getLayerCount();
|
|
323 |
this.previosList = previousList;
|
|
324 |
}
|
|
325 |
}
|
|
282 |
public boolean isValidPartialDrawnImage(MapContext context,
|
|
283 |
DrawList drawList) {
|
|
284 |
if (!hasChangedMapContextDrawVersion()) {
|
|
285 |
// Nothing has changed
|
|
286 |
return true;
|
|
287 |
}
|
|
326 | 288 |
|
327 |
public int getLayerCount() { |
|
328 |
return this.layers.size(); |
|
329 |
} |
|
289 |
if (partialDrawnImage == null || hasChangedViewPortDrawVersion()) { |
|
290 |
// No image available or changes in view port |
|
291 |
return false; |
|
292 |
} |
|
330 | 293 |
|
331 |
public int getLastLayerVisible(ViewPort viewPort) { |
|
332 |
Envelope area = viewPort.getAdjustedEnvelope(); |
|
333 |
for( int n=0; n<this.layers.size()-1; n++ ) { |
|
334 |
FLayer layer = (FLayer) this.layers.get(n); |
|
335 |
if( layer instanceof FLayerHidesArea ) { |
|
336 |
if( ((FLayerHidesArea)(layer)).hidesThisArea(area) ) { |
|
337 |
return n; |
|
338 |
} |
|
339 |
} |
|
340 |
} |
|
341 |
return this.layers.size()-1; |
|
342 |
} |
|
294 |
if (drawList.size() < lastDrawnLayerPosition + 1) { |
|
295 |
// New list has fewer layers than before |
|
296 |
return false; |
|
297 |
} |
|
343 | 298 |
|
344 |
private boolean hasChanged(FLayer layer, int pos) { |
|
345 |
FLayer previous = (FLayer) this.previosList.layers.get(pos); |
|
346 |
// String previousName = previous.getName(); |
|
347 |
// String layerName = layer.getName(); |
|
348 |
if (previous != layer) { |
|
349 |
return true; |
|
350 |
} |
|
351 |
long previousVersion = ((Long) this.previosList.versions.get(pos)) |
|
352 |
.longValue(); |
|
353 |
long layerVersion = layer.getDrawVersion(); |
|
299 |
// There is any change in the layers drawn in the partial drawn |
|
300 |
// image? |
|
301 |
return drawList.getFirstChangedLayer() > lastDrawnLayerPosition; |
|
302 |
} |
|
303 |
} |
|
354 | 304 |
|
355 |
return previousVersion != layerVersion; |
|
356 |
} |
|
305 |
public class DrawList { |
|
357 | 306 |
|
358 |
public void add(Object obj) { |
|
359 |
if (obj instanceof FLayer) { |
|
360 |
FLayer layer = (FLayer) obj; |
|
361 |
int curIndex = this.layers.size(); |
|
362 |
if (this.firstLayerChanged >= curIndex) { |
|
363 |
if (this.previosList.getLayerCount() > curIndex) { |
|
364 |
if (this.hasChanged(layer, curIndex)) { |
|
365 |
this.firstLayerChanged = curIndex; |
|
366 |
} |
|
367 |
} else if (this.previosList.getLayerCount() == curIndex) { |
|
368 |
this.firstLayerChanged = curIndex; |
|
369 |
} |
|
370 |
} |
|
371 |
this.layers.add(layer); |
|
372 |
this.versions.add(new Long(layer.getDrawVersion())); |
|
373 |
} else if (!(obj instanceof LayersGroupEvent)) { |
|
374 |
throw new UnsupportedOperationException(); |
|
375 |
} |
|
307 |
private List layers = new ArrayList(); |
|
308 |
private List all = new ArrayList(); |
|
309 |
private List versions = new ArrayList(); |
|
310 |
private DrawList previosList = null; |
|
311 |
private int firstLayerChanged = -1; |
|
376 | 312 |
|
377 |
this.all.add(obj);
|
|
378 |
}
|
|
313 |
public DrawList() {
|
|
314 |
}
|
|
379 | 315 |
|
380 |
public int size() { |
|
381 |
return this.all.size(); |
|
382 |
} |
|
316 |
public DrawList(DrawList previousList) { |
|
317 |
if (previousList != null) { |
|
318 |
this.firstLayerChanged = previousList.getLayerCount(); |
|
319 |
this.previosList = previousList; |
|
320 |
} |
|
321 |
} |
|
383 | 322 |
|
384 |
public int getFirstChangedLayer() { |
|
385 |
if (this.firstLayerChanged > this.layers.size()) { |
|
386 |
this.firstLayerChanged = this.layers.size(); |
|
387 |
} |
|
388 |
return this.firstLayerChanged; |
|
389 |
} |
|
323 |
public int getLayerCount() { |
|
324 |
return this.layers.size(); |
|
325 |
} |
|
390 | 326 |
|
391 |
public FLayer getLayer(int pos) { |
|
392 |
return (FLayer) this.layers.get(pos); |
|
393 |
} |
|
327 |
public int getLastLayerVisible(ViewPort viewPort) { |
|
328 |
Envelope area = viewPort.getAdjustedEnvelope(); |
|
329 |
for (int n = 0; n < this.layers.size() - 1; n++) { |
|
330 |
FLayer layer = (FLayer) this.layers.get(n); |
|
331 |
if (layer instanceof FLayerHidesArea) { |
|
332 |
if (((FLayerHidesArea) (layer)).hidesThisArea(area)) { |
|
333 |
return n; |
|
334 |
} |
|
335 |
} |
|
336 |
} |
|
337 |
return this.layers.size() - 1; |
|
338 |
} |
|
394 | 339 |
|
395 |
public Object get(int pos) { |
|
396 |
return this.all.get(pos); |
|
397 |
} |
|
340 |
private boolean hasChanged(FLayer layer, int pos) { |
|
341 |
FLayer previous = (FLayer) this.previosList.layers.get(pos); |
|
342 |
// String previousName = previous.getName(); |
|
343 |
// String layerName = layer.getName(); |
|
344 |
if (previous != layer) { |
|
345 |
return true; |
|
346 |
} |
|
347 |
long previousVersion = ((Long) this.previosList.versions.get(pos)) |
|
348 |
.longValue(); |
|
349 |
long layerVersion = layer.getDrawVersion(); |
|
398 | 350 |
|
399 |
public void drawLayers(BufferedImage image, Graphics2D g, |
|
400 |
int firstLayerToDraw, int lastLayerToDraw, Cancellable cancel, |
|
401 |
double scale) throws ReadException { |
|
351 |
return previousVersion != layerVersion; |
|
352 |
} |
|
402 | 353 |
|
403 |
if (firstLayerToDraw > lastLayerToDraw) { |
|
404 |
LOG.debug("Nothing to draw"); |
|
405 |
return; |
|
406 |
} |
|
354 |
public void add(Object obj) { |
|
355 |
if (obj instanceof FLayer) { |
|
356 |
FLayer layer = (FLayer) obj; |
|
357 |
int curIndex = this.layers.size(); |
|
358 |
if (this.firstLayerChanged >= curIndex) { |
|
359 |
if (this.previosList.getLayerCount() > curIndex) { |
|
360 |
if (this.hasChanged(layer, curIndex)) { |
|
361 |
this.firstLayerChanged = curIndex; |
|
362 |
} |
|
363 |
} else if (this.previosList.getLayerCount() == curIndex) { |
|
364 |
this.firstLayerChanged = curIndex; |
|
365 |
} |
|
366 |
} |
|
367 |
this.layers.add(layer); |
|
368 |
this.versions.add(new Long(layer.getDrawVersion())); |
|
369 |
} else if (!(obj instanceof LayersGroupEvent)) { |
|
370 |
throw new UnsupportedOperationException(); |
|
371 |
} |
|
407 | 372 |
|
408 |
// Find the real layer positions excluding LayersGroupEvents |
|
409 |
FLayer firstLayer = (FLayer) layers.get(firstLayerToDraw); |
|
410 |
int firstLayerPos = all.indexOf(firstLayer); |
|
411 |
// Look if it belongs to a group and start it |
|
412 |
if (firstLayerPos > 0) { |
|
413 |
for (int i = firstLayerPos - 1; i < all.size(); i++) { |
|
414 |
// firstLayer |
|
415 |
Object group = all.get(i); |
|
416 |
if (group instanceof LayersGroupEvent) { |
|
417 |
LayersGroupEvent event = (LayersGroupEvent) group; |
|
418 |
if (event.type == LayersGroupEvent.IN_Event) { |
|
419 |
event.group.beginDraw(g, viewPort); |
|
420 |
} |
|
421 |
break; |
|
422 |
} |
|
423 |
} |
|
424 |
} |
|
425 |
FLayer lastLayer = (FLayer) layers.get(lastLayerToDraw); |
|
426 |
int lastLayerPos = all.indexOf(lastLayer); |
|
373 |
this.all.add(obj); |
|
374 |
} |
|
427 | 375 |
|
428 |
LOG.debug("Drawing from layer {} in position (layers: {}, all: {})" |
|
429 |
+ " to layer {} in position (layers: {}, all: {})", |
|
430 |
new Object[] { firstLayer, new Integer(firstLayerToDraw), |
|
431 |
new Integer(firstLayerPos), lastLayer, |
|
432 |
new Integer(lastLayerToDraw), |
|
433 |
new Integer(lastLayerPos) }); |
|
376 |
public int size() { |
|
377 |
return this.all.size(); |
|
378 |
} |
|
434 | 379 |
|
435 |
ComposedLayer composed = null; |
|
436 |
for (int pos = firstLayerPos; pos <= lastLayerPos; pos++) { |
|
437 |
if (cancel.isCanceled()) { |
|
438 |
return; |
|
439 |
} |
|
380 |
public int getFirstChangedLayer() { |
|
381 |
if (this.firstLayerChanged > this.layers.size()) { |
|
382 |
this.firstLayerChanged = this.layers.size(); |
|
383 |
} |
|
384 |
return this.firstLayerChanged; |
|
385 |
} |
|
440 | 386 |
|
441 |
Object layerOrGroup = get(pos); |
|
387 |
public FLayer getLayer(int pos) { |
|
388 |
return (FLayer) this.layers.get(pos); |
|
389 |
} |
|
442 | 390 |
|
443 |
// Group drawing events management |
|
444 |
if (layerOrGroup instanceof LayersGroupEvent) { |
|
445 |
LayersGroupEvent event = (LayersGroupEvent) layerOrGroup; |
|
446 |
if (event.type == LayersGroupEvent.IN_Event) { |
|
447 |
event.group.beginDraw(g, viewPort); |
|
448 |
} else { |
|
449 |
event.group.endDraw(g, viewPort); |
|
450 |
} |
|
451 |
} else { |
|
452 |
FLayer layer = (FLayer) layerOrGroup; |
|
453 |
if (composed != null && composed.canAdd(layer)) { |
|
454 |
// Previous or current layer could be composed |
|
455 |
// Add current layer |
|
456 |
addToComposedLayer(composed, layer); |
|
457 |
} else { |
|
458 |
if (composed != null) { |
|
459 |
// Current layer can't be composed on the previous |
|
460 |
// composedlayer. Draw previous composed |
|
461 |
LOG.debug("Drawing composed layer {} ", composed); |
|
462 |
draw(composed, image, g, cancel, scale); |
|
463 |
composed = null; |
|
464 |
} |
|
391 |
public Object get(int pos) { |
|
392 |
return this.all.get(pos); |
|
393 |
} |
|
465 | 394 |
|
466 |
// Try if the current layer can be composed |
|
467 |
// Create new composed or draw current layer |
|
468 |
composed = layer.newComposedLayer(); |
|
469 |
if (composed == null) { |
|
470 |
LOG.debug("Drawing layer {} ", layer); |
|
471 |
draw(layer, image, g, cancel, scale); |
|
472 |
} else { |
|
473 |
addToComposedLayer(composed, layer); |
|
474 |
} |
|
475 |
} |
|
476 |
} |
|
477 |
} |
|
478 |
if (composed != null) { |
|
479 |
// Draw the pending composed |
|
480 |
draw(composed, image, g, cancel, scale); |
|
481 |
} |
|
395 |
public void drawLayers(BufferedImage image, Graphics2D g, |
|
396 |
int firstLayerToDraw, int lastLayerToDraw, Cancellable cancel, |
|
397 |
double scale) throws ReadException { |
|
482 | 398 |
|
483 |
// Check if the last layer is the last of a group and close it |
|
484 |
for (int i = lastLayerPos + 1; i < all.size(); i++) { |
|
485 |
Object group = all.get(i); |
|
486 |
if (group instanceof LayersGroupEvent) { |
|
487 |
LayersGroupEvent event = (LayersGroupEvent) group; |
|
488 |
if (event.type == LayersGroupEvent.OUT_Event) { |
|
489 |
event.group.endDraw(g, viewPort); |
|
490 |
} |
|
491 |
break; |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
399 |
if (firstLayerToDraw > lastLayerToDraw) { |
|
400 |
LOG.debug("Nothing to draw"); |
|
401 |
return; |
|
402 |
} |
|
495 | 403 |
|
496 |
private void addToComposedLayer(ComposedLayer composed, FLayer layer) |
|
497 |
throws ReadException { |
|
498 |
try { |
|
499 |
LOG.debug("Adding layer {} to composed layer ", layer, composed); |
|
500 |
composed.add(layer); |
|
501 |
} catch (Exception e) { |
|
502 |
throw new ReadException("DefalutMapContexDrawer exception", e); |
|
503 |
} |
|
504 |
} |
|
404 |
// Find the real layer positions excluding LayersGroupEvents |
|
405 |
FLayer firstLayer = (FLayer) layers.get(firstLayerToDraw); |
|
406 |
int firstLayerPos = all.indexOf(firstLayer); |
|
407 |
// Look if it belongs to a group and start it |
|
408 |
if (firstLayerPos > 0) { |
|
409 |
for (int i = firstLayerPos - 1; i < all.size(); i++) { |
|
410 |
// firstLayer |
|
411 |
Object group = all.get(i); |
|
412 |
if (group instanceof LayersGroupEvent) { |
|
413 |
LayersGroupEvent event = (LayersGroupEvent) group; |
|
414 |
if (event.type == LayersGroupEvent.IN_Event) { |
|
415 |
event.group.beginDraw(g, viewPort); |
|
416 |
} |
|
417 |
break; |
|
418 |
} |
|
419 |
} |
|
420 |
} |
|
421 |
FLayer lastLayer = (FLayer) layers.get(lastLayerToDraw); |
|
422 |
int lastLayerPos = all.indexOf(lastLayer); |
|
505 | 423 |
|
506 |
private void draw(Object layerOrComposed, BufferedImage image,
|
|
507 |
Graphics2D g, Cancellable cancel, double scale)
|
|
508 |
throws ReadException {
|
|
509 |
ILabelable labelable = null;
|
|
510 |
ILabelable tmp = null;
|
|
511 |
if (layerOrComposed instanceof ILabelable) {
|
|
424 |
LOG.debug("Drawing from layer {} in position (layers: {}, all: {})"
|
|
425 |
+ " to layer {} in position (layers: {}, all: {})",
|
|
426 |
new Object[]{firstLayer, new Integer(firstLayerToDraw),
|
|
427 |
new Integer(firstLayerPos), lastLayer,
|
|
428 |
new Integer(lastLayerToDraw),
|
|
429 |
new Integer(lastLayerPos)});
|
|
512 | 430 |
|
513 |
tmp = (ILabelable) layerOrComposed; |
|
431 |
ComposedLayer composed = null; |
|
432 |
for (int pos = firstLayerPos; pos <= lastLayerPos; pos++) { |
|
433 |
if (cancel.isCanceled()) { |
|
434 |
return; |
|
435 |
} |
|
514 | 436 |
|
515 |
if (tmp.isLabeled() && tmp.getLabelingStrategy() != null |
|
516 |
&& tmp.getLabelingStrategy().shouldDrawLabels(scale)) { |
|
517 |
labelable = tmp; |
|
518 |
} |
|
519 |
} |
|
520 |
|
|
521 |
if (layerOrComposed instanceof FLayer) { |
|
522 |
int beforeDrawEventType; |
|
523 |
int afterDrawEventType; |
|
524 |
if (layerOrComposed instanceof GraphicLayer) { |
|
525 |
beforeDrawEventType = LayerDrawEvent.GRAPHICLAYER_BEFORE_DRAW; |
|
526 |
afterDrawEventType = LayerDrawEvent.GRAPHICLAYER_AFTER_DRAW; |
|
527 |
} else { |
|
528 |
beforeDrawEventType = LayerDrawEvent.LAYER_BEFORE_DRAW; |
|
529 |
afterDrawEventType = LayerDrawEvent.LAYER_AFTER_DRAW; |
|
530 |
} |
|
531 |
FLayer layer = (FLayer) layerOrComposed; |
|
532 |
drawLayer(layer, image, g, cancel, scale, beforeDrawEventType, |
|
533 |
afterDrawEventType); |
|
534 |
} else { |
|
535 |
ComposedLayer composed = (ComposedLayer) layerOrComposed; |
|
536 |
composed.draw(image, g, viewPort, cancel, scale); |
|
537 |
} |
|
538 |
if (labelable != null) { |
|
539 |
labelable.drawLabels(image, g, viewPort, cancel, scale, |
|
540 |
mapContext.getViewPort().getDPI()); |
|
541 |
} |
|
437 |
Object layerOrGroup = get(pos); |
|
542 | 438 |
|
543 |
} |
|
439 |
// Group drawing events management |
|
440 |
if (layerOrGroup instanceof LayersGroupEvent) { |
|
441 |
LayersGroupEvent event = (LayersGroupEvent) layerOrGroup; |
|
442 |
if (event.type == LayersGroupEvent.IN_Event) { |
|
443 |
event.group.beginDraw(g, viewPort); |
|
444 |
} else { |
|
445 |
event.group.endDraw(g, viewPort); |
|
446 |
} |
|
447 |
} else { |
|
448 |
FLayer layer = (FLayer) layerOrGroup; |
|
449 |
if (composed != null && composed.canAdd(layer)) { |
|
450 |
// Previous or current layer could be composed |
|
451 |
// Add current layer |
|
452 |
addToComposedLayer(composed, layer); |
|
453 |
} else { |
|
454 |
if (composed != null) { |
|
455 |
// Current layer can't be composed on the previous |
|
456 |
// composedlayer. Draw previous composed |
|
457 |
LOG.debug("Drawing composed layer {} ", composed); |
|
458 |
draw(composed, image, g, cancel, scale); |
|
459 |
composed = null; |
|
460 |
} |
|
544 | 461 |
|
545 |
protected void drawLayer(FLayer layer, BufferedImage image, |
|
546 |
Graphics2D g, Cancellable cancel, double scale, |
|
547 |
int beforeDrawEventType, int afterDrawEventType) |
|
548 |
throws ReadException { |
|
549 |
LayerDrawEvent event = new LayerDrawEvent(layer, g, viewPort, beforeDrawEventType); |
|
550 |
mapContext.fireLayerDrawingEvent(event); |
|
551 |
layer.draw(image, g, viewPort, cancel, scale); |
|
552 |
event = new LayerDrawEvent(layer, g, viewPort, afterDrawEventType); |
|
553 |
mapContext.fireLayerDrawingEvent(event); |
|
554 |
} |
|
462 |
// Try if the current layer can be composed |
|
463 |
// Create new composed or draw current layer |
|
464 |
composed = layer.newComposedLayer(); |
|
465 |
if (composed == null) { |
|
466 |
LOG.debug("Drawing layer {} ", layer); |
|
467 |
draw(layer, image, g, cancel, scale); |
|
468 |
} else { |
|
469 |
addToComposedLayer(composed, layer); |
|
470 |
} |
|
471 |
} |
|
472 |
} |
|
473 |
} |
|
474 |
if (composed != null) { |
|
475 |
// Draw the pending composed |
|
476 |
draw(composed, image, g, cancel, scale); |
|
477 |
} |
|
555 | 478 |
|
556 |
} |
|
479 |
// Check if the last layer is the last of a group and close it |
|
480 |
for (int i = lastLayerPos + 1; i < all.size(); i++) { |
|
481 |
Object group = all.get(i); |
|
482 |
if (group instanceof LayersGroupEvent) { |
|
483 |
LayersGroupEvent event = (LayersGroupEvent) group; |
|
484 |
if (event.type == LayersGroupEvent.OUT_Event) { |
|
485 |
event.group.endDraw(g, viewPort); |
|
486 |
} |
|
487 |
break; |
|
488 |
} |
|
489 |
} |
|
490 |
} |
|
557 | 491 |
|
558 |
private class SimpleLayerIterator extends LayersIterator { |
|
492 |
private void addToComposedLayer(ComposedLayer composed, FLayer layer) |
|
493 |
throws ReadException { |
|
494 |
try { |
|
495 |
LOG.debug("Adding layer {} to composed layer ", layer, composed); |
|
496 |
composed.add(layer); |
|
497 |
} catch (Exception e) { |
|
498 |
throw new ReadException("DefalutMapContexDrawer exception", e); |
|
499 |
} |
|
500 |
} |
|
559 | 501 |
|
560 |
public SimpleLayerIterator(FLayer layer) { |
|
561 |
this.appendLayer(layer); |
|
562 |
} |
|
502 |
private void draw(Object layerOrComposed, BufferedImage image, |
|
503 |
Graphics2D g, Cancellable cancel, double scale) |
|
504 |
throws ReadException { |
|
505 |
ILabelable labelable = null; |
|
506 |
ILabelable tmp = null; |
|
507 |
if (layerOrComposed instanceof ILabelable) { |
|
563 | 508 |
|
564 |
public boolean evaluate(FLayer layer) { |
|
565 |
if (layer instanceof FLayers) { |
|
566 |
return false; |
|
567 |
} |
|
568 |
return layer.isAvailable() && layer.isVisible(); |
|
569 |
} |
|
509 |
tmp = (ILabelable) layerOrComposed; |
|
570 | 510 |
|
571 |
} |
|
511 |
if (tmp.isLabeled() && tmp.getLabelingStrategy() != null |
|
512 |
&& tmp.getLabelingStrategy().shouldDrawLabels(scale)) { |
|
513 |
labelable = tmp; |
|
514 |
} |
|
515 |
} |
|
572 | 516 |
|
573 |
public void dispose() { |
|
574 |
this.mapContext = null; |
|
575 |
this.viewPort = null; |
|
576 |
this.cachedImage = null; |
|
577 |
this.previousDrawList = null; |
|
578 |
} |
|
517 |
if (layerOrComposed instanceof FLayer) { |
|
518 |
int beforeDrawEventType; |
|
519 |
int afterDrawEventType; |
|
520 |
if (layerOrComposed instanceof GraphicLayer) { |
|
521 |
beforeDrawEventType = LayerDrawEvent.GRAPHICLAYER_BEFORE_DRAW; |
|
522 |
afterDrawEventType = LayerDrawEvent.GRAPHICLAYER_AFTER_DRAW; |
|
523 |
} else { |
|
524 |
beforeDrawEventType = LayerDrawEvent.LAYER_BEFORE_DRAW; |
|
525 |
afterDrawEventType = LayerDrawEvent.LAYER_AFTER_DRAW; |
|
526 |
} |
|
527 |
FLayer layer = (FLayer) layerOrComposed; |
|
528 |
drawLayer(layer, image, g, cancel, scale, beforeDrawEventType, |
|
529 |
afterDrawEventType); |
|
530 |
} else { |
|
531 |
ComposedLayer composed = (ComposedLayer) layerOrComposed; |
|
532 |
composed.draw(image, g, viewPort, cancel, scale); |
|
533 |
} |
|
534 |
if (labelable != null) { |
|
535 |
labelable.drawLabels(image, g, viewPort, cancel, scale, |
|
536 |
mapContext.getViewPort().getDPI()); |
|
537 |
} |
|
579 | 538 |
|
580 |
public void print(FLayers root, Graphics2D g, Cancellable cancel, |
|
581 |
double scale, PrintAttributes properties) throws ReadException { |
|
582 |
this.checkInitialized(); |
|
539 |
} |
|
583 | 540 |
|
584 |
List printList = this.createPrintList(root, cancel); |
|
585 |
if (cancel.isCanceled()) { |
|
586 |
return; |
|
587 |
} |
|
541 |
protected void drawLayer(FLayer layer, BufferedImage image, |
|
542 |
Graphics2D g, Cancellable cancel, double scale, |
|
543 |
int beforeDrawEventType, int afterDrawEventType) |
|
544 |
throws ReadException { |
|
545 |
LayerDrawEvent event = new LayerDrawEvent(layer, g, viewPort, beforeDrawEventType); |
|
546 |
mapContext.fireLayerDrawingEvent(event); |
|
547 |
layer.draw(image, g, viewPort, cancel, scale); |
|
548 |
event = new LayerDrawEvent(layer, g, viewPort, afterDrawEventType); |
|
549 |
mapContext.fireLayerDrawingEvent(event); |
|
550 |
} |
|
588 | 551 |
|
589 |
ComposedLayer composed = null; |
|
590 |
int pos; |
|
591 |
FLayer layer; |
|
592 |
int layerPos = -1; |
|
593 |
Object obj; |
|
594 |
LayersGroupEvent event; |
|
595 |
for (pos = 0; pos < printList.size(); pos++) { |
|
596 |
if (cancel.isCanceled()) { |
|
597 |
return; |
|
598 |
} |
|
552 |
} |
|
599 | 553 |
|
600 |
obj = printList.get(pos); |
|
601 |
if (obj instanceof LayersGroupEvent) { |
|
602 |
event = (LayersGroupEvent) obj; |
|
603 |
if (event.type == LayersGroupEvent.IN_Event) { |
|
604 |
// System.out.println("=======Empiza a pintar grupo de capas "+ |
|
605 |
// ((FLayers)event.group).getName() +"============"); |
|
606 |
event.group.beginDraw(g, viewPort); |
|
607 |
} else { |
|
608 |
event.group.endDraw(g, viewPort); |
|
609 |
// System.out.println("=======Fin a pintar grupo de capas "+ |
|
610 |
// ((FLayers)event.group).getName() +"============"); |
|
554 |
private class SimpleLayerIterator extends LayersIterator { |
|
611 | 555 |
|
612 |
} |
|
613 |
continue; |
|
614 |
} |
|
615 |
layerPos++; |
|
556 |
public SimpleLayerIterator(FLayer layer) { |
|
557 |
this.appendLayer(layer); |
|
558 |
} |
|
616 | 559 |
|
617 |
layer = (FLayer) obj; |
|
560 |
public boolean evaluate(FLayer layer) { |
|
561 |
if (layer instanceof FLayers) { |
|
562 |
return false; |
|
563 |
} |
|
564 |
return layer.isAvailable() && layer.isVisible(); |
|
565 |
} |
|
618 | 566 |
|
619 |
// *** Pintado de capa/composicion de capa *** |
|
620 |
if (composed == null) { |
|
621 |
composed = layer.newComposedLayer(); |
|
622 |
if (composed != null) { |
|
623 |
try { |
|
624 |
composed.add(layer); |
|
625 |
// System.out.println("=======Imprimiendo composicion de pintado "+ |
|
626 |
// (layerPos-1)+" ============"); |
|
627 |
continue; |
|
628 |
} catch (Exception e) { |
|
629 |
throw new ReadException( |
|
630 |
"DefaultMapContexDrawer exception", e); |
|
631 |
} |
|
632 |
} |
|
633 |
} else { |
|
634 |
if (composed.canAdd(layer)) { |
|
635 |
try { |
|
636 |
composed.add(layer); |
|
637 |
// System.out.println("=== a?adiendo a composicion de pintado "+ |
|
638 |
// layerPos+ " "+layer.getName()); |
|
639 |
continue; |
|
640 |
} catch (Exception e) { |
|
641 |
throw new ReadException( |
|
642 |
"DefaultMapContexDrawer exception", e); |
|
643 |
} |
|
644 |
} else { |
|
645 |
// System.out.println("=======Imprimiendo composicion de pintado "+ |
|
646 |
// (layerPos-1)+" ============"); |
|
647 |
this.print(composed, g, cancel, scale, properties); |
|
648 |
// composed.print( g, viewPort, cancel, scale,properties); |
|
649 |
composed = layer.newComposedLayer(); |
|
650 |
if (composed != null) { |
|
651 |
try { |
|
652 |
composed.add(layer); |
|
653 |
// System.out.println("=== a?adiendo a composicion de pintado "+ |
|
654 |
// layerPos+ " "+layer.getName()); |
|
655 |
continue; |
|
656 |
} catch (Exception e) { |
|
657 |
throw new ReadException( |
|
658 |
"DefaultMapContexDrawer exception", e); |
|
659 |
} |
|
660 |
} |
|
661 |
} |
|
662 |
} |
|
663 |
// System.out.println("=== imprimiendo "+ layerPos+ |
|
664 |
// " "+layer.getName()); |
|
665 |
this.print(layer, g, cancel, scale, properties); |
|
666 |
// layer.print(g, viewPort, cancel, scale,properties); |
|
667 |
// *** Pintado de capa/composicion de capa *** |
|
668 |
if (composed != null) { |
|
669 |
// si la composicion no se ha pintado la pintamos |
|
670 |
// System.out.println("=======Imprimiendo composicion de pintado "+ |
|
671 |
// (layerPos-1)+" (ultimo) ============"); |
|
672 |
this.print(composed, g, cancel, scale, properties); |
|
673 |
// composed.print(g, viewPort, cancel, scale, properties); |
|
674 |
composed = null; |
|
675 |
} |
|
676 |
} |
|
567 |
} |
|
677 | 568 |
|
678 |
} |
|
569 |
public void dispose() { |
|
570 |
this.mapContext = null; |
|
571 |
this.viewPort = null; |
|
572 |
this.cachedImage = null; |
|
573 |
this.previousDrawList = null; |
|
574 |
} |
|
679 | 575 |
|
680 |
private DrawList createDrawList(FLayers root, Cancellable cancel, |
|
681 |
double scale) { |
|
682 |
DrawList result = new DrawList(this.previousDrawList); |
|
683 |
Iterator iter = new MyLayerIterator((FLayer) root, scale); |
|
684 |
while (iter.hasNext()) { |
|
685 |
if (cancel.isCanceled()) { |
|
686 |
return null; |
|
687 |
} |
|
688 |
result.add(iter.next()); |
|
689 |
} |
|
690 |
if (cancel.isCanceled()) { |
|
691 |
return null; |
|
692 |
} |
|
693 |
// Take into account also the Graphic layer |
|
694 |
for (VectorLayer graphicsLayer : mapContext.getGraphicsLayers()) { |
|
695 |
result.add(graphicsLayer); |
|
576 |
public void print(FLayers root, Graphics2D g, Cancellable cancel, |
|
577 |
double scale, PrintAttributes properties) throws ReadException { |
|
578 |
this.checkInitialized(); |
|
579 |
|
|
580 |
List printList = this.createPrintList(root, cancel); |
|
581 |
if (cancel.isCanceled()) { |
|
582 |
return; |
|
583 |
} |
|
584 |
|
|
585 |
ComposedLayer composed = null; |
|
586 |
int pos; |
|
587 |
FLayer layer; |
|
588 |
int layerPos = -1; |
|
589 |
Object obj; |
|
590 |
LayersGroupEvent event; |
|
591 |
for (pos = 0; pos < printList.size(); pos++) { |
|
592 |
if (cancel.isCanceled()) { |
|
593 |
return; |
|
594 |
} |
|
595 |
|
|
596 |
obj = printList.get(pos); |
|
597 |
if (obj instanceof LayersGroupEvent) { |
|
598 |
event = (LayersGroupEvent) obj; |
|
599 |
if (event.type == LayersGroupEvent.IN_Event) { |
|
600 |
// System.out.println("=======Empiza a pintar grupo de capas "+ |
|
601 |
// ((FLayers)event.group).getName() +"============"); |
|
602 |
event.group.beginDraw(g, viewPort); |
|
603 |
} else { |
|
604 |
event.group.endDraw(g, viewPort); |
|
605 |
// System.out.println("=======Fin a pintar grupo de capas "+ |
|
606 |
// ((FLayers)event.group).getName() +"============"); |
|
607 |
|
|
696 | 608 |
} |
697 |
return result; |
|
698 |
} |
|
609 |
continue; |
|
610 |
} |
|
611 |
layerPos++; |
|
699 | 612 |
|
700 |
private List createPrintList(FLayers root, Cancellable cancel) { |
|
701 |
List result = new ArrayList(); |
|
702 |
Iterator iter = new SimpleLayerIterator((FLayer) root); |
|
703 |
while (iter.hasNext()) { |
|
704 |
if (cancel.isCanceled()) { |
|
705 |
return null; |
|
706 |
} |
|
707 |
result.add(iter.next()); |
|
708 |
} |
|
709 |
return result; |
|
710 |
} |
|
613 |
layer = (FLayer) obj; |
|
711 | 614 |
|
712 |
private class MyLayerIterator implements Iterator { |
|
713 |
List layersList = new ArrayList(); |
|
714 |
int index = 0; |
|
715 |
double scale = 0; |
|
615 |
// *** Pintado de capa/composicion de capa *** |
|
616 |
if (composed == null) { |
|
617 |
composed = layer.newComposedLayer(); |
|
618 |
if (composed != null) { |
|
619 |
try { |
|
620 |
composed.add(layer); |
|
621 |
// System.out.println("=======Imprimiendo composicion de pintado "+ |
|
622 |
// (layerPos-1)+" ============"); |
|
623 |
continue; |
|
624 |
} catch (Exception e) { |
|
625 |
throw new ReadException( |
|
626 |
"DefaultMapContexDrawer exception", e); |
|
627 |
} |
|
628 |
} |
|
629 |
} else { |
|
630 |
if (composed.canAdd(layer)) { |
|
631 |
try { |
|
632 |
composed.add(layer); |
|
633 |
// System.out.println("=== a?adiendo a composicion de pintado "+ |
|
634 |
// layerPos+ " "+layer.getName()); |
|
635 |
continue; |
|
636 |
} catch (Exception e) { |
|
637 |
throw new ReadException( |
|
638 |
"DefaultMapContexDrawer exception", e); |
|
639 |
} |
|
640 |
} else { |
|
641 |
// System.out.println("=======Imprimiendo composicion de pintado "+ |
|
642 |
// (layerPos-1)+" ============"); |
|
643 |
this.print(composed, g, cancel, scale, properties); |
|
644 |
// composed.print( g, viewPort, cancel, scale,properties); |
|
645 |
composed = layer.newComposedLayer(); |
|
646 |
if (composed != null) { |
|
647 |
try { |
|
648 |
composed.add(layer); |
|
649 |
// System.out.println("=== a?adiendo a composicion de pintado "+ |
|
650 |
// layerPos+ " "+layer.getName()); |
|
651 |
continue; |
|
652 |
} catch (Exception e) { |
|
653 |
throw new ReadException( |
|
654 |
"DefaultMapContexDrawer exception", e); |
|
655 |
} |
|
656 |
} |
|
657 |
} |
|
658 |
} |
|
659 |
// System.out.println("=== imprimiendo "+ layerPos+ |
|
660 |
// " "+layer.getName()); |
|
661 |
this.print(layer, g, cancel, scale, properties); |
|
662 |
// layer.print(g, viewPort, cancel, scale,properties); |
|
663 |
// *** Pintado de capa/composicion de capa *** |
|
664 |
if (composed != null) { |
|
665 |
// si la composicion no se ha pintado la pintamos |
|
666 |
// System.out.println("=======Imprimiendo composicion de pintado "+ |
|
667 |
// (layerPos-1)+" (ultimo) ============"); |
|
668 |
this.print(composed, g, cancel, scale, properties); |
|
669 |
// composed.print(g, viewPort, cancel, scale, properties); |
|
670 |
composed = null; |
|
671 |
} |
|
672 |
} |
|
716 | 673 |
|
717 |
public MyLayerIterator(FLayer layer, double scale) { |
|
718 |
this.scale = scale; |
|
719 |
this.appendLayer(layer); |
|
720 |
} |
|
674 |
} |
|
721 | 675 |
|
722 |
protected void appendLayer(FLayer layer) { |
|
723 |
if (layer instanceof LayerCollection) { |
|
724 |
appendLayers((LayerCollection) layer); |
|
725 |
} else if (this.evaluate(layer)) { |
|
726 |
layersList.add(layer); |
|
727 |
} |
|
728 |
} |
|
676 |
private DrawList createDrawList(FLayers root, Cancellable cancel, |
|
677 |
double scale) { |
|
678 |
DrawList result = new DrawList(this.previousDrawList); |
|
679 |
Iterator iter = new MyLayerIterator((FLayer) root, scale); |
|
680 |
while (iter.hasNext()) { |
|
681 |
if (cancel.isCanceled()) { |
|
682 |
return null; |
|
683 |
} |
|
684 |
result.add(iter.next()); |
|
685 |
} |
|
686 |
if (cancel.isCanceled()) { |
|
687 |
return null; |
|
688 |
} |
|
689 |
// Take into account also the Graphic layer |
|
690 |
for (VectorLayer graphicsLayer : mapContext.getGraphicsLayers()) { |
|
691 |
if (graphicsLayer != null && |
|
692 |
graphicsLayer.isAvailable() && |
|
693 |
graphicsLayer.isVisible() && |
|
694 |
graphicsLayer.isWithinScale(scale)) { |
|
695 |
result.add(graphicsLayer); |
|
696 |
} |
|
697 |
} |
|
698 |
return result; |
|
699 |
} |
|
729 | 700 |
|
730 |
private void appendLayers(LayerCollection layers) { |
|
731 |
int i; |
|
732 |
layersList.add(new LayersGroupEvent(layers, |
|
733 |
LayersGroupEvent.IN_Event)); |
|
734 |
for (i = 0; i < layers.getLayersCount(); i++) { |
|
735 |
appendLayer(layers.getLayer(i)); |
|
736 |
} |
|
737 |
layersList.add(new LayersGroupEvent(layers, |
|
738 |
LayersGroupEvent.OUT_Event)); |
|
739 |
} |
|
701 |
private List createPrintList(FLayers root, Cancellable cancel) { |
|
702 |
List result = new ArrayList(); |
|
703 |
Iterator iter = new SimpleLayerIterator((FLayer) root); |
|
704 |
while (iter.hasNext()) { |
|
705 |
if (cancel.isCanceled()) { |
|
706 |
return null; |
|
707 |
} |
|
708 |
result.add(iter.next()); |
|
709 |
} |
|
710 |
return result; |
|
711 |
} |
|
740 | 712 |
|
741 |
public void remove() { |
|
742 |
throw new UnsupportedOperationException(); |
|
743 |
} |
|
713 |
private class MyLayerIterator implements Iterator { |
|
744 | 714 |
|
745 |
public boolean hasNext() {
|
|
746 |
return index < layersList.size();
|
|
747 |
}
|
|
715 |
List layersList = new ArrayList();
|
|
716 |
int index = 0;
|
|
717 |
double scale = 0;
|
|
748 | 718 |
|
749 |
public Object next() { |
|
750 |
if (!this.hasNext()) { |
|
751 |
throw new NoSuchElementException(); |
|
752 |
} |
|
753 |
Object aux = layersList.get(index); |
|
754 |
index++; |
|
755 |
return aux; |
|
756 |
} |
|
719 |
public MyLayerIterator(FLayer layer, double scale) { |
|
720 |
this.scale = scale; |
|
721 |
this.appendLayer(layer); |
|
722 |
} |
|
757 | 723 |
|
758 |
public boolean evaluate(FLayer layer) {
|
|
759 |
if (layer instanceof LayerCollection) {
|
|
760 |
return false;
|
|
761 |
}
|
|
762 |
return layer.isAvailable() && layer.isVisible()
|
|
763 |
&& layer.isWithinScale(this.scale);
|
|
764 |
}
|
|
724 |
protected void appendLayer(FLayer layer) {
|
|
725 |
if (layer instanceof LayerCollection) {
|
|
726 |
appendLayers((LayerCollection) layer);
|
|
727 |
} else if (this.evaluate(layer)) {
|
|
728 |
layersList.add(layer);
|
|
729 |
}
|
|
730 |
}
|
|
765 | 731 |
|
766 |
} |
|
732 |
private void appendLayers(LayerCollection layers) { |
|
733 |
int i; |
|
734 |
layersList.add(new LayersGroupEvent(layers, |
|
735 |
LayersGroupEvent.IN_Event)); |
|
736 |
for (i = 0; i < layers.getLayersCount(); i++) { |
|
737 |
appendLayer(layers.getLayer(i)); |
|
738 |
} |
|
739 |
layersList.add(new LayersGroupEvent(layers, |
|
740 |
LayersGroupEvent.OUT_Event)); |
|
741 |
} |
|
767 | 742 |
|
768 |
private class LayersGroupEvent {
|
|
769 |
public static final String IN_Event = "in";
|
|
770 |
public static final String OUT_Event = "Out";
|
|
743 |
public void remove() {
|
|
744 |
throw new UnsupportedOperationException();
|
|
745 |
}
|
|
771 | 746 |
|
772 |
private LayerCollection group = null; |
|
773 |
private String type = IN_Event; |
|
747 |
public boolean hasNext() { |
|
748 |
return index < layersList.size(); |
|
749 |
} |
|
774 | 750 |
|
775 |
public LayersGroupEvent(LayerCollection group, String type) { |
|
776 |
this.group = group; |
|
777 |
this.type = type; |
|
778 |
} |
|
751 |
public Object next() { |
|
752 |
if (!this.hasNext()) { |
|
753 |
throw new NoSuchElementException(); |
|
754 |
} |
|
755 |
Object aux = layersList.get(index); |
|
756 |
index++; |
|
757 |
return aux; |
|
758 |
} |
|
779 | 759 |
|
780 |
public String getType() { |
|
781 |
return type; |
|
782 |
} |
|
760 |
public boolean evaluate(FLayer layer) { |
|
761 |
if (layer instanceof LayerCollection) { |
|
762 |
return false; |
|
763 |
} |
|
764 |
return layer.isAvailable() && layer.isVisible() |
|
765 |
&& layer.isWithinScale(this.scale); |
|
766 |
} |
|
783 | 767 |
|
784 |
public LayerCollection getGroup() { |
|
785 |
return group; |
|
786 |
} |
|
787 |
} |
|
768 |
} |
|
788 | 769 |
|
770 |
private class LayersGroupEvent { |
|
789 | 771 |
|
772 |
public static final String IN_Event = "in"; |
|
773 |
public static final String OUT_Event = "Out"; |
|
774 |
|
|
775 |
private LayerCollection group = null; |
|
776 |
private String type = IN_Event; |
|
777 |
|
|
778 |
public LayersGroupEvent(LayerCollection group, String type) { |
|
779 |
this.group = group; |
|
780 |
this.type = type; |
|
781 |
} |
|
782 |
|
|
783 |
public String getType() { |
|
784 |
return type; |
|
785 |
} |
|
786 |
|
|
787 |
public LayerCollection getGroup() { |
|
788 |
return group; |
|
789 |
} |
|
790 |
} |
|
791 |
|
|
790 | 792 |
public static class RegisterMapContextDrawer implements Callable { |
791 | 793 |
|
792 | 794 |
public Object call() { |
793 | 795 |
MapContextManager manager = MapContextLocator.getMapContextManager(); |
794 | 796 |
try { |
795 |
manager.setDefaultMapContextDrawer(DefaultMapContextDrawer.class);
|
|
797 |
manager.setDefaultMapContextDrawer(DefaultMapContextDrawer.class); |
|
796 | 798 |
} catch (MapContextException ex) { |
797 |
throw new RuntimeException("Can't register the default MapContextDrawer", ex);
|
|
799 |
throw new RuntimeException("Can't register the default MapContextDrawer", ex); |
|
798 | 800 |
} |
799 | 801 |
return Boolean.TRUE; |
800 | 802 |
} |
Also available in: Unified diff