altosui: Remove gratuitous threading from device flashing UI
[fw/altos] / altosui / AltosFlashUI.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 public class AltosFlashUI
32         extends JDialog
33         implements ActionListener
34 {
35         Container       pane;
36         Box             box;
37         JLabel          serial_label;
38         JLabel          serial_value;
39         JLabel          file_label;
40         JLabel          file_value;
41         JProgressBar    pbar;
42         JButton         cancel;
43
44         File            file;
45         Thread          thread;
46         JFrame          frame;
47         AltosDevice     debug_dongle;
48         AltosFlash      flash;
49
50         public void actionPerformed(ActionEvent e) {
51                 if (e.getSource() == cancel) {
52                         abort();
53                         dispose();
54                 } else {
55                         String  cmd = e.getActionCommand();
56                         if (cmd.equals("done"))
57                                 ;
58                         else if (cmd.equals("start")) {
59                                 setVisible(true);
60                         } else {
61                                 pbar.setValue(e.getID());
62                                 pbar.setString(cmd);
63                         }
64                 }
65         }
66
67         public void abort() {
68                 if (flash != null)
69                         flash.abort();
70         }
71
72         public void build_dialog() {
73                 GridBagConstraints c;
74                 Insets il = new Insets(4,4,4,4);
75                 Insets ir = new Insets(4,4,4,4);
76
77                 pane = getContentPane();
78                 pane.setLayout(new GridBagLayout());
79
80                 c = new GridBagConstraints();
81                 c.gridx = 0; c.gridy = 0;
82                 c.fill = GridBagConstraints.NONE;
83                 c.anchor = GridBagConstraints.LINE_START;
84                 c.insets = il;
85                 serial_label = new JLabel("Serial:");
86                 pane.add(serial_label, c);
87
88                 c = new GridBagConstraints();
89                 c.gridx = 1; c.gridy = 0;
90                 c.fill = GridBagConstraints.HORIZONTAL;
91                 c.weightx = 1;
92                 c.anchor = GridBagConstraints.LINE_START;
93                 c.insets = ir;
94                 serial_value = new JLabel("");
95                 pane.add(serial_value, c);
96
97                 c = new GridBagConstraints();
98                 c.fill = GridBagConstraints.NONE;
99                 c.gridx = 0; c.gridy = 1;
100                 c.anchor = GridBagConstraints.LINE_START;
101                 c.insets = il;
102                 file_label = new JLabel("File:");
103                 pane.add(file_label, c);
104
105                 c = new GridBagConstraints();
106                 c.fill = GridBagConstraints.HORIZONTAL;
107                 c.weightx = 1;
108                 c.gridx = 1; c.gridy = 1;
109                 c.anchor = GridBagConstraints.LINE_START;
110                 c.insets = ir;
111                 file_value = new JLabel("");
112                 pane.add(file_value, c);
113
114                 pbar = new JProgressBar();
115                 pbar.setMinimum(0);
116                 pbar.setMaximum(100);
117                 pbar.setValue(0);
118                 pbar.setString("");
119                 pbar.setStringPainted(true);
120                 pbar.setPreferredSize(new Dimension(600, 20));
121                 c = new GridBagConstraints();
122                 c.fill = GridBagConstraints.HORIZONTAL;
123                 c.anchor = GridBagConstraints.CENTER;
124                 c.gridx = 0; c.gridy = 2;
125                 c.gridwidth = GridBagConstraints.REMAINDER;
126                 Insets ib = new Insets(4,4,4,4);
127                 c.insets = ib;
128                 pane.add(pbar, c);
129
130                 cancel = new JButton("Cancel");
131                 c = new GridBagConstraints();
132                 c.fill = GridBagConstraints.NONE;
133                 c.anchor = GridBagConstraints.CENTER;
134                 c.gridx = 0; c.gridy = 3;
135                 c.gridwidth = GridBagConstraints.REMAINDER;
136                 Insets ic = new Insets(4,4,4,4);
137                 c.insets = ic;
138                 pane.add(cancel, c);
139                 cancel.addActionListener(this);
140                 pack();
141                 setLocationRelativeTo(frame);
142         }
143
144         public AltosFlashUI(JFrame in_frame) {
145                 super(in_frame, "Program Altusmetrum Device", false);
146
147                 frame = in_frame;
148
149                 build_dialog();
150
151                 debug_dongle = AltosDeviceDialog.show(frame, AltosDevice.product_any);
152
153                 if (debug_dongle == null)
154                         return;
155
156                 JFileChooser    hexfile_chooser = new JFileChooser();
157
158                 File firmwaredir = AltosPreferences.firmwaredir();
159                 if (firmwaredir != null)
160                         hexfile_chooser.setCurrentDirectory(firmwaredir);
161
162                 hexfile_chooser.setDialogTitle("Select Flash Image");
163                 hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
164                 int returnVal = hexfile_chooser.showOpenDialog(frame);
165
166                 if (returnVal != JFileChooser.APPROVE_OPTION)
167                         return;
168
169                 file = hexfile_chooser.getSelectedFile();
170
171                 if (file != null)
172                         AltosPreferences.set_firmwaredir(file.getParentFile());
173
174                 try {
175                         flash = new AltosFlash(file, debug_dongle);
176                         flash.addActionListener(this);
177                         AltosRomconfigUI romconfig_ui = new AltosRomconfigUI (frame);
178
179                         romconfig_ui.set(flash.romconfig());
180                         AltosRomconfig romconfig = romconfig_ui.showDialog();
181
182                         if (romconfig != null && romconfig.valid()) {
183                                 flash.set_romconfig(romconfig);
184                                 serial_value.setText(String.format("%d",
185                                                                    flash.romconfig().serial_number));
186                                 file_value.setText(file.toString());
187                                 setVisible(true);
188                                 flash.flash();
189                                 flash = null;
190                         }
191                 } catch (FileNotFoundException ee) {
192                         JOptionPane.showMessageDialog(frame,
193                                                       "Cannot open image",
194                                                       file.toString(),
195                                                       JOptionPane.ERROR_MESSAGE);
196                 } catch (AltosSerialInUseException si) {
197                         JOptionPane.showMessageDialog(frame,
198                                                       String.format("Device \"%s\" already in use",
199                                                                     debug_dongle.toShortString()),
200                                                       "Device in use",
201                                                       JOptionPane.ERROR_MESSAGE);
202                 } catch (IOException e) {
203                         JOptionPane.showMessageDialog(frame,
204                                                       e.getMessage(),
205                                                       file.toString(),
206                                                       JOptionPane.ERROR_MESSAGE);
207                 } catch (InterruptedException ie) {
208                 } finally {
209                         abort();
210                 }
211                 dispose();
212         }
213 }