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.altoslib_2.*;
27 import org.altusmetrum.altosuilib_1.*;
29 public class AltosFlashUI
31 implements ActionListener
44 // Hex file with rom image
48 AltosDevice debug_dongle;
50 // Desired Rom configuration
51 AltosRomconfig rom_config;
56 public void actionPerformed(ActionEvent e) {
57 if (e.getSource() == cancel) {
63 String cmd = e.getActionCommand();
64 if (e.getID() == -1) {
65 JOptionPane.showMessageDialog(frame,
68 JOptionPane.ERROR_MESSAGE);
71 } else if (cmd.equals("done")) {
74 } else if (cmd.equals("start")) {
77 pbar.setValue(e.getID());
83 public void build_dialog() {
85 Insets il = new Insets(4,4,4,4);
86 Insets ir = new Insets(4,4,4,4);
88 pane = getContentPane();
89 pane.setLayout(new GridBagLayout());
91 c = new GridBagConstraints();
92 c.gridx = 0; c.gridy = 0;
93 c.fill = GridBagConstraints.NONE;
94 c.anchor = GridBagConstraints.LINE_START;
96 serial_label = new JLabel("Serial:");
97 pane.add(serial_label, c);
99 c = new GridBagConstraints();
100 c.gridx = 1; c.gridy = 0;
101 c.fill = GridBagConstraints.HORIZONTAL;
103 c.anchor = GridBagConstraints.LINE_START;
105 serial_value = new JLabel("");
106 pane.add(serial_value, c);
108 c = new GridBagConstraints();
109 c.fill = GridBagConstraints.NONE;
110 c.gridx = 0; c.gridy = 1;
111 c.anchor = GridBagConstraints.LINE_START;
113 file_label = new JLabel("File:");
114 pane.add(file_label, c);
116 c = new GridBagConstraints();
117 c.fill = GridBagConstraints.HORIZONTAL;
119 c.gridx = 1; c.gridy = 1;
120 c.anchor = GridBagConstraints.LINE_START;
122 file_value = new JLabel(file.toString());
123 pane.add(file_value, c);
125 pbar = new JProgressBar();
127 pbar.setMaximum(100);
130 pbar.setStringPainted(true);
131 pbar.setPreferredSize(new Dimension(600, 20));
132 c = new GridBagConstraints();
133 c.fill = GridBagConstraints.HORIZONTAL;
134 c.anchor = GridBagConstraints.CENTER;
135 c.gridx = 0; c.gridy = 2;
136 c.gridwidth = GridBagConstraints.REMAINDER;
137 Insets ib = new Insets(4,4,4,4);
141 cancel = new JButton("Cancel");
142 c = new GridBagConstraints();
143 c.fill = GridBagConstraints.NONE;
144 c.anchor = GridBagConstraints.CENTER;
145 c.gridx = 0; c.gridy = 3;
146 c.gridwidth = GridBagConstraints.REMAINDER;
147 Insets ic = new Insets(4,4,4,4);
150 cancel.addActionListener(this);
152 setLocationRelativeTo(frame);
155 void set_serial(int serial_number) {
156 serial_value.setText(String.format("%d", serial_number));
159 boolean select_source_file() {
160 JFileChooser hexfile_chooser = new JFileChooser();
162 File firmwaredir = AltosUIPreferences.firmwaredir();
163 if (firmwaredir != null)
164 hexfile_chooser.setCurrentDirectory(firmwaredir);
166 hexfile_chooser.setDialogTitle("Select Flash Image");
167 hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
168 int returnVal = hexfile_chooser.showOpenDialog(frame);
170 if (returnVal != JFileChooser.APPROVE_OPTION)
172 file = hexfile_chooser.getSelectedFile();
175 AltosUIPreferences.set_firmwaredir(file.getParentFile());
179 boolean select_debug_dongle() {
180 debug_dongle = AltosDeviceUIDialog.show(frame, Altos.product_any);
182 if (debug_dongle == null)
187 boolean update_rom_config_info(AltosRomconfig existing_config) {
188 AltosRomconfig new_config;
189 new_config = AltosRomconfigUI.show(frame, existing_config);
190 if (new_config == null)
192 rom_config = new_config;
193 set_serial(rom_config.serial_number);
198 void exception (Exception e) {
199 if (e instanceof FileNotFoundException) {
200 JOptionPane.showMessageDialog(frame,
201 ((FileNotFoundException) e).getMessage(),
203 JOptionPane.ERROR_MESSAGE);
204 } else if (e instanceof AltosSerialInUseException) {
205 JOptionPane.showMessageDialog(frame,
206 String.format("Device \"%s\" already in use",
207 debug_dongle.toShortString()),
209 JOptionPane.ERROR_MESSAGE);
210 } else if (e instanceof IOException) {
211 JOptionPane.showMessageDialog(frame,
214 JOptionPane.ERROR_MESSAGE);
218 class flash_task implements Runnable, AltosFlashListener {
223 public void position(String in_s, int in_percent) {
224 final String s = in_s;
225 final int percent = in_percent;
226 Runnable r = new Runnable() {
229 ui.actionPerformed(new ActionEvent(this,
232 } catch (Exception ex) {
236 SwingUtilities.invokeLater(r);
241 flash = new AltosFlash(ui.file, new AltosSerial(ui.debug_dongle), this);
243 final AltosRomconfig current_config = flash.romconfig();
245 final Semaphore await_rom_config = new Semaphore(0);
246 SwingUtilities.invokeLater(new Runnable() {
249 ui.update_rom_config_info(current_config);
250 await_rom_config.release();
253 await_rom_config.acquire();
255 if (ui.rom_config != null) {
256 flash.set_romconfig(ui.rom_config);
259 } catch (InterruptedException ee) {
260 final Exception e = ee;
261 SwingUtilities.invokeLater(new Runnable() {
266 } catch (IOException ee) {
267 final Exception e = ee;
268 SwingUtilities.invokeLater(new Runnable() {
273 } catch (AltosSerialInUseException ee) {
274 final Exception e = ee;
275 SwingUtilities.invokeLater(new Runnable() {
286 public flash_task(AltosFlashUI in_ui) {
288 t = new Thread(this);
296 * Execute the steps for flashing
297 * a device. Note that this returns immediately;
298 * this dialog is not modal
301 if (!select_debug_dongle())
303 if (!select_source_file())
306 flash_task f = new flash_task(this);
309 static void show(JFrame frame) {
310 AltosFlashUI ui = new AltosFlashUI(frame);
315 public AltosFlashUI(JFrame in_frame) {
316 super(in_frame, "Program Altusmetrum Device", false);