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.*;
26 import org.altusmetrum.altosuilib_1.*;
28 public class AltosFlashUI
30 implements ActionListener
43 // Hex file with rom image
47 AltosDevice debug_dongle;
49 // Desired Rom configuration
50 AltosRomconfig rom_config;
55 public void actionPerformed(ActionEvent e) {
56 if (e.getSource() == cancel) {
62 String cmd = e.getActionCommand();
63 if (e.getID() == -1) {
64 JOptionPane.showMessageDialog(frame,
67 JOptionPane.ERROR_MESSAGE);
70 } else if (cmd.equals("done")) {
73 } else if (cmd.equals("start")) {
76 pbar.setValue(e.getID());
82 public void build_dialog() {
84 Insets il = new Insets(4,4,4,4);
85 Insets ir = new Insets(4,4,4,4);
87 pane = getContentPane();
88 pane.setLayout(new GridBagLayout());
90 c = new GridBagConstraints();
91 c.gridx = 0; c.gridy = 0;
92 c.fill = GridBagConstraints.NONE;
93 c.anchor = GridBagConstraints.LINE_START;
95 serial_label = new JLabel("Serial:");
96 pane.add(serial_label, c);
98 c = new GridBagConstraints();
99 c.gridx = 1; c.gridy = 0;
100 c.fill = GridBagConstraints.HORIZONTAL;
102 c.anchor = GridBagConstraints.LINE_START;
104 serial_value = new JLabel("");
105 pane.add(serial_value, c);
107 c = new GridBagConstraints();
108 c.fill = GridBagConstraints.NONE;
109 c.gridx = 0; c.gridy = 1;
110 c.anchor = GridBagConstraints.LINE_START;
112 file_label = new JLabel("File:");
113 pane.add(file_label, c);
115 c = new GridBagConstraints();
116 c.fill = GridBagConstraints.HORIZONTAL;
118 c.gridx = 1; c.gridy = 1;
119 c.anchor = GridBagConstraints.LINE_START;
121 file_value = new JLabel(file.toString());
122 pane.add(file_value, c);
124 pbar = new JProgressBar();
126 pbar.setMaximum(100);
129 pbar.setStringPainted(true);
130 pbar.setPreferredSize(new Dimension(600, 20));
131 c = new GridBagConstraints();
132 c.fill = GridBagConstraints.HORIZONTAL;
133 c.anchor = GridBagConstraints.CENTER;
134 c.gridx = 0; c.gridy = 2;
135 c.gridwidth = GridBagConstraints.REMAINDER;
136 Insets ib = new Insets(4,4,4,4);
140 cancel = new JButton("Cancel");
141 c = new GridBagConstraints();
142 c.fill = GridBagConstraints.NONE;
143 c.anchor = GridBagConstraints.CENTER;
144 c.gridx = 0; c.gridy = 3;
145 c.gridwidth = GridBagConstraints.REMAINDER;
146 Insets ic = new Insets(4,4,4,4);
149 cancel.addActionListener(this);
151 setLocationRelativeTo(frame);
154 void set_serial(int serial_number) {
155 serial_value.setText(String.format("%d", serial_number));
158 boolean select_source_file() {
159 JFileChooser hexfile_chooser = new JFileChooser();
161 File firmwaredir = AltosUIPreferences.firmwaredir();
162 if (firmwaredir != null)
163 hexfile_chooser.setCurrentDirectory(firmwaredir);
165 hexfile_chooser.setDialogTitle("Select Flash Image");
166 hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
167 int returnVal = hexfile_chooser.showOpenDialog(frame);
169 if (returnVal != JFileChooser.APPROVE_OPTION)
171 file = hexfile_chooser.getSelectedFile();
174 AltosUIPreferences.set_firmwaredir(file.getParentFile());
178 boolean select_debug_dongle() {
179 debug_dongle = AltosDeviceUIDialog.show(frame, Altos.product_any);
181 if (debug_dongle == null)
186 boolean update_rom_config_info(AltosRomconfig existing_config) {
187 AltosRomconfig new_config;
188 new_config = AltosRomconfigUI.show(frame, existing_config);
189 if (new_config == null)
191 rom_config = new_config;
192 set_serial(rom_config.serial_number);
197 void exception (Exception e) {
198 if (e instanceof FileNotFoundException) {
199 JOptionPane.showMessageDialog(frame,
200 ((FileNotFoundException) e).getMessage(),
202 JOptionPane.ERROR_MESSAGE);
203 } else if (e instanceof AltosSerialInUseException) {
204 JOptionPane.showMessageDialog(frame,
205 String.format("Device \"%s\" already in use",
206 debug_dongle.toShortString()),
208 JOptionPane.ERROR_MESSAGE);
209 } else if (e instanceof IOException) {
210 JOptionPane.showMessageDialog(frame,
213 JOptionPane.ERROR_MESSAGE);
217 class flash_task implements Runnable {
224 flash = new AltosFlash(ui.file, ui.debug_dongle);
225 flash.addActionListener(ui);
227 final AltosRomconfig current_config = flash.romconfig();
229 final Semaphore await_rom_config = new Semaphore(0);
230 SwingUtilities.invokeLater(new Runnable() {
233 ui.update_rom_config_info(current_config);
234 await_rom_config.release();
237 await_rom_config.acquire();
239 if (ui.rom_config != null) {
240 flash.set_romconfig(ui.rom_config);
243 } catch (InterruptedException ee) {
244 final Exception e = ee;
245 SwingUtilities.invokeLater(new Runnable() {
250 } catch (IOException ee) {
251 final Exception e = ee;
252 SwingUtilities.invokeLater(new Runnable() {
257 } catch (AltosSerialInUseException ee) {
258 final Exception e = ee;
259 SwingUtilities.invokeLater(new Runnable() {
270 public flash_task(AltosFlashUI in_ui) {
272 t = new Thread(this);
280 * Execute the steps for flashing
281 * a device. Note that this returns immediately;
282 * this dialog is not modal
285 if (!select_debug_dongle())
287 if (!select_source_file())
290 flash_task f = new flash_task(this);
293 static void show(JFrame frame) {
294 AltosFlashUI ui = new AltosFlashUI(frame);
299 public AltosFlashUI(JFrame in_frame) {
300 super(in_frame, "Program Altusmetrum Device", false);