Statistics
| Revision:

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

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

    
55
import javax.swing.table.DefaultTableModel;
56

    
57
import com.hardcode.gdbms.engine.data.DataSourceFactory;
58
import com.hardcode.gdbms.engine.data.driver.DriverException;
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.FNullGeometry;
63
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

    
68

    
69
/**
70
 * Clase abstracta para Driver en memoria.
71
 *
72
 * @author FJP
73
 */
74
public abstract class MemoryDriver implements VectorialDriver, ObjectDriver,
75
        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
        private int[] fieldWidth=null;
82

    
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
         * @param geom shape.
96
         * @param row fila.
97
         */
98
        public void addGeometry(IGeometry geom, Object[] row) {
99
                if (geom == null) {
100
                        return; // No a?adimos nada
101
                }
102
                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
                        }                         
111
                }
112
                else
113
                {
114
                         Rectangle2D boundsShp = new Rectangle2D.Double();
115
                         memShapeInfo.addShapeInfo (boundsShp, geom.getGeometryType());
116
                         arrayGeometries.add(geom);
117
                        
118
                }
119
//                Rectangle2D boundsShp = geom.getBounds();
120
//                memShapeInfo.addShapeInfo(boundsShp, geom.getGeometryType());
121
//                arrayGeometries.add(geom);
122
                if (fieldWidth==null) {
123
                        initializeFieldWidth(row);
124
                }
125
                actualizeFieldWidth(row);
126
                m_TableModel.addRow(row);
127

    
128
                try {
129
                        fullExtent = getFullExtent();
130
                } catch (IOException e) {
131
                        e.printStackTrace();
132
                }
133

    
134

    
135
                m_Position++;
136
        }
137

    
138

    
139
        /**
140
         * 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

    
151
                addGeometry(geom, row);
152
        }
153

    
154
        /**
155
         * 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
                return memShapeInfo.getBoundingBox(index);
165
        }
166

    
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
                return memShapeInfo.getType(index);
176
        }
177

    
178

    
179
        /* (non-Javadoc)
180
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
181
         */
182
        public IGeometry getShape(int index) {
183
                IGeometry geom = (IGeometry) arrayGeometries.get(index);
184

    
185
                return geom.cloneGeometry();
186
        }
187

    
188
        /* (non-Javadoc)
189
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
190
         */
191
        public int getShapeCount() throws IOException {
192
                return arrayGeometries.size();
193
        }
194

    
195
        /* (non-Javadoc)
196
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
197
         */
198
        public Rectangle2D getFullExtent() throws IOException {
199
                return fullExtent;
200
        }
201

    
202

    
203
        /* (non-Javadoc)
204
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
205
         */
206
        public abstract int getShapeType();
207

    
208
        /* (non-Javadoc)
209
         * @see com.hardcode.driverManager.Driver#getName()
210
         */
211
        public abstract String getName();
212

    
213
        /**
214
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
215
         */
216

    
217
/*
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
 *
223
 * 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
 *
227
 *
228
 *
229
 *
230
 */
231
        public int getFieldType(int i) throws DriverException {
232
            // TODO: Revisar esto. Por ejemplo, el long
233
            if (getRowCount() > 1)
234
        {
235
            Value val = getFieldValue(0,i);
236
            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
        }
243
        else
244
        {
245
            // TODO: ESTO CREO QUE NO TIENE SENTIDO. SIEMPRE DEVUELVE Object.class, lo dice en
246
            // 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
                if (m_TableModel.getColumnClass(i) == Double.class)
254
                    return Types.INTEGER;
255
                if (m_TableModel.getColumnClass(i) == Float.class)
256
                    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
        }
262
            return Types.VARCHAR;
263
            // return m_TableModel.getColumnClass(i);
264
//            throw new DriverException("Tipo no soportado: " + m_TableModel.getColumnClass(i).getName());
265
        }
266
        /* (non-Javadoc)
267
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
268
         */
269
        public Value getFieldValue(long rowIndex, int fieldId)
270
                throws DriverException {
271
                return (Value) m_TableModel.getValueAt((int) rowIndex, fieldId);
272
        }
273

    
274
        /* (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

    
281
        /* (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

    
288
        /* (non-Javadoc)
289
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
290
         */
291
        public long getRowCount() throws DriverException {
292
                return m_TableModel.getRowCount();
293
        }
294

    
295
    /* (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
    /* (non-Javadoc)
302
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
303
     */
304
    public void reload() throws DriverException, IOException {
305
                memShapeInfo = new MemoryShapeInfo();
306
                arrayGeometries.clear();
307
                m_TableModel= new DefaultTableModel();
308
                fullExtent = null;
309
                m_Position = 0;
310

    
311
    }
312
    private void initializeFieldWidth(Object[] row) {
313
            fieldWidth=new int[row.length];
314
                for (int i=0;i<row.length;i++) {
315
                        fieldWidth[i]=((Value)row[i]).getWidth();
316
                }
317
    }
318
    /**
319
     * Actualize the width fields with StringValue.
320
     * @param row
321
     */
322
    private void actualizeFieldWidth(Object[] row) {
323
            for (int i=0;i<row.length;i++) {
324
                        if (row[i] instanceof StringValue) {
325
                                int width=((StringValue)row[i]).getWidth();
326
                                if (fieldWidth[i]<width) {
327
                                        fieldWidth[i]=width;
328
                                }
329
                        }
330
                }
331
    }
332
    public int getFieldWidth(int fieldId){
333
            if (fieldWidth==null)
334
                    return 1;
335
            return fieldWidth[fieldId];
336
    }
337

    
338

    
339
}