Statistics
| Revision:

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

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

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

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

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

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

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

    
163
                if (mF != null){
164

    
165
                        mF.getStatusBar().setMessage("units",
166
                            PluginServices.getText(this, mapControl.getMapContext().getDistanceNames()[mapControl.getMapContext().getViewPort().getDistanceUnits()]));
167
            // FJP: No se debe llamar a setControlValue desde aqu?, porque
168
            // cambia la escala, y con ella el viewPort (adem?s, de
169
            // la vista que no es).
170
            // mF.getStatusBar().setControlValue("scale",String.valueOf(mapControl.getMapContext().getScaleView()));
171
            // Fin
172
                        mF.getStatusBar().setMessage("projection", mapControl.getViewPort().getProjection().getAbrev());
173

    
174
                        String[] coords=getCoords(p);
175
                        mF.getStatusBar().setMessage("x", axisText[0] + coords[0]);
176
                        mF.getStatusBar().setMessage("y", axisText[1] + coords[1]);
177
                }
178
        }
179

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

    
207
        /**
208
         * <p>Gets the name of the coordinates:
209
         *  <ul>
210
         *   <li><i>Longitude</i> and <i>Latitude</i>, if the associated <i>MapControl</i> object isn't projected, or the current distance unit
211
         *    of the <code>MapControl</code>'s view port is in degrees.</li>
212
         *   <li><i>X</i> and <i>Y</i>, otherwise.</li>
213
         *  </ul>
214
         * </p>
215
         *
216
         * @param p array of at least two <code>String</code>, where text will be stored and returned
217
         *
218
         * @return text describing the coordinate value:
219
         *  <ul>
220
         *   <li>If isn't projected:
221
         *    <ul>
222
         *     <li><code>String[0]</code> : "Long = "</li>
223
         *     <li><code>String[1]</code> : "Lat = "</li>
224
         *    </ul>
225
         *   </li>
226
         *   <li>Otherwise:
227
         *    <ul>
228
         *     <li><code>String[0]</code> : "X = "</li>
229
         *     <li><code>String[1]</code> : "Y = "</li>
230
         *    </ul>
231
         *   </li>
232
         *  </ul>
233
         *
234
         * @version 050211
235
         * @author jmorell
236
         */
237
        public String[] setCoorDisplayText(String[] axisText) {
238
                IProjection iProj = mapControl.getMapContext().getProjection();
239
                if (!iProj.isProjected() || MapContext.getDistanceNames()[mapControl.getMapContext().getViewPort().getDistanceUnits()].equals("Grados")) {
240
                        axisText[0] = "Lon = ";
241
                        axisText[1] = "Lat = ";
242
                } else {
243
                        axisText[0] = "X = ";
244
                        axisText[1] = "Y = ";
245
                }
246
                return axisText;
247
        }
248

    
249
        /**
250
         * <p>Converts a decimal value (expected latitude or longitude) in degrees, and formats it according this pattern:<br>
251
         *  <code><b><i>S?G? M' S''</i></b></code>, having:<br>
252
         *  <ul>
253
         *   <li><i>S?</i> : optionally, if the value is negative, sets a "-" symbol.</li>
254
         *   <li><i>G</i> : equivalent grades.</li>
255
         *   <li><i>M</i> : equivalent minutes.</li>
256
         *   <li><i>S</i> : equivalent seconds.</li>
257
         *  </ul>
258
         * </p>
259
         *
260
         * @param d the latitude or longitude value to convert
261
         *
262
         * @return value formatted in degrees
263
         */
264
        private String formatDegrees(double d) {
265
                String signo = d<0 ? "-" : "";
266
                d = Math.abs(d);
267
                long grado = 0;
268
                double minuto = 0;
269
                double segundo = 0;
270

    
271
                grado = (long)(d);
272
                minuto = (d - grado) * 60;
273
                segundo = (minuto - (long) minuto)*60;
274
//                System.out.println("Grados: " + grado);
275
//                System.out.println("Minutos: " + minuto);
276
//                System.out.println("Segundos: " + segundo);
277
                return signo+grado+"? "+(long) minuto+"' "+(long)segundo+"''";
278
        }
279

    
280
        /**
281
         * <p>Returns the coordinates equivalent to <code>p</code>:
282
         *  <ul>
283
         *   <li>Uses <i><code>formatDegrees(p.get{X or Y}()</code></i> if the associated <code>MapControl</code> object isn't projected.</li>
284
         *   <li>Uses <i><code>formatDegrees({MapControl's projection}.toGeo(p.get{X or Y}())</code></i> if the associated
285
         *    <code>MapControl</code> object is projected and its <code>ViewPort</code>'s distance units are in degrees.</li>
286
         *   <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>
287
         *    otherwise.</li>
288
         *  </ul>
289
         * </p>
290
         *
291
         * @param p point 2D to convert in text coordinates according the projection of the associated <code>MapControl</code> and the
292
         *  distance units of its <code>ViewPort</code>.
293
         *
294
         * @return coordinates equivalent to <code>p</code>, according to the algorithm explained up
295
         */
296
        public String[] getCoords(Point2D p) {
297
                String[] coords=new String[2];
298
                IProjection iProj = mapControl.getMapContext().getProjection();
299
                if (!iProj.isProjected()) {
300
                        coords[0]=String.valueOf(formatDegrees(p.getX()));
301
                        coords[1]=String.valueOf(formatDegrees(p.getY()));
302
                } else {
303
                        double[] trans2Meter=MapContext.getDistanceTrans2Meter();
304
                        if (PluginServices.getText(this,MapContext.getDistanceNames()[mapControl.getViewPort().getDistanceUnits()]).equals(PluginServices.getText(this,"Grados"))) {
305
                                Point2D pgeo=iProj.toGeo(p);
306
                                coords[0]=String.valueOf(formatDegrees(pgeo.getX()));
307
                                coords[1]=String.valueOf(formatDegrees(pgeo.getY()));
308
                        }else {
309
                                if (PluginServices.getText(this,MapContext.getDistanceNames()[mapControl.getViewPort().getMapUnits()]).equals(PluginServices.getText(this,"Grados"))) {
310
                                        mapControl.getViewPort().setMapUnits(1);
311
                                }
312

    
313
                                coords[0]=String.valueOf(nf.format((p.getX()/trans2Meter[mapControl.getViewPort().getDistanceUnits()])*trans2Meter[mapControl.getViewPort().getMapUnits()]));
314
                                coords[1]=String.valueOf(nf.format((p.getY()/trans2Meter[mapControl.getViewPort().getDistanceUnits()])*trans2Meter[mapControl.getViewPort().getMapUnits()]));
315
                        }
316
                }
317
                return coords;
318
        }
319

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