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