Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / StatusBarListener.java @ 12043

History | View | Annotate | Download (6.39 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.project.documents.view.toolListeners;
42

    
43
import java.awt.Cursor;
44
import java.awt.geom.Point2D;
45
import java.text.NumberFormat;
46

    
47
import org.cresques.cts.IProjection;
48

    
49
import com.iver.andami.PluginServices;
50
import com.iver.andami.ui.mdiFrame.MainFrame;
51
import com.iver.cit.gvsig.fmap.MapContext;
52
import com.iver.cit.gvsig.fmap.MapControl;
53
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
54
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
55
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
56
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
57

    
58

    
59
/**
60
 * DOCUMENT ME!
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class StatusBarListener implements PointListener {
65
        private MapControl mapControl = null;
66
        private NumberFormat nf = null;
67

    
68
        /**
69
         * Crea un nuevo StatusBarListener.
70
         *
71
         * @param mc DOCUMENT ME!
72
         */
73
        public StatusBarListener(MapControl mc) {
74
                mapControl = mc;
75
                nf = NumberFormat.getInstance();
76
                nf.setMaximumFractionDigits(2);
77
        }
78

    
79
        /**
80
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
81
         */
82
        public Cursor getCursor() {
83
                return null;
84
        }
85

    
86
        /**
87
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
88
         */
89
        public boolean cancelDrawing() {
90
                return false;
91
        }
92

    
93
        /**
94
         * 050211, jmorell: M?todo modificado para mejorar la manera de mostrar las
95
         * coordenadas geod?sicas en la barra de estado. Muestra Lat y Lon y aumenta
96
         * el n?mero de decimales para cuando trabajemos en coordenadas geod?sicas.
97
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
98
         */
99
        public void point(PointEvent event) throws BehaviorException {
100
                String[] axisText = new String[2];
101
                axisText[0] = "X = ";
102
                axisText[1] = "Y = ";
103
                Point2D p = mapControl.getMapContext().getViewPort().toMapPoint(event.getPoint());
104
                setFractionDigits(p);
105
                axisText = setCoorDisplayText(axisText);
106
                MainFrame mF = PluginServices.getMainFrame();
107

    
108
                if (mF != null){
109

    
110
                        mF.getStatusBar().setMessage("units",
111
                            PluginServices.getText(this, FConstant.NAMES[mapControl.getMapContext().getViewPort().getDistanceUnits()]));
112
            // FJP: No se debe llamar a setControlValue desde aqu?, porque
113
            // cambia la escala, y con ella el viewPort (adem?s, de
114
            // la vista que no es).
115
            // mF.getStatusBar().setControlValue("scale",String.valueOf(mapControl.getMapContext().getScaleView()));
116
            // Fin
117
                        mF.getStatusBar().setMessage("projection", mapControl.getViewPort().getProjection().getAbrev());
118

    
119
                        String[] coords=getCoords(p);
120
                        mF.getStatusBar().setMessage("x", axisText[0] + coords[0]);
121
                        mF.getStatusBar().setMessage("y", axisText[1] + coords[1]);
122
                }
123
        }
124

    
125
        /**
126
         * Aumenta el n?mero de decimales visibles cuando trabajamos con coordeandas
127
         * geod?sicas. En concreto se a?aden 8 decimales, con lo que logramos una
128
         * definici?n subm?trica.
129
         * 050211, jmorell.
130
         * @param p
131
         */
132
        public void setFractionDigits(Point2D p) {
133
                IProjection iProj = mapControl.getMapContext().getProjection();
134
                if (!iProj.isProjected()) {
135
                        nf.setMaximumFractionDigits(8);
136
                } else {
137
                        nf.setMaximumFractionDigits(2);
138
                }
139
        }
140

    
141
        /**
142
         * Cambia X e Y por Lat y Lon enb la barra de estado cuando trabajamos con
143
         * coordenadas geod?sicas.
144
         * 050211, jmorell.
145
         * @param p
146
         */
147
        public String[] setCoorDisplayText(String[] axisText) {
148
                IProjection iProj = mapControl.getMapContext().getProjection();
149
                if (!iProj.isProjected() || mapControl.getViewPort().getDistanceUnits()==MapContext.NAMES.length-1) {
150
                        axisText[0] = "Lon = ";
151
                        axisText[1] = "Lat = ";
152
                } else {
153
                        axisText[0] = "X = ";
154
                        axisText[1] = "Y = ";
155
                }
156
                return axisText;
157
        }
158
        private String formatDegrees(double d) {
159
                String signo = d<0 ? "-" : "";
160
                d = Math.abs(d);
161
                long grado = 0;
162
                double minuto = 0;
163
                double segundo = 0;
164

    
165
                grado = (long)(d);
166
                minuto = (d - grado) * 60;
167
                segundo = (minuto - (long) minuto)*60;
168
//                System.out.println("Grados: " + grado);
169
//                System.out.println("Minutos: " + minuto);
170
//                System.out.println("Segundos: " + segundo);
171
                return signo+grado+"? "+(long) minuto+"' "+(long)segundo+"''";
172
        }
173

    
174
        public String[] getCoords(Point2D p) {
175
                String[] coords=new String[2];
176
                IProjection iProj = mapControl.getMapContext().getProjection();
177
                if (!iProj.isProjected()) {
178
                        coords[0]=String.valueOf(formatDegrees(p.getX()));
179
                        coords[1]=String.valueOf(formatDegrees(p.getY()));
180
                } else {
181
                        if (mapControl.getViewPort().getDistanceUnits()==MapContext.NAMES.length-1) {
182
                                Point2D pgeo=iProj.toGeo(p);
183
                                coords[0]=String.valueOf(formatDegrees(pgeo.getX()));
184
                                coords[1]=String.valueOf(formatDegrees(pgeo.getY()));
185
                        }else {
186
                                if (mapControl.getViewPort().getMapUnits()==MapContext.NAMES.length) {
187
                                        mapControl.getViewPort().setMapUnits(1);
188
                                }
189
                                coords[0]=String.valueOf(nf.format((p.getX()/MapContext.CHANGEM[mapControl.getViewPort().getDistanceUnits()])*MapContext.CHANGEM[mapControl.getViewPort().getMapUnits()]));
190
                                coords[1]=String.valueOf(nf.format((p.getY()/MapContext.CHANGEM[mapControl.getViewPort().getDistanceUnits()])*MapContext.CHANGEM[mapControl.getViewPort().getMapUnits()]));
191
                        }
192
                }
193
                return coords;
194
        }
195
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
196
                // TODO Auto-generated method stub
197

    
198
        }
199

    
200
}