Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 2183

History | View | Annotate | Download (11.7 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.Memento;
55
import com.hardcode.gdbms.engine.data.persistence.MementoContentHandler;
56
import com.hardcode.gdbms.engine.data.persistence.MementoException;
57
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
58
import com.hardcode.gdbms.engine.instruction.SemanticException;
59
import com.hardcode.gdbms.engine.values.Value;
60
import com.hardcode.gdbms.engine.values.ValueCollection;
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
                SelectableDataSource sds=new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.CLOSING_AUTOMATIC_MODE));
99
                sds.selectionSupport=ss;
100
                return sds;
101
        }
102

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

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

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

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

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

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

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

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

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

    
220
        /**
221
         * A partir del XMLEntity se rellenan los atributos del DataSource.
222
         *
223
         * @param child
224
         */
225
        public void setXMLEntity03(XMLEntity child) {
226
                selectionSupport.setXMLEntity(child.getChild(0));
227
        }
228

    
229
        /**
230
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
231
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
232
         * eventos, se realiza la propagaci?n de manera manual al final de la
233
         * "r?faga" de eventos
234
         */
235
        public void fireSelectionEvents() {
236
                selectionSupport.fireSelectionEvents();
237
        }
238

    
239
        /**
240
         * A?ade un nuevo Listener al SelectionSupport.
241
         *
242
         * @param listener SelectionListener.
243
         */
244
        public void addSelectionListener(SelectionListener listener) {
245
                selectionSupport.addSelectionListener(listener);
246
        }
247

    
248
        /**
249
         * Borra un Listener al SelectionSupport.
250
         *
251
         * @param listener Listener a borrar.
252
         */
253
        public void removeSelectionListener(SelectionListener listener) {
254
                selectionSupport.removeSelectionListener(listener);
255
        }
256

    
257
        /**
258
         * Borra la selecci?n.
259
         */
260
        public void clearSelection() {
261
                selectionSupport.clearSelection();
262
        }
263

    
264
        /**
265
         * Develve un FBitSet con los ?ndices de los elementos seleccionados.
266
         *
267
         * @return FBitset con los elementos seleccionados.
268
         */
269
        public FBitSet getSelection() {
270
                return selectionSupport.getSelection();
271
        }
272

    
273
        /**
274
         * Devuelve el SelectionSupport.
275
         *
276
         * @return SelectinSuport.
277
         */
278
        public SelectionSupport getSelectionSupport() {
279
                return selectionSupport;
280
        }
281

    
282
        /**
283
         * Devuelve true si el elemento est? seleccionado.
284
         *
285
         * @param recordIndex ?ndice del registro.
286
         *
287
         * @return True si el registro est? seleccionado.
288
         */
289
        public boolean isSelected(int recordIndex) {
290
                return selectionSupport.isSelected(recordIndex);
291
        }
292

    
293
        /**
294
         * Inserta una nueva selecci?n.
295
         *
296
         * @param selection FBitSet.
297
         */
298
        public void setSelection(FBitSet selection) {
299
                selectionSupport.setSelection(selection);
300
        }
301

    
302
        /**
303
         * Inserta un nuevo nombre para el dataSource.
304
         *
305
         * @param name Nuevo nombre del DataSource.
306
         *
307
         * @throws RuntimeException
308
         */
309
        public void setName(String name) {
310
                try {
311
                        LayerFactory.getDataSourceFactory().changeDataSourceName(dataSource.getName(), name);
312
                } catch (NoSuchTableException e) {
313
                        throw new RuntimeException("No se encuentra la tabla????");
314
                }
315
        }
316

    
317
        private void putMemento(XMLEntity xml) throws XMLException {
318
                try {
319
                        GDBMSHandler handler = new GDBMSHandler();
320
                        Memento m = getMemento();
321
                        m.setContentHandler(handler);
322
                        m.getXML();
323
                        XMLEntity child = handler.getXMLEntity();
324
                        
325
                        xml.addChild(child);
326
                } catch (MementoException e) {
327
                        throw new XMLException(e);
328
                } catch (SAXException e) {
329
                        throw new XMLException(e);
330
                }
331
        }
332

    
333
        /**
334
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
335
         * DataSource.
336
         *
337
         * @return XMLEntity.
338
         * @throws XMLException
339
         */
340
        public XMLEntity getXMLEntity() throws XMLException {
341
                XMLEntity xml = new XMLEntity();
342
                xml.putProperty("className",this.getClass().getName());
343
                xml.addChild(selectionSupport.getXMLEntity());
344
                putMemento(xml);
345

    
346
                return xml;
347
        }
348

    
349
        /**
350
         * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
351
         */
352
        public long[] getWhereFilter() throws IOException {
353
                return dataSource.getWhereFilter();
354
        }
355

    
356
        /**
357
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
358
         */
359
        public int getFieldType(int i) throws DriverException {
360
                return dataSource.getFieldType(i);
361
        }
362

    
363
        /**
364
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
365
         */
366
        public DataSourceFactory getDataSourceFactory() {
367
                return dataSource.getDataSourceFactory();
368
        }
369

    
370
        /**
371
         * @see com.hardcode.gdbms.engine.data.HasProperties#getProperty(java.lang.String)
372
         */
373
        public String getProperty(String propertyName) {
374
                return dataSource.getProperty(propertyName);
375
        }
376

    
377
        /**
378
         * @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
379
         */
380
        public String getAsString() throws DriverException {
381
                return dataSource.getAsString();
382
        }
383

    
384
        /**
385
         * @throws DriverException
386
         * @see com.hardcode.gdbms.engine.data.DataSource#remove()
387
         */
388
        public void remove() throws DriverException {
389
                dataSource.remove();
390
        }
391

    
392
        /**
393
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
394
         */
395
        public Memento getMemento() throws MementoException {
396
                return dataSource.getMemento();
397
        }
398

    
399
        /**
400
         * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
401
         */
402
        public DriverInfo getSourceInfo() {
403
                return dataSource.getSourceInfo();
404
        }
405

    
406
    /**
407
     * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
408
     */
409
    public int[] getPrimaryKeys() throws DriverException {
410
            return dataSource.getPrimaryKeys();
411
    }
412

    
413
    /**
414
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
415
     */
416
    public ValueCollection getPKValue(long rowIndex) throws DriverException {
417
        return dataSource.getPKValue(rowIndex);
418
    }
419

    
420
    /**
421
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
422
     */
423
    public String getPKName(int fieldId) throws DriverException {
424
        return dataSource.getPKName(fieldId);
425
    }
426

    
427
    /**
428
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
429
     */
430
    public int getPKType(int i) throws DriverException {
431
        return dataSource.getPKType(i);
432
    }
433

    
434
    /**
435
     * @throws DriverException
436
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
437
     */
438
    public int getPKCardinality() throws DriverException {
439
        return dataSource.getPKCardinality();
440
    }
441

    
442
    /**
443
     * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
444
     */
445
    public Value[] getRow(long rowIndex) throws DriverException {
446
        return dataSource.getRow(rowIndex);
447
    }
448

    
449
    /**
450
     * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
451
     */
452
    public String[] getFieldNames() throws DriverException {
453
        return dataSource.getFieldNames();
454
    }
455

    
456
    /**
457
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
458
     */
459
    public String[] getPKNames() throws DriverException {
460
        return dataSource.getPKNames();
461
    }
462

    
463
        public void removeLinksSelectionListener() {
464
                selectionSupport.removeLinkSelectionListener();
465
        }
466
}