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.*;
26 public class AltosConfigTDUI
28 implements ActionListener, ItemListener, DocumentListener
36 JLabel frequency_label;
37 JLabel radio_calibration_label;
38 JLabel radio_frequency_label;
46 AltosFreqList radio_frequency_value;
47 JLabel radio_calibration_value;
54 ActionListener listener;
57 /* A window listener to catch closing events and tell the config code */
58 class ConfigListener extends WindowAdapter {
61 public ConfigListener(AltosConfigTDUI this_ui) {
65 public void windowClosing(WindowEvent e) {
66 ui.actionPerformed(new ActionEvent(e.getSource(),
67 ActionEvent.ACTION_PERFORMED,
72 /* Build the UI using a grid bag */
73 public AltosConfigTDUI(JFrame in_owner) {
74 super (in_owner, "Configure TeleDongle", false);
79 Insets il = new Insets(4,4,4,4);
80 Insets ir = new Insets(4,4,4,4);
82 pane = getContentPane();
83 pane.setLayout(new GridBagLayout());
86 c = new GridBagConstraints();
87 c.gridx = 0; c.gridy = 0;
89 c.fill = GridBagConstraints.NONE;
90 c.anchor = GridBagConstraints.LINE_START;
92 product_label = new JLabel("Product:");
93 pane.add(product_label, c);
95 c = new GridBagConstraints();
96 c.gridx = 4; c.gridy = 0;
98 c.fill = GridBagConstraints.HORIZONTAL;
100 c.anchor = GridBagConstraints.LINE_START;
102 product_value = new JLabel("");
103 pane.add(product_value, c);
106 c = new GridBagConstraints();
107 c.gridx = 0; c.gridy = 1;
109 c.fill = GridBagConstraints.NONE;
110 c.anchor = GridBagConstraints.LINE_START;
113 version_label = new JLabel("Software version:");
114 pane.add(version_label, c);
116 c = new GridBagConstraints();
117 c.gridx = 4; c.gridy = 1;
119 c.fill = GridBagConstraints.HORIZONTAL;
121 c.anchor = GridBagConstraints.LINE_START;
124 version_value = new JLabel("");
125 pane.add(version_value, c);
128 c = new GridBagConstraints();
129 c.gridx = 0; c.gridy = 2;
131 c.fill = GridBagConstraints.NONE;
132 c.anchor = GridBagConstraints.LINE_START;
135 serial_label = new JLabel("Serial:");
136 pane.add(serial_label, c);
138 c = new GridBagConstraints();
139 c.gridx = 4; c.gridy = 2;
141 c.fill = GridBagConstraints.HORIZONTAL;
143 c.anchor = GridBagConstraints.LINE_START;
146 serial_value = new JLabel("");
147 pane.add(serial_value, c);
150 c = new GridBagConstraints();
151 c.gridx = 0; c.gridy = 5;
153 c.fill = GridBagConstraints.NONE;
154 c.anchor = GridBagConstraints.LINE_START;
157 radio_frequency_label = new JLabel("Frequency:");
158 pane.add(radio_frequency_label, c);
160 c = new GridBagConstraints();
161 c.gridx = 4; c.gridy = 5;
163 c.fill = GridBagConstraints.HORIZONTAL;
165 c.anchor = GridBagConstraints.LINE_START;
168 radio_frequency_value = new AltosFreqList();
169 radio_frequency_value.addItemListener(this);
170 pane.add(radio_frequency_value, c);
171 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
173 /* Radio Calibration */
174 c = new GridBagConstraints();
175 c.gridx = 0; c.gridy = 6;
177 c.fill = GridBagConstraints.NONE;
178 c.anchor = GridBagConstraints.LINE_START;
181 radio_calibration_label = new JLabel("RF Calibration:");
182 pane.add(radio_calibration_label, c);
184 c = new GridBagConstraints();
185 c.gridx = 4; c.gridy = 6;
187 c.fill = GridBagConstraints.HORIZONTAL;
189 c.anchor = GridBagConstraints.LINE_START;
192 radio_calibration_value = new JLabel(String.format("%d", 1186611));
193 pane.add(radio_calibration_value, c);
196 c = new GridBagConstraints();
197 c.gridx = 0; c.gridy = 12;
199 c.fill = GridBagConstraints.NONE;
200 c.anchor = GridBagConstraints.LINE_START;
202 save = new JButton("Save");
204 save.addActionListener(this);
205 save.setActionCommand("Save");
207 c = new GridBagConstraints();
208 c.gridx = 2; c.gridy = 12;
210 c.fill = GridBagConstraints.NONE;
211 c.anchor = GridBagConstraints.CENTER;
213 reset = new JButton("Reset");
215 reset.addActionListener(this);
216 reset.setActionCommand("Reset");
218 c = new GridBagConstraints();
219 c.gridx = 6; c.gridy = 12;
221 c.fill = GridBagConstraints.NONE;
222 c.anchor = GridBagConstraints.LINE_END;
224 close = new JButton("Close");
226 close.addActionListener(this);
227 close.setActionCommand("Close");
229 addWindowListener(new ConfigListener(this));
232 /* Once the initial values are set, the config code will show the dialog */
233 public void make_visible() {
235 setLocationRelativeTo(owner);
239 /* If any values have been changed, confirm before closing */
240 public boolean check_dirty(String operation) {
242 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
244 i = JOptionPane.showOptionDialog(this,
245 String.format("Configuration modified. %s anyway?", operation),
246 "Configuration Modified",
247 JOptionPane.DEFAULT_OPTION,
248 JOptionPane.WARNING_MESSAGE,
249 null, options, options[1]);
256 /* Listen for events from our buttons */
257 public void actionPerformed(ActionEvent e) {
258 String cmd = e.getActionCommand();
260 if (cmd.equals("Close") || cmd.equals("Reboot"))
261 if (!check_dirty(cmd))
263 listener.actionPerformed(e);
264 if (cmd.equals("Close") || cmd.equals("Reboot")) {
271 /* ItemListener interface method */
272 public void itemStateChanged(ItemEvent e) {
276 /* DocumentListener interface methods */
277 public void changedUpdate(DocumentEvent e) {
281 public void insertUpdate(DocumentEvent e) {
285 public void removeUpdate(DocumentEvent e) {
289 /* Let the config code hook on a listener */
290 public void addActionListener(ActionListener l) {
294 /* set and get all of the dialog values */
295 public void set_product(String product) {
296 radio_frequency_value.set_product(product);
297 product_value.setText(product);
300 public void set_version(String version) {
301 version_value.setText(version);
304 public void set_serial(int serial) {
305 radio_frequency_value.set_serial(serial);
306 serial_value.setText(String.format("%d", serial));
309 public void set_radio_frequency(double new_radio_frequency) {
311 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
312 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
314 if (f.close(new_radio_frequency)) {
315 radio_frequency_value.setSelectedIndex(i);
319 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
320 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
322 if (new_radio_frequency < f.frequency)
325 String description = String.format("%s serial %s",
326 product_value.getText(),
327 serial_value.getText());
328 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
329 AltosPreferences.add_common_frequency(new_frequency);
330 radio_frequency_value.insertItemAt(new_frequency, i);
331 radio_frequency_value.setSelectedIndex(i);
334 public double radio_frequency() {
335 return radio_frequency_value.frequency();
338 public void set_radio_calibration(int calibration) {
339 radio_calibration_value.setText(String.format("%d", calibration));
342 public void set_clean() {