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;
25 import java.util.concurrent.*;
27 public class AltosFlashUI
29 implements ActionListener
42 // Hex file with rom image
46 AltosDevice debug_dongle;
48 // Desired Rom configuration
49 AltosRomconfig rom_config;
54 public void actionPerformed(ActionEvent e) {
55 if (e.getSource() == cancel) {
61 String cmd = e.getActionCommand();
62 if (e.getID() == -1) {
63 JOptionPane.showMessageDialog(frame,
66 JOptionPane.ERROR_MESSAGE);
69 } else if (cmd.equals("done")) {
72 } else if (cmd.equals("start")) {
75 pbar.setValue(e.getID());
81 public void build_dialog() {
83 Insets il = new Insets(4,4,4,4);
84 Insets ir = new Insets(4,4,4,4);
86 pane = getContentPane();
87 pane.setLayout(new GridBagLayout());
89 c = new GridBagConstraints();
90 c.gridx = 0; c.gridy = 0;
91 c.fill = GridBagConstraints.NONE;
92 c.anchor = GridBagConstraints.LINE_START;
94 serial_label = new JLabel("Serial:");
95 pane.add(serial_label, c);
97 c = new GridBagConstraints();
98 c.gridx = 1; c.gridy = 0;
99 c.fill = GridBagConstraints.HORIZONTAL;
101 c.anchor = GridBagConstraints.LINE_START;
103 serial_value = new JLabel("");
104 pane.add(serial_value, c);
106 c = new GridBagConstraints();
107 c.fill = GridBagConstraints.NONE;
108 c.gridx = 0; c.gridy = 1;
109 c.anchor = GridBagConstraints.LINE_START;
111 file_label = new JLabel("File:");
112 pane.add(file_label, c);
114 c = new GridBagConstraints();
115 c.fill = GridBagConstraints.HORIZONTAL;
117 c.gridx = 1; c.gridy = 1;
118 c.anchor = GridBagConstraints.LINE_START;
120 file_value = new JLabel(file.toString());
121 pane.add(file_value, c);
123 pbar = new JProgressBar();
125 pbar.setMaximum(100);
128 pbar.setStringPainted(true);
129 pbar.setPreferredSize(new Dimension(600, 20));
130 c = new GridBagConstraints();
131 c.fill = GridBagConstraints.HORIZONTAL;
132 c.anchor = GridBagConstraints.CENTER;
133 c.gridx = 0; c.gridy = 2;
134 c.gridwidth = GridBagConstraints.REMAINDER;
135 Insets ib = new Insets(4,4,4,4);
139 cancel = new JButton("Cancel");
140 c = new GridBagConstraints();
141 c.fill = GridBagConstraints.NONE;
142 c.anchor = GridBagConstraints.CENTER;
143 c.gridx = 0; c.gridy = 3;
144 c.gridwidth = GridBagConstraints.REMAINDER;
145 Insets ic = new Insets(4,4,4,4);
148 cancel.addActionListener(this);
150 setLocationRelativeTo(frame);
153 void set_serial(int serial_number) {
154 serial_value.setText(String.format("%d", serial_number));
157 boolean select_source_file() {
158 JFileChooser hexfile_chooser = new JFileChooser();
160 File firmwaredir = AltosUIPreferences.firmwaredir();
161 if (firmwaredir != null)
162 hexfile_chooser.setCurrentDirectory(firmwaredir);
164 hexfile_chooser.setDialogTitle("Select Flash Image");
165 hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
166 int returnVal = hexfile_chooser.showOpenDialog(frame);
168 if (returnVal != JFileChooser.APPROVE_OPTION)
170 file = hexfile_chooser.getSelectedFile();
173 AltosUIPreferences.set_firmwaredir(file.getParentFile());
177 boolean select_debug_dongle() {
178 debug_dongle = AltosDeviceDialog.show(frame, Altos.product_any);
180 if (debug_dongle == null)
185 boolean update_rom_config_info(AltosRomconfig existing_config) {
186 AltosRomconfig new_config;
187 new_config = AltosRomconfigUI.show(frame, existing_config);
188 if (new_config == null)
190 rom_config = new_config;
191 set_serial(rom_config.serial_number);
196 void exception (Exception e) {
197 if (e instanceof FileNotFoundException) {
198 JOptionPane.showMessageDialog(frame,
199 ((FileNotFoundException) e).getMessage(),
201 JOptionPane.ERROR_MESSAGE);
202 } else if (e instanceof AltosSerialInUseException) {
203 JOptionPane.showMessageDialog(frame,
204 String.format("Device \"%s\" already in use",
205 debug_dongle.toShortString()),
207 JOptionPane.ERROR_MESSAGE);
208 } else if (e instanceof IOException) {
209 JOptionPane.showMessageDialog(frame,
212 JOptionPane.ERROR_MESSAGE);
216 class flash_task implements Runnable {
223 flash = new AltosFlash(ui.file, ui.debug_dongle);
224 flash.addActionListener(ui);
226 final AltosRomconfig current_config = flash.romconfig();
228 final Semaphore await_rom_config = new Semaphore(0);
229 SwingUtilities.invokeLater(new Runnable() {
232 ui.update_rom_config_info(current_config);
233 await_rom_config.release();
236 await_rom_config.acquire();
238 if (ui.rom_config != null) {
239 flash.set_romconfig(ui.rom_config);
242 } catch (InterruptedException ee) {
243 final Exception e = ee;
244 SwingUtilities.invokeLater(new Runnable() {
249 } catch (IOException ee) {
250 final Exception e = ee;
251 SwingUtilities.invokeLater(new Runnable() {
256 } catch (AltosSerialInUseException ee) {
257 final Exception e = ee;
258 SwingUtilities.invokeLater(new Runnable() {
269 public flash_task(AltosFlashUI in_ui) {
271 t = new Thread(this);
279 * Execute the steps for flashing
280 * a device. Note that this returns immediately;
281 * this dialog is not modal
284 if (!select_debug_dongle())
286 if (!select_source_file())
289 flash_task f = new flash_task(this);
292 static void show(JFrame frame) {
293 AltosFlashUI ui = new AltosFlashUI(frame);
298 public AltosFlashUI(JFrame in_frame) {
299 super(in_frame, "Program Altusmetrum Device", false);