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 @ 45680

History | View | Annotate | Download (7.15 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40559 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40559 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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 40559 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20 40435 jjdelcerro
 *
21 40559 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
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 44246 omartinez
import org.gvsig.fmap.mapcontrol.tools.Listeners.AbstractCircleListener;
38 40435 jjdelcerro
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
39
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
40 42036 fdiaz
41 41977 jjdelcerro
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43 40435 jjdelcerro
44
/**
45 44246 omartinez
 * <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 40435 jjdelcerro
 *
50
 * @author Laura
51
 * @author Pablo Piqueras Bartolom?
52
 */
53
public class CircleBehavior extends Behavior {
54 42036 fdiaz
55 44246 omartinez
    private static final Logger logger = LoggerFactory.getLogger(CircleBehavior.class);
56 40435 jjdelcerro
57 44246 omartinez
    /**
58
     * First point set, that represents the center of the circle.
59
     */
60
    protected Point2D m_FirstPoint;
61 40435 jjdelcerro
62 44246 omartinez
    /**
63
     * Second point set, that permits calculate the radius of the circle.
64
     */
65
    protected Point2D m_LastPoint;
66 40435 jjdelcerro
67 44246 omartinez
    /**
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 40435 jjdelcerro
75 44246 omartinez
    /**
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 40435 jjdelcerro
81 44246 omartinez
    /**
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 40435 jjdelcerro
         * (non-Javadoc)
94
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
95 44246 omartinez
     */
96
    @Override
97
    public void paintComponent(MapControlDrawer mapControlDrawer) {
98
        double radio;
99
        mapControlDrawer.setColor(Color.black);
100 40435 jjdelcerro
101 44246 omartinez
        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 40435 jjdelcerro
106 44246 omartinez
            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 40435 jjdelcerro
116 44246 omartinez
        }
117
    }
118 40435 jjdelcerro
119 44246 omartinez
    /*
120 40435 jjdelcerro
         * (non-Javadoc)
121
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
122 44246 omartinez
     */
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 41977 jjdelcerro
                getMapControl().repaint();
133
            }
134 44246 omartinez
        }
135 40435 jjdelcerro
136 44246 omartinez
    }
137 41977 jjdelcerro
138 44246 omartinez
    /*
139 40435 jjdelcerro
         * (non-Javadoc)
140
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
141 44246 omartinez
     */
142
    @Override
143
    public void mouseReleased(MouseEvent e) throws BehaviorException {
144 40435 jjdelcerro
145 44246 omartinez
        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 40435 jjdelcerro
         * (non-Javadoc)
175
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
176 44246 omartinez
     */
177
    @Override
178
    public void mouseDragged(MouseEvent e) throws BehaviorException {
179
        mouseMoved(e);
180
    }
181 40435 jjdelcerro
182 44246 omartinez
    /*
183 40435 jjdelcerro
         * (non-Javadoc)
184
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
185 44246 omartinez
     */
186
    @Override
187
    public void mouseMoved(MouseEvent e) throws BehaviorException {
188
        if (m_FirstPoint == null) {
189
            return;
190
        }
191 40435 jjdelcerro
192 44246 omartinez
        m_LastPoint = e.getPoint();
193 40435 jjdelcerro
194 44246 omartinez
        ViewPort vp = getMapControl().getMapContext().getViewPort();
195 40435 jjdelcerro
196 44246 omartinez
        Point2D p1 = vp.toMapPoint(m_FirstPoint);
197
        Point2D p2 = vp.toMapPoint(m_LastPoint);
198 40435 jjdelcerro
199 44246 omartinez
        //        Fijamos el nuevo extent
200
        Rectangle2D.Double r = new Rectangle2D.Double();
201
        r.setFrameFromDiagonal(p1, p2);
202 40435 jjdelcerro
203 44246 omartinez
        Rectangle2D rectPixel = new Rectangle();
204
        rectPixel.setFrameFromDiagonal(m_FirstPoint, m_LastPoint);
205 40435 jjdelcerro
206 44246 omartinez
        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 40435 jjdelcerro
217 44246 omartinez
    /**
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 40435 jjdelcerro
228 44246 omartinez
    /*
229 40435 jjdelcerro
         * (non-Javadoc)
230
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
231 44246 omartinez
     */
232
    @Override
233
    public ToolListener getListener() {
234
        return listener;
235
    }
236 40435 jjdelcerro
}