svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 1100
History | View | Annotate | Download (6.32 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 |
|
50 |
import com.iver.cit.gvsig.fmap.DriverException; |
51 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
52 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
53 |
import com.iver.cit.gvsig.fmap.drivers.ExternalData; |
54 |
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver; |
55 |
import com.iver.cit.gvsig.fmap.rendering.indexes.IndexNotExistsException; |
56 |
import com.iver.cit.gvsig.fmap.write.FileWriterDriver; |
57 |
|
58 |
import java.awt.geom.Rectangle2D; |
59 |
|
60 |
import java.io.File; |
61 |
import java.io.IOException; |
62 |
|
63 |
|
64 |
/**
|
65 |
* Adapta un driver de fichero vectorial a la interfaz vectorial, manteniendo
|
66 |
* adem?s el estado necesario por una capa vectorial de fichero (el nombre del
|
67 |
* fichero)
|
68 |
*/
|
69 |
public class VectorialFileAdapter extends VectorialAdapter { |
70 |
private boolean driverInitialized = false; |
71 |
private File file; |
72 |
private FileWriterDriver writeDriver;
|
73 |
private DataSource ds; |
74 |
private String dataSourceName; |
75 |
|
76 |
/**
|
77 |
* Crea un nuevo VectorialFileAdapter.
|
78 |
*
|
79 |
* @param file Fichero.
|
80 |
*/
|
81 |
public VectorialFileAdapter(File file) { |
82 |
this.file = file;
|
83 |
} |
84 |
|
85 |
/**
|
86 |
* Devuelve driver.
|
87 |
*
|
88 |
* @return VectorialFileDriver.
|
89 |
*/
|
90 |
private VectorialFileDriver getFileDriver() {
|
91 |
return (VectorialFileDriver) getDriver();
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* incrementa el contador de las veces que se ha abierto el fichero.
|
96 |
* Solamente cuando el contador est? a cero pide al driver que abra el
|
97 |
* fichero
|
98 |
*
|
99 |
* @throws DriverIOException
|
100 |
*/
|
101 |
public void start() throws DriverIOException { |
102 |
try {
|
103 |
getFileDriver().open(file); |
104 |
|
105 |
if (!driverInitialized) {
|
106 |
getFileDriver().initialize(); |
107 |
driverInitialized = true;
|
108 |
} |
109 |
} catch (IOException e) { |
110 |
throw new DriverIOException(e); |
111 |
} |
112 |
} |
113 |
|
114 |
/**
|
115 |
* decrementa el contador de n?mero de aperturas y cuando llega a cero pide
|
116 |
* al driver que cierre el fichero
|
117 |
*
|
118 |
* @throws DriverIOException
|
119 |
*/
|
120 |
public void stop() throws DriverIOException { |
121 |
try {
|
122 |
getFileDriver().close(); |
123 |
} catch (IOException e) { |
124 |
throw new DriverIOException(e); |
125 |
} |
126 |
} |
127 |
|
128 |
/**
|
129 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
130 |
*/
|
131 |
public IGeometry getShape(int index) throws DriverIOException { |
132 |
try {
|
133 |
return getFileDriver().getShape(index);
|
134 |
} catch (IOException e) { |
135 |
throw new DriverIOException(e); |
136 |
} |
137 |
} |
138 |
|
139 |
/**
|
140 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
|
141 |
*/
|
142 |
public int getShapeCount() throws DriverIOException { |
143 |
try {
|
144 |
return getFileDriver().getShapeCount();
|
145 |
} catch (IOException e) { |
146 |
throw new DriverIOException(e); |
147 |
} |
148 |
} |
149 |
|
150 |
/**
|
151 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
152 |
*/
|
153 |
public Rectangle2D getFullExtent() throws DriverIOException { |
154 |
try {
|
155 |
return getFileDriver().getFullExtent();
|
156 |
} catch (IOException e) { |
157 |
throw new DriverIOException(e); |
158 |
} |
159 |
} |
160 |
|
161 |
/**
|
162 |
* @see com.iver.cit.gvsig.fmap.rendering.indexes.Index#getRecordIndexes(java.awt.geom.Rectangle2D)
|
163 |
*/
|
164 |
public int[] getRecordIndexes(Rectangle2D rect) |
165 |
throws DriverIOException, IndexNotExistsException {
|
166 |
//TODO implementar bien
|
167 |
return null; |
168 |
} |
169 |
|
170 |
/**
|
171 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
172 |
*/
|
173 |
public int getShapeType() throws DriverIOException { |
174 |
return getFileDriver().getShapeType();
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
179 |
*/
|
180 |
public DataSource getRecordset(String name) throws DriverLoadException { |
181 |
try {
|
182 |
if (ds == null) { |
183 |
VectorialFileDriver driver = (VectorialFileDriver) getDriver(); |
184 |
|
185 |
if (driver instanceof ExternalData) { |
186 |
ExternalData ed = (ExternalData) driver; |
187 |
File dataFile = ed.getDataFile(file);
|
188 |
String driverName = ed.getDataDriverName();
|
189 |
|
190 |
DataSourceFactory.addFileDataSource(driverName, name, |
191 |
dataFile.getAbsolutePath(), file.getAbsolutePath()); |
192 |
ds = DataSourceFactory.createRandomDataSource(name); |
193 |
} else if (driver instanceof FileDriver) { |
194 |
DataSourceFactory.addFileDataSource(name, |
195 |
file.getAbsolutePath(), file.getAbsolutePath(), |
196 |
(FileDriver) getDriver()); |
197 |
ds = DataSourceFactory.createRandomDataSource(name); |
198 |
} else {
|
199 |
return null; |
200 |
} |
201 |
} |
202 |
} catch (NoSuchTableException e) {
|
203 |
throw new RuntimeException( |
204 |
"Error de implementaci?n, se ha a?adido una tabla y luego esa tabla no ha podido ser cargada");
|
205 |
} |
206 |
|
207 |
return ds;
|
208 |
} |
209 |
|
210 |
/**
|
211 |
* Devuelve el fichero.
|
212 |
*
|
213 |
* @return Fichero.
|
214 |
*/
|
215 |
public File getFile() { |
216 |
return file;
|
217 |
} |
218 |
|
219 |
/**
|
220 |
* @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#changeRecordsetName(java.lang.String)
|
221 |
*/
|
222 |
public void changeRecordsetName(String newName) throws DriverException { |
223 |
try {
|
224 |
if (ds == null) { |
225 |
try {
|
226 |
ds = getRecordset(newName); |
227 |
} catch (DriverLoadException e1) {
|
228 |
throw new DriverException(e1); |
229 |
} |
230 |
} |
231 |
|
232 |
DataSourceFactory.changeDataSourceName(dataSourceName, newName); |
233 |
} catch (NoSuchTableException e) {
|
234 |
throw new RuntimeException( |
235 |
"No existe la tabla que hemos creado????");
|
236 |
} |
237 |
|
238 |
dataSourceName = newName; |
239 |
} |
240 |
} |