Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / CircleBehavior.java @ 44246

History | View | Annotate | Download (7.15 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontrol.tools.Behavior;
25

    
26
import java.awt.Color;
27
import java.awt.Rectangle;
28
import java.awt.event.MouseEvent;
29
import java.awt.geom.Point2D;
30
import java.awt.geom.Rectangle2D;
31

    
32
import org.gvsig.fmap.geom.primitive.Arc;
33
import org.gvsig.fmap.mapcontext.ViewPort;
34
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
35
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
36
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
37
import org.gvsig.fmap.mapcontrol.tools.Listeners.AbstractCircleListener;
38
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
39
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
40

    
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
/**
45
 * <p>
46
 * Behavior that allows user to draw a circle on the image of the associated
47
 * <code>MapControl</code> using a
48
 * {@link PolylineListener PolylineListener}.</p>
49
 *
50
 * @author Laura
51
 * @author Pablo Piqueras Bartolom?
52
 */
53
public class CircleBehavior extends Behavior {
54

    
55
    private static final Logger logger = LoggerFactory.getLogger(CircleBehavior.class);
56

    
57
    /**
58
     * First point set, that represents the center of the circle.
59
     */
60
    protected Point2D m_FirstPoint;
61

    
62
    /**
63
     * Second point set, that permits calculate the radius of the circle.
64
     */
65
    protected Point2D m_LastPoint;
66

    
67
    /**
68
     * Tool listener used to work with the <code>MapControl</code> object.
69
     *
70
     * @see #getListener()
71
     * @see #setListener(ToolListener)
72
     */
73
    private AbstractCircleListener listener;
74

    
75
    /**
76
     * Determines if user setting the radius of the circle (with one click of
77
     * the button 1 of the mouse), or not.
78
     */
79
    protected boolean isClicked = false;
80

    
81
    /**
82
     * <p>
83
     * Creates a new behavior for selecting circle areas.</p>
84
     *
85
     * @param zili listener used to permit this object to work with the
86
     * associated <code>MapControl</code>
87
     */
88
    public CircleBehavior(AbstractCircleListener zili) {
89
        listener = zili;
90
    }
91

    
92
    /*
93
         * (non-Javadoc)
94
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
95
     */
96
    @Override
97
    public void paintComponent(MapControlDrawer mapControlDrawer) {
98
        double radio;
99
        mapControlDrawer.setColor(Color.black);
100

    
101
        if ((m_FirstPoint != null) && (m_LastPoint != null)) {
102
            ViewPort vp = getMapControl().getMapContext().getViewPort();
103
            Point2D p1 = vp.toMapPoint(m_FirstPoint);
104
            Point2D p2 = vp.toMapPoint(m_LastPoint);
105

    
106
            radio = p1.distance(p2);
107
            if (radio > 0.0) {
108
                Arc arc = null;
109
                arc = createArc(p1.getX(), p1.getY(),
110
                        radio, 0, Math.PI * 2);
111
                if (arc != null) {
112
                    mapControlDrawer.draw(arc);
113
                }
114
            }
115

    
116
        }
117
    }
118

    
119
    /*
120
         * (non-Javadoc)
121
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
122
     */
123
    @Override
124
    public void mousePressed(MouseEvent e) {
125
        if (this.isMyButton(e)) {
126
            m_FirstPoint = e.getPoint();
127
            isClicked = true;
128
            getMapControl().repaint();
129
            if (listener.cancelDrawing()) {
130
                getMapControl().cancelDrawing();
131
                isClicked = false;
132
                getMapControl().repaint();
133
            }
134
        }
135

    
136
    }
137

    
138
    /*
139
         * (non-Javadoc)
140
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
141
     */
142
    @Override
143
    public void mouseReleased(MouseEvent e) throws BehaviorException {
144

    
145
        if (m_FirstPoint == null) {
146
            return;
147
        }
148

    
149
        m_LastPoint = e.getPoint();
150

    
151
        ViewPort vp = getMapControl().getMapContext().getViewPort();
152

    
153
        Point2D p1 = vp.toMapPoint(m_FirstPoint);
154
        Point2D p2 = vp.toMapPoint(m_LastPoint);
155

    
156
        Double[] x = new Double[2];
157
        Double[] y = new Double[2];
158
        x[0] = p1.getX();
159
        x[1] = p2.getX();
160
        y[0] = p1.getY();
161
        y[1] = p2.getY();
162
        MeasureEvent event = new MeasureEvent(x, y, e);
163

    
164
        listener.circleFinished(event);
165

    
166
        if (this.isMyButton(e)) {
167
            m_FirstPoint = null;
168
            m_LastPoint = null;
169
            isClicked = false;
170
        }
171
    }
172

    
173
    /*
174
         * (non-Javadoc)
175
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
176
     */
177
    @Override
178
    public void mouseDragged(MouseEvent e) throws BehaviorException {
179
        mouseMoved(e);
180
    }
181

    
182
    /*
183
         * (non-Javadoc)
184
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
185
     */
186
    @Override
187
    public void mouseMoved(MouseEvent e) throws BehaviorException {
188
        if (m_FirstPoint == null) {
189
            return;
190
        }
191

    
192
        m_LastPoint = e.getPoint();
193

    
194
        ViewPort vp = getMapControl().getMapContext().getViewPort();
195

    
196
        Point2D p1 = vp.toMapPoint(m_FirstPoint);
197
        Point2D p2 = vp.toMapPoint(m_LastPoint);
198

    
199
        //        Fijamos el nuevo extent
200
        Rectangle2D.Double r = new Rectangle2D.Double();
201
        r.setFrameFromDiagonal(p1, p2);
202

    
203
        Rectangle2D rectPixel = new Rectangle();
204
        rectPixel.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
205

    
206
        Double[] x = new Double[2];
207
        Double[] y = new Double[2];
208
        x[0] = p1.getX();
209
        x[1] = p2.getX();
210
        y[0] = p1.getY();
211
        y[1] = p2.getY();
212
        MeasureEvent event = new MeasureEvent(x, y, e);
213
        listener.circle(event);
214
        getMapControl().repaint();
215
    }
216

    
217
    /**
218
     * <p>
219
     * Sets a tool listener to work with the <code>MapControl</code> using this
220
     * behavior.</p>
221
     *
222
     * @param listener a <code>CircleListener</code> object for this behavior
223
     */
224
    public void setListener(ToolListener listener) {
225
        this.listener = (AbstractCircleListener) listener;
226
    }
227

    
228
    /*
229
         * (non-Javadoc)
230
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
231
     */
232
    @Override
233
    public ToolListener getListener() {
234
        return listener;
235
    }
236
}