Revision 33380
branches/v2_0_0_prep/libraries/libFMap_controls/src/org/gvsig/fmap/mapcontrol/dal/feature/swing/FeatureSelectionModel.java | ||
---|---|---|
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.mapcontrol.dal.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.mapcontrol.dal.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 = getSelection(); |
|
149 |
selection.beginComplexNotification(); |
|
150 |
try { |
|
151 |
selection.deselectAll(); |
|
152 |
for (int i = first; i <= last; i++) { |
|
153 |
selectFeatureAt(i, select, selection); |
|
154 |
} |
|
155 |
} catch (DataException e) { |
|
156 |
throw new SelectionChangeException(e); |
|
157 |
} |
|
158 |
selection.endComplexNotification(); |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
162 |
|
|
163 |
/** |
|
164 |
* Selects a feature at a row position. |
|
165 |
* |
|
166 |
* @param index |
|
167 |
* the Feature row index |
|
168 |
* @param select |
|
169 |
* if to select or deselect |
|
170 |
* @param selection |
|
171 |
* selection instance to use |
|
172 |
*/ |
|
173 |
private void selectFeatureAt(int index, boolean select, |
|
174 |
FeatureSelection selection) { |
|
175 |
Feature feature = getFeature(index); |
|
176 |
if (select) { |
|
177 |
selection.select(feature); |
|
178 |
} else { |
|
179 |
selection.deselect(feature); |
|
180 |
} |
|
181 |
} |
|
182 |
|
|
183 |
/** |
|
184 |
* Returns a Feature by table row position. |
|
185 |
*/ |
|
186 |
private Feature getFeature(int index) { |
|
187 |
return featureTableModel.getFeatureAt(index); |
|
188 |
} |
|
189 |
|
|
190 |
/** |
|
191 |
* Returns the FeatureStore. |
|
192 |
*/ |
|
193 |
private FeatureStore getFeatureStore() { |
|
194 |
return featureTableModel.getFeatureStore(); |
|
195 |
} |
|
196 |
|
|
197 |
/** |
|
198 |
* Returns the FeatureStore. |
|
199 |
*/ |
|
200 |
private FeatureSelection getSelection() { |
|
201 |
try { |
|
202 |
return (FeatureSelection) getFeatureStore().getSelection(); |
|
203 |
} catch (DataException ex) { |
|
204 |
throw new SelectionChangeException(ex); |
|
205 |
} |
|
206 |
} |
|
207 |
|
|
208 |
public void update(Observable observable, Object notification) { |
|
209 |
if (notification instanceof FeatureStoreNotification) { |
|
210 |
FeatureStoreNotification fnotification = (FeatureStoreNotification) notification; |
|
211 |
if (!fnotification.getSource().equals(getFeatureStore())) { |
|
212 |
return; |
|
213 |
} |
|
214 |
if (FeatureStoreNotification.SELECTION_CHANGE.equals(fnotification |
|
215 |
.getType())) { |
|
216 |
this.fireValueChanged(-1, -1, false); |
|
217 |
|
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
} |
|
222 |
|
|
223 |
} |
Also available in: Unified diff