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;
47 JMenuBar radio_frequency_menu_bar;
48 AltosFreqList radio_frequency_value;
49 JLabel radio_calibration_value;
56 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 AltosFreqList(false);
171 radio_frequency_value.addItemListener(this);
172 radio_frequency_menu_bar = new JMenuBar();
173 radio_frequency_menu_bar.add(radio_frequency_value);
174 pane.add(radio_frequency_menu_bar, c);
175 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
177 /* Radio Calibration */
178 c = new GridBagConstraints();
179 c.gridx = 0; c.gridy = 6;
181 c.fill = GridBagConstraints.NONE;
182 c.anchor = GridBagConstraints.LINE_START;
185 radio_calibration_label = new JLabel("RF Calibration:");
186 pane.add(radio_calibration_label, c);
188 c = new GridBagConstraints();
189 c.gridx = 4; c.gridy = 6;
191 c.fill = GridBagConstraints.HORIZONTAL;
193 c.anchor = GridBagConstraints.LINE_START;
196 radio_calibration_value = new JLabel(String.format("%d", 1186611));
197 pane.add(radio_calibration_value, c);
200 c = new GridBagConstraints();
201 c.gridx = 0; c.gridy = 12;
203 c.fill = GridBagConstraints.NONE;
204 c.anchor = GridBagConstraints.LINE_START;
206 save = new JButton("Save");
208 save.addActionListener(this);
209 save.setActionCommand("Save");
211 c = new GridBagConstraints();
212 c.gridx = 2; c.gridy = 12;
214 c.fill = GridBagConstraints.NONE;
215 c.anchor = GridBagConstraints.CENTER;
217 reset = new JButton("Reset");
219 reset.addActionListener(this);
220 reset.setActionCommand("Reset");
222 c = new GridBagConstraints();
223 c.gridx = 6; c.gridy = 12;
225 c.fill = GridBagConstraints.NONE;
226 c.anchor = GridBagConstraints.LINE_END;
228 close = new JButton("Close");
230 close.addActionListener(this);
231 close.setActionCommand("Close");
233 addWindowListener(new ConfigListener(this));
236 /* Once the initial values are set, the config code will show the dialog */
237 public void make_visible() {
239 setLocationRelativeTo(owner);
243 /* If any values have been changed, confirm before closing */
244 public boolean check_dirty(String operation) {
246 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
248 i = JOptionPane.showOptionDialog(this,
249 String.format("Configuration modified. %s anyway?", operation),
250 "Configuration Modified",
251 JOptionPane.DEFAULT_OPTION,
252 JOptionPane.WARNING_MESSAGE,
253 null, options, options[1]);
260 /* Listen for events from our buttons */
261 public void actionPerformed(ActionEvent e) {
262 String cmd = e.getActionCommand();
264 if (cmd.equals("Close") || cmd.equals("Reboot"))
265 if (!check_dirty(cmd))
267 listener.actionPerformed(e);
268 if (cmd.equals("Close") || cmd.equals("Reboot")) {
275 /* ItemListener interface method */
276 public void itemStateChanged(ItemEvent e) {
280 /* DocumentListener interface methods */
281 public void changedUpdate(DocumentEvent e) {
285 public void insertUpdate(DocumentEvent e) {
289 public void removeUpdate(DocumentEvent e) {
293 /* Let the config code hook on a listener */
294 public void addActionListener(ActionListener l) {
298 /* set and get all of the dialog values */
299 public void set_product(String product) {
300 radio_frequency_value.set_product(product);
301 product_value.setText(product);
304 public void set_version(String version) {
305 version_value.setText(version);
308 public void set_serial(int serial) {
309 radio_frequency_value.set_serial(serial);
310 serial_value.setText(String.format("%d", serial));
313 public void set_radio_frequency(double new_radio_frequency) {
314 radio_frequency_value.set_frequency(new_radio_frequency);
317 public double radio_frequency() {
318 return radio_frequency_value.frequency();
321 public void set_radio_calibration(int calibration) {
322 radio_calibration_value.setText(String.format("%d", calibration));
325 public void set_clean() {