62da413cd553dd4d6e911ed8e889aa0cf8829ce3
[fw/altos] / ao-tools / altosui / AltosIgniteUI.java
1 /*
2  * Copyright © 2010 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 altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30
31 public class AltosIgniteUI
32         extends JDialog
33         implements ActionListener
34 {
35         JFrame          owner;
36         JLabel          label;
37         JRadioButton    apogee;
38         JRadioButton    main;
39         JToggleButton   arm;
40         JButton         fire;
41         javax.swing.Timer       timer;
42
43         final static int        timeout = 1 * 1000;
44
45         int             time_remaining;
46
47         void set_arm_text() {
48                 if (arm.isSelected())
49                         arm.setText(String.format("%d", time_remaining));
50                 else
51                         arm.setText("Arm");
52         }
53
54         void start_timer() {
55                 time_remaining = 10;
56                 set_arm_text();
57                 timer.restart();
58         }
59
60         void stop_timer() {
61                 time_remaining = 0;
62                 arm.setSelected(false);
63                 arm.setEnabled(false);
64                 fire.setEnabled(false);
65                 timer.stop();
66                 set_arm_text();
67         }
68
69         void cancel () {
70                 apogee.setSelected(false);
71                 main.setSelected(false);
72                 fire.setEnabled(false);
73                 stop_timer();
74         }
75
76         void tick_timer() {
77                 --time_remaining;
78                 if (time_remaining <= 0)
79                         cancel();
80                 else
81                         set_arm_text();
82         }
83
84         void fire() {
85                 if (arm.isEnabled() && arm.isSelected() && time_remaining > 0) {
86                         int     igniter = AltosIgnite.None;
87                         if (apogee.isSelected() && !main.isSelected())
88                                 igniter = AltosIgnite.Apogee;
89                         else if (main.isSelected() && !apogee.isSelected())
90                                 igniter = AltosIgnite.Main;
91                         System.out.printf ("fire %d\n", igniter);
92                         cancel();
93                 }
94         }
95
96         public void actionPerformed(ActionEvent e) {
97                 String cmd = e.getActionCommand();
98                 if (cmd.equals("apogee") || cmd.equals("main")) {
99                         stop_timer();
100                         arm.setEnabled(true);
101                 }
102
103                 if (cmd.equals("apogee") && apogee.isSelected())
104                         main.setSelected(false);
105                 if (cmd.equals("main") && main.isSelected())
106                         apogee.setSelected(false);
107
108                 if (cmd.equals("arm")) {
109                         if (arm.isSelected()) {
110                                 fire.setEnabled(true);
111                                 start_timer();
112                         } else
113                                 cancel();
114                 }
115                 if (cmd.equals("fire"))
116                         fire();
117                 if (cmd.equals("tick"))
118                         tick_timer();
119         }
120
121         public AltosIgniteUI(JFrame in_owner) {
122                 Container               pane = getContentPane();
123                 GridBagConstraints      c = new GridBagConstraints();
124                 Insets                  i = new Insets(4,4,4,4);
125
126                 timer = new javax.swing.Timer(timeout, this);
127                 timer.setActionCommand("tick");
128
129                 owner = in_owner;
130
131                 pane.setLayout(new GridBagLayout());
132
133                 c.fill = GridBagConstraints.NONE;
134                 c.anchor = GridBagConstraints.CENTER;
135                 c.insets = i;
136                 c.weightx = 1;
137                 c.weighty = 1;
138
139                 c.gridx = 0;
140                 c.gridy = 0;
141                 c.gridwidth = 2;
142                 label = new JLabel ("Fire Igniter");
143                 pane.add(label, c);
144
145                 c.gridx = 0;
146                 c.gridy = 1;
147                 c.gridwidth = 1;
148                 apogee = new JRadioButton ("Apogee");
149                 pane.add(apogee, c);
150                 apogee.addActionListener(this);
151                 apogee.setActionCommand("apogee");
152
153                 c.gridx = 1;
154                 c.gridy = 1;
155                 c.gridwidth = 1;
156                 main = new JRadioButton ("Main");
157                 pane.add(main, c);
158                 main.addActionListener(this);
159                 main.setActionCommand("main");
160
161                 c.gridx = 0;
162                 c.gridy = 2;
163                 c.gridwidth = 1;
164                 arm = new JToggleButton ("Arm");
165                 pane.add(arm, c);
166                 arm.addActionListener(this);
167                 arm.setActionCommand("arm");
168                 arm.setEnabled(false);
169
170                 c.gridx = 1;
171                 c.gridy = 2;
172                 c.gridwidth = 1;
173                 fire = new JButton ("Fire");
174                 fire.setEnabled(false);
175                 pane.add(fire, c);
176                 fire.addActionListener(this);
177                 fire.setActionCommand("fire");
178
179                 pack();
180                 setLocationRelativeTo(owner);
181                 setVisible(true);
182         }
183 }