Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / MeasureListener.java @ 38605

History | View | Annotate | Download (3.76 KB)

1 7771 caballero
/* 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 29596 jpiera
package org.gvsig.app.project.documents.view.toolListeners;
42 7771 caballero
43
import java.awt.geom.Point2D;
44
import java.text.NumberFormat;
45
46 29596 jpiera
import org.gvsig.andami.PluginServices;
47 20994 jmvivo
import org.gvsig.fmap.mapcontext.MapContext;
48
import org.gvsig.fmap.mapcontext.ViewPort;
49
import org.gvsig.fmap.mapcontrol.MapControl;
50
import org.gvsig.fmap.mapcontrol.tools.MeasureListenerImpl;
51
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
52
53 7771 caballero
54
55
/**
56 20098 jmvivo
 * <p>Listener for calculating distances using vertexes of a polyline, defined in the associated {@link MapControl MapControl}
57
 *  object, and displaying that information at the status bar of the application's main frame.</p>
58 20334 vcaballero
 *
59 20098 jmvivo
 * <p>Calculates and displays:
60
 *  <ul>
61
 *   <li>Distance of the last segment of the polyline.</li>
62
 *   <li>Accumulated distance of all segments of the polyline.</li>
63
 *  </ul>
64
 * </p>
65 7771 caballero
 *
66 20098 jmvivo
 * @see MeasureListenerImpl
67
 *
68 7771 caballero
 * @author Vicente Caballero Navarro
69
 */
70
public class MeasureListener extends MeasureListenerImpl {
71
        /**
72 20098 jmvivo
         * <p>Creates a new listener for calculating distances using points of a polyline.</p>
73 7771 caballero
         *
74 20098 jmvivo
         * @param mc the <code>MapControl</code> where is calculated the length
75 7771 caballero
         */
76
        public MeasureListener(MapControl mc) {
77
                super(mc);
78
        }
79
80 20098 jmvivo
        /*
81
         * (non-Javadoc)
82
         * @see com.iver.cit.gvsig.fmap.tools.MeasureListenerImpl#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
83 7771 caballero
         */
84
        public void points(MeasureEvent event) {
85
                double dist = 0;
86
                double distAll = 0;
87
88
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
89
90
                Point2D p = new Point2D.Double(event.getXs()[0].doubleValue(),event.getYs()[0].doubleValue());//vp.toMapPoint(new Point(event.getXs()[0].intValue(),event.getYs()[0].intValue()));
91
                for (int i = 1; i < (event.getXs().length); i++) {
92
                        Point2D p2 = new Point2D.Double(event.getXs()[i].doubleValue(),        event.getYs()[i].doubleValue());// vp.toMapPoint(new Point(event.getXs()[i].intValue(),event.getYs()[i].intValue()));
93
                        dist = vp.distanceWorld(p, p2);
94
                        distAll += dist;
95
                        p = p2;
96
                }
97
98
                //System.out.println("Distancia = " + dist + " Distancia Total = " +
99
                //        (distAll));
100
                NumberFormat nf = NumberFormat.getInstance();
101
        nf.setMaximumFractionDigits(2);
102
        if (PluginServices.getMainFrame() != null)
103
        {
104 20334 vcaballero
                double[] trans2Meter=MapContext.getDistanceTrans2Meter();
105 7771 caballero
            PluginServices.getMainFrame().getStatusBar().setMessage("4",
106 20334 vcaballero
                            "Dist:" + nf.format(dist/trans2Meter[mapCtrl.getViewPort().getDistanceUnits()]) + "");
107 7771 caballero
                    PluginServices.getMainFrame().getStatusBar().setMessage("5",
108 20334 vcaballero
                            "Total:" + nf.format(distAll/trans2Meter[mapCtrl.getViewPort().getDistanceUnits()]) + "");
109 7771 caballero
        }
110
        }
111
}