Statistics
| Revision:

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

History | View | Annotate | Download (8.87 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.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
        private int[] fieldWidth=null;
81

    
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
         * @param geom shape.
95
         * @param row fila.
96
         */
97
        public void addGeometry(IGeometry geom, Object[] row) {
98
                if (geom == null) {
99
                        return; // No a?adimos nada
100
                }
101

    
102
                Rectangle2D boundsShp = geom.getBounds();
103
                memShapeInfo.addShapeInfo(boundsShp, geom.getGeometryType());
104
                arrayGeometries.add(geom);
105
                if (fieldWidth==null) {
106
                        initializeFieldWidth(row);
107
                }
108
                actualizeFieldWidth(row);
109
                m_TableModel.addRow(row);
110

    
111
                try {
112
                        fullExtent = getFullExtent();
113
                } catch (ReadDriverException e) {
114
                        e.printStackTrace();
115
                }
116

    
117
                if (fullExtent == null) {
118
                        fullExtent = boundsShp;
119
                } else {
120
                        fullExtent.add(boundsShp);
121
                }
122

    
123
                m_Position++;
124
        }
125

    
126

    
127
        /**
128
         * 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

    
139
                addGeometry(geom, row);
140
        }
141

    
142
        /**
143
         * Devuelve el extent a partir de un ?ndice.
144
         *
145
         * @param index ?ndice.
146
         *
147
         * @return Extent.
148
         */
149
        public Rectangle2D getShapeBounds(int index) throws ReadDriverException {
150
                return memShapeInfo.getBoundingBox(index);
151
        }
152

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

    
164

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

    
171
                return geom.cloneGeometry();
172
        }
173

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

    
181
        /* (non-Javadoc)
182
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
183
         */
184
        public Rectangle2D getFullExtent() throws ReadDriverException {
185
                return fullExtent;
186
        }
187

    
188

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

    
194
        /* (non-Javadoc)
195
         * @see com.hardcode.driverManager.Driver#getName()
196
         */
197
        public abstract String getName();
198

    
199
        /**
200
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
201
         */
202

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

    
260
        /* (non-Javadoc)
261
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
262
         */
263
        public int getFieldCount() throws ReadDriverException {
264
                return m_TableModel.getColumnCount();
265
        }
266

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

    
274
        /* (non-Javadoc)
275
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
276
         */
277
        public long getRowCount() throws ReadDriverException {
278
                return arrayGeometries.size();
279
        }
280

    
281
    /* (non-Javadoc)
282
     * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
283
     */
284
    public void setDataSourceFactory(DataSourceFactory dsf) {
285
    }
286

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

    
297
    }
298
    private void initializeFieldWidth(Object[] row) {
299
            fieldWidth=new int[row.length];
300
                for (int i=0;i<row.length;i++) {
301
                        fieldWidth[i]=((Value)row[i]).getWidth();
302
                }
303
    }
304
    /**
305
     * Actualize the width fields with StringValue.
306
     * @param row
307
     */
308
    private void actualizeFieldWidth(Object[] row) {
309
            for (int i=0;i<row.length;i++) {
310
                        if (row[i] instanceof StringValue) {
311
                                int width=((StringValue)row[i]).getWidth();
312
                                if (fieldWidth[i]<width) {
313
                                        fieldWidth[i]=width;
314
                                }
315
                        }
316
                }
317
    }
318
    public int getFieldWidth(int fieldId){
319
            if (fieldWidth==null)
320
                    return 1;
321
            return fieldWidth[fieldId];
322
    }
323

    
324

    
325
}