svn-gvsig-desktop / tags / v1_0_RELEASE / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / GraphicLayer.java @ 9167
History | View | Annotate | Download (8.31 KB)
1 | 2901 | fjp | /*
|
---|---|---|---|
2 | * Created on 19-sep-2005
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | 8765 | jjdelcerro | *
|
6 | 2901 | fjp | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 | 8765 | jjdelcerro | *
|
8 | 2901 | fjp | * This program is free software; you can redistribute it and/or
|
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | 8765 | jjdelcerro | *
|
13 | 2901 | fjp | * This program is distributed in the hope that it will be useful,
|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | 8765 | jjdelcerro | *
|
18 | 2901 | fjp | * You should have received a copy of the GNU General Public License
|
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||
21 | 8765 | jjdelcerro | *
|
22 | 2901 | fjp | * For more information, contact:
|
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | 8765 | jjdelcerro | *
|
34 | 2901 | fjp | * or
|
35 | 8765 | jjdelcerro | *
|
36 | 2901 | fjp | * IVER T.I. S.A
|
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | 8765 | jjdelcerro | *
|
41 | 2901 | fjp | * +34 963163400
|
42 | * dac@iver.es
|
||
43 | */
|
||
44 | package com.iver.cit.gvsig.fmap.layers; |
||
45 | |||
46 | import java.awt.Graphics2D; |
||
47 | import java.awt.geom.Rectangle2D; |
||
48 | import java.awt.image.BufferedImage; |
||
49 | import java.util.ArrayList; |
||
50 | 8765 | jjdelcerro | import java.util.Collection; |
51 | 2901 | fjp | |
52 | 9013 | caballero | import javax.print.attribute.PrintRequestAttributeSet; |
53 | |||
54 | 4479 | fjp | import org.cresques.cts.ICoordTrans; |
55 | |||
56 | 2901 | fjp | import com.iver.cit.gvsig.fmap.DriverException; |
57 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
58 | 2974 | fjp | import com.iver.cit.gvsig.fmap.core.IGeometry; |
59 | 8765 | jjdelcerro | import com.iver.cit.gvsig.fmap.core.ISymbol; |
60 | 6945 | fjp | import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities; |
61 | 2901 | fjp | import com.iver.cit.gvsig.fmap.rendering.FGraphic; |
62 | 5317 | fjp | import com.iver.utiles.swing.threads.Cancellable; |
63 | 6945 | fjp | import com.vividsolutions.jts.index.ItemVisitor; |
64 | import com.vividsolutions.jts.index.SpatialIndex; |
||
65 | import com.vividsolutions.jts.index.quadtree.Quadtree; |
||
66 | 2901 | fjp | |
67 | public class GraphicLayer extends FLyrDefault { |
||
68 | private ArrayList graphics = new ArrayList(); |
||
69 | private ArrayList symbols = new ArrayList(); |
||
70 | private Rectangle2D fullExtent; |
||
71 | 6945 | fjp | private SpatialIndex spatialIndex = new Quadtree(); |
72 | private FBitSet selection = new FBitSet(); |
||
73 | 2901 | fjp | |
74 | public GraphicLayer() {
|
||
75 | super();
|
||
76 | 2974 | fjp | System.err.println("GraphicLayer: Constructor"); |
77 | 2901 | fjp | } |
78 | |||
79 | /* (non-Javadoc)
|
||
80 | * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
|
||
81 | */
|
||
82 | public Rectangle2D getFullExtent() throws DriverException { |
||
83 | return fullExtent;
|
||
84 | } |
||
85 | |||
86 | /* (non-Javadoc)
|
||
87 | * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable, double)
|
||
88 | */
|
||
89 | public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale) throws DriverException { |
||
90 | 8765 | jjdelcerro | if (isVisible() && isWithinScale(scale)){
|
91 | 2901 | fjp | drawGraphics(image, g, viewPort, cancel); |
92 | 8765 | jjdelcerro | } |
93 | 2901 | fjp | } |
94 | 8765 | jjdelcerro | |
95 | 6945 | fjp | /**
|
96 | 8765 | jjdelcerro | * Generic visitor pattern. It may be used for queryByRect, byPoint,
|
97 | 6945 | fjp | * by polygon, etc.
|
98 | * @param visitor
|
||
99 | * @param cancel
|
||
100 | */
|
||
101 | public void process(ItemVisitor visitor, Cancellable cancel) |
||
102 | { |
||
103 | int numReg;
|
||
104 | 8765 | jjdelcerro | |
105 | 6945 | fjp | for (numReg = 0; numReg < graphics.size(); numReg++) { |
106 | 8765 | jjdelcerro | if(cancel != null){ |
107 | if (cancel.isCanceled()) {
|
||
108 | break;
|
||
109 | } |
||
110 | 6945 | fjp | } |
111 | FGraphic theGraphic = (FGraphic) graphics.get(numReg); |
||
112 | visitor.visitItem(theGraphic); |
||
113 | } |
||
114 | 8765 | jjdelcerro | |
115 | 6945 | fjp | } |
116 | 2901 | fjp | |
117 | /* (non-Javadoc)
|
||
118 | * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable, double)
|
||
119 | */
|
||
120 | 9013 | caballero | public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties) throws DriverException { |
121 | 8765 | jjdelcerro | if (isVisible() && isWithinScale(scale)){
|
122 | 2901 | fjp | drawGraphics(null, g, viewPort, cancel);
|
123 | 8765 | jjdelcerro | } |
124 | 2901 | fjp | } |
125 | 8765 | jjdelcerro | |
126 | 2901 | fjp | private void drawGraphics(BufferedImage image, Graphics2D g, |
127 | 8765 | jjdelcerro | ViewPort viewPort, Cancellable cancel) |
128 | 2901 | fjp | { |
129 | int numReg;
|
||
130 | 2974 | fjp | Rectangle2D elExtent = viewPort.getAdjustedExtent();
|
131 | 2901 | fjp | if (elExtent == null) return; |
132 | 8765 | jjdelcerro | |
133 | 2901 | fjp | //int anchoMapa;
|
134 | //int altoMapa;
|
||
135 | //double anchoReal;
|
||
136 | //double altoReal;
|
||
137 | //double escala;
|
||
138 | 8765 | jjdelcerro | ISymbol theSymbol = null;
|
139 | 4754 | fjp | // System.out.println("Dibujando gr?ficos...(" + graphics.size() + ")");
|
140 | 8765 | jjdelcerro | |
141 | 4479 | fjp | ICoordTrans ct = getCoordTrans(); |
142 | 8765 | jjdelcerro | |
143 | 2901 | fjp | for (numReg = 0; numReg < graphics.size(); numReg++) { |
144 | if (cancel.isCanceled()) {
|
||
145 | break;
|
||
146 | } |
||
147 | 8765 | jjdelcerro | |
148 | 2901 | fjp | FGraphic theGraphic = (FGraphic) graphics.get(numReg); |
149 | 2974 | fjp | IGeometry geom = theGraphic.getGeom(); |
150 | 8765 | jjdelcerro | |
151 | 4479 | fjp | // Modificaci?n para Jorge, para que le reproyecte los gr?ficos.
|
152 | if (ct != null) { |
||
153 | geom = geom.cloneGeometry(); |
||
154 | geom.reProject(ct); |
||
155 | } |
||
156 | 8765 | jjdelcerro | |
157 | if (geom.fastIntersects(elExtent.getMinX(),
|
||
158 | 2901 | fjp | elExtent.getMinY(), elExtent.getWidth(), elExtent.getHeight())) |
159 | { |
||
160 | 8765 | jjdelcerro | theSymbol = (ISymbol) symbols.get(theGraphic.getIdSymbol()); |
161 | if (theSymbol == null) |
||
162 | continue;
|
||
163 | 6945 | fjp | if (selection.get(numReg)) // Si est? seleccinado |
164 | { |
||
165 | FGraphicUtilities.DrawHandlers(g, viewPort.getAffineTransform(), geom.getHandlers(IGeometry.SELECTHANDLER)); |
||
166 | } |
||
167 | else
|
||
168 | { |
||
169 | theGraphic.draw(g, viewPort, theSymbol); |
||
170 | } |
||
171 | 2901 | fjp | } |
172 | } |
||
173 | 8765 | jjdelcerro | } |
174 | |||
175 | public int addSymbol(ISymbol newSymbol) |
||
176 | { |
||
177 | 2901 | fjp | if (symbols.add(newSymbol))
|
178 | return symbols.size()-1; |
||
179 | return -1; |
||
180 | 8765 | jjdelcerro | |
181 | 2901 | fjp | } |
182 | 2970 | fjp | /**
|
183 | * Add a graphic to the graphic layer.
|
||
184 | * @param g
|
||
185 | */
|
||
186 | 8765 | jjdelcerro | public void addGraphic(FGraphic g) |
187 | 2901 | fjp | { |
188 | if (graphics.add(g))
|
||
189 | { |
||
190 | 8765 | jjdelcerro | |
191 | 6945 | fjp | // spatialIndex.insert(g.getGeom().getBounds2D())
|
192 | 2901 | fjp | if (fullExtent == null) { |
193 | fullExtent = g.getGeom().getBounds2D(); |
||
194 | } else {
|
||
195 | fullExtent.add(g.getGeom().getBounds2D()); |
||
196 | } |
||
197 | |||
198 | 8765 | jjdelcerro | // return graphics.size()-1;
|
199 | 2901 | fjp | } |
200 | 8765 | jjdelcerro | // return -1;
|
201 | 2901 | fjp | |
202 | } |
||
203 | 8765 | jjdelcerro | |
204 | public void insertGraphic(int position, FGraphic g) { |
||
205 | graphics.add(position, g); |
||
206 | if (fullExtent == null) { |
||
207 | fullExtent = g.getGeom().getBounds2D(); |
||
208 | } else {
|
||
209 | fullExtent.add(g.getGeom().getBounds2D()); |
||
210 | } |
||
211 | } |
||
212 | 2901 | fjp | |
213 | 2970 | fjp | /**
|
214 | 8765 | jjdelcerro | *
|
215 | 2970 | fjp | */
|
216 | 2901 | fjp | public void clearAllGraphics() |
217 | { |
||
218 | 2970 | fjp | System.err.println("Clear all graphics. Antes hab?a " + graphics.size()); |
219 | 2901 | fjp | graphics.clear(); |
220 | 8765 | jjdelcerro | fullExtent = null;
|
221 | 2901 | fjp | } |
222 | 2970 | fjp | /**
|
223 | 8765 | jjdelcerro | *
|
224 | 2970 | fjp | */
|
225 | 2901 | fjp | public void clearSymbolsGraphics() |
226 | { |
||
227 | symbols.clear(); |
||
228 | } |
||
229 | 6945 | fjp | |
230 | public FBitSet getSelection() {
|
||
231 | return selection;
|
||
232 | } |
||
233 | |||
234 | public void setSelection(FBitSet selection) { |
||
235 | this.selection = selection;
|
||
236 | } |
||
237 | |||
238 | public FBitSet queryByRect(Rectangle2D r) { |
||
239 | FBitSet newSel = new FBitSet();
|
||
240 | for (int numReg = 0; numReg < graphics.size(); numReg++) { |
||
241 | 8765 | jjdelcerro | |
242 | 6945 | fjp | FGraphic theGraphic = (FGraphic) graphics.get(numReg); |
243 | IGeometry geom = theGraphic.getGeom(); |
||
244 | if (geom.intersects(r))
|
||
245 | { |
||
246 | newSel.set(numReg); |
||
247 | } |
||
248 | } |
||
249 | return newSel;
|
||
250 | } |
||
251 | 8765 | jjdelcerro | |
252 | public void removeGraphic(FGraphic graphic) { |
||
253 | graphics.remove(graphic); |
||
254 | reCalculateFullExtent(); |
||
255 | |||
256 | } |
||
257 | |||
258 | public FGraphic getGraphic(int idGraphic) { |
||
259 | return (FGraphic) graphics.get(idGraphic);
|
||
260 | } |
||
261 | |||
262 | public FGraphic getGraphicByObjectTag(Object objectTag) { |
||
263 | for (int i = 0; i < graphics.size(); i++) { |
||
264 | FGraphic theGraphic = (FGraphic) graphics.get(i); |
||
265 | if (theGraphic.getObjectTag() == objectTag)
|
||
266 | return theGraphic;
|
||
267 | } |
||
268 | return null; |
||
269 | } |
||
270 | |||
271 | |||
272 | public int getNumGraphics() { |
||
273 | return graphics.size();
|
||
274 | } |
||
275 | |||
276 | private void reCalculateFullExtent(){ |
||
277 | if (graphics.size() > 0) { |
||
278 | FGraphic g = (FGraphic) graphics.get(0);
|
||
279 | fullExtent = g.getGeom().getBounds2D(); |
||
280 | for (int i = 1; i < graphics.size(); i++) { |
||
281 | g = (FGraphic) graphics.get(i); |
||
282 | Rectangle2D rAux = g.getGeom().getBounds2D();
|
||
283 | |||
284 | fullExtent.add(rAux); |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | public void inserGraphics(int index, Collection c) { |
||
290 | graphics.addAll(index, c); |
||
291 | |||
292 | } |
||
293 | |||
294 | 2901 | fjp | } |