Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / layer / FLayerFileVectorial.java @ 21606

History | View | Annotate | Download (10 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop 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
 *   http://www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 *
43
 *    or
44
 *
45
 *   Instituto de Rob?tica
46
 *   Apartado de correos 2085
47
 *   46071 Valencia
48
 *   (Spain)
49
 *   
50
 *   +34 963 543 577
51
 *   jjordan@robotica.uv.es
52
 *   http://robotica.uv.es
53
 *   
54
 */
55

    
56
package es.prodevelop.gvsig.mobile.fmap.layer;
57

    
58

    
59
import java.io.File;
60
import java.io.IOException;
61

    
62
import org.apache.log4j.Logger;
63

    
64
import es.prodevelop.gvsig.mobile.fmap.MapContext;
65
import es.prodevelop.gvsig.mobile.fmap.driver.FMapDriverException;
66
import es.prodevelop.gvsig.mobile.fmap.driver.VectorialDriver;
67
import es.prodevelop.gvsig.mobile.fmap.driver.VectorialFileDriver;
68
import es.prodevelop.gvsig.mobile.fmap.driver.vect.shp.DiskShpDriver;
69
import es.prodevelop.gvsig.mobile.fmap.driver.vect.shp.MemoryShpDriver;
70
import es.prodevelop.gvsig.mobile.fmap.legend.LegendFactory;
71
import es.prodevelop.gvsig.mobile.fmap.legend.SingleSymbolLegend;
72
import es.prodevelop.gvsig.mobile.fmap.symbol.FSymbol;
73
import es.prodevelop.gvsig.mobile.fmap.util.Utils;
74
//import es.prodevelop.gvsig.mobile.fmap.util.xml.IPersistance;
75
//import es.prodevelop.gvsig.mobile.fmap.util.xml.XMLEntity;
76
//import es.prodevelop.gvsig.mobile.fmap.util.xml.XMLException;
77

    
78
public class FLayerFileVectorial extends FLyrVect {
79
        
80
        private static Logger logger = Logger.getLogger(FLayerFileVectorial.class);
81
        private boolean loaded = false;
82
        private VectorialDriver vDriver = null;
83
        private boolean driver_already_open = false;
84
        
85
        public static FLayerFileVectorial createLayer(File f, String lyr_name, MapContext mc){
86
                MemoryShpDriver drv;
87
                logger.debug(Utils.time() + "MainWindow instancia el driver MEMORY SHP.");
88
                drv = new MemoryShpDriver();
89
                
90
                try {
91
                        logger.debug(Utils.time() + "MainWindow llama open(f) de driver MEMORY SHP.");
92
                        drv.open(f);
93
                        logger.debug(Utils.time() + "MainWindow llama initialize() de driver MEMORY SHP.");
94
                        drv.initialize();
95
                } catch (IOException e) {
96
                        logger.error("Error while opening file: " + e.getMessage());
97
                        
98
                        logger.debug(Utils.time() + "No hay memoria suficuente: probamos DISK SHP.");
99
                        drv = null; System.gc();
100
                        drv = new DiskShpDriver();
101

    
102
                        try {
103
                                logger.debug(Utils.time() + "MainWindow llama open(f) de driver DISK SHP.");
104
                                drv.open(f);
105
                                logger.debug(Utils.time() + "MainWindow llama initialize() de driver DISK SHP.");
106
                                drv.initialize();
107
                        } catch (IOException e1) {
108
                                logger.error("Error while opening file: " + e1.getMessage());
109
                                drv = null; System.gc();
110
                                return null;
111
                        }
112
                }
113

    
114
                logger.debug(Utils.time() + "MainWindow instancia FLayerFileVectorial.");
115
                FLayerFileVectorial shplyr = new FLayerFileVectorial();
116
                shplyr.setMapContext(mc);
117
                
118
                shplyr.setVisible(true);
119
                shplyr.setAvailable(true);
120
                
121
                /*if (in_mem) {
122
                        shplyr.setName(lyr_name + " [m]");
123
                } else {
124
                        shplyr.setName(lyr_name + " [d]");
125
                }*/
126
                shplyr.setName(lyr_name);
127
                
128

    
129
                shplyr.setDriver(drv);
130
                shplyr.setProjection(mc.getProjection());
131
                
132
                logger.debug(Utils.time() + "Vamos a llamar a wakeup.");
133
                shplyr.wakeUp(true);
134

    
135
                // System.out.println("Layer shp instanciada (no probada)");
136
                logger.debug(Utils.time() + "MainWindow llama addLayer de mapcontext.");
137

    
138
                return shplyr;
139
        }
140
        
141
        public FLayerFileVectorial() {
142
                id = System.currentTimeMillis();
143
        }
144
        
145
        /* Esto deberia ir en el FLyrDefault */
146
        /*
147
        public void setProjectionByName(String projectionName) throws Exception{                
148
                IProjection proj = CRSFactory.getCRS(projectionName);
149
                if (proj == null) {
150
                        throw new Exception("No se ha encontrado la proyeccion: "+ projectionName);
151
                }
152
                setProjection(proj);
153
        }
154
        */
155

    
156
        public void setDriver(VectorialDriver vDriver) {
157
                this.vDriver = vDriver;
158
        }
159
        
160
        public VectorialDriver getDriver() {
161
                return this.vDriver ;
162
        }
163

    
164
        public void setDriverByName(String driverName) {
165
                this.setDriver(
166
                  (VectorialDriver)LayerFactory.getDM().getDriver(driverName) 
167
                );
168
        }
169

    
170
        
171
        /* Esto deberia ir en FLyrVect */
172
        private void initializeLegendDefault() {
173
                setLegend(LegendFactory.createSingleSymbolLegend());
174
        }
175
        
176
        public void setRandomColor() {
177
                ((SingleSymbolLegend) getLegend()).setDefaultSymbol(
178
                                FSymbol.createRandomSymbol(false));
179
        }
180

    
181
        public void wakeUp(boolean driver_open) {
182
                
183
                driver_already_open = driver_open;
184
                
185
                if (!loaded) {
186
                        try {
187
                                load();
188
                        } catch (FMapDriverException e) {
189
                        }
190
                }
191
                
192
        }
193

    
194
        
195
        public void load() throws FMapDriverException {
196
                if (this.getName() == null || this.getName().length() == 0) {
197
                        this.setAvailable(false);
198
                        //TODO: traducir???
199
                        throw new FMapDriverException("No se ha especificado nombre de capa");
200
                }
201
                if (this.vDriver == null) {
202
                        this.setAvailable(false);
203
                        //TODO: traducir???
204
                        throw new FMapDriverException("No se ha especificado driver");                        
205
                }
206
                if (this.getProjection() == null) {
207
                        this.setAvailable(false);
208
                        //TODO: traducir???
209
                        throw new FMapDriverException("No se ha especificado proyeccion");                        
210
                }
211
                
212
                VectorialFileAdapter adapter = null;
213
                try {
214
                        VectorialFileDriver filedrv = (VectorialFileDriver) vDriver;
215
                        File f = filedrv.getFile();
216
                        adapter = new VectorialFileAdapter(f);
217
                        
218
                        if (driver_already_open) adapter.setReferenceCount(1);
219

    
220
                        adapter.setDriver(filedrv);
221
                        setSource(adapter);
222
                } catch (Exception e) {
223
                        this.setAvailable(false);
224
                        throw new FMapDriverException(e.getMessage());
225
                }
226
                
227
                try {
228
                        initializeLegendDefault();
229
                } catch (Exception e) {
230
                        setAvailable(false);
231
                        //TODO: traducir???
232
                        throw new FMapDriverException(e.getMessage());
233
                }
234
        }
235
/*
236
        public void setXMLEntity(XMLEntity xml) throws XMLException {
237
        IProjection proj = null;
238
        if (xml.contains("proj")) {
239
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
240
        }
241
        else
242
        {
243
            proj = this.getMapContext().getViewPort().getProjection();
244
        }
245
                this.setName(xml.getName());
246
                this.setProjection(proj);
247
                
248
        String driverName = xml.getStringProperty("other");
249
        VectorialDriver driver = null;
250
        try {
251
            driver = (VectorialDriver) LayerFactory.getDM().getDriver(driverName);
252
        } catch (DriverLoadException e) {
253
            // Si no existe ese driver, no pasa nada.
254
            // Puede que el desarrollador no quiera que
255
            // aparezca en el cuadro de di?logo y ha metido
256
            // el jar con sus clases en nuestro directorio lib.
257
            // Intentamos cargar esa clase "a pelo".
258
            if (xml.getChild(2).contains("className"))
259
            {
260
                String className2 = xml.getChild(2).getStringProperty("className");
261
                try {
262
                    driver = (VectorialDriver) Class.forName(className2).newInstance();
263
                } catch (Exception e1) {
264
                    throw new XMLException(e1);
265
                }
266
            }
267
        } catch (NullPointerException npe) {
268
            // Si no existe ese driver, no pasa nada.
269
            // Puede que el desarrollador no quiera que
270
            // aparezca en el cuadro de di?logo y ha metido
271
            // el jar con sus clases en nuestro directorio lib.
272
            // Intentamos cargar esa clase "a pelo".
273
            if (xml.getChild(2).contains("className"))
274
            {
275
                String className2 = xml.getChild(2).getStringProperty("className");
276
                try {
277
                    driver = (VectorialDriver) Class.forName(className2).newInstance();
278
                } catch (Exception e1) {
279
                    throw new XMLException(e1);
280
                }
281
            }
282
        }
283
        if (driver == null) {
284
                throw new XMLException(new Exception("Error al cargar el driver"));
285
        }
286
        if (driver instanceof IPersistance)
287
        {
288
            IPersistance persist = (IPersistance) driver;
289
            persist.setXMLEntity(xml.getChild(2));
290
        }
291
        this.setDriver(driver);
292
        super.setXMLEntity(xml);
293
        }*/
294
        
295
        public StringBuffer toXML(String name, String projectPath) {
296
                StringBuffer buff = new StringBuffer();
297
                buff.append("<" + name +">\n");
298
                
299
                        buff.append("<type>");
300
                        buff.append(this.getClass().getName());
301
                        buff.append("</type>\n");
302

    
303
                        if (vDriver instanceof VectorialFileDriver){
304
                                VectorialFileDriver drv = (VectorialFileDriver)vDriver;
305
                                buff.append("<path>");
306
                                String absPath = drv.getFilePath();
307
                                String relPath = Utils.relPath(projectPath, absPath);
308
                                if (relPath == null) relPath = absPath;
309
                                buff.append(relPath);
310
                                buff.append("</path>\n");
311
                        }
312
                
313
                buff.append("</" + name +">\n");
314
                return buff;
315
        }
316
        
317
        public String getFilePath() {
318
                
319
                if (vDriver instanceof VectorialFileDriver){
320
                        VectorialFileDriver drv = (VectorialFileDriver)vDriver;
321
                        return drv.getFilePath();
322
                }
323
                return "[None]";
324
                
325
        }
326
        
327
        public void clean() {
328
                logger.debug("Cleaning layer: " + getName());
329
                
330
                vDriver = null;
331
                setLegend(null);
332
                getSource().setDriver(null);
333
                try {
334
                        getRecordset().setSelectionSupport(null);
335
                } catch (Exception e) {
336
                        logger.error("While cleaning layer: " + e.getMessage());
337
                }
338
                setRecordset(null);
339
                System.gc();
340
        }
341
        
342
}