Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 1773

History | View | Annotate | Download (6.52 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 com.hardcode.driverManager.DriverLoadException;
44

    
45
import com.hardcode.gdbms.engine.data.DataSource;
46
import com.hardcode.gdbms.engine.data.DataSourceFactory;
47
import com.hardcode.gdbms.engine.data.FileDriver;
48
import com.hardcode.gdbms.engine.data.NoSuchTableException;
49
import com.hardcode.gdbms.engine.data.ReadDriver;
50

    
51
import com.iver.cit.gvsig.fmap.DriverException;
52
import com.iver.cit.gvsig.fmap.core.IGeometry;
53
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
54
import com.iver.cit.gvsig.fmap.drivers.ExternalData;
55
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
56
import com.iver.cit.gvsig.fmap.rendering.indexes.Index;
57
import com.iver.cit.gvsig.fmap.rendering.indexes.IndexNotExistsException;
58
import com.iver.cit.gvsig.fmap.write.FileWriterDriver;
59

    
60
import java.awt.geom.Rectangle2D;
61

    
62
import java.io.File;
63
import java.io.IOException;
64

    
65

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

    
85
        /**
86
         * Crea un nuevo VectorialFileAdapter.
87
         *
88
         * @param file Fichero.
89
         */
90
        public VectorialFileAdapter(File file) {
91
                this.file = file;
92
        }
93

    
94
        /**
95
         * Devuelve driver.
96
         *
97
         * @return VectorialFileDriver.
98
         */
99
        private VectorialFileDriver getFileDriver() {
100
                return (VectorialFileDriver) getDriver();
101
        }
102

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

    
123
                } catch (IOException e) {
124
                        throw new DriverIOException(e);
125
                }
126
        }
127

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

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

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

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

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

    
192
        /**
193
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
194
         */
195
        public int getShapeType() throws DriverIOException {
196
                return getFileDriver().getShapeType();
197
        }
198

    
199
        /**
200
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
201
         */
202
        public DataSource getRecordset(String name) throws DriverLoadException {
203
                try {
204
                        if (ds == null) {
205
                                VectorialFileDriver driver = (VectorialFileDriver) getDriver();
206

    
207
                                if (driver instanceof ExternalData) {
208
                                        ExternalData ed = (ExternalData) driver;
209
                                        File dataFile = ed.getDataFile(file);
210
                                        String driverName = ed.getDataDriverName();
211

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

    
227
                return ds;
228
        }
229

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