X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosuilib%2FAltosUIMapPreloadNew.java;h=d043e9e34bc7580c9685112762438f0558d2487a;hp=af196105f77f668f3a3daccd47f12a33f95f1344;hb=59a9bdd73b580a9c934a574be7bf45c5033e14b5;hpb=4895f443e4a748de2677e51869f20c05d265c944 diff --git a/altosuilib/AltosUIMapPreloadNew.java b/altosuilib/AltosUIMapPreloadNew.java index af196105..d043e9e3 100644 --- a/altosuilib/AltosUIMapPreloadNew.java +++ b/altosuilib/AltosUIMapPreloadNew.java @@ -15,7 +15,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altosuilib_7; +package org.altusmetrum.altosuilib_11; import java.awt.*; import java.awt.event.*; @@ -26,7 +26,7 @@ import java.text.*; import java.lang.Math; import java.net.URL; import java.net.URLConnection; -import org.altusmetrum.altoslib_7.*; +import org.altusmetrum.altoslib_11.*; class AltosUIMapPos extends Box { AltosUIFrame owner; @@ -118,251 +118,99 @@ class AltosUIMapPos extends Box { } } -class AltosUISite { - String name; - double latitude; - double longitude; - - public String toString() { - return name; - } - - public AltosUISite(String in_name, double in_latitude, double in_longitude) { - name = in_name; - latitude = in_latitude; - longitude = in_longitude; - } - - public AltosUISite(String line) throws ParseException { - String[] elements = line.split(":"); - - if (elements.length < 3) - throw new ParseException(String.format("Invalid site line %s", line), 0); - - name = elements[0]; - - try { - latitude = AltosParse.parse_double_net(elements[1]); - longitude = AltosParse.parse_double_net(elements[2]); - } catch (ParseException pe) { - throw new ParseException(String.format("Invalid site line %s", line), 0); - } - } -} - -class AltosUISites extends Thread { - AltosUIMapPreloadNew preload; - URL url; - LinkedList sites; - - void notify_complete() { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - preload.set_sites(); - } - }); - } - - void add(AltosUISite site) { - sites.add(site); - } - - void add(String line) { - try { - add(new AltosUISite(line)); - } catch (ParseException pe) { - System.out.printf("parse exception %s\n", pe.toString()); - } - } - - public void run() { - try { - URLConnection uc = url.openConnection(); - //int length = uc.getContentLength(); - - InputStreamReader in_stream = new InputStreamReader(uc.getInputStream(), AltosLib.unicode_set); - BufferedReader in = new BufferedReader(in_stream); - - for (;;) { - String line = in.readLine(); - if (line == null) - break; - add(line); - } - } catch (IOException e) { - } finally { - notify_complete(); - } - } - - public AltosUISites(AltosUIMapPreloadNew in_preload) { - sites = new LinkedList(); - preload = in_preload; - try { - url = new URL(AltosLib.launch_sites_url); - } catch (java.net.MalformedURLException e) { - notify_complete(); - } - start(); - } -} - -public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosMapTileListener { +public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener, ItemListener, AltosLaunchSiteListener, AltosMapLoaderListener, AltosUnitsListener, AltosFontListener { AltosUIFrame owner; AltosUIMapNew map; - AltosMapCache cache; AltosUIMapPos lat; AltosUIMapPos lon; JProgressBar pbar; - int pbar_max; - int pbar_cur; - AltosUISites sites; JLabel site_list_label; - JComboBox site_list; + JComboBox site_list; JToggleButton load_button; - boolean loading; JButton close_button; JCheckBox[] maptypes = new JCheckBox[AltosMap.maptype_terrain - AltosMap.maptype_hybrid + 1]; JComboBox min_zoom; JComboBox max_zoom; - JComboBox radius; + JLabel radius_label; + JComboBox radius; + int scale = 1; Integer[] zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 }; - Integer[] radii = { 1, 2, 3, 4, 5 }; + + Double[] radius_mi = { 1.0, 2.0, 5.0, 10.0, 20.0 }; + Double radius_def_mi = 5.0; + Double[] radius_km = { 2.0, 5.0, 10.0, 20.0, 30.0 }; + Double radius_def_km = 10.0; + + AltosMapLoader loader; static final String[] lat_hemi_names = { "N", "S" }; static final String[] lon_hemi_names = { "E", "W" }; - class updatePbar implements Runnable { - String s; + double latitude, longitude; - public updatePbar(String in_s) { - s = in_s; - } + long loader_notify_time; - public void run() { - int n = ++pbar_cur; + /* AltosMapLoaderListener interfaces */ + public void loader_start(final int max) { + loader_notify_time = System.currentTimeMillis(); - pbar.setMaximum(pbar_max); - pbar.setValue(n); - pbar.setString(s); - } + SwingUtilities.invokeLater(new Runnable() { + public void run() { + pbar.setMaximum(max); + pbar.setValue(0); + pbar.setString(""); + map.clear_marks(); + map.add_mark(latitude, longitude, AltosLib.ao_flight_boost); + } + }); } - double latitude, longitude; - int min_z; - int max_z; - int cur_z; - int all_types; - int cur_type; - int r; - - int tiles_per_layer; - int tiles_loaded; - int layers_total; - int layers_loaded; - - - private void do_load() { - tiles_loaded = 0; - map.set_zoom(cur_z + AltosMap.default_zoom); - map.set_maptype(cur_type); - map.set_load_params(latitude, longitude, r, this); - } + public void loader_notify(final int cur, final int max, final String name) { + long now = System.currentTimeMillis(); - private int next_type(int start) { - int next_type; - for (next_type = start; - next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0; - next_type++) - ; - return next_type; - } + if (now - loader_notify_time < 100) + return; - private void next_load() { - int next_type = next_type(cur_type + 1); + loader_notify_time = now; - if (next_type > AltosMap.maptype_terrain) { - if (cur_z == max_z) { - return; - } else { - cur_z++; - } - next_type = next_type(0); - } - cur_type = next_type; - do_load(); + SwingUtilities.invokeLater(new Runnable() { + public void run() { + pbar.setValue(cur); + pbar.setString(name); + } + }); } - private void start_load() { - cur_z = min_z; - int ntype = 0; - all_types = 0; - for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++) - if (maptypes[t].isSelected()) { - all_types |= (1 << t); - ntype++; - } - if (ntype == 0) { - all_types |= (1 << AltosMap.maptype_hybrid); - ntype = 1; - } - - cur_type = next_type(0); - tiles_per_layer = (r * 2 + 1) * (r * 2 + 1); - layers_total = (max_z - min_z + 1) * ntype; - layers_loaded = 0; - pbar_max = layers_total * tiles_per_layer; - pbar_cur = 0; - - map.clear_marks(); - map.add_mark(latitude,longitude, AltosLib.ao_flight_boost); - do_load(); + public void loader_done(int max) { + loader = null; + SwingUtilities.invokeLater(new Runnable() { + public void run() { + pbar.setValue(0); + pbar.setString(""); + load_button.setSelected(false); + } + }); } - /* AltosMapTileListener methods */ - - public synchronized void notify_tile(AltosMapTile tile, int status) { - if (status == AltosMapTile.loading) - return; - - SwingUtilities.invokeLater(new updatePbar(tile.store.file.toString())); - ++tiles_loaded; - if (tiles_loaded == tiles_per_layer) { - ++layers_loaded; - if (layers_loaded == layers_total) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - pbar.setValue(0); - pbar.setString(""); - load_button.setSelected(false); - loading = false; - } - }); - } else { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - next_load(); - } - }); - } - } + public void debug(String format, Object ... arguments) { + if (AltosSerial.debug) + System.out.printf(format, arguments); } - public AltosMapCache cache() { return cache; } - public void set_sites() { - int i = 1; - for (AltosUISite site : sites.sites) { - site_list.insertItemAt(site, i); - i++; - } + private int all_types() { + int all_types = 0; + for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++) + if (maptypes[t].isSelected()) + all_types |= (1 << t); + return all_types; } public void itemStateChanged(ItemEvent e) { @@ -370,8 +218,8 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener if (state == ItemEvent.SELECTED) { Object o = e.getItem(); - if (o instanceof AltosUISite) { - AltosUISite site = (AltosUISite) o; + if (o instanceof AltosLaunchSite) { + AltosLaunchSite site = (AltosLaunchSite) o; lat.set_value(site.latitude); lon.set_value(site.longitude); } @@ -381,28 +229,83 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); - if (cmd.equals("close")) + if (cmd.equals("close")) { + if (loader != null) + loader.abort(); setVisible(false); + } if (cmd.equals("load")) { - if (!loading) { + if (loader == null) { try { latitude = lat.get_value(); longitude = lon.get_value(); - min_z = (Integer) min_zoom.getSelectedItem(); - max_z = (Integer) max_zoom.getSelectedItem(); + int min_z = (Integer) min_zoom.getSelectedItem(); + int max_z = (Integer) max_zoom.getSelectedItem(); if (max_z < min_z) max_z = min_z; - r = (Integer) radius.getSelectedItem(); - loading = true; + Double r = (Double) radius.getSelectedItem(); + + if (AltosPreferences.imperial_units()) + r = AltosConvert.miles_to_meters(r); + else + r = r * 1000; + + map.map.centre(new AltosLatLon(latitude, longitude)); + + loader = new AltosMapLoader(this, + latitude, longitude, + min_z, max_z, r, + all_types(), scale); + } catch (ParseException pe) { load_button.setSelected(false); } - start_load(); } } } + public void notify_launch_sites(final java.util.List sites) { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + int i = 1; + for (AltosLaunchSite site : sites) { + site_list.insertItemAt(site, i); + i++; + } + } + }); + } + + private void set_radius_values() { + radius_label.setText(String.format("Map Radius (%s)", + AltosPreferences.imperial_units() ? "mi" : "km")); + + Double[] radii; + + if (AltosPreferences.imperial_units()) + radii = radius_mi; + else + radii = radius_km; + + radius.removeAllItems(); + for (Double r : radii) { + System.out.printf("adding radius %f\n",r); + radius.addItem(r); + } + radius.setSelectedItem(radii[2]); + radius.setMaximumRowCount(radii.length); + } + + public void units_changed(boolean imperial_units) { + map.units_changed(imperial_units); + set_radius_values(); + } + + public void font_size_changed(int font_size) { + map.font_size_changed(font_size); + } + public AltosUIMapPreloadNew(AltosUIFrame in_owner) { owner = in_owner; @@ -414,8 +317,19 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener pane.setLayout(new GridBagLayout()); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + AltosUIPreferences.unregister_font_listener(AltosUIMapPreloadNew.this); + AltosPreferences.unregister_units_listener(AltosUIMapPreloadNew.this); + } + }); + + + AltosPreferences.register_units_listener(this); + AltosUIPreferences.register_font_listener(this); + map = new AltosUIMapNew(); - cache = new AltosMapCache(map); c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.CENTER; @@ -463,10 +377,10 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener pane.add(site_list_label, c); - site_list = new JComboBox(new AltosUISite[] { new AltosUISite("Site List", 0, 0) }); + site_list = new JComboBox(new AltosLaunchSite[] { new AltosLaunchSite("Site List", 0, 0) }); site_list.addItemListener(this); - sites = new AltosUISites(this); + new AltosLaunchSites(this); c.fill = GridBagConstraints.HORIZONTAL; c.anchor = GridBagConstraints.CENTER; @@ -590,18 +504,20 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener c.gridy = 3; pane.add(max_zoom, c); - JLabel radius_label = new JLabel("Tile Radius"); + radius_label = new JLabel(); + c.gridx = 4; c.gridy = 4; pane.add(radius_label, c); - radius = new JComboBox(radii); - radius.setSelectedItem(radii[4]); + radius = new JComboBox(); radius.setEditable(true); c.gridx = 5; c.gridy = 4; pane.add(radius, c); + set_radius_values(); + pack(); setLocationRelativeTo(owner); setVisible(true);