Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / FeatureSelectionModelNew.java @ 33378

History | View | Annotate | Download (9.04 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 {}  {{Task}}
26
 */
27
package org.gvsig.fmap.mapcontrol.dal.feature.swing;
28

    
29
import javax.swing.ListSelectionModel;
30
import javax.swing.event.EventListenerList;
31
import javax.swing.event.ListSelectionEvent;
32
import javax.swing.event.ListSelectionListener;
33

    
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.Feature;
36
import org.gvsig.fmap.dal.feature.FeatureSelection;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
39
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTableModel;
40
import org.gvsig.tools.observer.Observable;
41
import org.gvsig.tools.observer.Observer;
42

    
43
/**
44
 * @author 2010- C?sar Ordi?ana - gvSIG team
45
 */
46
public class FeatureSelectionModelNew implements ListSelectionModel, Observer {
47

    
48
        protected EventListenerList listenerList = new EventListenerList();
49

    
50
        private final FeatureTableModel featureTableModel;
51

    
52
        private int selectionMode = SINGLE_INTERVAL_SELECTION;
53

    
54
        private boolean isAdjusting = false;
55

    
56
        private int anchor = -1;
57

    
58
        private int lead = -1;
59

    
60
        /**
61
         * Creates a new {@link FeatureSelectionModelNew} with a
62
         * {@link FeatureTableModel} used to get the {@link Feature}s by position in
63
         * the table.
64
         * 
65
         * @param featureTableModel
66
         *            to get Features from
67
         * @throws DataException
68
         *             if there is an error getting the store selection
69
         */
70
        public FeatureSelectionModelNew(FeatureTableModel featureTableModel)
71
                        throws DataException {
72
                this.featureTableModel = featureTableModel;
73
                this.featureTableModel.getFeatureStore().addObserver(this);
74
        }
75

    
76
        public int getAnchorSelectionIndex() {
77
                return anchor;
78
        }
79

    
80
        public int getLeadSelectionIndex() {
81
                return lead;
82
        }
83

    
84
        public int getMaxSelectionIndex() {
85
                FeatureSelection selection = getFeatureSelection();
86
                try {
87
                        if (!selection.isEmpty()) {
88
                                for (int i = featureTableModel.getRowCount() - 1; i >= 0; i--) {
89
                                        if (selection.isSelected(featureTableModel.getFeatureAt(i))) {
90
                                                return i;
91
                                        }
92
                                }
93
                        }
94
                } catch (DataException e) {
95
                        throw new SelectionChangeException(e);
96
                }
97
                return -1;
98
        }
99

    
100
        public int getMinSelectionIndex() {
101
                FeatureSelection selection = getFeatureSelection();
102
                try {
103
                        if (!selection.isEmpty()) {
104
                                for (int i = 0; i < featureTableModel.getRowCount(); i++) {
105
                                        if (selection.isSelected(featureTableModel.getFeatureAt(i))) {
106
                                                return i;
107
                                        }
108
                                }
109
                        }
110
                } catch (DataException e) {
111
                        throw new SelectionChangeException(e);
112
                }
113
                return -1;
114
        }
115

    
116
        public void insertIndexInterval(int index, int length, boolean before) {
117
                // Nothing to do
118
        }
119

    
120
        public void removeIndexInterval(int index0, int index1) {
121
                // Nothing to do
122
        }
123

    
124
        public void setAnchorSelectionIndex(int index) {
125
                this.anchor = index;
126
        }
127

    
128
        public void setLeadSelectionIndex(int index) {
129
                this.lead = index;
130
        }
131

    
132
        public void addSelectionInterval(int index0, int index1) {
133
                doWithSelection(new FeatureSelectionOperation() {
134

    
135
                        public void doWithSelection(FeatureSelection selection, int first,
136
                                        int last) throws DataException {
137
                                for (int i = first; i <= last; i++) {
138
                                        Feature feature = getFeature(i);
139
                                        if (!selection.isSelected(feature)) {
140
                                                selection.select(feature);
141
                                        }
142
                                }
143
                        }
144

    
145
                }, index0, index1, true);
146
        }
147

    
148
        public void setSelectionInterval(int index0, int index1) {
149
                doWithSelection(new FeatureSelectionOperation() {
150

    
151
                        public void doWithSelection(FeatureSelection selection, int first,
152
                                        int last) throws DataException {
153
                                selection.deselectAll();
154
                                for (int i = first; i <= last; i++) {
155
                                        Feature feature = getFeature(i);
156
                                        selection.select(feature);
157
                                }
158
                        }
159

    
160
                }, index0, index1, true);
161
        }
162

    
163
        public void removeSelectionInterval(int index0, int index1) {
164
                doWithSelection(new FeatureSelectionOperation() {
165

    
166
                        public void doWithSelection(FeatureSelection selection, int first,
167
                                        int last) throws DataException {
168
                                for (int i = first; i <= last; i++) {
169
                                        Feature feature = getFeature(i);
170
                                        if (selection.isSelected(feature)) {
171
                                                selection.deselect(feature);
172
                                        }
173
                                }
174
                        }
175

    
176
                }, index0, index1, false);
177
        }
178

    
179
        public void clearSelection() {
180
                try {
181
                        getFeatureSelection().deselectAll();
182
                } catch (DataException e) {
183
                        throw new SelectionChangeException(e);
184
                }
185
        }
186

    
187
        public boolean isSelectedIndex(int index) {
188
                if (index == -1) {
189
                        return false;
190
                }
191
                Feature feature = featureTableModel.getFeatureAt(index);
192
                return getFeatureSelection().isSelected(feature);
193
        }
194

    
195
        public boolean isSelectionEmpty() {
196
                try {
197
                        return getFeatureSelection().isEmpty();
198
                } catch (DataException ex) {
199
                        throw new SelectionChangeException(ex);
200
                }
201
        }
202

    
203
        public boolean getValueIsAdjusting() {
204
                return isAdjusting;
205
        }
206

    
207
        public void setValueIsAdjusting(boolean valueIsAdjusting) {
208
                if (this.isAdjusting != valueIsAdjusting) {
209
                        this.isAdjusting = valueIsAdjusting;
210
                        if (this.isAdjusting) {
211
                                getFeatureSelection().beginComplexNotification();
212
                        } else {
213
                                getFeatureSelection().endComplexNotification();
214
                        }
215
                }
216
        }
217

    
218
        public int getSelectionMode() {
219
                return selectionMode;
220
        }
221

    
222
        public void setSelectionMode(int selectionMode) {
223
                this.selectionMode = selectionMode;
224
        }
225

    
226
        public void addListSelectionListener(ListSelectionListener listener) {
227
                listenerList.add(ListSelectionListener.class, listener);
228
        }
229

    
230
        public void removeListSelectionListener(ListSelectionListener listener) {
231
                listenerList.remove(ListSelectionListener.class, listener);
232
        }
233

    
234
        public void update(Observable observable, Object notification) {
235
                if (notification instanceof FeatureStoreNotification) {
236
                        FeatureStoreNotification fnotification =
237
                                        (FeatureStoreNotification) notification;
238
                        if (!fnotification.getSource().equals(getFeatureStore())) {
239
                                return;
240
                        }
241
                        if (FeatureStoreNotification.SELECTION_CHANGE.equals(fnotification.getType())) {
242
                                fireValueChanged(-1, -1, false);
243
                        }
244
                }
245
        }
246

    
247
        private FeatureSelection getFeatureSelection() {
248
                try {
249
                        return (FeatureSelection) getFeatureStore().getSelection();
250
                } catch (DataException ex) {
251
                        throw new SelectionChangeException(ex);
252
                }
253
        }
254

    
255
        /**
256
         * @param operation
257
         * @param index0
258
         * @param index1
259
         */
260
        private void doWithSelection(FeatureSelectionOperation operation,
261
                        int index0, int index1, boolean select) {
262
                // Set the anchor and lead
263
                anchor = index0;
264
                lead = index1;
265

    
266
                // As index0 <= index1 is no guaranteed, calculate the first and second
267
                // values
268
                int first = (index0 <= index1) ? index0 : index1;
269
                int last = (index0 <= index1) ? index1 : index0;
270

    
271
                FeatureSelection selection = getFeatureSelection();
272

    
273
                // Perform the selection operation into a complex notification
274
                selection.beginComplexNotification();
275
                try {
276
                        // Is a full select or deselect
277
                        if (first == 00 && last == featureTableModel.getRowCount() - 1) {
278
                                if (select) {
279
                                        selection.selectAll();
280
                                } else {
281
                                        selection.deselectAll();
282
                                }
283
                        } else {
284
                                operation.doWithSelection(selection, first, last);
285
                        }
286
                } catch (DataException e) {
287
                        throw new SelectionChangeException(e);
288
                } finally {
289
                        selection.endComplexNotification();
290
                }
291

    
292
                fireValueChanged(first, last, isAdjusting);
293
        }
294

    
295
        /**
296
         * Returns a Feature by table row position.
297
         */
298
        private Feature getFeature(int index) {
299
                return featureTableModel.getFeatureAt(index);
300
        }
301

    
302
        /**
303
         * Returns the FeatureStore.
304
         */
305
        private FeatureStore getFeatureStore() {
306
                return featureTableModel.getFeatureStore();
307
        }
308

    
309
        /**
310
         * @param firstIndex
311
         *            the first index in the interval
312
         * @param lastIndex
313
         *            the last index in the interval
314
         * @param isAdjusting
315
         *            true if this is the final change in a series of adjustments
316
         * @see EventListenerList
317
         */
318
        protected void fireValueChanged(int firstIndex, int lastIndex,
319
                        boolean isAdjusting) {
320
                Object[] listeners = listenerList.getListenerList();
321
                ListSelectionEvent e = null;
322

    
323
                for (int i = listeners.length - 2; i >= 0; i -= 2) {
324
                        if (listeners[i] == ListSelectionListener.class) {
325
                                if (e == null) {
326
                                        e =
327
                                                        new ListSelectionEvent(this, firstIndex, lastIndex,
328
                                                                        isAdjusting);
329
                                }
330
                                ((ListSelectionListener) listeners[i + 1]).valueChanged(e);
331
                        }
332
                }
333
        }
334

    
335
        private interface FeatureSelectionOperation {
336
                void doWithSelection(FeatureSelection selection, int first, int last)
337
                                throws DataException;
338
        }
339
}