altosui: Deliver firmware for Tmega 2.0 and TBT 3.0 for Windows too
[fw/altos] / altosui / AltosConfigTDUI.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.event.*;
24 import org.altusmetrum.altoslib_11.*;
25 import org.altusmetrum.altosuilib_11.*;
26
27 public class AltosConfigTDUI
28         extends AltosUIDialog
29         implements ActionListener, ItemListener, DocumentListener
30 {
31
32         Container       pane;
33         Box             box;
34         JLabel          product_label;
35         JLabel          version_label;
36         JLabel          serial_label;
37         JLabel          frequency_label;
38         JLabel          radio_calibration_label;
39         JLabel          radio_frequency_label;
40         JLabel          rate_label;
41
42         public boolean          dirty;
43
44         JFrame          owner;
45         JLabel          product_value;
46         JLabel          version_value;
47         JLabel          serial_value;
48         AltosUIFreqList radio_frequency_value;
49         JLabel          radio_calibration_value;
50         AltosUIRateList rate_value;
51
52         JButton         save;
53         JButton         reset;
54         JButton         reboot;
55         JButton         close;
56
57         ActionListener  listener;
58
59         /* A window listener to catch closing events and tell the config code */
60         class ConfigListener extends WindowAdapter {
61                 AltosConfigTDUI ui;
62
63                 public ConfigListener(AltosConfigTDUI this_ui) {
64                         ui = this_ui;
65                 }
66
67                 public void windowClosing(WindowEvent e) {
68                         ui.actionPerformed(new ActionEvent(e.getSource(),
69                                                            ActionEvent.ACTION_PERFORMED,
70                                                            "Close"));
71                 }
72         }
73
74         /* Build the UI using a grid bag */
75         public AltosConfigTDUI(JFrame in_owner) {
76                 super (in_owner, "Configure TeleDongle", false);
77
78                 owner = in_owner;
79                 GridBagConstraints c;
80
81                 Insets il = new Insets(4,4,4,4);
82                 Insets ir = new Insets(4,4,4,4);
83
84                 pane = getContentPane();
85                 pane.setLayout(new GridBagLayout());
86
87                 /* Product */
88                 c = new GridBagConstraints();
89                 c.gridx = 0; c.gridy = 0;
90                 c.gridwidth = 4;
91                 c.fill = GridBagConstraints.NONE;
92                 c.anchor = GridBagConstraints.LINE_START;
93                 c.insets = il;
94                 product_label = new JLabel("Product:");
95                 pane.add(product_label, c);
96
97                 c = new GridBagConstraints();
98                 c.gridx = 4; c.gridy = 0;
99                 c.gridwidth = 4;
100                 c.fill = GridBagConstraints.HORIZONTAL;
101                 c.weightx = 1;
102                 c.anchor = GridBagConstraints.LINE_START;
103                 c.insets = ir;
104                 product_value = new JLabel("");
105                 pane.add(product_value, c);
106
107                 /* Version */
108                 c = new GridBagConstraints();
109                 c.gridx = 0; c.gridy = 1;
110                 c.gridwidth = 4;
111                 c.fill = GridBagConstraints.NONE;
112                 c.anchor = GridBagConstraints.LINE_START;
113                 c.insets = il;
114                 c.ipady = 5;
115                 version_label = new JLabel("Software version:");
116                 pane.add(version_label, c);
117
118                 c = new GridBagConstraints();
119                 c.gridx = 4; c.gridy = 1;
120                 c.gridwidth = 4;
121                 c.fill = GridBagConstraints.HORIZONTAL;
122                 c.weightx = 1;
123                 c.anchor = GridBagConstraints.LINE_START;
124                 c.insets = ir;
125                 c.ipady = 5;
126                 version_value = new JLabel("");
127                 pane.add(version_value, c);
128
129                 /* Serial */
130                 c = new GridBagConstraints();
131                 c.gridx = 0; c.gridy = 2;
132                 c.gridwidth = 4;
133                 c.fill = GridBagConstraints.NONE;
134                 c.anchor = GridBagConstraints.LINE_START;
135                 c.insets = il;
136                 c.ipady = 5;
137                 serial_label = new JLabel("Serial:");
138                 pane.add(serial_label, c);
139
140                 c = new GridBagConstraints();
141                 c.gridx = 4; c.gridy = 2;
142                 c.gridwidth = 4;
143                 c.fill = GridBagConstraints.HORIZONTAL;
144                 c.weightx = 1;
145                 c.anchor = GridBagConstraints.LINE_START;
146                 c.insets = ir;
147                 c.ipady = 5;
148                 serial_value = new JLabel("");
149                 pane.add(serial_value, c);
150
151                 /* Frequency */
152                 c = new GridBagConstraints();
153                 c.gridx = 0; c.gridy = 5;
154                 c.gridwidth = 4;
155                 c.fill = GridBagConstraints.NONE;
156                 c.anchor = GridBagConstraints.LINE_START;
157                 c.insets = il;
158                 c.ipady = 5;
159                 radio_frequency_label = new JLabel("Frequency:");
160                 pane.add(radio_frequency_label, c);
161
162                 c = new GridBagConstraints();
163                 c.gridx = 4; c.gridy = 5;
164                 c.gridwidth = 4;
165                 c.fill = GridBagConstraints.HORIZONTAL;
166                 c.weightx = 1;
167                 c.anchor = GridBagConstraints.LINE_START;
168                 c.insets = ir;
169                 c.ipady = 5;
170                 radio_frequency_value = new AltosUIFreqList();
171                 radio_frequency_value.addItemListener(this);
172                 pane.add(radio_frequency_value, c);
173                 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
174
175                 /* Radio Calibration */
176                 c = new GridBagConstraints();
177                 c.gridx = 0; c.gridy = 6;
178                 c.gridwidth = 4;
179                 c.fill = GridBagConstraints.NONE;
180                 c.anchor = GridBagConstraints.LINE_START;
181                 c.insets = il;
182                 c.ipady = 5;
183                 radio_calibration_label = new JLabel("RF Calibration:");
184                 pane.add(radio_calibration_label, c);
185
186                 c = new GridBagConstraints();
187                 c.gridx = 4; c.gridy = 6;
188                 c.gridwidth = 4;
189                 c.fill = GridBagConstraints.HORIZONTAL;
190                 c.weightx = 1;
191                 c.anchor = GridBagConstraints.LINE_START;
192                 c.insets = ir;
193                 c.ipady = 5;
194                 radio_calibration_value = new JLabel(String.format("%d", 1186611));
195                 pane.add(radio_calibration_value, c);
196
197                 /* Telemetry Rate */
198                 c = new GridBagConstraints();
199                 c.gridx = 0; c.gridy = 7;
200                 c.gridwidth = 4;
201                 c.fill = GridBagConstraints.NONE;
202                 c.anchor = GridBagConstraints.LINE_START;
203                 c.insets = il;
204                 c.ipady = 5;
205                 rate_label = new JLabel("Telemetry Rate:");
206                 pane.add(rate_label, c);
207
208                 c = new GridBagConstraints();
209                 c.gridx = 4; c.gridy = 7;
210                 c.gridwidth = 4;
211                 c.fill = GridBagConstraints.HORIZONTAL;
212                 c.weightx = 1;
213                 c.anchor = GridBagConstraints.LINE_START;
214                 c.insets = ir;
215                 c.ipady = 5;
216                 rate_value = new AltosUIRateList();
217                 pane.add(rate_value, c);
218
219                 /* Buttons */
220                 c = new GridBagConstraints();
221                 c.gridx = 0; c.gridy = 12;
222                 c.gridwidth = 2;
223                 c.fill = GridBagConstraints.NONE;
224                 c.anchor = GridBagConstraints.LINE_START;
225                 c.insets = il;
226                 save = new JButton("Save");
227                 pane.add(save, c);
228                 save.addActionListener(this);
229                 save.setActionCommand("Save");
230
231                 c = new GridBagConstraints();
232                 c.gridx = 2; c.gridy = 12;
233                 c.gridwidth = 2;
234                 c.fill = GridBagConstraints.NONE;
235                 c.anchor = GridBagConstraints.CENTER;
236                 c.insets = il;
237                 reset = new JButton("Reset");
238                 pane.add(reset, c);
239                 reset.addActionListener(this);
240                 reset.setActionCommand("Reset");
241
242                 c = new GridBagConstraints();
243                 c.gridx = 6; c.gridy = 12;
244                 c.gridwidth = 2;
245                 c.fill = GridBagConstraints.NONE;
246                 c.anchor = GridBagConstraints.LINE_END;
247                 c.insets = il;
248                 close = new JButton("Close");
249                 pane.add(close, c);
250                 close.addActionListener(this);
251                 close.setActionCommand("Close");
252
253                 addWindowListener(new ConfigListener(this));
254         }
255
256         /* Once the initial values are set, the config code will show the dialog */
257         public void make_visible() {
258                 pack();
259                 setLocationRelativeTo(owner);
260                 setVisible(true);
261         }
262
263         /* If any values have been changed, confirm before closing */
264         public boolean check_dirty(String operation) {
265                 if (dirty) {
266                         Object[] options = { String.format("%s anyway", operation), "Keep editing" };
267                         int i;
268                         i = JOptionPane.showOptionDialog(this,
269                                                          String.format("Configuration modified. %s anyway?", operation),
270                                                          "Configuration Modified",
271                                                          JOptionPane.DEFAULT_OPTION,
272                                                          JOptionPane.WARNING_MESSAGE,
273                                                          null, options, options[1]);
274                         if (i != 0)
275                                 return false;
276                 }
277                 return true;
278         }
279
280         /* Listen for events from our buttons */
281         public void actionPerformed(ActionEvent e) {
282                 String  cmd = e.getActionCommand();
283
284                 if (cmd.equals("Close") || cmd.equals("Reboot"))
285                         if (!check_dirty(cmd))
286                                 return;
287                 listener.actionPerformed(e);
288                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
289                         setVisible(false);
290                         dispose();
291                 }
292                 dirty = false;
293         }
294
295         /* ItemListener interface method */
296         public void itemStateChanged(ItemEvent e) {
297                 dirty = true;
298         }
299
300         /* DocumentListener interface methods */
301         public void changedUpdate(DocumentEvent e) {
302                 dirty = true;
303         }
304
305         public void insertUpdate(DocumentEvent e) {
306                 dirty = true;
307         }
308
309         public void removeUpdate(DocumentEvent e) {
310                 dirty = true;
311         }
312
313         /* Let the config code hook on a listener */
314         public void addActionListener(ActionListener l) {
315                 listener = l;
316         }
317
318         /* set and get all of the dialog values */
319         public void set_product(String product) {
320                 radio_frequency_value.set_product(product);
321                 product_value.setText(product);
322         }
323
324         public void set_version(String version) {
325                 version_value.setText(version);
326         }
327
328         public void set_serial(int serial) {
329                 radio_frequency_value.set_serial(serial);
330                 serial_value.setText(String.format("%d", serial));
331         }
332
333         public void set_radio_frequency(double new_radio_frequency) {
334                 int i;
335                 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
336                         AltosFrequency  f = (AltosFrequency) radio_frequency_value.getItemAt(i);
337
338                         if (f.close(new_radio_frequency)) {
339                                 radio_frequency_value.setSelectedIndex(i);
340                                 return;
341                         }
342                 }
343                 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
344                         AltosFrequency  f = (AltosFrequency) radio_frequency_value.getItemAt(i);
345
346                         if (new_radio_frequency < f.frequency)
347                                 break;
348                 }
349                 String  description = String.format("%s serial %s",
350                                                     product_value.getText(),
351                                                     serial_value.getText());
352                 AltosFrequency  new_frequency = new AltosFrequency(new_radio_frequency, description);
353                 AltosPreferences.add_common_frequency(new_frequency);
354                 radio_frequency_value.insertItemAt(new_frequency, i);
355                 radio_frequency_value.setSelectedIndex(i);
356         }
357
358         public double radio_frequency() {
359                 return radio_frequency_value.frequency();
360         }
361
362         public void set_radio_calibration(int calibration) {
363                 radio_calibration_value.setText(String.format("%d", calibration));
364         }
365
366         public int telemetry_rate() {
367                 return rate_value.getSelectedIndex();
368         }
369
370         public void set_telemetry_rate(int rate) {
371                 rate_value.setSelectedIndex(rate);
372         }
373
374         public void set_clean() {
375                 dirty = false;
376         }
377 }