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