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