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