Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1003 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / SelectionSupport.java @ 12271

History | View | Annotate | Download (4.65 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
                if (!listeners.contains(listener))
104
                        listeners.add(listener);
105
        }
106

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

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

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

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

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

    
144
                        int n = 0;
145

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

    
154
                return xml;
155
        }
156

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

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

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

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

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