Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / GraphicLayer.java @ 12043

History | View | Annotate | Download (8.11 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.hardcode.gdbms.driver.exceptions.ReadDriverException;
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.symbols.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
    }
77

    
78
    /* (non-Javadoc)
79
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
80
     */
81
    public Rectangle2D getFullExtent() {
82
        return fullExtent;
83
    }
84

    
85
    /* (non-Javadoc)
86
     * @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)
87
     */
88
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale) throws ReadDriverException {
89
        if (isVisible() && isWithinScale(scale)){
90
            drawGraphics(image, g, viewPort, cancel);
91
            }
92
    }
93

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

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

    
114
    }
115

    
116
    /* (non-Javadoc)
117
     * @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)
118
     */
119
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet properties) throws ReadDriverException {
120
        if (isVisible() && isWithinScale(scale)){
121
            drawGraphics(null, g, viewPort, cancel);
122
            }
123
    }
124

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

    
132
        //int anchoMapa;
133
        //int altoMapa;
134
        //double anchoReal;
135
        //double altoReal;
136
        //double escala;
137
        ISymbol theSymbol = null;
138

    
139
        ICoordTrans ct = getCoordTrans();
140

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

    
146
            FGraphic theGraphic = (FGraphic) graphics.get(numReg);
147
            IGeometry geom = theGraphic.getGeom();
148

    
149
            // Modificaci?n para Jorge, para que le reproyecte los gr?ficos.
150
                        if (ct != null) {
151
                        geom = geom.cloneGeometry();
152
                                geom.reProject(ct);
153
                        }
154

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

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

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

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

    
196
//            return graphics.size()-1;
197
        }
198
//        return -1;
199

    
200
    }
201

    
202
    public void insertGraphic(int position, FGraphic g) {
203
            graphics.add(position, g);
204
        if (fullExtent == null) {
205
            fullExtent = g.getGeom().getBounds2D();
206
        } else {
207
            fullExtent.add(g.getGeom().getBounds2D());
208
        }
209
    }
210

    
211
    /**
212
     *
213
     */
214
    public void clearAllGraphics()
215
    {
216
        graphics.clear();
217
        fullExtent = null;
218
    }
219
    /**
220
     *
221
     */
222
    public void clearSymbolsGraphics()
223
    {
224
        symbols.clear();
225
    }
226

    
227
        public FBitSet getSelection() {
228
                return selection;
229
        }
230

    
231
        public void setSelection(FBitSet selection) {
232
                this.selection = selection;
233
        }
234

    
235
        public FBitSet queryByRect(Rectangle2D r) {
236
                FBitSet newSel = new FBitSet();
237
        for (int numReg = 0; numReg < graphics.size(); numReg++) {
238

    
239
            FGraphic theGraphic = (FGraphic) graphics.get(numReg);
240
            IGeometry geom = theGraphic.getGeom();
241
            if (geom.intersects(r))
242
            {
243
                    newSel.set(numReg);
244
            }
245
        }
246
        return newSel;
247
        }
248

    
249
        public void removeGraphic(FGraphic graphic) {
250
                graphics.remove(graphic);
251
                reCalculateFullExtent();
252

    
253
        }
254

    
255
        public FGraphic getGraphic(int idGraphic) {
256
                return (FGraphic) graphics.get(idGraphic);
257
        }
258

    
259
        public FGraphic getGraphicByObjectTag(Object objectTag) {
260
        for (int i = 0; i < graphics.size(); i++) {
261
             FGraphic theGraphic = (FGraphic) graphics.get(i);
262
             if (theGraphic.getObjectTag() == objectTag)
263
                     return theGraphic;
264
         }
265
        return null;
266
        }
267

    
268

    
269
        public int getNumGraphics() {
270
                return graphics.size();
271
        }
272

    
273
        private void reCalculateFullExtent(){
274
                if (graphics.size() > 0) {
275
                        FGraphic g = (FGraphic) graphics.get(0);
276
                        fullExtent = g.getGeom().getBounds2D();
277
                        for (int i = 1; i < graphics.size(); i++) {
278
                                g = (FGraphic) graphics.get(i);
279
                                Rectangle2D rAux = g.getGeom().getBounds2D();
280

    
281
                                fullExtent.add(rAux);
282
                        }
283
                }
284
        }
285

    
286
        public void inserGraphics(int index, Collection c) {
287
                graphics.addAll(index, c);
288

    
289
        }
290

    
291
}