root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / MemoryDriver.java @ 36188
History | View | Annotate | Download (9.41 KB)
1 |
/*
|
---|---|
2 |
* Created on 27-dic-2004
|
3 |
*
|
4 |
* TODO To change the template for this generated file go to
|
5 |
* Window - Preferences - Java - Code Generation - Code and Comments
|
6 |
*/
|
7 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
8 |
*
|
9 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
10 |
*
|
11 |
* This program is free software; you can redistribute it and/or
|
12 |
* modify it under the terms of the GNU General Public License
|
13 |
* as published by the Free Software Foundation; either version 2
|
14 |
* of the License, or (at your option) any later version.
|
15 |
*
|
16 |
* This program is distributed in the hope that it will be useful,
|
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 |
* GNU General Public License for more details.
|
20 |
*
|
21 |
* You should have received a copy of the GNU General Public License
|
22 |
* along with this program; if not, write to the Free Software
|
23 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
24 |
*
|
25 |
* For more information, contact:
|
26 |
*
|
27 |
* Generalitat Valenciana
|
28 |
* Conselleria d'Infraestructures i Transport
|
29 |
* Av. Blasco Ib??ez, 50
|
30 |
* 46010 VALENCIA
|
31 |
* SPAIN
|
32 |
*
|
33 |
* +34 963862235
|
34 |
* gvsig@gva.es
|
35 |
* www.gvsig.gva.es
|
36 |
*
|
37 |
* or
|
38 |
*
|
39 |
* IVER T.I. S.A
|
40 |
* Salamanca 50
|
41 |
* 46005 Valencia
|
42 |
* Spain
|
43 |
*
|
44 |
* +34 963163400
|
45 |
* dac@iver.es
|
46 |
*/
|
47 |
package com.iver.cit.gvsig.fmap.drivers; |
48 |
|
49 |
import java.awt.geom.Rectangle2D; |
50 |
import java.sql.Types; |
51 |
import java.util.ArrayList; |
52 |
import java.util.Date; |
53 |
|
54 |
import javax.swing.table.DefaultTableModel; |
55 |
|
56 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
57 |
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException; |
58 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
59 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
60 |
import com.hardcode.gdbms.engine.values.StringValue; |
61 |
import com.hardcode.gdbms.engine.values.Value; |
62 |
import com.iver.cit.gvsig.fmap.core.FNullGeometry; |
63 |
import com.iver.cit.gvsig.fmap.core.FShape; |
64 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
65 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
66 |
import com.iver.cit.gvsig.fmap.operations.strategies.MemoryShapeInfo; |
67 |
|
68 |
|
69 |
/**
|
70 |
* Clase abstracta para Driver en memoria.
|
71 |
*
|
72 |
* @author FJP
|
73 |
*/
|
74 |
public abstract class MemoryDriver implements VectorialDriver, ObjectDriver, |
75 |
BoundedShapes { |
76 |
private MemoryShapeInfo memShapeInfo = new MemoryShapeInfo(); |
77 |
private ArrayList arrayGeometries = new ArrayList(); |
78 |
private Rectangle2D fullExtent; |
79 |
private int m_Position; |
80 |
private DefaultTableModel m_TableModel = new DefaultTableModel(); |
81 |
private int[] fieldWidth=null; |
82 |
|
83 |
/**
|
84 |
* Devuelve el modelo de la tabla.
|
85 |
*
|
86 |
* @return modelo de la tabla.
|
87 |
*/
|
88 |
public DefaultTableModel getTableModel() { |
89 |
return m_TableModel;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* A?ade un shape.
|
94 |
*
|
95 |
* @param geom shape.
|
96 |
* @param row fila.
|
97 |
*/
|
98 |
public void addGeometry(IGeometry geom, Object[] row) { |
99 |
if (geom == null) { |
100 |
return; // No a?adimos nada |
101 |
} |
102 |
if(! (geom instanceof FNullGeometry)){ |
103 |
Rectangle2D boundsShp = geom.getBounds2D();
|
104 |
memShapeInfo.addShapeInfo (boundsShp, geom.getGeometryType()); |
105 |
arrayGeometries.add(geom); |
106 |
if (fullExtent == null) { |
107 |
fullExtent = (Rectangle2D) boundsShp.clone();
|
108 |
} else {
|
109 |
fullExtent.add(boundsShp); |
110 |
} |
111 |
} |
112 |
else
|
113 |
{ |
114 |
Rectangle2D boundsShp = new Rectangle2D.Double(); |
115 |
memShapeInfo.addShapeInfo (boundsShp, geom.getGeometryType()); |
116 |
arrayGeometries.add(geom); |
117 |
|
118 |
} |
119 |
if (fieldWidth==null) { |
120 |
initializeFieldWidth(row); |
121 |
} |
122 |
actualizeFieldWidth(row); |
123 |
m_TableModel.addRow(row); |
124 |
|
125 |
try {
|
126 |
fullExtent = getFullExtent(); |
127 |
} catch (ReadDriverException e) {
|
128 |
e.printStackTrace(); |
129 |
} |
130 |
|
131 |
m_Position++; |
132 |
} |
133 |
|
134 |
|
135 |
/**
|
136 |
* M?todo de conveniencia, para poder a?adir directamente un shape
|
137 |
* o una IGeometry. (Arriba est? el de a?adir una IGeometry.
|
138 |
* @param shp
|
139 |
* @param row
|
140 |
*/
|
141 |
public void addShape(FShape shp, Object[] row) { |
142 |
if (shp == null) { |
143 |
return; // No a?adimos nada |
144 |
} |
145 |
IGeometry geom = ShapeFactory.createGeometry(shp); |
146 |
|
147 |
addGeometry(geom, row); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Devuelve el extent a partir de un ?ndice.
|
152 |
*
|
153 |
* @param index ?ndice.
|
154 |
*
|
155 |
* @return Extent.
|
156 |
*/
|
157 |
public Rectangle2D getShapeBounds(int index) throws ReadDriverException { |
158 |
return memShapeInfo.getBoundingBox(index);
|
159 |
} |
160 |
|
161 |
/**
|
162 |
* Devuelve el tipo del shape.
|
163 |
*
|
164 |
* @param index ?ndice.
|
165 |
*
|
166 |
* @return tipo del shape.
|
167 |
*/
|
168 |
public int getShapeType(int index) { |
169 |
return memShapeInfo.getType(index);
|
170 |
} |
171 |
|
172 |
|
173 |
/* (non-Javadoc)
|
174 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
|
175 |
*/
|
176 |
public IGeometry getShape(int index) throws ReadDriverException { |
177 |
IGeometry geom = (IGeometry) arrayGeometries.get(index); |
178 |
|
179 |
return geom.cloneGeometry();
|
180 |
} |
181 |
|
182 |
/* (non-Javadoc)
|
183 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
|
184 |
*/
|
185 |
public int getShapeCount() throws ReadDriverException { |
186 |
return arrayGeometries.size();
|
187 |
} |
188 |
|
189 |
/* (non-Javadoc)
|
190 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
|
191 |
*/
|
192 |
public Rectangle2D getFullExtent() throws ReadDriverException { |
193 |
return fullExtent;
|
194 |
} |
195 |
|
196 |
|
197 |
/* (non-Javadoc)
|
198 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
|
199 |
*/
|
200 |
public abstract int getShapeType(); |
201 |
|
202 |
/* (non-Javadoc)
|
203 |
* @see com.hardcode.driverManager.Driver#getName()
|
204 |
*/
|
205 |
public abstract String getName(); |
206 |
|
207 |
/**
|
208 |
* @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
|
209 |
*/
|
210 |
|
211 |
/*
|
212 |
* azabala, bug 666
|
213 |
* Habra que estudiar como hacer, porque si una fila tiene el primer valor
|
214 |
* (o todos) a NullValue, devolvera como tipo de dato Types.NULL.
|
215 |
* Quizas, habra que hacer que los MemoryDriver tb tengan un ITableDefinition.
|
216 |
*
|
217 |
* DxfMemoryDriver no obstante si que tiene informacion sobre el esquema, asi
|
218 |
* que debera sobreescribir este metodo para salvar el bug
|
219 |
* (metodo getTableDefinition)
|
220 |
*
|
221 |
*
|
222 |
*
|
223 |
*
|
224 |
*/
|
225 |
public int getFieldType(int i) throws ReadDriverException { |
226 |
// TODO: Revisar esto. Por ejemplo, el long
|
227 |
if (getRowCount() >= 1) |
228 |
{ |
229 |
Value val = getFieldValue(0,i);
|
230 |
if (val == null) |
231 |
return Types.VARCHAR; |
232 |
if (val.getSQLType() == Types.INTEGER) |
233 |
// Sabemos que es num?rico, pero no sabemos
|
234 |
// si luego habr? otra cosa.
|
235 |
return Types.FLOAT; |
236 |
else
|
237 |
return val.getSQLType();
|
238 |
} |
239 |
else
|
240 |
{ |
241 |
// TODO: ESTO CREO QUE NO TIENE SENTIDO. SIEMPRE DEVUELVE Object.class, lo dice en
|
242 |
// la documentaci?n. Creo que habr?a que quitarlo.
|
243 |
if (m_TableModel.getColumnClass(i) == String.class) |
244 |
return Types.VARCHAR; |
245 |
if (m_TableModel.getColumnClass(i) == Float.class) |
246 |
return Types.FLOAT; |
247 |
if (m_TableModel.getColumnClass(i) == Double.class) |
248 |
return Types.DOUBLE; |
249 |
if (m_TableModel.getColumnClass(i) == Double.class) |
250 |
return Types.INTEGER; |
251 |
if (m_TableModel.getColumnClass(i) == Float.class) |
252 |
return Types.INTEGER; |
253 |
if (m_TableModel.getColumnClass(i) == Boolean.class) |
254 |
return Types.BIT; |
255 |
if (m_TableModel.getColumnClass(i) == Date.class) |
256 |
return Types.DATE; |
257 |
} |
258 |
return Types.VARCHAR; |
259 |
// return m_TableModel.getColumnClass(i);
|
260 |
// throw new DriverException("Tipo no soportado: " + m_TableModel.getColumnClass(i).getName());
|
261 |
} |
262 |
/* (non-Javadoc)
|
263 |
* @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
|
264 |
*/
|
265 |
public Value getFieldValue(long rowIndex, int fieldId) |
266 |
throws ReadDriverException {
|
267 |
return (Value) m_TableModel.getValueAt((int) rowIndex, fieldId); |
268 |
} |
269 |
|
270 |
/* (non-Javadoc)
|
271 |
* @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
|
272 |
*/
|
273 |
public int getFieldCount() throws ReadDriverException { |
274 |
return m_TableModel.getColumnCount();
|
275 |
} |
276 |
|
277 |
/* (non-Javadoc)
|
278 |
* @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
|
279 |
*/
|
280 |
public String getFieldName(int fieldId) throws ReadDriverException { |
281 |
return m_TableModel.getColumnName(fieldId);
|
282 |
} |
283 |
|
284 |
/* (non-Javadoc)
|
285 |
* @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
|
286 |
*/
|
287 |
public long getRowCount() throws ReadDriverException { |
288 |
return arrayGeometries.size();
|
289 |
} |
290 |
|
291 |
/* (non-Javadoc)
|
292 |
* @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
|
293 |
*/
|
294 |
public void setDataSourceFactory(DataSourceFactory dsf) { |
295 |
} |
296 |
|
297 |
/* (non-Javadoc)
|
298 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
|
299 |
*/
|
300 |
public void reload() throws ReloadDriverException{ |
301 |
memShapeInfo = new MemoryShapeInfo();
|
302 |
arrayGeometries.clear(); |
303 |
m_TableModel= new DefaultTableModel(); |
304 |
fullExtent = null;
|
305 |
m_Position = 0;
|
306 |
|
307 |
} |
308 |
private void initializeFieldWidth(Object[] row) { |
309 |
fieldWidth=new int[row.length]; |
310 |
for (int i=0;i<row.length;i++) { |
311 |
if (row[i] instanceof StringValue && row[i]==null) |
312 |
fieldWidth[i]=0;
|
313 |
else
|
314 |
{ |
315 |
if (row[i] == null) |
316 |
fieldWidth[i] = 0;
|
317 |
else
|
318 |
fieldWidth[i]=((Value)row[i]).getWidth(); |
319 |
} |
320 |
} |
321 |
} |
322 |
/**
|
323 |
* Actualize the width fields with StringValue.
|
324 |
* @param row
|
325 |
*/
|
326 |
private void actualizeFieldWidth(Object[] row) { |
327 |
for (int i=0;i<row.length;i++) { |
328 |
if (row[i] instanceof StringValue) { |
329 |
if (row[i]!=null) { |
330 |
int width=((StringValue)row[i]).getWidth();
|
331 |
if (fieldWidth[i]<width) {
|
332 |
fieldWidth[i]=width; |
333 |
} |
334 |
} |
335 |
} |
336 |
} |
337 |
} |
338 |
public int getFieldWidth(int fieldId){ |
339 |
if (fieldWidth==null) |
340 |
return 1; |
341 |
return fieldWidth[fieldId];
|
342 |
} |
343 |
|
344 |
|
345 |
} |