Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap / src / org / gvsig / fmap / mapcontext / layers / LinkSelectionListener.java @ 20989

History | View | Annotate | Download (3.92 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 org.gvsig.fmap.mapcontext.layers;
42

    
43
import java.util.HashMap;
44

    
45
import org.gvsig.fmap.datasources.SelectableDataSource;
46

    
47
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
48
import com.hardcode.gdbms.engine.values.Value;
49

    
50

    
51
/**
52
 * Listener que est? pendiente de la selecci?n que se haga sobre la tabla para
53
 * poder seleccionar de la tabla linkada los registros oportunos.
54
 *
55
 * @author Vicente Caballero Navarro
56
 */
57
public class LinkSelectionListener implements SelectionListener {
58
        private SelectableDataSource modelo1;
59
        private SelectableDataSource modelo2;
60
        private int index1;
61
        private int index2;
62

    
63
        /**
64
         * Crea un nuevo LinkSelectionListener. Donde se pasan como par?metros dos
65
         * SelectableDataSource, el primero de ellos(model1), representa la tabla
66
         * sobre la que al seleccionar alg?n registro buscaremos en el modelo2
67
         * para ver el valor del campo s1 y compararlo con el valor del campo s2 y
68
         * de esta forma seleccionar instantanemente los registros oportunos en la
69
         * tabla al que se representa con el modelo2.
70
         *
71
         * @param model1 SelectableDataSource de la tabla sobre la que se efectua
72
         *                   el link.
73
         * @param model2 SelectableDataSource de la tabla linkada.
74
         * @param s1 nombre del campo de la tabla donde se efectua el link.
75
         * @param s2 nombre del campo de la tabla linkada.
76
         */
77
        public LinkSelectionListener(SelectableDataSource model1,
78
                SelectableDataSource model2, String s1, String s2) {
79
                modelo1 = model1;
80
                modelo2 = model2;
81

    
82
                try {
83
                        index1 = modelo1.getFieldIndexByName(s1);
84
                        index2 = modelo2.getFieldIndexByName(s2);
85
                } catch (ReadDriverException e) {
86
                        e.printStackTrace();
87
                }
88

    
89
                //field1 = s1;
90
                //field2 = s2;
91
        }
92

    
93
        /**
94
         * @see com.iver.cit.gvsig.fmap.layers.LegendListener#selectionChanged(com.iver.cit.gvsig.fmap.layers.LayerEvent)
95
         */
96
        public void selectionChanged(SelectionEvent e) {
97
                FBitSet bs1 = modelo1.getSelection();
98
                FBitSet bs2 = new FBitSet(); //(FBitSet)modelo2.getSelection().clone();
99
                HashMap idx = new HashMap(bs1.cardinality());
100
                //int index1 = -1;
101
                //int index2 = -1;
102

    
103
                try {
104
                        //                Construimos el ?ndice
105
                        for (int i = bs1.nextSetBit(0); i >= 0;
106
                                        i = bs1.nextSetBit(i + 1)) {
107
                                Value v = modelo1.getFieldValue((long) i, index1);
108

    
109
                                if (idx.get(v) == null) {
110
                                        idx.put(v, new Integer(i));
111
                                }
112
                        }
113
                } catch (ReadDriverException e2) {
114
                        e2.printStackTrace();
115
                }
116

    
117
                try {
118
                        for (int i = 0; i < modelo2.getRowCount(); i++) {
119
                                Value v = modelo2.getFieldValue(i, index2);
120
                                Integer pi = (Integer) idx.get(v);
121

    
122
                                if (pi != null) {
123
                                        bs2.set(i);
124
                                }
125
                        }
126
                } catch (ReadDriverException e1) {
127
                        e1.printStackTrace();
128
                }
129

    
130
                // this applies the selection to the linked table
131
                if (modelo1!=modelo2)
132
                        modelo2.setSelection(bs2);
133
                //modelo2.fireSelectionEvents();
134
        }
135
}