root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / InternalPolygonCADTool.java @ 38539
History | View | Annotate | Download (13.1 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.editing.gui.cad.tools; |
23 |
|
24 |
import java.awt.Component; |
25 |
import java.awt.event.InputEvent; |
26 |
import java.awt.event.MouseEvent; |
27 |
import java.util.ArrayList; |
28 |
import java.util.List; |
29 |
|
30 |
import javax.swing.JOptionPane; |
31 |
|
32 |
import com.vividsolutions.jts.geom.GeometryCollection; |
33 |
|
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
import org.gvsig.andami.PluginServices; |
38 |
import org.gvsig.andami.messages.NotificationManager; |
39 |
import org.gvsig.editing.CADExtension; |
40 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
41 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
42 |
import org.gvsig.editing.gui.cad.tools.smc.InternalPolygonCADToolContext; |
43 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
44 |
import org.gvsig.fmap.dal.exception.DataException; |
45 |
import org.gvsig.fmap.dal.exception.ReadException; |
46 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
47 |
import org.gvsig.fmap.dal.feature.Feature; |
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.geom.Geometry; |
52 |
import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
53 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
54 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
55 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
56 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
57 |
import org.gvsig.fmap.geom.primitive.Point; |
58 |
import org.gvsig.fmap.geom.primitive.Surface; |
59 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
60 |
import org.gvsig.tools.dispose.DisposableIterator; |
61 |
import org.gvsig.tools.dispose.DisposeUtils; |
62 |
|
63 |
/**
|
64 |
* DOCUMENT ME!
|
65 |
*
|
66 |
* @author Vicente Caballero Navarro
|
67 |
*/
|
68 |
public class InternalPolygonCADTool extends DefaultCADTool { |
69 |
private static final Logger LOG = LoggerFactory |
70 |
.getLogger(InternalPolygonCADTool.class); |
71 |
|
72 |
protected InternalPolygonCADToolContext _fsm;
|
73 |
protected List<Point> points = new ArrayList<Point>(); |
74 |
protected Geometry geometry = null; |
75 |
|
76 |
/**
|
77 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
78 |
* carga previa a la utilizaci?n de la herramienta.
|
79 |
*/
|
80 |
public void init() { |
81 |
_fsm = new InternalPolygonCADToolContext(this); |
82 |
points.clear(); |
83 |
} |
84 |
|
85 |
/*
|
86 |
* (non-Javadoc)
|
87 |
*
|
88 |
* @see
|
89 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
90 |
* .layers.FBitSet, double, double)
|
91 |
*/
|
92 |
public void transition(double x, double y, InputEvent event) { |
93 |
_fsm.addPoint(x, y, event); |
94 |
} |
95 |
|
96 |
/*
|
97 |
* (non-Javadoc)
|
98 |
*
|
99 |
* @see
|
100 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
101 |
* .layers.FBitSet, double)
|
102 |
*/
|
103 |
public void transition(double d) { |
104 |
_fsm.addValue(d); |
105 |
} |
106 |
|
107 |
/*
|
108 |
* (non-Javadoc)
|
109 |
*
|
110 |
* @see
|
111 |
* com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap
|
112 |
* .layers.FBitSet, java.lang.String)
|
113 |
*/
|
114 |
public void transition(String s) throws CommandException { |
115 |
if (!super.changeCommand(s)) { |
116 |
_fsm.addOption(s); |
117 |
} |
118 |
} |
119 |
|
120 |
/**
|
121 |
* DOCUMENT ME!
|
122 |
*/
|
123 |
public void selection() { |
124 |
FeatureSet selection = null;
|
125 |
try {
|
126 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
127 |
|
128 |
if (selection.getSize() == 0 |
129 |
&& !CADExtension |
130 |
.getCADTool() |
131 |
.getClass() |
132 |
.getName() |
133 |
.equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
134 |
CADExtension.setCADTool("_selection", false); |
135 |
((SelectionCADTool) CADExtension.getCADTool()) |
136 |
.setNextTool("_internalpolygon");
|
137 |
} |
138 |
} catch (ReadException e) {
|
139 |
// TODO Auto-generated catch block
|
140 |
e.printStackTrace(); |
141 |
} catch (DataException e) {
|
142 |
// TODO Auto-generated catch block
|
143 |
e.printStackTrace(); |
144 |
} |
145 |
} |
146 |
|
147 |
/**
|
148 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
149 |
* editableFeatureSource que ya estar? creado.
|
150 |
*
|
151 |
* @param x
|
152 |
* par?metro x del punto que se pase en esta transici?n.
|
153 |
* @param y
|
154 |
* par?metro y del punto que se pase en esta transici?n.
|
155 |
*/
|
156 |
public void addPoint(double x, double y, InputEvent event) { |
157 |
if (((MouseEvent) event).getClickCount() == 2) { |
158 |
addOption(PluginServices |
159 |
.getText(this, "InternalPolygonCADTool.end")); |
160 |
return;
|
161 |
} |
162 |
VectorialLayerEdited vle = getVLE(); |
163 |
FeatureSet featureCollection = null;
|
164 |
DisposableIterator iterator = null;
|
165 |
try {
|
166 |
featureCollection = |
167 |
(FeatureSet) vle.getFeatureStore().getSelection(); |
168 |
if (featureCollection.getSize() == 1) { |
169 |
iterator = featureCollection.iterator(); |
170 |
Feature feature = (Feature) iterator.next(); |
171 |
geometry = (feature.getDefaultGeometry()).cloneGeometry(); |
172 |
if (geometry.contains(x, y)) {
|
173 |
points.add(createPoint(x, y)); |
174 |
} else {
|
175 |
JOptionPane
|
176 |
.showMessageDialog( |
177 |
((Component) PluginServices.getMainFrame()),
|
178 |
PluginServices |
179 |
.getText(this,
|
180 |
"debe_insertar_el_punto_dentro_de_los_limites_de_la_geometria"));
|
181 |
} |
182 |
} |
183 |
} catch (ReadException e) {
|
184 |
NotificationManager.addError(e.getMessage(), e); |
185 |
} catch (DataException e) {
|
186 |
NotificationManager.addError(e.getMessage(), e); |
187 |
} finally {
|
188 |
DisposeUtils.dispose(iterator); |
189 |
} |
190 |
} |
191 |
|
192 |
/**
|
193 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
194 |
* encontremos.
|
195 |
*
|
196 |
* @param g
|
197 |
* Graphics sobre el que dibujar.
|
198 |
* @param x
|
199 |
* par?metro x del punto que se pase para dibujar.
|
200 |
* @param y
|
201 |
* par?metro x del punto que se pase para dibujar.
|
202 |
*/
|
203 |
public void drawOperation(MapControlDrawer renderer, double x, double y) { |
204 |
GeneralPathX gpx = new GeneralPathX();
|
205 |
GeneralPathX gpx1 = new GeneralPathX();
|
206 |
|
207 |
if (points.size() > 0) { |
208 |
for (int i = 0; i < points.size(); i++) { |
209 |
if (i == 0) { |
210 |
gpx.moveTo(points.get(i)); |
211 |
gpx1.moveTo(points.get(i)); |
212 |
} else {
|
213 |
gpx.lineTo(points.get(i)); |
214 |
gpx1.lineTo(points.get(i)); |
215 |
} |
216 |
|
217 |
} |
218 |
gpx.lineTo(createPoint(x, y)); |
219 |
gpx.closePath(); |
220 |
gpx1.closePath(); |
221 |
|
222 |
Geometry geom = createSurface(gpx); |
223 |
Geometry geom1 = createSurface(gpx1); |
224 |
|
225 |
renderer.draw(geom1, mapControlManager.getSelectionSymbol()); |
226 |
renderer.draw(geom, mapControlManager.getGeometrySelectionSymbol()); |
227 |
} |
228 |
} |
229 |
|
230 |
/**
|
231 |
* Add a diferent option.
|
232 |
*
|
233 |
* @param s
|
234 |
* Diferent option.
|
235 |
*/
|
236 |
public void addOption(String s) { |
237 |
VectorialLayerEdited vle = getVLE(); |
238 |
FeatureStore featureStore = null;
|
239 |
try {
|
240 |
featureStore = vle.getFeatureStore(); |
241 |
} catch (ReadException e1) {
|
242 |
// TODO Auto-generated catch block
|
243 |
e1.printStackTrace(); |
244 |
} |
245 |
DisposableIterator iterator = null;
|
246 |
try {
|
247 |
iterator = |
248 |
((FeatureSelection) featureStore.getSelection()).iterator(); |
249 |
if (s.equals(PluginServices.getText(this, "end")) |
250 |
|| s.equalsIgnoreCase(PluginServices.getText(this,
|
251 |
"InternalPolygonCADTool.end"))) {
|
252 |
if (points.size() > 0) { |
253 |
Feature feature = (Feature) iterator.next(); |
254 |
geometry = (feature.getDefaultGeometry()).cloneGeometry(); |
255 |
if (geometry instanceof GeometryCollection) { |
256 |
MultiPrimitive gc = (MultiPrimitive) geometry; |
257 |
geometry = createNewPolygonGC(gc, points); |
258 |
} else {
|
259 |
geometry = createNewPolygon(geometry, points); |
260 |
} |
261 |
try {
|
262 |
EditableFeature eFeature = feature.getEditable(); |
263 |
eFeature.setGeometry(featureStore |
264 |
.getDefaultFeatureType() |
265 |
.getDefaultGeometryAttributeName(), geometry); |
266 |
featureStore.update(eFeature); |
267 |
} catch (ReadException e) {
|
268 |
NotificationManager.addError(e.getMessage(), e); |
269 |
} catch (DataException e) {
|
270 |
NotificationManager.addError(e.getMessage(), e); |
271 |
} |
272 |
ArrayList rows = new ArrayList(); |
273 |
rows.add(feature); |
274 |
// vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
|
275 |
// rows);
|
276 |
} |
277 |
points.clear(); |
278 |
refresh(); |
279 |
|
280 |
} else
|
281 |
if (s.equals(PluginServices.getText(this, "cancel"))) { |
282 |
points.clear(); |
283 |
} |
284 |
} catch (DataException e1) {
|
285 |
e1.printStackTrace(); |
286 |
} finally {
|
287 |
if (iterator != null) { |
288 |
iterator.dispose(); |
289 |
} |
290 |
} |
291 |
} |
292 |
|
293 |
/*
|
294 |
* (non-Javadoc)
|
295 |
*
|
296 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
297 |
*/
|
298 |
public void addValue(double d) { |
299 |
} |
300 |
|
301 |
private Geometry createNewPolygon(Geometry geometry, List<Point> points) { |
302 |
Geometry newGeometry = geometry.cloneGeometry(); |
303 |
|
304 |
GeneralPathX gpxInternal = new GeneralPathX();
|
305 |
gpxInternal.moveTo(points.get(points.size() - 1));
|
306 |
for (int i = points.size() - 2; i >= 0; i--) { |
307 |
gpxInternal.lineTo(points.get(i)); |
308 |
} |
309 |
gpxInternal.closePath(); |
310 |
|
311 |
if (!gpxInternal.isCCW()) {
|
312 |
gpxInternal.flip(); |
313 |
} |
314 |
|
315 |
if (newGeometry.getGeometryType().isTypeOf(Geometry.TYPES.SURFACE)){
|
316 |
GeneralPathX newGp = newGeometry.getGeneralPath(); |
317 |
newGp.append(gpxInternal.getPathIterator(null), false); |
318 |
}else if (newGeometry.getGeometryType().isTypeOf(Geometry.TYPES.MULTISURFACE)){ |
319 |
MultiSurface multiSurface = (MultiSurface)newGeometry; |
320 |
for (int i=0 ; i<multiSurface.getPrimitivesNumber() ; i++){ |
321 |
Surface surcafe = multiSurface.getSurfaceAt(i); |
322 |
try {
|
323 |
if (createSurface(gpxInternal).intersects(surcafe)){
|
324 |
GeneralPathX newGp = surcafe.getGeneralPath(); |
325 |
newGp.append(gpxInternal.getPathIterator(null), false); |
326 |
} |
327 |
} catch (GeometryOperationNotSupportedException e) {
|
328 |
LOG.error("Erro calculating the intersection", e);
|
329 |
} catch (GeometryOperationException e) {
|
330 |
LOG.error("Erro calculating the intersection", e);
|
331 |
} |
332 |
} |
333 |
} |
334 |
|
335 |
return newGeometry;
|
336 |
} |
337 |
|
338 |
private Geometry createNewPolygonGC(MultiPrimitive multiPrimitive,
|
339 |
List<Point> points) { |
340 |
MultiPrimitive multiPrimitiveAux = createMultiPrimitive(); |
341 |
|
342 |
for (int i = 0; i < multiPrimitive.getPrimitivesNumber(); i++) { |
343 |
multiPrimitiveAux.addPrimitive(multiPrimitive.getPrimitiveAt(i)); |
344 |
} |
345 |
|
346 |
GeneralPathX gpx = new GeneralPathX();
|
347 |
gpx.moveTo(points.get(points.size() - 1));
|
348 |
for (int i = points.size() - 2; i >= 0; i--) { |
349 |
gpx.lineTo(points.get(i)); |
350 |
multiPrimitiveAux.addPrimitive(createCurve(gpx)); |
351 |
gpx = new GeneralPathX();
|
352 |
gpx.moveTo(points.get(i)); |
353 |
} |
354 |
gpx.closePath(); |
355 |
multiPrimitiveAux.addPrimitive(createCurve(gpx)); |
356 |
|
357 |
return multiPrimitiveAux;
|
358 |
} |
359 |
|
360 |
public String getName() { |
361 |
return PluginServices.getText(this, "internal_polygon_"); |
362 |
} |
363 |
|
364 |
public String toString() { |
365 |
return "_internalpolygon"; |
366 |
} |
367 |
|
368 |
@Override
|
369 |
protected int[] getSupportedGeometryTypes() { |
370 |
return new int[] { SURFACE, MULTISURFACE }; |
371 |
} |
372 |
} |