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 / FreehandBehavior.java @ 44246

History | View | Annotate | Download (7.56 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.GeometryLocator;
33
import org.gvsig.fmap.geom.exception.CreateGeometryException;
34

    
35
import org.gvsig.fmap.geom.primitive.Line;
36
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
37
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
38
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
39
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
40
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
41

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

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

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

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

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

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

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

    
97
        if (!arrayX.isEmpty()) {
98
            drawPolyLine(mapControlDrawer);
99
        }
100
    }
101

    
102
    /*
103
         * (non-Javadoc)
104
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
105
     */
106
    @Override
107
    public void mousePressed(MouseEvent e) throws BehaviorException {
108
        if (!isMyButton(e)) {
109
            return;
110
        }
111
        if (e.getClickCount() == 2) {
112
            listener.polylineFinished(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e));
113

    
114
            arrayX.clear();
115
            arrayY.clear();
116

    
117
            isClicked = false;
118
        } else {
119
            isClicked = true;
120
        
121
        }
122
//        } else {
123
//            isClicked = true;
124
//            Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
125
//            addPoint(point);
126
//
127
//            if (arrayX.size() < 2) {
128
//                addPoint(point);
129
//            }
130
//
131
//            listener.pointFixed(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e));
132
//        }
133
    }
134

    
135
    /*
136
         * (non-Javadoc)
137
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
138
     */
139
    @Override
140
    public void mouseDragged(MouseEvent e) throws BehaviorException {
141
        mouseMoved(e);
142
    }
143

    
144
    /**
145
     * <p>
146
     * Changes the last point added of the polyline.</p>
147
     *
148
     * @param p a point which will replace the last added to the polyline
149
     */
150
    protected void changeLastPoint(Point2D p) {
151
        if (arrayX.size() > 0) {
152
            arrayX.set(arrayX.size() - 1, p.getX());
153
            arrayY.set(arrayY.size() - 1, p.getY());
154
        }
155
    }
156

    
157
    /*
158
         * (non-Javadoc)
159
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
160
     */
161
    @Override
162
    public void mouseMoved(MouseEvent e) throws BehaviorException {
163
        if (isClicked) {
164
            //System.err.println("moved despues de click");
165
//            changeLastPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
166
            Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
167
            addPoint(point);
168
            
169
            MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e);
170

    
171
            listener.points(event);
172
            getMapControl().repaint();
173
        }
174
    }
175

    
176
    /**
177
     * <p>
178
     * Draws the polyline in the <code>Graphics2D</code> of the associated
179
     * <code>MapControl</code>.</p>
180
     *
181
     * @param mapControlDrawer
182
     */
183
    protected void drawPolyLine(MapControlDrawer mapControlDrawer) {
184

    
185
//                GeneralPathX polyline = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, arrayX.size());
186
//
187
//                polyline.moveTo(((Double) arrayX.get(0)).doubleValue(),
188
//                                ((Double) arrayY.get(0)).doubleValue());
189
//
190
//                for (int index = 0; index < arrayX.size(); index++) {
191
//                    polyline.lineTo(((Double) arrayX.get(index)).doubleValue(),((Double) arrayY.get(index)).doubleValue());
192
//                }
193
        try {
194
            Line line = geomManager.createLine(Geometry.SUBTYPES.GEOM2D);
195
            for (int index = 0; index < arrayX.size(); index++) {
196
                line.addVertex(arrayX.get(index), arrayY.get(index));
197
            }
198
            mapControlDrawer.draw(line);
199
        } catch (Exception ex) {
200
            LOG.warn("Can't draw polyline", ex);
201
        }
202
    }
203

    
204
    /**
205
     * <p>
206
     * Adds a new point to the polyline.</p>
207
     *
208
     * @param p a new point to the polyline
209
     */
210
    protected void addPoint(Point2D p) {
211
        arrayX.add(p.getX());
212
        arrayY.add(p.getY());
213
    }
214

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

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