Revision 4365 trunk/extensions/extCAD/src/com/iver/cit/gvsig/gui/cad/tools/SelectionCADTool.java

View differences:

SelectionCADTool.java
40 40
 */
41 41
package com.iver.cit.gvsig.gui.cad.tools;
42 42

  
43
import java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46
import java.awt.event.InputEvent;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.io.IOException;
50
import java.util.ArrayList;
51

  
52 43
import com.iver.andami.PluginServices;
44

  
53 45
import com.iver.cit.gvsig.CADExtension;
54 46
import com.iver.cit.gvsig.fmap.DriverException;
55 47
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
......
58 50
import com.iver.cit.gvsig.fmap.core.IFeature;
59 51
import com.iver.cit.gvsig.fmap.core.IGeometry;
60 52
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62 53
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
63 54
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
64 55
import com.iver.cit.gvsig.fmap.layers.FBitSet;
......
66 57
import com.iver.cit.gvsig.gui.cad.DefaultCADTool;
67 58
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext;
68 59
import com.iver.cit.gvsig.gui.cad.tools.smc.SelectionCADToolContext.SelectionCADToolState;
60
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
69 61

  
62
import java.awt.Color;
63
import java.awt.Graphics;
64
import java.awt.Graphics2D;
65
import java.awt.event.InputEvent;
66
import java.awt.geom.Point2D;
67
import java.awt.geom.Rectangle2D;
68

  
69
import java.util.ArrayList;
70

  
70 71
/**
71 72
 * DOCUMENT ME!
72 73
 *
......
81 82

  
82 83
	private Point2D lastPoint;
83 84

  
84
	private ArrayList selectedHandler = new ArrayList();
85

  
86
	private ArrayList selectedRow = new ArrayList();
87

  
88
	private ArrayList selectedRowIndex = new ArrayList();
89

  
90 85
	private String tool = "selection";
91 86

  
92
	//private IGeometry clonedGeometry = null;
93

  
94 87
	private String state;
95 88

  
96
	// double
97
	// FLATNESS=getCadToolAdapter().getMapControl().getViewPort().toMapDistance(2);
98

  
99 89
	/**
100 90
	 * Crea un nuevo LineCADTool.
101 91
	 */
102 92
	public SelectionCADTool() {
103

  
104 93
	}
105 94

  
106 95
	/**
......
150 139

  
151 140
	public boolean select(double x, double y) {
152 141
		firstPoint = new Point2D.Double(x, y);
142

  
153 143
		FBitSet selection = getCadToolAdapter().getVectorialAdapter()
154 144
				.getSelection();
155
		// if ((selectedRow.size() == 0) ||
156
		// (selection.cardinality() == 0)) {
145
		VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
146
				.getEditionManager().getActiveLayerEdited();
147
		ArrayList selectedHandler = vle.getSelectedHandler();
148
		ArrayList selectedRow = vle.getSelectedRow();
149
		ArrayList selectedRowIndex = vle.getSelectedRowIndex();
150

  
157 151
		// Se comprueba si se pincha en una gemometr?a
158 152
		PluginServices.getMDIManager().setWaitCursor();
159 153

  
......
161 155
				.toMapDistance(tolerance);
162 156
		Rectangle2D rect = new Rectangle2D.Double(firstPoint.getX() - tam,
163 157
				firstPoint.getY() - tam, tam * 2, tam * 2);
164
		// int[] indexes = getCadToolAdapter().getVectorialAdapter()
165
		// 		.getRowsIndexes_OLD(rect);
166 158
		VectorialEditableAdapter vea = getCadToolAdapter()
167 159
				.getVectorialAdapter();
168 160
		String strEPSG = getCadToolAdapter().getMapControl().getViewPort()
......
175 167
			selection.clear();
176 168

  
177 169
			for (int i = 0; i < feats.length; i++) {
178
			// for (int i = 0; i < indexes.length; i++) {
179

  
180
				IFeature feat = (IFeature)feats[i].getLinkedRow();
170
				IFeature feat = (IFeature) feats[i].getLinkedRow();
181 171
				IGeometry geom = feat.getGeometry();
182
				/* IGeometry geom = null;
183
				try {
184
					geom = vea.getShape(indexes[i]);
185
				} catch (DriverIOException e) {
186
					// TODO Auto-generated catch block
187
					e.printStackTrace();
188
				} */
172

  
189 173
				if (geom.intersects(rect)) { // , 0.1)){
190 174
					selection.set(feats[i].getIndex(), true);
191
					// selection.set(indexes[i], true);
192 175
				}
193 176
			}
194 177
		} catch (DriverException e1) {
195
			// TODO Auto-generated catch block
196 178
			e1.printStackTrace();
197 179
		}
198 180

  
199
		/*
200
		 * for (int i = 0; i < indexes.length; i++) { try { if
201
		 * (getCadToolAdapter().getVectorialAdapter().getShape(
202
		 * indexes[i]).intersects(rect)) { // .intersects(rect,FLATNESS)) {
203
		 * selection.set(indexes[i], true); } } catch (DriverIOException e) { //
204
		 * TODO Auto-generated catch block e.printStackTrace(); } }
205
		 */
206 181
		boolean isTheSame = false;
207
		if (prevSelection.cardinality() != 0
182

  
183
		if ((prevSelection.cardinality() != 0)
208 184
				&& selection.intersects(prevSelection)
209 185
				&& !selection.equals(prevSelection)) {
210 186
			selection = prevSelection;
211 187
			getCadToolAdapter().getVectorialAdapter().setSelection(selection);
212 188
			isTheSame = true;
213 189
		}
190

  
214 191
		if (selection.cardinality() > 0) {
215 192
			// Se comprueba si se pincha un handler. El m?s cercano (o los m?s
216 193
			// cercanos si hay empate)
217 194
			selectedRow.clear();
218 195
			selectedRowIndex.clear();
219 196
			selectedHandler.clear();
220

  
221
			double min = Double.MAX_VALUE;
222

  
223
			// Cogemos las entidades seleccionadas
224
			for (int i = selection.nextSetBit(0); i >= 0; i = selection
225
					.nextSetBit(i + 1)) {
226
				Handler[] handlers = null;
227

  
228
				DefaultFeature fea = null;
229
				try {
230
					fea = (DefaultFeature) getCadToolAdapter()
231
							.getVectorialAdapter().getRow(i).getLinkedRow();
232
					/* clonedGeometry = fea.getGeometry().cloneGeometry();
233
					handlers = clonedGeometry
234
							.getHandlers(IGeometry.SELECTHANDLER);
235
					selectedRow.add(new DefaultFeature(clonedGeometry, fea
236
							.getAttributes())); */
237
					handlers = fea.getGeometry().getHandlers(IGeometry.SELECTHANDLER);
238
					selectedRow.add(fea);
239
					selectedRowIndex.add(new Integer(i));
240
					// y miramos los handlers de cada entidad seleccionada
241
					min = getCadToolAdapter().getMapControl().getViewPort()
242
							.toMapDistance(tolerance);
243
					// int hSel = -1;
244
					for (int j = 0; j < handlers.length; j++) {
245
						Point2D handlerPoint = handlers[j].getPoint();
246
						double distance = firstPoint.distance(handlerPoint);
247
						if (distance <= min) {
248
							min = distance;
249
							//hSel = j;
250
							selectedHandler.add(handlers[j]);
251
						}
252
					}
253
					// Se a?ade un solo handler por
254
					// cada geometr?a seleccionada
255
					// if (hSel != -1) {
256
					// 	selectedHandler.add(handlers[hSel]);
257
					// 	System.out.println("Handler seleccionado: " + hSel);
258
					// }
259

  
260
				} catch (DriverIOException e) {
261
					// TODO Auto-generated catch block
262
					e.printStackTrace();
263
				} catch (IOException e) {
264
					// TODO Auto-generated catch block
265
					e.printStackTrace();
266
				}
267
			}
268

  
197
			vle.refreshSelectionCache(firstPoint, getCadToolAdapter());
269 198
		}
199

  
270 200
		PluginServices.getMDIManager().restoreCursor();
271
		if ((selection.cardinality() > 0 && selectedHandler.size() > 0)
201

  
202
		if (((selection.cardinality() > 0) && (selectedHandler.size() > 0))
272 203
				|| isTheSame) {
273 204
			state = "Selection.EndPoint";
274 205
		} else if (selection.cardinality() > 0) {
......
276 207
		} else {
277 208
			state = "Selection.SecondPoint";
278 209
		}
210

  
279 211
		return true;
280 212
	}
281 213

  
......
290 222
	 * @param y
291 223
	 *            par?metro y del punto que se pase en esta transici?n.
292 224
	 */
293
	public void addPoint(double x, double y) {
225
	public void addPoint(double x, double y, InputEvent event) {
294 226
		SelectionCADToolState actualState = (SelectionCADToolState) _fsm
295 227
				.getPreviousState();
296 228
		String status = actualState.getName();
297 229
		FBitSet selection = getCadToolAdapter().getVectorialAdapter()
298 230
				.getSelection();
231
		VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
232
				.getEditionManager().getActiveLayerEdited();
233
		ArrayList selectedHandler = vle.getSelectedHandler();
234
		ArrayList selectedRow = vle.getSelectedRow();
235
		ArrayList selectedRowIndex = vle.getSelectedRowIndex();
236

  
299 237
		try {
300 238
			if (status.equals("Selection.FirstPoint")) {
301
				// firstPoint = new Point2D.Double(x, y);
302 239
			} else if (status.equals("Selection.SecondPoint")) {
303
				// PluginServices.getMDIManager().setWaitCursor();
304 240
				lastPoint = new Point2D.Double(x, y);
305 241
				selection.clear();
306 242

  
......
327 263

  
328 264
				Rectangle2D rect = new Rectangle2D.Double(x1, y1, w1, h1);
329 265

  
330
				// int[] indexes = getCadToolAdapter().getVectorialAdapter()
331
				// 		.getRowsIndexes_OLD(
332
				// 				new Rectangle2D.Double(x1, y1, w1, h1));
333 266
				VectorialEditableAdapter vea = getCadToolAdapter()
334 267
						.getVectorialAdapter();
335 268
				String strEPSG = getCadToolAdapter().getMapControl()
......
337 270
				IRowEdited[] feats = vea.getFeatures(rect, strEPSG);
338 271

  
339 272
				for (int i = 0; i < feats.length; i++) {
340
				// for (int i = 0; i < indexes.length; i++) {
341
					IGeometry geom =
342
					((IFeature)feats[i].getLinkedRow()).getGeometry();
343
					/* IGeometry geom = null;
344
					try {
345
						geom = vea.getShape(indexes[i]);
346
					} catch (DriverIOException e) {
347
						// TODO Auto-generated catch block
348
						e.printStackTrace();
349
					} */
273
					IGeometry geom = ((IFeature) feats[i].getLinkedRow())
274
							.getGeometry();
275

  
350 276
					if (firstPoint.getX() < lastPoint.getX()) {
351 277
						if (rect.contains(geom.getBounds2D())) {
352 278
							selection.set(feats[i].getIndex(), true);
353
							// selection.set(indexes[i], true);
354 279
						}
355 280
					} else {
356 281
						if (geom.intersects(rect)) { // , 0.1)){
357 282
							selection.set(feats[i].getIndex(), true);
358
							// selection.set(indexes[i], true);
359 283
						}
360 284
					}
361 285
				}
362 286

  
363 287
				PluginServices.getMDIManager().restoreCursor();
364
				// cardinality = selection.cardinality();
365 288
			} else if (status.equals("Selection.EndPoint")) {
366

  
367
				/*
368
				 * for (int k = 0; k < selectedHandler.size(); k++) { Handler h =
369
				 * (Handler) selectedHandler.get(k); h.set(x, y); }
370
				 */
371

  
372 289
				for (int i = 0; i < selectedRow.size(); i++) {
373

  
374
					// System.out.println(h.getPoint());
375 290
					DefaultFeature row = (DefaultFeature) selectedRow.get(i);
376 291
					int index = ((Integer) selectedRowIndex.get(i)).intValue();
377
					// System.out.println(row.getGeometry().getBounds2D());
292

  
378 293
					// Movemos los handlers que hemos seleccionado
379 294
					// previamente dentro del m?todo select()
380 295
					for (int k = 0; k < selectedHandler.size(); k++) {
381 296
						Handler h = (Handler) selectedHandler.get(k);
382 297
						h.set(x, y);
383
						// System.out.println(h.getPoint());
384 298
					}
385 299

  
386
					// h.set(x, y);
387
					// getVectorialAdapter().modifyRow(index, row);
388

  
389 300
					modifyFeature(index, row);
390 301
				}
391

  
392 302
			}
393
			// } catch (IOException e) {
394
			// TODO Auto-generated catch block
395
			// e.printStackTrace();
396 303
		} catch (DriverException e) {
397
			// TODO Auto-generated catch block
398 304
			e.printStackTrace();
399 305
		}
400 306
	}
......
417 323
		String status = actualState.getName();
418 324
		FBitSet selection = getCadToolAdapter().getVectorialAdapter()
419 325
				.getSelection();
420
		if (selection.cardinality() == 0)
421
		{
326
		VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
327
				.getEditionManager().getActiveLayerEdited();
328
		ArrayList selectedHandler = vle.getSelectedHandler();
329
		ArrayList selectedRow = vle.getSelectedRow();
330
		ArrayList selectedRowIndex = vle.getSelectedRowIndex();
331

  
332
		if (selection.cardinality() == 0) {
422 333
			selectedRow.clear();
423 334
			selectedRowIndex.clear();
424 335
			selectedHandler.clear();
425 336
		}
426
		// try {
427
			// drawHandlers(g, selection, getCadToolAdapter().getMapControl()
428
			// 		.getViewPort().getAffineTransform());
429
			drawHandlers(g, selectedRow, getCadToolAdapter().getMapControl()
430
					.getViewPort().getAffineTransform());
431
		/* } catch (DriverIOException e) {
432
			e.printStackTrace();
433
		} */
434 337

  
338
		drawHandlers(g, selectedRow, getCadToolAdapter().getMapControl()
339
				.getViewPort().getAffineTransform());
340

  
435 341
		if (status.equals("Selection.SecondPoint")) {
436 342
			// Dibuja el rect?ngulo de selecci?n
437

  
438 343
			GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
439 344
					4);
440 345
			elShape.moveTo(firstPoint.getX(), firstPoint.getY());
......
445 350
			ShapeFactory.createPolyline2D(elShape).draw((Graphics2D) g,
446 351
					getCadToolAdapter().getMapControl().getViewPort(),
447 352
					CADTool.selectSymbol);
448

  
449 353
		} else if (status.equals("Selection.EndPoint")) {
450 354
			// Movemos los handlers que hemos seleccionado
451 355
			// previamente dentro del m?todo select()
452 356
			for (int k = 0; k < selectedHandler.size(); k++) {
453 357
				Handler h = (Handler) selectedHandler.get(k);
454 358
				h.set(x, y);
455
				// System.out.println(h.getPoint());
456 359
			}
457 360

  
458 361
			// Y una vez movidos los v?rtices (handles)
459 362
			// redibujamos la nueva geometr?a.
460 363
			for (int i = 0; i < selectedRow.size(); i++) {
461

  
462 364
				IGeometry geom = ((IFeature) selectedRow.get(i)).getGeometry()
463 365
						.cloneGeometry();
464
				// System.out.println(geom.getBounds2D());
465
				// int index = ((Integer) selectedRowIndex.get(i)).intValue();
466 366
				g.setColor(Color.gray);
467

  
468 367
				geom.draw((Graphics2D) g, getCadToolAdapter().getMapControl()
469 368
						.getViewPort(), CADTool.modifySymbol);
470 369
			}
471 370
		}
472

  
473 371
	}
474 372

  
475

  
476 373
	/**
477 374
	 * Add a diferent option.
478 375
	 *
......
493 390
	public void addValue(double d) {
494 391
	}
495 392

  
496
	/**
497
	 * DOCUMENT ME!
498
	 *
499
	 * @return DOCUMENT ME!
500
	 */
501
	public int getSelectedRowSize() {
502
		return this.selectedRow.size();
503
	}
504

  
505 393
	public String getStatus() {
506 394
		try {
507 395
			SelectionCADToolState actualState = (SelectionCADToolState) _fsm
508 396
					.getPreviousState();
509 397
			String status = actualState.getName();
398

  
510 399
			return status;
511 400
		} catch (NullPointerException e) {
512 401
			return "ExecuteMap.Initial";
......
528 417
	public String getName() {
529 418
		return "SELECCION";
530 419
	}
531

  
532
	public void clearSelection() {
533
		// Limpiamos la selecci?n cacheada
534
		selectedHandler.clear();
535
		selectedRow.clear();
536
		selectedRowIndex.clear();
537
	}
538

  
539
	/**
540
	 * @return Returns the selectedRow.
541
	 */
542
	public IFeature[] getSelectedRowsCache() {
543
		return (IFeature[]) selectedRow.toArray(new IFeature[0]);
544
	}
545

  
546

  
547 420
}

Also available in: Unified diff