Statistics
| Revision:

svn-gvsig-desktop / branches / simbologia / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / MemoryDriver.java @ 10450

History | View | Annotate | Download (8.92 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
 * @author   FJP
71
 */
72
public abstract class MemoryDriver implements VectorialDriver, ObjectDriver,
73
        BoundedShapes {
74
        private MemoryShapeInfo memShapeInfo = new MemoryShapeInfo();
75
        private ArrayList arrayGeometries = new ArrayList();
76
        /**
77
         * @uml.property  name="fullExtent"
78
         */
79
        private Rectangle2D fullExtent;
80
        private int m_Position;
81
        private DefaultTableModel m_TableModel = new DefaultTableModel();
82
        private int[] fieldWidth=null;
83
        
84
        /**
85
         * Devuelve el modelo de la tabla.
86
         *
87
         * @return modelo de la tabla.
88
         */
89
        public DefaultTableModel getTableModel() {
90
                return m_TableModel;
91
        }
92

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

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

    
113
                try {
114
                        fullExtent = getFullExtent();
115
                } catch (IOException e) {
116
                        e.printStackTrace();
117
                }
118

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

    
125
                m_Position++;
126
        }
127

    
128

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

    
141
                addGeometry(geom, row);
142
        }
143

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

    
157
        /**
158
         * Devuelve el tipo del shape.
159
         *
160
         * @param index ?ndice.
161
         *
162
         * @return tipo del shape.
163
         */
164
        public int getShapeType(int index) {
165
                return memShapeInfo.getType(index);
166
        }
167

    
168

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

    
175
                return geom.cloneGeometry();
176
        }
177

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

    
185
        /* (non-Javadoc)
186
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
187
         */
188
        /**
189
         * @return
190
         * @throws IOException
191
         * @uml.property  name="fullExtent"
192
         */
193
        public Rectangle2D getFullExtent() throws IOException {
194
                return fullExtent;
195
        }
196

    
197

    
198
        /* (non-Javadoc)
199
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
200
         */
201
        public abstract int getShapeType();
202

    
203
        /* (non-Javadoc)
204
         * @see com.hardcode.driverManager.Driver#getName()
205
         */
206
        public abstract String getName();
207

    
208
        /**
209
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
210
         */
211

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

    
269
        /* (non-Javadoc)
270
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
271
         */
272
        public int getFieldCount() throws DriverException {
273
                return m_TableModel.getColumnCount();
274
        }
275

    
276
        /* (non-Javadoc)
277
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
278
         */
279
        public String getFieldName(int fieldId) throws DriverException {
280
                return m_TableModel.getColumnName(fieldId);
281
        }
282

    
283
        /* (non-Javadoc)
284
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
285
         */
286
        public long getRowCount() throws DriverException {
287
                return arrayGeometries.size();
288
        }
289

    
290
    /* (non-Javadoc)
291
     * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
292
     */
293
    public void setDataSourceFactory(DataSourceFactory dsf) {
294
    }
295

    
296
    /* (non-Javadoc)
297
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
298
     */
299
    public void reload() throws DriverException, IOException {
300
                memShapeInfo = new MemoryShapeInfo();
301
                arrayGeometries.clear();
302
                m_TableModel= new DefaultTableModel();
303
                fullExtent = null;
304
                m_Position = 0;
305

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

    
333

    
334
}