2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 import java.awt.event.*;
24 import javax.swing.event.*;
25 import org.altusmetrum.altoslib_14.*;
26 import org.altusmetrum.altosuilib_14.*;
28 public class AltosConfigTDUI
30 implements ActionListener, ItemListener, DocumentListener
38 JLabel frequency_label;
39 JLabel radio_calibration_label;
40 JLabel radio_frequency_label;
49 AltosUIFreqList radio_frequency_value;
50 JLabel radio_calibration_value;
51 AltosUIRateList rate_value;
58 ActionListener listener;
60 /* A window listener to catch closing events and tell the config code */
61 class ConfigListener extends WindowAdapter {
64 public ConfigListener(AltosConfigTDUI this_ui) {
68 public void windowClosing(WindowEvent e) {
69 ui.actionPerformed(new ActionEvent(e.getSource(),
70 ActionEvent.ACTION_PERFORMED,
75 /* Build the UI using a grid bag */
76 public AltosConfigTDUI(JFrame in_owner) {
77 super (in_owner, "Configure TeleDongle", false);
82 Insets il = new Insets(4,4,4,4);
83 Insets ir = new Insets(4,4,4,4);
85 pane = getScrollablePane();
86 pane.setLayout(new GridBagLayout());
89 c = new GridBagConstraints();
90 c.gridx = 0; c.gridy = 0;
92 c.fill = GridBagConstraints.NONE;
93 c.anchor = GridBagConstraints.LINE_START;
95 product_label = new JLabel("Product:");
96 pane.add(product_label, c);
98 c = new GridBagConstraints();
99 c.gridx = 4; c.gridy = 0;
101 c.fill = GridBagConstraints.HORIZONTAL;
103 c.anchor = GridBagConstraints.LINE_START;
105 product_value = new JLabel("");
106 pane.add(product_value, c);
109 c = new GridBagConstraints();
110 c.gridx = 0; c.gridy = 1;
112 c.fill = GridBagConstraints.NONE;
113 c.anchor = GridBagConstraints.LINE_START;
116 version_label = new JLabel("Software version:");
117 pane.add(version_label, c);
119 c = new GridBagConstraints();
120 c.gridx = 4; c.gridy = 1;
122 c.fill = GridBagConstraints.HORIZONTAL;
124 c.anchor = GridBagConstraints.LINE_START;
127 version_value = new JLabel("");
128 pane.add(version_value, c);
131 c = new GridBagConstraints();
132 c.gridx = 0; c.gridy = 2;
134 c.fill = GridBagConstraints.NONE;
135 c.anchor = GridBagConstraints.LINE_START;
138 serial_label = new JLabel("Serial:");
139 pane.add(serial_label, c);
141 c = new GridBagConstraints();
142 c.gridx = 4; c.gridy = 2;
144 c.fill = GridBagConstraints.HORIZONTAL;
146 c.anchor = GridBagConstraints.LINE_START;
149 serial_value = new JLabel("");
150 pane.add(serial_value, c);
153 c = new GridBagConstraints();
154 c.gridx = 0; c.gridy = 5;
156 c.fill = GridBagConstraints.NONE;
157 c.anchor = GridBagConstraints.LINE_START;
160 radio_frequency_label = new JLabel("Frequency:");
161 pane.add(radio_frequency_label, c);
163 c = new GridBagConstraints();
164 c.gridx = 4; c.gridy = 5;
166 c.fill = GridBagConstraints.HORIZONTAL;
168 c.anchor = GridBagConstraints.LINE_START;
171 radio_frequency_value = new AltosUIFreqList();
172 radio_frequency_value.addItemListener(this);
173 pane.add(radio_frequency_value, c);
174 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
176 /* Radio Calibration */
177 c = new GridBagConstraints();
178 c.gridx = 0; c.gridy = 6;
180 c.fill = GridBagConstraints.NONE;
181 c.anchor = GridBagConstraints.LINE_START;
184 radio_calibration_label = new JLabel("RF Calibration:");
185 pane.add(radio_calibration_label, c);
187 c = new GridBagConstraints();
188 c.gridx = 4; c.gridy = 6;
190 c.fill = GridBagConstraints.HORIZONTAL;
192 c.anchor = GridBagConstraints.LINE_START;
195 radio_calibration_value = new JLabel(String.format("%d", 1186611));
196 pane.add(radio_calibration_value, c);
199 c = new GridBagConstraints();
200 c.gridx = 0; c.gridy = 7;
202 c.fill = GridBagConstraints.NONE;
203 c.anchor = GridBagConstraints.LINE_START;
206 rate_label = new JLabel("Telemetry Rate:");
207 pane.add(rate_label, c);
209 c = new GridBagConstraints();
210 c.gridx = 4; c.gridy = 7;
212 c.fill = GridBagConstraints.HORIZONTAL;
214 c.anchor = GridBagConstraints.LINE_START;
217 rate_value = new AltosUIRateList();
218 pane.add(rate_value, c);
221 c = new GridBagConstraints();
222 c.gridx = 0; c.gridy = 12;
224 c.fill = GridBagConstraints.NONE;
225 c.anchor = GridBagConstraints.LINE_START;
227 save = new JButton("Save");
229 save.addActionListener(this);
230 save.setActionCommand("Save");
232 c = new GridBagConstraints();
233 c.gridx = 2; c.gridy = 12;
235 c.fill = GridBagConstraints.NONE;
236 c.anchor = GridBagConstraints.CENTER;
238 reset = new JButton("Reset");
240 reset.addActionListener(this);
241 reset.setActionCommand("Reset");
243 c = new GridBagConstraints();
244 c.gridx = 6; c.gridy = 12;
246 c.fill = GridBagConstraints.NONE;
247 c.anchor = GridBagConstraints.LINE_END;
249 close = new JButton("Close");
251 close.addActionListener(this);
252 close.setActionCommand("Close");
254 addWindowListener(new ConfigListener(this));
257 /* Once the initial values are set, the config code will show the dialog */
258 public void make_visible() {
260 setLocationRelativeTo(owner);
264 /* If any values have been changed, confirm before closing */
265 public boolean check_dirty(String operation) {
267 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
269 i = JOptionPane.showOptionDialog(this,
270 String.format("Configuration modified. %s anyway?", operation),
271 "Configuration Modified",
272 JOptionPane.DEFAULT_OPTION,
273 JOptionPane.WARNING_MESSAGE,
274 null, options, options[1]);
281 /* Listen for events from our buttons */
282 public void actionPerformed(ActionEvent e) {
283 String cmd = e.getActionCommand();
285 if (cmd.equals("Close") || cmd.equals("Reboot"))
286 if (!check_dirty(cmd))
288 listener.actionPerformed(e);
289 if (cmd.equals("Close") || cmd.equals("Reboot")) {
296 /* ItemListener interface method */
297 public void itemStateChanged(ItemEvent e) {
301 /* DocumentListener interface methods */
302 public void changedUpdate(DocumentEvent e) {
306 public void insertUpdate(DocumentEvent e) {
310 public void removeUpdate(DocumentEvent e) {
314 /* Let the config code hook on a listener */
315 public void addActionListener(ActionListener l) {
319 /* set and get all of the dialog values */
320 public void set_product(String product) {
321 radio_frequency_value.set_product(product);
322 product_value.setText(product);
325 public void set_version(String version) {
326 version_value.setText(version);
329 public void set_serial(int serial) {
330 radio_frequency_value.set_serial(serial);
331 serial_value.setText(String.format("%d", serial));
334 public void set_radio_frequency(double new_radio_frequency) {
336 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
337 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
339 if (f.close(new_radio_frequency)) {
340 radio_frequency_value.setSelectedIndex(i);
344 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
345 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
347 if (new_radio_frequency < f.frequency)
350 String description = String.format("%s serial %s",
351 product_value.getText(),
352 serial_value.getText());
353 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
354 AltosPreferences.add_common_frequency(new_frequency);
355 radio_frequency_value.insertItemAt(new_frequency, i);
356 radio_frequency_value.setSelectedIndex(i);
359 public double radio_frequency() {
360 return radio_frequency_value.frequency();
363 public void set_radio_calibration(int calibration) {
364 radio_calibration_value.setText(String.format("%d", calibration));
367 public int telemetry_rate() {
368 return rate_value.getSelectedIndex();
371 public void set_telemetry_rate(int rate) {
372 rate_value.setSelectedIndex(rate);
375 public void set_clean() {