Statistics
| Revision:

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

History | View | Annotate | Download (5.75 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
 * Behaviour que espera un listener de tipo MeasureListener.
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62
public class PolylineBehavior extends Behavior {
63
        protected ArrayList arrayX = new ArrayList();
64
        protected ArrayList arrayY = new ArrayList();
65
        protected boolean isClicked = false;
66
        protected PolylineListener listener;
67

    
68
        /**
69
         * Crea un nuevo PolylineBehavior.
70
         *
71
         * @param mli listener.
72
         */
73
        public PolylineBehavior(PolylineListener mli) {
74
                listener = mli;
75
        }
76

    
77
        /**
78
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
79
         */
80
        public void paintComponent(Graphics g) {
81
                g.drawImage(getMapControl().getImage(), 0, 0, null);
82
                g.setColor(Color.black);
83

    
84
                if (!arrayX.isEmpty()) {
85
                        drawPolyLine((Graphics2D) g);
86
                }
87
        }
88

    
89
        /**
90
         * Reimplementaci?n del m?todo mousePressed de Behavior.
91
         *
92
         * @param E MouseEvent
93
         *
94
         * @throws BehaviorException Excepci?n lanzada cuando el Behavior.
95
         */
96
        public void mousePressed(MouseEvent E) throws BehaviorException {
97
                if (E.getClickCount() == 2) {
98
                        //System.err.println("dobleclick");
99
                        //Point2D p1=getMapControl().getViewPort().toMapPoint();
100
                        listener.polylineFinished(new MeasureEvent(
101
                                        (Double[]) arrayX.toArray(new Double[0]),
102
                                        (Double[]) arrayY.toArray(new Double[0]), E));
103
                        arrayX.clear();
104
                        arrayY.clear();
105
                        isClicked = false;
106
                } else {
107
                        //System.err.println("simpleclick");
108
                        isClicked = true;
109
                        addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
110

    
111
                        if (arrayX.size() < 2) {
112
                                addPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
113
                        }
114

    
115
                        listener.pointFixed(new MeasureEvent(
116
                                        (Double[]) arrayX.toArray(new Double[0]),
117
                                        (Double[]) arrayY.toArray(new Double[0]), E));
118
                }
119
        }
120

    
121
        /**
122
         * Reimplementaci?n del m?todo mouseDragged de Behavior.
123
         *
124
         * @param e MouseEvent
125
         *
126
         * @throws BehaviorException Excepci?n lanzada cuando el Behavior.
127
         */
128
        public void mouseDragged(MouseEvent e) throws BehaviorException {
129
                mouseMoved(e);
130
        }
131

    
132
        /**
133
         * Cambio del ?ltimo punto.
134
         *
135
         * @param p punto.
136
         */
137
        protected void changeLastPoint(Point2D p) {
138
                arrayX.set(arrayX.size() - 1, new Double(p.getX()));
139
                arrayY.set(arrayY.size() - 1, new Double(p.getY()));
140
        }
141

    
142
        /**
143
         * Reimplementaci?n del m?todo mouseMoved de Behavior.
144
         *
145
         * @param e MouseEvent
146
         *
147
         * @throws BehaviorException Excepci?n lanzada cuando el Behavior.
148
         */
149
        public void mouseMoved(MouseEvent E) throws BehaviorException {
150
                //System.err.println("moved antes de click");
151
                if (isClicked) {
152
                        //System.err.println("moved despues de click");
153
                        changeLastPoint(getMapControl().getViewPort().toMapPoint(E.getPoint()));
154

    
155
                        MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(
156
                                                new Double[0]),
157
                                        (Double[]) arrayY.toArray(new Double[0]), E);
158
                        listener.points(event);
159
                        getMapControl().repaint();
160
                }
161
        }
162

    
163
        /**
164
         * Dibujo de la polil?nea.
165
         *
166
         * @param g2 Graphics2D sobre el que dibujamos.
167
         */
168
        protected void drawPolyLine(Graphics2D g2) {
169
                GeneralPathX polyline = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
170
                                arrayX.size());
171
                Point2D pScreen0=getMapControl().getViewPort().fromMapPoint(new Point2D.Double(((Double) arrayX.get(0)).doubleValue(),
172
                                ((Double) arrayY.get(0)).doubleValue()));
173
                polyline.moveTo(pScreen0.getX(),
174
                                pScreen0.getY());
175

    
176
                for (int index = 1; index < arrayX.size(); index++) {
177
                        Point2D pScreen=getMapControl().getViewPort().fromMapPoint(new Point2D.Double(((Double) arrayX.get(index)).doubleValue(),
178
                                        ((Double) arrayY.get(index)).doubleValue()));
179
                        polyline.lineTo(pScreen.getX(),pScreen.getY());
180
                }
181

    
182
                g2.draw(polyline);
183
        }
184

    
185
        /**
186
         * A?ade un punto a la polil?nea.
187
         *
188
         * @param p Punto.
189
         */
190
        protected void addPoint(Point2D p) {
191
                arrayX.add(new Double(p.getX()));
192
                arrayY.add(new Double(p.getY()));
193
        }
194

    
195
        /**
196
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setListener(com.iver.cit.gvsig.fmap.tools.ToolListener)
197
         */
198
        public void setListener(ToolListener listener) {
199
                this.listener = (PolylineListener) listener;
200
        }
201

    
202
        /**
203
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
204
         */
205
        public ToolListener getListener() {
206
                return listener;
207
        }
208
}