2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
31 public class AltosFlashUI
33 implements ActionListener
46 // Hex file with rom image
50 AltosDevice debug_dongle;
52 // Desired Rom configuration
53 AltosRomconfig rom_config;
58 public void actionPerformed(ActionEvent e) {
59 if (e.getSource() == cancel) {
65 String cmd = e.getActionCommand();
66 if (e.getID() == -1) {
67 JOptionPane.showMessageDialog(frame,
70 JOptionPane.ERROR_MESSAGE);
73 } else if (cmd.equals("done")) {
76 } else if (cmd.equals("start")) {
79 pbar.setValue(e.getID());
85 public void build_dialog() {
87 Insets il = new Insets(4,4,4,4);
88 Insets ir = new Insets(4,4,4,4);
90 pane = getContentPane();
91 pane.setLayout(new GridBagLayout());
93 c = new GridBagConstraints();
94 c.gridx = 0; c.gridy = 0;
95 c.fill = GridBagConstraints.NONE;
96 c.anchor = GridBagConstraints.LINE_START;
98 serial_label = new JLabel("Serial:");
99 pane.add(serial_label, c);
101 c = new GridBagConstraints();
102 c.gridx = 1; c.gridy = 0;
103 c.fill = GridBagConstraints.HORIZONTAL;
105 c.anchor = GridBagConstraints.LINE_START;
107 serial_value = new JLabel("");
108 pane.add(serial_value, c);
110 c = new GridBagConstraints();
111 c.fill = GridBagConstraints.NONE;
112 c.gridx = 0; c.gridy = 1;
113 c.anchor = GridBagConstraints.LINE_START;
115 file_label = new JLabel("File:");
116 pane.add(file_label, c);
118 c = new GridBagConstraints();
119 c.fill = GridBagConstraints.HORIZONTAL;
121 c.gridx = 1; c.gridy = 1;
122 c.anchor = GridBagConstraints.LINE_START;
124 file_value = new JLabel(file.toString());
125 pane.add(file_value, c);
127 pbar = new JProgressBar();
129 pbar.setMaximum(100);
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);
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);
152 cancel.addActionListener(this);
154 setLocationRelativeTo(frame);
157 void set_serial(int serial_number) {
158 serial_value.setText(String.format("%d", serial_number));
161 boolean select_source_file() {
162 JFileChooser hexfile_chooser = new JFileChooser();
164 File firmwaredir = AltosPreferences.firmwaredir();
165 if (firmwaredir != null)
166 hexfile_chooser.setCurrentDirectory(firmwaredir);
168 hexfile_chooser.setDialogTitle("Select Flash Image");
169 hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
170 int returnVal = hexfile_chooser.showOpenDialog(frame);
172 if (returnVal != JFileChooser.APPROVE_OPTION)
174 file = hexfile_chooser.getSelectedFile();
177 AltosPreferences.set_firmwaredir(file.getParentFile());
181 boolean select_debug_dongle() {
182 debug_dongle = AltosDeviceDialog.show(frame, Altos.product_any);
184 if (debug_dongle == null)
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)
194 rom_config = new_config;
195 set_serial(rom_config.serial_number);
200 void exception (Exception e) {
201 if (e instanceof FileNotFoundException) {
202 JOptionPane.showMessageDialog(frame,
203 ((FileNotFoundException) e).getMessage(),
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()),
211 JOptionPane.ERROR_MESSAGE);
212 } else if (e instanceof IOException) {
213 JOptionPane.showMessageDialog(frame,
216 JOptionPane.ERROR_MESSAGE);
220 class flash_task implements Runnable {
227 flash = new AltosFlash(ui.file, ui.debug_dongle);
228 flash.addActionListener(ui);
230 final AltosRomconfig current_config = flash.romconfig();
232 final Semaphore await_rom_config = new Semaphore(0);
233 SwingUtilities.invokeLater(new Runnable() {
236 ui.update_rom_config_info(current_config);
237 System.out.printf("Done updating rom config info\n");
238 await_rom_config.release();
241 System.out.printf("Waiting for rom configuration updates\n");
242 await_rom_config.acquire();
243 System.out.printf("Got rom config update\n");
245 if (ui.rom_config != null) {
246 System.out.printf("rom_config not null\n");
247 flash.set_romconfig(ui.rom_config);
250 } catch (InterruptedException ee) {
251 final Exception e = ee;
252 System.out.printf("exception %s\n", e.toString());
253 SwingUtilities.invokeLater(new Runnable() {
258 } catch (IOException ee) {
259 final Exception e = ee;
260 System.out.printf("exception %s\n", e.toString());
261 SwingUtilities.invokeLater(new Runnable() {
266 } catch (AltosSerialInUseException ee) {
267 final Exception e = ee;
268 System.out.printf("exception %s\n", e.toString());
269 SwingUtilities.invokeLater(new Runnable() {
280 public flash_task(AltosFlashUI in_ui) {
282 t = new Thread(this);
290 * Execute the steps for flashing
291 * a device. Note that this returns immediately;
292 * this dialog is not modal
295 if (!select_debug_dongle())
297 if (!select_source_file())
300 flash_task f = new flash_task(this);
303 static void show(JFrame frame) {
304 AltosFlashUI ui = new AltosFlashUI(frame);
309 public AltosFlashUI(JFrame in_frame) {
310 super(in_frame, "Program Altusmetrum Device", false);