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