svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / gui / cad / tools / AutoCompletePolygon.java @ 29097
History | View | Annotate | Download (5.14 KB)
1 |
package com.iver.cit.gvsig.gui.cad.tools; |
---|---|
2 |
|
3 |
import java.awt.Graphics; |
4 |
import java.awt.Graphics2D; |
5 |
|
6 |
import org.gvsig.fmap.dal.exception.ReadException; |
7 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
8 |
import org.gvsig.fmap.dal.feature.Feature; |
9 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
10 |
import org.gvsig.fmap.geom.Geometry; |
11 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
12 |
import org.gvsig.fmap.geom.operation.Draw; |
13 |
import org.gvsig.fmap.geom.operation.DrawOperationContext; |
14 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
15 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
16 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
17 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
18 |
import org.gvsig.fmap.geom.util.Converter; |
19 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
20 |
|
21 |
import com.iver.andami.PluginServices; |
22 |
import com.iver.andami.messages.NotificationManager; |
23 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
24 |
|
25 |
public class AutoCompletePolygon extends PolylineCADTool { |
26 |
|
27 |
@Override
|
28 |
/**
|
29 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
30 |
* encontremos.
|
31 |
*
|
32 |
* @param g Graphics sobre el que dibujar.
|
33 |
* @param selectedGeometries BitSet con las geometr?as seleccionadas.
|
34 |
* @param x par?metro x del punto que se pase para dibujar.
|
35 |
* @param y par?metro x del punto que se pase para dibujar.
|
36 |
*/
|
37 |
public void drawOperation(Graphics g, double x, |
38 |
double y) {
|
39 |
Geometry geom=getGeometry(); |
40 |
if (geom.getHandlers(Geometry.SELECTHANDLER).length ==0 && firstPoint!=null) { |
41 |
GeneralPathX gpx = new GeneralPathX();
|
42 |
gpx.moveTo(firstPoint.getX(), firstPoint.getY()); |
43 |
gpx.lineTo(x, y); |
44 |
DrawOperationContext doc=new DrawOperationContext();
|
45 |
doc.setGraphics((Graphics2D)g);
|
46 |
doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
47 |
doc.setSymbol(DefaultCADTool.geometrySelectSymbol); |
48 |
try {
|
49 |
createCurve(gpx).invokeOperation(Draw.CODE, doc); |
50 |
} catch (GeometryOperationNotSupportedException e) {
|
51 |
// TODO Auto-generated catch block
|
52 |
e.printStackTrace(); |
53 |
} catch (GeometryOperationException e) {
|
54 |
// TODO Auto-generated catch block
|
55 |
e.printStackTrace(); |
56 |
} |
57 |
} |
58 |
else if (geom.getHandlers(Geometry.SELECTHANDLER).length > 1) { |
59 |
GeneralPathX gpxGeom=new GeneralPathX();
|
60 |
gpxGeom.moveTo(x,y); |
61 |
gpxGeom.append(geom.getPathIterator(null,Converter.FLATNESS), true); |
62 |
|
63 |
gpxGeom.closePath(); |
64 |
DrawOperationContext doc=new DrawOperationContext();
|
65 |
doc.setGraphics((Graphics2D)g);
|
66 |
doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
67 |
doc.setSymbol(DefaultCADTool.geometrySelectSymbol); |
68 |
try {
|
69 |
createCurve(gpxGeom).invokeOperation(Draw.CODE, doc); |
70 |
} catch (GeometryOperationNotSupportedException e) {
|
71 |
// TODO Auto-generated catch block
|
72 |
e.printStackTrace(); |
73 |
} catch (GeometryOperationException e) {
|
74 |
// TODO Auto-generated catch block
|
75 |
e.printStackTrace(); |
76 |
} |
77 |
} |
78 |
} |
79 |
|
80 |
|
81 |
private Geometry autoComplete(Geometry digitizedGeom) throws CreateGeometryException { |
82 |
com.vividsolutions.jts.geom.Geometry jtsGeom = null;
|
83 |
DisposableIterator iterator = null;
|
84 |
try {
|
85 |
jtsGeom = (com.vividsolutions.jts.geom.Geometry)digitizedGeom.invokeOperation(ToJTS.CODE, null);
|
86 |
FLyrVect lyrVect = (FLyrVect) getVLE().getLayer(); |
87 |
// Se supone que debe ser r?pido, ya que est? indexado
|
88 |
|
89 |
FeatureSet selected = lyrVect.queryByGeometry(digitizedGeom, |
90 |
lyrVect.getFeatureStore().getDefaultFeatureType()); |
91 |
iterator = selected.iterator(); |
92 |
while (iterator.hasNext()) {
|
93 |
Feature feature = (Feature) iterator.next(); |
94 |
Geometry aux = feature.getDefaultGeometry(); |
95 |
com.vividsolutions.jts.geom.Geometry jtsAux = (com.vividsolutions.jts.geom.Geometry)aux.invokeOperation(ToJTS.CODE, null);
|
96 |
jtsGeom = jtsGeom.difference(jtsAux); |
97 |
} |
98 |
} catch (ReadException e) {
|
99 |
NotificationManager.showMessageError( |
100 |
PluginServices.getText(this, "Error_in_Autocomplete_Polygon_Tool_") |
101 |
+ " " + e.getLocalizedMessage(),
|
102 |
e); |
103 |
} catch (com.vividsolutions.jts.geom.TopologyException e) {
|
104 |
NotificationManager.showMessageError( |
105 |
PluginServices.getText(this, "Error_in_Autocomplete_Polygon_Tool_") |
106 |
+ " " + e.getLocalizedMessage(),
|
107 |
e); |
108 |
} catch (Exception e) { |
109 |
NotificationManager.showMessageError( |
110 |
PluginServices.getText(this, "Error_in_Autocomplete_Polygon_Tool_") |
111 |
+ " " + e.getLocalizedMessage(),
|
112 |
e); |
113 |
} finally {
|
114 |
if (iterator != null) { |
115 |
iterator.dispose(); |
116 |
} |
117 |
} |
118 |
|
119 |
return Converter.jtsToGeometry(jtsGeom);
|
120 |
} |
121 |
|
122 |
@Override
|
123 |
public Feature insertGeometry(Geometry geometry) {
|
124 |
Geometry newGeom; |
125 |
try {
|
126 |
newGeom = autoComplete(geometry); |
127 |
return super.insertGeometry(newGeom); |
128 |
} catch (CreateGeometryException e) {
|
129 |
NotificationManager.addError(e); |
130 |
} |
131 |
return null; |
132 |
} |
133 |
|
134 |
public boolean isApplicable(int shapeType) { |
135 |
switch (shapeType) {
|
136 |
case Geometry.TYPES.SURFACE:
|
137 |
return true; |
138 |
} |
139 |
return false; |
140 |
} |
141 |
} |