svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / gui / selectionByTheme / MySelectionByThemeListener.java @ 43704
History | View | Annotate | Download (15.4 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 |
package org.gvsig.app.gui.selectionByTheme; |
25 |
|
26 |
import org.apache.commons.lang3.mutable.MutableLong; |
27 |
import org.cresques.cts.IProjection; |
28 |
import org.gvsig.fmap.dal.feature.Feature; |
29 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
30 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
31 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
35 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
36 |
import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory; |
37 |
import org.gvsig.tools.ToolsLocator; |
38 |
import org.gvsig.tools.dispose.DisposeUtils; |
39 |
import org.gvsig.tools.evaluator.Evaluator; |
40 |
import org.gvsig.tools.exception.BaseException; |
41 |
import org.gvsig.tools.i18n.I18nManager; |
42 |
import org.gvsig.tools.task.SimpleTaskStatus; |
43 |
import org.gvsig.tools.visitor.VisitCanceledException; |
44 |
import org.gvsig.tools.visitor.Visitor; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
public class MySelectionByThemeListener implements SelectionByThemeListener { |
49 |
private static final Logger logger = LoggerFactory.getLogger(MySelectionByThemeListener.class); |
50 |
|
51 |
@Override
|
52 |
public void newSet(FLayer[] layersToSelect, FLayer selectionLayer, final int action, final SimpleTaskStatus status) { |
53 |
if (selectionLayer instanceof FLyrVect) { |
54 |
logger.info("SelectByLayer begin newSet, action "+action);
|
55 |
status.message(getActionLabel(action)); |
56 |
status.setIndeterminate(); |
57 |
try {
|
58 |
if( action == SelectionByTheme.INTERSECTS ) {
|
59 |
IntersectOperation operation = new IntersectOperation();
|
60 |
for (FLayer layerToSelect : layersToSelect) {
|
61 |
if (layerToSelect instanceof FLyrVect) { |
62 |
FeatureStore store1 = ((FLyrVect) selectionLayer).getFeatureStore(); |
63 |
FeatureStore store2 = ((FLyrVect) layerToSelect).getFeatureStore(); |
64 |
FeatureSelection selection = operation.execute( |
65 |
store1, store2, status |
66 |
); |
67 |
if( selection!=null ) { |
68 |
store2.setSelection(selection); |
69 |
} |
70 |
} |
71 |
} |
72 |
return;
|
73 |
} |
74 |
FeatureSelection selection = ((FLyrVect) selectionLayer).getFeatureStore().getFeatureSelection(); |
75 |
final IProjection projection = selectionLayer.getMapContext().getProjection();
|
76 |
for (FLayer layerToSelect : layersToSelect) {
|
77 |
if (layerToSelect instanceof FLyrVect) { |
78 |
final FeatureStore storeToQuery = ((FLyrVect) layerToSelect).getFeatureStore();
|
79 |
final FeatureSelection newSelection = storeToQuery.createFeatureSelection();
|
80 |
final MutableLong count = new MutableLong(0); |
81 |
status.setRangeOfValues(0, selection.getSelectedCount());
|
82 |
status.message(getActionLabel(action)); |
83 |
selection.accept(new Visitor() {
|
84 |
@Override
|
85 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
86 |
if( status.isCancellationRequested() ) {
|
87 |
throw new VisitCanceledException(); |
88 |
} |
89 |
Feature f = (Feature)o; |
90 |
FeatureSet featureSet = null;
|
91 |
try {
|
92 |
Geometry geometry = f.getDefaultGeometry(); |
93 |
Evaluator filter = createEvaluatorFromAction(action, projection, geometry, storeToQuery); |
94 |
FeatureQuery fQuery = storeToQuery.createFeatureQuery(); |
95 |
fQuery.setFilter(filter); |
96 |
featureSet = storeToQuery.getFeatureSet(fQuery); |
97 |
newSelection.select(featureSet); |
98 |
} finally {
|
99 |
DisposeUtils.disposeQuietly(featureSet); |
100 |
} |
101 |
count.add(1);
|
102 |
status.setCurValue(count.longValue()); |
103 |
} |
104 |
}); |
105 |
if( status.isCancellationRequested() ) {
|
106 |
status.cancel(); |
107 |
break;
|
108 |
} |
109 |
storeToQuery.setSelection(newSelection); |
110 |
} |
111 |
} |
112 |
} catch (Exception e) { |
113 |
logger.warn("error_selection_by_theme", e);
|
114 |
} finally {
|
115 |
logger.info("SelectByLayer end newSet.");
|
116 |
} |
117 |
} |
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void addToSet(FLayer[] layersToSelect, FLayer selectionLayer, final int action, final SimpleTaskStatus status) { |
122 |
if (selectionLayer instanceof FLyrVect) { |
123 |
logger.info("SelectByLayer begin addToSet, action "+action);
|
124 |
status.message(getActionLabel(action)); |
125 |
status.setIndeterminate(); |
126 |
try {
|
127 |
FeatureSelection selection = ((FLyrVect) selectionLayer).getFeatureStore().getFeatureSelection(); |
128 |
final IProjection projection = selectionLayer.getMapContext().getProjection();
|
129 |
for (FLayer layerToSelect : layersToSelect) {
|
130 |
if (layerToSelect instanceof FLyrVect) { |
131 |
final FeatureStore storeToQuery = ((FLyrVect) layerToSelect).getFeatureStore();
|
132 |
final FeatureSelection newSelection = storeToQuery.createFeatureSelection();
|
133 |
final MutableLong count = new MutableLong(0); |
134 |
status.setRangeOfValues(0, selection.getSelectedCount());
|
135 |
status.message(getActionLabel(action)); |
136 |
selection.accept(new Visitor() {
|
137 |
@Override
|
138 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
139 |
if( status.isCancellationRequested() ) {
|
140 |
throw new VisitCanceledException(); |
141 |
} |
142 |
Feature f = (Feature)o; |
143 |
FeatureSet featureSet = null;
|
144 |
try {
|
145 |
Geometry geometry = f.getDefaultGeometry(); |
146 |
Evaluator filter = createEvaluatorFromAction(action, projection, geometry, storeToQuery); |
147 |
FeatureQuery fQuery = storeToQuery.createFeatureQuery(); |
148 |
fQuery.setFilter(filter); |
149 |
featureSet = storeToQuery.getFeatureSet(fQuery); |
150 |
newSelection.select(featureSet); |
151 |
} finally {
|
152 |
DisposeUtils.disposeQuietly(featureSet); |
153 |
} |
154 |
count.add(1);
|
155 |
status.setCurValue(count.longValue()); |
156 |
} |
157 |
}); |
158 |
if( status.isCancellationRequested() ) {
|
159 |
status.cancel(); |
160 |
break;
|
161 |
} |
162 |
storeToQuery.getFeatureSelection().select(newSelection); |
163 |
} |
164 |
} |
165 |
} catch (Exception e) { |
166 |
logger.warn("error_selection_by_theme", e);
|
167 |
} |
168 |
logger.info("SelectByLayer end addToSet.");
|
169 |
} |
170 |
} |
171 |
|
172 |
@Override
|
173 |
public void fromSet(FLayer[] layersToSelect, FLayer selectionLayer, final int action, final SimpleTaskStatus status) { |
174 |
if (selectionLayer instanceof FLyrVect) { |
175 |
logger.info("SelectByLayer begin fromSet, action "+getActionLabel(action));
|
176 |
status.message(getActionLabel(action)); |
177 |
status.setIndeterminate(); |
178 |
try {
|
179 |
FeatureSelection selection = ((FLyrVect) selectionLayer).getFeatureStore().getFeatureSelection(); |
180 |
final IProjection projection = selectionLayer.getMapContext().getProjection();
|
181 |
for (FLayer layerToSelect : layersToSelect) {
|
182 |
if (layerToSelect instanceof FLyrVect) { |
183 |
final FeatureStore storeToQuery = ((FLyrVect) layerToSelect).getFeatureStore();
|
184 |
final FeatureSelection currentSelection = storeToQuery.getFeatureSelection();
|
185 |
final FeatureSelection newSelection = storeToQuery.createFeatureSelection();
|
186 |
final MutableLong count = new MutableLong(0); |
187 |
status.setRangeOfValues(0, selection.getSelectedCount());
|
188 |
status.message(getActionLabel(action)); |
189 |
selection.accept(new Visitor() {
|
190 |
@Override
|
191 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
192 |
if( status.isCancellationRequested() ) {
|
193 |
throw new VisitCanceledException(); |
194 |
} |
195 |
Feature f = (Feature)o; |
196 |
if( !currentSelection.isSelected(f) ) {
|
197 |
return;
|
198 |
} |
199 |
FeatureSet featureSet = null;
|
200 |
try {
|
201 |
Geometry geometry = f.getDefaultGeometry(); |
202 |
Evaluator filter = createEvaluatorFromAction(action, projection, geometry, storeToQuery); |
203 |
FeatureQuery fQuery = storeToQuery.createFeatureQuery(); |
204 |
fQuery.setFilter(filter); |
205 |
featureSet = storeToQuery.getFeatureSet(fQuery); |
206 |
newSelection.select(featureSet); |
207 |
} finally {
|
208 |
DisposeUtils.disposeQuietly(featureSet); |
209 |
} |
210 |
count.add(1);
|
211 |
status.setCurValue(count.longValue()); |
212 |
} |
213 |
}); |
214 |
if( status.isCancellationRequested() ) {
|
215 |
status.cancel(); |
216 |
break;
|
217 |
} |
218 |
storeToQuery.getFeatureSelection().select(newSelection); |
219 |
} |
220 |
} |
221 |
} catch (Exception e) { |
222 |
logger.warn("error_selection_by_theme", e);
|
223 |
} |
224 |
logger.info("SelectByLayer end fromSet.");
|
225 |
} |
226 |
} |
227 |
|
228 |
private String getActionLabel(int action) { |
229 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
230 |
switch (action) {
|
231 |
case SelectionByTheme.INTERSECTS:
|
232 |
return i18n.getTranslation("Intersecten_con"); |
233 |
case SelectionByTheme.CONTAINS:
|
234 |
return i18n.getTranslation("Contengan"); |
235 |
case SelectionByTheme.CROSSES:
|
236 |
return i18n.getTranslation("Crucen_con"); |
237 |
case SelectionByTheme.DISJOINT:
|
238 |
return i18n.getTranslation("Sean_disjuntos_a"); |
239 |
case SelectionByTheme.EQUALS:
|
240 |
return i18n.getTranslation("Sean_iguales_a"); |
241 |
case SelectionByTheme.OVERLAPS:
|
242 |
return i18n.getTranslation("Se_superponen_a"); |
243 |
case SelectionByTheme.TOUCHES:
|
244 |
return i18n.getTranslation("Toquen"); |
245 |
case SelectionByTheme.WITHIN:
|
246 |
return i18n.getTranslation("Esten_contenidos_en"); |
247 |
default:
|
248 |
return i18n.getTranslation("Intersecten_con"); |
249 |
} |
250 |
} |
251 |
|
252 |
private Evaluator createEvaluatorFromAction(int action, IProjection projection, Geometry geometry, FeatureStore store) { |
253 |
Evaluator eval; |
254 |
switch (action) {
|
255 |
case SelectionByTheme.INTERSECTS:
|
256 |
eval = SpatialEvaluatorsFactory.getInstance().intersects( |
257 |
geometry, |
258 |
projection, |
259 |
store |
260 |
); |
261 |
break;
|
262 |
case SelectionByTheme.CONTAINS:
|
263 |
eval = SpatialEvaluatorsFactory.getInstance().contains( |
264 |
geometry, |
265 |
projection, |
266 |
store |
267 |
); |
268 |
break;
|
269 |
case SelectionByTheme.CROSSES:
|
270 |
eval = SpatialEvaluatorsFactory.getInstance().crosses( |
271 |
geometry, |
272 |
projection, |
273 |
store |
274 |
); |
275 |
break;
|
276 |
case SelectionByTheme.DISJOINT:
|
277 |
eval = SpatialEvaluatorsFactory.getInstance().disjoint( |
278 |
geometry, |
279 |
projection, |
280 |
store |
281 |
); |
282 |
break;
|
283 |
case SelectionByTheme.EQUALS:
|
284 |
eval = SpatialEvaluatorsFactory.getInstance().equals( |
285 |
geometry, |
286 |
projection, |
287 |
store |
288 |
); |
289 |
break;
|
290 |
case SelectionByTheme.OVERLAPS:
|
291 |
eval = SpatialEvaluatorsFactory.getInstance().overlaps( |
292 |
geometry, |
293 |
projection, |
294 |
store |
295 |
); |
296 |
break;
|
297 |
case SelectionByTheme.TOUCHES:
|
298 |
eval = SpatialEvaluatorsFactory.getInstance().touches( |
299 |
geometry, |
300 |
projection, |
301 |
store |
302 |
); |
303 |
break;
|
304 |
case SelectionByTheme.WITHIN:
|
305 |
eval = SpatialEvaluatorsFactory.getInstance().within( |
306 |
geometry, |
307 |
projection, |
308 |
store |
309 |
); |
310 |
break;
|
311 |
|
312 |
default:
|
313 |
eval = SpatialEvaluatorsFactory.getInstance().intersects( |
314 |
geometry, |
315 |
projection, |
316 |
store |
317 |
); |
318 |
break;
|
319 |
} |
320 |
return eval;
|
321 |
} |
322 |
} |