root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectableDataSource.java @ 2859
History | View | Annotate | Download (11.5 KB)
1 | 1100 | fjp | /* 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 | 460 | fernando | package com.iver.cit.gvsig.fmap.layers; |
42 | |||
43 | 1828 | fernando | import java.io.IOException; |
44 | 2565 | fernando | import java.util.BitSet; |
45 | 1828 | fernando | |
46 | import org.apache.log4j.Logger; |
||
47 | 1836 | fernando | import org.xml.sax.SAXException; |
48 | 1828 | fernando | |
49 | import com.hardcode.driverManager.DriverLoadException; |
||
50 | 460 | fernando | import com.hardcode.gdbms.engine.data.DataSource; |
51 | 546 | fernando | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
52 | import com.hardcode.gdbms.engine.data.NoSuchTableException; |
||
53 | 1828 | fernando | import com.hardcode.gdbms.engine.data.driver.DriverException; |
54 | 2217 | fernando | import com.hardcode.gdbms.engine.data.edition.DataWare; |
55 | import com.hardcode.gdbms.engine.data.SourceInfo; |
||
56 | 1836 | fernando | import com.hardcode.gdbms.engine.data.persistence.Memento; |
57 | import com.hardcode.gdbms.engine.data.persistence.MementoContentHandler; |
||
58 | import com.hardcode.gdbms.engine.data.persistence.MementoException; |
||
59 | 2217 | fernando | import com.hardcode.gdbms.engine.instruction.EvaluationException; |
60 | 460 | fernando | import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
61 | 1828 | fernando | import com.hardcode.gdbms.engine.instruction.SemanticException; |
62 | 460 | fernando | import com.hardcode.gdbms.engine.values.Value; |
63 | 2183 | fernando | import com.hardcode.gdbms.engine.values.ValueCollection; |
64 | 1828 | fernando | import com.hardcode.gdbms.parser.ParseException; |
65 | 581 | vcaballero | import com.iver.utiles.XMLEntity; |
66 | 460 | fernando | |
67 | |||
68 | 527 | vcaballero | /**
|
69 | 1034 | vcaballero | * DataSource seleccionable.
|
70 | 527 | vcaballero | *
|
71 | 546 | fernando | * @author Fernando Gonz?lez Cort?s
|
72 | 527 | vcaballero | */
|
73 | public class SelectableDataSource implements DataSource { |
||
74 | private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName()); |
||
75 | 460 | fernando | private SelectionSupport selectionSupport = new SelectionSupport(); |
76 | private DataSource dataSource; |
||
77 | 527 | vcaballero | |
78 | 460 | fernando | /**
|
79 | 1034 | vcaballero | * Crea un nuevo SelectableDataSource.
|
80 | 527 | vcaballero | *
|
81 | 1034 | vcaballero | * @param name
|
82 | 460 | fernando | * @param ds
|
83 | 2565 | fernando | * @throws DriverException
|
84 | 460 | fernando | */
|
85 | 2565 | fernando | public SelectableDataSource(DataSource ds) throws DriverException { |
86 | 460 | fernando | dataSource = ds; |
87 | 2565 | fernando | |
88 | 460 | fernando | } |
89 | 527 | vcaballero | |
90 | 1836 | fernando | public static SelectableDataSource createSelectableDataSource(XMLEntity xml) throws NoSuchTableException, ParseException, DriverLoadException, DriverException, SemanticException, IOException, XMLException{ |
91 | 1828 | fernando | |
92 | SelectionSupport ss = new SelectionSupport();
|
||
93 | ss.setXMLEntity(xml.getChild(0));
|
||
94 | XMLEntity xmlDS = xml.getChild(1);
|
||
95 | 1836 | fernando | GDBMSParser parser = new GDBMSParser(xmlDS);
|
96 | MementoContentHandler gdbmsHandler = new MementoContentHandler();
|
||
97 | parser.setContentHandler(gdbmsHandler); |
||
98 | try {
|
||
99 | parser.parse(); |
||
100 | } catch (SAXException e) {
|
||
101 | throw new XMLException(e); |
||
102 | 1828 | fernando | } |
103 | 2217 | fernando | SelectableDataSource sds; |
104 | try {
|
||
105 | 2667 | fernando | sds = new SelectableDataSource(gdbmsHandler.getDataSource(LayerFactory.getDataSourceFactory(), DataSourceFactory.AUTOMATIC_OPENING));
|
106 | 2217 | fernando | } catch (EvaluationException e1) {
|
107 | throw new XMLException(e1); |
||
108 | } |
||
109 | sds.selectionSupport=ss; |
||
110 | 2183 | fernando | return sds;
|
111 | 1828 | fernando | } |
112 | 1836 | fernando | |
113 | 1828 | fernando | public void setDataSourceFactory(DataSourceFactory dsf) { |
114 | dataSource.setDataSourceFactory(dsf); |
||
115 | } |
||
116 | 2217 | fernando | public void setSourceInfo(SourceInfo sourceInfo) { |
117 | 1828 | fernando | dataSource.setSourceInfo(sourceInfo); |
118 | } |
||
119 | 460 | fernando | /**
|
120 | 1034 | vcaballero | * A?ade el soporte para la selecci?n.
|
121 | 527 | vcaballero | *
|
122 | 460 | fernando | * @param selectionSupport
|
123 | */
|
||
124 | public void setSelectionSupport(SelectionSupport selectionSupport) { |
||
125 | this.selectionSupport = selectionSupport;
|
||
126 | } |
||
127 | |||
128 | 527 | vcaballero | /**
|
129 | 1034 | vcaballero | * Devuelve el n?mero de campos.
|
130 | 527 | vcaballero | *
|
131 | 1034 | vcaballero | * @return N?mero de campos.
|
132 | 527 | vcaballero | *
|
133 | 1034 | vcaballero | * @throws DriverException
|
134 | 527 | vcaballero | */
|
135 | 460 | fernando | public int getFieldCount() throws DriverException { |
136 | return dataSource.getFieldCount();
|
||
137 | } |
||
138 | 527 | vcaballero | |
139 | /**
|
||
140 | 1034 | vcaballero | * Devuelve el ?ndice del campo a partir de su nombre.
|
141 | 527 | vcaballero | *
|
142 | 1034 | vcaballero | * @param arg0 nombre del campo.
|
143 | 527 | vcaballero | *
|
144 | 1034 | vcaballero | * @return ?ndice.
|
145 | 527 | vcaballero | *
|
146 | 1034 | vcaballero | * @throws DriverException
|
147 | * @throws FieldNotFoundException
|
||
148 | 527 | vcaballero | */
|
149 | public int getFieldIndexByName(String arg0) |
||
150 | 1773 | fernando | throws DriverException {
|
151 | 460 | fernando | return dataSource.getFieldIndexByName(arg0);
|
152 | } |
||
153 | 527 | vcaballero | |
154 | /**
|
||
155 | 1034 | vcaballero | * Devuelve el nombre del campo a partir del ?ndice.
|
156 | 527 | vcaballero | *
|
157 | 1034 | vcaballero | * @param arg0 ?ndice.
|
158 | 527 | vcaballero | *
|
159 | 1034 | vcaballero | * @return nombre del campo.
|
160 | 527 | vcaballero | *
|
161 | 1034 | vcaballero | * @throws DriverException
|
162 | 527 | vcaballero | */
|
163 | 460 | fernando | public String getFieldName(int arg0) throws DriverException { |
164 | 2565 | fernando | return dataSource.getFieldName(arg0);
|
165 | 460 | fernando | } |
166 | 527 | vcaballero | |
167 | /**
|
||
168 | 1034 | vcaballero | * Devuelve el valor a partir del n?mro de fila y columna.
|
169 | 527 | vcaballero | *
|
170 | 1034 | vcaballero | * @param arg0 n?mero de registro.
|
171 | * @param arg1 n?mero de campo.
|
||
172 | 527 | vcaballero | *
|
173 | 1034 | vcaballero | * @return Valor.
|
174 | 527 | vcaballero | *
|
175 | 1034 | vcaballero | * @throws DriverException
|
176 | 527 | vcaballero | */
|
177 | 460 | fernando | public Value getFieldValue(long arg0, int arg1) throws DriverException { |
178 | return dataSource.getFieldValue(arg0, arg1);
|
||
179 | } |
||
180 | 527 | vcaballero | |
181 | /**
|
||
182 | 1034 | vcaballero | * Devuelve el nombre del DataSource.
|
183 | 527 | vcaballero | *
|
184 | 1034 | vcaballero | * @return Nombre.
|
185 | 527 | vcaballero | */
|
186 | 460 | fernando | public String getName() { |
187 | return dataSource.getName();
|
||
188 | } |
||
189 | 527 | vcaballero | |
190 | /**
|
||
191 | 2565 | fernando | * Devuelve el n?mero de filas en total.
|
192 | 527 | vcaballero | *
|
193 | 1034 | vcaballero | * @return n?mero de filas.
|
194 | 527 | vcaballero | *
|
195 | 1034 | vcaballero | * @throws DriverException
|
196 | 527 | vcaballero | */
|
197 | 460 | fernando | public long getRowCount() throws DriverException { |
198 | return dataSource.getRowCount();
|
||
199 | } |
||
200 | 527 | vcaballero | |
201 | /**
|
||
202 | 1034 | vcaballero | * Inicializa el dataSource.
|
203 | 527 | vcaballero | *
|
204 | 1034 | vcaballero | * @throws DriverException
|
205 | 527 | vcaballero | */
|
206 | 460 | fernando | public void start() throws DriverException { |
207 | 527 | vcaballero | logger.debug("dataSource.start()");
|
208 | 460 | fernando | dataSource.start(); |
209 | } |
||
210 | 527 | vcaballero | |
211 | /**
|
||
212 | 1034 | vcaballero | * Finaliza el DataSource.
|
213 | 527 | vcaballero | *
|
214 | 1034 | vcaballero | * @throws DriverException
|
215 | 527 | vcaballero | */
|
216 | 460 | fernando | public void stop() throws DriverException { |
217 | 527 | vcaballero | logger.debug("dataSource.stop()");
|
218 | 460 | fernando | dataSource.stop(); |
219 | } |
||
220 | |||
221 | /**
|
||
222 | 2183 | fernando | * A partir del XMLEntity se rellenan los atributos del DataSource.
|
223 | *
|
||
224 | * @param child
|
||
225 | */
|
||
226 | public void setXMLEntity03(XMLEntity child) { |
||
227 | selectionSupport.setXMLEntity(child.getChild(0));
|
||
228 | } |
||
229 | |||
230 | /**
|
||
231 | 527 | vcaballero | * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
|
232 | * una gran cantidad de eventos. Con el fin de no propagar todos estos
|
||
233 | * eventos, se realiza la propagaci?n de manera manual al final de la
|
||
234 | * "r?faga" de eventos
|
||
235 | 460 | fernando | */
|
236 | public void fireSelectionEvents() { |
||
237 | selectionSupport.fireSelectionEvents(); |
||
238 | } |
||
239 | |||
240 | 527 | vcaballero | /**
|
241 | 1034 | vcaballero | * A?ade un nuevo Listener al SelectionSupport.
|
242 | 527 | vcaballero | *
|
243 | 1034 | vcaballero | * @param listener SelectionListener.
|
244 | 527 | vcaballero | */
|
245 | 460 | fernando | public void addSelectionListener(SelectionListener listener) { |
246 | selectionSupport.addSelectionListener(listener); |
||
247 | } |
||
248 | |||
249 | 527 | vcaballero | /**
|
250 | 1034 | vcaballero | * Borra un Listener al SelectionSupport.
|
251 | 527 | vcaballero | *
|
252 | 1034 | vcaballero | * @param listener Listener a borrar.
|
253 | 527 | vcaballero | */
|
254 | 460 | fernando | public void removeSelectionListener(SelectionListener listener) { |
255 | selectionSupport.removeSelectionListener(listener); |
||
256 | } |
||
257 | 527 | vcaballero | |
258 | /**
|
||
259 | 1034 | vcaballero | * Borra la selecci?n.
|
260 | 527 | vcaballero | */
|
261 | 460 | fernando | public void clearSelection() { |
262 | selectionSupport.clearSelection(); |
||
263 | } |
||
264 | 527 | vcaballero | |
265 | /**
|
||
266 | 1034 | vcaballero | * Develve un FBitSet con los ?ndices de los elementos seleccionados.
|
267 | 527 | vcaballero | *
|
268 | 1034 | vcaballero | * @return FBitset con los elementos seleccionados.
|
269 | 527 | vcaballero | */
|
270 | 884 | fernando | public FBitSet getSelection() {
|
271 | 460 | fernando | return selectionSupport.getSelection();
|
272 | } |
||
273 | 1034 | vcaballero | |
274 | /**
|
||
275 | * Devuelve el SelectionSupport.
|
||
276 | *
|
||
277 | * @return SelectinSuport.
|
||
278 | */
|
||
279 | public SelectionSupport getSelectionSupport() {
|
||
280 | 581 | vcaballero | return selectionSupport;
|
281 | } |
||
282 | 1034 | vcaballero | |
283 | 527 | vcaballero | /**
|
284 | 1034 | vcaballero | * Devuelve true si el elemento est? seleccionado.
|
285 | 527 | vcaballero | *
|
286 | 1034 | vcaballero | * @param recordIndex ?ndice del registro.
|
287 | 527 | vcaballero | *
|
288 | 1034 | vcaballero | * @return True si el registro est? seleccionado.
|
289 | 527 | vcaballero | */
|
290 | 460 | fernando | public boolean isSelected(int recordIndex) { |
291 | return selectionSupport.isSelected(recordIndex);
|
||
292 | } |
||
293 | 527 | vcaballero | |
294 | /**
|
||
295 | 1034 | vcaballero | * Inserta una nueva selecci?n.
|
296 | 527 | vcaballero | *
|
297 | 1034 | vcaballero | * @param selection FBitSet.
|
298 | 527 | vcaballero | */
|
299 | 683 | fernando | public void setSelection(FBitSet selection) { |
300 | 460 | fernando | selectionSupport.setSelection(selection); |
301 | } |
||
302 | 546 | fernando | |
303 | 1828 | fernando | private void putMemento(XMLEntity xml) throws XMLException { |
304 | try {
|
||
305 | 1836 | fernando | GDBMSHandler handler = new GDBMSHandler();
|
306 | Memento m = getMemento(); |
||
307 | m.setContentHandler(handler); |
||
308 | m.getXML(); |
||
309 | XMLEntity child = handler.getXMLEntity(); |
||
310 | 1828 | fernando | |
311 | xml.addChild(child); |
||
312 | } catch (MementoException e) {
|
||
313 | throw new XMLException(e); |
||
314 | 1836 | fernando | } catch (SAXException e) {
|
315 | throw new XMLException(e); |
||
316 | 1828 | fernando | } |
317 | } |
||
318 | |||
319 | 1034 | vcaballero | /**
|
320 | * Devuelve el XMLEntity con la informaci?n necesaria para reproducir el
|
||
321 | * DataSource.
|
||
322 | *
|
||
323 | * @return XMLEntity.
|
||
324 | 1828 | fernando | * @throws XMLException
|
325 | 1034 | vcaballero | */
|
326 | 1828 | fernando | public XMLEntity getXMLEntity() throws XMLException { |
327 | 1034 | vcaballero | XMLEntity xml = new XMLEntity();
|
328 | 1094 | vcaballero | xml.putProperty("className",this.getClass().getName()); |
329 | 581 | vcaballero | xml.addChild(selectionSupport.getXMLEntity()); |
330 | 1828 | fernando | putMemento(xml); |
331 | 1034 | vcaballero | |
332 | 581 | vcaballero | return xml;
|
333 | } |
||
334 | 732 | fernando | |
335 | /**
|
||
336 | 884 | fernando | * @see com.hardcode.gdbms.engine.data.DataSource#getWhereFilter()
|
337 | */
|
||
338 | public long[] getWhereFilter() throws IOException { |
||
339 | return dataSource.getWhereFilter();
|
||
340 | } |
||
341 | 1653 | fernando | |
342 | /**
|
||
343 | * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldType(int)
|
||
344 | */
|
||
345 | 1773 | fernando | public int getFieldType(int i) throws DriverException { |
346 | 1653 | fernando | return dataSource.getFieldType(i);
|
347 | } |
||
348 | 1828 | fernando | |
349 | /**
|
||
350 | * @see com.hardcode.gdbms.engine.data.DataSource#getDataSourceFactory()
|
||
351 | */
|
||
352 | public DataSourceFactory getDataSourceFactory() {
|
||
353 | return dataSource.getDataSourceFactory();
|
||
354 | } |
||
355 | |||
356 | /**
|
||
357 | * @see com.hardcode.gdbms.engine.data.DataSource#getAsString()
|
||
358 | */
|
||
359 | public String getAsString() throws DriverException { |
||
360 | return dataSource.getAsString();
|
||
361 | } |
||
362 | 1831 | fernando | |
363 | /**
|
||
364 | 2183 | fernando | * @throws DriverException
|
365 | 1831 | fernando | * @see com.hardcode.gdbms.engine.data.DataSource#remove()
|
366 | */
|
||
367 | 2183 | fernando | public void remove() throws DriverException { |
368 | 1831 | fernando | dataSource.remove(); |
369 | } |
||
370 | 1836 | fernando | |
371 | /**
|
||
372 | * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
|
||
373 | */
|
||
374 | public Memento getMemento() throws MementoException { |
||
375 | return dataSource.getMemento();
|
||
376 | } |
||
377 | |||
378 | /**
|
||
379 | * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
|
||
380 | */
|
||
381 | 2217 | fernando | public SourceInfo getSourceInfo() {
|
382 | 1836 | fernando | return dataSource.getSourceInfo();
|
383 | } |
||
384 | 2183 | fernando | |
385 | /**
|
||
386 | * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
|
||
387 | */
|
||
388 | public int[] getPrimaryKeys() throws DriverException { |
||
389 | return dataSource.getPrimaryKeys();
|
||
390 | } |
||
391 | |||
392 | /**
|
||
393 | * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
|
||
394 | */
|
||
395 | public ValueCollection getPKValue(long rowIndex) throws DriverException { |
||
396 | return dataSource.getPKValue(rowIndex);
|
||
397 | } |
||
398 | |||
399 | /**
|
||
400 | * @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
|
||
401 | */
|
||
402 | public String getPKName(int fieldId) throws DriverException { |
||
403 | return dataSource.getPKName(fieldId);
|
||
404 | } |
||
405 | |||
406 | /**
|
||
407 | * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
|
||
408 | */
|
||
409 | public int getPKType(int i) throws DriverException { |
||
410 | return dataSource.getPKType(i);
|
||
411 | } |
||
412 | |||
413 | /**
|
||
414 | * @throws DriverException
|
||
415 | * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
|
||
416 | */
|
||
417 | public int getPKCardinality() throws DriverException { |
||
418 | return dataSource.getPKCardinality();
|
||
419 | } |
||
420 | |||
421 | /**
|
||
422 | * @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
|
||
423 | */
|
||
424 | public Value[] getRow(long rowIndex) throws DriverException { |
||
425 | return dataSource.getRow(rowIndex);
|
||
426 | } |
||
427 | |||
428 | /**
|
||
429 | * @see com.hardcode.gdbms.engine.data.DataSource#getFieldNames()
|
||
430 | */
|
||
431 | public String[] getFieldNames() throws DriverException { |
||
432 | return dataSource.getFieldNames();
|
||
433 | } |
||
434 | |||
435 | /**
|
||
436 | * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
|
||
437 | */
|
||
438 | public String[] getPKNames() throws DriverException { |
||
439 | return dataSource.getPKNames();
|
||
440 | } |
||
441 | |||
442 | public void removeLinksSelectionListener() { |
||
443 | selectionSupport.removeLinkSelectionListener(); |
||
444 | } |
||
445 | 2217 | fernando | |
446 | /**
|
||
447 | * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
|
||
448 | */
|
||
449 | public DataWare getDataWare(int arg0) { |
||
450 | return dataSource.getDataWare(arg0);
|
||
451 | } |
||
452 | 460 | fernando | } |