893c3c447f8ef5a5712ae29170c9bf5d5232d4f5
[fw/altos] / altosuilib / AltosUITelemetryMenu.java
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
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.util.*;
21 import javax.swing.*;
22 import java.awt.event.*;
23 import org.altusmetrum.altoslib_5.*;
24
25 class TelemetryMenuItem extends JMenuItem {
26         public int      telemetry;
27
28         public TelemetryMenuItem (int telemetry) {
29                 super(AltosLib.telemetry_name(telemetry));
30                 this.telemetry = telemetry;
31         }
32 }
33
34 public class AltosUITelemetryMenu extends JMenu implements ActionListener {
35         TelemetryMenuItem       selected = null;
36
37         public int get_selected() {
38                 if (selected == null)
39                         return AltosLib.ao_telemetry_off;
40                 return selected.telemetry;
41         }
42
43         public void set_selected(int telemetry) {
44                 for (int i = 0; i < getItemCount(); i++) {
45                         TelemetryMenuItem       item = (TelemetryMenuItem) getItem(i);
46                         if (item.telemetry == telemetry) {
47                                 selected = item;
48                                 String new_text = String.format("Format: %s ▾", AltosLib.telemetry_name(telemetry));
49                                 setText(new_text);
50                                 break;
51                         }
52                 }
53         }
54
55         private LinkedList<ActionListener> action_listeners = new LinkedList<ActionListener>();
56
57         public void addActionListener(ActionListener l) {
58                 action_listeners.add(l);
59         }
60
61         public void removeActionListener(ActionListener l) {
62                 action_listeners.remove(l);
63         }
64
65         public void actionPerformed(ActionEvent e) {
66                 TelemetryMenuItem item = (TelemetryMenuItem) e.getSource();
67                 set_selected(item.telemetry);
68                 ActionEvent my_e = new ActionEvent(selected, 0, "selected");
69                 for (ActionListener l : action_listeners)
70                         l.actionPerformed(my_e);
71         }
72
73         public AltosUITelemetryMenu(int serial) {
74                 super();
75                 for (int i = AltosLib.ao_telemetry_min; i <= AltosLib.ao_telemetry_max; i++) {
76                         TelemetryMenuItem       item = new TelemetryMenuItem(i);
77
78                         item.addActionListener(this);
79                         add(item);
80                 }
81
82                 int telemetry = AltosPreferences.telemetry(serial);
83                 if (telemetry < AltosLib.ao_telemetry_min || AltosLib.ao_telemetry_max < telemetry)
84                         telemetry = AltosLib.ao_telemetry_standard;
85                 set_selected(telemetry);
86         }
87 }
88