further refinment of Releasing document
[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.*;
30 import org.altusmetrum.AltosLib.*;
31
32 public class AltosFlashUI
33         extends AltosDialog
34         implements ActionListener
35 {
36         Container       pane;
37         Box             box;
38         JLabel          serial_label;
39         JLabel          serial_value;
40         JLabel          file_label;
41         JLabel          file_value;
42         JProgressBar    pbar;
43         JButton         cancel;
44
45         JFrame          frame;
46
47         // Hex file with rom image
48         File            file;
49
50         // Debug connection
51         AltosDevice     debug_dongle;
52
53         // Desired Rom configuration
54         AltosRomconfig  rom_config;
55
56         // Flash controller
57         AltosFlash      flash;
58
59         public void actionPerformed(ActionEvent e) {
60                 if (e.getSource() == cancel) {
61                         if (flash != null)
62                                 flash.abort();
63                         setVisible(false);
64                         dispose();
65                 } else {
66                         String  cmd = e.getActionCommand();
67                         if (e.getID() == -1) {
68                                 JOptionPane.showMessageDialog(frame,
69                                                               e.getActionCommand(),
70                                                               file.toString(),
71                                                               JOptionPane.ERROR_MESSAGE);
72                                 setVisible(false);
73                                 dispose();
74                         } else if (cmd.equals("done")) {
75                                 setVisible(false);
76                                 dispose();
77                         } else if (cmd.equals("start")) {
78                                 setVisible(true);
79                         } else {
80                                 pbar.setValue(e.getID());
81                                 pbar.setString(cmd);
82                         }
83                 }
84         }
85
86         public void build_dialog() {
87                 GridBagConstraints c;
88                 Insets il = new Insets(4,4,4,4);
89                 Insets ir = new Insets(4,4,4,4);
90
91                 pane = getContentPane();
92                 pane.setLayout(new GridBagLayout());
93
94                 c = new GridBagConstraints();
95                 c.gridx = 0; c.gridy = 0;
96                 c.fill = GridBagConstraints.NONE;
97                 c.anchor = GridBagConstraints.LINE_START;
98                 c.insets = il;
99                 serial_label = new JLabel("Serial:");
100                 pane.add(serial_label, c);
101
102                 c = new GridBagConstraints();
103                 c.gridx = 1; c.gridy = 0;
104                 c.fill = GridBagConstraints.HORIZONTAL;
105                 c.weightx = 1;
106                 c.anchor = GridBagConstraints.LINE_START;
107                 c.insets = ir;
108                 serial_value = new JLabel("");
109                 pane.add(serial_value, c);
110
111                 c = new GridBagConstraints();
112                 c.fill = GridBagConstraints.NONE;
113                 c.gridx = 0; c.gridy = 1;
114                 c.anchor = GridBagConstraints.LINE_START;
115                 c.insets = il;
116                 file_label = new JLabel("File:");
117                 pane.add(file_label, c);
118
119                 c = new GridBagConstraints();
120                 c.fill = GridBagConstraints.HORIZONTAL;
121                 c.weightx = 1;
122                 c.gridx = 1; c.gridy = 1;
123                 c.anchor = GridBagConstraints.LINE_START;
124                 c.insets = ir;
125                 file_value = new JLabel(file.toString());
126                 pane.add(file_value, c);
127
128                 pbar = new JProgressBar();
129                 pbar.setMinimum(0);
130                 pbar.setMaximum(100);
131                 pbar.setValue(0);
132                 pbar.setString("");
133                 pbar.setStringPainted(true);
134                 pbar.setPreferredSize(new Dimension(600, 20));
135                 c = new GridBagConstraints();
136                 c.fill = GridBagConstraints.HORIZONTAL;
137                 c.anchor = GridBagConstraints.CENTER;
138                 c.gridx = 0; c.gridy = 2;
139                 c.gridwidth = GridBagConstraints.REMAINDER;
140                 Insets ib = new Insets(4,4,4,4);
141                 c.insets = ib;
142                 pane.add(pbar, c);
143
144                 cancel = new JButton("Cancel");
145                 c = new GridBagConstraints();
146                 c.fill = GridBagConstraints.NONE;
147                 c.anchor = GridBagConstraints.CENTER;
148                 c.gridx = 0; c.gridy = 3;
149                 c.gridwidth = GridBagConstraints.REMAINDER;
150                 Insets ic = new Insets(4,4,4,4);
151                 c.insets = ic;
152                 pane.add(cancel, c);
153                 cancel.addActionListener(this);
154                 pack();
155                 setLocationRelativeTo(frame);
156         }
157
158         void set_serial(int serial_number) {
159                 serial_value.setText(String.format("%d", serial_number));
160         }
161
162         boolean select_source_file() {
163                 JFileChooser    hexfile_chooser = new JFileChooser();
164
165                 File firmwaredir = AltosUIPreferences.firmwaredir();
166                 if (firmwaredir != null)
167                         hexfile_chooser.setCurrentDirectory(firmwaredir);
168
169                 hexfile_chooser.setDialogTitle("Select Flash Image");
170                 hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
171                 int returnVal = hexfile_chooser.showOpenDialog(frame);
172
173                 if (returnVal != JFileChooser.APPROVE_OPTION)
174                         return false;
175                 file = hexfile_chooser.getSelectedFile();
176                 if (file == null)
177                         return false;
178                 AltosUIPreferences.set_firmwaredir(file.getParentFile());
179                 return true;
180         }
181
182         boolean select_debug_dongle() {
183                 debug_dongle = AltosDeviceDialog.show(frame, Altos.product_any);
184
185                 if (debug_dongle == null)
186                         return false;
187                 return true;
188         }
189
190         boolean update_rom_config_info(AltosRomconfig existing_config) {
191                 AltosRomconfig  new_config;
192                 new_config = AltosRomconfigUI.show(frame, existing_config);
193                 if (new_config == null)
194                         return false;
195                 rom_config = new_config;
196                 set_serial(rom_config.serial_number);
197                 setVisible(true);
198                 return true;
199         }
200
201         void exception (Exception e) {
202                 if (e instanceof FileNotFoundException) {
203                         JOptionPane.showMessageDialog(frame,
204                                                       ((FileNotFoundException) e).getMessage(),
205                                                       "Cannot open file",
206                                                       JOptionPane.ERROR_MESSAGE);
207                 } else if (e instanceof AltosSerialInUseException) {
208                         JOptionPane.showMessageDialog(frame,
209                                                       String.format("Device \"%s\" already in use",
210                                                                     debug_dongle.toShortString()),
211                                                       "Device in use",
212                                                       JOptionPane.ERROR_MESSAGE);
213                 } else if (e instanceof IOException) {
214                         JOptionPane.showMessageDialog(frame,
215                                                       e.getMessage(),
216                                                       file.toString(),
217                                                       JOptionPane.ERROR_MESSAGE);
218                 }
219         }
220
221         class flash_task implements Runnable {
222                 AltosFlashUI    ui;
223                 Thread          t;
224                 AltosFlash      flash;
225
226                 public void run () {
227                         try {
228                                 flash = new AltosFlash(ui.file, ui.debug_dongle);
229                                 flash.addActionListener(ui);
230
231                                 final AltosRomconfig    current_config = flash.romconfig();
232
233                                 final Semaphore await_rom_config = new Semaphore(0);
234                                 SwingUtilities.invokeLater(new Runnable() {
235                                                 public void run() {
236                                                         ui.flash = flash;
237                                                         ui.update_rom_config_info(current_config);
238                                                         await_rom_config.release();
239                                                 }
240                                         });
241                                 await_rom_config.acquire();
242
243                                 if (ui.rom_config != null) {
244                                         flash.set_romconfig(ui.rom_config);
245                                         flash.flash();
246                                 }
247                         } catch (InterruptedException ee) {
248                                 final Exception e = ee;
249                                 SwingUtilities.invokeLater(new Runnable() {
250                                                 public void run() {
251                                                         ui.exception(e);
252                                                 }
253                                         });
254                         } catch (IOException ee) {
255                                 final Exception e = ee;
256                                 SwingUtilities.invokeLater(new Runnable() {
257                                                 public void run() {
258                                                         ui.exception(e);
259                                                 }
260                                         });
261                         } catch (AltosSerialInUseException ee) {
262                                 final Exception e = ee;
263                                 SwingUtilities.invokeLater(new Runnable() {
264                                                 public void run() {
265                                                         ui.exception(e);
266                                                 }
267                                         });
268                         } finally {
269                                 if (flash != null)
270                                         flash.close();
271                         }
272                 }
273
274                 public flash_task(AltosFlashUI in_ui) {
275                         ui = in_ui;
276                         t = new Thread(this);
277                         t.start();
278                 }
279         }
280
281         flash_task      flasher;
282
283         /*
284          * Execute the steps for flashing
285          * a device. Note that this returns immediately;
286          * this dialog is not modal
287          */
288         void showDialog() {
289                 if (!select_debug_dongle())
290                         return;
291                 if (!select_source_file())
292                         return;
293                 build_dialog();
294                 flash_task      f = new flash_task(this);
295         }
296
297         static void show(JFrame frame) {
298                 AltosFlashUI    ui = new AltosFlashUI(frame);
299
300                 ui.showDialog();
301         }
302
303         public AltosFlashUI(JFrame in_frame) {
304                 super(in_frame, "Program Altusmetrum Device", false);
305
306                 frame = in_frame;
307         }
308 }