root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / InternalPolygonCADTool.java @ 29616
History | View | Annotate | Download (12.9 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.editing.gui.cad.tools; |
42 |
|
43 |
import java.awt.Component; |
44 |
import java.awt.Graphics; |
45 |
import java.awt.Graphics2D; |
46 |
import java.awt.event.InputEvent; |
47 |
import java.awt.event.MouseEvent; |
48 |
import java.awt.geom.PathIterator; |
49 |
import java.awt.geom.Point2D; |
50 |
import java.util.ArrayList; |
51 |
|
52 |
import javax.swing.JOptionPane; |
53 |
|
54 |
import org.gvsig.andami.PluginServices; |
55 |
import org.gvsig.andami.messages.NotificationManager; |
56 |
import org.gvsig.editing.CADExtension; |
57 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
58 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
59 |
import org.gvsig.editing.gui.cad.tools.smc.InternalPolygonCADToolContext; |
60 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
61 |
import org.gvsig.fmap.dal.exception.DataException; |
62 |
import org.gvsig.fmap.dal.exception.ReadException; |
63 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
64 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
65 |
import org.gvsig.fmap.dal.feature.Feature; |
66 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
67 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
68 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
69 |
import org.gvsig.fmap.geom.Geometry; |
70 |
import org.gvsig.fmap.geom.aggregate.MultiPrimitive; |
71 |
import org.gvsig.fmap.geom.aggregate.impl.BaseMultiPrimitive; |
72 |
import org.gvsig.fmap.geom.operation.DrawInts; |
73 |
import org.gvsig.fmap.geom.operation.DrawOperationContext; |
74 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
75 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
76 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
77 |
import org.gvsig.fmap.geom.util.Converter; |
78 |
|
79 |
import com.vividsolutions.jts.geom.GeometryCollection; |
80 |
|
81 |
|
82 |
/**
|
83 |
* DOCUMENT ME!
|
84 |
*
|
85 |
* @author Vicente Caballero Navarro
|
86 |
*/
|
87 |
public class InternalPolygonCADTool extends DefaultCADTool { |
88 |
protected InternalPolygonCADToolContext _fsm;
|
89 |
protected ArrayList<Point2D> points=new ArrayList<Point2D>(); |
90 |
protected Geometry geometry=null; |
91 |
|
92 |
/**
|
93 |
* Crea un nuevo PolylineCADTool.
|
94 |
*/
|
95 |
public InternalPolygonCADTool() {
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
100 |
* carga previa a la utilizaci?n de la herramienta.
|
101 |
*/
|
102 |
public void init() { |
103 |
_fsm = new InternalPolygonCADToolContext(this); |
104 |
points.clear(); |
105 |
} |
106 |
|
107 |
/* (non-Javadoc)
|
108 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
109 |
*/
|
110 |
public void transition(double x, double y, InputEvent event) { |
111 |
_fsm.addPoint(x, y, event); |
112 |
} |
113 |
|
114 |
/* (non-Javadoc)
|
115 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
116 |
*/
|
117 |
public void transition(double d) { |
118 |
_fsm.addValue(d); |
119 |
} |
120 |
|
121 |
/* (non-Javadoc)
|
122 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
123 |
*/
|
124 |
public void transition(String s) throws CommandException { |
125 |
if (!super.changeCommand(s)){ |
126 |
_fsm.addOption(s); |
127 |
} |
128 |
} |
129 |
|
130 |
/**
|
131 |
* DOCUMENT ME!
|
132 |
*/
|
133 |
public void selection() { |
134 |
FeatureSet selection=null;
|
135 |
try {
|
136 |
selection = (FeatureSet)getVLE().getFeatureStore().getSelection(); |
137 |
|
138 |
if (selection.getSize() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
139 |
CADExtension.setCADTool("_selection",false); |
140 |
((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
141 |
"_internalpolygon");
|
142 |
} |
143 |
} catch (ReadException e) {
|
144 |
// TODO Auto-generated catch block
|
145 |
e.printStackTrace(); |
146 |
} catch (DataException e) {
|
147 |
// TODO Auto-generated catch block
|
148 |
e.printStackTrace(); |
149 |
} |
150 |
} |
151 |
|
152 |
/**
|
153 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
154 |
* editableFeatureSource que ya estar? creado.
|
155 |
*
|
156 |
* @param x par?metro x del punto que se pase en esta transici?n.
|
157 |
* @param y par?metro y del punto que se pase en esta transici?n.
|
158 |
*/
|
159 |
public void addPoint(double x, double y,InputEvent event) { |
160 |
if (((MouseEvent)event).getClickCount()==2){ |
161 |
addOption(PluginServices.getText(this,"InternalPolygonCADTool.end")); |
162 |
return;
|
163 |
} |
164 |
VectorialLayerEdited vle=getVLE(); |
165 |
FeatureSet featureCollection=null;
|
166 |
try {
|
167 |
featureCollection = (FeatureSet)vle.getFeatureStore().getSelection(); |
168 |
if (featureCollection.getSize()==1){ |
169 |
Feature feature=(Feature) featureCollection.iterator().next(); |
170 |
geometry=(feature.getDefaultGeometry()).cloneGeometry(); |
171 |
if (geometry.contains(x,y)){
|
172 |
points.add(new Point2D.Double(x,y)); |
173 |
}else{
|
174 |
JOptionPane.showMessageDialog(((Component)PluginServices.getMainFrame()),PluginServices.getText(this,"debe_insertar_el_punto_dentro_de_los_limites_de_la_geometria")); |
175 |
} |
176 |
} |
177 |
} catch (ReadException e) {
|
178 |
NotificationManager.addError(e.getMessage(),e); |
179 |
} catch (DataException e) {
|
180 |
NotificationManager.addError(e.getMessage(),e); |
181 |
} |
182 |
} |
183 |
|
184 |
/**
|
185 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
186 |
* encontremos.
|
187 |
*
|
188 |
* @param g Graphics sobre el que dibujar.
|
189 |
* @param x par?metro x del punto que se pase para dibujar.
|
190 |
* @param y par?metro x del punto que se pase para dibujar.
|
191 |
*/
|
192 |
public void drawOperation(Graphics g, double x, double y) { |
193 |
Point2D[] ps=points.toArray(new Point2D[0]); |
194 |
GeneralPathX gpx=new GeneralPathX();
|
195 |
GeneralPathX gpx1=new GeneralPathX();
|
196 |
|
197 |
if (ps.length>0){ |
198 |
for (int i=0;i<ps.length;i++){ |
199 |
if (i==0){ |
200 |
gpx.moveTo(ps[i].getX(),ps[i].getY()); |
201 |
gpx1.moveTo(ps[i].getX(),ps[i].getY()); |
202 |
}else{
|
203 |
gpx.lineTo(ps[i].getX(),ps[i].getY()); |
204 |
gpx1.lineTo(ps[i].getX(),ps[i].getY()); |
205 |
} |
206 |
|
207 |
} |
208 |
DrawOperationContext doc=new DrawOperationContext();
|
209 |
doc.setGraphics((Graphics2D)g);
|
210 |
doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
211 |
doc.setSymbol(DefaultCADTool.selectionSymbol); |
212 |
gpx.lineTo(x,y); |
213 |
gpx.closePath(); |
214 |
gpx1.closePath(); |
215 |
// if (ps.length==1){
|
216 |
// Geometry geom=geomFactory.createPolyline2D(gpx);
|
217 |
//
|
218 |
// try {
|
219 |
// geom.invokeOperation(DrawInts.CODE,doc);
|
220 |
// } catch (GeometryOperationNotSupportedException e) {
|
221 |
// e.printStackTrace();
|
222 |
// } catch (GeometryOperationException e) {
|
223 |
// e.printStackTrace();
|
224 |
// }
|
225 |
//// geom.drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.geometrySelectSymbol);
|
226 |
// }
|
227 |
Geometry geom = createSurface(gpx); |
228 |
Geometry geom1 = createSurface(gpx1); |
229 |
try {
|
230 |
|
231 |
geom1.invokeOperation(DrawInts.CODE,doc); |
232 |
} catch (GeometryOperationNotSupportedException e) {
|
233 |
e.printStackTrace(); |
234 |
} catch (GeometryOperationException e) {
|
235 |
e.printStackTrace(); |
236 |
} |
237 |
|
238 |
try {
|
239 |
doc.setSymbol(DefaultCADTool.geometrySelectSymbol); |
240 |
|
241 |
geom.invokeOperation(DrawInts.CODE,doc); |
242 |
} catch (GeometryOperationNotSupportedException e) {
|
243 |
e.printStackTrace(); |
244 |
} catch (GeometryOperationException e) {
|
245 |
e.printStackTrace(); |
246 |
} |
247 |
} |
248 |
} |
249 |
|
250 |
/**
|
251 |
* Add a diferent option.
|
252 |
*
|
253 |
* @param s Diferent option.
|
254 |
*/
|
255 |
public void addOption(String s) { |
256 |
VectorialLayerEdited vle=getVLE(); |
257 |
FeatureStore featureStore=null;
|
258 |
try {
|
259 |
featureStore = vle.getFeatureStore(); |
260 |
} catch (ReadException e1) {
|
261 |
// TODO Auto-generated catch block
|
262 |
e1.printStackTrace(); |
263 |
} |
264 |
DisposableIterator iterator = null;
|
265 |
try {
|
266 |
iterator = ((FeatureSelection) featureStore.getSelection()) |
267 |
.iterator(); |
268 |
if (s.equals(PluginServices.getText(this, "end")) |
269 |
|| s.equalsIgnoreCase(PluginServices.getText(this,
|
270 |
"InternalPolygonCADTool.end"))) {
|
271 |
if (points.size() > 0) { |
272 |
Feature feature = (Feature) iterator.next(); |
273 |
geometry = (feature.getDefaultGeometry()).cloneGeometry(); |
274 |
if (geometry instanceof GeometryCollection) { |
275 |
BaseMultiPrimitive gc = (BaseMultiPrimitive) geometry; |
276 |
geometry = createNewPolygonGC(gc, points |
277 |
.toArray(new Point2D[0])); |
278 |
} else {
|
279 |
geometry = createNewPolygon(geometry, points |
280 |
.toArray(new Point2D[0])); |
281 |
} |
282 |
try {
|
283 |
EditableFeature eFeature = feature.getEditable(); |
284 |
eFeature.setGeometry(featureStore |
285 |
.getDefaultFeatureType() |
286 |
.getDefaultGeometryAttributeName(), geometry); |
287 |
featureStore.update(eFeature); |
288 |
} catch (ReadException e) {
|
289 |
NotificationManager.addError(e.getMessage(), e); |
290 |
} catch (DataException e) {
|
291 |
NotificationManager.addError(e.getMessage(), e); |
292 |
} |
293 |
ArrayList rows = new ArrayList(); |
294 |
rows.add(feature); |
295 |
// vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
|
296 |
// rows);
|
297 |
} |
298 |
points.clear(); |
299 |
refresh(); |
300 |
|
301 |
} else if (s.equals(PluginServices.getText(this, "cancel"))) { |
302 |
points.clear(); |
303 |
} |
304 |
} catch (DataException e1) {
|
305 |
if (iterator != null) { |
306 |
iterator.dispose(); |
307 |
} |
308 |
e1.printStackTrace(); |
309 |
} |
310 |
} |
311 |
/* (non-Javadoc)
|
312 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
313 |
*/
|
314 |
public void addValue(double d) { |
315 |
} |
316 |
private Geometry createNewPolygon(Geometry gp,Point2D[] ps) { |
317 |
GeneralPathX newGp = new GeneralPathX();
|
318 |
double[] theData = new double[6]; |
319 |
|
320 |
PathIterator theIterator;
|
321 |
int theType;
|
322 |
int numParts = 0; |
323 |
|
324 |
|
325 |
theIterator = gp.getPathIterator(null, Converter.FLATNESS);
|
326 |
while (!theIterator.isDone()) {
|
327 |
theType = theIterator.currentSegment(theData); |
328 |
switch (theType) {
|
329 |
|
330 |
case PathIterator.SEG_MOVETO: |
331 |
numParts++; |
332 |
newGp.moveTo(theData[0], theData[1]); |
333 |
break;
|
334 |
|
335 |
case PathIterator.SEG_LINETO: |
336 |
newGp.lineTo(theData[0], theData[1]); |
337 |
break;
|
338 |
|
339 |
case PathIterator.SEG_QUADTO: |
340 |
newGp.quadTo(theData[0], theData[1], theData[2], theData[3]); |
341 |
break;
|
342 |
|
343 |
case PathIterator.SEG_CUBICTO: |
344 |
newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]); |
345 |
break;
|
346 |
|
347 |
case PathIterator.SEG_CLOSE: |
348 |
newGp.closePath(); |
349 |
break;
|
350 |
} //end switch
|
351 |
|
352 |
theIterator.next(); |
353 |
} //end while loop
|
354 |
GeneralPathX gpxInternal=new GeneralPathX();
|
355 |
gpxInternal.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY()); |
356 |
for (int i=ps.length-1;i>=0;i--){ |
357 |
gpxInternal.lineTo(ps[i].getX(),ps[i].getY()); |
358 |
} |
359 |
gpxInternal.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY()); |
360 |
if (!gpxInternal.isCCW()) {
|
361 |
gpxInternal.flip(); |
362 |
} |
363 |
newGp.append(gpxInternal,false);
|
364 |
|
365 |
return createSurface(newGp);
|
366 |
} |
367 |
private Geometry createNewPolygonGC(BaseMultiPrimitive gp,Point2D[] ps) { |
368 |
ArrayList geoms=new ArrayList(); |
369 |
Geometry[] geometries=gp.getGeometries();
|
370 |
for (int i = 0;i<geometries.length;i++) { |
371 |
geoms.add(geometries[i]); |
372 |
} |
373 |
GeneralPathX gpx=new GeneralPathX();
|
374 |
gpx.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY()); |
375 |
for (int i=ps.length-2;i>=0;i--){ |
376 |
gpx.lineTo(ps[i].getX(),ps[i].getY()); |
377 |
geoms.add(createCurve(gpx)); |
378 |
gpx=new GeneralPathX();
|
379 |
gpx.moveTo(ps[i].getX(),ps[i].getY()); |
380 |
} |
381 |
gpx.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY()); |
382 |
geoms.add(createCurve(gpx)); |
383 |
MultiPrimitive gc = createMultiPrimitive(((Geometry[])geoms.toArray(new Geometry[0]))); |
384 |
return gc;
|
385 |
} |
386 |
public String getName() { |
387 |
return PluginServices.getText(this,"internal_polygon_"); |
388 |
} |
389 |
|
390 |
public String toString() { |
391 |
return "_internalpolygon"; |
392 |
} |
393 |
|
394 |
public boolean isApplicable(int shapeType) { |
395 |
switch (shapeType) {
|
396 |
case Geometry.TYPES.POINT:
|
397 |
case Geometry.TYPES.CURVE:
|
398 |
case Geometry.TYPES.MULTIPOINT:
|
399 |
return false; |
400 |
} |
401 |
return true; |
402 |
} |
403 |
} |