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