Statistics
| Revision:

svn-gvsig-desktop / tags / Root_FMap_piloto_CAD_Layout_version / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 1664

History | View | Annotate | Download (6.84 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.FileDriver;
51
import com.hardcode.gdbms.engine.data.NoSuchTableException;
52
import com.iver.cit.gvsig.fmap.DriverException;
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.IndexNotExistsException;
58

    
59

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

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

    
87
        /**
88
         * Devuelve driver.
89
         *
90
         * @return VectorialFileDriver.
91
         */
92
        private VectorialFileDriver getFileDriver() {
93
                return (VectorialFileDriver) getDriver();
94
        }
95

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

    
116
                } catch (IOException e) {
117
                        throw new DriverIOException(e);
118
                }
119
        }
120

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

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

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

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

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

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

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

    
200
                                if (driver instanceof ExternalData) {
201
                                        ExternalData ed = (ExternalData) driver;
202
                                        File dataFile = ed.getDataFile(file);
203
                                        String driverName = ed.getDataDriverName();
204

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

    
222
                return ds;
223
        }
224

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

    
234
        /**
235
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#changeRecordsetName(java.lang.String)
236
         */
237
        public void changeRecordsetName(String newName) throws DriverException {
238
                try {
239
                        if (ds == null) {
240
                                try {
241
                                        ds = getRecordset(newName);
242
                                } catch (DriverLoadException e1) {
243
                                        throw new DriverException(e1);
244
                                }
245
                        }
246

    
247
                        DataSourceFactory.changeDataSourceName(dataSourceName, newName);
248
                } catch (NoSuchTableException e) {
249
                        throw new RuntimeException(
250
                                "No existe la tabla que hemos creado????");
251
                }
252

    
253
                dataSourceName = newName;
254
        }
255
}