Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / StatusBarListener.java @ 24759

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.Image;
44
import java.awt.geom.Point2D;
45
import java.text.NumberFormat;
46

    
47
import org.cresques.cts.IProjection;
48
import org.gvsig.fmap.mapcontext.MapContext;
49
import org.gvsig.fmap.mapcontrol.MapControl;
50
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
51
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
52
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
53

    
54
import com.iver.andami.PluginServices;
55
import com.iver.andami.ui.mdiFrame.MainFrame;
56

    
57

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

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

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

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

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

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

    
162
                if (mF != null){
163

    
164
                        mF.getStatusBar().setMessage("units",
165
                            PluginServices.getText(this, MapContext.getDistanceNames()[mapControl.getMapContext().getViewPort().getDistanceUnits()]));
166

    
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.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
}