Statistics
| Revision:

svn-gvsig-desktop / branches / FMap_piloto_CAD_Layout_version / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 1763

History | View | Annotate | Download (6.93 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
                } catch (NullPointerException e) {
174
                        return new Rectangle2D.Double(0,0,500,500);
175
                }
176
        }
177

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

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

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

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

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

    
224
                return ds;
225
        }
226

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

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

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

    
255
                dataSourceName = newName;
256
        }
257
}