Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libFMap_mapcontrol / src / org / gvsig / fmap / mapcontrol / tools / Behaibor / PolylineBehavior.java @ 21203

History | View | Annotate | Download (6.78 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.mapcontrol.tools.Behaibor;
42

    
43
import java.awt.Color;
44
import java.awt.Graphics;
45
import java.awt.Graphics2D;
46
import java.awt.event.MouseEvent;
47
import java.awt.geom.Point2D;
48
import java.util.ArrayList;
49

    
50
import org.gvsig.fmap.geom.primitive.GeneralPathX;
51
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
52
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
53
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener;
54
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
55

    
56

    
57

    
58
/**
59
 * <p>Behavior that allows user to draw a polyline by its vertexes on the image of the associated
60
 *  <code>MapControl</code> using a {@link PolylineListener PolylineListener}.</p>
61
 *
62
 * @author Vicente Caballero Navarro
63
 * @author Pablo Piqueras Bartolom?
64
 */
65
public class PolylineBehavior extends Behavior {
66
        /**
67
         * The abscissa coordinate of all vertexes of the polyline.
68
         */
69
        protected ArrayList<Double> arrayX = new ArrayList<Double>();
70

    
71
        /**
72
         * The ordinate coordinate of all vertexes of the polyline.
73
         */
74
        protected ArrayList<Double> arrayY = new ArrayList<Double>();
75

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

    
81
        /**
82
         * Tool listener used to work with the <code>MapControl</code> object.
83
         *
84
         * @see #getListener()
85
         * @see #setListener(ToolListener)
86
         */
87
        protected PolylineListener listener;
88

    
89
        /**
90
         * <p>Creates a new behavior for drawing a polyline by its vertexes.</p>
91
         *
92
         * @param mli listener used to permit this object to work with the associated <code>MapControl</code>
93
         */
94
        public PolylineBehavior(PolylineListener mli) {
95
                listener = mli;
96
        }
97

    
98
        /*
99
         * (non-Javadoc)
100
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
101
         */
102
        public void paintComponent(Graphics g) {
103
                g.drawImage(getMapControl().getImage(), 0, 0, null);
104
                g.setColor(Color.black);
105

    
106
                if (!arrayX.isEmpty()) {
107
                        drawPolyLine((Graphics2D) g);
108
                }
109
        }
110

    
111
        /*
112
         * (non-Javadoc)
113
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
114
         */
115
        public void mousePressed(MouseEvent e) throws BehaviorException {
116
                if (e.getClickCount() == 2) {
117
                        listener.polylineFinished(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e));
118

    
119
                        arrayX.clear();
120
                        arrayY.clear();
121

    
122
                        isClicked = false;
123
                } else {
124
                        //System.err.println("simpleclick");
125
                        isClicked = true;
126
                        Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
127
                        addPoint(point);
128

    
129
                        if (arrayX.size() < 2) {
130
                                addPoint(point);
131
                        }
132

    
133
                        listener.pointFixed(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e));
134
                }
135
        }
136

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

    
145
        /**
146
         * <p>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, new Double(p.getX()));
153
                        arrayY.set(arrayY.size() - 1, new Double(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
        public void mouseMoved(MouseEvent e) throws BehaviorException {
162
                //System.err.println("moved antes de click");
163
                if (isClicked) {
164
                        //System.err.println("moved despues de click");
165
                        changeLastPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
166

    
167
                        MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e);
168

    
169
                        listener.points(event);
170
                        getMapControl().repaint();
171
                }
172
        }
173

    
174
        /**
175
         * <p>Draws the polyline in the <code>Graphics2D</code> of the associated <code>MapControl</code>.</p>
176
         *
177
         * @param g2 the 2D context that allows draw the polyline
178
         */
179
        protected void drawPolyLine(Graphics2D g2) {
180
                GeneralPathX polyline = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, arrayX.size());
181

    
182
                Point2D pScreen0=getMapControl().getViewPort().fromMapPoint(new Point2D.Double(((Double) arrayX.get(0)).doubleValue(),
183
                                ((Double) arrayY.get(0)).doubleValue()));
184

    
185
                polyline.moveTo(pScreen0.getX(), pScreen0.getY());
186

    
187
                for (int index = 0; index < arrayX.size(); index++) {
188
                        Point2D pScreen=getMapControl().getViewPort().fromMapPoint(new Point2D.Double(((Double) arrayX.get(index)).doubleValue(),
189
                                        ((Double) arrayY.get(index)).doubleValue()));
190

    
191
                        polyline.lineTo(pScreen.getX(),pScreen.getY());
192
                }
193

    
194
                g2.draw(polyline);
195
        }
196

    
197
        /**
198
         * <p>Adds a new point to the polyline.</p>
199
         *
200
         * @param p a new point to the polyline
201
         */
202
        protected void addPoint(Point2D p) {
203
                arrayX.add(new Double(p.getX()));
204
                arrayY.add(new Double(p.getY()));
205
        }
206

    
207
        /**
208
         * <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
209
         *
210
         * @param listener a <code>PolylineListener</code> object for this behavior
211
         */
212
        public void setListener(ToolListener listener) {
213
                this.listener = (PolylineListener) listener;
214
        }
215

    
216
        /*
217
         * (non-Javadoc)
218
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
219
         */
220
        public ToolListener getListener() {
221
                return listener;
222
        }
223
}