Statistics
| Revision:

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

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