Revision 24363 branches/v2_0_0_prep/extensions/extCAD/src/com/iver/cit/gvsig/gui/cad/tools/ExtendCADTool.java

View differences:

ExtendCADTool.java
70 70
import com.iver.cit.gvsig.gui.cad.tools.smc.ExtendCADToolContext.ExtendCADToolState;
71 71
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
72 72

  
73

  
74 73
/**
75 74
 * DOCUMENT ME!
76 75
 *
77 76
 * @author Vicente Caballero Navarro
78 77
 */
79 78
public class ExtendCADTool extends DefaultCADTool {
80
    private ExtendCADToolContext _fsm;
81
    private GeometryFactory geomFactory=GeometryManager.getInstance().getGeometryFactory();
82
    /**
83
     * Crea un nuevo ExtendCADTool.
84
     */
85
    public ExtendCADTool() {
86
    }
79
	private ExtendCADToolContext _fsm;
80
	private GeometryFactory geomFactory = GeometryManager.getInstance()
81
			.getGeometryFactory();
87 82

  
88
    /**
89
     * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
90
     * carga previa a la utilizaci?n de la herramienta.
91
     */
92
    public void init() {
93
        _fsm = new ExtendCADToolContext(this);
94
    }
83
	/**
84
	 * Crea un nuevo ExtendCADTool.
85
	 */
86
	public ExtendCADTool() {
87
	}
95 88

  
96
    /* (non-Javadoc)
97
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double, double)
98
     */
99
    public void transition(double x, double y, InputEvent event) {
100
        _fsm.addPoint(x, y, event);
101
    }
89
	/**
90
	 * M?todo de incio, para poner el c?digo de todo lo que se requiera de una
91
	 * carga previa a la utilizaci?n de la herramienta.
92
	 */
93
	public void init() {
94
		_fsm = new ExtendCADToolContext(this);
95
	}
102 96

  
103
    /* (non-Javadoc)
104
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, double)
105
     */
106
    public void transition(double d) {
107
        _fsm.addValue(d);
108
    }
97
	/*
98
	 * (non-Javadoc)
99
	 *
100
	 * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
101
	 *      double, double)
102
	 */
103
	public void transition(double x, double y, InputEvent event) {
104
		_fsm.addPoint(x, y, event);
105
	}
109 106

  
110
    /* (non-Javadoc)
111
     * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet, java.lang.String)
112
     */
113
    public void transition(String s) throws CommandException {
114
    	if (!super.changeCommand(s)){
115
    		_fsm.addOption(s);
116
    	}
117
    }
107
	/*
108
	 * (non-Javadoc)
109
	 *
110
	 * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
111
	 *      double)
112
	 */
113
	public void transition(double d) {
114
		_fsm.addValue(d);
115
	}
118 116

  
119
    /**
120
     * DOCUMENT ME!
121
     */
122
    public void selection() {
123
    	FeatureSet selection=null;
124
    	try {
125
    		selection = (FeatureSet)getVLE().getFeatureStore().getSelection();
117
	/*
118
	 * (non-Javadoc)
119
	 *
120
	 * @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
121
	 *      java.lang.String)
122
	 */
123
	public void transition(String s) throws CommandException {
124
		if (!super.changeCommand(s)) {
125
			_fsm.addOption(s);
126
		}
127
	}
126 128

  
127
    		if (selection.getSize() == 0 && !CADExtension.getCADTool().getClass().getName().equals("com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
128
    			CADExtension.setCADTool("_selection",false);
129
    			((SelectionCADTool) CADExtension.getCADTool()).setNextTool(
130
    			"_extend");
131
    		}
132
    	} catch (ReadException e) {
133
    		// TODO Auto-generated catch block
134
    		e.printStackTrace();
135
    	} catch (DataException e) {
129
	/**
130
	 * DOCUMENT ME!
131
	 */
132
	public void selection() {
133
		FeatureSet selection = null;
134
		try {
135
			selection = (FeatureSet) getVLE().getFeatureStore().getSelection();
136

  
137
			if (selection.getSize() == 0
138
					&& !CADExtension
139
							.getCADTool()
140
							.getClass()
141
							.getName()
142
							.equals(
143
									"com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
144
				CADExtension.setCADTool("_selection", false);
145
				((SelectionCADTool) CADExtension.getCADTool())
146
						.setNextTool("_extend");
147
			}
148
		} catch (ReadException e) {
136 149
			// TODO Auto-generated catch block
137 150
			e.printStackTrace();
151
		} catch (DataException e) {
152
			// TODO Auto-generated catch block
153
			e.printStackTrace();
138 154
		}
139
    }
155
	}
140 156

  
141
    /**
142
     * Equivale al transition del prototipo pero sin pasarle como par?metro el
143
     * editableFeatureSource que ya estar? creado.
144
     *
145
     * @param x par?metro x del punto que se pase en esta transici?n.
146
     * @param y par?metro y del punto que se pase en esta transici?n.
147
     */
148
    public void addPoint(double x, double y,InputEvent event) {
149
    	ExtendCADToolState actualState = (ExtendCADToolState) _fsm.getPreviousState();
150
    	String status = actualState.getName();
157
	/**
158
	 * Equivale al transition del prototipo pero sin pasarle como par?metro el
159
	 * editableFeatureSource que ya estar? creado.
160
	 *
161
	 * @param x
162
	 *            par?metro x del punto que se pase en esta transici?n.
163
	 * @param y
164
	 *            par?metro y del punto que se pase en esta transici?n.
165
	 */
166
	public void addPoint(double x, double y, InputEvent event) {
167
		ExtendCADToolState actualState = (ExtendCADToolState) _fsm
168
				.getPreviousState();
169
		String status = actualState.getName();
151 170

  
171
		if (status.equals("Extend.SelectGeometryToExtend")) {
152 172

  
153
    	if (status.equals("Extend.SelectGeometryToExtend")) {
154

  
155
    		VectorialLayerEdited vle=getVLE();
156
    		FeatureStore featureStore = null;
173
			VectorialLayerEdited vle = getVLE();
174
			FeatureStore featureStore = null;
157 175
			try {
158 176
				featureStore = vle.getFeatureStore();
159
			} catch (ReadException e1) {
160
				NotificationManager.addError(e1.getMessage(),e1);
161
			}
162
    		featureStore.beginEditingGroup(getName());
163
    		Iterator iterator=null;
164
			try {
165
				iterator = ((FeatureSelection)featureStore.getSelection()).iterator();
166
			} catch (DataException e1) {
167
				// TODO Auto-generated catch block
168
				e1.printStackTrace();
169
			}
170
//    		ArrayList selectedRow=getSelectedRows();
171
    		ArrayList selectedRowAux=new ArrayList();
172
    		//for (int i=0;i<selectedRow.size();i++) {
173
//    		selectedRowAux.addAll(selectedRow);
174
    		//}
175
    		//selection();
176
    		vle.selectWithPoint(x,y,false);
177
//    		ArrayList newSelectedRow=new ArrayList();
178
    		try {
179
    			while (iterator.hasNext()) {
177

  
178
				featureStore.beginEditingGroup(getName());
179
				Iterator iterator = null;
180
				iterator = ((FeatureSelection) featureStore.getSelection())
181
						.iterator();
182

  
183
				// ArrayList selectedRow=getSelectedRows();
184
				ArrayList selectedRowAux = new ArrayList();
185
				// for (int i=0;i<selectedRow.size();i++) {
186
				// selectedRowAux.addAll(selectedRow);
187
				// }
188
				// selection();
189
				vle.selectWithPoint(x, y, false);
190
				// ArrayList newSelectedRow=new ArrayList();
191
				while (iterator.hasNext()) {
180 192
					Feature feature = (Feature) iterator.next();
181 193

  
182
//				}
183
//    			for (int i=0;i<selectedRowAux.size();i++) {
184
//    				IRowEdited edRow1 = (IRowEdited) selectedRowAux.get(i);
185
//    				DefaultFeature fea1 = (DefaultFeature) edRow1.getLinkedRow().cloneRow();
186
    				Geometry geometry1 = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
194
					// }
195
					// for (int i=0;i<selectedRowAux.size();i++) {
196
					// IRowEdited edRow1 = (IRowEdited) selectedRowAux.get(i);
197
					// DefaultFeature fea1 = (DefaultFeature)
198
					// edRow1.getLinkedRow().cloneRow();
199
					Geometry geometry1 = ((Geometry) feature
200
							.getDefaultGeometry()).cloneGeometry();
187 201

  
188
//    				IRowEdited edRow2 = (IRowEdited) newSelectedRow.get(i);
189
//    				DefaultFeature fea2 = (DefaultFeature) edRow2.getLinkedRow().cloneRow();
190
    				Geometry geometry2 = ((Geometry)feature.getDefaultGeometry()).cloneGeometry();
191
    				//for (int j=0;j<newSelectedRow.size();j++) {
192
    				//if (geometry1 instanceof FPolygon2D) {
193
    				EditableFeature eFeature=feature.getEditable();
194
    				eFeature.setGeometry(featureStore.getDefaultFeatureType().getDefaultGeometryAttributeName(), intersectsGeometry(geometry2,geometry1));
195
//    				fea2.setGeometry(intersectsGeometry(geometry2,geometry1));
196
    				//}
197
    				//}
198
    				featureStore.update(eFeature);
199
//    				vea.modifyRow(edRow2.getIndex(),fea2,getName(),EditionEvent.GRAPHIC);
200
    				clearSelection();
201
//    				newSelectedRow.add(new DefaultRowEdited(fea2,IRowEdited.STATUS_MODIFIED,edRow2.getIndex()));
202
    			}
202
					// IRowEdited edRow2 = (IRowEdited) newSelectedRow.get(i);
203
					// DefaultFeature fea2 = (DefaultFeature)
204
					// edRow2.getLinkedRow().cloneRow();
205
					Geometry geometry2 = ((Geometry) feature
206
							.getDefaultGeometry()).cloneGeometry();
207
					// for (int j=0;j<newSelectedRow.size();j++) {
208
					// if (geometry1 instanceof FPolygon2D) {
209
					EditableFeature eFeature = feature.getEditable();
210
					eFeature.setGeometry(featureStore.getDefaultFeatureType()
211
							.getDefaultGeometryAttributeName(),
212
							intersectsGeometry(geometry2, geometry1));
213
					// fea2.setGeometry(intersectsGeometry(geometry2,geometry1));
214
					// }
215
					// }
216
					featureStore.update(eFeature);
217
					// vea.modifyRow(edRow2.getIndex(),fea2,getName(),EditionEvent.GRAPHIC);
218
					clearSelection();
219
					// newSelectedRow.add(new
220
					// DefaultRowEdited(fea2,IRowEdited.STATUS_MODIFIED,edRow2.getIndex()));
221
				}
203 222

  
204
    			featureStore.endEditingGroup();
205
//    			vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS, newSelectedRow);
206
    		} catch (ReadException e) {
207
    			NotificationManager.addError(e.getMessage(),e);
208
    		} catch (DataException e) {
209
    			NotificationManager.addError(e.getMessage(),e);
223
				featureStore.endEditingGroup();
224
				// vle.setSelectionCache(VectorialLayerEdited.NOTSAVEPREVIOUS,
225
				// newSelectedRow);
226
			} catch (ReadException e) {
227
				NotificationManager.addError(e.getMessage(), e);
228
			} catch (DataException e) {
229
				NotificationManager.addError(e.getMessage(), e);
210 230
			}
211
    	}
212
    }
231
		}
232
	}
213 233

  
214
    private Geometry intersectsGeometry(Geometry geometry1, Geometry geometry2) {
215
    	Point2D p3=null;
216
        Point2D p4=null;
217
    	Point2D p1=null;
218
        Point2D p2=null;
219
		GeneralPathX gpx=new GeneralPathX();
220
		PathIterator theIterator=geometry1.getInternalShape().getPathIterator(null,Converter.FLATNESS);
221
		boolean first=true;
234
	private Geometry intersectsGeometry(Geometry geometry1, Geometry geometry2) {
235
		Point2D p3 = null;
236
		Point2D p4 = null;
237
		Point2D p1 = null;
238
		Point2D p2 = null;
239
		GeneralPathX gpx = new GeneralPathX();
240
		PathIterator theIterator = geometry1.getInternalShape()
241
				.getPathIterator(null, Converter.FLATNESS);
242
		boolean first = true;
222 243
		double[] theData = new double[6];
223
        int theType;
224
			while (!theIterator.isDone()) {
225
                theType = theIterator.currentSegment(theData);
226
                switch (theType) {
244
		int theType;
245
		while (!theIterator.isDone()) {
246
			theType = theIterator.currentSegment(theData);
247
			switch (theType) {
227 248

  
228
                    case PathIterator.SEG_MOVETO:
229
                    	p1=new Point2D.Double(theData[0], theData[1]);
230
                        gpx.moveTo(p1.getX(),p1.getY());
231
                    	break;
249
			case PathIterator.SEG_MOVETO:
250
				p1 = new Point2D.Double(theData[0], theData[1]);
251
				gpx.moveTo(p1.getX(), p1.getY());
252
				break;
232 253

  
233
                    case PathIterator.SEG_LINETO:
234
                    	p2=new Point2D.Double(theData[0], theData[1]);
235
                    	ArrayList lines=getLines(geometry2);
236
                    	boolean isLineTo=true;
237
                    	for (int i=0;i<lines.size();i++) {
238
                    		Point2D[] ps1=(Point2D[])lines.get(i);
239
                    		Point2D p=UtilFunctions.getIntersection(ps1[0],ps1[1],p1,p2);
240
//                    		GeneralPathX gpxAux=new GeneralPathX();
241
//                    		gpxAux.moveTo(p.getX(),p.getY());
242
//                    		gpxAux.lineTo(ps1[0].getX(),ps1[0].getY());
243
//                    		Geometry gjts1=FConverter.java2d_to_jts((FShape)ShapeFactory.createPolyline2D(gpxAux).getInternalShape());
244
//                    		Geometry gjts2=FConverter.java2d_to_jts((FShape)geometry2.getInternalShape());
245
//                    		GeometryCollection result=(GeometryCollection)gjts1.intersection(gjts2);
246
//                    		Point point=(Point)result.getGeometryN(1);
247
//                    		p=new Point2D.Double(point.getX(),point.getY());
248
                    		if (p!=null && first) {
249
                    			gpx.lineTo(p.getX(),p.getY());
250
                    			first=false;
251
                    			isLineTo=false;
252
                    			break;
253
                    		}else {
254
                    			//gpx.lineTo(p2.getX(),p2.getY());
255
                    		}
256
                    	}
257
                    	if (!first && isLineTo)
258
                    		gpx.lineTo(p2.getX(),p2.getY());
254
			case PathIterator.SEG_LINETO:
255
				p2 = new Point2D.Double(theData[0], theData[1]);
256
				ArrayList lines = getLines(geometry2);
257
				boolean isLineTo = true;
258
				for (int i = 0; i < lines.size(); i++) {
259
					Point2D[] ps1 = (Point2D[]) lines.get(i);
260
					Point2D p = UtilFunctions.getIntersection(ps1[0], ps1[1],
261
							p1, p2);
262
					// GeneralPathX gpxAux=new GeneralPathX();
263
					// gpxAux.moveTo(p.getX(),p.getY());
264
					// gpxAux.lineTo(ps1[0].getX(),ps1[0].getY());
265
					// Geometry
266
					// gjts1=FConverter.java2d_to_jts((FShape)ShapeFactory.createPolyline2D(gpxAux).getInternalShape());
267
					// Geometry
268
					// gjts2=FConverter.java2d_to_jts((FShape)geometry2.getInternalShape());
269
					// GeometryCollection
270
					// result=(GeometryCollection)gjts1.intersection(gjts2);
271
					// Point point=(Point)result.getGeometryN(1);
272
					// p=new Point2D.Double(point.getX(),point.getY());
273
					if (p != null && first) {
274
						gpx.lineTo(p.getX(), p.getY());
275
						first = false;
276
						isLineTo = false;
277
						break;
278
					} else {
279
						// gpx.lineTo(p2.getX(),p2.getY());
280
					}
281
				}
282
				if (!first && isLineTo)
283
					gpx.lineTo(p2.getX(), p2.getY());
259 284

  
260
                    	break;
285
				break;
261 286

  
262
                } //end switch
287
			} // end switch
263 288

  
264
                theIterator.next();
265
            } //end while loop
266
			return geomFactory.createPolyline2D(gpx);
289
			theIterator.next();
290
		} // end while loop
291
		return geomFactory.createPolyline2D(gpx);
267 292
	}
268 293

  
269

  
270 294
	private ArrayList getLines(Geometry geometry1) {
271
		Point2D p1=null;
272
        Point2D p2=null;
273
		ArrayList lines=new ArrayList();
274
    	PathIterator theIterator=geometry1.getInternalShape().getPathIterator(null,Converter.FLATNESS);
295
		Point2D p1 = null;
296
		Point2D p2 = null;
297
		ArrayList lines = new ArrayList();
298
		PathIterator theIterator = geometry1.getInternalShape()
299
				.getPathIterator(null, Converter.FLATNESS);
275 300
		double[] theData = new double[6];
276
        int theType;
277
			while (!theIterator.isDone()) {
278
                theType = theIterator.currentSegment(theData);
279
                switch (theType) {
280
                    case PathIterator.SEG_MOVETO:
281
                    	p1=new Point2D.Double(theData[0], theData[1]);
282
                        break;
301
		int theType;
302
		while (!theIterator.isDone()) {
303
			theType = theIterator.currentSegment(theData);
304
			switch (theType) {
305
			case PathIterator.SEG_MOVETO:
306
				p1 = new Point2D.Double(theData[0], theData[1]);
307
				break;
283 308

  
284
                    case PathIterator.SEG_LINETO:
285
                    	p2=new Point2D.Double(theData[0], theData[1]);
286
                    	lines.add(new Point2D[] {p1,p2});
287
                    	break;
309
			case PathIterator.SEG_LINETO:
310
				p2 = new Point2D.Double(theData[0], theData[1]);
311
				lines.add(new Point2D[] { p1, p2 });
312
				break;
288 313

  
289
                } //end switch
314
			} // end switch
290 315

  
291
                theIterator.next();
292
            } //end while loop
316
			theIterator.next();
317
		} // end while loop
293 318
		return lines;
294 319
	}
320

  
295 321
	/**
296
     * M?todo para dibujar la lo necesario para el estado en el que nos
297
     * encontremos.
298
     *
299
     * @param g Graphics sobre el que dibujar.
300
     * @param x par?metro x del punto que se pase para dibujar.
301
     * @param y par?metro x del punto que se pase para dibujar.
302
     */
303
    public void drawOperation(Graphics g, double x, double y) {
322
	 * M?todo para dibujar la lo necesario para el estado en el que nos
323
	 * encontremos.
324
	 *
325
	 * @param g
326
	 *            Graphics sobre el que dibujar.
327
	 * @param x
328
	 *            par?metro x del punto que se pase para dibujar.
329
	 * @param y
330
	 *            par?metro x del punto que se pase para dibujar.
331
	 */
332
	public void drawOperation(Graphics g, double x, double y) {
304 333
	}
305 334

  
306
    /**
335
	/**
307 336
	 * Add a diferent option.
308 337
	 *
309 338
	 * @param s
310 339
	 *            Diferent option.
311 340
	 */
312
    public void addOption(String s) {
313
    }
341
	public void addOption(String s) {
342
	}
314 343

  
315
    /* (non-Javadoc)
316
     * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
317
     */
318
    public void addValue(double d) {
319
    }
344
	/*
345
	 * (non-Javadoc)
346
	 *
347
	 * @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
348
	 */
349
	public void addValue(double d) {
350
	}
320 351

  
321
    public String getName() {
322
		return PluginServices.getText(this,"extend_");
352
	public String getName() {
353
		return PluginServices.getText(this, "extend_");
323 354
	}
324 355

  
325 356
	public String toString() {
326 357
		return "_extend";
327 358
	}
359

  
328 360
	public boolean isApplicable(int shapeType) {
329 361
		switch (shapeType) {
330 362
		case Geometry.TYPES.CURVE:

Also available in: Unified diff