Statistics
| Revision:

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

History | View | Annotate | Download (8.66 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.core.v02.FSymbol;
62
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
63
import com.iver.utiles.swing.threads.Cancellable;
64
import com.vividsolutions.jts.index.ItemVisitor;
65
import com.vividsolutions.jts.index.SpatialIndex;
66
import com.vividsolutions.jts.index.quadtree.Quadtree;
67

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

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

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

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

    
116
    }
117

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

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

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

    
142
        ICoordTrans ct = getCoordTrans();
143

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

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

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

    
158
            if (geom.fastIntersects(elExtent.getMinX(),
159
                        elExtent.getMinY(), elExtent.getWidth(), elExtent.getHeight()))
160
             {
161
                theSymbol = (ISymbol) symbols.get(theGraphic.getIdSymbol());
162
                if (theSymbol == null)
163
                        continue;
164
                if (selection.get(numReg)) // Si est? seleccinado
165
                {
166
                        FGraphicUtilities.DrawHandlers(g, viewPort.getAffineTransform(), geom.getHandlers(IGeometry.SELECTHANDLER),(FSymbol)theSymbol);
167
                }
168
                else
169
                {
170
                        theGraphic.draw(g, viewPort, theSymbol);
171
                }
172
            }
173
        }
174
    }
175

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

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

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

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

    
203
    }
204

    
205
    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

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

    
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

    
243
            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

    
253
        public void removeGraphic(FGraphic graphic) {
254
                graphics.remove(graphic);
255
                reCalculateFullExtent();
256

    
257
        }
258

    
259
        public FGraphic getGraphic(int idGraphic) {
260
                return (FGraphic) graphics.get(idGraphic);
261
        }
262

    
263
        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

    
273
        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

    
293
        }
294

    
295
        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

    
306
                return lyr;
307
        }
308

    
309
}