Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1010 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / GraphicLayer.java @ 12804

History | View | Annotate | Download (8.66 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 11434 caballero
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
62 2901 fjp
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
63 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
64 6945 fjp
import com.vividsolutions.jts.index.ItemVisitor;
65
import com.vividsolutions.jts.index.SpatialIndex;
66
import com.vividsolutions.jts.index.quadtree.Quadtree;
67 2901 fjp
68
public class GraphicLayer extends FLyrDefault {
69
    private ArrayList graphics = new ArrayList();
70
    private ArrayList symbols = new ArrayList();
71
    private Rectangle2D fullExtent;
72 6945 fjp
    private SpatialIndex spatialIndex = new Quadtree();
73
    private FBitSet selection = new FBitSet();
74 2901 fjp
75
    public GraphicLayer() {
76
        super();
77 2974 fjp
        System.err.println("GraphicLayer: Constructor");
78 2901 fjp
    }
79
80
    /* (non-Javadoc)
81
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
82
     */
83
    public Rectangle2D getFullExtent() throws DriverException {
84
        return fullExtent;
85
    }
86
87
    /* (non-Javadoc)
88
     * @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)
89
     */
90
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale) throws DriverException {
91 8765 jjdelcerro
        if (isVisible() && isWithinScale(scale)){
92 2901 fjp
            drawGraphics(image, g, viewPort, cancel);
93 8765 jjdelcerro
            }
94 2901 fjp
    }
95 8765 jjdelcerro
96 6945 fjp
    /**
97 8765 jjdelcerro
     * Generic visitor pattern. It may be used for queryByRect, byPoint,
98 6945 fjp
     * by polygon, etc.
99
     * @param visitor
100
     * @param cancel
101
     */
102
    public void process(ItemVisitor visitor, Cancellable cancel)
103
    {
104
        int numReg;
105 8765 jjdelcerro
106 6945 fjp
        for (numReg = 0; numReg < graphics.size(); numReg++) {
107 8765 jjdelcerro
           if(cancel != null){
108
                        if (cancel.isCanceled()) {
109
                        break;
110
                    }
111 6945 fjp
            }
112
            FGraphic theGraphic = (FGraphic) graphics.get(numReg);
113
            visitor.visitItem(theGraphic);
114
        }
115 8765 jjdelcerro
116 6945 fjp
    }
117 2901 fjp
118
    /* (non-Javadoc)
119
     * @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)
120
     */
121 9013 caballero
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties) throws DriverException {
122 8765 jjdelcerro
        if (isVisible() && isWithinScale(scale)){
123 2901 fjp
            drawGraphics(null, g, viewPort, cancel);
124 8765 jjdelcerro
            }
125 2901 fjp
    }
126 8765 jjdelcerro
127 2901 fjp
    private void drawGraphics(BufferedImage image, Graphics2D g,
128 8765 jjdelcerro
            ViewPort viewPort, Cancellable cancel)
129 2901 fjp
    {
130
        int numReg;
131 2974 fjp
        Rectangle2D elExtent = viewPort.getAdjustedExtent();
132 2901 fjp
        if (elExtent == null) return;
133 8765 jjdelcerro
134 2901 fjp
        //int anchoMapa;
135
        //int altoMapa;
136
        //double anchoReal;
137
        //double altoReal;
138
        //double escala;
139 8765 jjdelcerro
        ISymbol theSymbol = null;
140 4754 fjp
        // System.out.println("Dibujando gr?ficos...(" + graphics.size() + ")");
141 8765 jjdelcerro
142 4479 fjp
        ICoordTrans ct = getCoordTrans();
143 8765 jjdelcerro
144 2901 fjp
        for (numReg = 0; numReg < graphics.size(); numReg++) {
145
            if (cancel.isCanceled()) {
146
                break;
147
            }
148 8765 jjdelcerro
149 2901 fjp
            FGraphic theGraphic = (FGraphic) graphics.get(numReg);
150 2974 fjp
            IGeometry geom = theGraphic.getGeom();
151 8765 jjdelcerro
152 4479 fjp
            // Modificaci?n para Jorge, para que le reproyecte los gr?ficos.
153
                        if (ct != null) {
154
                        geom = geom.cloneGeometry();
155
                                geom.reProject(ct);
156
                        }
157 8765 jjdelcerro
158
            if (geom.fastIntersects(elExtent.getMinX(),
159 2901 fjp
                        elExtent.getMinY(), elExtent.getWidth(), elExtent.getHeight()))
160
             {
161 8765 jjdelcerro
                theSymbol = (ISymbol) symbols.get(theGraphic.getIdSymbol());
162
                if (theSymbol == null)
163
                        continue;
164 6945 fjp
                if (selection.get(numReg)) // Si est? seleccinado
165
                {
166 11434 caballero
                        FGraphicUtilities.DrawHandlers(g, viewPort.getAffineTransform(), geom.getHandlers(IGeometry.SELECTHANDLER),(FSymbol)theSymbol);
167 6945 fjp
                }
168
                else
169
                {
170
                        theGraphic.draw(g, viewPort, theSymbol);
171
                }
172 2901 fjp
            }
173
        }
174 8765 jjdelcerro
    }
175
176
    public int addSymbol(ISymbol newSymbol)
177
    {
178 2901 fjp
        if (symbols.add(newSymbol))
179
            return symbols.size()-1;
180
        return -1;
181 8765 jjdelcerro
182 2901 fjp
    }
183 2970 fjp
    /**
184
     * Add a graphic to the graphic layer.
185
     * @param g
186
     */
187 8765 jjdelcerro
    public void addGraphic(FGraphic g)
188 2901 fjp
    {
189
        if (graphics.add(g))
190
        {
191 8765 jjdelcerro
192 6945 fjp
//                spatialIndex.insert(g.getGeom().getBounds2D())
193 2901 fjp
            if (fullExtent == null) {
194
                fullExtent = g.getGeom().getBounds2D();
195
            } else {
196
                fullExtent.add(g.getGeom().getBounds2D());
197
            }
198
199 8765 jjdelcerro
//            return graphics.size()-1;
200 2901 fjp
        }
201 8765 jjdelcerro
//        return -1;
202 2901 fjp
203
    }
204 11434 caballero
205 8765 jjdelcerro
    public void insertGraphic(int position, FGraphic g) {
206
            graphics.add(position, g);
207
        if (fullExtent == null) {
208
            fullExtent = g.getGeom().getBounds2D();
209
        } else {
210
            fullExtent.add(g.getGeom().getBounds2D());
211
        }
212
    }
213 2901 fjp
214 2970 fjp
    /**
215 8765 jjdelcerro
     *
216 2970 fjp
     */
217 2901 fjp
    public void clearAllGraphics()
218
    {
219 2970 fjp
        System.err.println("Clear all graphics. Antes hab?a " + graphics.size());
220 2901 fjp
        graphics.clear();
221 8765 jjdelcerro
        fullExtent = null;
222 2901 fjp
    }
223 2970 fjp
    /**
224 8765 jjdelcerro
     *
225 2970 fjp
     */
226 2901 fjp
    public void clearSymbolsGraphics()
227
    {
228
        symbols.clear();
229
    }
230 6945 fjp
231
        public FBitSet getSelection() {
232
                return selection;
233
        }
234
235
        public void setSelection(FBitSet selection) {
236
                this.selection = selection;
237
        }
238
239
        public FBitSet queryByRect(Rectangle2D r) {
240
                FBitSet newSel = new FBitSet();
241
        for (int numReg = 0; numReg < graphics.size(); numReg++) {
242 8765 jjdelcerro
243 6945 fjp
            FGraphic theGraphic = (FGraphic) graphics.get(numReg);
244
            IGeometry geom = theGraphic.getGeom();
245
            if (geom.intersects(r))
246
            {
247
                    newSel.set(numReg);
248
            }
249
        }
250
        return newSel;
251
        }
252 8765 jjdelcerro
253
        public void removeGraphic(FGraphic graphic) {
254
                graphics.remove(graphic);
255
                reCalculateFullExtent();
256 11434 caballero
257 8765 jjdelcerro
        }
258 11434 caballero
259 8765 jjdelcerro
        public FGraphic getGraphic(int idGraphic) {
260
                return (FGraphic) graphics.get(idGraphic);
261
        }
262 11434 caballero
263 8765 jjdelcerro
        public FGraphic getGraphicByObjectTag(Object objectTag) {
264
        for (int i = 0; i < graphics.size(); i++) {
265
             FGraphic theGraphic = (FGraphic) graphics.get(i);
266
             if (theGraphic.getObjectTag() == objectTag)
267
                     return theGraphic;
268
         }
269
        return null;
270
        }
271
272 11434 caballero
273 8765 jjdelcerro
        public int getNumGraphics() {
274
                return graphics.size();
275
        }
276
277
        private void reCalculateFullExtent(){
278
                if (graphics.size() > 0) {
279
                        FGraphic g = (FGraphic) graphics.get(0);
280
                        fullExtent = g.getGeom().getBounds2D();
281
                        for (int i = 1; i < graphics.size(); i++) {
282
                                g = (FGraphic) graphics.get(i);
283
                                Rectangle2D rAux = g.getGeom().getBounds2D();
284
285
                                fullExtent.add(rAux);
286
                        }
287
                }
288
        }
289
290
        public void inserGraphics(int index, Collection c) {
291
                graphics.addAll(index, c);
292 11434 caballero
293 8765 jjdelcerro
        }
294
295 9226 fjp
        public FLayer cloneLayer() throws Exception {
296
                GraphicLayer lyr = new GraphicLayer();
297
                for (int i =0; i < symbols.size(); i++)
298
                {
299
                        lyr.addSymbol((ISymbol) symbols.get(i));
300
                }
301
                for (int i =0; i < graphics.size(); i++)
302
                {
303
                        lyr.addGraphic((FGraphic) graphics.get(i));
304
                }
305 11434 caballero
306 9226 fjp
                return lyr;
307
        }
308
309 2901 fjp
}