Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1013 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / MemoryDriver.java @ 13521

History | View | Annotate | Download (9.32 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
import java.io.IOException;
51 1773 fernando
import java.sql.Types;
52 1005 vcaballero
import java.util.ArrayList;
53 1661 fjp
import java.util.Date;
54 1005 vcaballero
55
import javax.swing.table.DefaultTableModel;
56
57 2944 fjp
import com.hardcode.gdbms.engine.data.DataSourceFactory;
58 1828 fernando
import com.hardcode.gdbms.engine.data.driver.DriverException;
59
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
60 10235 caballero
import com.hardcode.gdbms.engine.values.StringValue;
61 1653 fernando
import com.hardcode.gdbms.engine.values.Value;
62 12579 fjp
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 10235 caballero
        private int[] fieldWidth=null;
82 1005 vcaballero
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 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 12579 fjp
                if(! (geom instanceof FNullGeometry)){
103
                         Rectangle2D boundsShp = geom.getBounds();
104
                         memShapeInfo.addShapeInfo (boundsShp, geom.getGeometryType());
105
                         arrayGeometries.add(geom);
106
                        if (fullExtent == null) {
107
                                fullExtent = boundsShp;
108
                        } else {
109
                                fullExtent.add(boundsShp);
110 13046 caballero
                        }
111 12579 fjp
                }
112
                else
113
                {
114
                         Rectangle2D boundsShp = new Rectangle2D.Double();
115
                         memShapeInfo.addShapeInfo (boundsShp, geom.getGeometryType());
116
                         arrayGeometries.add(geom);
117 13046 caballero
118 12579 fjp
                }
119
//                Rectangle2D boundsShp = geom.getBounds();
120
//                memShapeInfo.addShapeInfo(boundsShp, geom.getGeometryType());
121
//                arrayGeometries.add(geom);
122 10235 caballero
                if (fieldWidth==null) {
123
                        initializeFieldWidth(row);
124
                }
125
                actualizeFieldWidth(row);
126 1005 vcaballero
                m_TableModel.addRow(row);
127
128
                try {
129 695 fjp
                        fullExtent = getFullExtent();
130
                } catch (IOException e) {
131
                        e.printStackTrace();
132
                }
133
134 1005 vcaballero
135
                m_Position++;
136
        }
137
138 10235 caballero
139 1005 vcaballero
        /**
140 5126 fjp
         * M?todo de conveniencia, para poder a?adir directamente un shape
141
         * o una IGeometry. (Arriba est? el de a?adir una IGeometry.
142
         * @param shp
143
         * @param row
144
         */
145
        public void addShape(FShape shp, Object[] row) {
146
                if (shp == null) {
147
                        return; // No a?adimos nada
148
                }
149
                IGeometry geom = ShapeFactory.createGeometry(shp);
150 10235 caballero
151 5126 fjp
                addGeometry(geom, row);
152
        }
153
154
        /**
155 1005 vcaballero
         * Devuelve el extent a partir de un ?ndice.
156
         *
157
         * @param index ?ndice.
158
         *
159
         * @return Extent.
160
         *
161
         * @throws IOException
162
         */
163
        public Rectangle2D getShapeBounds(int index) throws IOException {
164 695 fjp
                return memShapeInfo.getBoundingBox(index);
165
        }
166 1005 vcaballero
167
        /**
168
         * Devuelve el tipo del shape.
169
         *
170
         * @param index ?ndice.
171
         *
172
         * @return tipo del shape.
173
         */
174
        public int getShapeType(int index) {
175 695 fjp
                return memShapeInfo.getType(index);
176
        }
177 1005 vcaballero
178
179 695 fjp
        /* (non-Javadoc)
180 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
181 695 fjp
         */
182 3271 fjp
        public IGeometry getShape(int index) {
183 703 fjp
                IGeometry geom = (IGeometry) arrayGeometries.get(index);
184 1005 vcaballero
185 703 fjp
                return geom.cloneGeometry();
186 695 fjp
        }
187 1005 vcaballero
188 695 fjp
        /* (non-Javadoc)
189 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
190 695 fjp
         */
191
        public int getShapeCount() throws IOException {
192
                return arrayGeometries.size();
193
        }
194 1005 vcaballero
195 695 fjp
        /* (non-Javadoc)
196 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
197 695 fjp
         */
198
        public Rectangle2D getFullExtent() throws IOException {
199
                return fullExtent;
200
        }
201 1005 vcaballero
202
203 695 fjp
        /* (non-Javadoc)
204
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
205
         */
206 714 fjp
        public abstract int getShapeType();
207 1005 vcaballero
208 695 fjp
        /* (non-Javadoc)
209
         * @see com.hardcode.driverManager.Driver#getName()
210
         */
211
        public abstract String getName();
212 1005 vcaballero
213 1653 fernando
        /**
214
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
215 695 fjp
         */
216 10235 caballero
217 6520 azabala
/*
218
 * azabala, bug 666
219
 * Habra que estudiar como hacer, porque si una fila tiene el primer valor
220
 * (o todos) a NullValue, devolvera como tipo de dato Types.NULL.
221
 * Quizas, habra que hacer que los MemoryDriver tb tengan un ITableDefinition.
222 10235 caballero
 *
223 6520 azabala
 * DxfMemoryDriver no obstante si que tiene informacion sobre el esquema, asi
224
 * que debera sobreescribir este metodo para salvar el bug
225
 * (metodo getTableDefinition)
226 10235 caballero
 *
227
 *
228
 *
229
 *
230 6520 azabala
 */
231 1773 fernando
        public int getFieldType(int i) throws DriverException {
232 1661 fjp
            // TODO: Revisar esto. Por ejemplo, el long
233 3247 fjp
            if (getRowCount() > 1)
234
        {
235
            Value val = getFieldValue(0,i);
236 3272 fjp
            if (val.getSQLType() == Types.INTEGER)
237
                // Sabemos que es num?rico, pero no sabemos
238
                // si luego habr? otra cosa.
239
                return Types.FLOAT;
240
            else
241
                return val.getSQLType();
242 3247 fjp
        }
243
        else
244
        {
245 10235 caballero
            // TODO: ESTO CREO QUE NO TIENE SENTIDO. SIEMPRE DEVUELVE Object.class, lo dice en
246 3247 fjp
            // la documentaci?n. Creo que habr?a que quitarlo.
247
                if (m_TableModel.getColumnClass(i) == String.class)
248
                    return Types.VARCHAR;
249
                if (m_TableModel.getColumnClass(i) == Float.class)
250
                    return Types.FLOAT;
251
                if (m_TableModel.getColumnClass(i) == Double.class)
252
                    return Types.DOUBLE;
253 3272 fjp
                if (m_TableModel.getColumnClass(i) == Double.class)
254 3247 fjp
                    return Types.INTEGER;
255 3272 fjp
                if (m_TableModel.getColumnClass(i) == Float.class)
256 3247 fjp
                    return Types.INTEGER;
257
                if (m_TableModel.getColumnClass(i) == Boolean.class)
258
                    return Types.BIT;
259
                if (m_TableModel.getColumnClass(i) == Date.class)
260
                    return Types.DATE;
261 10235 caballero
        }
262 1828 fernando
            return Types.VARCHAR;
263 1661 fjp
            // return m_TableModel.getColumnClass(i);
264 1828 fernando
//            throw new DriverException("Tipo no soportado: " + m_TableModel.getColumnClass(i).getName());
265 695 fjp
        }
266
        /* (non-Javadoc)
267
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
268
         */
269 1005 vcaballero
        public Value getFieldValue(long rowIndex, int fieldId)
270
                throws DriverException {
271 695 fjp
                return (Value) m_TableModel.getValueAt((int) rowIndex, fieldId);
272
        }
273 1005 vcaballero
274 695 fjp
        /* (non-Javadoc)
275
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
276
         */
277
        public int getFieldCount() throws DriverException {
278
                return m_TableModel.getColumnCount();
279
        }
280 1005 vcaballero
281 695 fjp
        /* (non-Javadoc)
282
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
283
         */
284
        public String getFieldName(int fieldId) throws DriverException {
285
                return m_TableModel.getColumnName(fieldId);
286
        }
287 1005 vcaballero
288 695 fjp
        /* (non-Javadoc)
289
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
290
         */
291
        public long getRowCount() throws DriverException {
292 12579 fjp
                return m_TableModel.getRowCount();
293 695 fjp
        }
294 10235 caballero
295 2944 fjp
    /* (non-Javadoc)
296
     * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
297
     */
298
    public void setDataSourceFactory(DataSourceFactory dsf) {
299
    }
300
301 4937 fjp
    /* (non-Javadoc)
302
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
303
     */
304 6323 fjp
    public void reload() throws DriverException, IOException {
305 4937 fjp
                memShapeInfo = new MemoryShapeInfo();
306
                arrayGeometries.clear();
307
                m_TableModel= new DefaultTableModel();
308
                fullExtent = null;
309
                m_Position = 0;
310
311 3952 fjp
    }
312 10235 caballero
    private void initializeFieldWidth(Object[] row) {
313
            fieldWidth=new int[row.length];
314
                for (int i=0;i<row.length;i++) {
315 13046 caballero
                        if (row[i] instanceof StringValue && row[i]==null)
316
                                fieldWidth[i]=0;
317
                        else
318
                                fieldWidth[i]=((Value)row[i]).getWidth();
319 10235 caballero
                }
320 4863 fjp
    }
321 10235 caballero
    /**
322
     * Actualize the width fields with StringValue.
323
     * @param row
324
     */
325
    private void actualizeFieldWidth(Object[] row) {
326
            for (int i=0;i<row.length;i++) {
327
                        if (row[i] instanceof StringValue) {
328 13046 caballero
                                if (row[i]!=null) {
329
                                        int width=((StringValue)row[i]).getWidth();
330
                                        if (fieldWidth[i]<width) {
331
                                                fieldWidth[i]=width;
332
                                        }
333 10235 caballero
                                }
334
                        }
335
                }
336
    }
337
    public int getFieldWidth(int fieldId){
338 10344 caballero
            if (fieldWidth==null)
339
                    return 1;
340 10235 caballero
            return fieldWidth[fieldId];
341
    }
342 6323 fjp
343 10235 caballero
344 695 fjp
}