ca3657183e037f339cd46ed8e1a69cebc29484c0
[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_5.*;
25 import org.altusmetrum.altosuilib_3.*;
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
41         public boolean          dirty;
42
43         JFrame          owner;
44         JLabel          product_value;
45         JLabel          version_value;
46         JLabel          serial_value;
47         JMenuBar        radio_frequency_menu_bar;
48         AltosFreqList   radio_frequency_value;
49         JLabel          radio_calibration_value;
50
51         JButton         save;
52         JButton         reset;
53         JButton         reboot;
54         JButton         close;
55
56         ActionListener  listener;
57
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 AltosFreqList(false);
171                 radio_frequency_value.addItemListener(this);
172                 radio_frequency_menu_bar = new JMenuBar();
173                 radio_frequency_menu_bar.add(radio_frequency_value);
174                 pane.add(radio_frequency_menu_bar, c);
175                 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
176
177                 /* Radio Calibration */
178                 c = new GridBagConstraints();
179                 c.gridx = 0; c.gridy = 6;
180                 c.gridwidth = 4;
181                 c.fill = GridBagConstraints.NONE;
182                 c.anchor = GridBagConstraints.LINE_START;
183                 c.insets = il;
184                 c.ipady = 5;
185                 radio_calibration_label = new JLabel("RF Calibration:");
186                 pane.add(radio_calibration_label, c);
187
188                 c = new GridBagConstraints();
189                 c.gridx = 4; c.gridy = 6;
190                 c.gridwidth = 4;
191                 c.fill = GridBagConstraints.HORIZONTAL;
192                 c.weightx = 1;
193                 c.anchor = GridBagConstraints.LINE_START;
194                 c.insets = ir;
195                 c.ipady = 5;
196                 radio_calibration_value = new JLabel(String.format("%d", 1186611));
197                 pane.add(radio_calibration_value, c);
198
199                 /* Buttons */
200                 c = new GridBagConstraints();
201                 c.gridx = 0; c.gridy = 12;
202                 c.gridwidth = 2;
203                 c.fill = GridBagConstraints.NONE;
204                 c.anchor = GridBagConstraints.LINE_START;
205                 c.insets = il;
206                 save = new JButton("Save");
207                 pane.add(save, c);
208                 save.addActionListener(this);
209                 save.setActionCommand("Save");
210
211                 c = new GridBagConstraints();
212                 c.gridx = 2; c.gridy = 12;
213                 c.gridwidth = 2;
214                 c.fill = GridBagConstraints.NONE;
215                 c.anchor = GridBagConstraints.CENTER;
216                 c.insets = il;
217                 reset = new JButton("Reset");
218                 pane.add(reset, c);
219                 reset.addActionListener(this);
220                 reset.setActionCommand("Reset");
221
222                 c = new GridBagConstraints();
223                 c.gridx = 6; c.gridy = 12;
224                 c.gridwidth = 2;
225                 c.fill = GridBagConstraints.NONE;
226                 c.anchor = GridBagConstraints.LINE_END;
227                 c.insets = il;
228                 close = new JButton("Close");
229                 pane.add(close, c);
230                 close.addActionListener(this);
231                 close.setActionCommand("Close");
232
233                 addWindowListener(new ConfigListener(this));
234         }
235
236         /* Once the initial values are set, the config code will show the dialog */
237         public void make_visible() {
238                 pack();
239                 setLocationRelativeTo(owner);
240                 setVisible(true);
241         }
242
243         /* If any values have been changed, confirm before closing */
244         public boolean check_dirty(String operation) {
245                 if (dirty) {
246                         Object[] options = { String.format("%s anyway", operation), "Keep editing" };
247                         int i;
248                         i = JOptionPane.showOptionDialog(this,
249                                                          String.format("Configuration modified. %s anyway?", operation),
250                                                          "Configuration Modified",
251                                                          JOptionPane.DEFAULT_OPTION,
252                                                          JOptionPane.WARNING_MESSAGE,
253                                                          null, options, options[1]);
254                         if (i != 0)
255                                 return false;
256                 }
257                 return true;
258         }
259
260         /* Listen for events from our buttons */
261         public void actionPerformed(ActionEvent e) {
262                 String  cmd = e.getActionCommand();
263
264                 if (cmd.equals("Close") || cmd.equals("Reboot"))
265                         if (!check_dirty(cmd))
266                                 return;
267                 listener.actionPerformed(e);
268                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
269                         setVisible(false);
270                         dispose();
271                 }
272                 dirty = false;
273         }
274
275         /* ItemListener interface method */
276         public void itemStateChanged(ItemEvent e) {
277                 dirty = true;
278         }
279
280         /* DocumentListener interface methods */
281         public void changedUpdate(DocumentEvent e) {
282                 dirty = true;
283         }
284
285         public void insertUpdate(DocumentEvent e) {
286                 dirty = true;
287         }
288
289         public void removeUpdate(DocumentEvent e) {
290                 dirty = true;
291         }
292
293         /* Let the config code hook on a listener */
294         public void addActionListener(ActionListener l) {
295                 listener = l;
296         }
297
298         /* set and get all of the dialog values */
299         public void set_product(String product) {
300                 radio_frequency_value.set_product(product);
301                 product_value.setText(product);
302         }
303
304         public void set_version(String version) {
305                 version_value.setText(version);
306         }
307
308         public void set_serial(int serial) {
309                 radio_frequency_value.set_serial(serial);
310                 serial_value.setText(String.format("%d", serial));
311         }
312
313         public void set_radio_frequency(double new_radio_frequency) {
314                 radio_frequency_value.set_frequency(new_radio_frequency);
315         }
316
317         public double radio_frequency() {
318                 return radio_frequency_value.frequency();
319         }
320
321         public void set_radio_calibration(int calibration) {
322                 radio_calibration_value.setText(String.format("%d", calibration));
323         }
324
325         public void set_clean() {
326                 dirty = false;
327         }
328 }