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.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.LinkedBlockingQueue;
34 import org.altusmetrum.AltosLib.*;
36 public class AltosConfigTDUI
38 implements ActionListener, ItemListener, DocumentListener
46 JLabel frequency_label;
47 JLabel radio_calibration_label;
48 JLabel radio_frequency_label;
56 AltosFreqList radio_frequency_value;
57 JLabel radio_calibration_value;
64 ActionListener listener;
67 /* A window listener to catch closing events and tell the config code */
68 class ConfigListener extends WindowAdapter {
71 public ConfigListener(AltosConfigTDUI this_ui) {
75 public void windowClosing(WindowEvent e) {
76 ui.actionPerformed(new ActionEvent(e.getSource(),
77 ActionEvent.ACTION_PERFORMED,
82 /* Build the UI using a grid bag */
83 public AltosConfigTDUI(JFrame in_owner) {
84 super (in_owner, "Configure TeleDongle", false);
89 Insets il = new Insets(4,4,4,4);
90 Insets ir = new Insets(4,4,4,4);
92 pane = getContentPane();
93 pane.setLayout(new GridBagLayout());
96 c = new GridBagConstraints();
97 c.gridx = 0; c.gridy = 0;
99 c.fill = GridBagConstraints.NONE;
100 c.anchor = GridBagConstraints.LINE_START;
102 product_label = new JLabel("Product:");
103 pane.add(product_label, c);
105 c = new GridBagConstraints();
106 c.gridx = 4; c.gridy = 0;
108 c.fill = GridBagConstraints.HORIZONTAL;
110 c.anchor = GridBagConstraints.LINE_START;
112 product_value = new JLabel("");
113 pane.add(product_value, c);
116 c = new GridBagConstraints();
117 c.gridx = 0; c.gridy = 1;
119 c.fill = GridBagConstraints.NONE;
120 c.anchor = GridBagConstraints.LINE_START;
123 version_label = new JLabel("Software version:");
124 pane.add(version_label, c);
126 c = new GridBagConstraints();
127 c.gridx = 4; c.gridy = 1;
129 c.fill = GridBagConstraints.HORIZONTAL;
131 c.anchor = GridBagConstraints.LINE_START;
134 version_value = new JLabel("");
135 pane.add(version_value, c);
138 c = new GridBagConstraints();
139 c.gridx = 0; c.gridy = 2;
141 c.fill = GridBagConstraints.NONE;
142 c.anchor = GridBagConstraints.LINE_START;
145 serial_label = new JLabel("Serial:");
146 pane.add(serial_label, c);
148 c = new GridBagConstraints();
149 c.gridx = 4; c.gridy = 2;
151 c.fill = GridBagConstraints.HORIZONTAL;
153 c.anchor = GridBagConstraints.LINE_START;
156 serial_value = new JLabel("");
157 pane.add(serial_value, c);
160 c = new GridBagConstraints();
161 c.gridx = 0; c.gridy = 5;
163 c.fill = GridBagConstraints.NONE;
164 c.anchor = GridBagConstraints.LINE_START;
167 radio_frequency_label = new JLabel("Frequency:");
168 pane.add(radio_frequency_label, c);
170 c = new GridBagConstraints();
171 c.gridx = 4; c.gridy = 5;
173 c.fill = GridBagConstraints.HORIZONTAL;
175 c.anchor = GridBagConstraints.LINE_START;
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");
183 /* Radio Calibration */
184 c = new GridBagConstraints();
185 c.gridx = 0; c.gridy = 6;
187 c.fill = GridBagConstraints.NONE;
188 c.anchor = GridBagConstraints.LINE_START;
191 radio_calibration_label = new JLabel("RF Calibration:");
192 pane.add(radio_calibration_label, c);
194 c = new GridBagConstraints();
195 c.gridx = 4; c.gridy = 6;
197 c.fill = GridBagConstraints.HORIZONTAL;
199 c.anchor = GridBagConstraints.LINE_START;
202 radio_calibration_value = new JLabel(String.format("%d", 1186611));
203 pane.add(radio_calibration_value, c);
206 c = new GridBagConstraints();
207 c.gridx = 0; c.gridy = 12;
209 c.fill = GridBagConstraints.NONE;
210 c.anchor = GridBagConstraints.LINE_START;
212 save = new JButton("Save");
214 save.addActionListener(this);
215 save.setActionCommand("Save");
217 c = new GridBagConstraints();
218 c.gridx = 2; c.gridy = 12;
220 c.fill = GridBagConstraints.NONE;
221 c.anchor = GridBagConstraints.CENTER;
223 reset = new JButton("Reset");
225 reset.addActionListener(this);
226 reset.setActionCommand("Reset");
228 c = new GridBagConstraints();
229 c.gridx = 6; c.gridy = 12;
231 c.fill = GridBagConstraints.NONE;
232 c.anchor = GridBagConstraints.LINE_END;
234 close = new JButton("Close");
236 close.addActionListener(this);
237 close.setActionCommand("Close");
239 addWindowListener(new ConfigListener(this));
242 /* Once the initial values are set, the config code will show the dialog */
243 public void make_visible() {
245 setLocationRelativeTo(owner);
249 /* If any values have been changed, confirm before closing */
250 public boolean check_dirty(String operation) {
252 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
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]);
266 /* Listen for events from our buttons */
267 public void actionPerformed(ActionEvent e) {
268 String cmd = e.getActionCommand();
270 if (cmd.equals("Close") || cmd.equals("Reboot"))
271 if (!check_dirty(cmd))
273 listener.actionPerformed(e);
274 if (cmd.equals("Close") || cmd.equals("Reboot")) {
281 /* ItemListener interface method */
282 public void itemStateChanged(ItemEvent e) {
286 /* DocumentListener interface methods */
287 public void changedUpdate(DocumentEvent e) {
291 public void insertUpdate(DocumentEvent e) {
295 public void removeUpdate(DocumentEvent e) {
299 /* Let the config code hook on a listener */
300 public void addActionListener(ActionListener l) {
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);
310 public void set_version(String version) {
311 version_value.setText(version);
314 public void set_serial(int serial) {
315 radio_frequency_value.set_serial(serial);
316 serial_value.setText(String.format("%d", serial));
319 public void set_radio_frequency(double new_radio_frequency) {
321 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
322 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
324 if (f.close(new_radio_frequency)) {
325 radio_frequency_value.setSelectedIndex(i);
329 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
330 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
332 if (new_radio_frequency < f.frequency)
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);
344 public double radio_frequency() {
345 return radio_frequency_value.frequency();
348 public void set_radio_calibration(int calibration) {
349 radio_calibration_value.setText(String.format("%d", calibration));
352 public void set_clean() {