Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 1836

History | View | Annotate | Download (9.71 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.layers;
42

    
43
import java.io.IOException;
44

    
45
import org.apache.log4j.Logger;
46
import org.xml.sax.SAXException;
47

    
48
import com.hardcode.driverManager.DriverLoadException;
49
import com.hardcode.gdbms.engine.data.DataSource;
50
import com.hardcode.gdbms.engine.data.DataSourceFactory;
51
import com.hardcode.gdbms.engine.data.NoSuchTableException;
52
import com.hardcode.gdbms.engine.data.driver.DriverException;
53
import com.hardcode.gdbms.engine.data.driver.DriverInfo;
54
import com.hardcode.gdbms.engine.data.persistence.Handler;
55
import com.hardcode.gdbms.engine.data.persistence.Memento;
56
import com.hardcode.gdbms.engine.data.persistence.MementoContentHandler;
57
import com.hardcode.gdbms.engine.data.persistence.MementoException;
58
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
59
import com.hardcode.gdbms.engine.instruction.SemanticException;
60
import com.hardcode.gdbms.engine.values.Value;
61
import com.hardcode.gdbms.parser.ParseException;
62
import com.iver.utiles.XMLEntity;
63

    
64

    
65
/**
66
 * DataSource seleccionable.
67
 *
68
 * @author Fernando Gonz?lez Cort?s
69
 */
70
public class SelectableDataSource implements DataSource {
71
        private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName());
72
        private SelectionSupport selectionSupport = new SelectionSupport();
73
        private DataSource dataSource;
74

    
75
        /**
76
         * Crea un nuevo SelectableDataSource.
77
         *
78
         * @param name
79
         * @param ds
80
         */
81
        public SelectableDataSource(DataSource ds) {
82
                dataSource = ds;
83
        }
84

    
85
        public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws NoSuchTableException, ParseException, DriverLoadException, DriverException, SemanticException, IOException, XMLException{
86
                
87
                SelectionSupport ss = new SelectionSupport();
88
                ss.setXMLEntity(xml.getChild(0));
89
                XMLEntity xmlDS = xml.getChild(1);
90
                GDBMSParser parser = new GDBMSParser(xmlDS);
91
                MementoContentHandler gdbmsHandler = new MementoContentHandler();
92
                parser.setContentHandler(gdbmsHandler);
93
                try {
94
                        parser.parse();
95
                } catch (SAXException e) {
96
                        throw new XMLException(e);
97
                }
98
                
99
                return new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.AUTOMATIC_MODE));
100
        }
101

    
102
        public void setDataSourceFactory(DataSourceFactory dsf) {
103
                dataSource.setDataSourceFactory(dsf);
104
        }
105
        public void setSourceInfo(DriverInfo sourceInfo) {
106
                dataSource.setSourceInfo(sourceInfo);
107
        }
108
        /**
109
         * A?ade el soporte para la selecci?n.
110
         *
111
         * @param selectionSupport
112
         */
113
        public void setSelectionSupport(SelectionSupport selectionSupport) {
114
                this.selectionSupport = selectionSupport;
115
        }
116

    
117
        /**
118
         * Devuelve el DBMS.
119
         *
120
         * @return String.
121
         */
122
        public String getDBMS() {
123
                return dataSource.getDBMS();
124
        }
125
        
126
        /**
127
         * Devuelve el n?mero de campos.
128
         *
129
         * @return N?mero de campos.
130
         *
131
         * @throws DriverException
132
         */
133
        public int getFieldCount() throws DriverException {
134
                return dataSource.getFieldCount();
135
        }
136

    
137
        /**
138
         * Devuelve el ?ndice del campo a partir de su nombre.
139
         *
140
         * @param arg0 nombre del campo.
141
         *
142
         * @return ?ndice.
143
         *
144
         * @throws DriverException
145
         * @throws FieldNotFoundException
146
         */
147
        public int getFieldIndexByName(String arg0)
148
                throws DriverException {
149
                return dataSource.getFieldIndexByName(arg0);
150
        }
151

    
152
        /**
153
         * Devuelve el nombre del campo a partir del ?ndice.
154
         *
155
         * @param arg0 ?ndice.
156
         *
157
         * @return nombre del campo.
158
         *
159
         * @throws DriverException
160
         */
161
        public String getFieldName(int arg0) throws DriverException {
162
                return dataSource.getFieldName(arg0);
163
        }
164

    
165
        /**
166
         * Devuelve el valor a partir del n?mro de fila y columna.
167
         *
168
         * @param arg0 n?mero de registro.
169
         * @param arg1 n?mero de campo.
170
         *
171
         * @return Valor.
172
         *
173
         * @throws DriverException
174
         */
175
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
176
                return dataSource.getFieldValue(arg0, arg1);
177
        }
178

    
179
        /**
180
         * Devuelve el nombre del DataSource.
181
         *
182
         * @return Nombre.
183
         */
184
        public String getName() {
185
                return dataSource.getName();
186
        }
187

    
188
        /**
189
         * Devuelve el n?mro de filas en total.
190
         *
191
         * @return n?mero de filas.
192
         *
193
         * @throws DriverException
194
         */
195
        public long getRowCount() throws DriverException {
196
                return dataSource.getRowCount();
197
        }
198

    
199
        /**
200
         * Inicializa el dataSource.
201
         *
202
         * @throws DriverException
203
         */
204
        public void start() throws DriverException {
205
                logger.debug("dataSource.start()");
206
                dataSource.start();
207
        }
208

    
209
        /**
210
         * Finaliza el DataSource.
211
         *
212
         * @throws DriverException
213
         */
214
        public void stop() throws DriverException {
215
                logger.debug("dataSource.stop()");
216
                dataSource.stop();
217
        }
218

    
219
        /**
220
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
221
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
222
         * eventos, se realiza la propagaci?n de manera manual al final de la
223
         * "r?faga" de eventos
224
         */
225
        public void fireSelectionEvents() {
226
                selectionSupport.fireSelectionEvents();
227
        }
228

    
229
        /**
230
         * A?ade un nuevo Listener al SelectionSupport.
231
         *
232
         * @param listener SelectionListener.
233
         */
234
        public void addSelectionListener(SelectionListener listener) {
235
                selectionSupport.addSelectionListener(listener);
236
        }
237

    
238
        /**
239
         * Borra un Listener al SelectionSupport.
240
         *
241
         * @param listener Listener a borrar.
242
         */
243
        public void removeSelectionListener(SelectionListener listener) {
244
                selectionSupport.removeSelectionListener(listener);
245
        }
246

    
247
        /**
248
         * Borra la selecci?n.
249
         */
250
        public void clearSelection() {
251
                selectionSupport.clearSelection();
252
        }
253

    
254
        /**
255
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
256
         *
257
         * @return FBitset con los elementos seleccionados.
258
         */
259
        public FBitSet getSelection() {
260
                return selectionSupport.getSelection();
261
        }
262

    
263
        /**
264
         * Devuelve el SelectionSupport.
265
         *
266
         * @return SelectinSuport.
267
         */
268
        public SelectionSupport getSelectionSupport() {
269
                return selectionSupport;
270
        }
271

    
272
        /**
273
         * Devuelve true si el elemento est? seleccionado.
274
         *
275
         * @param recordIndex ?ndice del registro.
276
         *
277
         * @return True si el registro est? seleccionado.
278
         */
279
        public boolean isSelected(int recordIndex) {
280
                return selectionSupport.isSelected(recordIndex);
281
        }
282

    
283
        /**
284
         * Inserta una nueva selecci?n.
285
         *
286
         * @param selection FBitSet.
287
         */
288
        public void setSelection(FBitSet selection) {
289
                selectionSupport.setSelection(selection);
290
        }
291

    
292
        /**
293
         * Inserta un nuevo nombre para el dataSource.
294
         *
295
         * @param name Nuevo nombre del DataSource.
296
         *
297
         * @throws RuntimeException
298
         */
299
        public void setName(String name) {
300
                try {
301
                        LayerFactory.getDataSourceFactory().changeDataSourceName(dataSource.getName(), name);
302
                } catch (NoSuchTableException e) {
303
                        throw new RuntimeException("No se encuentra la tabla????");
304
                }
305
        }
306

    
307
        private void putMemento(XMLEntity xml) throws XMLException {
308
                try {
309
                        GDBMSHandler handler = new GDBMSHandler();
310
                        Memento m = getMemento();
311
                        m.setContentHandler(handler);
312
                        m.getXML();
313
                        XMLEntity child = handler.getXMLEntity();
314
                        
315
                        xml.addChild(child);
316
                } catch (MementoException e) {
317
                        throw new XMLException(e);
318
                } catch (SAXException e) {
319
                        throw new XMLException(e);
320
                }
321
        }
322

    
323
        /**
324
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
325
         * DataSource.
326
         *
327
         * @return XMLEntity.
328
         * @throws XMLException
329
         */
330
        public XMLEntity getXMLEntity() throws XMLException {
331
                XMLEntity xml = new XMLEntity();
332
                xml.putProperty("className",this.getClass().getName());
333
                xml.addChild(selectionSupport.getXMLEntity());
334
                putMemento(xml);
335

    
336
                return xml;
337
        }
338

    
339
        /**
340
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
341
         */
342
        public long[] getWhereFilter() throws IOException {
343
                return dataSource.getWhereFilter();
344
        }
345

    
346
        /**
347
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
348
         */
349
        public int getFieldType(int i) throws DriverException {
350
                return dataSource.getFieldType(i);
351
        }
352

    
353
        /**
354
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
355
         */
356
        public DataSourceFactory getDataSourceFactory() {
357
                return dataSource.getDataSourceFactory();
358
        }
359

    
360
        /**
361
         * @see com.hardcode.gdbms.engine.data.HasProperties#getProperty(java.lang.String)
362
         */
363
        public String getProperty(String propertyName) {
364
                return dataSource.getProperty(propertyName);
365
        }
366

    
367
        /**
368
         * @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
369
         */
370
        public String getAsString() throws DriverException {
371
                return dataSource.getAsString();
372
        }
373

    
374
        /**
375
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
376
         */
377
        public void remove() {
378
                dataSource.remove();
379
        }
380

    
381
        /**
382
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
383
         */
384
        public Memento getMemento() throws MementoException {
385
                return dataSource.getMemento();
386
        }
387

    
388
        /**
389
         * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
390
         */
391
        public DriverInfo getSourceInfo() {
392
                return dataSource.getSourceInfo();
393
        }
394
}