Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toolListeners / MeasureListener.java @ 40558

History | View | Annotate | Download (3.51 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.app.project.documents.view.toolListeners;
25

    
26
import java.awt.geom.Point2D;
27
import java.text.NumberFormat;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.fmap.mapcontext.MapContext;
31
import org.gvsig.fmap.mapcontext.ViewPort;
32
import org.gvsig.fmap.mapcontrol.MapControl;
33
import org.gvsig.fmap.mapcontrol.tools.MeasureListenerImpl;
34
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent;
35

    
36

    
37

    
38
/**
39
 * <p>Listener for calculating distances using vertexes of a polyline, defined in the associated {@link MapControl MapControl}
40
 *  object, and displaying that information at the status bar of the application's main frame.</p>
41
 *
42
 * <p>Calculates and displays:
43
 *  <ul>
44
 *   <li>Distance of the last segment of the polyline.</li>
45
 *   <li>Accumulated distance of all segments of the polyline.</li>
46
 *  </ul>
47
 * </p>
48
 *
49
 * @see MeasureListenerImpl
50
 *
51
 * @author Vicente Caballero Navarro
52
 */
53
public class MeasureListener extends MeasureListenerImpl {
54
        /**
55
         * <p>Creates a new listener for calculating distances using points of a polyline.</p>
56
         *
57
         * @param mc the <code>MapControl</code> where is calculated the length
58
         */
59
        public MeasureListener(MapControl mc) {
60
                super(mc);
61
        }
62

    
63
        /*
64
         * (non-Javadoc)
65
         * @see com.iver.cit.gvsig.fmap.tools.MeasureListenerImpl#points(com.iver.cit.gvsig.fmap.tools.Events.MeasureEvent)
66
         */
67
        public void points(MeasureEvent event) {
68
                double dist = 0;
69
                double distAll = 0;
70

    
71
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
72

    
73
                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()));
74
                for (int i = 1; i < (event.getXs().length); i++) {
75
                        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()));
76
                        dist = vp.distanceWorld(p, p2);
77
                        distAll += dist;
78
                        p = p2;
79
                }
80

    
81
                //System.out.println("Distancia = " + dist + " Distancia Total = " +
82
                //        (distAll));
83
                NumberFormat nf = NumberFormat.getInstance();
84
        nf.setMaximumFractionDigits(2);
85
        if (PluginServices.getMainFrame() != null)
86
        {
87
                double[] trans2Meter=MapContext.getDistanceTrans2Meter();
88
            PluginServices.getMainFrame().getStatusBar().setMessage("4",
89
                            "Dist:" + nf.format(dist/trans2Meter[mapCtrl.getViewPort().getDistanceUnits()]) + "");
90
                    PluginServices.getMainFrame().getStatusBar().setMessage("5",
91
                            "Total:" + nf.format(distAll/trans2Meter[mapCtrl.getViewPort().getDistanceUnits()]) + "");
92
        }
93
        }
94
}