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; version 2 of the License.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.event.*;
24 import org.altusmetrum.altoslib_5.*;
25 import org.altusmetrum.altosuilib_3.*;
27 public class AltosConfigTDUI
29 implements ActionListener, ItemListener, DocumentListener
37 JLabel frequency_label;
38 JLabel radio_calibration_label;
39 JLabel radio_frequency_label;
48 AltosUIFreqList radio_frequency_value;
49 JLabel radio_calibration_value;
50 AltosUIRateList rate_value;
57 ActionListener listener;
59 /* A window listener to catch closing events and tell the config code */
60 class ConfigListener extends WindowAdapter {
63 public ConfigListener(AltosConfigTDUI this_ui) {
67 public void windowClosing(WindowEvent e) {
68 ui.actionPerformed(new ActionEvent(e.getSource(),
69 ActionEvent.ACTION_PERFORMED,
74 /* Build the UI using a grid bag */
75 public AltosConfigTDUI(JFrame in_owner) {
76 super (in_owner, "Configure TeleDongle", false);
81 Insets il = new Insets(4,4,4,4);
82 Insets ir = new Insets(4,4,4,4);
84 pane = getContentPane();
85 pane.setLayout(new GridBagLayout());
88 c = new GridBagConstraints();
89 c.gridx = 0; c.gridy = 0;
91 c.fill = GridBagConstraints.NONE;
92 c.anchor = GridBagConstraints.LINE_START;
94 product_label = new JLabel("Product:");
95 pane.add(product_label, c);
97 c = new GridBagConstraints();
98 c.gridx = 4; c.gridy = 0;
100 c.fill = GridBagConstraints.HORIZONTAL;
102 c.anchor = GridBagConstraints.LINE_START;
104 product_value = new JLabel("");
105 pane.add(product_value, c);
108 c = new GridBagConstraints();
109 c.gridx = 0; c.gridy = 1;
111 c.fill = GridBagConstraints.NONE;
112 c.anchor = GridBagConstraints.LINE_START;
115 version_label = new JLabel("Software version:");
116 pane.add(version_label, c);
118 c = new GridBagConstraints();
119 c.gridx = 4; c.gridy = 1;
121 c.fill = GridBagConstraints.HORIZONTAL;
123 c.anchor = GridBagConstraints.LINE_START;
126 version_value = new JLabel("");
127 pane.add(version_value, c);
130 c = new GridBagConstraints();
131 c.gridx = 0; c.gridy = 2;
133 c.fill = GridBagConstraints.NONE;
134 c.anchor = GridBagConstraints.LINE_START;
137 serial_label = new JLabel("Serial:");
138 pane.add(serial_label, c);
140 c = new GridBagConstraints();
141 c.gridx = 4; c.gridy = 2;
143 c.fill = GridBagConstraints.HORIZONTAL;
145 c.anchor = GridBagConstraints.LINE_START;
148 serial_value = new JLabel("");
149 pane.add(serial_value, c);
152 c = new GridBagConstraints();
153 c.gridx = 0; c.gridy = 5;
155 c.fill = GridBagConstraints.NONE;
156 c.anchor = GridBagConstraints.LINE_START;
159 radio_frequency_label = new JLabel("Frequency:");
160 pane.add(radio_frequency_label, c);
162 c = new GridBagConstraints();
163 c.gridx = 4; c.gridy = 5;
165 c.fill = GridBagConstraints.HORIZONTAL;
167 c.anchor = GridBagConstraints.LINE_START;
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");
175 /* Radio Calibration */
176 c = new GridBagConstraints();
177 c.gridx = 0; c.gridy = 6;
179 c.fill = GridBagConstraints.NONE;
180 c.anchor = GridBagConstraints.LINE_START;
183 radio_calibration_label = new JLabel("RF Calibration:");
184 pane.add(radio_calibration_label, c);
186 c = new GridBagConstraints();
187 c.gridx = 4; c.gridy = 6;
189 c.fill = GridBagConstraints.HORIZONTAL;
191 c.anchor = GridBagConstraints.LINE_START;
194 radio_calibration_value = new JLabel(String.format("%d", 1186611));
195 pane.add(radio_calibration_value, c);
198 c = new GridBagConstraints();
199 c.gridx = 0; c.gridy = 7;
201 c.fill = GridBagConstraints.NONE;
202 c.anchor = GridBagConstraints.LINE_START;
205 rate_label = new JLabel("Telemetry Rate:");
206 pane.add(rate_label, c);
208 c = new GridBagConstraints();
209 c.gridx = 4; c.gridy = 7;
211 c.fill = GridBagConstraints.HORIZONTAL;
213 c.anchor = GridBagConstraints.LINE_START;
216 rate_value = new AltosUIRateList();
217 pane.add(rate_value, c);
220 c = new GridBagConstraints();
221 c.gridx = 0; c.gridy = 12;
223 c.fill = GridBagConstraints.NONE;
224 c.anchor = GridBagConstraints.LINE_START;
226 save = new JButton("Save");
228 save.addActionListener(this);
229 save.setActionCommand("Save");
231 c = new GridBagConstraints();
232 c.gridx = 2; c.gridy = 12;
234 c.fill = GridBagConstraints.NONE;
235 c.anchor = GridBagConstraints.CENTER;
237 reset = new JButton("Reset");
239 reset.addActionListener(this);
240 reset.setActionCommand("Reset");
242 c = new GridBagConstraints();
243 c.gridx = 6; c.gridy = 12;
245 c.fill = GridBagConstraints.NONE;
246 c.anchor = GridBagConstraints.LINE_END;
248 close = new JButton("Close");
250 close.addActionListener(this);
251 close.setActionCommand("Close");
253 addWindowListener(new ConfigListener(this));
256 /* Once the initial values are set, the config code will show the dialog */
257 public void make_visible() {
259 setLocationRelativeTo(owner);
263 /* If any values have been changed, confirm before closing */
264 public boolean check_dirty(String operation) {
266 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
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]);
280 /* Listen for events from our buttons */
281 public void actionPerformed(ActionEvent e) {
282 String cmd = e.getActionCommand();
284 if (cmd.equals("Close") || cmd.equals("Reboot"))
285 if (!check_dirty(cmd))
287 listener.actionPerformed(e);
288 if (cmd.equals("Close") || cmd.equals("Reboot")) {
295 /* ItemListener interface method */
296 public void itemStateChanged(ItemEvent e) {
300 /* DocumentListener interface methods */
301 public void changedUpdate(DocumentEvent e) {
305 public void insertUpdate(DocumentEvent e) {
309 public void removeUpdate(DocumentEvent e) {
313 /* Let the config code hook on a listener */
314 public void addActionListener(ActionListener l) {
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);
324 public void set_version(String version) {
325 version_value.setText(version);
328 public void set_serial(int serial) {
329 radio_frequency_value.set_serial(serial);
330 serial_value.setText(String.format("%d", serial));
333 public void set_radio_frequency(double new_radio_frequency) {
335 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
336 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
338 if (f.close(new_radio_frequency)) {
339 radio_frequency_value.setSelectedIndex(i);
343 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
344 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
346 if (new_radio_frequency < f.frequency)
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);
358 public double radio_frequency() {
359 return radio_frequency_value.frequency();
362 public void set_radio_calibration(int calibration) {
363 radio_calibration_value.setText(String.format("%d", calibration));
366 public int telemetry_rate() {
367 return rate_value.getSelectedIndex();
370 public void set_telemetry_rate(int rate) {
371 rate_value.setSelectedIndex(rate);
374 public void set_clean() {