root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / InternalPolygonCADTool.java @ 28763
History | View | Annotate | Download (12 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 |
|
52 |
import javax.swing.JOptionPane; |
53 |
|
54 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
55 |
import com.iver.andami.PluginServices; |
56 |
import com.iver.andami.messages.NotificationManager; |
57 |
import com.iver.cit.gvsig.CADExtension; |
58 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileWriteException; |
59 |
import com.iver.cit.gvsig.exceptions.validate.ValidateRowException; |
60 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
61 |
import com.iver.cit.gvsig.fmap.core.FGeometryCollection; |
62 |
import com.iver.cit.gvsig.fmap.core.FShape; |
63 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
64 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
65 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
66 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
67 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
68 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
69 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
70 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
71 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
72 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
73 |
import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
74 |
import com.iver.cit.gvsig.gui.cad.tools.smc.InternalPolygonCADToolContext; |
75 |
import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
76 |
|
77 |
|
78 |
/**
|
79 |
* DOCUMENT ME!
|
80 |
*
|
81 |
* @author Vicente Caballero Navarro
|
82 |
*/
|
83 |
public class InternalPolygonCADTool extends DefaultCADTool { |
84 |
private InternalPolygonCADToolContext _fsm;
|
85 |
private ArrayList<Point2D> points=new ArrayList<Point2D>(); |
86 |
private IGeometry geometry=null; |
87 |
/**
|
88 |
* Crea un nuevo PolylineCADTool.
|
89 |
*/
|
90 |
public InternalPolygonCADTool() {
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* M?todo de incio, para poner el c?digo de todo lo que se requiera de una
|
95 |
* carga previa a la utilizaci?n de la herramienta.
|
96 |
*/
|
97 |
public void init() { |
98 |
_fsm = new InternalPolygonCADToolContext(this); |
99 |
points.clear(); |
100 |
} |
101 |
|
102 |
/* (non-Javadoc)
|
103 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
|
104 |
*/
|
105 |
public void transition(double x, double y, InputEvent event) { |
106 |
_fsm.addPoint(x, y, event); |
107 |
} |
108 |
|
109 |
/* (non-Javadoc)
|
110 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
|
111 |
*/
|
112 |
public void transition(double d) { |
113 |
_fsm.addValue(d); |
114 |
} |
115 |
|
116 |
/* (non-Javadoc)
|
117 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
|
118 |
*/
|
119 |
public void transition(String s) throws CommandException { |
120 |
if (!super.changeCommand(s)){ |
121 |
_fsm.addOption(s); |
122 |
} |
123 |
} |
124 |
|
125 |
/**
|
126 |
* DOCUMENT ME!
|
127 |
*/
|
128 |
public void selection() { |
129 |
ArrayList selectedRow=getSelectedRows();
|
130 |
if (selectedRow.size() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) { |
131 |
CADExtension.setCADTool("_selection",false); |
132 |
((SelectionCADTool) CADExtension.getCADTool()).setNextTool( |
133 |
"_internalpolygon");
|
134 |
} |
135 |
} |
136 |
|
137 |
/**
|
138 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
139 |
* editableFeatureSource que ya estar? creado.
|
140 |
*
|
141 |
* @param x par?metro x del punto que se pase en esta transici?n.
|
142 |
* @param y par?metro y del punto que se pase en esta transici?n.
|
143 |
*/
|
144 |
public void addPoint(double x, double y,InputEvent event) { |
145 |
VectorialLayerEdited vle=getVLE(); |
146 |
ArrayList selectedRows=vle.getSelectedRow();
|
147 |
if (selectedRows.size()==1){ |
148 |
// if (geometry==null){
|
149 |
IRowEdited row = (DefaultRowEdited) selectedRows.get(0);
|
150 |
IFeature feat = (IFeature) row.getLinkedRow().cloneRow(); |
151 |
geometry=feat.getGeometry(); |
152 |
// }
|
153 |
if (geometry.contains(x,y)){
|
154 |
points.add(new Point2D.Double(x,y)); |
155 |
}else{
|
156 |
JOptionPane.showMessageDialog(((Component)PluginServices.getMainFrame()),PluginServices.getText(this,"debe_insertar_el_punto_dentro_de_los_limites_de_la_geometria")); |
157 |
} |
158 |
} |
159 |
} |
160 |
|
161 |
/**
|
162 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
163 |
* encontremos.
|
164 |
*
|
165 |
* @param g Graphics sobre el que dibujar.
|
166 |
* @param x par?metro x del punto que se pase para dibujar.
|
167 |
* @param y par?metro x del punto que se pase para dibujar.
|
168 |
*/
|
169 |
public void drawOperation(Graphics g, double x, double y) { |
170 |
Point2D[] ps=(Point2D[])points.toArray(new Point2D[0]); |
171 |
GeneralPathX gpx=new GeneralPathX();
|
172 |
// GeneralPathX gpx1=new GeneralPathX();
|
173 |
|
174 |
if (ps.length>0){ |
175 |
for (int i=0;i<ps.length;i++){ |
176 |
if (i==0){ |
177 |
gpx.moveTo(ps[i].getX(),ps[i].getY()); |
178 |
// gpx1.moveTo(ps[i].getX(),ps[i].getY());
|
179 |
}else{
|
180 |
gpx.lineTo(ps[i].getX(),ps[i].getY()); |
181 |
// gpx1.lineTo(ps[i].getX(),ps[i].getY());
|
182 |
} |
183 |
|
184 |
} |
185 |
DefaultRowEdited[] rows=(DefaultRowEdited[])getSelectedRows().toArray(new DefaultRowEdited[0]); |
186 |
for (int i=0;i<rows.length;i++){ |
187 |
((IFeature)rows[i].getLinkedRow()).getGeometry().drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.selectionSymbol);
|
188 |
} |
189 |
gpx.lineTo(x,y); |
190 |
gpx.closePath(); |
191 |
// gpx1.closePath();
|
192 |
if (ps.length==1){ |
193 |
IGeometry geom=ShapeFactory.createPolyline2D(gpx); |
194 |
geom.drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.geometrySelectSymbol);
|
195 |
} |
196 |
IGeometry geom=ShapeFactory.createPolygon2D(gpx); |
197 |
// IGeometry geom1=ShapeFactory.createPolygon2D(gpx1);
|
198 |
// geom1.drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.selectionSymbol);
|
199 |
geom.drawInts((Graphics2D)g,CADExtension.getEditionManager().getMapControl().getViewPort(),DefaultCADTool.geometrySelectSymbol);
|
200 |
} |
201 |
|
202 |
} |
203 |
|
204 |
/**
|
205 |
* Add a diferent option.
|
206 |
*
|
207 |
* @param s Diferent option.
|
208 |
*/
|
209 |
public void addOption(String s) { |
210 |
VectorialLayerEdited vle=getVLE(); |
211 |
ArrayList selectedRows=vle.getSelectedRow();
|
212 |
VectorialEditableAdapter vea = vle.getVEA(); |
213 |
IRowEdited row=null;
|
214 |
if (s.equals(PluginServices.getText(this,"end")) || s.equalsIgnoreCase(PluginServices.getText(this,"InternalPolygonCADTool.end"))){ |
215 |
if (points.size()>0){ |
216 |
row = (DefaultRowEdited) selectedRows.get(0);
|
217 |
IFeature feat = (IFeature) row.getLinkedRow().cloneRow(); |
218 |
|
219 |
geometry=feat.getGeometry(); |
220 |
if (geometry instanceof FGeometryCollection) { |
221 |
FGeometryCollection gc=(FGeometryCollection)geometry; |
222 |
geometry=createNewPolygonGC(gc,(Point2D[])points.toArray(new Point2D[0])); |
223 |
}else {
|
224 |
geometry=createNewPolygon(geometry,(Point2D[])points.toArray(new Point2D[0])); |
225 |
} |
226 |
DefaultFeature df=new DefaultFeature(geometry,feat.getAttributes(), feat.getID());
|
227 |
DefaultRowEdited dre=new DefaultRowEdited(df,DefaultRowEdited.STATUS_MODIFIED,row.getIndex());
|
228 |
try {
|
229 |
vea.modifyRow(dre.getIndex(),dre.getLinkedRow(),getName(),EditionEvent.GRAPHIC); |
230 |
} catch (ValidateRowException e) {
|
231 |
NotificationManager.addError(e.getMessage(),e); |
232 |
} catch (ExpansionFileWriteException e) {
|
233 |
NotificationManager.addError(e.getMessage(),e); |
234 |
} catch (ReadDriverException e) {
|
235 |
NotificationManager.addError(e.getMessage(),e); |
236 |
} |
237 |
ArrayList rows=new ArrayList(); |
238 |
rows.add(dre); |
239 |
vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, rows); |
240 |
} |
241 |
points.clear(); |
242 |
|
243 |
|
244 |
}else if (s.equals(PluginServices.getText(this,"cancel"))){ |
245 |
points.clear(); |
246 |
} |
247 |
PluginServices.getMainFrame().enableControls(); |
248 |
} |
249 |
/* (non-Javadoc)
|
250 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
251 |
*/
|
252 |
public void addValue(double d) { |
253 |
} |
254 |
private IGeometry createNewPolygon(IGeometry gp,Point2D[] ps) { |
255 |
GeneralPathX newGp = new GeneralPathX();
|
256 |
double[] theData = new double[6]; |
257 |
|
258 |
PathIterator theIterator;
|
259 |
int theType;
|
260 |
int numParts = 0; |
261 |
|
262 |
|
263 |
theIterator = gp.getPathIterator(null, FConverter.FLATNESS);
|
264 |
while (!theIterator.isDone()) {
|
265 |
theType = theIterator.currentSegment(theData); |
266 |
switch (theType) {
|
267 |
|
268 |
case PathIterator.SEG_MOVETO: |
269 |
numParts++; |
270 |
newGp.moveTo(theData[0], theData[1]); |
271 |
break;
|
272 |
|
273 |
case PathIterator.SEG_LINETO: |
274 |
newGp.lineTo(theData[0], theData[1]); |
275 |
break;
|
276 |
|
277 |
case PathIterator.SEG_QUADTO: |
278 |
newGp.quadTo(theData[0], theData[1], theData[2], theData[3]); |
279 |
break;
|
280 |
|
281 |
case PathIterator.SEG_CUBICTO: |
282 |
newGp.curveTo(theData[0], theData[1], theData[2], theData[3], theData[4], theData[5]); |
283 |
break;
|
284 |
|
285 |
case PathIterator.SEG_CLOSE: |
286 |
newGp.closePath(); |
287 |
break;
|
288 |
} //end switch
|
289 |
|
290 |
theIterator.next(); |
291 |
} //end while loop
|
292 |
GeneralPathX gpxInternal=new GeneralPathX();
|
293 |
gpxInternal.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY()); |
294 |
for (int i=ps.length-1;i>=0;i--){ |
295 |
gpxInternal.lineTo(ps[i].getX(),ps[i].getY()); |
296 |
} |
297 |
gpxInternal.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY()); |
298 |
if (!gpxInternal.isCCW()) {
|
299 |
gpxInternal.flip(); |
300 |
} |
301 |
newGp.append(gpxInternal,false);
|
302 |
|
303 |
return ShapeFactory.createPolygon2D(newGp);
|
304 |
} |
305 |
private IGeometry createNewPolygonGC(FGeometryCollection gp,Point2D[] ps) { |
306 |
ArrayList geoms=new ArrayList(); |
307 |
IGeometry[] geometries=gp.getGeometries();
|
308 |
for (int i = 0;i<geometries.length;i++) { |
309 |
geoms.add(geometries[i]); |
310 |
} |
311 |
GeneralPathX gpx=new GeneralPathX();
|
312 |
gpx.moveTo(ps[ps.length-1].getX(),ps[ps.length-1].getY()); |
313 |
for (int i=ps.length-2;i>=0;i--){ |
314 |
gpx.lineTo(ps[i].getX(),ps[i].getY()); |
315 |
geoms.add(ShapeFactory.createPolyline2D(gpx)); |
316 |
gpx=new GeneralPathX();
|
317 |
gpx.moveTo(ps[i].getX(),ps[i].getY()); |
318 |
} |
319 |
gpx.lineTo(ps[ps.length-1].getX(),ps[ps.length-1].getY()); |
320 |
geoms.add(ShapeFactory.createPolyline2D(gpx)); |
321 |
FGeometryCollection gc=new FGeometryCollection((IGeometry[])geoms.toArray(new IGeometry[0])); |
322 |
// gc.setGeometryType(FShape.POLYGON);
|
323 |
return gc;
|
324 |
} |
325 |
public String getName() { |
326 |
return PluginServices.getText(this,"internal_polygon_"); |
327 |
} |
328 |
|
329 |
public String toString() { |
330 |
return "_internalpolygon"; |
331 |
} |
332 |
|
333 |
public boolean isApplicable(int shapeType) { |
334 |
switch (shapeType) {
|
335 |
case FShape.POINT:
|
336 |
case FShape.LINE:
|
337 |
case FShape.MULTIPOINT:
|
338 |
return false; |
339 |
} |
340 |
return true; |
341 |
} |
342 |
public void endTransition(double x, double y, MouseEvent event) { |
343 |
_fsm.endPoint(x, y, event); |
344 |
} |
345 |
|
346 |
} |