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