Statistics
| Revision:

svn-gvsig-desktop / tags / Root_Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / DefaultStrategy.java @ 1826

History | View | Annotate | Download (9.11 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
                                        geom.draw(g, viewPort, symbol);
194
                                }
195
                                /* else
196
                                {
197
                                    System.out.println("no pinto id=" + i);
198
                                } */
199
                        }
200

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

    
205

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

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

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

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

    
248
                        if (visitor.start(capa)) {
249
                                VectorialAdapter va = ((SingleLayer) capa).getSource();
250

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

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

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

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

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

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

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

    
321
                return visitor.getBitSet();
322
        }
323

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