Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretable / FeatureSelectionModel.java @ 42807

History | View | Annotate | Download (13.1 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2010 {}  {{Task}}
27
 */
28
package org.gvsig.fmap.dal.swing.impl.featuretable;
29

    
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import javax.swing.ListSelectionModel;
33
import javax.swing.event.EventListenerList;
34
import javax.swing.event.ListSelectionEvent;
35
import javax.swing.event.ListSelectionListener;
36

    
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.feature.Feature;
39
import org.gvsig.fmap.dal.feature.FeatureQuery;
40
import org.gvsig.fmap.dal.feature.FeatureSelection;
41
import org.gvsig.fmap.dal.feature.FeatureSet;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
44
import org.gvsig.fmap.dal.feature.exception.ConcurrentDataModificationException;
45
import org.gvsig.fmap.dal.swing.FeatureTableModel;
46
import org.gvsig.tools.dispose.DisposableIterator;
47
import org.gvsig.tools.observer.Observable;
48
import org.gvsig.tools.observer.Observer;
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

    
52
/**
53
 * @author 2010- C?sar Ordi?ana - gvSIG team
54
 */
55
public class FeatureSelectionModel implements ListSelectionModel, Observer {
56
    private static final Logger LOG = LoggerFactory.getLogger(FeatureSelectionModel.class);
57

    
58
        protected EventListenerList listenerList = new EventListenerList();
59

    
60
        private final FeatureTableModel featureTableModel;
61

    
62
        private int selectionMode = SINGLE_INTERVAL_SELECTION;
63

    
64
        private boolean isAdjusting = false;
65

    
66
        private int anchor = -1;
67

    
68
        private int lead = -1;
69

    
70
        private int currentFirst = -1;
71
        private int currentLast = -1;
72

    
73
        /**
74
         * Creates a new {@link FeatureSelectionModel} with a
75
         * {@link FeatureTableModel} used to get the {@link Feature}s by position in
76
         * the table.
77
         *
78
         * @param featureTableModel
79
         *            to get Features from
80
         * @throws DataException
81
         *             if there is an error getting the store selection
82
         */
83
        public FeatureSelectionModel(FeatureTableModel featureTableModel)  {
84
                this.featureTableModel = featureTableModel;
85
                this.featureTableModel.getFeatureSelection().addObserver(this);
86
                this.featureTableModel.addChangeListener(new ActionListener() {
87

    
88
                    @Override
89
                    public void actionPerformed(ActionEvent e) {
90
                        if( FeatureTableModel.CHANGE_SELECTION.equals(e.getActionCommand()) ) {
91
                            featureSelectionChanged();
92
                        }
93
                    }
94
                });
95
        }
96
        
97
        private void featureSelectionChanged() {
98
            this.featureTableModel.getFeatureSelection().addObserver(this);
99
        }
100
        
101
    @Override
102
        public int getAnchorSelectionIndex() {
103
                return anchor;
104
        }
105

    
106
    @Override
107
        public int getLeadSelectionIndex() {
108
                return lead;
109
        }
110

    
111
    @Override
112
        public int getMaxSelectionIndex() {
113

    
114
            int resp = this.getSelectionIndex(true);
115
            return resp;
116
            /*
117
             *
118
             * The call to "featureTableModel.getFeatureAt(i)"
119
             * causes a lot of reloadPage all over
120
             *
121
                FeatureSelection selection = getFeatureSelection();
122
                try {
123
                        if (!selection.isEmpty()) {
124
                                for (int i = featureTableModel.getRowCount() - 1; i >= 0; i--) {
125
                                        if (selection.isSelected(featureTableModel.getFeatureAt(i))) {
126
                                                return i;
127
                                        }
128
                                }
129
                        }
130
                } catch (DataException e) {
131
                        throw new SelectionChangeException(e);
132
                }
133
                return -1;
134
                */
135
        }
136

    
137
    @Override
138
        public int getMinSelectionIndex() {
139

    
140
            int resp = this.getSelectionIndex(false);
141
            return resp;
142

    
143
            /*
144
             *
145
         * The call to "featureTableModel.getFeatureAt(i)"
146
         * causes a lot of reloadPage all over
147
             *
148
                try {
149

150
                    int ii = 0;
151

152
                    FeatureSelection selection = this.getFeatureSelection();
153
            for (int i = 0; i < featureTableModel.getRowCount(); i++) {
154
                if (selection.isSelected(featureTableModel.getFeatureAt(i))) {
155
                    ii = i;
156
                }
157
            }
158
                } catch (Exception e) {
159
                        throw new SelectionChangeException(e);
160
                }
161
                */
162

    
163
        }
164

    
165
    @Override
166
        public void insertIndexInterval(int index, int length, boolean before) {
167
                // Nothing to do
168
        }
169

    
170
    @Override
171
        public void removeIndexInterval(int index0, int index1) {
172
                // Nothing to do
173
        }
174

    
175
    @Override
176
        public void setAnchorSelectionIndex(int index) {
177
                this.anchor = index;
178
        }
179

    
180
    @Override
181
        public void setLeadSelectionIndex(int index) {
182
                this.lead = index;
183
        }
184

    
185
    @Override
186
        public void addSelectionInterval(int index0, int index1) {
187
            if (!featureTableModel.isSelectionLocked()){
188
                doWithSelection(new FeatureSelectionOperation() {
189

    
190
                    @Override
191
                    public void doWithSelection(FeatureSelection selection, int first,
192
                        int last) throws DataException {
193
                        for (int i = first; i <= last; i++) {
194
                            Feature feature = getFeature(i);
195
                            if (!selection.isSelected(feature)) {
196
                                selection.select(feature);
197
                            }
198
                        }
199
                    }
200

    
201
                }, index0, index1, true);
202
            }
203
        }
204

    
205
    @Override
206
        public void setSelectionInterval(int index0, int index1) {
207
            if (!featureTableModel.isSelectionLocked()){
208
                doWithSelection(new FeatureSelectionOperation() {
209

    
210
                    @Override
211
                    public void doWithSelection(FeatureSelection selection, int first,
212
                        int last) throws DataException {
213
                        selection.deselectAll();
214
                        for (int i = first; i <= last; i++) {
215
                            Feature feature = getFeature(i);
216
                            selection.select(feature);
217
                        }
218
                    }
219

    
220
                }, index0, index1, true);
221
            }
222
        }
223

    
224
    @Override
225
        public void removeSelectionInterval(int index0, int index1) {
226
            if (!featureTableModel.isSelectionLocked()){
227
                doWithSelection(new FeatureSelectionOperation() {
228

    
229
                    @Override
230
                    public void doWithSelection(FeatureSelection selection, int first,
231
                        int last) throws DataException {
232
                        for (int i = first; i <= last; i++) {
233
                            Feature feature = getFeature(i);
234
                            if (selection.isSelected(feature)) {
235
                                selection.deselect(feature);
236
                            }
237
                        }
238
                    }
239

    
240
                }, index0, index1, false);
241
            }
242
        }
243

    
244
    @Override
245
        public void clearSelection() {
246
            if (!featureTableModel.isSelectionLocked()){
247
                try {
248
                    getFeatureSelection().deselectAll();
249
                } catch (DataException e) {
250
                    throw new SelectionChangeException(e);
251
                }
252
            }
253
        }
254

    
255
    @Override
256
        public boolean isSelectedIndex(int index) {
257
                if (index == -1) {
258
                        return false;
259
                }
260
                Feature feature = featureTableModel.getFeatureAt(index);
261
                return getFeatureSelection().isSelected(feature);
262
        }
263

    
264
    @Override
265
        public boolean isSelectionEmpty() {
266
                try {
267
                        return getFeatureSelection().isEmpty();
268
                } catch (DataException ex) {
269
                        throw new SelectionChangeException(ex);
270
                }
271
        }
272

    
273
    /**
274
     *
275
     * @return
276
     */
277
    @Override
278
        public boolean getValueIsAdjusting() {
279
                return isAdjusting;
280
        }
281

    
282
    @Override
283
        public void setValueIsAdjusting(boolean valueIsAdjusting) {
284
                if (this.isAdjusting != valueIsAdjusting) {
285
                        this.isAdjusting = valueIsAdjusting;
286
                        if (this.isAdjusting) {
287
                                getFeatureSelection().beginComplexNotification();
288
                        } else {
289
                                getFeatureSelection().endComplexNotification();
290
                        }
291
                }
292
        }
293

    
294
    @Override
295
        public int getSelectionMode() {
296
                return selectionMode;
297
        }
298

    
299
    @Override
300
        public void setSelectionMode(int selectionMode) {
301
                this.selectionMode = selectionMode;
302
        }
303

    
304
    @Override
305
        public void addListSelectionListener(ListSelectionListener listener) {
306
                listenerList.add(ListSelectionListener.class, listener);
307
        }
308

    
309
    @Override
310
        public void removeListSelectionListener(ListSelectionListener listener) {
311
                listenerList.remove(ListSelectionListener.class, listener);
312
        }
313

    
314
    @Override
315
        public void update(Observable observable, Object notification) {
316
            if( observable instanceof FeatureSelection ) {
317
                try{
318
                    fireValueChanged(-1, -1, false);
319
                }catch(ConcurrentDataModificationException e){
320
                    LOG.warn("The store has been updated and the selection can not be refreshed", e);
321
                }                        
322
            }
323
        }
324

    
325
        private FeatureSelection getFeatureSelection() {
326
                return this.featureTableModel.getFeatureSelection();
327
        }
328

    
329
        /**
330
         * @param operation
331
         * @param index0
332
         * @param index1
333
         */
334
        private void doWithSelection(FeatureSelectionOperation operation,
335
                        int index0, int index1, boolean select) {
336
                // Set the anchor and lead
337
                anchor = index0;
338
                lead = index1;
339

    
340
                // As index0 <= index1 is no guaranteed, calculate the first and second
341
                // values
342
                int first = (index0 <= index1) ? index0 : index1;
343
                int last = (index0 <= index1) ? index1 : index0;
344

    
345
                //If the new selection is not updated don't continue
346
                if ((currentFirst == first) && (currentLast == last)){
347
                    return;
348
                }
349
                currentFirst = first;
350
                currentLast = last;
351

    
352
                FeatureSelection selection = getFeatureSelection();
353

    
354
                // Perform the selection operation into a complex notification
355
                selection.beginComplexNotification();
356
                try {
357
                        // Is a full select or deselect
358
                        if (first == 00 && last == featureTableModel.getRowCount() - 1) {
359
                                if (select) {
360
                                        selection.selectAll();
361
                                } else {
362
                                        selection.deselectAll();
363
                                }
364
                        } else {
365
                                operation.doWithSelection(selection, first, last);
366
                        }
367
                } catch (DataException e) {
368
                        throw new SelectionChangeException(e);
369
                } finally {
370
                        selection.endComplexNotification();
371
                }
372

    
373
                fireValueChanged(first, last, isAdjusting);
374
        }
375

    
376
        /**
377
         * Returns a Feature by table row position.
378
         */
379
        private Feature getFeature(int index) {
380
                return featureTableModel.getFeatureAt(index);
381
        }
382

    
383
        /**
384
         * Returns the FeatureStore.
385
         */
386
        private FeatureStore getFeatureStore() {
387
                return featureTableModel.getFeatureStore();
388
        }
389

    
390
        /**
391
         * @param firstIndex
392
         *            the first index in the interval
393
         * @param lastIndex
394
         *            the last index in the interval
395
         * @param isAdjusting
396
         *            true if this is the final change in a series of adjustments
397
         * @see EventListenerList
398
         */
399
        protected void fireValueChanged(int firstIndex, int lastIndex,
400
                        boolean isAdjusting) {
401
                Object[] listeners = listenerList.getListenerList();
402
                ListSelectionEvent e = null;
403

    
404
                for (int i = listeners.length - 2; i >= 0; i -= 2) {
405
                        if (listeners[i] == ListSelectionListener.class) {
406
                                if (e == null) {
407
                                        e =
408
                                                        new ListSelectionEvent(this, firstIndex, lastIndex,
409
                                                                        isAdjusting);
410
                                }
411
                                ((ListSelectionListener) listeners[i + 1]).valueChanged(e);
412
                        }
413
                }
414
        }
415

    
416
        private interface FeatureSelectionOperation {
417
                void doWithSelection(FeatureSelection selection, int first, int last)
418
                                throws DataException;
419
        }
420

    
421
        /**
422
         *
423
         * Return the index of the the first (last) selected feature
424
         *
425
         * @param last whether to return the index of the last selected feature
426
         * @return
427
         */
428
        private int getSelectionIndex(boolean last) {
429

    
430
        int ind = -1;
431
        int resp = -1;
432

    
433
        FeatureSet fs = null;
434
        DisposableIterator diter = null;
435

    
436
        try {
437
            FeatureSelection selection = getFeatureSelection();
438
            if (!selection.isEmpty()) {
439
                    FeatureQuery query = this.featureTableModel.getFeatureQuery();
440
                    if(query!= null){
441
                            fs = getFeatureStore().getFeatureSet(query);
442
                    }else{
443
                            fs = getFeatureStore().getFeatureSet();
444
                    }
445
                diter = fs.fastIterator();
446
                Feature feat = null;
447
                while (diter.hasNext()) {
448
                    ind++;
449
                    feat = (Feature) diter.next();
450
                    if (selection.isSelected(feat)) {
451
                        resp = ind;
452
                        if (!last) {
453
                            break;
454
                        }
455
                    }
456
                }
457

    
458
            }
459
        } catch (DataException e) {
460
            throw new SelectionChangeException(e);
461
        } finally {
462
            if (diter != null) {
463
                diter.dispose();
464
            }
465

    
466
            if (fs != null) {
467
                fs.dispose();
468
            }
469
        }
470
        return resp;
471
        }
472

    
473
}