Statistics
| Revision:

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

History | View | Annotate | Download (6.64 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.layers;
42

    
43
import java.awt.geom.Rectangle2D;
44
import java.io.File;
45
import java.io.IOException;
46

    
47
import com.hardcode.driverManager.DriverLoadException;
48
import com.hardcode.gdbms.engine.data.DataSource;
49
import com.hardcode.gdbms.engine.data.DataSourceFactory;
50
import com.hardcode.gdbms.engine.data.NoSuchTableException;
51
import com.hardcode.gdbms.engine.data.driver.DriverException;
52
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
53
import com.iver.cit.gvsig.fmap.core.IGeometry;
54
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
55
import com.iver.cit.gvsig.fmap.drivers.ExternalData;
56
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
57
import com.iver.cit.gvsig.fmap.rendering.indexes.Index;
58
import com.iver.cit.gvsig.fmap.rendering.indexes.IndexNotExistsException;
59
import com.iver.cit.gvsig.fmap.write.FileWriterDriver;
60

    
61

    
62
/**
63
 * Adapta un driver de fichero vectorial a la interfaz vectorial, manteniendo
64
 * adem?s el estado necesario por una capa vectorial de fichero (el nombre del
65
 * fichero)
66
 */
67
public class VectorialFileAdapter extends VectorialAdapter implements Index {
68
        private boolean driverInitialized = false;
69
        private File file;
70
        private FileWriterDriver writeDriver;
71
        private DataSource ds;
72
        private String dataSourceName;
73
        
74
        /**
75
         * <code>reference_count</code> lleva un contador de referencias a este
76
         * adaptador, de forma que si la cuenta de referencias no es cero, no 
77
         * abrimos otra vez el adaptador porque se supone que est? abierto.
78
         */
79
        private int reference_count = 0;
80

    
81
        /**
82
         * Crea un nuevo VectorialFileAdapter.
83
         *
84
         * @param file Fichero.
85
         */
86
        public VectorialFileAdapter(File file) {
87
                this.file = file;
88
        }
89

    
90
        /**
91
         * Devuelve driver.
92
         *
93
         * @return VectorialFileDriver.
94
         */
95
        private VectorialFileDriver getFileDriver() {
96
                return (VectorialFileDriver) getDriver();
97
        }
98

    
99
        /**
100
         * incrementa el contador de las veces que se ha abierto el fichero.
101
         * Solamente cuando el contador est? a cero pide al driver que abra el
102
         * fichero
103
         *
104
         * @throws DriverIOException
105
         */
106
        public void start() throws DriverIOException {
107
                try {
108
                    if (reference_count == 0)
109
                    {
110
                                getFileDriver().open(file);
111
        
112
                                if (!driverInitialized) {
113
                                        getFileDriver().initialize();
114
                                        driverInitialized = true;
115
                                }
116
                    }
117
                        reference_count++;
118

    
119
                } catch (IOException e) {
120
                        throw new DriverIOException(e);
121
                }
122
        }
123

    
124
        /**
125
         * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
126
         * al driver que cierre el fichero
127
         *
128
         * @throws DriverIOException
129
         */
130
        public void stop() throws DriverIOException {
131
                try {
132
                    if (reference_count == 0)
133
                    {
134
                        getFileDriver().close();
135
                    }
136
                    else
137
                        if (reference_count < 0)
138
                            throw new RuntimeException("Contador de referencias de driver =" 
139
                                    + reference_count + ". Demasiados stop().");
140
                    reference_count--;
141
                } catch (IOException e) {
142
                        throw new DriverIOException(e);
143
                }
144
        }
145

    
146
        /**
147
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
148
         */
149
        public IGeometry getShape(int index) throws DriverIOException {
150
                try {
151
                        return getFileDriver().getShape(index);
152
                } catch (IOException e) {
153
                        throw new DriverIOException(e);
154
                }
155
        }
156

    
157
        /**
158
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
159
         */
160
        public int getShapeCount() throws DriverIOException {
161
                try {
162
                        return getFileDriver().getShapeCount();
163
                } catch (IOException e) {
164
                        throw new DriverIOException(e);
165
                }
166
        }
167

    
168
        /**
169
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
170
         */
171
        public Rectangle2D getFullExtent() throws DriverIOException {
172
                try {
173
                        return (Rectangle2D)getFileDriver().getFullExtent().clone();
174
                } catch (IOException e) {
175
                        throw new DriverIOException(e);
176
                }
177
        }
178

    
179
        /**
180
         * @see com.iver.cit.gvsig.fmap.rendering.indexes.Index#getRecordIndexes(java.awt.geom.Rectangle2D)
181
         */
182
        public int[] getRecordIndexes(Rectangle2D rect)
183
                throws DriverIOException, IndexNotExistsException {
184
                //TODO implementar bien
185
                return null;
186
        }
187

    
188
        /**
189
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
190
         */
191
        public int getShapeType() throws DriverIOException {
192
                return getFileDriver().getShapeType();
193
        }
194

    
195
        /**
196
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
197
         */
198
        public DataSource getRecordset() throws DriverLoadException {
199
                try {
200
                        if (ds == null) {
201
                                VectorialFileDriver driver = (VectorialFileDriver) getDriver();
202

    
203
                                if (driver instanceof ExternalData) {
204
                                        ExternalData ed = (ExternalData) driver;
205
                                        File dataFile = ed.getDataFile(file);
206
                                        String driverName = ed.getDataDriverName();
207

    
208
                                        String name = LayerFactory.getDataSourceFactory().addFileDataSource(driverName,
209
                                                dataFile.getAbsolutePath(), file.getAbsolutePath(), null);
210
                                        ds = LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_MODE);
211
                                } else if (driver instanceof ObjectDriver) {
212
                                        String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver, null);
213
                                        ds = LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_MODE);
214
                                } else {
215
                                        return null;
216
                                }
217
                        }
218
                } catch (NoSuchTableException e) {
219
                        throw new RuntimeException(
220
                                "Error de implementaci?n, se ha a?adido una tabla y luego esa tabla no ha podido ser cargada");
221
                } catch (DriverException e) {
222
                        throw new RuntimeException(e);
223
                }
224

    
225
                return ds;
226
        }
227

    
228
        /**
229
         * Devuelve el fichero.
230
         *
231
         * @return Fichero.
232
         */
233
        public File getFile() {
234
                return file;
235
        }
236
}