Statistics
| Revision:

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

History | View | Annotate | Download (8.61 KB)

1
/*
2
 * Created on 19-sep-2005
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * 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
 *
13
 * 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
 *
18
 * 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
 *
22
 * 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
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +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
import java.util.Collection;
51

    
52
import javax.print.attribute.PrintRequestAttributeSet;
53

    
54
import org.cresques.cts.ICoordTrans;
55

    
56
import com.iver.cit.gvsig.fmap.DriverException;
57
import com.iver.cit.gvsig.fmap.ViewPort;
58
import com.iver.cit.gvsig.fmap.core.IGeometry;
59
import com.iver.cit.gvsig.fmap.core.ISymbol;
60
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
61
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
62
import com.iver.utiles.swing.threads.Cancellable;
63
import com.vividsolutions.jts.index.ItemVisitor;
64
import com.vividsolutions.jts.index.SpatialIndex;
65
import com.vividsolutions.jts.index.quadtree.Quadtree;
66

    
67
public class GraphicLayer extends FLyrDefault {
68
    private ArrayList graphics = new ArrayList();
69
    private ArrayList symbols = new ArrayList();
70
    private Rectangle2D fullExtent;
71
    private SpatialIndex spatialIndex = new Quadtree();
72
    private FBitSet selection = new FBitSet();
73

    
74
    public GraphicLayer() {
75
        super();
76
        System.err.println("GraphicLayer: Constructor");
77
    }
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
        if (isVisible() && isWithinScale(scale)){
91
            drawGraphics(image, g, viewPort, cancel);
92
            }
93
    }
94

    
95
    /**
96
     * Generic visitor pattern. It may be used for queryByRect, byPoint,
97
     * by polygon, etc.
98
     * @param visitor
99
     * @param cancel
100
     */
101
    public void process(ItemVisitor visitor, Cancellable cancel)
102
    {
103
        int numReg;
104

    
105
        for (numReg = 0; numReg < graphics.size(); numReg++) {
106
           if(cancel != null){
107
                        if (cancel.isCanceled()) {
108
                        break;
109
                    }
110
            }
111
            FGraphic theGraphic = (FGraphic) graphics.get(numReg);
112
            visitor.visitItem(theGraphic);
113
        }
114

    
115
    }
116

    
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
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties) throws DriverException {
121
        if (isVisible() && isWithinScale(scale)){
122
            drawGraphics(null, g, viewPort, cancel);
123
            }
124
    }
125

    
126
    private void drawGraphics(BufferedImage image, Graphics2D g,
127
            ViewPort viewPort, Cancellable cancel)
128
    {
129
        int numReg;
130
        Rectangle2D elExtent = viewPort.getAdjustedExtent();
131
        if (elExtent == null) return;
132

    
133
        //int anchoMapa;
134
        //int altoMapa;
135
        //double anchoReal;
136
        //double altoReal;
137
        //double escala;
138
        ISymbol theSymbol = null;
139
        // System.out.println("Dibujando gr?ficos...(" + graphics.size() + ")");
140

    
141
        ICoordTrans ct = getCoordTrans();
142

    
143
        for (numReg = 0; numReg < graphics.size(); numReg++) {
144
            if (cancel.isCanceled()) {
145
                break;
146
            }
147

    
148
            FGraphic theGraphic = (FGraphic) graphics.get(numReg);
149
            IGeometry geom = theGraphic.getGeom();
150

    
151
            // 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

    
157
            if (geom.fastIntersects(elExtent.getMinX(),
158
                        elExtent.getMinY(), elExtent.getWidth(), elExtent.getHeight()))
159
             {
160
                theSymbol = (ISymbol) symbols.get(theGraphic.getIdSymbol());
161
                if (theSymbol == null)
162
                        continue;
163
                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
            }
172
        }
173
    }
174

    
175
    public int addSymbol(ISymbol newSymbol)
176
    {
177
        if (symbols.add(newSymbol))
178
            return symbols.size()-1;
179
        return -1;
180

    
181
    }
182
    /**
183
     * Add a graphic to the graphic layer.
184
     * @param g
185
     */
186
    public void addGraphic(FGraphic g)
187
    {
188
        if (graphics.add(g))
189
        {
190

    
191
//                spatialIndex.insert(g.getGeom().getBounds2D())
192
            if (fullExtent == null) {
193
                fullExtent = g.getGeom().getBounds2D();
194
            } else {
195
                fullExtent.add(g.getGeom().getBounds2D());
196
            }
197

    
198
//            return graphics.size()-1;
199
        }
200
//        return -1;
201

    
202
    }
203
    
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

    
213
    /**
214
     *
215
     */
216
    public void clearAllGraphics()
217
    {
218
        System.err.println("Clear all graphics. Antes hab?a " + graphics.size());
219
        graphics.clear();
220
        fullExtent = null;
221
    }
222
    /**
223
     *
224
     */
225
    public void clearSymbolsGraphics()
226
    {
227
        symbols.clear();
228
    }
229

    
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

    
242
            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

    
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
        public FLayer cloneLayer() throws Exception {
295
                GraphicLayer lyr = new GraphicLayer();
296
                for (int i =0; i < symbols.size(); i++)
297
                {
298
                        lyr.addSymbol((ISymbol) symbols.get(i));
299
                }
300
                for (int i =0; i < graphics.size(); i++)
301
                {
302
                        lyr.addGraphic((FGraphic) graphics.get(i));
303
                }
304
                
305
                return lyr;
306
        }
307

    
308
}