From 6aa99c160f0695eb25ccc0598e4c36224c89dab4 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 12 Nov 2013 14:06:20 +0900 Subject: altoslib: Start moving eeprom download logic to altoslib Signed-off-by: Keith Packard --- altosui/AltosEepromMonitorUI.java | 269 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 altosui/AltosEepromMonitorUI.java (limited to 'altosui/AltosEepromMonitorUI.java') 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); + } +} -- cgit v1.2.3 From 45db3076b257adcf2c9f69ed0927f09d94af7a50 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 12 Nov 2013 14:28:30 +0900 Subject: altosui: Make AltosEepromDownload not swing-dependent Will move to altoslib Signed-off-by: Keith Packard --- altoslib/AltosEepromMonitor.java | 8 +++- altosui/AltosEepromDownload.java | 84 ++++++++++----------------------------- altosui/AltosEepromManage.java | 6 ++- altosui/AltosEepromMonitorUI.java | 60 +++++++++++++++++++++++----- 4 files changed, 82 insertions(+), 76 deletions(-) (limited to 'altosui/AltosEepromMonitorUI.java') diff --git a/altoslib/AltosEepromMonitor.java b/altoslib/AltosEepromMonitor.java index 77655903..02cbed1e 100644 --- a/altoslib/AltosEepromMonitor.java +++ b/altoslib/AltosEepromMonitor.java @@ -33,9 +33,15 @@ public interface AltosEepromMonitor { public void set_thread(Thread eeprom_thread); + final static int INFO_MESSAGE = 0; + final static int WARNING_MESSAGE = 1; + final static int ERROR_MESSAGE = 2; + + public void show_message(String message, String title, int message_type); + public void start(); - public void done(); + public void done(boolean success); public void reset(); } diff --git a/altosui/AltosEepromDownload.java b/altosui/AltosEepromDownload.java index 27bde799..6ce420d3 100644 --- a/altosui/AltosEepromDownload.java +++ b/altosui/AltosEepromDownload.java @@ -17,8 +17,6 @@ package altosui; -import java.awt.event.*; -import javax.swing.*; import java.io.*; import java.util.*; import java.text.*; @@ -27,7 +25,6 @@ import org.altusmetrum.altoslib_2.*; public class AltosEepromDownload implements Runnable { - JFrame frame; AltosSerial serial_line; boolean remote; Thread eeprom_thread; @@ -38,7 +35,6 @@ public class AltosEepromDownload implements Runnable { LinkedList eeprom_pending; AltosEepromList flights; - ActionListener listener; boolean success; ParseException parse_exception; AltosState state; @@ -190,28 +186,6 @@ public class AltosEepromDownload implements Runnable { } } - private void show_message_internal(String message, String title, int message_type) { - JOptionPane.showMessageDialog(frame, - message, - title, - message_type); - } - - private void show_message(String in_message, String in_title, int in_message_type) { - final String message = in_message; - final String title = in_title; - final int message_type = in_message_type; - Runnable r = new Runnable() { - public void run() { - try { - show_message_internal(message, title, message_type); - } catch (Exception ex) { - } - } - }; - SwingUtilities.invokeLater(r); - } - public void run () { try { boolean failed = false; @@ -230,28 +204,28 @@ public class AltosEepromDownload implements Runnable { } if (parse_exception != null) { failed = true; - show_message(String.format("Flight %d download error\n%s\nValid log data saved", - log.flight, - parse_exception.getMessage()), - serial_line.device.toShortString(), - JOptionPane.WARNING_MESSAGE); + monitor.show_message(String.format("Flight %d download error\n%s\nValid log data saved", + log.flight, + parse_exception.getMessage()), + serial_line.device.toShortString(), + AltosEepromMonitor.WARNING_MESSAGE); } } success = !failed; } catch (IOException ee) { - show_message(ee.getLocalizedMessage(), - serial_line.device.toShortString(), - JOptionPane.ERROR_MESSAGE); + monitor.show_message(ee.getLocalizedMessage(), + serial_line.device.toShortString(), + AltosEepromMonitor.ERROR_MESSAGE); } catch (InterruptedException ie) { - show_message(String.format("Connection to \"%s\" interrupted", - serial_line.device.toShortString()), - "Connection Interrupted", - JOptionPane.ERROR_MESSAGE); + monitor.show_message(String.format("Connection to \"%s\" interrupted", + serial_line.device.toShortString()), + "Connection Interrupted", + AltosEepromMonitor.ERROR_MESSAGE); } catch (TimeoutException te) { - show_message(String.format("Connection to \"%s\" failed", - serial_line.device.toShortString()), - "Connection Failed", - JOptionPane.ERROR_MESSAGE); + monitor.show_message(String.format("Connection to \"%s\" failed", + serial_line.device.toShortString()), + "Connection Failed", + AltosEepromMonitor.ERROR_MESSAGE); } finally { if (remote) { try { @@ -261,20 +235,7 @@ public class AltosEepromDownload implements Runnable { } serial_line.flush_output(); } - monitor.done(); - if (listener != null) { - Runnable r = new Runnable() { - public void run() { - try { - listener.actionPerformed(new ActionEvent(this, - success ? 1 : 0, - "download")); - } catch (Exception ex) { - } - } - }; - SwingUtilities.invokeLater(r); - } + monitor.done(success); } public void start() { @@ -282,25 +243,20 @@ public class AltosEepromDownload implements Runnable { eeprom_thread.start(); } - public void addActionListener(ActionListener l) { - listener = l; - } - - public AltosEepromDownload(JFrame given_frame, + public AltosEepromDownload(AltosEepromMonitor given_monitor, AltosSerial given_serial_line, boolean given_remote, AltosEepromList given_flights) { - frame = given_frame; + monitor = given_monitor; serial_line = given_serial_line; - serial_line.set_frame(frame); remote = given_remote; flights = given_flights; success = false; - monitor = new AltosEepromMonitorUI(given_frame); monitor.set_states(Altos.ao_flight_boost, Altos.ao_flight_landed); monitor.set_thread(eeprom_thread); + monitor.start(); } } diff --git a/altosui/AltosEepromManage.java b/altosui/AltosEepromManage.java index b2d8a130..da0a9777 100644 --- a/altosui/AltosEepromManage.java +++ b/altosui/AltosEepromManage.java @@ -133,11 +133,13 @@ public class AltosEepromManage implements ActionListener { for (AltosEepromLog flight : flights) any_selected = any_selected || flight.selected; if (any_selected) { - download = new AltosEepromDownload(frame, + AltosEepromMonitorUI monitor = new AltosEepromMonitorUI(frame); + monitor.addActionListener(this); + serial_line.set_frame(frame); + download = new AltosEepromDownload(monitor, serial_line, remote, flights); - download.addActionListener(this); /* * Start flight log download */ diff --git a/altosui/AltosEepromMonitorUI.java b/altosui/AltosEepromMonitorUI.java index ddd1a564..6ad4ca5c 100644 --- a/altosui/AltosEepromMonitorUI.java +++ b/altosui/AltosEepromMonitorUI.java @@ -24,7 +24,7 @@ import org.altusmetrum.altosuilib_1.*; import org.altusmetrum.altoslib_2.*; public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMonitor { - + JFrame owner; Container pane; Box box; JLabel serial_label; @@ -36,10 +36,13 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo JButton cancel; JProgressBar pbar; int min_state, max_state; + ActionListener listener; public AltosEepromMonitorUI(JFrame owner) { super (owner, "Download Flight Data", false); + this.owner = owner; + GridBagConstraints c; Insets il = new Insets(4,4,4,4); Insets ir = new Insets(4,4,4,4); @@ -129,6 +132,10 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo setLocationRelativeTo(owner); } + public void addActionListener(ActionListener l) { + listener = l; + } + public void set_states(int min_state, int max_state) { this.min_state = min_state; this.max_state = max_state; @@ -136,7 +143,7 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo public void set_thread(Thread in_eeprom_thread) { final Thread eeprom_thread = in_eeprom_thread; - addActionListener(new ActionListener() { + cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (eeprom_thread != null) eeprom_thread.interrupt(); @@ -148,10 +155,6 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo 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; @@ -232,16 +235,20 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo SwingUtilities.invokeLater(r); } - private void done_internal() { + private void done_internal(boolean success) { + listener.actionPerformed(new ActionEvent(this, + success ? 1 : 0, + "download")); setVisible(false); dispose(); } - public void done() { + public void done(boolean in_success) { + final boolean success = in_success; Runnable r = new Runnable() { public void run() { try { - done_internal(); + done_internal(success); } catch (Exception ex) { } } @@ -266,4 +273,39 @@ public class AltosEepromMonitorUI extends AltosUIDialog implements AltosEepromMo }; SwingUtilities.invokeLater(r); } + + private void show_message_internal(String message, String title, int message_type) { + int joption_message_type = JOptionPane.ERROR_MESSAGE; + + switch (message_type) { + case INFO_MESSAGE: + joption_message_type = JOptionPane.INFORMATION_MESSAGE; + break; + case WARNING_MESSAGE: + joption_message_type = JOptionPane.WARNING_MESSAGE; + break; + case ERROR_MESSAGE: + joption_message_type = JOptionPane.ERROR_MESSAGE; + break; + } + JOptionPane.showMessageDialog(owner, + message, + title, + joption_message_type); + } + + public void show_message(String in_message, String in_title, int in_message_type) { + final String message = in_message; + final String title = in_title; + final int message_type = in_message_type; + Runnable r = new Runnable() { + public void run() { + try { + show_message_internal(message, title, message_type); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } } -- cgit v1.2.3