From 6aa99c160f0695eb25ccc0598e4c36224c89dab4 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 12 Nov 2013 14:06:20 +0900 Subject: [PATCH] altoslib: Start moving eeprom download logic to altoslib Signed-off-by: Keith Packard --- altoslib/AltosEepromMonitor.java | 41 +++++ altoslib/Makefile.am | 1 + altosui/AltosEepromDownload.java | 13 +- altosui/AltosEepromMonitorUI.java | 269 ++++++++++++++++++++++++++++++ altosui/Makefile.am | 4 +- 5 files changed, 318 insertions(+), 10 deletions(-) create mode 100644 altoslib/AltosEepromMonitor.java create mode 100644 altosui/AltosEepromMonitorUI.java diff --git a/altoslib/AltosEepromMonitor.java b/altoslib/AltosEepromMonitor.java new file mode 100644 index 00000000..77655903 --- /dev/null +++ b/altoslib/AltosEepromMonitor.java @@ -0,0 +1,41 @@ +/* + * Copyright © 2013 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.altoslib_2; + +import org.altusmetrum.altosuilib_1.*; + +public interface AltosEepromMonitor { + + public void set_states(int min_state, int max_state); + + public void set_value(String in_state_name, int in_state, int in_state_block, int in_block); + + public void set_serial(int in_serial); + + public void set_flight(int in_flight); + + public void set_filename(String in_file); + + public void set_thread(Thread eeprom_thread); + + public void start(); + + public void done(); + + public void reset(); +} diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index c1cf053c..2cf7b63d 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -43,6 +43,7 @@ altoslib_JAVA = \ AltosEepromMega.java \ AltosEepromMetrum2.java \ AltosEepromMini.java \ + AltosEepromMonitor.java \ AltosFile.java \ AltosFlash.java \ AltosFlashListener.java \ diff --git a/altosui/AltosEepromDownload.java b/altosui/AltosEepromDownload.java index 7ccf26a5..27bde799 100644 --- a/altosui/AltosEepromDownload.java +++ b/altosui/AltosEepromDownload.java @@ -72,7 +72,7 @@ public class AltosEepromDownload implements Runnable { eeprom_file = new FileWriter(eeprom_name); if (eeprom_file != null) { - monitor.set_file(eeprom_name.getName()); + monitor.set_filename(eeprom_name.getName()); FlushPending(); eeprom_pending = null; } @@ -298,12 +298,9 @@ public class AltosEepromDownload implements Runnable { flights = given_flights; success = false; - monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed); - monitor.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - if (eeprom_thread != null) - eeprom_thread.interrupt(); - } - }); + monitor = new AltosEepromMonitorUI(given_frame); + monitor.set_states(Altos.ao_flight_boost, Altos.ao_flight_landed); + + monitor.set_thread(eeprom_thread); } } diff --git a/altosui/AltosEepromMonitorUI.java b/altosui/AltosEepromMonitorUI.java new file mode 100644 index 00000000..ddd1a564 --- /dev/null +++ b/altosui/AltosEepromMonitorUI.java @@ -0,0 +1,269 @@ +/* + * Copyright © 2010 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package altosui; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import org.altusmetrum.altosuilib_1.*; +import org.altusmetrum.altoslib_2.*; + +public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor { + + Container pane; + Box box; + JLabel serial_label; + JLabel flight_label; + JLabel file_label; + JLabel serial_value; + JLabel flight_value; + JLabel file_value; + JButton cancel; + JProgressBar pbar; + int min_state, max_state; + + public AltosEepromMonitorUI(JFrame owner) { + super (owner, "Download Flight Data", false); + + GridBagConstraints c; + Insets il = new Insets(4,4,4,4); + Insets ir = new Insets(4,4,4,4); + + pane = getContentPane(); + pane.setLayout(new GridBagLayout()); + + c = new GridBagConstraints(); + c.gridx = 0; c.gridy = 0; + c.fill = GridBagConstraints.NONE; + c.anchor = GridBagConstraints.LINE_START; + c.insets = il; + serial_label = new JLabel("Serial:"); + pane.add(serial_label, c); + + c = new GridBagConstraints(); + c.gridx = 1; c.gridy = 0; + c.fill = GridBagConstraints.HORIZONTAL; + c.weightx = 1; + c.anchor = GridBagConstraints.LINE_START; + c.insets = ir; + serial_value = new JLabel(""); + pane.add(serial_value, c); + + c = new GridBagConstraints(); + c.fill = GridBagConstraints.NONE; + c.gridx = 0; c.gridy = 1; + c.anchor = GridBagConstraints.LINE_START; + c.insets = il; + flight_label = new JLabel("Flight:"); + pane.add(flight_label, c); + + c = new GridBagConstraints(); + c.fill = GridBagConstraints.HORIZONTAL; + c.weightx = 1; + c.gridx = 1; c.gridy = 1; + c.anchor = GridBagConstraints.LINE_START; + c.insets = ir; + flight_value = new JLabel(""); + pane.add(flight_value, c); + + c = new GridBagConstraints(); + c.fill = GridBagConstraints.NONE; + c.gridx = 0; c.gridy = 2; + c.anchor = GridBagConstraints.LINE_START; + c.insets = il; + file_label = new JLabel("File:"); + pane.add(file_label, c); + + c = new GridBagConstraints(); + c.fill = GridBagConstraints.HORIZONTAL; + c.weightx = 1; + c.gridx = 1; c.gridy = 2; + c.anchor = GridBagConstraints.LINE_START; + c.insets = ir; + file_value = new JLabel(""); + pane.add(file_value, c); + + pbar = new JProgressBar(); + pbar.setMinimum(0); + pbar.setMaximum(1000); + pbar.setValue(0); + pbar.setString("startup"); + pbar.setStringPainted(true); + pbar.setPreferredSize(new Dimension(600, 20)); + c = new GridBagConstraints(); + c.fill = GridBagConstraints.HORIZONTAL; + c.anchor = GridBagConstraints.CENTER; + c.gridx = 0; c.gridy = 3; + c.gridwidth = GridBagConstraints.REMAINDER; + Insets ib = new Insets(4,4,4,4); + c.insets = ib; + pane.add(pbar, c); + + + cancel = new JButton("Cancel"); + c = new GridBagConstraints(); + c.fill = GridBagConstraints.NONE; + c.anchor = GridBagConstraints.CENTER; + c.gridx = 0; c.gridy = 4; + c.gridwidth = GridBagConstraints.REMAINDER; + Insets ic = new Insets(4,4,4,4); + c.insets = ic; + pane.add(cancel, c); + + pack(); + setLocationRelativeTo(owner); + } + + public void set_states(int min_state, int max_state) { + this.min_state = min_state; + this.max_state = max_state; + } + + public void set_thread(Thread in_eeprom_thread) { + final Thread eeprom_thread = in_eeprom_thread; + addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (eeprom_thread != null) + eeprom_thread.interrupt(); + } + }); + } + + public void start() { + setVisible(true); + } + + public void addActionListener (ActionListener l) { + cancel.addActionListener(l); + } + + private void set_value_internal(String state_name, int state, int state_block, int block) { + if (state_block > 100) + state_block = 100; + if (state < min_state) state = min_state; + if (state >= max_state) state = max_state - 1; + state -= min_state; + + int pos = state * 100 + state_block; + + pbar.setString(String.format("block %d state %s", block, state_name)); + pbar.setValue(pos); + } + + public void set_value(String in_state_name, int in_state, int in_state_block, int in_block) { + final String state_name = in_state_name; + final int state = in_state; + final int state_block = in_state_block; + final int block = in_block; + Runnable r = new Runnable() { + public void run() { + try { + set_value_internal(state_name, state, state_block, block); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void set_serial_internal(int serial) { + serial_value.setText(String.format("%d", serial)); + } + + public void set_serial(int in_serial) { + final int serial = in_serial; + Runnable r = new Runnable() { + public void run() { + try { + set_serial_internal(serial); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void set_flight_internal(int flight) { + flight_value.setText(String.format("%d", flight)); + } + + public void set_flight(int in_flight) { + final int flight = in_flight; + Runnable r = new Runnable() { + public void run() { + try { + set_flight_internal(flight); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void set_filename_internal(String filename) { + file_value.setText(String.format("%s", filename)); + } + + public void set_filename(String in_filename) { + final String filename = in_filename; + Runnable r = new Runnable() { + public void run() { + try { + set_filename_internal(filename); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void done_internal() { + setVisible(false); + dispose(); + } + + public void done() { + Runnable r = new Runnable() { + public void run() { + try { + done_internal(); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + private void reset_internal() { + set_value_internal("startup",min_state,0, 0); + set_flight_internal(0); + set_filename_internal(""); + } + + public void reset() { + Runnable r = new Runnable() { + public void run() { + try { + reset_internal(); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } +} diff --git a/altosui/Makefile.am b/altosui/Makefile.am index 53f4ed0b..b51f8112 100644 --- a/altosui/Makefile.am +++ b/altosui/Makefile.am @@ -1,6 +1,6 @@ JAVAROOT=classes -AM_JAVACFLAGS=-encoding UTF-8 -Xlint:deprecation +AM_JAVACFLAGS=-encoding UTF-8 -Xlint:deprecation -source 6 man_MANS=altosui.1 @@ -37,7 +37,7 @@ altosui_JAVA = \ AltosEepromDownload.java \ AltosEepromList.java \ AltosEepromManage.java \ - AltosEepromMonitor.java \ + AltosEepromMonitorUI.java \ AltosEepromSelect.java \ AltosFlashUI.java \ AltosFlightDisplay.java \ -- 2.30.2