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