Revision 45947 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/tools/PointSelectionListener.java
PointSelectionListener.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.awt.Image; |
27 | 27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ComponentAdapter; |
|
29 |
import java.awt.event.ComponentEvent; |
|
30 |
import java.awt.event.ComponentListener; |
|
28 | 31 |
import java.awt.geom.Point2D; |
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Iterator; |
|
29 | 34 |
import java.util.List; |
35 |
import javax.swing.SwingUtilities; |
|
36 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
|
37 |
import org.apache.commons.lang3.tuple.Pair; |
|
30 | 38 |
|
31 | 39 |
import org.gvsig.fmap.IconThemeHelper; |
32 | 40 |
import org.gvsig.fmap.dal.exception.DataException; |
33 | 41 |
import org.gvsig.fmap.dal.feature.Feature; |
42 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
|
34 | 43 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
35 | 44 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
46 |
import org.gvsig.fmap.geom.primitive.Point; |
|
36 | 47 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
37 | 48 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
38 | 49 |
import org.gvsig.fmap.mapcontrol.MapControl; |
... | ... | |
45 | 56 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
46 | 57 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
47 | 58 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
59 |
import org.gvsig.tools.util.UnmodifiableBasicList64; |
|
48 | 60 |
|
49 | 61 |
/** |
50 | 62 |
* <p> |
... | ... | |
56 | 68 |
*/ |
57 | 69 |
public class PointSelectionListener implements PointListener { |
58 | 70 |
|
71 |
public static int PIXEL_TOLERANCE_FOR_QUERY_BY_POINT = 3; |
|
59 | 72 |
/** |
60 | 73 |
* Reference to the <code>MapControl</code> object that uses. |
61 | 74 |
*/ |
62 | 75 |
protected MapControl mapCtrl; |
76 |
private PointSelectionListenerUniqueSelectPanel panel; |
|
63 | 77 |
|
64 | 78 |
/** |
65 | 79 |
* <p> |
... | ... | |
71 | 85 |
this.mapCtrl = mc; |
72 | 86 |
} |
73 | 87 |
|
88 |
List<Pair<FLyrVect, UnmodifiableBasicList64<Feature>>> getClickedFeatures(Point mapPoint) throws BehaviorException { |
|
89 |
List<Pair<FLyrVect, UnmodifiableBasicList64<Feature>>> clickedFeatures = new ArrayList(); |
|
90 |
|
|
91 |
// Tolerancia de 3 pixels |
|
92 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
93 |
double tol = mapCtrl.getViewPort().toMapDistance(PIXEL_TOLERANCE_FOR_QUERY_BY_POINT); |
|
94 |
FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives(); |
|
95 |
for (int i = 0; i < actives.length; i++) { |
|
96 |
if (actives[i] instanceof FLyrVect) { |
|
97 |
FLyrVect lyrVect = (FLyrVect) actives[i]; |
|
98 |
try { |
|
99 |
FeatureStore store = lyrVect.getFeatureStore(); |
|
100 |
FeatureQuery query = lyrVect.createQueryByPoint(mapPoint.getX(), mapPoint.getY(), tol, store.getDefaultFeatureType()); |
|
101 |
UnmodifiableBasicList64<Feature> features = (UnmodifiableBasicList64<Feature>) store.getFeatures(query); |
|
102 |
|
|
103 |
if (!features.isEmpty()) { |
|
104 |
Pair<FLyrVect, UnmodifiableBasicList64<Feature>> pair = new ImmutablePair( |
|
105 |
lyrVect, |
|
106 |
features |
|
107 |
); |
|
108 |
clickedFeatures.add(pair); |
|
109 |
} |
|
110 |
} catch (DataException e) { |
|
111 |
throw new BehaviorException("No se pudo hacer la selecci?n", e); |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
return clickedFeatures; |
|
116 |
} |
|
117 |
|
|
74 | 118 |
public void point(PointEvent event) throws BehaviorException { |
75 | 119 |
try { |
76 |
Point2D p = event.getPoint(); |
|
77 |
Point2D mapPoint = mapCtrl.getViewPort().toMapPoint((int) p.getX(), (int) p.getY()); |
|
120 |
if (this.panel != null && this.panel.isVisible()) { |
|
121 |
PointSelectionListenerUniqueSelectPanel myPanel = this.panel; |
|
122 |
this.panel = null; |
|
123 |
myPanel.setVisible(false); |
|
124 |
} |
|
125 |
Point mapPoint = event.getMapPoint(); |
|
78 | 126 |
|
79 |
// Tolerancia de 3 pixels |
|
80 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
81 |
double tol = mapCtrl.getViewPort().toMapDistance(3); |
|
82 |
FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives(); |
|
83 |
for (int i = 0; i < actives.length; i++) { |
|
84 |
if (actives[i] instanceof FLyrVect) { |
|
85 |
FLyrVect lyrVect = (FLyrVect) actives[i]; |
|
86 |
FeatureSet newSelection = null; |
|
87 |
try { |
|
88 |
newSelection = lyrVect.queryByPoint( |
|
89 |
mapPoint, tol, |
|
90 |
lyrVect.getFeatureStore().getDefaultFeatureType()); |
|
91 |
if (event.getEvent().isControlDown()) { |
|
92 |
FeatureSelection currentSelection = (FeatureSelection) lyrVect.getDataStore().getSelection(); |
|
93 |
DisposableIterator it = newSelection.fastIterator(); |
|
94 |
while (it.hasNext()) { |
|
95 |
Object obj = it.next(); |
|
96 |
if (obj instanceof Feature) { |
|
97 |
Feature feat = ((Feature) obj); |
|
98 |
if (currentSelection.isSelected(feat)) { |
|
99 |
currentSelection.deselect(feat); |
|
100 |
} else { |
|
101 |
currentSelection.select(feat); |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
} else { |
|
106 |
lyrVect.getFeatureStore() |
|
107 |
.setSelection(newSelection); |
|
108 |
} |
|
109 |
if (event.getEvent().isShiftDown()) { |
|
110 |
//select features in the feature selection in a table |
|
111 |
FeatureSelection actualSelection = lyrVect.getFeatureStore().getFeatureSelection(); |
|
112 |
if (actualSelection.getSize() > 1) { |
|
127 |
List<Pair<FLyrVect, UnmodifiableBasicList64<Feature>>> clickedFeatures = getClickedFeatures(mapPoint); |
|
128 |
int countFeatures = 0; |
|
113 | 129 |
|
114 |
WindowManager_v2 winmanager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
|
115 |
PointSelectionListenerUniqueSelectPanel panel = new PointSelectionListenerUniqueSelectPanel(); |
|
116 |
panel.put(lyrVect.getFeatureStore(), actualSelection); |
|
117 |
final Dialog dialog = winmanager.createDialog( |
|
118 |
panel.asJComponent(), |
|
119 |
i18n.getTranslation("_Features_selector"), |
|
120 |
null, |
|
121 |
WindowManager_v2.BUTTONS_OK_CANCEL |
|
122 |
); |
|
123 |
dialog.addActionListener((ActionEvent e) -> { |
|
124 |
if (dialog.getAction() == WindowManager_v2.BUTTONS_OK) { |
|
125 |
List<Feature> selectedFeatures = panel.getSelectedFeatures(); |
|
126 |
FeatureSelection newFeatureSelection = null; |
|
127 |
try { |
|
128 |
newFeatureSelection = lyrVect.getFeatureStore().createFeatureSelection(); |
|
129 |
if (newFeatureSelection!=null) { |
|
130 |
for (Feature selectedFeature : selectedFeatures) { |
|
131 |
newFeatureSelection.select(selectedFeature.getReference()); |
|
132 |
} |
|
133 |
} |
|
134 |
lyrVect.getFeatureStore().setSelection(newFeatureSelection); |
|
135 |
} catch (DataException ex) { |
|
136 |
|
|
137 |
} finally { |
|
130 |
for (Pair<FLyrVect, UnmodifiableBasicList64<Feature>> pair : clickedFeatures) { |
|
131 |
countFeatures += pair.getRight().size64(); |
|
132 |
} |
|
138 | 133 |
|
139 |
} |
|
140 |
} |
|
141 |
}); |
|
142 |
dialog.show(WindowManager.MODE.DIALOG); |
|
143 |
} |
|
134 |
if (event.getEvent().isShiftDown() && countFeatures > 1) { |
|
135 |
if (!event.getEvent().isControlDown()) { |
|
136 |
FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives(); |
|
137 |
for (int i = 0; i < actives.length; i++) { |
|
138 |
if (actives[i] instanceof FLyrVect) { |
|
139 |
FLyrVect lyrVect = (FLyrVect) actives[i]; |
|
140 |
FeatureStore store = lyrVect.getFeatureStore(); |
|
141 |
store.getFeatureSelection().deselectAll(); |
|
144 | 142 |
} |
145 |
} finally { |
|
146 |
if (newSelection != null) { |
|
147 |
newSelection.dispose(); |
|
143 |
} |
|
144 |
} |
|
145 |
|
|
146 |
this.panel = new PointSelectionListenerUniqueSelectPanel(this.mapCtrl, event.getEvent().isControlDown()); |
|
147 |
this.panel.addComponentListener(new ComponentAdapter() { |
|
148 |
@Override |
|
149 |
public void componentHidden(ComponentEvent e) { |
|
150 |
if (e.getSource() == panel) { |
|
151 |
panel = null; |
|
148 | 152 |
} |
149 | 153 |
} |
150 |
mapCtrl.drawMap(false); // FIXME Esto deber?a sobrar (la capa deber?a de ser observada) |
|
154 |
}); |
|
155 |
final WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager(); |
|
156 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
157 |
|
|
158 |
panel.put(clickedFeatures); |
|
159 |
// SwingUtilities.invokeLater(() -> { |
|
160 |
winManager.showWindow( |
|
161 |
panel.asJComponent(), |
|
162 |
i18n.getTranslation("_Features_selector"), |
|
163 |
WindowManager.MODE.TOOL |
|
164 |
); |
|
165 |
// }); |
|
166 |
|
|
167 |
} else if (event.getEvent().isControlDown()) { |
|
168 |
for (Pair<FLyrVect, UnmodifiableBasicList64<Feature>> pair : clickedFeatures) { |
|
169 |
FLyrVect layer = pair.getLeft(); |
|
170 |
FeatureStore store = layer.getFeatureStore(); |
|
171 |
FeatureSelection selection = store.getFeatureSelection(); |
|
172 |
UnmodifiableBasicList64<Feature> features = pair.getRight(); |
|
173 |
for (Feature feature : features) { |
|
174 |
if (selection.isSelected(feature)) { |
|
175 |
selection.deselect(feature); |
|
176 |
} else { |
|
177 |
selection.select(feature); |
|
178 |
} |
|
179 |
} |
|
151 | 180 |
} |
181 |
} else { |
|
182 |
FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives(); |
|
183 |
for (int i = 0; i < actives.length; i++) { |
|
184 |
if (actives[i] instanceof FLyrVect) { |
|
185 |
FLyrVect lyrVect = (FLyrVect) actives[i]; |
|
186 |
FeatureStore store = lyrVect.getFeatureStore(); |
|
187 |
store.getFeatureSelection().deselectAll(); |
|
188 |
} |
|
189 |
} |
|
190 |
for (Pair<FLyrVect, UnmodifiableBasicList64<Feature>> pair : clickedFeatures) { |
|
191 |
FLyrVect layer = pair.getLeft(); |
|
192 |
FeatureStore store = layer.getFeatureStore(); |
|
193 |
FeatureSelection selection = store.getFeatureSelection(); |
|
194 |
UnmodifiableBasicList64<Feature> features = pair.getRight(); |
|
195 |
for (Feature feature : features) { |
|
196 |
selection.select(feature); |
|
197 |
} |
|
198 |
} |
|
152 | 199 |
} |
153 | 200 |
|
154 | 201 |
} catch (DataException e) { |
155 |
throw new BehaviorException("No se pudo hacer la selecci?n", e);
|
|
202 |
throw new BehaviorException("Can't do the selection", e);
|
|
156 | 203 |
} |
157 | 204 |
} |
158 | 205 |
|
206 |
@Override |
|
159 | 207 |
public Image getImageCursor() { |
160 | 208 |
return IconThemeHelper.getImage("cursor-select-by-point"); |
161 | 209 |
} |
162 | 210 |
|
211 |
@Override |
|
163 | 212 |
public boolean cancelDrawing() { |
164 | 213 |
return false; |
165 | 214 |
} |
166 | 215 |
|
216 |
@Override |
|
167 | 217 |
public void pointDoubleClick(PointEvent event) throws BehaviorException { |
168 | 218 |
|
169 | 219 |
} |
Also available in: Unified diff