2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altosuilib_7;
21 import java.awt.event.*;
26 import java.lang.Math;
28 import java.net.URLConnection;
29 import org.altusmetrum.altoslib_7.*;
31 class AltosUIMapPos extends Box {
40 public void set_value(double new_value) {
47 new_value = -new_value;
49 d = Math.floor(new_value);
50 deg.setText(String.format("%3.0f", d));
51 m = (new_value - d) * 60.0;
52 min.setText(String.format("%7.4f", m));
53 hemi.setSelectedIndex(h);
56 public double get_value() throws ParseException {
57 int h = hemi.getSelectedIndex();
58 String d_t = deg.getText();
59 String m_t = min.getText();
62 d = AltosParse.parse_double_locale(d_t);
63 } catch (ParseException pe) {
64 JOptionPane.showMessageDialog(owner,
65 String.format("Invalid degrees \"%s\"",
68 JOptionPane.ERROR_MESSAGE);
75 m = AltosParse.parse_double_locale(m_t);
76 } catch (ParseException pe) {
77 JOptionPane.showMessageDialog(owner,
78 String.format("Invalid minutes \"%s\"",
81 JOptionPane.ERROR_MESSAGE);
90 public AltosUIMapPos(AltosUIFrame in_owner,
93 double default_value) {
94 super(BoxLayout.X_AXIS);
96 label = new JLabel(label_value);
97 hemi = new JComboBox<String>(hemi_names);
98 hemi.setEditable(false);
99 deg = new JTextField(5);
100 deg.setMinimumSize(deg.getPreferredSize());
101 deg.setHorizontalAlignment(JTextField.RIGHT);
102 deg_label = new JLabel("°");
103 min = new JTextField(9);
104 min.setMinimumSize(min.getPreferredSize());
105 min_label = new JLabel("'");
106 set_value(default_value);
108 add(Box.createRigidArea(new Dimension(5, 0)));
110 add(Box.createRigidArea(new Dimension(5, 0)));
112 add(Box.createRigidArea(new Dimension(5, 0)));
114 add(Box.createRigidArea(new Dimension(5, 0)));
116 add(Box.createRigidArea(new Dimension(5, 0)));
121 public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener {
131 AltosMapLoader loader;
133 JLabel site_list_label;
134 JComboBox<AltosLaunchSite> site_list;
136 JToggleButton load_button;
138 JButton close_button;
140 JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1];
142 JComboBox<Integer> min_zoom;
143 JComboBox<Integer> max_zoom;
144 JComboBox<Integer> radius;
146 Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
147 Integer[] radii = { 1, 2, 3, 4, 5 };
149 static final String[] lat_hemi_names = { "N", "S" };
150 static final String[] lon_hemi_names = { "E", "W" };
152 double latitude, longitude;
154 /* AltosMapLoaderListener interfaces */
155 public void loader_start(final int max) {
156 SwingUtilities.invokeLater(new Runnable() {
158 pbar.setMaximum(max);
162 map.add_mark(latitude, longitude, AltosLib.ao_flight_boost);
167 public void loader_notify(final int cur, final int max, final String name) {
168 SwingUtilities.invokeLater(new Runnable() {
171 pbar.setString(name);
176 public void loader_done(int max) {
177 SwingUtilities.invokeLater(new Runnable() {
181 load_button.setSelected(false);
187 private int all_types() {
189 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
190 if (maptypes[t].isSelected())
191 all_types |= (1 << t);
195 public void itemStateChanged(ItemEvent e) {
196 int state = e.getStateChange();
198 if (state == ItemEvent.SELECTED) {
199 Object o = e.getItem();
200 if (o instanceof AltosLaunchSite) {
201 AltosLaunchSite site = (AltosLaunchSite) o;
202 lat.set_value(site.latitude);
203 lon.set_value(site.longitude);
208 public void actionPerformed(ActionEvent e) {
209 String cmd = e.getActionCommand();
211 if (cmd.equals("close"))
214 if (cmd.equals("load")) {
217 latitude = lat.get_value();
218 longitude = lon.get_value();
219 int min_z = (Integer) min_zoom.getSelectedItem();
220 int max_z = (Integer) max_zoom.getSelectedItem();
223 int r = (Integer) radius.getSelectedItem();
226 loader.load(latitude, longitude, min_z, max_z, r, all_types());
227 } catch (ParseException pe) {
228 load_button.setSelected(false);
234 public void notify_launch_sites(final java.util.List<AltosLaunchSite> sites) {
235 SwingUtilities.invokeLater(new Runnable() {
238 for (AltosLaunchSite site : sites) {
239 site_list.insertItemAt(site, i);
246 public AltosUIMapPreloadNew(AltosUIFrame in_owner) {
249 Container pane = getContentPane();
250 GridBagConstraints c = new GridBagConstraints();
251 Insets i = new Insets(4,4,4,4);
253 setTitle("AltOS Load Maps");
255 pane.setLayout(new GridBagLayout());
257 map = new AltosUIMapNew();
258 cache = new AltosMapCache(map);
260 loader = new AltosMapLoader(map.map, cache, this);
262 c.fill = GridBagConstraints.BOTH;
263 c.anchor = GridBagConstraints.CENTER;
271 c.anchor = GridBagConstraints.CENTER;
275 pbar = new JProgressBar();
280 pbar.setStringPainted(true);
282 c.fill = GridBagConstraints.HORIZONTAL;
283 c.anchor = GridBagConstraints.CENTER;
294 site_list_label = new JLabel ("Known Launch Sites:");
296 c.fill = GridBagConstraints.NONE;
297 c.anchor = GridBagConstraints.CENTER;
306 pane.add(site_list_label, c);
308 site_list = new JComboBox<AltosLaunchSite>(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) });
309 site_list.addItemListener(this);
311 new AltosLaunchSites(this);
313 c.fill = GridBagConstraints.HORIZONTAL;
314 c.anchor = GridBagConstraints.CENTER;
323 pane.add(site_list, c);
325 lat = new AltosUIMapPos(owner,
329 c.fill = GridBagConstraints.NONE;
330 c.anchor = GridBagConstraints.CENTER;
338 c.anchor = GridBagConstraints.CENTER;
342 lon = new AltosUIMapPos(owner,
347 c.fill = GridBagConstraints.NONE;
348 c.anchor = GridBagConstraints.CENTER;
356 c.anchor = GridBagConstraints.CENTER;
360 load_button = new JToggleButton("Load Map");
361 load_button.addActionListener(this);
362 load_button.setActionCommand("load");
364 c.fill = GridBagConstraints.NONE;
365 c.anchor = GridBagConstraints.CENTER;
373 c.anchor = GridBagConstraints.CENTER;
375 pane.add(load_button, c);
377 close_button = new JButton("Close");
378 close_button.addActionListener(this);
379 close_button.setActionCommand("close");
381 c.fill = GridBagConstraints.NONE;
382 c.anchor = GridBagConstraints.CENTER;
390 c.anchor = GridBagConstraints.CENTER;
392 pane.add(close_button, c);
394 JLabel types_label = new JLabel("Map Types");
398 pane.add(types_label, c);
402 for (int type = AltosMap.maptype_hybrid; type <= AltosMap.maptype_terrain; type++) {
403 maptypes[type] = new JCheckBox(AltosMap.maptype_labels[type],
404 type == AltosMap.maptype_hybrid);
405 c.gridx = 2 + (type >> 1);
406 c.fill = GridBagConstraints.HORIZONTAL;
407 c.gridy = (type & 1) + 3;
408 pane.add(maptypes[type], c);
411 JLabel min_zoom_label = new JLabel("Minimum Zoom");
414 pane.add(min_zoom_label, c);
416 min_zoom = new JComboBox<Integer>(zooms);
417 min_zoom.setSelectedItem(zooms[10]);
418 min_zoom.setEditable(false);
421 pane.add(min_zoom, c);
423 JLabel max_zoom_label = new JLabel("Maximum Zoom");
426 pane.add(max_zoom_label, c);
428 max_zoom = new JComboBox<Integer>(zooms);
429 max_zoom.setSelectedItem(zooms[14]);
430 max_zoom.setEditable(false);
433 pane.add(max_zoom, c);
435 JLabel radius_label = new JLabel("Tile Radius");
438 pane.add(radius_label, c);
440 radius = new JComboBox<Integer>(radii);
441 radius.setSelectedItem(radii[4]);
442 radius.setEditable(true);
448 setLocationRelativeTo(owner);