Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / MemoryDriver.java @ 6458

History | View | Annotate | Download (7.69 KB)

1 695 fjp
/*
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 1100 fjp
/* 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 695 fjp
package com.iver.cit.gvsig.fmap.drivers;
48
49 1005 vcaballero
import java.awt.geom.Rectangle2D;
50
import java.io.File;
51
import java.io.IOException;
52 1773 fernando
import java.sql.Types;
53 1005 vcaballero
import java.util.ArrayList;
54 1661 fjp
import java.util.Date;
55 1005 vcaballero
56
import javax.swing.table.DefaultTableModel;
57
58 2944 fjp
import com.hardcode.gdbms.engine.data.DataSourceFactory;
59 1828 fernando
import com.hardcode.gdbms.engine.data.driver.DriverException;
60
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
61 1653 fernando
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 1005 vcaballero
67 1653 fernando
68 695 fjp
/**
69 1005 vcaballero
 * Clase abstracta para Driver en memoria.
70
 *
71 695 fjp
 * @author FJP
72
 */
73 3271 fjp
public abstract class MemoryDriver implements VectorialDriver, ObjectDriver,
74 1005 vcaballero
        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 5126 fjp
         * @param geom shape.
94 1005 vcaballero
         * @param row fila.
95
         */
96 5126 fjp
        public void addGeometry(IGeometry geom, Object[] row) {
97
                if (geom == null) {
98 1005 vcaballero
                        return; // No a?adimos nada
99
                }
100
101 5126 fjp
                Rectangle2D boundsShp = geom.getBounds();
102
                memShapeInfo.addShapeInfo(boundsShp, geom.getGeometryType());
103
                arrayGeometries.add(geom);
104 1005 vcaballero
                m_TableModel.addRow(row);
105
106
                try {
107 695 fjp
                        fullExtent = getFullExtent();
108
                } catch (IOException e) {
109
                        e.printStackTrace();
110
                }
111
112 1005 vcaballero
                if (fullExtent == null) {
113
                        fullExtent = boundsShp;
114
                } else {
115
                        fullExtent.add(boundsShp);
116
                }
117
118
                m_Position++;
119
        }
120
121 5126 fjp
122 1005 vcaballero
        /**
123 5126 fjp
         * 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 1005 vcaballero
         * 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 695 fjp
                return memShapeInfo.getBoundingBox(index);
148
        }
149 1005 vcaballero
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 695 fjp
                return memShapeInfo.getType(index);
159
        }
160 1005 vcaballero
161
162 695 fjp
        /* (non-Javadoc)
163 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShape(int)
164 695 fjp
         */
165 3271 fjp
        public IGeometry getShape(int index) {
166 703 fjp
                IGeometry geom = (IGeometry) arrayGeometries.get(index);
167 1005 vcaballero
168 703 fjp
                return geom.cloneGeometry();
169 695 fjp
        }
170 1005 vcaballero
171 695 fjp
        /* (non-Javadoc)
172 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeCount()
173 695 fjp
         */
174
        public int getShapeCount() throws IOException {
175
                return arrayGeometries.size();
176
        }
177 1005 vcaballero
178 695 fjp
        /* (non-Javadoc)
179 3271 fjp
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getFullExtent()
180 695 fjp
         */
181
        public Rectangle2D getFullExtent() throws IOException {
182
                return fullExtent;
183
        }
184 1005 vcaballero
185
186 695 fjp
        /* (non-Javadoc)
187
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
188
         */
189 714 fjp
        public abstract int getShapeType();
190 1005 vcaballero
191 695 fjp
        /* (non-Javadoc)
192
         * @see com.hardcode.driverManager.Driver#getName()
193
         */
194
        public abstract String getName();
195 1005 vcaballero
196 1653 fernando
        /**
197
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
198 695 fjp
         */
199 1773 fernando
        public int getFieldType(int i) throws DriverException {
200 1661 fjp
            // TODO: Revisar esto. Por ejemplo, el long
201 3247 fjp
            if (getRowCount() > 1)
202
        {
203
            Value val = getFieldValue(0,i);
204 3272 fjp
            if (val.getSQLType() == Types.INTEGER)
205
                // Sabemos que es num?rico, pero no sabemos
206
                // si luego habr? otra cosa.
207
                return Types.FLOAT;
208
            else
209
                return val.getSQLType();
210 3247 fjp
        }
211
        else
212
        {
213
            // TODO: ESTO CREO QUE NO TIENE SENTIDO. SIEMPRE DEVUELVE Object.class, lo dice en
214
            // la documentaci?n. Creo que habr?a que quitarlo.
215
                if (m_TableModel.getColumnClass(i) == String.class)
216
                    return Types.VARCHAR;
217
                if (m_TableModel.getColumnClass(i) == Float.class)
218
                    return Types.FLOAT;
219
                if (m_TableModel.getColumnClass(i) == Double.class)
220
                    return Types.DOUBLE;
221 3272 fjp
                if (m_TableModel.getColumnClass(i) == Double.class)
222 3247 fjp
                    return Types.INTEGER;
223 3272 fjp
                if (m_TableModel.getColumnClass(i) == Float.class)
224 3247 fjp
                    return Types.INTEGER;
225
                if (m_TableModel.getColumnClass(i) == Boolean.class)
226
                    return Types.BIT;
227
                if (m_TableModel.getColumnClass(i) == Date.class)
228
                    return Types.DATE;
229
        }
230 1828 fernando
            return Types.VARCHAR;
231 1661 fjp
            // return m_TableModel.getColumnClass(i);
232 1828 fernando
//            throw new DriverException("Tipo no soportado: " + m_TableModel.getColumnClass(i).getName());
233 695 fjp
        }
234
        /* (non-Javadoc)
235
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
236
         */
237 1005 vcaballero
        public Value getFieldValue(long rowIndex, int fieldId)
238
                throws DriverException {
239 695 fjp
                return (Value) m_TableModel.getValueAt((int) rowIndex, fieldId);
240
        }
241 1005 vcaballero
242 695 fjp
        /* (non-Javadoc)
243
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
244
         */
245
        public int getFieldCount() throws DriverException {
246
                return m_TableModel.getColumnCount();
247
        }
248 1005 vcaballero
249 695 fjp
        /* (non-Javadoc)
250
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
251
         */
252
        public String getFieldName(int fieldId) throws DriverException {
253
                return m_TableModel.getColumnName(fieldId);
254
        }
255 1005 vcaballero
256 695 fjp
        /* (non-Javadoc)
257
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
258
         */
259
        public long getRowCount() throws DriverException {
260
                return arrayGeometries.size();
261
        }
262 2944 fjp
263
    /* (non-Javadoc)
264
     * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
265
     */
266
    public void setDataSourceFactory(DataSourceFactory dsf) {
267
    }
268
269 4937 fjp
    /* (non-Javadoc)
270
     * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
271
     */
272 6323 fjp
    public void reload() throws DriverException, IOException {
273 4937 fjp
                memShapeInfo = new MemoryShapeInfo();
274
                arrayGeometries.clear();
275
                m_TableModel= new DefaultTableModel();
276
                fullExtent = null;
277
                m_Position = 0;
278
279 3952 fjp
    }
280 4863 fjp
281
    public int getFieldWidth(int fieldId)
282
    {
283
            // TODO
284
            return 30;
285
    }
286 6323 fjp
287
288 695 fjp
}