java: Bump java library versions for next release
[fw/altos] / altosuilib / AltosUIMap.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altosuilib_3;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.lang.Math;
25 import java.awt.geom.*;
26 import java.util.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_5.*;
29
30 public class AltosUIMap extends JComponent implements AltosFlightDisplay, AltosUIMapZoomListener {
31
32         static final int px_size = 512;
33
34         static final int maptype_hybrid = 0;
35         static final int maptype_roadmap = 1;
36         static final int maptype_satellite = 2;
37         static final int maptype_terrain = 3;
38         static final int maptype_default = maptype_hybrid;
39
40         static final String[] maptype_names = {
41                 "hybrid",
42                 "roadmap",
43                 "satellite",
44                 "terrain"
45         };
46
47         public static final String[] maptype_labels = {
48                 "Hybrid",
49                 "Roadmap",
50                 "Satellite",
51                 "Terrain"
52         };
53
54         public static final Color stateColors[] = {
55                 Color.WHITE,  // startup
56                 Color.WHITE,  // idle
57                 Color.WHITE,  // pad
58                 Color.RED,    // boost
59                 Color.PINK,   // fast
60                 Color.YELLOW, // coast
61                 Color.CYAN,   // drogue
62                 Color.BLUE,   // main
63                 Color.BLACK,  // landed
64                 Color.BLACK,  // invalid
65                 Color.CYAN,   // stateless
66         };
67
68         public void reset() {
69                 // nothing
70         }
71
72         public void font_size_changed(int font_size) {
73                 view.set_font();
74         }
75
76         public void units_changed(boolean imperial_units) {
77                 view.set_units();
78         }
79
80         JLabel  zoom_label;
81
82         private void set_zoom_label() {
83                 zoom_label.setText(String.format("Zoom %d", view.zoom() - view.default_zoom));
84         }
85
86         public void zoom_changed(int zoom) {
87                 set_zoom_label();
88         }
89
90         public void set_zoom(int zoom) {
91                 view.set_zoom(zoom);
92         }
93
94         public int get_zoom() {
95                 return view.zoom();
96         }
97
98         public void set_maptype(int type) {
99                 view.set_maptype(type);
100                 maptype_combo.setSelectedIndex(type);
101         }
102
103         public void show(AltosState state, AltosListenerState listener_state) {
104                 view.show(state, listener_state);
105         }
106
107         public void centre(double lat, double lon) {
108                 view.centre(lat, lon);
109         }
110
111         public void centre(AltosState state) {
112                 if (!state.gps.locked && state.gps.nsat < 4)
113                         return;
114                 centre(state.gps.lat, state.gps.lon);
115         }
116
117         public void add_mark(double lat, double lon, int state) {
118                 view.add_mark(lat, lon, state);
119         }
120
121         public void clear_marks() {
122                 view.clear_marks();
123         }
124
125         AltosUIMapView  view;
126
127         private GridBagLayout layout = new GridBagLayout();
128
129         JComboBox<String>       maptype_combo;
130
131         public void set_load_params(double lat, double lon, int radius, AltosUIMapTileListener listener) {
132                 view.set_load_params(lat, lon, radius, listener);
133         }
134
135         public boolean all_fetched() {
136                 return view.all_fetched();
137         }
138
139         public static void prefetch_maps(double lat, double lon) {
140         }
141
142         public String getName() {
143                 return "Map";
144         }
145
146         public AltosUIMap() {
147
148                 view = new AltosUIMapView();
149
150                 view.setPreferredSize(new Dimension(500,500));
151                 view.setVisible(true);
152                 view.setEnabled(true);
153                 view.add_zoom_listener(this);
154
155                 GridBagLayout   my_layout = new GridBagLayout();
156
157                 setLayout(my_layout);
158
159                 GridBagConstraints c = new GridBagConstraints();
160                 c.anchor = GridBagConstraints.CENTER;
161                 c.fill = GridBagConstraints.BOTH;
162                 c.gridx = 0;
163                 c.gridy = 0;
164                 c.gridwidth = 1;
165                 c.gridheight = 10;
166                 c.weightx = 1;
167                 c.weighty = 1;
168                 add(view, c);
169
170                 int     y = 0;
171
172                 zoom_label = new JLabel("", JLabel.CENTER);
173                 set_zoom_label();
174
175                 c = new GridBagConstraints();
176                 c.anchor = GridBagConstraints.CENTER;
177                 c.fill = GridBagConstraints.HORIZONTAL;
178                 c.gridx = 1;
179                 c.gridy = y++;
180                 c.weightx = 0;
181                 c.weighty = 0;
182                 add(zoom_label, c);
183
184                 JButton zoom_reset = new JButton("0");
185                 zoom_reset.addActionListener(new ActionListener() {
186                                 public void actionPerformed(ActionEvent e) {
187                                         set_zoom(view.default_zoom);
188                                 }
189                         });
190
191                 c = new GridBagConstraints();
192                 c.anchor = GridBagConstraints.CENTER;
193                 c.fill = GridBagConstraints.HORIZONTAL;
194                 c.gridx = 1;
195                 c.gridy = y++;
196                 c.weightx = 0;
197                 c.weighty = 0;
198                 add(zoom_reset, c);
199
200                 JButton zoom_in = new JButton("+");
201                 zoom_in.addActionListener(new ActionListener() {
202                                 public void actionPerformed(ActionEvent e) {
203                                         set_zoom(get_zoom() + 1);
204                                 }
205                         });
206
207                 c = new GridBagConstraints();
208                 c.anchor = GridBagConstraints.CENTER;
209                 c.fill = GridBagConstraints.HORIZONTAL;
210                 c.gridx = 1;
211                 c.gridy = y++;
212                 c.weightx = 0;
213                 c.weighty = 0;
214                 add(zoom_in, c);
215
216                 JButton zoom_out = new JButton("-");
217                 zoom_out.addActionListener(new ActionListener() {
218                                 public void actionPerformed(ActionEvent e) {
219                                         set_zoom(get_zoom() - 1);
220                                 }
221                         });
222                 c = new GridBagConstraints();
223                 c.anchor = GridBagConstraints.CENTER;
224                 c.fill = GridBagConstraints.HORIZONTAL;
225                 c.gridx = 1;
226                 c.gridy = y++;
227                 c.weightx = 0;
228                 c.weighty = 0;
229                 add(zoom_out, c);
230
231                 maptype_combo = new JComboBox<String>(maptype_labels);
232
233                 maptype_combo.setEditable(false);
234                 maptype_combo.setMaximumRowCount(maptype_combo.getItemCount());
235                 maptype_combo.addItemListener(new ItemListener() {
236                                 public void itemStateChanged(ItemEvent e) {
237                                         view.set_maptype(maptype_combo.getSelectedIndex());
238                                 }
239                         });
240
241                 c = new GridBagConstraints();
242                 c.anchor = GridBagConstraints.CENTER;
243                 c.fill = GridBagConstraints.HORIZONTAL;
244                 c.gridx = 1;
245                 c.gridy = y++;
246                 c.weightx = 0;
247                 c.weighty = 0;
248                 add(maptype_combo, c);
249         }
250 }