Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DefaultStrategy.java @ 2454

History | View | Annotate | Download (9.19 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.operations.strategies;
42

    
43
import java.awt.Graphics2D;
44
import java.awt.geom.AffineTransform;
45
import java.awt.geom.Point2D;
46
import java.awt.geom.Rectangle2D;
47
import java.awt.image.BufferedImage;
48
import java.util.BitSet;
49

    
50
import org.apache.log4j.Logger;
51
import org.cresques.cts.ICoordTrans;
52

    
53
import com.iver.cit.gvsig.fmap.DriverException;
54
import com.iver.cit.gvsig.fmap.ViewPort;
55
import com.iver.cit.gvsig.fmap.core.IGeometry;
56
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
57
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
58
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
59
import com.iver.cit.gvsig.fmap.layers.FBitSet;
60
import com.iver.cit.gvsig.fmap.layers.FLayer;
61
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
62
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
63
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
64
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
65
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
66
import com.iver.cit.gvsig.fmap.operations.Cancellable;
67
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
68
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
69

    
70

    
71
/**
72
 * Implementa la Strategy por defecto. Los m?todos que tendr?n en com?n la
73
 * mayor parte de las estrategias
74
 */
75
public class DefaultStrategy implements Strategy {
76
        private static Logger logger = Logger.getLogger(DefaultStrategy.class.getName());
77
        FLayer capa = null;
78

    
79
        /**
80
         * Crea un nuevo DefaultStrategy.
81
         *
82
         * @param capa DOCUMENT ME!
83
         */
84
        public DefaultStrategy(FLayer capa) {
85
                this.capa = capa;
86

    
87
                SingleLayer foo = (SingleLayer) capa;
88
                ClassifiableVectorial vectorial = (ClassifiableVectorial) capa;
89
        }
90

    
91
        /**
92
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
93
         */
94
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
95
                QueryByRectVisitor visitor = new QueryByRectVisitor();
96

    
97
                visitor.setRect(rect);
98

    
99
                try {
100
                        process(visitor);
101
                } catch (VisitException e) {
102
                        throw new RuntimeException(
103
                                "QueryByRectVisitor lanza una VisitException?");
104
                }
105

    
106
                return visitor.getBitSet();
107
        }
108

    
109
        /**
110
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
111
         *                 int)
112
         */
113
        public FBitSet queryByShape(IGeometry g, int relationship)
114
                throws DriverException, VisitException {
115
                QueryByShapeVisitor visitor = new QueryByShapeVisitor();
116
                visitor.setRelationShip(relationship);
117
                visitor.setShape(g);
118
                process(visitor);
119

    
120
                return visitor.getBitSet();
121
        }
122

    
123
        /**
124
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
125
         */
126
        public Rectangle2D getSelectionBounds() {
127
                return null;
128
        }
129

    
130
        /**
131
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#createIndex()
132
         */
133
        public void createIndex() {
134
        }
135

    
136
        /**
137
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
138
         *                 java.awt.Graphics2D, FStyle2D)
139
         */
140
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
141
                Cancellable cancel) throws DriverException {
142
                try {
143
                        VectorialAdapter adapter = ((SingleLayer) capa).getSource();
144
                        ICoordTrans ct = getCapa().getCoordTrans();
145
                        logger.debug("adapter.start()");
146
                        adapter.start();
147

    
148
                        logger.debug("getCapa().getRecordset().start()");
149
                        SelectableDataSource rsSel = ((AlphanumericData) getCapa()).getRecordset(); 
150
                        if (rsSel != null)
151
                            rsSel.start();
152

    
153
                        int sc;
154
                        Rectangle2D extent = viewPort.getAdjustedExtent();
155
                        AffineTransform at = viewPort.getAffineTransform();
156

    
157
                        sc = adapter.getShapeCount();
158
                        // TODO: A revisar si es o no conveniente este sistema
159
                        // de comunicaci?n con los drivers.
160
                        DriverAttributes attr = adapter.getDriverAttributes();
161
                        boolean bMustClone = false;
162
                        if (attr != null)
163
                        {
164
                            if (attr.isLoadedInMemory())
165
                            {
166
                                bMustClone = attr.isLoadedInMemory();                            
167
                            }
168
                        }
169
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) capa).getLegend();
170
                        for (int i = 0; i < sc; i++) {
171
                                if (cancel.isCanceled()) {
172
                                        break;
173
                                }
174

    
175
                                
176
                                IGeometry geom = adapter.getShape(i);
177

    
178
                                if (geom == null) {
179
                                        continue;
180
                                }
181

    
182
                                if (ct != null) {
183
                                    if (bMustClone)
184
                                        geom = geom.cloneGeometry();                                    
185
                                        geom.reProject(ct);
186
                                }
187
                                
188
                                // if (geom.intersects(extent)) {
189
                                if (geom.fastIntersects(extent.getMinX(), extent.getMinY(), 
190
                                         extent.getWidth(), extent.getHeight())) {
191
                                        FSymbol symbol = l.getSymbol(i);
192
                                        
193
                    if (symbol ==null)
194
                        continue;
195
                                        geom.draw(g, viewPort, symbol);
196
                                }
197
                                /* else
198
                                {
199
                                    System.out.println("no pinto id=" + i);
200
                                } */
201
                        }
202

    
203
                        logger.debug("getCapa().getRecordset().stop()");
204
                        if (rsSel != null)
205
                            rsSel.stop();
206

    
207

    
208
                        logger.debug("adapter.stop()");
209
                        adapter.stop();
210
                        // TODO: A revisar si es o no conveniente este sistema
211
                        // de comunicaci?n con los drivers.
212
                        // DriverAttributes attr = adapter.getDriverAttributes();
213
                        /* if (attr != null)
214
                        {
215
                            if (attr.isLoadedInMemory())
216
                            {
217
                                // Quitamos lo de la reproyecci?n al vuelo para que 
218
                                // solo se haga una vez.
219
                                getCapa().setCoordTrans(null);
220
                            }
221
                        } */
222

    
223
                } catch (DriverIOException e) {
224
                        throw new DriverException(e);
225
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
226
                    throw new DriverException(e);
227
                } catch (DriverException e) {
228
                    throw new DriverException(e);
229
        }
230
        }
231

    
232
        /**
233
         * Devuelve la capa.
234
         *
235
         * @return Returns the capa.
236
         */
237
        public FLayer getCapa() {
238
                return capa;
239
        }
240

    
241
        /**
242
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor,
243
         *                 java.util.BitSet)
244
         */
245
        public void process(FeatureVisitor visitor, BitSet subset)
246
                throws DriverException, VisitException {
247
                try {
248
                        logger.debug("visitor.start()");
249

    
250
                        if (visitor.start(capa)) {
251
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
252

    
253
                                va.start();
254
                                for (int i = 0; i < va.getShapeCount(); i++) {
255
                                        if (subset.get(i)) {
256
                                                visitor.visit(va.getShape(i), i);
257
                                        }
258
                                }
259
                                va.stop();
260

    
261
                                logger.debug("visitor.stop()");
262
                                visitor.stop(capa);
263
                        }
264
                } catch (DriverIOException e) {
265
                        throw new DriverException(e);
266
                }
267
        }
268

    
269
        /**
270
         * DOCUMENT ME!
271
         *
272
         * @param visitor DOCUMENT ME!
273
         *
274
         * @throws DriverException DOCUMENT ME!
275
         * @throws VisitException
276
         *
277
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor)
278
         */
279
        public void process(FeatureVisitor visitor)
280
                throws DriverException, VisitException {
281
                try {
282
                        logger.debug("visitor.start()");
283

    
284
                        if (visitor.start(capa)) {
285
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
286
                                ICoordTrans ct = getCapa().getCoordTrans();
287
                                va.start();
288
                                for (int i = 0; i < va.getShapeCount(); i++) {
289
                                    IGeometry geom = va.getShape(i);
290
                                    if (ct != null) {
291
                                                geom.reProject(ct);
292
                                        }
293

    
294
                                        visitor.visit(geom, i);
295
                                }
296
                                va.stop();
297
                                logger.debug("visitor.stop()");
298
                                visitor.stop(capa);
299
                        }
300
                } catch (DriverIOException e) {
301
                        throw new DriverException(e);
302
                }
303
        }
304

    
305
        /**
306
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
307
         *                 double)
308
         */
309
        public FBitSet queryByPoint(Point2D p, double tolerance)
310
                throws DriverException {
311
                QueryByPointVisitor visitor = new QueryByPointVisitor();
312
                visitor.setLayer(capa);
313
                visitor.setTolerance(tolerance);
314
                visitor.setQueriedPoint(p);
315

    
316
                try {
317
                        process(visitor);
318
                } catch (VisitException e) {
319
                        throw new RuntimeException(
320
                                "QueryByPointVisitor lanza una VisitException?");
321
                }
322

    
323
                return visitor.getBitSet();
324
        }
325

    
326
        /* (non-Javadoc)
327
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort)
328
         */
329
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
330
                throws DriverException {
331
                draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
332
        }
333
}