svn-gvsig-desktop / tags / v2_0_0_Build_2038 / extensions / extEditing / src / org / gvsig / editing / layers / VectorialLayerEdited.java @ 36805
History | View | Annotate | Download (16.5 KB)
1 |
package org.gvsig.editing.layers; |
---|---|
2 |
|
3 |
import java.awt.Image; |
4 |
import java.awt.geom.Point2D; |
5 |
import java.util.ArrayList; |
6 |
import java.util.ConcurrentModificationException; |
7 |
import java.util.EmptyStackException; |
8 |
|
9 |
import org.slf4j.Logger; |
10 |
import org.slf4j.LoggerFactory; |
11 |
|
12 |
import org.gvsig.andami.PluginServices; |
13 |
import org.gvsig.andami.messages.NotificationManager; |
14 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
15 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
16 |
import org.gvsig.editing.CADExtension; |
17 |
import org.gvsig.editing.EditionChangeManager; |
18 |
import org.gvsig.editing.StartEditing; |
19 |
import org.gvsig.editing.ViewCommandStackExtension; |
20 |
import org.gvsig.editing.gui.cad.CADTool; |
21 |
import org.gvsig.editing.gui.cad.tools.SelectionCADTool; |
22 |
import org.gvsig.editing.gui.cad.tools.select.SelectRowPanel; |
23 |
import org.gvsig.fmap.dal.exception.DataException; |
24 |
import org.gvsig.fmap.dal.exception.ReadException; |
25 |
import org.gvsig.fmap.dal.feature.Feature; |
26 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
27 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
28 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
29 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
32 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
33 |
import org.gvsig.fmap.geom.GeometryLocator; |
34 |
import org.gvsig.fmap.geom.GeometryManager; |
35 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
36 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
37 |
import org.gvsig.fmap.geom.handler.Handler; |
38 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
39 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
40 |
import org.gvsig.fmap.geom.primitive.Circle; |
41 |
import org.gvsig.fmap.geom.primitive.Envelope; |
42 |
import org.gvsig.fmap.geom.primitive.Point; |
43 |
import org.gvsig.fmap.mapcontext.ViewPort; |
44 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
45 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
46 |
import org.gvsig.fmap.mapcontext.layers.LayerDrawEvent; |
47 |
import org.gvsig.fmap.mapcontext.layers.LayerDrawingListener; |
48 |
import org.gvsig.fmap.mapcontext.layers.LayerEvent; |
49 |
import org.gvsig.fmap.mapcontext.layers.vectorial.ContainsGeometryEvaluator; |
50 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
51 |
import org.gvsig.fmap.mapcontext.layers.vectorial.IntersectsGeometryEvaluator; |
52 |
import org.gvsig.fmap.mapcontext.layers.vectorial.OutGeometryEvaluator; |
53 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
54 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
55 |
import org.gvsig.fmap.mapcontrol.MapControl; |
56 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
57 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
58 |
import org.gvsig.fmap.mapcontrol.MapControlManager; |
59 |
import org.gvsig.tools.dispose.DisposableIterator; |
60 |
import org.gvsig.tools.dispose.DisposeUtils; |
61 |
import org.gvsig.tools.evaluator.Evaluator; |
62 |
import org.gvsig.tools.observer.Observable; |
63 |
import org.gvsig.tools.observer.Observer; |
64 |
|
65 |
public class VectorialLayerEdited extends DefaultLayerEdited implements |
66 |
LayerDrawingListener, Observer {
|
67 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
68 |
private static final Logger LOG = LoggerFactory.getLogger(VectorialLayerEdited.class); |
69 |
private static final MapControlManager MAP_CONTROL_MANAGER = MapControlLocator.getMapControlManager(); |
70 |
private ArrayList selectedHandler = new ArrayList(); |
71 |
private Point2D lastPoint; |
72 |
private Point2D firstPoint; |
73 |
private CADTool cadtool = null; |
74 |
|
75 |
private ILegend legend;
|
76 |
private Image imageSelection; |
77 |
private Image imageHandlers; |
78 |
private EditionChangeManager echm=null; |
79 |
private MapControl mapControl = null; |
80 |
|
81 |
public VectorialLayerEdited(FLayer lyr, MapControl mapControl) {
|
82 |
super(lyr);
|
83 |
this.mapControl = mapControl;
|
84 |
lyr.getMapContext().addLayerDrawingListener(this);
|
85 |
// Por defecto, siempre hacemos snapping sobre la capa en edici?n.
|
86 |
lyr.getMapContext().getLayersToSnap().add(lyr); |
87 |
((FLyrVect) lyr).getFeatureStore().addObserver(this);
|
88 |
} |
89 |
|
90 |
public ArrayList getSelectedHandler() { |
91 |
return selectedHandler;
|
92 |
} |
93 |
|
94 |
public void clearSelection() throws DataException { |
95 |
if (getFeatureStore() == null) { |
96 |
return;
|
97 |
} |
98 |
FeatureSelection selection = getFeatureStore().getFeatureSelection(); |
99 |
selectedHandler.clear(); |
100 |
selection.deselectAll(); |
101 |
} |
102 |
|
103 |
public void selectWithPoint(double x, double y, boolean multipleSelection) { |
104 |
FeatureSet set = null;
|
105 |
try {
|
106 |
firstPoint = new Point2D.Double(x, y); |
107 |
FeatureStore featureStore = getFeatureStore(); |
108 |
// Se comprueba si se pincha en una gemometr?a
|
109 |
ViewPort viewPort = getLayer().getMapContext().getViewPort(); |
110 |
double tol = viewPort.toMapDistance(MAP_CONTROL_MANAGER.getTolerance());
|
111 |
|
112 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
113 |
Point center = (org.gvsig.fmap.geom.primitive.Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
|
114 |
center.setX(x); |
115 |
center.setY(y); |
116 |
Circle circle = (Circle)geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D); |
117 |
circle.setPoints(center, tol); |
118 |
|
119 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
120 |
featureQuery.setAttributeNames(new String[]{featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName()}); |
121 |
Evaluator evaluator = new IntersectsGeometryEvaluator(circle, viewPort.getProjection(), featureStore.getDefaultFeatureType(), featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName());
|
122 |
featureQuery.setFilter(evaluator); |
123 |
set = featureStore.getFeatureSet(featureQuery); |
124 |
if (!multipleSelection && set.getSize()>1){ |
125 |
SelectRowPanel selectionPanel = new SelectRowPanel(set, this); |
126 |
PluginServices.getMDIManager().addCentredWindow(selectionPanel); |
127 |
} |
128 |
else {
|
129 |
selectGeometries(featureStore, set, viewPort, multipleSelection); |
130 |
} |
131 |
} catch (ReadException e) {
|
132 |
NotificationManager.addError(e.getMessage(), e); |
133 |
} catch (GeometryOperationNotSupportedException e) {
|
134 |
NotificationManager.addError(e.getMessage(), e); |
135 |
} catch (GeometryOperationException e) {
|
136 |
NotificationManager.addError(e.getMessage(), e); |
137 |
} catch (DataException e) {
|
138 |
NotificationManager.addError(e.getMessage(), e); |
139 |
} catch (CreateGeometryException e) {
|
140 |
NotificationManager.addError(e.getMessage(), e); |
141 |
} finally {
|
142 |
set.dispose(); |
143 |
} |
144 |
} |
145 |
|
146 |
public void selectWithSecondPoint(double x, double y) { |
147 |
FeatureSet set = null;
|
148 |
try {
|
149 |
FeatureStore featureStore = getFeatureStore(); |
150 |
lastPoint = new Point2D.Double(x, y); |
151 |
ViewPort viewPort = getLayer().getMapContext().getViewPort(); |
152 |
double x1;
|
153 |
double y1;
|
154 |
double w1;
|
155 |
double h1;
|
156 |
|
157 |
if (firstPoint.getX() < lastPoint.getX()) {
|
158 |
x1 = firstPoint.getX(); |
159 |
w1 = lastPoint.getX() - firstPoint.getX(); |
160 |
} else {
|
161 |
x1 = lastPoint.getX(); |
162 |
w1 = firstPoint.getX() - lastPoint.getX(); |
163 |
} |
164 |
|
165 |
if (firstPoint.getY() < lastPoint.getY()) {
|
166 |
y1 = firstPoint.getY(); |
167 |
h1 = lastPoint.getY() - firstPoint.getY(); |
168 |
} else {
|
169 |
y1 = lastPoint.getY(); |
170 |
h1 = firstPoint.getY() - lastPoint.getY(); |
171 |
} |
172 |
|
173 |
Envelope envelope = geomManager.createEnvelope(x1, y1, x1 + w1, y1 + h1, SUBTYPES.GEOM2D); |
174 |
|
175 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
176 |
featureQuery.setAttributeNames(new String[]{featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName()}); |
177 |
Evaluator evaluator=null;
|
178 |
if (firstPoint.getX() < lastPoint.getX()) {
|
179 |
evaluator = new IntersectsGeometryEvaluator(envelope.getGeometry(), viewPort.getProjection(), featureStore.getDefaultFeatureType(), featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName());
|
180 |
}else{
|
181 |
evaluator = new IntersectsGeometryEvaluator(envelope.getGeometry(), viewPort.getProjection(), featureStore.getDefaultFeatureType(), featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName());
|
182 |
} |
183 |
featureQuery.setFilter(evaluator); |
184 |
set = featureStore.getFeatureSet(featureQuery); |
185 |
selectGeometries(featureStore, set, viewPort, false);
|
186 |
} catch (ReadException e) {
|
187 |
NotificationManager.addError(e.getMessage(), e); |
188 |
} catch (GeometryOperationNotSupportedException e) {
|
189 |
NotificationManager.addError(e.getMessage(), e); |
190 |
} catch (GeometryOperationException e) {
|
191 |
NotificationManager.addError(e.getMessage(), e); |
192 |
} catch (DataException e) {
|
193 |
NotificationManager.addError(e.getMessage(), e); |
194 |
} catch (CreateEnvelopeException e) {
|
195 |
NotificationManager.addError(e.getMessage(), e); |
196 |
} finally {
|
197 |
DisposeUtils.dispose(set); |
198 |
} |
199 |
} |
200 |
|
201 |
public void selectContainsSurface(org.gvsig.fmap.geom.Geometry polygon) { |
202 |
FeatureSet set = null;
|
203 |
try {
|
204 |
FeatureStore featureStore = getFeatureStore(); |
205 |
ViewPort viewPort = getLayer().getMapContext().getViewPort(); |
206 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
207 |
featureQuery.setAttributeNames(new String[]{featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName()}); |
208 |
Evaluator evaluator = new ContainsGeometryEvaluator(polygon, viewPort.getProjection(), featureStore.getDefaultFeatureType(), featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName());
|
209 |
featureQuery.setFilter(evaluator); |
210 |
set = featureStore.getFeatureSet(featureQuery); |
211 |
selectGeometries(featureStore, set, viewPort, false);
|
212 |
} catch (ReadException e) {
|
213 |
NotificationManager.addError(e.getMessage(), e); |
214 |
} catch (DataException e) {
|
215 |
NotificationManager.addError(e.getMessage(), e); |
216 |
} catch (GeometryOperationNotSupportedException e) {
|
217 |
NotificationManager.addError(e.getMessage(), e); |
218 |
} catch (GeometryOperationException e) {
|
219 |
NotificationManager.addError(e.getMessage(), e); |
220 |
} finally {
|
221 |
DisposeUtils.dispose(set); |
222 |
} |
223 |
} |
224 |
|
225 |
private void selectGeometries(FeatureStore featureStore, FeatureSet set, ViewPort vp, boolean multipleSelection) throws DataException, GeometryOperationNotSupportedException, GeometryOperationException{ |
226 |
FeatureSelection featureSelection = null;
|
227 |
if (multipleSelection) {
|
228 |
featureSelection = (FeatureSelection) featureStore.getSelection(); |
229 |
featureSelection.select(set); |
230 |
} else {
|
231 |
featureSelection = featureStore.createFeatureSelection(); |
232 |
featureSelection.select(set); |
233 |
featureStore.setSelection(featureSelection); |
234 |
} |
235 |
//Draw the geometries with the edition symbology
|
236 |
|
237 |
} |
238 |
|
239 |
public void selectIntersectsSurface(org.gvsig.fmap.geom.Geometry polygon) { |
240 |
FeatureSet set = null;
|
241 |
try {
|
242 |
FeatureStore featureStore = getFeatureStore(); |
243 |
ViewPort vp = getLayer().getMapContext().getViewPort(); |
244 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
245 |
featureQuery.setAttributeNames(new String[]{featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName()}); |
246 |
Evaluator evaluator = new IntersectsGeometryEvaluator(polygon, vp.getProjection(), featureStore.getDefaultFeatureType(), featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName());
|
247 |
featureQuery.setFilter(evaluator); |
248 |
set = featureStore.getFeatureSet(featureQuery); |
249 |
selectGeometries(featureStore,set,vp,false);
|
250 |
} catch (ReadException e) {
|
251 |
NotificationManager.addError(e.getMessage(), e); |
252 |
} catch (DataException e) {
|
253 |
NotificationManager.addError(e.getMessage(), e); |
254 |
} catch (GeometryOperationNotSupportedException e) {
|
255 |
NotificationManager.addError(e.getMessage(), e); |
256 |
} catch (GeometryOperationException e) {
|
257 |
NotificationManager.addError(e.getMessage(), e); |
258 |
} finally {
|
259 |
DisposeUtils.dispose(set); |
260 |
} |
261 |
} |
262 |
|
263 |
public void selectOutPolygon(org.gvsig.fmap.geom.Geometry polygon) { |
264 |
FeatureSet set = null;
|
265 |
try {
|
266 |
FeatureStore featureStore = getFeatureStore(); |
267 |
ViewPort vp = getLayer().getMapContext().getViewPort(); |
268 |
FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
269 |
featureQuery.setAttributeNames(new String[]{featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName()}); |
270 |
Evaluator evaluator=new OutGeometryEvaluator(polygon, vp.getProjection(), featureStore.getDefaultFeatureType(), featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName());
|
271 |
featureQuery.setFilter(evaluator); |
272 |
set = featureStore.getFeatureSet(featureQuery); |
273 |
selectGeometries(featureStore, set, vp, false);
|
274 |
|
275 |
} catch (ReadException e) {
|
276 |
NotificationManager.addError(e.getMessage(), e); |
277 |
} catch (DataException e) {
|
278 |
NotificationManager.addError(e.getMessage(), e); |
279 |
} catch (GeometryOperationNotSupportedException e) {
|
280 |
NotificationManager.addError(e.getMessage(), e); |
281 |
} catch (GeometryOperationException e) {
|
282 |
NotificationManager.addError(e.getMessage(), e); |
283 |
} finally {
|
284 |
DisposeUtils.dispose(set); |
285 |
} |
286 |
} |
287 |
|
288 |
public void selectAll() { |
289 |
FeatureSet set = null;
|
290 |
try {
|
291 |
FeatureStore featureStore = getFeatureStore(); |
292 |
ViewPort viewPort = getLayer().getMapContext().getViewPort(); |
293 |
set = featureStore.getFeatureSet(); |
294 |
selectGeometries(featureStore, set, viewPort, false);
|
295 |
} catch (ReadException e) {
|
296 |
NotificationManager.addError(e.getMessage(), e); |
297 |
} catch (DataException e) {
|
298 |
NotificationManager.addError(e.getMessage(), e); |
299 |
} catch (GeometryOperationNotSupportedException e) {
|
300 |
NotificationManager.addError(e.getMessage(), e); |
301 |
} catch (GeometryOperationException e) {
|
302 |
NotificationManager.addError(e.getMessage(), e); |
303 |
} finally {
|
304 |
DisposeUtils.dispose(set); |
305 |
} |
306 |
} |
307 |
|
308 |
public void drawHandlers(org.gvsig.fmap.geom.Geometry geom, |
309 |
ViewPort vp) { |
310 |
Handler[] handlers = geom.getHandlers(org.gvsig.fmap.geom.Geometry.SELECTHANDLER); |
311 |
mapControl.getMapControlDrawer().drawHandlers(handlers, vp.getAffineTransform(), |
312 |
MAP_CONTROL_MANAGER.getHandlerSymbol()); |
313 |
} |
314 |
|
315 |
public Image getSelectionImage() { |
316 |
return imageSelection;
|
317 |
} |
318 |
|
319 |
public void setSelectionImage(Image image) { |
320 |
imageSelection = image; |
321 |
} |
322 |
|
323 |
public Image getHandlersImage() { |
324 |
return imageHandlers;
|
325 |
} |
326 |
|
327 |
public void setHandlersImage(Image image) { |
328 |
imageHandlers = image; |
329 |
} |
330 |
|
331 |
public void beforeLayerDraw(LayerDrawEvent e) throws CancelationException { |
332 |
|
333 |
} |
334 |
|
335 |
public void afterLayerDraw(LayerDrawEvent e) throws CancelationException { |
336 |
//Draw the selected geometries...
|
337 |
FeatureStore featureStore = null;
|
338 |
DisposableIterator iterator = null;
|
339 |
try {
|
340 |
featureStore = getFeatureStore(); |
341 |
if (featureStore.isEditing()) {
|
342 |
ViewPort viewPort = getLayer().getMapContext().getViewPort(); |
343 |
iterator = featureStore.getFeatureSelection().fastIterator(); |
344 |
MapControlDrawer mapControlDrawer = mapControl.getMapControlDrawer(); |
345 |
try {
|
346 |
while (iterator.hasNext()) {
|
347 |
Feature feature = (Feature) iterator.next(); |
348 |
Geometry geometry = feature.getDefaultGeometry(); |
349 |
mapControlDrawer.startDrawing(this);
|
350 |
mapControlDrawer.setGraphics(e.getGraphics()); |
351 |
drawHandlers(geometry.cloneGeometry(), viewPort); |
352 |
mapControlDrawer.stopDrawing(this);
|
353 |
} |
354 |
} catch (ConcurrentModificationException e1) { |
355 |
// throw new CancelationException(e1);
|
356 |
// A mitad de pintado se cambia la selecci?n y por tanto no
|
357 |
// se puede seguir recorriendo la anterior.
|
358 |
return;
|
359 |
} |
360 |
} |
361 |
} catch (Exception e2) { |
362 |
LOG.error("Error drawing the selected geometry", e2);
|
363 |
} finally {
|
364 |
if (iterator != null) { |
365 |
iterator.dispose(); |
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
public void beforeGraphicLayerDraw(LayerDrawEvent e) |
371 |
throws CancelationException {
|
372 |
} |
373 |
|
374 |
public void afterLayerGraphicDraw(LayerDrawEvent e) |
375 |
throws CancelationException {
|
376 |
} |
377 |
|
378 |
public void activationGained(LayerEvent e) { |
379 |
if (ViewCommandStackExtension.csd != null) { |
380 |
ViewCommandStackExtension.csd.setModel(((FLyrVect) getLayer()) |
381 |
.getFeatureStore()); |
382 |
|
383 |
} |
384 |
IWindow window = PluginServices.getMDIManager().getActiveWindow(); |
385 |
if (window instanceof DefaultViewPanel) { |
386 |
DefaultViewPanel view = (DefaultViewPanel) window; |
387 |
if (e.getSource().isEditing()) {
|
388 |
view.showConsole(); |
389 |
} |
390 |
} |
391 |
if (cadtool != null) { |
392 |
CADExtension.getCADToolAdapter().setCadTool(cadtool); |
393 |
PluginServices.getMainFrame().setSelectedTool(cadtool.toString()); |
394 |
StartEditing.startCommandsApplicable(null, (FLyrVect) getLayer());
|
395 |
CADExtension.initFocus(); |
396 |
} |
397 |
|
398 |
} |
399 |
|
400 |
public void activationLost(LayerEvent e) { |
401 |
try {
|
402 |
cadtool = CADExtension.getCADTool(); |
403 |
IWindow window = PluginServices.getMDIManager().getActiveWindow(); |
404 |
if (window instanceof DefaultViewPanel) { |
405 |
DefaultViewPanel view = (DefaultViewPanel) window; |
406 |
view.hideConsole(); |
407 |
} |
408 |
} catch (EmptyStackException e1) { |
409 |
cadtool = new SelectionCADTool();
|
410 |
cadtool.init(); |
411 |
} |
412 |
|
413 |
} |
414 |
|
415 |
public void setLegend(ILegend legend) { |
416 |
this.legend = legend;
|
417 |
} |
418 |
|
419 |
public ILegend getLegend() {
|
420 |
return legend;
|
421 |
} |
422 |
|
423 |
public FeatureStore getFeatureStore() throws ReadException { |
424 |
return ((FLyrVect) getLayer()).getFeatureStore();
|
425 |
} |
426 |
|
427 |
public void update(Observable observable, Object notification) { |
428 |
echm.update(observable, notification); |
429 |
} |
430 |
|
431 |
public EditionChangeManager getEditionChangeManager() {
|
432 |
return echm;
|
433 |
} |
434 |
|
435 |
public void setEditionChangeManager(EditionChangeManager echm) { |
436 |
this.echm = echm;
|
437 |
} |
438 |
} |