Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / data / feature / swing / FeatureSelectionModel.java @ 31544

History | View | Annotate | Download (6.94 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
 * 2008 {DiSiD Technologies}  {{Task}}
26
 */
27
package org.gvsig.fmap.data.feature.swing;
28

    
29
import javax.swing.DefaultListSelectionModel;
30

    
31
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.feature.Feature;
33
import org.gvsig.fmap.dal.feature.FeatureSelection;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
36
import org.gvsig.fmap.data.feature.swing.table.FeatureTableModel;
37
import org.gvsig.tools.observer.Observable;
38
import org.gvsig.tools.observer.Observer;
39

    
40
/**
41
 * ListSelectionModel implementation based on the FeatureSelection of a
42
 * FeatureStore of a FeatureTableModel used to show Features of a FeatureSet.
43
 *
44
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
45
 */
46
public class FeatureSelectionModel extends DefaultListSelectionModel implements
47
                Observer {
48

    
49
    private static final long serialVersionUID = 7643472991526133823L;
50

    
51
    private final FeatureTableModel featureTableModel;
52

    
53
    /**
54
     * Creates a new FeatureSelectionModel with a FeatureTableModel used to get
55
     * the Features by position in the table.
56
     *
57
     * @param featureTableModel
58
     *            to get Features from
59
     * @throws DataException
60
     *             if there is an error getting the store selection
61
     */
62
    public FeatureSelectionModel(FeatureTableModel featureTableModel)
63
            throws DataException {
64
        this.featureTableModel = featureTableModel;
65
        this.featureTableModel.getFeatureStore().addObserver(this);
66
    }
67

    
68
    @Override
69
    public boolean isSelectedIndex(int index) {
70
        // if (getValueIsAdjusting()) {
71
        // return super.isSelectedIndex(index);
72
        // } else {
73
            if (index==-1){
74
                    return false;
75
            }
76
        Feature feature = featureTableModel.getFeatureAt(index);
77
        return getSelection().isSelected(feature);
78
        // }
79
    }
80

    
81
    @Override
82
    public void setSelectionInterval(int index0, int index1) {
83
        // if (!getValueIsAdjusting()) {
84
        try {
85
                getSelection().deselectAll();
86
        } catch (DataException ex) {
87
            throw new SelectionChangeException(ex);
88
        }
89
        selectFeatureInterval(index0, index1);
90
        // }
91
        super.setSelectionInterval(index0, index1);
92
    }
93

    
94
    @Override
95
    public void addSelectionInterval(int index0, int index1) {
96
        // if (!getValueIsAdjusting()) {
97
        selectFeatureInterval(index0, index1);
98
        // }
99
        super.addSelectionInterval(index0, index1);
100
    }
101

    
102
    @Override
103
    public void removeSelectionInterval(int index0, int index1) {
104
        // if (!getValueIsAdjusting()) {
105
        selectFeatureInterval(index0, index1, false);
106
        // }
107
        super.removeSelectionInterval(index0, index1);
108
    }
109

    
110
    /**
111
     * Selects the features between an interval of rows.
112
     */
113
    private void selectFeatureInterval(int index0, int index1) {
114
        selectFeatureInterval(index0, index1, true);
115
    }
116

    
117
    /**
118
     * Selects the features between an interval of rows.
119
     *
120
     * @param index0
121
     *            the first row index
122
     * @param index1
123
     *            the second row index (index0 > index1 not required)
124
     * @param select
125
     *            if to select or deselect
126
     */
127
    private void selectFeatureInterval(int index0, int index1, boolean select) {
128
        // If only single selection is allowed, the valid value is index1
129
        if (getSelectionMode() == SINGLE_SELECTION) {
130
            selectFeatureAt(index1, select, getSelection());
131
        } else {
132
            // index0 < index1 is not guaranteed
133
            int first = index0 <= index1 ? index0 : index1;
134
            int last = index0 <= index1 ? index1 : index0;
135

    
136
            // Its a full de/selection
137
            if (first == 0 && last == featureTableModel.getRowCount() - 1) {
138
                try {
139
                    if (select) {
140
                            getSelection().selectAll();
141
                    } else {
142
                            getSelection().deselectAll();
143
                    }
144
                } catch (DataException ex) {
145
                    throw new SelectionChangeException(ex);
146
                }
147
            } else {
148
                    FeatureSelection selection;
149
                                try {
150
                                        selection = this.getFeatureStore().createFeatureSelection();
151
                                } catch (DataException e) {
152
                                        throw new SelectionChangeException(e);
153
                                }
154
                                for (int i = first; i <= last; i++) {
155
                                        selectFeatureAt(i, select, selection);
156
                }
157
                                try {
158
                                        this.getFeatureStore().setSelection(selection);
159
                                } catch (DataException e) {
160
                                        throw new SelectionChangeException(e);
161
                                }
162
            }
163
        }
164
    }
165

    
166
        /**
167
         * Selects a feature at a row position.
168
         *
169
         * @param index
170
         *            the Feature row index
171
         * @param select
172
         *            if to select or deselect
173
         * @param selection
174
         *            selection instance to use
175
         */
176
    private void selectFeatureAt(int index, boolean select,
177
                        FeatureSelection selection) {
178
        Feature feature = getFeature(index);
179
        if (select) {
180
            selection.select(feature);
181
        } else {
182
                selection.deselect(feature);
183
        }
184
    }
185

    
186
    /**
187
     * Returns a Feature by table row position.
188
     */
189
    private Feature getFeature(int index) {
190
        return featureTableModel.getFeatureAt(index);
191
    }
192

    
193
    /**
194
     * Returns the FeatureStore.
195
     */
196
    private FeatureStore getFeatureStore() {
197
        return featureTableModel.getFeatureStore();
198
    }
199

    
200
    /**
201
         * Returns the FeatureStore.
202
         */
203
        private FeatureSelection getSelection() {
204
        try {
205
                        return (FeatureSelection) getFeatureStore().getSelection();
206
                } catch (DataException ex) {
207
                        throw new SelectionChangeException(ex);
208
                }
209
        }
210

    
211
        public void update(Observable observable, Object notification) {
212
                if (notification instanceof FeatureStoreNotification) {
213
                        FeatureStoreNotification fnotification = (FeatureStoreNotification) notification;
214
                        if (!fnotification.getSource().equals(getFeatureStore())) {
215
                                return;
216
                        }
217
                        if (FeatureStoreNotification.SELECTION_CHANGE.equals(fnotification
218
                                        .getType())) {
219
                                this.fireValueChanged(-1, -1, false);
220

    
221
                        }
222
                }
223

    
224
        }
225

    
226
}