gvsig-attributeeditor / org.gvsig.attributeeditor / trunk / org.gvsig.attributeeditor / org.gvsig.attributeeditor.mapcontrol / src / main / java / org / gvsig / fmap / mapcontrol / tools / Listeners / AttributeEditorPointListener.java @ 1059
History | View | Annotate | Download (13.5 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2014 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
|
24 |
package org.gvsig.fmap.mapcontrol.tools.Listeners; |
25 |
|
26 |
import java.awt.Dimension; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ComponentAdapter; |
29 |
import java.awt.event.ComponentEvent; |
30 |
import java.awt.event.InputEvent; |
31 |
import java.awt.event.MouseEvent; |
32 |
import java.util.function.Predicate; |
33 |
|
34 |
import javax.swing.JOptionPane; |
35 |
import javax.swing.SwingUtilities; |
36 |
import org.apache.commons.lang3.StringUtils; |
37 |
|
38 |
import org.cresques.cts.IProjection; |
39 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
40 |
import org.gvsig.expressionevaluator.GeometryExpressionUtils; |
41 |
import org.slf4j.Logger; |
42 |
import org.slf4j.LoggerFactory; |
43 |
|
44 |
import org.gvsig.featureform.swing.CreateJFeatureFormException; |
45 |
import org.gvsig.featureform.swing.JFeaturesForm; |
46 |
import org.gvsig.fmap.dal.exception.DataException; |
47 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
48 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
49 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
50 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
51 |
import org.gvsig.fmap.dal.feature.FeatureType; |
52 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
53 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
54 |
import org.gvsig.fmap.geom.Geometry; |
55 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
56 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
57 |
import org.gvsig.fmap.geom.GeometryLocator; |
58 |
import org.gvsig.fmap.geom.GeometryManager; |
59 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
60 |
import org.gvsig.fmap.geom.primitive.Circle; |
61 |
import org.gvsig.fmap.geom.primitive.Point; |
62 |
import org.gvsig.fmap.mapcontext.ViewPort; |
63 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
64 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
65 |
import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory; |
66 |
import org.gvsig.fmap.mapcontrol.MapControl; |
67 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
68 |
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
69 |
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent; |
70 |
import org.gvsig.tools.ToolsLocator; |
71 |
import org.gvsig.tools.dispose.DisposeUtils; |
72 |
import org.gvsig.tools.evaluator.Evaluator; |
73 |
import org.gvsig.tools.i18n.I18nManager; |
74 |
import org.gvsig.tools.service.ServiceException; |
75 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
76 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE; |
77 |
|
78 |
/**
|
79 |
* @author gvSIG Team
|
80 |
*
|
81 |
*/
|
82 |
@SuppressWarnings("UseSpecificCatch") |
83 |
public class AttributeEditorPointListener |
84 |
extends AbstractPointListener
|
85 |
implements RectangleListener
|
86 |
{ |
87 |
|
88 |
private static Logger LOGGER = LoggerFactory.getLogger(AttributeEditorPointListener.class); |
89 |
|
90 |
public static final String ATTRIBUTE_EDITOR_TOOL_NAME = "attributeEditorTool"; |
91 |
private static final String ATTRIBUTE_EDITOR_FORM_NAME = "attributeEditorForm"; |
92 |
|
93 |
private final MapControl mapControl; |
94 |
private JFeaturesForm form;
|
95 |
private Predicate<FeatureStore> refreshCallback; |
96 |
|
97 |
public AttributeEditorPointListener(MapControl mapControl, JFeaturesForm form) {
|
98 |
super();
|
99 |
this.mapControl = mapControl;
|
100 |
this.form = form;
|
101 |
} |
102 |
|
103 |
public AttributeEditorPointListener(MapControl mapControl) {
|
104 |
super();
|
105 |
this.mapControl = mapControl;
|
106 |
} |
107 |
|
108 |
private FLyrVect getLayer() {
|
109 |
FLayer[] layers = mapControl.getMapContext().getLayers().getActives();
|
110 |
if (layers == null || layers.length != 1) { |
111 |
return null; |
112 |
} |
113 |
return (FLyrVect) layers[0]; |
114 |
|
115 |
} |
116 |
|
117 |
@Override
|
118 |
public void point(PointEvent e) throws BehaviorException { |
119 |
MouseEvent mouseEvent = e.getEvent();
|
120 |
if (((mouseEvent.getModifiersEx() & (InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK |
121 |
| InputEvent.ALT_DOWN_MASK | InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.META_DOWN_MASK)) != 0) |
122 |
|| !SwingUtilities.isLeftMouseButton(mouseEvent)) {
|
123 |
return;
|
124 |
} |
125 |
String layerName = ""; |
126 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
127 |
try {
|
128 |
FLyrVect layer = getLayer(); |
129 |
if (layer == null) { |
130 |
String msg = i18nManager.getTranslation("There_is_no_one_and_only_one_active_layer"); |
131 |
ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(msg, |
132 |
i18nManager.getTranslation("attribute_editor"), JOptionPane.INFORMATION_MESSAGE); |
133 |
return;
|
134 |
} |
135 |
layerName = layer.getName(); |
136 |
ViewPort vp = this.mapControl.getViewPort();
|
137 |
Point point = vp.convertToMapPoint(e.getPoint());
|
138 |
double tolerance = vp.toMapDistance(layer.getDefaultTolerance());
|
139 |
FeatureQuery query = queryByPoint(point, tolerance); |
140 |
showOrUpdateForm(layer, query); |
141 |
} catch (Exception ex) { |
142 |
LOGGER.warn("Can't show attribute information from point on the layer " + layerName, ex);
|
143 |
String msg =
|
144 |
i18nManager.getTranslation("Cant_show_attribute_information_for_layer_%1_on_point",
|
145 |
new String[] { layerName }); |
146 |
ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(msg, |
147 |
i18nManager.getTranslation("attribute_editor"), JOptionPane.WARNING_MESSAGE); |
148 |
} |
149 |
} |
150 |
|
151 |
private void showOrUpdateForm(FLyrVect layer, FeatureQuery query) { |
152 |
if( layer == null || query==null ) { |
153 |
return;
|
154 |
} |
155 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
156 |
String layerName = ""; |
157 |
try {
|
158 |
layerName = layer.getName(); |
159 |
FeatureStore featureStore; |
160 |
featureStore = layer.getFeatureStore(); |
161 |
try {
|
162 |
synchronized (featureStore) {
|
163 |
FeatureSet featSet = featureStore.getFeatureSet(query); |
164 |
if( featSet.isEmpty() ) {
|
165 |
return;
|
166 |
} |
167 |
FeatureSelection selection = (FeatureSelection) featureStore.getSelection(); |
168 |
selection.deselectAll(); |
169 |
selection.select(featSet); |
170 |
} |
171 |
} catch (DataException ex) {
|
172 |
LOGGER.warn("Can't query layer " + layerName+", query: "+query, ex); |
173 |
return;
|
174 |
} |
175 |
if (this.form == null) { |
176 |
this.form = getForm(layer, query);
|
177 |
String title = i18n.getTranslation("attribute_editor")+": "+(StringUtils.equalsIgnoreCase(layerName, featureStore.getName())? |
178 |
layerName : layerName+" ("+featureStore.getName()+")"); |
179 |
this.form.showForm(title, MODE.TOOL);
|
180 |
} else if( this.form.getFeatureStore()!=featureStore ) { |
181 |
// Dimension size = this.form.asJComponent().getPreferredSize();
|
182 |
disposeForm(); |
183 |
this.form = getForm(layer, query);
|
184 |
// this.form.asJComponent().setPreferredSize(size);
|
185 |
String title = StringUtils.equalsIgnoreCase(layerName, featureStore.getName())?
|
186 |
layerName : layerName+"/"+featureStore.getName();
|
187 |
this.form.showForm(title, MODE.TOOL);
|
188 |
} |
189 |
this.form.setQuery(query);
|
190 |
} catch (Exception ex) { |
191 |
LOGGER.warn("Can't show attribute information from point on the layer " + layerName, ex);
|
192 |
String msg =
|
193 |
i18n.getTranslation("Cant_show_attribute_information_for_layer_%1_on_point",
|
194 |
new String[] { layerName }); |
195 |
ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(msg, |
196 |
i18n.getTranslation("attribute_editor"), JOptionPane.WARNING_MESSAGE); |
197 |
disposeForm(); |
198 |
} |
199 |
} |
200 |
|
201 |
private void disposeForm() { |
202 |
DisposeUtils.disposeQuietly(this.form);
|
203 |
this.form = null; |
204 |
} |
205 |
|
206 |
public void setRefreshCallback(Predicate<FeatureStore> refreshCallback) { |
207 |
this.refreshCallback = refreshCallback;
|
208 |
} |
209 |
|
210 |
private JFeaturesForm getForm(FLyrVect layer, FeatureQuery query) throws CreateJFeatureFormException, ServiceException, DataException { |
211 |
if (this.form == null) { |
212 |
DataSwingManager swingManager = DALSwingLocator.getSwingManager(); |
213 |
FeatureStore store = layer.getFeatureStore(); |
214 |
JFeaturesForm theForm = swingManager.createJFeaturesForm(store, query); |
215 |
this.form = theForm;
|
216 |
this.form.asJComponent().setName(ATTRIBUTE_EDITOR_FORM_NAME);
|
217 |
this.form.asJComponent().addComponentListener(new ComponentAdapter() { |
218 |
@Override
|
219 |
public void componentHidden(ComponentEvent e) { |
220 |
DisposeUtils.disposeQuietly(theForm); |
221 |
} |
222 |
}); |
223 |
this.form.addActionListener((ActionEvent e) -> { |
224 |
switch (e.getActionCommand()) {
|
225 |
case "formAfterSave": |
226 |
case "formAfterDelete": |
227 |
if( this.refreshCallback!=null ) { |
228 |
this.refreshCallback.test(store);
|
229 |
} |
230 |
break;
|
231 |
} |
232 |
}); |
233 |
} |
234 |
return this.form; |
235 |
} |
236 |
|
237 |
private FeatureQuery queryByPoint(Point point, double tol) throws CreateGeometryException, DataException { |
238 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
239 |
Circle circle = (Circle) manager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D); |
240 |
circle.setPoints(point, tol); |
241 |
|
242 |
FLyrVect currentLayer = this.getLayer();
|
243 |
if( currentLayer==null ) { |
244 |
return null; |
245 |
} |
246 |
FeatureStore featureStore = currentLayer.getFeatureStore(); |
247 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
248 |
FeatureType featureType = featureStore.getDefaultFeatureType(); |
249 |
featureQuery.setFeatureType(featureType); |
250 |
|
251 |
Geometry query_geo = currentLayer.transformToSourceCRS(circle, true);
|
252 |
|
253 |
IProjection query_proj = currentLayer.getMapContext().getProjection(); |
254 |
if (currentLayer.getCoordTrans() != null) { |
255 |
query_proj = currentLayer.getCoordTrans().getPOrig(); |
256 |
} |
257 |
|
258 |
Evaluator iee = SpatialEvaluatorsFactory.getInstance().intersects( |
259 |
query_geo, |
260 |
query_proj, |
261 |
featureStore |
262 |
); |
263 |
featureQuery.setFilter(iee); |
264 |
featureQuery.retrievesAllAttributes(); |
265 |
return featureQuery;
|
266 |
} |
267 |
|
268 |
@Override
|
269 |
public void rectangle(EnvelopeEvent e) throws BehaviorException { |
270 |
MouseEvent mouseEvent = e.getEvent();
|
271 |
if (((mouseEvent.getModifiersEx() & (InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK |
272 |
| InputEvent.ALT_DOWN_MASK | InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.META_DOWN_MASK)) != 0) |
273 |
|| !SwingUtilities.isLeftMouseButton(mouseEvent)) {
|
274 |
return;
|
275 |
} |
276 |
String layerName = ""; |
277 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
278 |
try {
|
279 |
FLyrVect layer = getLayer(); |
280 |
if (layer == null) { |
281 |
String msg = i18nManager.getTranslation("There_is_no_one_and_only_one_active_layer"); |
282 |
ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(msg, |
283 |
i18nManager.getTranslation("attribute_editor"), JOptionPane.INFORMATION_MESSAGE); |
284 |
return;
|
285 |
} |
286 |
layerName = layer.getName(); |
287 |
FeatureQuery query; |
288 |
Geometry geom = e.getWorldCoordRect().getGeometry(); |
289 |
if( geom instanceof Point ) { |
290 |
ViewPort vp = this.mapControl.getViewPort();
|
291 |
double tolerance = vp.toMapDistance(layer.getDefaultTolerance());
|
292 |
query = queryByPoint((Point) geom, tolerance);
|
293 |
} else {
|
294 |
FeatureStore store = layer.getFeatureStore(); |
295 |
FeatureType featureType = store.getDefaultFeatureTypeQuietly(); |
296 |
GeometryExpressionBuilder builder = GeometryExpressionUtils.createExpressionBuilder(); |
297 |
String filter = builder.ST_Intersects(
|
298 |
builder.column(featureType.getDefaultGeometryAttributeName()), |
299 |
builder.geometry(e.getWorldCoordRect().getGeometry()) |
300 |
).toString(); |
301 |
query = store.createFeatureQuery(filter); |
302 |
} |
303 |
showOrUpdateForm(layer, query); |
304 |
} catch (Exception ex) { |
305 |
LOGGER.warn("Can't show attribute information from point on the layer " + layerName, ex);
|
306 |
String msg =
|
307 |
i18nManager.getTranslation("Cant_show_attribute_information_for_layer_%1_on_point",
|
308 |
new String[] { layerName }); |
309 |
ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(msg, |
310 |
i18nManager.getTranslation("attribute_editor"), JOptionPane.WARNING_MESSAGE); |
311 |
} |
312 |
} |
313 |
} |