Revision 42036 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/tools/Behavior/Behavior.java

View differences:

Behavior.java
47 47
import org.slf4j.Logger;
48 48
import org.slf4j.LoggerFactory;
49 49

  
50

  
51

  
52 50
/**
53
 * <p>When user is working with a tool on a {@link MapControl MapControl} instance, <code>Behavior</code> defines
54
 *  the basic ways of interacting: selecting a point, a circle, a rectangle, or ...</p>
51
 * <p>
52
 * When user is working with a tool on a {@link MapControl MapControl} instance,
53
 * <code>Behavior</code> defines the basic ways of interacting: selecting a
54
 * point, a circle, a rectangle, or ...
55
 * </p>
55 56
 *
56
 * <p>All events generated will be <code>MouseEvent</code>, and will depend on the nature of the
57
 *  <i>behavior</i>, like the kind of tool for applying the changes.</p>
57
 * <p>
58
 * All events generated will be <code>MouseEvent</code>, and will depend on the
59
 * nature of the <i>behavior</i>, like the kind of tool for applying the
60
 * changes.
61
 * </p>
58 62
 *
59
 * <p><code>Behavior</code> defines the common and basic functionality for all kinds of interacting ways
60
 *  with the <code>MapControl</code> object.</p>
63
 * <p>
64
 * <code>Behavior</code> defines the common and basic functionality for all
65
 * kinds of interacting ways with the <code>MapControl</code> object.
66
 * </p>
61 67
 *
62 68
 * @see IBehavior
63 69
 *
64 70
 * @author Luis W. Sevilla
65 71
 */
66 72
public abstract class Behavior implements IBehavior {
67
        public static final int BUTTON_LEFT = 1;
68
        public static final int BUTTON_MIDDLE = 2;
69
        public static final int BUTTON_RIGHT = 4;
70
        
71
	/**
72
	 * Reference to the <code>MapControl</code> object that uses.
73
	 *
74
	 * @see #getMapControl()
75
	 * @see #setMapControl(MapControl)
76
	 */
77
	private MapControl mapControl;
78
	
73

  
74
    public static final int BUTTON_LEFT = 1;
75
    public static final int BUTTON_MIDDLE = 2;
76
    public static final int BUTTON_RIGHT = 4;
77

  
78
    /**
79
     * Reference to the <code>MapControl</code> object that uses.
80
     *
81
     * @see #getMapControl()
82
     * @see #setMapControl(MapControl)
83
     */
84
    private MapControl mapControl;
85

  
79 86
    private static final Logger LOG = LoggerFactory.getLogger(Behavior.class);
80 87

  
81
	protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
82
	
83
        private int mouseButton = BUTTON_LEFT;
84
        private boolean isMyButton = false; 
85
        
86
        public Behavior() {
87
            
88
    protected GeometryManager geomManager = GeometryLocator
89
        .getGeometryManager();
90

  
91
    private int mouseButton = BUTTON_LEFT;
92
    private boolean isMyButton = false;
93

  
94
    public Behavior() {
95

  
96
    }
97

  
98
    public Behavior(int mouseButton) {
99
        this.mouseButton = mouseButton;
100
    }
101

  
102
    /*
103
     * (non-Javadoc)
104
     *
105
     * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getListener()
106
     */
107
    public abstract ToolListener getListener();
108

  
109
    /*
110
     * <p>Draws as much of the associated <code>MapControl</code> image as is
111
     * currently available. The image
112
     * is drawn with its top-left corner at (0, 0) in this graphics context's
113
     * coordinate space. Transparent
114
     * pixels in the image do not affect whatever pixels are already there.</p>
115
     *
116
     * @see
117
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#paintComponent(java.
118
     * awt.Graphics)
119
     */
120
    public void paintComponent(MapControlDrawer mapControlDrawer) {
121
    }
122

  
123
    public void paintComponent(MapControlDrawer mapControlDrawer, boolean clean) {
124
        if (clean){
125
            clean(mapControlDrawer);
88 126
        }
89
        
90
        public Behavior(int mouseButton) {
91
            this.mouseButton = mouseButton;
127
        paintComponent(mapControlDrawer);
128
    }
129

  
130
    public void clean(MapControlDrawer mapControlDrawer){
131
        BufferedImage img = getMapControl().getImage();
132

  
133
        if (img != null) {
134
            mapControlDrawer.drawImage(img, 0, 0);
92 135
        }
93
        
94
	/*
95
	 * (non-Javadoc)
96
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getListener()
97
	 */
98
	public abstract ToolListener getListener();
136
    }
99 137

  
100
	/*
101
	 * <p>Draws as much of the associated <code>MapControl</code> image as is currently available. The image
102
	 *  is drawn with its top-left corner at (0, 0) in this graphics context's coordinate space. Transparent
103
	 *  pixels in the image do not affect whatever pixels are already there.</p>
104
	 *
105
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#paintComponent(java.awt.Graphics)
106
	 */
107
	public void paintComponent(MapControlDrawer mapControlDrawer) {
108
		BufferedImage img = getMapControl().getImage();
138
    /*
139
     * (non-Javadoc)
140
     *
141
     * @see
142
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#setMapControl(com.iver
143
     * .cit.gvsig.fmap.MapControl)
144
     */
145
    public void setMapControl(MapControl mc) {
146
        mapControl = mc;
147
    }
109 148

  
110
		if (img != null) {
111
			mapControlDrawer.drawImage(img, 0, 0);
112
		}
113
	}
149
    /*
150
     * (non-Javadoc)
151
     *
152
     * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getImageCursor()
153
     */
154
    public Image getImageCursor() {
155
        return getListener().getImageCursor();
156
    }
114 157

  
115
	/* (non-Javadoc)
116
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
117
	 */
118
	public void setMapControl(MapControl mc) {
119
		mapControl = mc;
120
	}
158
    /*
159
     * (non-Javadoc)
160
     *
161
     * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getMapControl()
162
     */
163
    public MapControl getMapControl() {
164
        return mapControl;
165
    }
121 166

  
122
	/* (non-Javadoc)
123
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getImageCursor()
124
	 */
125
	public Image getImageCursor(){
126
		return getListener().getImageCursor();
127
	}
167
    /*
168
     * (non-Javadoc)
169
     *
170
     * @see
171
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseClicked(java.awt
172
     * .event.MouseEvent)
173
     */
174
    public void mouseClicked(MouseEvent e) throws BehaviorException {
175
    }
128 176

  
129
	/* (non-Javadoc)
130
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getMapControl()
131
	 */
132
	public MapControl getMapControl() {
133
		return mapControl;
134
	}
177
    /*
178
     * (non-Javadoc)
179
     *
180
     * @see
181
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseEntered(java.awt
182
     * .event.MouseEvent)
183
     */
184
    public void mouseEntered(MouseEvent e) throws BehaviorException {
185
    }
135 186

  
136
	/* (non-Javadoc)
137
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseClicked(java.awt.event.MouseEvent)
138
	 */
139
	public void mouseClicked(MouseEvent e) throws BehaviorException {
140
	}
187
    /*
188
     * (non-Javadoc)
189
     *
190
     * @see
191
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseExited(java.awt
192
     * .event.MouseEvent)
193
     */
194
    public void mouseExited(MouseEvent e) throws BehaviorException {
195
    }
141 196

  
142
	/* (non-Javadoc)
143
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseEntered(java.awt.event.MouseEvent)
144
	 */
145
	public void mouseEntered(MouseEvent e) throws BehaviorException {
146
	}
197
    /*
198
     * (non-Javadoc)
199
     *
200
     * @see
201
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mousePressed(java.awt
202
     * .event.MouseEvent)
203
     */
204
    public void mousePressed(MouseEvent e) throws BehaviorException {
205
    }
147 206

  
148
	/* (non-Javadoc)
149
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseExited(java.awt.event.MouseEvent)
150
	 */
151
	public void mouseExited(MouseEvent e) throws BehaviorException {
152
	}
207
    /*
208
     * (non-Javadoc)
209
     *
210
     * @see
211
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseReleased(java.awt
212
     * .event.MouseEvent)
213
     */
214
    public void mouseReleased(MouseEvent e) throws BehaviorException {
215
    }
153 216

  
154
	/* (non-Javadoc)
155
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mousePressed(java.awt.event.MouseEvent)
156
	 */
157
	public void mousePressed(MouseEvent e) throws BehaviorException {
158
	}
217
    /*
218
     * (non-Javadoc)
219
     *
220
     * @see
221
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseDragged(java.awt
222
     * .event.MouseEvent)
223
     */
224
    public void mouseDragged(MouseEvent e) throws BehaviorException {
225
    }
159 226

  
160
	/* (non-Javadoc)
161
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseReleased(java.awt.event.MouseEvent)
162
	 */
163
	public void mouseReleased(MouseEvent e) throws BehaviorException {
164
	}
227
    /*
228
     * (non-Javadoc)
229
     *
230
     * @see
231
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseMoved(java.awt.
232
     * event.MouseEvent)
233
     */
234
    public void mouseMoved(MouseEvent e) throws BehaviorException {
235
    }
165 236

  
166
	/* (non-Javadoc)
167
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseDragged(java.awt.event.MouseEvent)
168
	 */
169
	public void mouseDragged(MouseEvent e) throws BehaviorException {
170
	}
237
    /*
238
     * (non-Javadoc)
239
     *
240
     * @see
241
     * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseWheelMoved(java
242
     * .awt.event.MouseWheelEvent)
243
     */
244
    public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
245
    }
171 246

  
172
	/* (non-Javadoc)
173
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseMoved(java.awt.event.MouseEvent)
174
	 */
175
	public void mouseMoved(MouseEvent e) throws BehaviorException {
176
	}
177

  
178
	/* (non-Javadoc)
179
	 * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseWheelMoved(java.awt.event.MouseWheelEvent)
180
	 */
181
	public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
182
	}
183
	
184
	/**
185
	 * Create point. If there is an
186
	 * error return <code>null</code> and add the error
187
	 * to the log
188
	 * @param x
189
	 * The X coordinate
190
	 * @param y
191
	 * The y coordinate
192
	 * @return
193
	 * The Point
194
	 */
195
	protected Point createPoint(double x, double y){
196
		Point point = null;
197
		try {
198
			point = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
199
			point.setX(x);
200
			point.setY(y);
201
		} catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
247
    /**
248
     * Create point. If there is an
249
     * error return <code>null</code> and add the error
250
     * to the log
251
     *
252
     * @param x
253
     *            The X coordinate
254
     * @param y
255
     *            The y coordinate
256
     * @return
257
     *         The Point
258
     */
259
    protected Point createPoint(double x, double y) {
260
        Point point = null;
261
        try {
262
            point = (Point) geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
263
            point.setX(x);
264
            point.setY(y);
265
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
202 266
            LOG.error("Error creating a point with x=" + x + ", y=" + y,
203
                new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D,
204
                e));
205
		}
206
		return point;
207
	}
208
	
209
	/**
210
	 * Create an Arc. If there is an
211
	 * error return <code>null</code> and add the error
212
	 * to the log
213
	 * @param p1
214
	 * @param p2
215
	 * @param p3
216
	 * @return
217
	 * The arc
218
	 */
219
	protected Arc createArc(Point2D p1, Point2D p2, Point2D p3){
220
		return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
221
	}
267
                new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e));
268
        }
269
        return point;
270
    }
222 271

  
223
	/**
224
	 * Create an arc. If there is an
225
	 * error return <code>null</code> and add the error
226
	 * to the log
227
	 * @param p1
228
	 * @param p2
229
	 * @param p3
230
	 * @return
231
	 * The arc
232
	 */
233
	protected Arc createArc(Point p1, Point p2, Point p3){
234
		Arc arc = null;
235
		try {
236
			arc = (Arc)geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D);
237
			arc.setPoints(p1, p2, p3);
238
	    } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
272
    /**
273
     * Create an Arc. If there is an
274
     * error return <code>null</code> and add the error
275
     * to the log
276
     *
277
     * @param p1
278
     * @param p2
279
     * @param p3
280
     * @return
281
     *         The arc
282
     */
283
    protected Arc createArc(Point2D p1, Point2D p2, Point2D p3) {
284
        return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
285
    }
286

  
287
    /**
288
     * Create an arc. If there is an
289
     * error return <code>null</code> and add the error
290
     * to the log
291
     *
292
     * @param p1
293
     * @param p2
294
     * @param p3
295
     * @return
296
     *         The arc
297
     */
298
    protected Arc createArc(Point p1, Point p2, Point p3) {
299
        Arc arc = null;
300
        try {
301
            arc = (Arc) geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D);
302
            arc.setPoints(p1, p2, p3);
303
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
239 304
            LOG.error("Error creating and arc with p1=" + p1 + ", p2=" + p2
240 305
                + ",p3=" + p3, new CreateGeometryException(TYPES.ARC,
241 306
                SUBTYPES.GEOM2D, e));
242 307
        } catch (IllegalArgumentException ex) {
243
            LOG.info("Warning: unable to create arc from points: "
244
                + p1.getX() + " " + p1.getY() + " :: "
245
                + p2.getX() + " " + p2.getY() + " :: "
246
                + p3.getX() + " " + p3.getY());
308
            LOG.info("Warning: unable to create arc from points: " + p1.getX()
309
                + " " + p1.getY() + " :: " + p2.getX() + " " + p2.getY()
310
                + " :: " + p3.getX() + " " + p3.getY());
247 311
            arc = null;
248 312
        }
249
	    return arc;
250
	}
251
	
252
	/**
253
	 * Create a curve point. If there is an
254
	 * error return <code>null</code> and add the error
255
	 * to the log
256
	 * @param p1
257
	 * The AWT point
258
	 * @return
259
	 * The gvSIG point
260
	 */
261
	protected Point createPoint(Point2D p1){
262
		return createPoint(p1.getX(), p1.getY());
263
	}
264
	
313
        return arc;
314
    }
265 315

  
266
	/**
267
	 * Create an arc. If there is an
268
	 * error return <code>null</code> and add the error
269
	 * to the log
270
        * @param centerX
271
        * @param centerY
272
        * @param radious
273
        * @param angleStart
274
        * @param angleExtent
275
        * @return The arc
276
	 */
277
	protected Arc createArc(double centerX, double centerY, double radious,
278
			double angleStart, double angleExtent){
279
		Arc arc = null;
280
		try {
281
			arc = (Arc)geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D);
282
			arc.setPoints(createPoint(centerX, centerY), radious, angleStart, angleExtent);
283
	    } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
316
    /**
317
     * Create a curve point. If there is an
318
     * error return <code>null</code> and add the error
319
     * to the log
320
     *
321
     * @param p1
322
     *            The AWT point
323
     * @return
324
     *         The gvSIG point
325
     */
326
    protected Point createPoint(Point2D p1) {
327
        return createPoint(p1.getX(), p1.getY());
328
    }
329

  
330
    /**
331
     * Create an arc. If there is an
332
     * error return <code>null</code> and add the error
333
     * to the log
334
     *
335
     * @param centerX
336
     * @param centerY
337
     * @param radious
338
     * @param angleStart
339
     * @param angleExtent
340
     * @return The arc
341
     */
342
    protected Arc createArc(double centerX, double centerY, double radious,
343
        double angleStart, double angleExtent) {
344
        Arc arc = null;
345
        try {
346
            arc = (Arc) geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D);
347
            arc.setPoints(createPoint(centerX, centerY), radious, angleStart,
348
                angleExtent);
349
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
284 350
            LOG.error(
285 351
                "Error creating an arc with centerX=" + centerX + ", centerY="
286 352
                    + centerY + ", radious=" + radious + ", angleStart="
......
290 356
            LOG.info("Warning: unable to create arc from points.");
291 357
            arc = null;
292 358
        }
293
	    return arc;
294
	}
359
        return arc;
360
    }
295 361

  
296
	/**
297
	 * Create an circle. If there is an
298
	 * error return <code>null</code> and add the error
299
	 * to the log
300
	 * @param centerX
301
	 * @param centerY
362
    /**
363
     * Create an circle. If there is an
364
     * error return <code>null</code> and add the error
365
     * to the log
366
     *
367
     * @param centerX
368
     * @param centerY
302 369
     * @param radious
303
	 * @return
304
	 * The arc
305
	 */
306
	protected Circle createCircle(double centerX, double centerY, double radious){
307
		Circle circle = null;
308
		try {
309
			circle = (Circle)geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
310
			circle.setPoints(createPoint(centerX, centerY), radious);
311
	    } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
370
     * @return
371
     *         The arc
372
     */
373
    protected Circle createCircle(double centerX, double centerY, double radious) {
374
        Circle circle = null;
375
        try {
376
            circle = (Circle) geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
377
            circle.setPoints(createPoint(centerX, centerY), radious);
378
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
312 379
            LOG.error("Error creating circle with centerX=" + centerX
313 380
                + ", centerY=" + centerY + ", radious=" + radious,
314
                new CreateGeometryException(TYPES.CIRCLE,
315
                SUBTYPES.GEOM2D, e));
316
		}
317
	    
318
	    return circle;
319
	}
381
                new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
382
        }
320 383

  
321
	/**
322
	 * Create a lineString from a GeneralPath. If there is an
323
	 * error return <code>null</code> and add the error
324
	 * to the log
325
	 * @param gpx
326
	 * The GeneralPath
327
	 * @return
328
	 * The LineString
329
	 */
330
	protected Curve createLineString(GeneralPathX gpx){
331
		Curve curve = null;
332
		try {
333
			curve = (Curve)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
334
			curve.setGeneralPath(gpx);
335
		} catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
384
        return circle;
385
    }
386

  
387
    /**
388
     * Create a lineString from a GeneralPath. If there is an
389
     * error return <code>null</code> and add the error
390
     * to the log
391
     *
392
     * @param gpx
393
     *            The GeneralPath
394
     * @return
395
     *         The LineString
396
     */
397
    protected Curve createLineString(GeneralPathX gpx) {
398
        Curve curve = null;
399
        try {
400
            curve = (Curve) geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
401
            curve.setGeneralPath(gpx);
402
        } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
336 403
            LOG.error("Error creating lineString with GeneralPathX=" + gpx,
337
                new CreateGeometryException(TYPES.CURVE, SUBTYPES.GEOM2D,
338
                e));
339
		}
340
		return curve;
341
	}
342
        
343
        
344
        public boolean isMyButton(MouseEvent e) {
345
            if( e == null ) {
346
                return this.isMyButton;
347
            }
348
            switch(this.mouseButton) {
349
                case BUTTON_LEFT:
350
                default:
351
                    this.isMyButton = SwingUtilities.isLeftMouseButton(e);
352
                    return this.isMyButton ;
353
                case BUTTON_MIDDLE:
354
                    this.isMyButton = SwingUtilities.isMiddleMouseButton(e);
355
                    return this.isMyButton ;
356
                case BUTTON_RIGHT:
357
                    this.isMyButton = SwingUtilities.isRightMouseButton(e);
358
                    return this.isMyButton ;
359
            }
404
                new CreateGeometryException(TYPES.CURVE, SUBTYPES.GEOM2D, e));
360 405
        }
361
        
362
        public boolean isMyButton() {
406
        return curve;
407
    }
408

  
409
    public void resetMyButton() {
410
        this.isMyButton = false;
411
    }
412

  
413
    public boolean isMyButton(MouseEvent e) {
414
        if (e == null) {
363 415
            return this.isMyButton;
364 416
        }
417
        switch (this.mouseButton) {
418
        case BUTTON_LEFT:
419
        default:
420
            this.isMyButton = SwingUtilities.isLeftMouseButton(e);
421
            return this.isMyButton;
422
        case BUTTON_MIDDLE:
423
            this.isMyButton = SwingUtilities.isMiddleMouseButton(e);
424
            return this.isMyButton;
425
        case BUTTON_RIGHT:
426
            this.isMyButton = SwingUtilities.isRightMouseButton(e);
427
            return this.isMyButton;
428
        }
429
    }
430

  
431
    public boolean isMyButton() {
432
        return this.isMyButton;
433
    }
365 434
}

Also available in: Unified diff