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 | 1100 | fjp | /* 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 | 214 | fernando | package com.iver.cit.gvsig.fmap.layers; |
42 | |||
43 | 1369 | fernando | import java.awt.geom.Rectangle2D; |
44 | import java.io.File; |
||
45 | import java.io.IOException; |
||
46 | 442 | vcaballero | |
47 | 1369 | fernando | import com.hardcode.driverManager.DriverLoadException; |
48 | 408 | fernando | import com.hardcode.gdbms.engine.data.DataSource; |
49 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
50 | 504 | fernando | import com.hardcode.gdbms.engine.data.FileDriver; |
51 | 408 | fernando | import com.hardcode.gdbms.engine.data.NoSuchTableException; |
52 | 651 | fernando | import com.iver.cit.gvsig.fmap.DriverException; |
53 | 305 | fjp | import com.iver.cit.gvsig.fmap.core.IGeometry; |
54 | 214 | fernando | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
55 | 504 | fernando | import com.iver.cit.gvsig.fmap.drivers.ExternalData; |
56 | 214 | fernando | import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
57 | import com.iver.cit.gvsig.fmap.rendering.indexes.IndexNotExistsException; |
||
58 | 229 | vcaballero | |
59 | |||
60 | 214 | fernando | /**
|
61 | 229 | vcaballero | * 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 | 214 | fernando | */
|
65 | public class VectorialFileAdapter extends VectorialAdapter { |
||
66 | 504 | fernando | private boolean driverInitialized = false; |
67 | private File file; |
||
68 | 546 | fernando | private DataSource ds; |
69 | private String dataSourceName; |
||
70 | 1327 | fjp | |
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 | 229 | vcaballero | |
78 | 504 | fernando | /**
|
79 | * Crea un nuevo VectorialFileAdapter.
|
||
80 | *
|
||
81 | 1034 | vcaballero | * @param file Fichero.
|
82 | 504 | fernando | */
|
83 | public VectorialFileAdapter(File file) { |
||
84 | this.file = file;
|
||
85 | } |
||
86 | 214 | fernando | |
87 | 504 | fernando | /**
|
88 | 1034 | vcaballero | * Devuelve driver.
|
89 | 504 | fernando | *
|
90 | 1034 | vcaballero | * @return VectorialFileDriver.
|
91 | 504 | fernando | */
|
92 | private VectorialFileDriver getFileDriver() {
|
||
93 | return (VectorialFileDriver) getDriver();
|
||
94 | } |
||
95 | 229 | vcaballero | |
96 | 504 | fernando | /**
|
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 | 1327 | fjp | 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 | 229 | vcaballero | |
116 | 504 | fernando | } catch (IOException e) { |
117 | throw new DriverIOException(e); |
||
118 | } |
||
119 | } |
||
120 | 214 | fernando | |
121 | 504 | fernando | /**
|
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 | 1327 | fjp | 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 | 504 | fernando | } catch (IOException e) { |
139 | throw new DriverIOException(e); |
||
140 | } |
||
141 | } |
||
142 | 214 | fernando | |
143 | 504 | fernando | /**
|
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 | 214 | fernando | |
154 | 504 | fernando | /**
|
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 | 214 | fernando | |
165 | 504 | fernando | /**
|
166 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
||
167 | */
|
||
168 | public Rectangle2D getFullExtent() throws DriverIOException { |
||
169 | try {
|
||
170 | 1331 | vcaballero | return (Rectangle2D)getFileDriver().getFullExtent().clone(); |
171 | 504 | fernando | } catch (IOException e) { |
172 | throw new DriverIOException(e); |
||
173 | } |
||
174 | } |
||
175 | 214 | fernando | |
176 | 504 | fernando | /**
|
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 | 369 | fernando | |
185 | 504 | fernando | /**
|
186 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
||
187 | */
|
||
188 | public int getShapeType() throws DriverIOException { |
||
189 | return getFileDriver().getShapeType();
|
||
190 | } |
||
191 | 408 | fernando | |
192 | 504 | fernando | /**
|
193 | * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
||
194 | */
|
||
195 | public DataSource getRecordset(String name) throws DriverLoadException { |
||
196 | 508 | fernando | try {
|
197 | 1034 | vcaballero | if (ds == null) { |
198 | VectorialFileDriver driver = (VectorialFileDriver) getDriver(); |
||
199 | 442 | vcaballero | |
200 | 1034 | vcaballero | if (driver instanceof ExternalData) { |
201 | ExternalData ed = (ExternalData) driver; |
||
202 | File dataFile = ed.getDataFile(file);
|
||
203 | String driverName = ed.getDataDriverName();
|
||
204 | 442 | vcaballero | |
205 | 1034 | vcaballero | 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 | 463 | fernando | } |
217 | 504 | fernando | } 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 | 508 | fernando | return ds;
|
223 | 504 | fernando | } |
224 | 442 | vcaballero | |
225 | 504 | fernando | /**
|
226 | 1034 | vcaballero | * Devuelve el fichero.
|
227 | 504 | fernando | *
|
228 | 1034 | vcaballero | * @return Fichero.
|
229 | 504 | fernando | */
|
230 | public File getFile() { |
||
231 | return file;
|
||
232 | } |
||
233 | 546 | fernando | |
234 | /**
|
||
235 | * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#changeRecordsetName(java.lang.String)
|
||
236 | */
|
||
237 | 651 | fernando | public void changeRecordsetName(String newName) throws DriverException { |
238 | 546 | fernando | try {
|
239 | 1034 | vcaballero | if (ds == null) { |
240 | 651 | fernando | try {
|
241 | ds = getRecordset(newName); |
||
242 | } catch (DriverLoadException e1) {
|
||
243 | throw new DriverException(e1); |
||
244 | } |
||
245 | 1034 | vcaballero | } |
246 | |||
247 | 546 | fernando | DataSourceFactory.changeDataSourceName(dataSourceName, newName); |
248 | } catch (NoSuchTableException e) {
|
||
249 | 1034 | vcaballero | throw new RuntimeException( |
250 | "No existe la tabla que hemos creado????");
|
||
251 | 546 | fernando | } |
252 | 1034 | vcaballero | |
253 | 546 | fernando | dataSourceName = newName; |
254 | } |
||
255 | 214 | fernando | } |