-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLatLongEntry.java
executable file
·223 lines (199 loc) · 7.24 KB
/
LatLongEntry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// LatLongEntry.java implements a widget to allow entry of a latitude and
// longitude location.
//-----------------------------------------------------------------------
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.GridLayout;
import java.awt.Point;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class LatLongEntry extends JPanel implements ActionListener, Observer
{
private FocusTextField latitude;
private FocusTextField longitude;
private JButton goButton;
private MosaicData md;
private imgViewer applet;
// Constructor for the LatLongEntry widget
//----------------------------------------
LatLongEntry(imgViewer applet, MosaicData md)
{
this.md = md;
this.applet = applet;
// use a box layout for the panel since it does a good job of sizing
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
// set the font to use for this component
setFont(applet.normalFont);
// build a panel for the label
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(2,1));
// create the "Lat/Long:" label on two lines
JLabel latLabel = new JLabel("Lat/");
latLabel.setFont(applet.boldFont);
labelPanel.add(latLabel);
JLabel longLabel = new JLabel("Long:");
longLabel.setFont(applet.boldFont);
labelPanel.add(longLabel);
add(labelPanel);
// create the latitude entry field
latitude = new FocusTextField(5);
latitude.setToolTipText("Latitude entry");
latitude.addActionListener(this);
add(latitude);
// create the longitude entry field
longitude = new FocusTextField(6);
longitude.setToolTipText("Longitude entry");
longitude.addActionListener(this);
add(longitude);
// create a go button to apply a new lat/long
goButton = new JButton("Go");
goButton.setToolTipText("Go to Lat/Long");
goButton.addActionListener(this);
add(goButton);
// set the size so that it won't grow in height (otherwise the box
// layout will let it grow too tall)
Dimension size = getPreferredSize();
size.width = 100;
setMinimumSize(size);
size.width = 240;
setMaximumSize(size);
}
// action handler for the "Go" Button and enter key action
//--------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
parseInput();
}
// Method for the Observer interface
//----------------------------------
public void update(Observable ob, Object arg)
{
// get the current lat/long location
Sensor currSensor = applet.sensorMenu.getCurrentSensor();
TOC cell = md.getCurrentCell();
LatLong latLong = null;
if ((currSensor.displaySceneCenterLatLong) && cell.valid)
{
// the current sensor should display the selected scene center
// in the entry box and the selected cell is valid, so calculate
// the lat/long of the scene center
// calculate the X/Y coordinates of the selected scene center
Metadata scene = cell.scenes[cell.currentDateIndex];
latLong = md.getLatLong(scene.centerXY);
}
else
{
// FIXME - this won't work well for MODIS tiles on the edge of
// the world
// display the grid cell lat/long in the entry box
ProjectionTransformation proj = md.getProjection();
Point coords = cell.getCenterProjCoords(proj);
latLong = proj.projToLatLong(coords.x,coords.y);
// FIXME
if (latLong == null)
{
latitude.setText("");
longitude.setText("");
return;
}
}
// convert the lat/long to a single digit after the decimal place
// and update the entry box
double temp = latLong.latitude * 10;
if (temp > 0)
temp += 0.5;
else
temp -= 0.5;
temp = (double)((int)temp) / 10.0;
latitude.setText("" + temp);
temp = latLong.longitude * 10;
if (temp > 0)
temp += 0.5;
else
temp -= 0.5;
temp = (double)((int)temp) / 10.0;
longitude.setText("" + temp);
}
// Method for the Event-Listeners
// as the "Go" button and "Enter Key"
//------------------------------------------
private void parseInput()
{
double lat; // latitude read from the entry field
double lon; // longitude read from the entry field
boolean doingLong = false; // flag to track what is being converted
try
{
// get the latitude, trim any leading or trailing spaces,
// and make sure it contains something
String temp = latitude.getText();
temp.trim();
if (temp.equals(""))
{
applet.statusBar.showStatus("Latitude value invalid");
return;
}
// convert the latitude to a double
Double d = new Double(temp);
lat = d.doubleValue();
// flag longitude is being converted for the catch statement
doingLong = true;
// get the longitude, trim any leading or trailing spaces,
// and make sure it contains something
temp = longitude.getText();
temp.trim();
if (temp.equals(""))
{
applet.statusBar.showStatus("Longitude value invalid");
return;
}
// convert the longitude to a double
d = new Double(temp);
lon = d.doubleValue();
// check the range on the latitude and longitude
if ((lat < -90.0) || (lat > 90.0))
{
applet.statusBar.showStatus("Latitude " + lat +
" out of range!");
latitude.setText("");
return;
}
else if ((lon < -180.0) || (lon > 180.0))
{
applet.statusBar.showStatus("Longitude " + lon +
" out of range!");
longitude.setText("");
return;
}
else
{
// jump to the new location
md.gotoLatLong(lat, lon);
}
}
catch (NumberFormatException e)
{
// error converting string to a Double, so display a message
// in the browser status bar
String badValue;
if (doingLong)
{
badValue = longitude.getText();
longitude.setText("");
}
else
{
badValue = latitude.getText();
latitude.setText("");
}
applet.statusBar.showStatus(
"Illegal lat/long value of \"" + badValue + "\"");
}
}
}