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_1.*;
25 import org.altusmetrum.altosuilib_1.*;
27 public class AltosConfigTDUI
29 implements ActionListener, ItemListener, DocumentListener
37 JLabel frequency_label;
38 JLabel radio_calibration_label;
39 JLabel radio_frequency_label;
47 AltosFreqList radio_frequency_value;
48 JLabel radio_calibration_value;
55 ActionListener listener;
58 /* A window listener to catch closing events and tell the config code */
59 class ConfigListener extends WindowAdapter {
62 public ConfigListener(AltosConfigTDUI this_ui) {
66 public void windowClosing(WindowEvent e) {
67 ui.actionPerformed(new ActionEvent(e.getSource(),
68 ActionEvent.ACTION_PERFORMED,
73 /* Build the UI using a grid bag */
74 public AltosConfigTDUI(JFrame in_owner) {
75 super (in_owner, "Configure TeleDongle", false);
80 Insets il = new Insets(4,4,4,4);
81 Insets ir = new Insets(4,4,4,4);
83 pane = getContentPane();
84 pane.setLayout(new GridBagLayout());
87 c = new GridBagConstraints();
88 c.gridx = 0; c.gridy = 0;
90 c.fill = GridBagConstraints.NONE;
91 c.anchor = GridBagConstraints.LINE_START;
93 product_label = new JLabel("Product:");
94 pane.add(product_label, c);
96 c = new GridBagConstraints();
97 c.gridx = 4; c.gridy = 0;
99 c.fill = GridBagConstraints.HORIZONTAL;
101 c.anchor = GridBagConstraints.LINE_START;
103 product_value = new JLabel("");
104 pane.add(product_value, c);
107 c = new GridBagConstraints();
108 c.gridx = 0; c.gridy = 1;
110 c.fill = GridBagConstraints.NONE;
111 c.anchor = GridBagConstraints.LINE_START;
114 version_label = new JLabel("Software version:");
115 pane.add(version_label, c);
117 c = new GridBagConstraints();
118 c.gridx = 4; c.gridy = 1;
120 c.fill = GridBagConstraints.HORIZONTAL;
122 c.anchor = GridBagConstraints.LINE_START;
125 version_value = new JLabel("");
126 pane.add(version_value, c);
129 c = new GridBagConstraints();
130 c.gridx = 0; c.gridy = 2;
132 c.fill = GridBagConstraints.NONE;
133 c.anchor = GridBagConstraints.LINE_START;
136 serial_label = new JLabel("Serial:");
137 pane.add(serial_label, c);
139 c = new GridBagConstraints();
140 c.gridx = 4; c.gridy = 2;
142 c.fill = GridBagConstraints.HORIZONTAL;
144 c.anchor = GridBagConstraints.LINE_START;
147 serial_value = new JLabel("");
148 pane.add(serial_value, c);
151 c = new GridBagConstraints();
152 c.gridx = 0; c.gridy = 5;
154 c.fill = GridBagConstraints.NONE;
155 c.anchor = GridBagConstraints.LINE_START;
158 radio_frequency_label = new JLabel("Frequency:");
159 pane.add(radio_frequency_label, c);
161 c = new GridBagConstraints();
162 c.gridx = 4; c.gridy = 5;
164 c.fill = GridBagConstraints.HORIZONTAL;
166 c.anchor = GridBagConstraints.LINE_START;
169 radio_frequency_value = new AltosFreqList();
170 radio_frequency_value.addItemListener(this);
171 pane.add(radio_frequency_value, c);
172 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
174 /* Radio Calibration */
175 c = new GridBagConstraints();
176 c.gridx = 0; c.gridy = 6;
178 c.fill = GridBagConstraints.NONE;
179 c.anchor = GridBagConstraints.LINE_START;
182 radio_calibration_label = new JLabel("RF Calibration:");
183 pane.add(radio_calibration_label, c);
185 c = new GridBagConstraints();
186 c.gridx = 4; c.gridy = 6;
188 c.fill = GridBagConstraints.HORIZONTAL;
190 c.anchor = GridBagConstraints.LINE_START;
193 radio_calibration_value = new JLabel(String.format("%d", 1186611));
194 pane.add(radio_calibration_value, c);
197 c = new GridBagConstraints();
198 c.gridx = 0; c.gridy = 12;
200 c.fill = GridBagConstraints.NONE;
201 c.anchor = GridBagConstraints.LINE_START;
203 save = new JButton("Save");
205 save.addActionListener(this);
206 save.setActionCommand("Save");
208 c = new GridBagConstraints();
209 c.gridx = 2; c.gridy = 12;
211 c.fill = GridBagConstraints.NONE;
212 c.anchor = GridBagConstraints.CENTER;
214 reset = new JButton("Reset");
216 reset.addActionListener(this);
217 reset.setActionCommand("Reset");
219 c = new GridBagConstraints();
220 c.gridx = 6; c.gridy = 12;
222 c.fill = GridBagConstraints.NONE;
223 c.anchor = GridBagConstraints.LINE_END;
225 close = new JButton("Close");
227 close.addActionListener(this);
228 close.setActionCommand("Close");
230 addWindowListener(new ConfigListener(this));
233 /* Once the initial values are set, the config code will show the dialog */
234 public void make_visible() {
236 setLocationRelativeTo(owner);
240 /* If any values have been changed, confirm before closing */
241 public boolean check_dirty(String operation) {
243 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
245 i = JOptionPane.showOptionDialog(this,
246 String.format("Configuration modified. %s anyway?", operation),
247 "Configuration Modified",
248 JOptionPane.DEFAULT_OPTION,
249 JOptionPane.WARNING_MESSAGE,
250 null, options, options[1]);
257 /* Listen for events from our buttons */
258 public void actionPerformed(ActionEvent e) {
259 String cmd = e.getActionCommand();
261 if (cmd.equals("Close") || cmd.equals("Reboot"))
262 if (!check_dirty(cmd))
264 listener.actionPerformed(e);
265 if (cmd.equals("Close") || cmd.equals("Reboot")) {
272 /* ItemListener interface method */
273 public void itemStateChanged(ItemEvent e) {
277 /* DocumentListener interface methods */
278 public void changedUpdate(DocumentEvent e) {
282 public void insertUpdate(DocumentEvent e) {
286 public void removeUpdate(DocumentEvent e) {
290 /* Let the config code hook on a listener */
291 public void addActionListener(ActionListener l) {
295 /* set and get all of the dialog values */
296 public void set_product(String product) {
297 radio_frequency_value.set_product(product);
298 product_value.setText(product);
301 public void set_version(String version) {
302 version_value.setText(version);
305 public void set_serial(int serial) {
306 radio_frequency_value.set_serial(serial);
307 serial_value.setText(String.format("%d", serial));
310 public void set_radio_frequency(double new_radio_frequency) {
312 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
313 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
315 if (f.close(new_radio_frequency)) {
316 radio_frequency_value.setSelectedIndex(i);
320 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
321 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
323 if (new_radio_frequency < f.frequency)
326 String description = String.format("%s serial %s",
327 product_value.getText(),
328 serial_value.getText());
329 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
330 AltosPreferences.add_common_frequency(new_frequency);
331 radio_frequency_value.insertItemAt(new_frequency, i);
332 radio_frequency_value.setSelectedIndex(i);
335 public double radio_frequency() {
336 return radio_frequency_value.frequency();
339 public void set_radio_calibration(int calibration) {
340 radio_calibration_value.setText(String.format("%d", calibration));
343 public void set_clean() {