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.*;
30 import org.altusmetrum.AltosLib.*;
32 public class AltosFlashUI
34 implements ActionListener
47 // Hex file with rom image
51 AltosDevice debug_dongle;
53 // Desired Rom configuration
54 AltosRomconfig rom_config;
59 public void actionPerformed(ActionEvent e) {
60 if (e.getSource() == cancel) {
66 String cmd = e.getActionCommand();
67 if (e.getID() == -1) {
68 JOptionPane.showMessageDialog(frame,
71 JOptionPane.ERROR_MESSAGE);
74 } else if (cmd.equals("done")) {
77 } else if (cmd.equals("start")) {
80 pbar.setValue(e.getID());
86 public void build_dialog() {
88 Insets il = new Insets(4,4,4,4);
89 Insets ir = new Insets(4,4,4,4);
91 pane = getContentPane();
92 pane.setLayout(new GridBagLayout());
94 c = new GridBagConstraints();
95 c.gridx = 0; c.gridy = 0;
96 c.fill = GridBagConstraints.NONE;
97 c.anchor = GridBagConstraints.LINE_START;
99 serial_label = new JLabel("Serial:");
100 pane.add(serial_label, c);
102 c = new GridBagConstraints();
103 c.gridx = 1; c.gridy = 0;
104 c.fill = GridBagConstraints.HORIZONTAL;
106 c.anchor = GridBagConstraints.LINE_START;
108 serial_value = new JLabel("");
109 pane.add(serial_value, c);
111 c = new GridBagConstraints();
112 c.fill = GridBagConstraints.NONE;
113 c.gridx = 0; c.gridy = 1;
114 c.anchor = GridBagConstraints.LINE_START;
116 file_label = new JLabel("File:");
117 pane.add(file_label, c);
119 c = new GridBagConstraints();
120 c.fill = GridBagConstraints.HORIZONTAL;
122 c.gridx = 1; c.gridy = 1;
123 c.anchor = GridBagConstraints.LINE_START;
125 file_value = new JLabel(file.toString());
126 pane.add(file_value, c);
128 pbar = new JProgressBar();
130 pbar.setMaximum(100);
133 pbar.setStringPainted(true);
134 pbar.setPreferredSize(new Dimension(600, 20));
135 c = new GridBagConstraints();
136 c.fill = GridBagConstraints.HORIZONTAL;
137 c.anchor = GridBagConstraints.CENTER;
138 c.gridx = 0; c.gridy = 2;
139 c.gridwidth = GridBagConstraints.REMAINDER;
140 Insets ib = new Insets(4,4,4,4);
144 cancel = new JButton("Cancel");
145 c = new GridBagConstraints();
146 c.fill = GridBagConstraints.NONE;
147 c.anchor = GridBagConstraints.CENTER;
148 c.gridx = 0; c.gridy = 3;
149 c.gridwidth = GridBagConstraints.REMAINDER;
150 Insets ic = new Insets(4,4,4,4);
153 cancel.addActionListener(this);
155 setLocationRelativeTo(frame);
158 void set_serial(int serial_number) {
159 serial_value.setText(String.format("%d", serial_number));
162 boolean select_source_file() {
163 JFileChooser hexfile_chooser = new JFileChooser();
165 File firmwaredir = AltosUIPreferences.firmwaredir();
166 if (firmwaredir != null)
167 hexfile_chooser.setCurrentDirectory(firmwaredir);
169 hexfile_chooser.setDialogTitle("Select Flash Image");
170 hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
171 int returnVal = hexfile_chooser.showOpenDialog(frame);
173 if (returnVal != JFileChooser.APPROVE_OPTION)
175 file = hexfile_chooser.getSelectedFile();
178 AltosUIPreferences.set_firmwaredir(file.getParentFile());
182 boolean select_debug_dongle() {
183 debug_dongle = AltosDeviceDialog.show(frame, Altos.product_any);
185 if (debug_dongle == null)
190 boolean update_rom_config_info(AltosRomconfig existing_config) {
191 AltosRomconfig new_config;
192 new_config = AltosRomconfigUI.show(frame, existing_config);
193 if (new_config == null)
195 rom_config = new_config;
196 set_serial(rom_config.serial_number);
201 void exception (Exception e) {
202 if (e instanceof FileNotFoundException) {
203 JOptionPane.showMessageDialog(frame,
204 ((FileNotFoundException) e).getMessage(),
206 JOptionPane.ERROR_MESSAGE);
207 } else if (e instanceof AltosSerialInUseException) {
208 JOptionPane.showMessageDialog(frame,
209 String.format("Device \"%s\" already in use",
210 debug_dongle.toShortString()),
212 JOptionPane.ERROR_MESSAGE);
213 } else if (e instanceof IOException) {
214 JOptionPane.showMessageDialog(frame,
217 JOptionPane.ERROR_MESSAGE);
221 class flash_task implements Runnable {
228 flash = new AltosFlash(ui.file, ui.debug_dongle);
229 flash.addActionListener(ui);
231 final AltosRomconfig current_config = flash.romconfig();
233 final Semaphore await_rom_config = new Semaphore(0);
234 SwingUtilities.invokeLater(new Runnable() {
237 ui.update_rom_config_info(current_config);
238 await_rom_config.release();
241 await_rom_config.acquire();
243 if (ui.rom_config != null) {
244 flash.set_romconfig(ui.rom_config);
247 } catch (InterruptedException ee) {
248 final Exception e = ee;
249 SwingUtilities.invokeLater(new Runnable() {
254 } catch (IOException ee) {
255 final Exception e = ee;
256 SwingUtilities.invokeLater(new Runnable() {
261 } catch (AltosSerialInUseException ee) {
262 final Exception e = ee;
263 SwingUtilities.invokeLater(new Runnable() {
274 public flash_task(AltosFlashUI in_ui) {
276 t = new Thread(this);
284 * Execute the steps for flashing
285 * a device. Note that this returns immediately;
286 * this dialog is not modal
289 if (!select_debug_dongle())
291 if (!select_source_file())
294 flash_task f = new flash_task(this);
297 static void show(JFrame frame) {
298 AltosFlashUI ui = new AltosFlashUI(frame);
303 public AltosFlashUI(JFrame in_frame) {
304 super(in_frame, "Program Altusmetrum Device", false);