svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / PointSelectionListener.java @ 43510
History | View | Annotate | Download (4.24 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.fmap.mapcontrol.tools; |
||
25 | |||
26 | import java.awt.Image; |
||
27 | import java.awt.geom.Point2D; |
||
28 | |||
29 | import org.gvsig.fmap.IconThemeHelper; |
||
30 | import org.gvsig.fmap.dal.exception.DataException; |
||
31 | 41175 | nbrodin | import org.gvsig.fmap.dal.feature.Feature; |
32 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSelection; |
33 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
34 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
35 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
36 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
37 | import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent; |
||
38 | import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener; |
||
39 | 41175 | nbrodin | import org.gvsig.tools.dispose.DisposableIterator; |
40 | 40435 | jjdelcerro | |
41 | /**
|
||
42 | * <p>Listener that selects all features of the active, and vector layers of the associated <code>MapControl</code>
|
||
43 | * that their area intersects with the point selected by a single click of any button of the mouse.</p>
|
||
44 | *
|
||
45 | * @author Vicente Caballero Navarro
|
||
46 | */
|
||
47 | public class PointSelectionListener implements PointListener { |
||
48 | /**
|
||
49 | * Reference to the <code>MapControl</code> object that uses.
|
||
50 | */
|
||
51 | protected MapControl mapCtrl;
|
||
52 | |||
53 | /**
|
||
54 | * <p>Creates a new <code>PointSelectionListener</code> object.</p>
|
||
55 | *
|
||
56 | * @param mc the <code>MapControl</code> where will be applied the changes
|
||
57 | */
|
||
58 | public PointSelectionListener(MapControl mc) {
|
||
59 | this.mapCtrl = mc;
|
||
60 | } |
||
61 | |||
62 | public void point(PointEvent event) throws BehaviorException { |
||
63 | try {
|
||
64 | Point2D p = event.getPoint();
|
||
65 | Point2D mapPoint = mapCtrl.getViewPort().toMapPoint((int) p.getX(), (int) p.getY()); |
||
66 | |||
67 | // Tolerancia de 3 pixels
|
||
68 | double tol = mapCtrl.getViewPort().toMapDistance(3); |
||
69 | 41175 | nbrodin | FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives();
|
70 | for (int i = 0; i < actives.length; i++) { |
||
71 | 40435 | jjdelcerro | if (actives[i] instanceof FLyrVect) { |
72 | FLyrVect lyrVect = (FLyrVect) actives[i]; |
||
73 | FeatureSet newSelection = null;
|
||
74 | try {
|
||
75 | 41175 | nbrodin | newSelection = lyrVect.queryByPoint( |
76 | mapPoint, tol, |
||
77 | lyrVect.getFeatureStore().getDefaultFeatureType()); |
||
78 | 40435 | jjdelcerro | if (event.getEvent().isControlDown()) {
|
79 | 41177 | nbrodin | FeatureSelection currentSelection = (FeatureSelection)lyrVect.getDataStore().getSelection(); |
80 | DisposableIterator it = newSelection.fastIterator(); |
||
81 | while(it.hasNext()) {
|
||
82 | Object obj = it.next();
|
||
83 | if(obj instanceof Feature) { |
||
84 | Feature feat = ((Feature)obj); |
||
85 | if(currentSelection.isSelected(feat))
|
||
86 | currentSelection.deselect(feat); |
||
87 | else
|
||
88 | currentSelection.select(feat); |
||
89 | } |
||
90 | 41175 | nbrodin | } |
91 | 40435 | jjdelcerro | } else {
|
92 | lyrVect.getFeatureStore() |
||
93 | .setSelection(newSelection); |
||
94 | } |
||
95 | } finally {
|
||
96 | if (newSelection!=null){ |
||
97 | newSelection.dispose(); |
||
98 | } |
||
99 | } |
||
100 | mapCtrl.drawMap(false); // FIXME Esto deber?a sobrar (la capa deber?a de ser observada) |
||
101 | } |
||
102 | } |
||
103 | |||
104 | } catch (DataException e) {
|
||
105 | throw new BehaviorException("No se pudo hacer la selecci?n", e); |
||
106 | } |
||
107 | } |
||
108 | 41175 | nbrodin | |
109 | 40435 | jjdelcerro | |
110 | public Image getImageCursor() { |
||
111 | return IconThemeHelper.getImage("cursor-select-by-point"); |
||
112 | } |
||
113 | |||
114 | public boolean cancelDrawing() { |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | public void pointDoubleClick(PointEvent event) throws BehaviorException { |
||
119 | |||
120 | } |
||
121 | } |