gvsig-attributeeditor / org.gvsig.attributeeditor / trunk / org.gvsig.attributeeditor / org.gvsig.attributeeditor.mapcontrol / src / main / java / org / gvsig / fmap / mapcontrol / tools / Listeners / AttributeEditorPointListener.java @ 95
History | View | Annotate | Download (9.15 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.ComponentEvent; |
28 |
import java.awt.event.ComponentListener; |
29 |
import java.awt.event.InputEvent; |
30 |
import java.awt.event.MouseEvent; |
31 |
|
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.SwingUtilities; |
34 |
|
35 |
import org.cresques.cts.IProjection; |
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
import org.gvsig.featureform.swing.CreateJFeatureFormException; |
40 |
import org.gvsig.featureform.swing.JFeatureForm; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
43 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
44 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
45 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
47 |
import org.gvsig.fmap.dal.swing.DALSwingLocator; |
48 |
import org.gvsig.fmap.dal.swing.DataSwingManager; |
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
51 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
52 |
import org.gvsig.fmap.geom.GeometryLocator; |
53 |
import org.gvsig.fmap.geom.GeometryManager; |
54 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
55 |
import org.gvsig.fmap.geom.primitive.Circle; |
56 |
import org.gvsig.fmap.geom.primitive.Point; |
57 |
import org.gvsig.fmap.mapcontext.ViewPort; |
58 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
59 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
60 |
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsGeometryEvaluator; |
61 |
import org.gvsig.fmap.mapcontrol.MapControl; |
62 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
63 |
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent; |
64 |
import org.gvsig.tools.ToolsLocator; |
65 |
import org.gvsig.tools.i18n.I18nManager; |
66 |
import org.gvsig.tools.service.ServiceException; |
67 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
68 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE; |
69 |
|
70 |
/**
|
71 |
* @author fdiaz
|
72 |
*
|
73 |
*/
|
74 |
public class AttributeEditorPointListener extends AbstractPointListener implements ComponentListener { |
75 |
|
76 |
private static Logger logger = LoggerFactory.getLogger(AttributeEditorPointListener.class); |
77 |
|
78 |
public static final String ATTRIBUTE_EDITOR_TOOL_NAME = "attributeEditorTool"; |
79 |
private static final String ATTRIBUTE_EDITOR_FORM_NAME = "attributeEditorForm"; |
80 |
private MapControl mapControl;
|
81 |
private JFeatureForm form;
|
82 |
private FLyrVect currentLayer = null; |
83 |
/**
|
84 |
* Radius as tolerance around the selected point, the area will be used to
|
85 |
* look for information.
|
86 |
*/
|
87 |
private static int TOL = 7; |
88 |
|
89 |
public AttributeEditorPointListener(MapControl mapControl, JFeatureForm form) {
|
90 |
super();
|
91 |
this.mapControl = mapControl;
|
92 |
this.form = form;
|
93 |
} |
94 |
|
95 |
public AttributeEditorPointListener(MapControl mapControl) {
|
96 |
super();
|
97 |
this.mapControl = mapControl;
|
98 |
} |
99 |
|
100 |
private FLyrVect getLayer() {
|
101 |
FLayer[] layers = mapControl.getMapContext().getLayers().getActives();
|
102 |
if (layers == null || layers.length != 1) { |
103 |
return null; |
104 |
} |
105 |
return (FLyrVect) layers[0]; |
106 |
|
107 |
} |
108 |
|
109 |
public void point(PointEvent e) throws BehaviorException { |
110 |
MouseEvent mouseEvent = e.getEvent();
|
111 |
if (((mouseEvent.getModifiersEx() & (InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK |
112 |
| InputEvent.ALT_DOWN_MASK | InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.META_DOWN_MASK)) != 0) |
113 |
|| !SwingUtilities.isLeftMouseButton(mouseEvent)) {
|
114 |
return;
|
115 |
} |
116 |
|
117 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
118 |
String layerName = ""; |
119 |
try {
|
120 |
FLyrVect layer = getLayer(); |
121 |
if (layer == null) { |
122 |
String msg = i18nManager.getTranslation("There_is_no_one_and_only_one_active_layer"); |
123 |
ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(msg, |
124 |
i18nManager.getTranslation("attribute_editor"), JOptionPane.INFORMATION_MESSAGE); |
125 |
return;
|
126 |
} |
127 |
FeatureStore featureStore; |
128 |
featureStore = layer.getFeatureStore(); |
129 |
if (layer != this.currentLayer) { |
130 |
if (this.currentLayer != null) { |
131 |
if (this.form != null) { |
132 |
this.form.saveChanges();
|
133 |
} |
134 |
} |
135 |
this.currentLayer = layer;
|
136 |
if (this.form != null) { |
137 |
this.form.bind(featureStore);
|
138 |
} |
139 |
} |
140 |
layerName = layer.getName(); |
141 |
ViewPort vp = this.mapControl.getViewPort();
|
142 |
Point point = vp.convertToMapPoint(e.getPoint());
|
143 |
double tolerance = vp.toMapDistance(TOL);
|
144 |
FeatureQuery query = null;
|
145 |
query = queryByPoint(point, tolerance); |
146 |
if (query != null) { |
147 |
if (this.form == null) { |
148 |
this.form = getForm();
|
149 |
this.form.showForm(MODE.TOOL);
|
150 |
} |
151 |
|
152 |
if (query != null) { |
153 |
try {
|
154 |
synchronized (featureStore) {
|
155 |
FeatureSet featSet = featureStore.getFeatureSet(query); |
156 |
FeatureSelection selection = (FeatureSelection) featureStore.getSelection(); |
157 |
selection.deselectAll(); |
158 |
selection.select(featSet); |
159 |
} |
160 |
} catch (DataException ex) {
|
161 |
throw new RuntimeException("Error getting a feature set with the query " + query); |
162 |
} |
163 |
} |
164 |
this.form.setQuery(query);
|
165 |
} |
166 |
} catch (Exception ex) { |
167 |
logger.warn("Can't show attribute information from point on the layer " + layerName, ex);
|
168 |
String msg =
|
169 |
i18nManager.getTranslation("Cant_show_attribute_information_for_layer_%1_on_point",
|
170 |
new String[] { layerName }); |
171 |
ToolsSwingLocator.getThreadSafeDialogsManager().messageDialog(msg, |
172 |
i18nManager.getTranslation("attribute_editor"), JOptionPane.WARNING_MESSAGE); |
173 |
} |
174 |
} |
175 |
|
176 |
private JFeatureForm getForm() throws CreateJFeatureFormException, ServiceException, DataException { |
177 |
if (this.form == null) { |
178 |
DataSwingManager swingManager = DALSwingLocator.getSwingManager(); |
179 |
FeatureStore store = this.currentLayer.getFeatureStore();
|
180 |
this.form = swingManager.createJFeatureForm(store);
|
181 |
this.form.setPreferredSize(new Dimension(300, 200)); |
182 |
this.form.asJComponent().setName(ATTRIBUTE_EDITOR_FORM_NAME);
|
183 |
this.form.asJComponent().addComponentListener(this); |
184 |
} |
185 |
return this.form; |
186 |
} |
187 |
|
188 |
private FeatureQuery queryByPoint(Point point, double tol) throws CreateGeometryException, DataException { |
189 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
190 |
Circle circle = (Circle) manager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D); |
191 |
circle.setPoints(point, tol); |
192 |
|
193 |
FeatureStore featureStore = this.currentLayer.getFeatureStore();
|
194 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
195 |
FeatureType featureType = featureStore.getDefaultFeatureType(); |
196 |
String geomName = featureType.getDefaultGeometryAttributeName();
|
197 |
featureQuery.setFeatureType(featureType); |
198 |
|
199 |
Geometry query_geo = this.currentLayer.transformToSourceCRS(circle, true); |
200 |
|
201 |
IProjection query_proj = this.currentLayer.getMapContext().getProjection();
|
202 |
if (this.currentLayer.getCoordTrans() != null) { |
203 |
query_proj = this.currentLayer.getCoordTrans().getPOrig();
|
204 |
} |
205 |
|
206 |
IntersectsGeometryEvaluator iee = |
207 |
new IntersectsGeometryEvaluator(query_geo, query_proj, featureStore.getDefaultFeatureType(), geomName);
|
208 |
featureQuery.setFilter(iee); |
209 |
featureQuery.setAttributeNames(null);
|
210 |
return featureQuery;
|
211 |
} |
212 |
|
213 |
public void componentResized(ComponentEvent e) { |
214 |
// TODO Auto-generated method stub
|
215 |
|
216 |
} |
217 |
|
218 |
public void componentMoved(ComponentEvent e) { |
219 |
// TODO Auto-generated method stub
|
220 |
|
221 |
} |
222 |
|
223 |
public void componentShown(ComponentEvent e) { |
224 |
// TODO Auto-generated method stub
|
225 |
|
226 |
} |
227 |
|
228 |
public void componentHidden(ComponentEvent e) { |
229 |
this.form = null; |
230 |
} |
231 |
|
232 |
} |