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