Revision 1763 branches/gvSIG_CAD_Layout_version/applications/appgvSIG/src/com/iver/cit/gvsig/gui/cad/CADToolAdapter.java

View differences:

CADToolAdapter.java
26 26
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
27 27
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
28 28
import com.iver.fsac.Automaton;
29
import com.iver.utiles.console.InvalidResponseException;
30
import com.iver.utiles.console.JConsole;
31
import com.iver.utiles.console.ResponseListener;
32 29

  
33 30

  
34 31
/**
......
36 33
 *
37 34
 * @author Fernando Gonz?lez Cort?s
38 35
 */
39
public class CADToolAdapter extends Behavior {
36
public abstract class CADToolAdapter extends Behavior {
40 37
	private static Logger logger = Logger.getLogger(CADToolAdapter.class.getName());
41
	private Stack cadToolStack = new Stack();
42
	private FBitSet selection;
43
	private EditableFeatureSource editableFeatureSource;
38
	protected Stack cadToolStack = new Stack();
39
	protected FBitSet selection;
40
	protected EditableFeatureSource editableFeatureSource;
44 41

  
45 42
	//Para pasarle las coordenadas cuando se produce un evento textEntered
46 43
	private int lastX;
47 44
	private int lastY;
48 45
	//private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
49
	private JConsole console;
50 46
	private Point2D mapAdjustedPoint;
51 47
	private CadMapControl cadMapControl;
52 48
	private boolean questionAsked = false;
53 49
	protected CadGrid grid;// = new CadGrid();
54
	protected Point adjustedPoint;
50
	protected Point2D adjustedPoint;
55 51
	private boolean snapping=true;
56 52
	private boolean adjustSnapping=false;
57 53
	
58 54
	public CADToolAdapter(){
59 55
		 grid= new CadGrid(this);
60 56
	}
61
	/**
62
	 * Pinta de alguna manera especial las geometrias seleccionadas para la
63
	 * edici?n. En caso de que el snapping est? activado, pintar? el efecto
64
	 * del mismo.
65
	 *
66
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
67
	 */
68
	public void paintComponent(Graphics g) {
57
	public void paintComponent(Graphics g){
69 58
		super.paintComponent(g);
70

  
71
		/*
72
		   for (int i = selection.nextSetBit(0); i >= 0;
73
		                   i = selection.nextSetBit(i + 1)) {
74
		           try {
75
		                   IGeometry geom = editableFeatureSource.getGeometry(i);
76
		                   Handler[] handlers=geom.getHandlers();
77
		                   geom.draw((Graphics2D) g, getMapControl().getViewPort(), symbol);
78
		                   FGraphicUtilities.DrawHandlers((Graphics2D)g,getMapControl().getViewPort().getAffineTransform(),handlers);
79
		           } catch (IOException e) {
80
		                   e.printStackTrace();
81
		           } catch (DriverIOException e) {
82
		                   e.printStackTrace();
83
		           }
84
		   }
85
		 */
86
		grid.drawGrid(g);
87
		drawCursor(g);
88
		
89
		if (adjustedPoint!=null){
90
		Point2D p = getMapControl().getViewPort().toMapPoint((int) adjustedPoint.getX(),
91
				(int) adjustedPoint.getY());
92
		CadTool cadtool=(CadTool) cadToolStack.peek();
93
		cadtool.setAT(getMapControl().getViewPort().getAffineTransform());
94
		cadtool.drawOperation(g, editableFeatureSource,
95
			selection, p.getX(), p.getY());
96
		}
97 59
	}
98

  
99 60
	/**
100 61
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
101 62
	 */
......
142 103
	 *
143 104
	 * @return Distancia del punto que se pasa como par?metro al punto ajustado
144 105
	 */
145
	private double adjustToHandler(Point point, Point2D mapHandlerAdjustedPoint) {
106
	private double adjustToHandler(Point2D point, Point2D mapHandlerAdjustedPoint) {
146 107
		//if (selection.cardinality() > 0) {
108
			double rw=toDistance(5);
147 109
			try {
148
				Point2D mapPoint = toPoint(point);
110
				Point2D mapPoint = point;
149 111

  
150
				Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - 1,
151
						mapPoint.getY() - 1, 2, 2);
112
				Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw/2,
113
					mapPoint.getY() - rw/2, rw, rw);
152 114

  
153 115
				int[] indexes = editableFeatureSource.getGeometriesIndexes(r);
154 116
				
......
161 123
					IGeometry geom;
162 124
					geom = editableFeatureSource.getGeometry(indexes[i]);
163 125

  
164
					Handler[] handlers = geom.getHandlers(FGeometry.STRETCHINGHANDLER);
126
					Handler[] handlers = geom.getHandlers(FGeometry.SELECTHANDLER);
165 127

  
166 128
					for (int j = 0; j < handlers.length; j++) {
167 129
						Point2D handlerPoint = handlers[j].getPoint();
168
						Point2D handlerImagePoint = fromPoint(handlerPoint);
130
						Point2D handlerImagePoint = handlerPoint;
169 131
						double dist = handlerImagePoint.distance(point);
170 132

  
171
						if ((dist < SelectionCadTool.tolerance) &&
133
						if ((dist < toDistance(SelectionCadTool.tolerance)) &&
172 134
								(dist < min)) {
173 135
							min = dist;
174 136
							argmin = handlerImagePoint;
......
181 143
					point.setLocation(argmin);
182 144

  
183 145
					//Se hace el casting porque no se quiere redondeo
184
					point.setLocation((int)argmin.getX(),
185
							(int) argmin.getY());
146
					//point.setLocation((int)argmin.getX(),
147
					//		(int) argmin.getY());
186 148
					
187 149
					mapHandlerAdjustedPoint.setLocation(mapArgmin);
188 150
					return min;
......
216 178
	/**
217 179
	 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
218 180
	 */
219
	public void mouseMoved(MouseEvent e) throws BehaviorException {
220
		lastX = e.getX();
221
		lastY = e.getY();
181
	public abstract void mouseMoved(MouseEvent e) throws BehaviorException;
222 182

  
223
		calculateSnapPoint(e.getPoint());
224

  
225
		getMapControl().repaint();
226
	}
227

  
228 183
	/**
229 184
	 * DOCUMENT ME!
230 185
	 *
231 186
	 * @param g DOCUMENT ME!
232 187
	 */
233 188
	public void drawCursor(Graphics g) {
234
		Point p = adjustedPoint;
189
		g.setColor(Color.black);
190
		Point2D p = adjustedPoint;
235 191
		if (p==null) grid.setAT(getAT());
236 192
		//else{
237 193
		int size1 = 15;
......
261 217
	 *
262 218
	 * @param point
263 219
	 */
264
	protected void calculateSnapPoint(Point point) {
220
	 protected void calculateSnapPoint(Point point) {
265 221
		//Se comprueba el ajuste a rejilla
222
		
223
		Point2D gridAdjustedPoint = toPoint(point);
224
		double minDistance = Double.MAX_VALUE; 
225
			
226
		if (!cadToolStack.isEmpty() && cadToolStack.peek() instanceof SelectionCadTool && ((SelectionCadTool)cadToolStack.peek()).getAutomaton().getStatus()==0){
227
			mapAdjustedPoint=gridAdjustedPoint;
228
			adjustedPoint=(Point2D)point.clone();
229
		}else{
230
			minDistance= grid.adjustToGrid(gridAdjustedPoint);
231
			if (minDistance < Double.MAX_VALUE) {
232
				adjustedPoint = fromPoint(gridAdjustedPoint);
233
				mapAdjustedPoint = gridAdjustedPoint;
234
			} else {
235
				mapAdjustedPoint = null;
236
			}
237
		}
238
		Point2D handlerAdjustedPoint = null;
239

  
240
		//Se comprueba el ajuste a los handlers
241
		if (mapAdjustedPoint != null) {
242
			handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); //getMapControl().getViewPort().toMapPoint(point);
243
		} else {
244
			handlerAdjustedPoint = toPoint(point);
245
		}
246

  
247
		Point2D mapPoint = new Point2D.Double();
248
		double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
249

  
250
		if (distance < minDistance) {
251
			adjustSnapping = true;
252
			adjustedPoint = fromPoint(handlerAdjustedPoint);
253
			mapAdjustedPoint = mapPoint;
254
			minDistance = distance;
255
		}
256

  
257
		//Si no hay ajuste
258
		if (minDistance == Double.MAX_VALUE) {
259
			adjustedPoint = point;
260
			mapAdjustedPoint = null;
261
		}
262
	}
263
	/*protected void calculateSnapPoint(Point point) {
264
		//Se comprueba el ajuste a rejilla
266 265
		Point gridAdjustedPoint = (Point) point.clone();
267 266
		double minDistance = grid.adjustToGrid(gridAdjustedPoint);
268 267
		if (minDistance < Double.MAX_VALUE) {
......
271 270
		}
272 271
		
273 272
		//Se comprueba el ajuste a los handlers
274
		Point handlerAdjustedPoint = gridAdjustedPoint;
273
		Point handlerAdjustedPoint = (Point) point.clone();
275 274
		Point2D mapPoint = new Point2D.Double();
276 275
		double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
277 276
		if (distance < minDistance) {
......
288 287
		}
289 288
	}
290 289

  
291

  
290
*/
292 291
	/**
293 292
	 * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
294 293
	 */
......
334 333
		configureMenu();
335 334
	}
336 335

  
337
	/**
338
	 * DOCUMENT ME!
339
	 */
340
	private void configureMenu() {
341
		String[] desc = ((CadTool) cadToolStack.peek()).getAutomaton()
342
						 .getCurrentTransitionDescriptions();
343
		String[] labels = ((CadTool) cadToolStack.peek()).getAutomaton()
344
						   .getCurrentTransitions();
345
		if (getCadMapControl()!=null){
346
		getCadMapControl().clearMenu();
336
	public abstract void configureMenu();
347 337

  
348
		for (int i = 0; i < desc.length; i++) {
349
			if (desc[i] != null) {
350
				getCadMapControl().addMenuEntry(desc[i], labels[i]);
351
			}
352
		}
353
		}
354
	}
355

  
356 338
	/**
357 339
	 * DOCUMENT ME!
358 340
	 *
......
389 371
				askQuestion();
390 372
			}
391 373
		}
392
		if (console!=null){
374
		//if (console!=null){
393 375
			configureMenu();
394
		}
376
		//}
395 377
		PluginServices.getMainFrame().enableControls();
396 378
	}
397 379

  
......
400 382
	 *
401 383
	 * @param value DOCUMENT ME!
402 384
	 */
403
	public void setGrid(boolean value) {
404
		grid.setUseGrid(value);
405
		grid.setAT(getAT());
406
		setGridSizeX(toDistance(20));
407
		setGridSizeY(toDistance(20));
408
	}
385
	public abstract void setGrid(boolean value);
386
	
409 387

  
410 388
	/**
411 389
	 * DOCUMENT ME!
......
483 461
	 * DOCUMENT ME!
484 462
	 */
485 463
	public void popCadTool() {
486
		if (console!=null)
487
		console.cancelQuestion();
488 464
		cadToolStack.pop();
489 465
	}
490 466

  
......
492 468
	 * DOCUMENT ME!
493 469
	 */
494 470
	private void askQuestion() {
495
		if (console!=null){
496
		console.askQuestion(((CadTool) cadToolStack.peek()).getQuestion(),
497
			new ResponseListener() {
498
				public void acceptResponse(String response,
499
					boolean userCancelled) throws InvalidResponseException {
500
					if (response != null) {
501
						if ("".equals(response)) {
502
							transition("aceptar");
503
						}
504

  
505
						textEntered(response);
506
					} else if (userCancelled) {
507
						transition("cancel");
508
					}
509
				}
510
			});
471
		CadTool cadtool=(CadTool) cadToolStack.peek();
472
		if (cadtool.getAutomaton().getStatus()==0){
473
			PluginServices.getMainFrame().addTextToConsole("\n" +cadtool.getName());
511 474
		}
475
		PluginServices.getMainFrame().addTextToConsole("\n" + cadtool.getQuestion());
512 476
		questionAsked = true;
513 477
		
514 478
	}
......
521 485
	public void setCadTool(CadTool cadTool) {
522 486
		cadToolStack.clear();
523 487
		pushCadTool(cadTool);
524
		if (console!=null)
488
		//if (console!=null)
525 489
		askQuestion();
526 490
	}
527 491

  
......
551 515
	 *
552 516
	 * @param console
553 517
	 */
554
	public void setConsole(JConsole console) {
518
	/*public void setConsole(JConsole console) {
555 519
		this.console = console;
556 520
	}
557

  
521
*/
558 522
	/**
559 523
	 * DOCUMENT ME!
560 524
	 *
......
597 561
		}
598 562
		}
599 563
		selection.clear();
600
		getCadMapControl().getMapControl().drawMap(false);
564
		refresh();
565
		//getCadMapControl().getMapControl().drawMap(false);
601 566
	}
602 567

  
603 568
	/**
......
613 578
	public void keyPressed(String actionCommand) {
614 579
		if (actionCommand.equals("eliminar")){
615 580
			delete();
616
		}else if (actionCommand.equals("escape")){
617
			transition("cancel");
618
		}
581
		}else if (actionCommand.equals("escape")) {
582
				cadToolStack.clear();
583
				pushCadTool(new SelectionCadTool());
584
				selection.clear();
585
				getMapControl().drawMap(false);
586
				PluginServices.getMainFrame().selectTool("selection");
587
				askQuestion();
588
		} 
619 589
		PluginServices.getMainFrame().enableControls();
620 590
	}
621
	public AffineTransform getAT(){
622
		return getMapControl().getViewPort().getAffineTransform();
623
	}
624
	public double toDistance(int i){
625
		return getMapControl().getViewPort().toMapDistance(i);
626
	}
591
	public abstract AffineTransform getAT();
592
	public abstract double toDistance(int i);
627 593
	public FBitSet getSelection(){
628 594
		return selection;
629 595
	}
630
	public void refresh(){
631
		getMapControl().drawMap(false);
632
	}
633
	public Point2D toPoint(Point2D p){
634
		return getMapControl().getViewPort().toMapPoint(p);
635
	}
636
	public Rectangle2D getExtent(){
637
		return getMapControl().getViewPort().getAdjustedExtent();
638
	}
639
	public double fromDistance(double d){
640
		return getMapControl().getViewPort().fromMapDistance(d);
641
	}
642
	public Point2D fromPoint(Point2D p){
643
		return getMapControl().getViewPort().fromMapPoint(p);
644
	}
596
	public abstract void refresh();
597
	public abstract Point2D toPoint(Point2D p);
598
	public abstract Rectangle2D getExtent();
599
	public abstract double fromDistance(double d);
600
	public abstract Point2D fromPoint(Point2D p);
645 601
	public void setGridSizeX(double i1){
646 602
		grid.setGridSizeX(i1);
647 603
	}

Also available in: Unified diff