Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / StatusBarListener.java @ 20100

History | View | Annotate | Download (11.9 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
import com.iver.cit.gvsig.gui.layout.Attributes;
58

    
59

    
60
/**
61
 * <p>Listener that displays at the status bar of the application's main frame, the value of the point coordinates of the mouse's
62
 *  cursor on the associated <code>MapControl</code>, just as is received a <code>PointEvent</code> event.</p>
63
 *
64
 * <p>Calculates the coordinates equivalent to the point according this rules:
65
 *  <ul>
66
 *   <li>uses <i><code>formatDegrees(p.get{X or Y}()</code></i> if the associated <code>MapControl</code> object isn't projected.</li>
67
 *   <li>uses <i><code>formatDegrees({MapControl's projection}.toGeo(p.get{X or Y}())</code></i> if the associated
68
 *    <code>MapControl</code> object is projected and its <code>ViewPort</code>'s distance units are in degrees.</li>
69
 *   <li>uses <i><code>{NumberFormat according to {@link #setFractionDigits(Point2D) #setFractionDigits(Point2D)}}.format((p.get{X or Y}()/MapContext.CHANGEM[mapControl.getViewPort().getDistanceUnits()])*MapContext.CHANGEM[mapControl.getViewPort().getMapUnits()])</code></i>
70
 *    otherwise.</li>
71
 *  </ul>
72
 * </p>
73
 * 
74
 * <p>The <u>prefix</u> of the coordinate expressions will be:
75
 *  <ul>
76
 *   <li>Longitude "<i>Long =</i>" and latitude "<i>Lat =</i>", if the associated <i>MapControl</i> object isn't projected, or the current distance unit
77
 *    of the <code>MapControl</code>'s view port is in degrees.</li>
78
 *   <li>X "<i>X =</i>" and Y "<i>Y =</i>", otherwise.</li>
79
 *  </ul>
80
 * </p>
81
 * 
82
 * <p>And the <u>sufix</u> value:
83
 *  <ul>
84
 *   <li>If the associated <i>MapControl</i> object isn't projected, or the current distance unit
85
 *    of the <code>MapControl</code>'s view port is in degrees(expected latitude or longitude), according this pattern:<br>
86
 *    <code><b><i>S?G? M' S''</i></b></code>, having:<br>
87
 *    <ul>
88
 *     <li><i>S?</i> : optionally, if the value is negative, sets a "-" symbol.</li>
89
 *     <li><i>G</i> : equivalent grades.</li>
90
 *     <li><i>M</i> : equivalent minutes.</li>
91
 *     <li><i>S</i> : equivalent seconds.</li>
92
 *    </ul>
93
 *   </li>
94
 *   <li>Otherwise a decimal number according this rules:
95
 *    <ul>
96
 *     <li><i>8 decimals</i>, if is using any of the following geographic coordinate systems:
97
 *      <ul>
98
 *       <li><i>EPSG:4230 (known as <a href="http://en.wikipedia.org/wiki/ED50">ED50</a>)</i>.</li>
99
 *       <li><i>EPSG:4326 (known as <a href="http://en.wikipedia.org/wiki/WGS84">WGS84</a>)</i>.</li>
100
 *      </ul>
101
 *     <li><i>2 decimals</i>, otherwise.</li>
102
 *    </ul>
103
 *   </li>
104
 *  </ul>
105
 * </p>
106
 * 
107
 * @author Vicente Caballero Navarro
108
 */
109
public class StatusBarListener implements PointListener {
110
        /**
111
         * Reference to the <code>MapControl</code> object that uses.
112
         */
113
        private MapControl mapControl = null;
114

    
115
        /**
116
         * Format of the coordinates. Is used to set the number of decimals.
117
         */
118
        private NumberFormat nf = null;
119

    
120
        /**
121
         * <p>Creates a new <code>StatusBarListener</code> object.</p>
122
         * 
123
         * @param mc the <code>MapControl</code> where will be applied the changes
124
         */
125
        public StatusBarListener(MapControl mc) {
126
                mapControl = mc;
127
                nf = NumberFormat.getInstance();
128
                nf.setMaximumFractionDigits(2);
129
        }
130

    
131
        /*
132
         * (non-Javadoc)
133
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
134
         */
135
        public Cursor getCursor() {
136
                return null;
137
        }
138

    
139
        /*
140
         * (non-Javadoc)
141
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
142
         */
143
        public boolean cancelDrawing() {
144
                return false;
145
        }
146

    
147
        /*
148
         * 050211, jmorell: M?todo modificado para mejorar la manera de mostrar las
149
         * coordenadas geod?sicas en la barra de estado. Muestra Lat y Lon y aumenta
150
         * el n?mero de decimales para cuando trabajemos en coordenadas geod?sicas.
151
         * 
152
         * (non-Javadoc)
153
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
154
         */
155
        public void point(PointEvent event) throws BehaviorException {
156
                String[] axisText = new String[2];
157
                axisText[0] = "X = ";
158
                axisText[1] = "Y = ";
159
                Point2D p = mapControl.getMapContext().getViewPort().toMapPoint(event.getPoint());
160
                setFractionDigits(p);
161
                axisText = setCoorDisplayText(axisText);
162
                MainFrame mF = PluginServices.getMainFrame();
163

    
164
                if (mF != null)
165
                {
166
            mF.getStatusBar().setMessage("units",
167
                            PluginServices.getText(this, FConstant.NAMES[mapControl.getMapContext().getViewPort().getDistanceUnits()]));
168
            // FJP: No se debe llamar a setControlValue desde aqu?, porque
169
            // cambia la escala, y con ella el viewPort (adem?s, de
170
            // la vista que no es).
171
            // mF.getStatusBar().setControlValue("scale",String.valueOf(mapControl.getMapContext().getScaleView()));
172
            // Fin
173
                        mF.getStatusBar().setMessage("projection", mapControl.getViewPort().getProjection().getAbrev());
174
                        String[] coords=getCoords(p);
175
                        mF.getStatusBar().setMessage("x",
176
                                        axisText[0] + coords[0]);
177
                        mF.getStatusBar().setMessage("y",
178
                                        axisText[1] + coords[1]);
179
                }
180
        }
181

    
182
        /**
183
         * <p>Sets the number of decimals of the coordinates that will be displayed, according the current projection
184
         *  of the associated <code>MapControl</code>:
185
         *  <ul>
186
         *   <li><i>8 decimals</i>, if is using geographical coordinates:
187
         *    <ul>
188
         *     <li><i>EPSG:4230 (known as <a href="http://en.wikipedia.org/wiki/ED50">ED50</a>)</i>.</li>
189
         *     <li><i>EPSG:4326 (known as <a href="http://en.wikipedia.org/wiki/WGS84">WGS84</a>)</i>.</li>
190
         *    </ul>
191
         *   <li><i>2 decimals</i>, otherwise.</li>
192
         *  </ul>
193
         * </p>
194
         * 
195
         * @param p unused parameter
196
         * 
197
         * @version 050211
198
         * @author jmorell.
199
         */
200
        public void setFractionDigits(Point2D p) {
201
                IProjection iProj = mapControl.getMapContext().getProjection();
202
                if (iProj.getAbrev().equals("EPSG:4326") || iProj.getAbrev().equals("EPSG:4230")) {
203
                        nf.setMaximumFractionDigits(8);
204
                } else {
205
                        nf.setMaximumFractionDigits(2);
206
                }
207
        }
208

    
209
        /**
210
         * <p>Returns the coordinates equivalent to <code>p</code>:
211
         *  <ul>
212
         *   <li>Uses <i><code>formatDegrees(p.get{X or Y}()</code></i> if the associated <code>MapControl</code> object isn't projected.</li>
213
         *   <li>Uses <i><code>formatDegrees({MapControl's projection}.toGeo(p.get{X or Y}())</code></i> if the associated
214
         *    <code>MapControl</code> object is projected and its <code>ViewPort</code>'s distance units are in degrees.</li>
215
         *   <li>Uses <i><code>{NumberFormat according to {@link #setFractionDigits(Point2D) #setFractionDigits(Point2D)}}.format((p.get{X or Y}()/MapContext.CHANGEM[mapControl.getViewPort().getDistanceUnits()])*MapContext.CHANGEM[mapControl.getViewPort().getMapUnits()])</code></i>
216
         *    otherwise.</li>
217
         *  </ul>
218
         * </p>
219
         * 
220
         * @param p point 2D to convert in text coordinates according the projection of the associated <code>MapControl</code> and the
221
         *  distance units of its <code>ViewPort</code>.
222
         *
223
         * @return coordinates equivalent to <code>p</code>, according to the algorithm explained up
224
         */
225
        public String[] getCoords(Point2D p) {
226
                String[] coords=new String[2];
227
                IProjection iProj = mapControl.getMapContext().getProjection();
228
                if (!iProj.isProjected()) {
229
                        coords[0]=String.valueOf(formatDegrees(p.getX()));
230
                        coords[1]=String.valueOf(formatDegrees(p.getY()));
231
                } else {
232
                        if (mapControl.getViewPort().getDistanceUnits()==Attributes.NAMES.length-1) {
233
                                Point2D pgeo=iProj.toGeo(p);
234
                                coords[0]=String.valueOf(formatDegrees(pgeo.getX()));
235
                                coords[1]=String.valueOf(formatDegrees(pgeo.getY()));
236
                        }else {
237
                                coords[0]=String.valueOf(nf.format((p.getX()/MapContext.CHANGEM[mapControl.getViewPort().getDistanceUnits()])*MapContext.CHANGEM[mapControl.getViewPort().getMapUnits()]));
238
                                coords[1]=String.valueOf(nf.format((p.getY()/MapContext.CHANGEM[mapControl.getViewPort().getDistanceUnits()])*MapContext.CHANGEM[mapControl.getViewPort().getMapUnits()]));
239
                        }
240
                }
241
                return coords;
242
        }
243

    
244
        /**
245
         * <p>Gets the name of the coordinates:
246
         *  <ul>
247
         *   <li><i>Longitude</i> and <i>Latitude</i>, if the associated <i>MapControl</i> object isn't projected, or the current distance unit
248
         *    of the <code>MapControl</code>'s view port is in degrees.</li>
249
         *   <li><i>X</i> and <i>Y</i>, otherwise.</li>
250
         *  </ul>
251
         * </p>
252
         * 
253
         * @param p array of at least two <code>String</code>, where text will be stored and returned
254
         * 
255
         * @return text describing the coordinate value:
256
         *  <ul>
257
         *   <li>If isn't projected:
258
         *    <ul>
259
         *     <li><code>String[0]</code> : "Long = "</li>
260
         *     <li><code>String[1]</code> : "Lat = "</li>
261
         *    </ul>
262
         *   </li>
263
         *   <li>Otherwise:
264
         *    <ul>
265
         *     <li><code>String[0]</code> : "X = "</li>
266
         *     <li><code>String[1]</code> : "Y = "</li>
267
         *    </ul>
268
         *   </li>
269
         *  </ul>
270
         * 
271
         * @version 050211
272
         * @author jmorell
273
         */
274
        public String[] setCoorDisplayText(String[] axisText) {
275
                IProjection iProj = mapControl.getMapContext().getProjection();
276
                if (!iProj.isProjected()|| mapControl.getViewPort().getDistanceUnits()==Attributes.NAMES.length-1) {
277
                        axisText[0] = "Lon = ";
278
                        axisText[1] = "Lat = ";
279
                } else {
280
                        axisText[0] = "X = ";
281
                        axisText[1] = "Y = ";
282
                }
283
                return axisText;
284
        }
285

    
286
        /**
287
         * <p>Converts a decimal value (expected latitude or longitude) in degrees, and formats it according this pattern:<br>
288
         *  <code><b><i>S?G? M' S''</i></b></code>, having:<br>
289
         *  <ul>
290
         *   <li><i>S?</i> : optionally, if the value is negative, sets a "-" symbol.</li>
291
         *   <li><i>G</i> : equivalent grades.</li>
292
         *   <li><i>M</i> : equivalent minutes.</li>
293
         *   <li><i>S</i> : equivalent seconds.</li>
294
         *  </ul>
295
         * </p>
296
         * 
297
         * @param d the latitude or longitude value to convert
298
         * 
299
         * @return value formatted in degrees
300
         */
301
        private String formatDegrees(double d) {
302
                String signo = d<0 ? "-" : "";
303
                d = Math.abs(d);
304
                long grado = 0;
305
                double minuto = 0;
306
                double segundo = 0;
307

    
308
                grado = (long)(d);
309
                minuto = (d - grado) * 60;
310
                segundo = (minuto - (long) minuto)*60;
311
//                System.out.println("Grados: " + grado);
312
//                System.out.println("Minutos: " + minuto);
313
//                System.out.println("Segundos: " + segundo);
314
                return signo+grado+"? "+(long) minuto+"' "+(long)segundo+"''";
315
        }
316

    
317
        /*
318
         * (non-Javadoc)
319
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#pointDoubleClick(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
320
         */
321
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
322
                // TODO Auto-generated method stub
323

    
324
        }
325
}