Statistics
| Revision:

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

History | View | Annotate | Download (8.79 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.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 (IOException 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
         * @throws IOException
150
         */
151
        public Rectangle2D getShapeBounds(int index) throws IOException {
152
                return memShapeInfo.getBoundingBox(index);
153
        }
154

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

    
166

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

    
173
                return geom.cloneGeometry();
174
        }
175

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

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

    
190

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

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

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

    
205
/*
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
 *
211
 * 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
 *
215
 *
216
 *
217
 *
218
 */
219
        public int getFieldType(int i) throws DriverException {
220
            // TODO: Revisar esto. Por ejemplo, el long
221
            if (getRowCount() > 1)
222
        {
223
            Value val = getFieldValue(0,i);
224
            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
        }
231
        else
232
        {
233
            // TODO: ESTO CREO QUE NO TIENE SENTIDO. SIEMPRE DEVUELVE Object.class, lo dice en
234
            // 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
                if (m_TableModel.getColumnClass(i) == Double.class)
242
                    return Types.INTEGER;
243
                if (m_TableModel.getColumnClass(i) == Float.class)
244
                    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
        }
250
            return Types.VARCHAR;
251
            // return m_TableModel.getColumnClass(i);
252
//            throw new DriverException("Tipo no soportado: " + m_TableModel.getColumnClass(i).getName());
253
        }
254
        /* (non-Javadoc)
255
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
256
         */
257
        public Value getFieldValue(long rowIndex, int fieldId)
258
                throws DriverException {
259
                return (Value) m_TableModel.getValueAt((int) rowIndex, fieldId);
260
        }
261

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

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

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

    
283
    /* (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
    /* (non-Javadoc)
290
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
291
     */
292
    public void reload() throws DriverException, IOException {
293
                memShapeInfo = new MemoryShapeInfo();
294
                arrayGeometries.clear();
295
                m_TableModel= new DefaultTableModel();
296
                fullExtent = null;
297
                m_Position = 0;
298

    
299
    }
300
    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
    }
306
    /**
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
            if (fieldWidth==null)
322
                    return 1;
323
            return fieldWidth[fieldId];
324
    }
325

    
326

    
327
}