Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / MemoryDriver.java @ 36188

History | View | Annotate | Download (9.41 KB)

1 695 fjp
/*
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 1100 fjp
/* 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 695 fjp
package com.iver.cit.gvsig.fmap.drivers;
48
49 1005 vcaballero
import java.awt.geom.Rectangle2D;
50 1773 fernando
import java.sql.Types;
51 1005 vcaballero
import java.util.ArrayList;
52 1661 fjp
import java.util.Date;
53 1005 vcaballero
54
import javax.swing.table.DefaultTableModel;
55
56 10627 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
57
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
58 2944 fjp
import com.hardcode.gdbms.engine.data.DataSourceFactory;
59 1828 fernando
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
60 10203 caballero
import com.hardcode.gdbms.engine.values.StringValue;
61 1653 fernando
import com.hardcode.gdbms.engine.values.Value;
62 24839 vcaballero
import com.iver.cit.gvsig.fmap.core.FNullGeometry;
63 1653 fernando
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 1005 vcaballero
68 1653 fernando
69 695 fjp
/**
70 1005 vcaballero
 * Clase abstracta para Driver en memoria.
71
 *
72 695 fjp
 * @author FJP
73
 */
74 3271 fjp
public abstract class MemoryDriver implements VectorialDriver, ObjectDriver,
75 1005 vcaballero
        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 10203 caballero
        private int[] fieldWidth=null;
82 10627 caballero
83 1005 vcaballero
        /**
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 5126 fjp
         * @param geom shape.
96 1005 vcaballero
         * @param row fila.
97
         */
98 5126 fjp
        public void addGeometry(IGeometry geom, Object[] row) {
99
                if (geom == null) {
100 1005 vcaballero
                        return; // No a?adimos nada
101
                }
102 24839 vcaballero
                if(! (geom instanceof FNullGeometry)){
103
                         Rectangle2D boundsShp = geom.getBounds2D();
104
                         memShapeInfo.addShapeInfo (boundsShp, geom.getGeometryType());
105
                         arrayGeometries.add(geom);
106
                        if (fullExtent == null) {
107 36188 jldominguez
                                fullExtent = (Rectangle2D) boundsShp.clone();
108 24839 vcaballero
                        } 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 1005 vcaballero
118 24839 vcaballero
                }
119 10203 caballero
                if (fieldWidth==null) {
120
                        initializeFieldWidth(row);
121
                }
122
                actualizeFieldWidth(row);
123 1005 vcaballero
                m_TableModel.addRow(row);
124
125
                try {
126 695 fjp
                        fullExtent = getFullExtent();
127 10627 caballero
                } catch (ReadDriverException e) {
128 695 fjp
                        e.printStackTrace();
129
                }
130
131 1005 vcaballero
                m_Position++;
132
        }
133
134 10203 caballero
135 1005 vcaballero
        /**
136 5126 fjp
         * 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 10203 caballero
147 5126 fjp
                addGeometry(geom, row);
148
        }
149
150
        /**
151 1005 vcaballero
         * Devuelve el extent a partir de un ?ndice.
152
         *
153
         * @param index ?ndice.
154
         *
155
         * @return Extent.
156
         */
157 10627 caballero
        public Rectangle2D getShapeBounds(int index) throws ReadDriverException {
158 695 fjp
                return memShapeInfo.getBoundingBox(index);
159
        }
160 1005 vcaballero
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 695 fjp
                return memShapeInfo.getType(index);
170
        }
171 1005 vcaballero
172
173 695 fjp
        /* (non-Javadoc)
174 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
175 695 fjp
         */
176 10627 caballero
        public IGeometry getShape(int index) throws ReadDriverException {
177 703 fjp
                IGeometry geom = (IGeometry) arrayGeometries.get(index);
178 1005 vcaballero
179 703 fjp
                return geom.cloneGeometry();
180 695 fjp
        }
181 1005 vcaballero
182 695 fjp
        /* (non-Javadoc)
183 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
184 695 fjp
         */
185 10627 caballero
        public int getShapeCount() throws ReadDriverException {
186 695 fjp
                return arrayGeometries.size();
187
        }
188 1005 vcaballero
189 695 fjp
        /* (non-Javadoc)
190 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
191 695 fjp
         */
192 10627 caballero
        public Rectangle2D getFullExtent() throws ReadDriverException {
193 695 fjp
                return fullExtent;
194
        }
195 1005 vcaballero
196
197 695 fjp
        /* (non-Javadoc)
198
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
199
         */
200 714 fjp
        public abstract int getShapeType();
201 1005 vcaballero
202 695 fjp
        /* (non-Javadoc)
203
         * @see com.hardcode.driverManager.Driver#getName()
204
         */
205
        public abstract String getName();
206 1005 vcaballero
207 1653 fernando
        /**
208
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
209 695 fjp
         */
210 10203 caballero
211 6520 azabala
/*
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 10203 caballero
 *
217 6520 azabala
 * 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 10203 caballero
 *
221
 *
222
 *
223
 *
224 6520 azabala
 */
225 10627 caballero
        public int getFieldType(int i) throws ReadDriverException {
226 1661 fjp
            // TODO: Revisar esto. Por ejemplo, el long
227 24382 vcaballero
            if (getRowCount() >= 1)
228 3247 fjp
        {
229
            Value val = getFieldValue(0,i);
230 35335 fpenarrubia
            if (val == null)
231
                    return Types.VARCHAR;
232 3272 fjp
            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 3247 fjp
        }
239
        else
240
        {
241 10203 caballero
            // TODO: ESTO CREO QUE NO TIENE SENTIDO. SIEMPRE DEVUELVE Object.class, lo dice en
242 3247 fjp
            // 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 3272 fjp
                if (m_TableModel.getColumnClass(i) == Double.class)
250 3247 fjp
                    return Types.INTEGER;
251 3272 fjp
                if (m_TableModel.getColumnClass(i) == Float.class)
252 3247 fjp
                    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 10203 caballero
        }
258 1828 fernando
            return Types.VARCHAR;
259 1661 fjp
            // return m_TableModel.getColumnClass(i);
260 1828 fernando
//            throw new DriverException("Tipo no soportado: " + m_TableModel.getColumnClass(i).getName());
261 695 fjp
        }
262
        /* (non-Javadoc)
263
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
264
         */
265 1005 vcaballero
        public Value getFieldValue(long rowIndex, int fieldId)
266 10627 caballero
                throws ReadDriverException {
267 695 fjp
                return (Value) m_TableModel.getValueAt((int) rowIndex, fieldId);
268
        }
269 1005 vcaballero
270 695 fjp
        /* (non-Javadoc)
271
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
272
         */
273 10627 caballero
        public int getFieldCount() throws ReadDriverException {
274 695 fjp
                return m_TableModel.getColumnCount();
275
        }
276 1005 vcaballero
277 695 fjp
        /* (non-Javadoc)
278
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
279
         */
280 10627 caballero
        public String getFieldName(int fieldId) throws ReadDriverException {
281 695 fjp
                return m_TableModel.getColumnName(fieldId);
282
        }
283 1005 vcaballero
284 695 fjp
        /* (non-Javadoc)
285
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
286
         */
287 10627 caballero
        public long getRowCount() throws ReadDriverException {
288 695 fjp
                return arrayGeometries.size();
289
        }
290 10203 caballero
291 2944 fjp
    /* (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 4937 fjp
    /* (non-Javadoc)
298
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
299
     */
300 10627 caballero
    public void reload() throws ReloadDriverException{
301 4937 fjp
                memShapeInfo = new MemoryShapeInfo();
302
                arrayGeometries.clear();
303
                m_TableModel= new DefaultTableModel();
304
                fullExtent = null;
305
                m_Position = 0;
306
307 3952 fjp
    }
308 10203 caballero
    private void initializeFieldWidth(Object[] row) {
309
            fieldWidth=new int[row.length];
310
                for (int i=0;i<row.length;i++) {
311 13046 caballero
                        if (row[i] instanceof StringValue && row[i]==null)
312
                                fieldWidth[i]=0;
313
                        else
314 31292 fpenarrubia
                        {
315
                                if (row[i] == null)
316
                                        fieldWidth[i] = 0;
317
                                else
318
                                        fieldWidth[i]=((Value)row[i]).getWidth();
319
                        }
320 10203 caballero
                }
321 4863 fjp
    }
322 10203 caballero
    /**
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 13046 caballero
                                if (row[i]!=null) {
330
                                        int width=((StringValue)row[i]).getWidth();
331
                                        if (fieldWidth[i]<width) {
332
                                                fieldWidth[i]=width;
333
                                        }
334 10203 caballero
                                }
335
                        }
336
                }
337
    }
338
    public int getFieldWidth(int fieldId){
339 10343 caballero
            if (fieldWidth==null)
340
                    return 1;
341 10203 caballero
            return fieldWidth[fieldId];
342
    }
343 6323 fjp
344 10203 caballero
345 695 fjp
}