Statistics
| Revision:

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

History | View | Annotate | Download (8.12 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.io.File;
51
import java.io.IOException;
52
import java.sql.Types;
53
import java.util.ArrayList;
54
import java.util.Date;
55

    
56
import javax.swing.table.DefaultTableModel;
57

    
58
import com.hardcode.gdbms.engine.data.DataSourceFactory;
59
import com.hardcode.gdbms.engine.data.driver.DriverException;
60
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
61
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

    
67

    
68
/**
69
 * Clase abstracta para Driver en memoria.
70
 *
71
 * @author FJP
72
 */
73
public abstract class MemoryDriver implements VectorialDriver, ObjectDriver,
74
        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

    
81
        /**
82
         * Devuelve el modelo de la tabla.
83
         *
84
         * @return modelo de la tabla.
85
         */
86
        public DefaultTableModel getTableModel() {
87
                return m_TableModel;
88
        }
89

    
90
        /**
91
         * A?ade un shape.
92
         *
93
         * @param geom shape.
94
         * @param row fila.
95
         */
96
        public void addGeometry(IGeometry geom, Object[] row) {
97
                if (geom == null) {
98
                        return; // No a?adimos nada
99
                }
100

    
101
                Rectangle2D boundsShp = geom.getBounds();
102
                memShapeInfo.addShapeInfo(boundsShp, geom.getGeometryType());
103
                arrayGeometries.add(geom);
104
                m_TableModel.addRow(row);
105

    
106
                try {
107
                        fullExtent = getFullExtent();
108
                } catch (IOException e) {
109
                        e.printStackTrace();
110
                }
111

    
112
                if (fullExtent == null) {
113
                        fullExtent = boundsShp;
114
                } else {
115
                        fullExtent.add(boundsShp);
116
                }
117

    
118
                m_Position++;
119
        }
120

    
121
        
122
        /**
123
         * M?todo de conveniencia, para poder a?adir directamente un shape
124
         * o una IGeometry. (Arriba est? el de a?adir una IGeometry.
125
         * @param shp
126
         * @param row
127
         */
128
        public void addShape(FShape shp, Object[] row) {
129
                if (shp == null) {
130
                        return; // No a?adimos nada
131
                }
132
                IGeometry geom = ShapeFactory.createGeometry(shp);
133
                
134
                addGeometry(geom, row);
135
        }
136

    
137
        /**
138
         * Devuelve el extent a partir de un ?ndice.
139
         *
140
         * @param index ?ndice.
141
         *
142
         * @return Extent.
143
         *
144
         * @throws IOException
145
         */
146
        public Rectangle2D getShapeBounds(int index) throws IOException {
147
                return memShapeInfo.getBoundingBox(index);
148
        }
149

    
150
        /**
151
         * Devuelve el tipo del shape.
152
         *
153
         * @param index ?ndice.
154
         *
155
         * @return tipo del shape.
156
         */
157
        public int getShapeType(int index) {
158
                return memShapeInfo.getType(index);
159
        }
160

    
161

    
162
        /* (non-Javadoc)
163
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
164
         */
165
        public IGeometry getShape(int index) {
166
                IGeometry geom = (IGeometry) arrayGeometries.get(index);
167

    
168
                return geom.cloneGeometry();
169
        }
170

    
171
        /* (non-Javadoc)
172
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
173
         */
174
        public int getShapeCount() throws IOException {
175
                return arrayGeometries.size();
176
        }
177

    
178
        /* (non-Javadoc)
179
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
180
         */
181
        public Rectangle2D getFullExtent() throws IOException {
182
                return fullExtent;
183
        }
184

    
185

    
186
        /* (non-Javadoc)
187
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
188
         */
189
        public abstract int getShapeType();
190

    
191
        /* (non-Javadoc)
192
         * @see com.hardcode.driverManager.Driver#getName()
193
         */
194
        public abstract String getName();
195

    
196
        /**
197
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
198
         */
199
        
200
/*
201
 * azabala, bug 666
202
 * Habra que estudiar como hacer, porque si una fila tiene el primer valor
203
 * (o todos) a NullValue, devolvera como tipo de dato Types.NULL.
204
 * Quizas, habra que hacer que los MemoryDriver tb tengan un ITableDefinition.
205
 * 
206
 * DxfMemoryDriver no obstante si que tiene informacion sobre el esquema, asi
207
 * que debera sobreescribir este metodo para salvar el bug
208
 * (metodo getTableDefinition)
209
 * 
210
 * 
211
 * 
212
 * 
213
 */
214
        public int getFieldType(int i) throws DriverException {
215
            // TODO: Revisar esto. Por ejemplo, el long
216
            if (getRowCount() > 1)
217
        {
218
            Value val = getFieldValue(0,i);
219
            if (val.getSQLType() == Types.INTEGER)
220
                // Sabemos que es num?rico, pero no sabemos
221
                // si luego habr? otra cosa.
222
                return Types.FLOAT;
223
            else
224
                return val.getSQLType();
225
        }
226
        else
227
        {
228
            // TODO: ESTO CREO QUE NO TIENE SENTIDO. SIEMPRE DEVUELVE Object.class, lo dice en 
229
            // la documentaci?n. Creo que habr?a que quitarlo.
230
                if (m_TableModel.getColumnClass(i) == String.class)
231
                    return Types.VARCHAR;
232
                if (m_TableModel.getColumnClass(i) == Float.class)
233
                    return Types.FLOAT;
234
                if (m_TableModel.getColumnClass(i) == Double.class)
235
                    return Types.DOUBLE;
236
                if (m_TableModel.getColumnClass(i) == Double.class)
237
                    return Types.INTEGER;
238
                if (m_TableModel.getColumnClass(i) == Float.class)
239
                    return Types.INTEGER;
240
                if (m_TableModel.getColumnClass(i) == Boolean.class)
241
                    return Types.BIT;
242
                if (m_TableModel.getColumnClass(i) == Date.class)
243
                    return Types.DATE;
244
        }            
245
            return Types.VARCHAR;
246
            // return m_TableModel.getColumnClass(i);
247
//            throw new DriverException("Tipo no soportado: " + m_TableModel.getColumnClass(i).getName());
248
        }
249
        /* (non-Javadoc)
250
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
251
         */
252
        public Value getFieldValue(long rowIndex, int fieldId)
253
                throws DriverException {
254
                return (Value) m_TableModel.getValueAt((int) rowIndex, fieldId);
255
        }
256

    
257
        /* (non-Javadoc)
258
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
259
         */
260
        public int getFieldCount() throws DriverException {
261
                return m_TableModel.getColumnCount();
262
        }
263

    
264
        /* (non-Javadoc)
265
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
266
         */
267
        public String getFieldName(int fieldId) throws DriverException {
268
                return m_TableModel.getColumnName(fieldId);
269
        }
270

    
271
        /* (non-Javadoc)
272
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
273
         */
274
        public long getRowCount() throws DriverException {
275
                return arrayGeometries.size();
276
        }
277
    
278
    /* (non-Javadoc)
279
     * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
280
     */
281
    public void setDataSourceFactory(DataSourceFactory dsf) {
282
    }
283

    
284
    /* (non-Javadoc)
285
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
286
     */
287
    public void reload() throws DriverException, IOException {
288
                memShapeInfo = new MemoryShapeInfo();
289
                arrayGeometries.clear();
290
                m_TableModel= new DefaultTableModel();
291
                fullExtent = null;
292
                m_Position = 0;
293

    
294
    }
295
    
296
    public int getFieldWidth(int fieldId)
297
    {
298
            // TODO
299
            return 30;
300
    }
301
    
302

    
303
}