altosui: Replace device dialog. Center eeprom monitor.
[fw/altos] / ao-tools / altosui / AltosEepromMonitor.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.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 import altosui.AltosSerial;
32 import altosui.AltosSerialMonitor;
33 import altosui.AltosTelemetry;
34 import altosui.AltosState;
35 import altosui.AltosDeviceDialog;
36 import altosui.AltosPreferences;
37 import altosui.AltosLog;
38 import altosui.AltosVoice;
39
40 public class AltosEepromMonitor extends JDialog {
41
42         Container       pane;
43         Box             box;
44         JLabel          serial_label;
45         JLabel          flight_label;
46         JLabel          file_label;
47         JLabel          serial_value;
48         JLabel          flight_value;
49         JLabel          file_value;
50         JButton         cancel;
51         JProgressBar    pbar;
52         int             min_state, max_state;
53
54         public AltosEepromMonitor(JFrame owner, int in_min_state, int in_max_state) {
55                 super (owner, "Download Flight Data", false);
56
57                 GridBagConstraints c;
58                 Insets il = new Insets(4,4,4,4);
59                 Insets ir = new Insets(4,4,4,4);
60
61                 pane = getContentPane();
62                 pane.setLayout(new GridBagLayout());
63
64                 c = new GridBagConstraints();
65                 c.gridx = 0; c.gridy = 0;
66                 c.fill = GridBagConstraints.NONE;
67                 c.anchor = GridBagConstraints.LINE_START;
68                 c.insets = il;
69                 serial_label = new JLabel("Serial:");
70                 pane.add(serial_label, c);
71
72                 c = new GridBagConstraints();
73                 c.gridx = 1; c.gridy = 0;
74                 c.fill = GridBagConstraints.HORIZONTAL;
75                 c.weightx = 1;
76                 c.anchor = GridBagConstraints.LINE_START;
77                 c.insets = ir;
78                 serial_value = new JLabel("");
79                 pane.add(serial_value, c);
80
81                 c = new GridBagConstraints();
82                 c.fill = GridBagConstraints.NONE;
83                 c.gridx = 0; c.gridy = 1;
84                 c.anchor = GridBagConstraints.LINE_START;
85                 c.insets = il;
86                 flight_label = new JLabel("Flight:");
87                 pane.add(flight_label, c);
88
89                 c = new GridBagConstraints();
90                 c.fill = GridBagConstraints.HORIZONTAL;
91                 c.weightx = 1;
92                 c.gridx = 1; c.gridy = 1;
93                 c.anchor = GridBagConstraints.LINE_START;
94                 c.insets = ir;
95                 flight_value = new JLabel("");
96                 pane.add(flight_value, c);
97
98                 c = new GridBagConstraints();
99                 c.fill = GridBagConstraints.NONE;
100                 c.gridx = 0; c.gridy = 2;
101                 c.anchor = GridBagConstraints.LINE_START;
102                 c.insets = il;
103                 file_label = new JLabel("File:");
104                 pane.add(file_label, c);
105
106                 c = new GridBagConstraints();
107                 c.fill = GridBagConstraints.HORIZONTAL;
108                 c.weightx = 1;
109                 c.gridx = 1; c.gridy = 2;
110                 c.anchor = GridBagConstraints.LINE_START;
111                 c.insets = ir;
112                 file_value = new JLabel("");
113                 pane.add(file_value, c);
114
115                 min_state = in_min_state;
116                 max_state = in_max_state;
117                 pbar = new JProgressBar();
118                 pbar.setMinimum(0);
119                 pbar.setMaximum((max_state - min_state) * 100);
120                 pbar.setValue(0);
121                 pbar.setString("startup");
122                 pbar.setStringPainted(true);
123                 pbar.setPreferredSize(new Dimension(600, 20));
124                 c = new GridBagConstraints();
125                 c.fill = GridBagConstraints.HORIZONTAL;
126                 c.anchor = GridBagConstraints.CENTER;
127                 c.gridx = 0; c.gridy = 3;
128                 c.gridwidth = GridBagConstraints.REMAINDER;
129                 Insets ib = new Insets(4,4,4,4);
130                 c.insets = ib;
131                 pane.add(pbar, c);
132
133
134                 cancel = new JButton("Cancel");
135                 c = new GridBagConstraints();
136                 c.fill = GridBagConstraints.NONE;
137                 c.anchor = GridBagConstraints.CENTER;
138                 c.gridx = 0; c.gridy = 4;
139                 c.gridwidth = GridBagConstraints.REMAINDER;
140                 Insets ic = new Insets(4,4,4,4);
141                 c.insets = ic;
142                 pane.add(cancel, c);
143
144                 pack();
145                 setLocationRelativeTo(owner);
146                 setVisible(true);
147         }
148
149         public void addActionListener (ActionListener l) {
150                 cancel.addActionListener(l);
151         }
152
153         public void set_value(String state_name, int in_state, int in_block) {
154                 int block = in_block;
155                 int state = in_state;
156
157                 if (block > 100)
158                         block = 100;
159                 if (state < min_state) state = min_state;
160                 if (state >= max_state) state = max_state - 1;
161                 state -= min_state;
162
163                 int pos = state * 100 + block;
164
165                 pbar.setString(state_name);
166                 pbar.setValue(pos);
167         }
168
169         public void set_serial(int serial) {
170                 serial_value.setText(String.format("%d", serial));
171         }
172
173         public void set_flight(int flight) {
174                 flight_value.setText(String.format("%d", flight));
175         }
176
177         public void set_file(String file) {
178                 file_value.setText(String.format("%s", file));
179         }
180
181         public void done() {
182                 setVisible(false);
183                 dispose();
184         }
185 }