Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / Behavior / PolylineBehavior.java @ 20100

History | View | Annotate | Download (6.54 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 com.iver.cit.gvsig.fmap.core.GeneralPathX;
44
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
45
import com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent;
46
import com.iver.cit.gvsig.fmap.tools.Listeners.PolylineListener;
47
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
48

    
49
import java.awt.Color;
50
import java.awt.Graphics;
51
import java.awt.Graphics2D;
52
import java.awt.event.MouseEvent;
53
import java.awt.geom.Point2D;
54

    
55
import java.util.ArrayList;
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
 */
64
public class PolylineBehavior extends Behavior {
65
        /**
66
         * The abscissa coordinate of all vertexes of the polyline. 
67
         */
68
        protected ArrayList arrayX = new ArrayList();
69

    
70
        /**
71
         * The ordinate coordinate of all vertexes of the polyline. 
72
         */
73
        protected ArrayList arrayY = new ArrayList();
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
                        //System.err.println("dobleclick");
117
                        //Point2D p1=getMapControl().getViewPort().toMapPoint();
118
                        listener.polylineFinished(new MeasureEvent(
119
                                        (Double[]) arrayX.toArray(new Double[0]),
120
                                        (Double[]) arrayY.toArray(new Double[0]), E));
121
                        arrayX.clear();
122
                        arrayY.clear();
123
                        isClicked = false;
124
                } else {
125
                        //System.err.println("simpleclick");
126
                        isClicked = true;
127
                        addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
128

    
129
                        if (arrayX.size() < 2) {
130
                                addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
131
                        }
132

    
133
                        listener.pointFixed(new MeasureEvent(
134
                                        (Double[]) arrayX.toArray(new Double[0]),
135
                                        (Double[]) arrayY.toArray(new Double[0]), E));
136
                }
137
        }
138

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

    
147
        /**
148
         * <p>Changes the last point added of the polyline.</p>
149
         *
150
         * @param p a point which will replace the last added to the polyline
151
         */
152
        protected void changeLastPoint(Point2D p) {
153
                arrayX.set(arrayX.size() - 1, new Double(p.getX()));
154
                arrayY.set(arrayY.size() - 1, new Double(p.getY()));
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(
168
                                                new Double[0]),
169
                                        (Double[]) arrayY.toArray(new Double[0]), E);
170
                        listener.points(event);
171
                        getMapControl().repaint();
172
                }
173
        }
174

    
175
        /**
176
         * <p>Draws the polyline in the <code>Graphics2D</code> of the associated <code>MapControl</code>.</p>
177
         *
178
         * @param g2 the 2D context that allows draw the polyline
179
         */
180
        protected void drawPolyLine(Graphics2D g2) {
181
                GeneralPathX polyline = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
182
                                arrayX.size());
183
                Point2D pScreen0=getMapControl().getViewPort().fromMapPoint(new Point2D.Double(((Double) arrayX.get(0)).doubleValue(),
184
                                ((Double) arrayY.get(0)).doubleValue()));
185
                polyline.moveTo(pScreen0.getX(),
186
                                pScreen0.getY());
187

    
188
                for (int index = 1; index < arrayX.size(); index++) {
189
                        Point2D pScreen=getMapControl().getViewPort().fromMapPoint(new Point2D.Double(((Double) arrayX.get(index)).doubleValue(),
190
                                        ((Double) arrayY.get(index)).doubleValue()));
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
}