altosui: Match directories in hex file matcher
[fw/altos] / altosui / AltosConfigTDUI.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.event.*;
24 import org.altusmetrum.altoslib_2.*;
25 import org.altusmetrum.altosuilib_1.*;
26
27 public class AltosConfigTDUI
28         extends AltosUIDialog
29         implements ActionListener, ItemListener, DocumentListener
30 {
31
32         Container       pane;
33         Box             box;
34         JLabel          product_label;
35         JLabel          version_label;
36         JLabel          serial_label;
37         JLabel          frequency_label;
38         JLabel          radio_calibration_label;
39         JLabel          radio_frequency_label;
40
41         public boolean          dirty;
42
43         JFrame          owner;
44         JLabel          product_value;
45         JLabel          version_value;
46         JLabel          serial_value;
47         AltosFreqList   radio_frequency_value;
48         JLabel          radio_calibration_value;
49
50         JButton         save;
51         JButton         reset;
52         JButton         reboot;
53         JButton         close;
54
55         ActionListener  listener;
56
57
58         /* A window listener to catch closing events and tell the config code */
59         class ConfigListener extends WindowAdapter {
60                 AltosConfigTDUI ui;
61
62                 public ConfigListener(AltosConfigTDUI this_ui) {
63                         ui = this_ui;
64                 }
65
66                 public void windowClosing(WindowEvent e) {
67                         ui.actionPerformed(new ActionEvent(e.getSource(),
68                                                            ActionEvent.ACTION_PERFORMED,
69                                                            "Close"));
70                 }
71         }
72
73         /* Build the UI using a grid bag */
74         public AltosConfigTDUI(JFrame in_owner) {
75                 super (in_owner, "Configure TeleDongle", false);
76
77                 owner = in_owner;
78                 GridBagConstraints c;
79
80                 Insets il = new Insets(4,4,4,4);
81                 Insets ir = new Insets(4,4,4,4);
82
83                 pane = getContentPane();
84                 pane.setLayout(new GridBagLayout());
85
86                 /* Product */
87                 c = new GridBagConstraints();
88                 c.gridx = 0; c.gridy = 0;
89                 c.gridwidth = 4;
90                 c.fill = GridBagConstraints.NONE;
91                 c.anchor = GridBagConstraints.LINE_START;
92                 c.insets = il;
93                 product_label = new JLabel("Product:");
94                 pane.add(product_label, c);
95
96                 c = new GridBagConstraints();
97                 c.gridx = 4; c.gridy = 0;
98                 c.gridwidth = 4;
99                 c.fill = GridBagConstraints.HORIZONTAL;
100                 c.weightx = 1;
101                 c.anchor = GridBagConstraints.LINE_START;
102                 c.insets = ir;
103                 product_value = new JLabel("");
104                 pane.add(product_value, c);
105
106                 /* Version */
107                 c = new GridBagConstraints();
108                 c.gridx = 0; c.gridy = 1;
109                 c.gridwidth = 4;
110                 c.fill = GridBagConstraints.NONE;
111                 c.anchor = GridBagConstraints.LINE_START;
112                 c.insets = il;
113                 c.ipady = 5;
114                 version_label = new JLabel("Software version:");
115                 pane.add(version_label, c);
116
117                 c = new GridBagConstraints();
118                 c.gridx = 4; c.gridy = 1;
119                 c.gridwidth = 4;
120                 c.fill = GridBagConstraints.HORIZONTAL;
121                 c.weightx = 1;
122                 c.anchor = GridBagConstraints.LINE_START;
123                 c.insets = ir;
124                 c.ipady = 5;
125                 version_value = new JLabel("");
126                 pane.add(version_value, c);
127
128                 /* Serial */
129                 c = new GridBagConstraints();
130                 c.gridx = 0; c.gridy = 2;
131                 c.gridwidth = 4;
132                 c.fill = GridBagConstraints.NONE;
133                 c.anchor = GridBagConstraints.LINE_START;
134                 c.insets = il;
135                 c.ipady = 5;
136                 serial_label = new JLabel("Serial:");
137                 pane.add(serial_label, c);
138
139                 c = new GridBagConstraints();
140                 c.gridx = 4; c.gridy = 2;
141                 c.gridwidth = 4;
142                 c.fill = GridBagConstraints.HORIZONTAL;
143                 c.weightx = 1;
144                 c.anchor = GridBagConstraints.LINE_START;
145                 c.insets = ir;
146                 c.ipady = 5;
147                 serial_value = new JLabel("");
148                 pane.add(serial_value, c);
149
150                 /* Frequency */
151                 c = new GridBagConstraints();
152                 c.gridx = 0; c.gridy = 5;
153                 c.gridwidth = 4;
154                 c.fill = GridBagConstraints.NONE;
155                 c.anchor = GridBagConstraints.LINE_START;
156                 c.insets = il;
157                 c.ipady = 5;
158                 radio_frequency_label = new JLabel("Frequency:");
159                 pane.add(radio_frequency_label, c);
160
161                 c = new GridBagConstraints();
162                 c.gridx = 4; c.gridy = 5;
163                 c.gridwidth = 4;
164                 c.fill = GridBagConstraints.HORIZONTAL;
165                 c.weightx = 1;
166                 c.anchor = GridBagConstraints.LINE_START;
167                 c.insets = ir;
168                 c.ipady = 5;
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");
173
174                 /* Radio Calibration */
175                 c = new GridBagConstraints();
176                 c.gridx = 0; c.gridy = 6;
177                 c.gridwidth = 4;
178                 c.fill = GridBagConstraints.NONE;
179                 c.anchor = GridBagConstraints.LINE_START;
180                 c.insets = il;
181                 c.ipady = 5;
182                 radio_calibration_label = new JLabel("RF Calibration:");
183                 pane.add(radio_calibration_label, c);
184
185                 c = new GridBagConstraints();
186                 c.gridx = 4; c.gridy = 6;
187                 c.gridwidth = 4;
188                 c.fill = GridBagConstraints.HORIZONTAL;
189                 c.weightx = 1;
190                 c.anchor = GridBagConstraints.LINE_START;
191                 c.insets = ir;
192                 c.ipady = 5;
193                 radio_calibration_value = new JLabel(String.format("%d", 1186611));
194                 pane.add(radio_calibration_value, c);
195
196                 /* Buttons */
197                 c = new GridBagConstraints();
198                 c.gridx = 0; c.gridy = 12;
199                 c.gridwidth = 2;
200                 c.fill = GridBagConstraints.NONE;
201                 c.anchor = GridBagConstraints.LINE_START;
202                 c.insets = il;
203                 save = new JButton("Save");
204                 pane.add(save, c);
205                 save.addActionListener(this);
206                 save.setActionCommand("Save");
207
208                 c = new GridBagConstraints();
209                 c.gridx = 2; c.gridy = 12;
210                 c.gridwidth = 2;
211                 c.fill = GridBagConstraints.NONE;
212                 c.anchor = GridBagConstraints.CENTER;
213                 c.insets = il;
214                 reset = new JButton("Reset");
215                 pane.add(reset, c);
216                 reset.addActionListener(this);
217                 reset.setActionCommand("Reset");
218
219                 c = new GridBagConstraints();
220                 c.gridx = 6; c.gridy = 12;
221                 c.gridwidth = 2;
222                 c.fill = GridBagConstraints.NONE;
223                 c.anchor = GridBagConstraints.LINE_END;
224                 c.insets = il;
225                 close = new JButton("Close");
226                 pane.add(close, c);
227                 close.addActionListener(this);
228                 close.setActionCommand("Close");
229
230                 addWindowListener(new ConfigListener(this));
231         }
232
233         /* Once the initial values are set, the config code will show the dialog */
234         public void make_visible() {
235                 pack();
236                 setLocationRelativeTo(owner);
237                 setVisible(true);
238         }
239
240         /* If any values have been changed, confirm before closing */
241         public boolean check_dirty(String operation) {
242                 if (dirty) {
243                         Object[] options = { String.format("%s anyway", operation), "Keep editing" };
244                         int i;
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]);
251                         if (i != 0)
252                                 return false;
253                 }
254                 return true;
255         }
256
257         /* Listen for events from our buttons */
258         public void actionPerformed(ActionEvent e) {
259                 String  cmd = e.getActionCommand();
260
261                 if (cmd.equals("Close") || cmd.equals("Reboot"))
262                         if (!check_dirty(cmd))
263                                 return;
264                 listener.actionPerformed(e);
265                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
266                         setVisible(false);
267                         dispose();
268                 }
269                 dirty = false;
270         }
271
272         /* ItemListener interface method */
273         public void itemStateChanged(ItemEvent e) {
274                 dirty = true;
275         }
276
277         /* DocumentListener interface methods */
278         public void changedUpdate(DocumentEvent e) {
279                 dirty = true;
280         }
281
282         public void insertUpdate(DocumentEvent e) {
283                 dirty = true;
284         }
285
286         public void removeUpdate(DocumentEvent e) {
287                 dirty = true;
288         }
289
290         /* Let the config code hook on a listener */
291         public void addActionListener(ActionListener l) {
292                 listener = l;
293         }
294
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);
299         }
300
301         public void set_version(String version) {
302                 version_value.setText(version);
303         }
304
305         public void set_serial(int serial) {
306                 radio_frequency_value.set_serial(serial);
307                 serial_value.setText(String.format("%d", serial));
308         }
309
310         public void set_radio_frequency(double new_radio_frequency) {
311                 int i;
312                 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
313                         AltosFrequency  f = (AltosFrequency) radio_frequency_value.getItemAt(i);
314                         
315                         if (f.close(new_radio_frequency)) {
316                                 radio_frequency_value.setSelectedIndex(i);
317                                 return;
318                         }
319                 }
320                 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
321                         AltosFrequency  f = (AltosFrequency) radio_frequency_value.getItemAt(i);
322                         
323                         if (new_radio_frequency < f.frequency)
324                                 break;
325                 }
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);
333         }
334
335         public double radio_frequency() {
336                 return radio_frequency_value.frequency();
337         }
338
339         public void set_radio_calibration(int calibration) {
340                 radio_calibration_value.setText(String.format("%d", calibration));
341         }
342
343         public void set_clean() {
344                 dirty = false;
345         }
346 }