Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectionSupport.java @ 6220

History | View | Annotate | Download (4.61 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 com.iver.cit.gvsig.fmap.operations.selection.LinkSelectionListener;
44
import com.iver.utiles.XMLEntity;
45

    
46
import java.util.ArrayList;
47
import java.util.Iterator;
48

    
49

    
50
/**
51
 * Clase que gestiona las operaci?nes sobre la selecci?n.
52
 *
53
 * @author Vicente Caballero Navarro
54
 */
55
public class SelectionSupport {
56
        private FBitSet selection = new FBitSet();
57
        private ArrayList listeners = new ArrayList();
58

    
59
        /**
60
         * Inserta una nueva selecci?n.
61
         *
62
         * @param selection FBitSet con la selecci?n.
63
         */
64
        public void setSelection(FBitSet selection) {
65
                this.selection = selection;
66
                fireSelectionEvents();
67
        }
68

    
69
        /**
70
         * Devuelve un FBitSet con los ?ndices de los elementos seleccionados.
71
         *
72
         * @return FBitSet.
73
         */
74
        public FBitSet getSelection() {
75
                return selection;
76
        }
77

    
78
        /**
79
         * Devuelve true si el elemento est? seleccionado.
80
         *
81
         * @param recordIndex ?ndice del registro.
82
         *
83
         * @return True si est? seleccionado.
84
         */
85
        public boolean isSelected(int recordIndex) {
86
                return selection.get(recordIndex);
87
        }
88

    
89
        /**
90
         * Elimina la selecci?n.
91
         */
92
        public void clearSelection() {
93
            this.selection.clear();
94
            fireSelectionEvents();
95
    }
96

    
97
        /**
98
         * A?ade un SelectionListener al ArrayList de Listeners.
99
         *
100
         * @param listener SelectionListener a a?adir.
101
         */
102
        public void addSelectionListener(SelectionListener listener) {
103
                listeners.add(listener);
104
        }
105

    
106
        /**
107
         * Borra el SelectionListener que se le pasa como par?metro del ArrayList
108
         * de Listener.
109
         *
110
         * @param listener SlectionListener a borrar.
111
         */
112
        public void removeSelectionListener(SelectionListener listener) {
113
                listeners.remove(listener);
114
        }
115

    
116
        /**
117
         * Cuando ocurre un evento de cambio en la selecci?n, ?ste puede ser uno de
118
         * una gran cantidad de eventos. Con el fin de no propagar todos estos
119
         * eventos, se realiza la propagaci?n de manera manual al final de la
120
         * "r?faga" de eventos
121
         */
122
        public void fireSelectionEvents() {
123
                for (Iterator iter = listeners.iterator(); iter.hasNext();) {
124
                        SelectionListener listener = (SelectionListener) iter.next();
125

    
126
                        listener.selectionChanged(new SelectionEvent());
127
                }
128
        }
129

    
130
        /**
131
         * Devuelve el XMLEntity con la informaci?n necesaria para reproducir un
132
         * objeto igual al actual.
133
         *
134
         * @return XMLEntity.
135
         */
136
        public XMLEntity getXMLEntity() {
137
                XMLEntity xml = new XMLEntity();
138
                xml.putProperty("className",this.getClass().getName());
139

    
140
                if (selection != null) {
141
                        xml.putProperty("numBitSet", selection.cardinality());
142

    
143
                        int n = 0;
144

    
145
                        for (int i = 0; i < selection.length(); i++) {
146
                                if (selection.get(i)) {
147
                                        xml.putProperty(String.valueOf(n), i);
148
                                        n++;
149
                                }
150
                        }
151
                }
152

    
153
                return xml;
154
        }
155

    
156
        /**
157
         * A partir del XMLEntity reproduce la selecci?n.
158
         *
159
         * @param xml DOCUMENT ME!
160
         */
161
        public void setXMLEntity(XMLEntity xml) {
162
                int numBitSet = xml.getIntProperty("numBitSet");
163

    
164
                if (numBitSet != 0) {
165
                        for (int i = 0; i < numBitSet; i++) {
166
                                selection.set(xml.getIntProperty(String.valueOf(i)));
167
                        }
168
                }
169
        }
170

    
171
        /**
172
         * A partir del XMLEntity reproduce la selecci?n.
173
         *
174
         * @param xml DOCUMENT ME!
175
         */
176
        public void setXMLEntity03(XMLEntity xml) {
177
                int numBitSet = xml.getIntProperty("numBitSet");
178

    
179
                if (numBitSet != 0) {
180
                        for (int i = 0; i < numBitSet; i++) {
181
                                selection.set(xml.getIntProperty(String.valueOf(i)));
182
                        }
183
                }
184
        }
185

    
186
        public void removeLinkSelectionListener() {
187
                for (int i=0;i<listeners.size();i++){
188
                        if (listeners.get(i) instanceof LinkSelectionListener)
189
                        listeners.remove(listeners.get(i));
190
                }
191
        }
192
}