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 / PolylineBehavior.java @ 45680

History | View | Annotate | Download (6.43 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.event.MouseEvent;
28
import java.awt.geom.Point2D;
29
import java.util.ArrayList;
30
import java.util.List;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.fmap.geom.primitive.Line;
33
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
34
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
35
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
36
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
37
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
38

    
39
/**
40
 * <p>
41
 * Behavior that allows user to draw a polyline by its vertexes on the image of
42
 * the associated <code>MapControl</code> using a
43
 * {@link PolylineListener PolylineListener}.</p>
44
 *
45
 * @author Vicente Caballero Navarro
46
 * @author Pablo Piqueras Bartolom?
47
 */
48
public class PolylineBehavior extends Behavior {
49

    
50
    /**
51
     * The abscissa coordinate of all vertexes of the polyline.
52
     */
53
    protected List<Double> arrayX = new ArrayList<>();
54

    
55
    /**
56
     * The ordinate coordinate of all vertexes of the polyline.
57
     */
58
    protected List<Double> arrayY = new ArrayList<>();
59

    
60
    /**
61
     * Determines if user is setting the vertexes (with one click of the button
62
     * 1 of the mouse), or not.
63
     */
64
    protected boolean isClicked = false;
65

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

    
74
    /**
75
     * <p>
76
     * Creates a new behavior for drawing a polyline by its vertexes.</p>
77
     *
78
     * @param mli listener used to permit this object to work with the
79
     * associated <code>MapControl</code>
80
     */
81
    public PolylineBehavior(PolylineListener mli) {
82
        super(Behavior.BUTTON_LEFT);
83
        listener = mli;
84
    }
85

    
86
    /*
87
         * (non-Javadoc)
88
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
89
     */
90
    @Override
91
    public void paintComponent(MapControlDrawer mapControlDrawer) {
92
        mapControlDrawer.setColor(Color.black);
93

    
94
        if (!arrayX.isEmpty()) {
95
            drawPolyLine(mapControlDrawer);
96
        }
97
    }
98

    
99
    @Override
100
    public void mouseClicked(MouseEvent e) throws BehaviorException {
101
        if (!isMyButton(e)) {
102
            return;
103
        }
104
        if (e.getClickCount() == 2) {
105
            listener.polylineFinished(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e));
106

    
107
            arrayX.clear();
108
            arrayY.clear();
109

    
110
            isClicked = false;
111
        } else {
112
            isClicked = true;
113
            Point2D point = getMapControl().convertToMapPoint(e.getPoint(), this.getUseSnapping());
114
            addPoint(point);
115

    
116
            if (arrayX.size() < 2) {
117
                addPoint(point);
118
            }
119

    
120
            listener.pointFixed(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e));
121
        }
122
    }
123

    
124
    @Override
125
    public void mouseDragged(MouseEvent e) throws BehaviorException {
126
        mouseMoved(e);
127
    }
128

    
129
    /**
130
     * <p>
131
     * Changes the last point added of the polyline.</p>
132
     *
133
     * @param p a point which will replace the last added to the polyline
134
     */
135
    protected void changeLastPoint(Point2D p) {
136
        if (arrayX.size() > 0) {
137
            arrayX.set(arrayX.size() - 1, p.getX());
138
            arrayY.set(arrayY.size() - 1, p.getY());
139
        }
140
    }
141

    
142
    @Override
143
    public void mouseMoved(MouseEvent e) throws BehaviorException {
144
        if (isClicked) {
145
            //System.err.println("moved despues de click");
146
            Point2D p = getMapControl().convertToMapPoint(e.getPoint(), this.getUseSnapping());
147
            changeLastPoint(p);
148
            
149
            MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e);
150

    
151
            listener.points(event);
152
            getMapControl().repaint();
153
        }
154
    }
155

    
156
    /**
157
     * <p>
158
     * Draws the polyline in the <code>Graphics2D</code> of the associated
159
     * <code>MapControl</code>.</p>
160
     *
161
     * @param mapControlDrawer
162
     */
163
    protected void drawPolyLine(MapControlDrawer mapControlDrawer) {
164

    
165
        try {
166
            Line line = geomManager.createLine(Geometry.SUBTYPES.GEOM2D);
167
            for (int index = 0; index < arrayX.size(); index++) {
168
                line.addVertex(arrayX.get(index), arrayY.get(index));
169
            }
170
            mapControlDrawer.draw(line);
171
        } catch (Exception ex) {
172
            LOG.warn("Can't draw polyline", ex);
173
        }
174
    }
175

    
176
    /**
177
     * <p>
178
     * Adds a new point to the polyline.</p>
179
     *
180
     * @param p a new point to the polyline
181
     */
182
    protected void addPoint(Point2D p) {
183
        arrayX.add(p.getX());
184
        arrayY.add(p.getY());
185
    }
186

    
187
    /**
188
     * <p>
189
     * Sets a tool listener to work with the <code>MapControl</code> using this
190
     * behavior.</p>
191
     *
192
     * @param listener a <code>PolylineListener</code> object for this behavior
193
     */
194
    public void setListener(ToolListener listener) {
195
        this.listener = (PolylineListener) listener;
196
    }
197

    
198
    @Override
199
    public ToolListener getListener() {
200
        return listener;
201
    }
202
}