Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / Behavior / PolylineBehavior.java @ 20098

History | View | Annotate | Download (6.55 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 com.iver.cit.gvsig.fmap.tools.Behavior;
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 com.iver.cit.gvsig.fmap.core.GeneralPathX;
51
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
52
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent;
53
import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener;
54
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
55

    
56

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
156
        /*
157
         * (non-Javadoc)
158
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
159
         */
160
        public void mouseMoved(MouseEvent e) throws BehaviorException {
161
                //System.err.println("moved antes de click");
162
                if (isClicked) {
163
                        //System.err.println("moved despues de click");
164
                        changeLastPoint(getMapControl().getViewPort().toMapPoint(e.getPoint()));
165

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

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

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

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

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

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

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

    
193
                g2.draw(polyline);
194
        }
195

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

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

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