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