root / tags / v1_1_Build_1000 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / selection / LinkSelectionListener.java @ 11885
History | View | Annotate | Download (4.08 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.operations.selection; |
42 |
|
43 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
44 |
import com.hardcode.gdbms.engine.values.Value; |
45 |
|
46 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
47 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
48 |
import com.iver.cit.gvsig.fmap.layers.SelectionEvent; |
49 |
import com.iver.cit.gvsig.fmap.layers.SelectionListener; |
50 |
|
51 |
import java.util.HashMap; |
52 |
|
53 |
|
54 |
/**
|
55 |
* Listener que est? pendiente de la selecci?n que se haga sobre la tabla para
|
56 |
* poder seleccionar de la tabla linkada los registros oportunos.
|
57 |
*
|
58 |
* @author Vicente Caballero Navarro
|
59 |
*/
|
60 |
public class LinkSelectionListener implements SelectionListener { |
61 |
private SelectableDataSource modelo1;
|
62 |
private SelectableDataSource modelo2;
|
63 |
private int index1; |
64 |
private int index2; |
65 |
|
66 |
/**
|
67 |
* Crea un nuevo LinkSelectionListener. Donde se pasan como par?metros dos
|
68 |
* SelectableDataSource, el primero de ellos(model1), representa la tabla
|
69 |
* sobre la que al seleccionar alg?n registro buscaremos en el modelo2
|
70 |
* para ver el valor del campo s1 y compararlo con el valor del campo s2 y
|
71 |
* de esta forma seleccionar instantanemente los registros oportunos en la
|
72 |
* tabla al que se representa con el modelo2.
|
73 |
*
|
74 |
* @param model1 SelectableDataSource de la tabla sobre la que se efectua
|
75 |
* el link.
|
76 |
* @param model2 SelectableDataSource de la tabla linkada.
|
77 |
* @param s1 nombre del campo de la tabla donde se efectua el link.
|
78 |
* @param s2 nombre del campo de la tabla linkada.
|
79 |
*/
|
80 |
public LinkSelectionListener(SelectableDataSource model1,
|
81 |
SelectableDataSource model2, String s1, String s2) { |
82 |
modelo1 = model1; |
83 |
modelo2 = model2; |
84 |
|
85 |
try {
|
86 |
index1 = modelo1.getFieldIndexByName(s1); |
87 |
index2 = modelo2.getFieldIndexByName(s2); |
88 |
} catch (DriverException e) {
|
89 |
e.printStackTrace(); |
90 |
} |
91 |
|
92 |
//field1 = s1;
|
93 |
//field2 = s2;
|
94 |
} |
95 |
|
96 |
/**
|
97 |
* @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
|
98 |
*/
|
99 |
public void selectionChanged(SelectionEvent e) { |
100 |
FBitSet bs1 = modelo1.getSelection(); |
101 |
FBitSet bs2 = new FBitSet(); //(FBitSet)modelo2.getSelection().clone(); |
102 |
HashMap idx = new HashMap((int) bs1.cardinality()); |
103 |
//int index1 = -1;
|
104 |
//int index2 = -1;
|
105 |
|
106 |
try {
|
107 |
// Construimos el ?ndice
|
108 |
for (int i = bs1.nextSetBit(0); i >= 0; |
109 |
i = bs1.nextSetBit(i + 1)) {
|
110 |
Value v = modelo1.getFieldValue((long) i, index1);
|
111 |
|
112 |
if (idx.get(v) == null) { |
113 |
idx.put(v, new Integer(i)); |
114 |
} |
115 |
} |
116 |
} catch (DriverException e2) {
|
117 |
e2.printStackTrace(); |
118 |
} |
119 |
|
120 |
try {
|
121 |
for (int i = 0; i < modelo2.getRowCount(); i++) { |
122 |
Value v = modelo2.getFieldValue(i, index2); |
123 |
Integer pi = (Integer) idx.get(v); |
124 |
|
125 |
if (pi != null) { |
126 |
bs2.set(i); |
127 |
} |
128 |
} |
129 |
} catch (DriverException e1) {
|
130 |
e1.printStackTrace(); |
131 |
} |
132 |
|
133 |
// this applies the selection to the linked table
|
134 |
if (modelo1!=modelo2)
|
135 |
modelo2.setSelection(bs2); |
136 |
//modelo2.fireSelectionEvents();
|
137 |
} |
138 |
} |