From 03496dc47372c40f7faae1766b0e729a1feeab7c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 31 Dec 2012 11:32:56 -0800 Subject: Create altosuilib to share code between altosui and micropeak Need to convert altosui to using it, but that shouldn't be hard Signed-off-by: Keith Packard --- altosuilib/AltosConfigureUI.java | 393 ++++++++++++++++++++++++++++++ altosuilib/AltosFontListener.java | 22 ++ altosuilib/AltosUIDialog.java | 59 +++++ altosuilib/AltosUIFrame.java | 82 +++++++ altosuilib/AltosUILib.java | 93 +++++++ altosuilib/AltosUIListener.java | 22 ++ altosuilib/AltosUIPreferences.java | 180 ++++++++++++++ altosuilib/AltosUIPreferencesBackend.java | 101 ++++++++ altosuilib/AltosUIVersion.java.in | 22 ++ altosuilib/AltosUnitsListener.java | 22 ++ altosuilib/Makefile.am | 41 ++++ 11 files changed, 1037 insertions(+) create mode 100644 altosuilib/AltosConfigureUI.java create mode 100644 altosuilib/AltosFontListener.java create mode 100644 altosuilib/AltosUIDialog.java create mode 100644 altosuilib/AltosUIFrame.java create mode 100644 altosuilib/AltosUILib.java create mode 100644 altosuilib/AltosUIListener.java create mode 100644 altosuilib/AltosUIPreferences.java create mode 100644 altosuilib/AltosUIPreferencesBackend.java create mode 100644 altosuilib/AltosUIVersion.java.in create mode 100644 altosuilib/AltosUnitsListener.java create mode 100644 altosuilib/Makefile.am (limited to 'altosuilib') diff --git a/altosuilib/AltosConfigureUI.java b/altosuilib/AltosConfigureUI.java new file mode 100644 index 00000000..a4b644bf --- /dev/null +++ b/altosuilib/AltosConfigureUI.java @@ -0,0 +1,393 @@ +/* + * 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 org.altusmetrum.altosuilib; + +import java.awt.*; +import java.awt.event.*; +import java.beans.*; +import javax.swing.*; +import javax.swing.event.*; + +class DelegatingRenderer implements ListCellRenderer { + + // ... + public static void install(JComboBox comboBox) { + DelegatingRenderer renderer = new DelegatingRenderer(comboBox); + renderer.initialise(); + comboBox.setRenderer(renderer); + } + + // ... + private final JComboBox comboBox; + + // ... + private ListCellRenderer delegate; + + // ... + private DelegatingRenderer(JComboBox comboBox) { + this.comboBox = comboBox; + } + + // ... + private void initialise() { + delegate = new JComboBox().getRenderer(); + comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() { + + public void propertyChange(PropertyChangeEvent evt) { + delegate = new JComboBox().getRenderer(); + } + }); + } + + // ... + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, boolean cellHasFocus) { + + return delegate.getListCellRendererComponent(list, + ((UIManager.LookAndFeelInfo) value).getName(), + index, isSelected, cellHasFocus); + } +} + +public class AltosConfigureUI + extends AltosUIDialog + implements DocumentListener +{ + JFrame owner; + Container pane; + + JRadioButton enable_voice; + JButton test_voice; + JButton close; + + JButton configure_log; + JTextField log_directory; + + JLabel callsign_label; + JTextField callsign_value; + + JRadioButton imperial_units; + + JLabel font_size_label; + JComboBox font_size_value; + + JLabel look_and_feel_label; + JComboBox look_and_feel_value; + + JRadioButton serial_debug; + + JButton manage_bluetooth; + JButton manage_frequencies; + + int row; + + final static String[] font_size_names = { "Small", "Medium", "Large" }; + + /* DocumentListener interface methods */ + public void changedUpdate(DocumentEvent e) { + if (callsign_value != null) + AltosUIPreferences.set_callsign(callsign_value.getText()); + } + + public void insertUpdate(DocumentEvent e) { + changedUpdate(e); + } + + public void removeUpdate(DocumentEvent e) { + changedUpdate(e); + } + + public GridBagConstraints constraints (int x, int width, int fill) { + GridBagConstraints c = new GridBagConstraints(); + Insets insets = new Insets(4, 4, 4, 4); + + c.insets = insets; + c.fill = fill; + if (width == 3) + c.anchor = GridBagConstraints.CENTER; + else + c.anchor = GridBagConstraints.WEST; + c.gridx = x; + c.gridwidth = width; + c.gridy = row; + return c; + } + + public GridBagConstraints constraints(int x, int width) { + return constraints(x, width, GridBagConstraints.NONE); + } + + public void add_voice() { +// GridBagConstraints c = new GridBagConstraints(); +// +// /* Voice settings */ +// c.gridx = 0; +// c.gridy = row; +// c.gridwidth = 1; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(new JLabel("Voice"), c); +// +// enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice()); +// enable_voice.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// JRadioButton item = (JRadioButton) e.getSource(); +// boolean enabled = item.isSelected(); +// AltosUIPreferences.set_voice(enabled); +// if (enabled) +// voice.speak_always("Enable voice."); +// else +// voice.speak_always("Disable voice."); +// } +// }); +// c.gridx = 1; +// c.gridy = row; +// c.gridwidth = 1; +// c.weightx = 1; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(enable_voice, c); +// enable_voice.setToolTipText("Enable/Disable all audio in-flight announcements"); +// +// c.gridx = 2; +// c.gridy = row++; +// c.gridwidth = 1; +// c.weightx = 1; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.EAST; +// test_voice = new JButton("Test Voice"); +// test_voice.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// voice.speak("That's one small step for man; one giant leap for mankind."); +// } +// }); +// pane.add(test_voice, c); +// test_voice.setToolTipText("Play a stock audio clip to check volume"); +// row++; + } + + public void add_log_dir() { + /* Log directory settings */ + pane.add(new JLabel("Log Directory"), constraints(0, 1)); + + configure_log = new JButton(AltosUIPreferences.logdir().getPath()); + configure_log.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + AltosUIPreferences.ConfigureLog(); + configure_log.setText(AltosUIPreferences.logdir().getPath()); + } + }); + pane.add(configure_log, constraints(1, 2)); + configure_log.setToolTipText("Which directory flight logs are stored in"); + row++; + } + + public void add_callsign() { +// /* Callsign setting */ +// pane.add(new JLabel("Callsign"), constraints(0, 1)); +// +// callsign_value = new JTextField(AltosUIPreferences.callsign()); +// callsign_value.getDocument().addDocumentListener(this); +// callsign_value.setToolTipText("Callsign sent in packet mode"); +// pane.add(callsign_value, constraints(1, 2, GridBagConstraints.BOTH)); +// row++; + } + + public void add_units() { + /* Imperial units setting */ + pane.add(new JLabel("Imperial Units"), constraints(0, 1)); + + imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units()); + imperial_units.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JRadioButton item = (JRadioButton) e.getSource(); + boolean enabled = item.isSelected(); + AltosUIPreferences.set_imperial_units(enabled); + } + }); + imperial_units.setToolTipText("Use Imperial units instead of metric"); + pane.add(imperial_units, constraints(1, 2)); + row++; + } + + public void add_font_size() { + /* Font size setting */ + pane.add(new JLabel("Font size"), constraints(0, 1)); + + font_size_value = new JComboBox(font_size_names); + int font_size = AltosUIPreferences.font_size(); + font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small); + font_size_value.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int size = font_size_value.getSelectedIndex() + AltosUILib.font_size_small; + + AltosUIPreferences.set_font_size(size); + } + }); + pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH)); + font_size_value.setToolTipText("Font size used in telemetry window"); + row++; + } + + public void add_look_and_feel() { + /* Look & Feel setting */ + pane.add(new JLabel("Look & feel"), constraints(0, 1)); + + /* + class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer { + + public LookAndFeelRenderer() { + super(); + } + + public Component getListCellRendererComponent( + JList list, + Object value, + int index, + boolean isSelected, + boolean cellHasFocus) + { + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + setText(((UIManager.LookAndFeelInfo) value).getName()); + return this; + } + } + */ + + final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels(); + + look_and_feel_value = new JComboBox(look_and_feels); + + DelegatingRenderer.install(look_and_feel_value); + + String look_and_feel = AltosUIPreferences.look_and_feel(); + for (int i = 0; i < look_and_feels.length; i++) + if (look_and_feel.equals(look_and_feels[i].getClassName())) + look_and_feel_value.setSelectedIndex(i); + + look_and_feel_value.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int id = look_and_feel_value.getSelectedIndex(); + + AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName()); + } + }); + pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH)); + look_and_feel_value.setToolTipText("Look&feel used for new windows"); + row++; + } + + public void add_serial_debug() { + GridBagConstraints c = new GridBagConstraints(); + + /* Serial debug setting */ + pane.add(new JLabel("Serial Debug"), constraints(0, 1)); + + serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug()); + serial_debug.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JRadioButton item = (JRadioButton) e.getSource(); + boolean enabled = item.isSelected(); + AltosUIPreferences.set_serial_debug(enabled); + } + }); + serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console"); + } + + public void add_bluetooth() { +// GridBagConstraints c = new GridBagConstraints(); +// c.gridx = 1; +// c.gridy = row++; +// c.gridwidth = 3; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(serial_debug, c); +// +// manage_bluetooth = new JButton("Manage Bluetooth"); +// manage_bluetooth.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// AltosBTManage.show(owner, AltosBTKnown.bt_known()); +// } +// }); +// c.gridx = 0; +// c.gridy = row; +// c.gridwidth = 2; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(manage_bluetooth, c); + } + + public void add_frequencies() { +// GridBagConstraints c = new GridBagConstraints(); +// manage_frequencies = new JButton("Manage Frequencies"); +// manage_frequencies.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// AltosConfigFreqUI.show(owner); +// } +// }); +// manage_frequencies.setToolTipText("Configure which values are shown in frequency menus"); +// c.gridx = 2; +// c.gridx = 2; +// c.gridy = row++; +// c.gridwidth = 2; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(manage_frequencies, c); + } + + public AltosConfigureUI(JFrame in_owner) { + super(in_owner, "Configure AltosUI", false); + + owner = in_owner; + pane = getContentPane(); + pane.setLayout(new GridBagLayout()); + + row = 0; + + /* Nice label at the top */ + pane.add(new JLabel ("Configure AltOS UI"), + constraints(0, 3)); + row++; + + pane.add(new JLabel (String.format("AltOS version %s", AltosUIVersion.version)), + constraints(0, 3)); + row++; + + add_voice(); + add_log_dir(); + add_callsign(); + add_units(); + add_font_size(); + add_look_and_feel(); + add_bluetooth(); + add_frequencies(); + + /* And a close button at the bottom */ + close = new JButton("Close"); + close.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + setVisible(false); + } + }); + pane.add(close, constraints(0, 3)); + + pack(); + setLocationRelativeTo(owner); + setVisible(true); + } +} diff --git a/altosuilib/AltosFontListener.java b/altosuilib/AltosFontListener.java new file mode 100644 index 00000000..ef543264 --- /dev/null +++ b/altosuilib/AltosFontListener.java @@ -0,0 +1,22 @@ +/* + * Copyright © 2011 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.altosuilib; + +public interface AltosFontListener { + void font_size_changed(int font_size); +} diff --git a/altosuilib/AltosUIDialog.java b/altosuilib/AltosUIDialog.java new file mode 100644 index 00000000..c0c33ba6 --- /dev/null +++ b/altosuilib/AltosUIDialog.java @@ -0,0 +1,59 @@ +/* + * Copyright © 2011 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.altosuilib; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +class AltosUIDialogListener extends WindowAdapter { + public void windowClosing (WindowEvent e) { + AltosUIPreferences.unregister_ui_listener((AltosUIDialog) e.getWindow()); + } +} + +public class AltosUIDialog extends JDialog implements AltosUIListener { + + public void ui_changed(String look_and_feel) { + SwingUtilities.updateComponentTreeUI(this); + this.pack(); + } + + public AltosUIDialog() { + AltosUIPreferences.register_ui_listener(this); + addWindowListener(new AltosUIDialogListener()); + } + + public AltosUIDialog(Frame frame, String label, boolean modal) { + super(frame, label, modal); + AltosUIPreferences.register_ui_listener(this); + addWindowListener(new AltosUIDialogListener()); + } + + public AltosUIDialog(Dialog dialog, String label, boolean modal) { + super(dialog, label, modal); + AltosUIPreferences.register_ui_listener(this); + addWindowListener(new AltosUIDialogListener()); + } + + public AltosUIDialog(Frame frame, boolean modal) { + super(frame, modal); + AltosUIPreferences.register_ui_listener(this); + addWindowListener(new AltosUIDialogListener()); + } +} diff --git a/altosuilib/AltosUIFrame.java b/altosuilib/AltosUIFrame.java new file mode 100644 index 00000000..409aea2e --- /dev/null +++ b/altosuilib/AltosUIFrame.java @@ -0,0 +1,82 @@ +/* + * Copyright © 2011 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.altosuilib; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.util.*; + +class AltosUIFrameListener extends WindowAdapter { + public void windowClosing (WindowEvent e) { + AltosUIPreferences.unregister_ui_listener((AltosUIFrame) e.getWindow()); + } +} + +public class AltosUIFrame extends JFrame implements AltosUIListener { + + public void ui_changed(String look_and_feel) { + SwingUtilities.updateComponentTreeUI(this); + this.pack(); + } + + static String[] altos_icon_names = { + "/altus-metrum-16.png", + "/altus-metrum-32.png", + "/altus-metrum-48.png", + "/altus-metrum-64.png", + "/altus-metrum-128.png", + "/altus-metrum-256.png" + }; + + static public String[] icon_names; + + static public void set_icon_names(String[] new_icon_names) { icon_names = new_icon_names; } + + public String[] icon_names() { + if (icon_names == null) + set_icon_names(altos_icon_names); + return icon_names; + } + + public void set_icon() { + ArrayList icons = new ArrayList(); + String[] icon_names = icon_names(); + + for (int i = 0; i < icon_names.length; i++) { + java.net.URL imgURL = AltosUIFrame.class.getResource(icon_names[i]); + if (imgURL != null) + icons.add(new ImageIcon(imgURL).getImage()); + } + setIconImages(icons); + } + + + public AltosUIFrame() { + AltosUIPreferences.register_ui_listener(this); + addWindowListener(new AltosUIFrameListener()); + set_icon(); + } + + public AltosUIFrame(String name) { + super(name); + AltosUIPreferences.register_ui_listener(this); + addWindowListener(new AltosUIFrameListener()); + set_icon(); + } +} diff --git a/altosuilib/AltosUILib.java b/altosuilib/AltosUILib.java new file mode 100644 index 00000000..717678ba --- /dev/null +++ b/altosuilib/AltosUILib.java @@ -0,0 +1,93 @@ +/* + * 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 org.altusmetrum.altosuilib; + +import java.awt.*; +import libaltosJNI.*; + +import org.altusmetrum.AltosLib.*; + +public class AltosUILib extends AltosLib { + + static final int tab_elt_pad = 5; + + static Font label_font; + static Font value_font; + static Font status_font; + static Font table_label_font; + static Font table_value_font; + + final static int font_size_small = 1; + final static int font_size_medium = 2; + final static int font_size_large = 3; + + static void set_fonts(int size) { + int brief_size; + int table_size; + int status_size; + + switch (size) { + case font_size_small: + brief_size = 16; + status_size = 18; + table_size = 11; + break; + default: + case font_size_medium: + brief_size = 22; + status_size = 24; + table_size = 14; + break; + case font_size_large: + brief_size = 26; + status_size = 30; + table_size = 17; + break; + } + label_font = new Font("Dialog", Font.PLAIN, brief_size); + value_font = new Font("Monospaced", Font.PLAIN, brief_size); + status_font = new Font("SansSerif", Font.BOLD, status_size); + table_label_font = new Font("SansSerif", Font.PLAIN, table_size); + table_value_font = new Font("Monospaced", Font.PLAIN, table_size); + } + + static final int text_width = 20; + + static public boolean initialized = false; + static public boolean loaded_library = false; + + public static boolean load_library() { + if (!initialized) { + try { + System.loadLibrary("altos"); + libaltos.altos_init(); + loaded_library = true; + } catch (UnsatisfiedLinkError e) { + try { + System.loadLibrary("altos64"); + libaltos.altos_init(); + loaded_library = true; + } catch (UnsatisfiedLinkError e2) { + loaded_library = false; + } + } + initialized = true; + } + return loaded_library; + } +} diff --git a/altosuilib/AltosUIListener.java b/altosuilib/AltosUIListener.java new file mode 100644 index 00000000..f4127f58 --- /dev/null +++ b/altosuilib/AltosUIListener.java @@ -0,0 +1,22 @@ +/* + * Copyright © 2011 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.altosuilib; + +public interface AltosUIListener { + public void ui_changed(String look_and_feel); +} diff --git a/altosuilib/AltosUIPreferences.java b/altosuilib/AltosUIPreferences.java new file mode 100644 index 00000000..485cb582 --- /dev/null +++ b/altosuilib/AltosUIPreferences.java @@ -0,0 +1,180 @@ +/* + * Copyright © 2011 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.altosuilib; + +import java.io.*; +import java.util.*; +import java.awt.Component; +import javax.swing.*; +import org.altusmetrum.AltosLib.*; + +public class AltosUIPreferences extends AltosPreferences { + + /* font size preferences name */ + final static String fontSizePreference = "FONT-SIZE"; + + /* Look&Feel preference name */ + final static String lookAndFeelPreference = "LOOK-AND-FEEL"; + + /* UI Component to pop dialogs up */ + static Component component; + + static LinkedList font_listeners; + + static int font_size = AltosUILib.font_size_medium; + + static LinkedList ui_listeners; + + static String look_and_feel = null; + + /* Serial debug */ + public static boolean serial_debug; + + public static void init() { + AltosPreferences.init(new AltosUIPreferencesBackend()); + + font_listeners = new LinkedList(); + + font_size = backend.getInt(fontSizePreference, AltosUILib.font_size_medium); + AltosUILib.set_fonts(font_size); + look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName()); + + ui_listeners = new LinkedList(); + serial_debug = backend.getBoolean(serialDebugPreference, false); + AltosLink.set_debug(serial_debug); + } + + static { init(); } + + public static void set_component(Component in_component) { + component = in_component; + } + + private static boolean check_dir(File dir) { + if (!dir.exists()) { + if (!dir.mkdirs()) { + JOptionPane.showMessageDialog(component, + dir.getName(), + "Cannot create directory", + JOptionPane.ERROR_MESSAGE); + return false; + } + } else if (!dir.isDirectory()) { + JOptionPane.showMessageDialog(component, + dir.getName(), + "Is not a directory", + JOptionPane.ERROR_MESSAGE); + return false; + } + return true; + } + + /* Configure the log directory. This is where all telemetry and eeprom files + * will be written to, and where replay will look for telemetry files + */ + public static void ConfigureLog() { + JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile()); + + logdir_chooser.setDialogTitle("Configure Data Logging Directory"); + logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + + if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) { + File dir = logdir_chooser.getSelectedFile(); + if (check_dir(dir)) + set_logdir(dir); + } + } + public static int font_size() { + synchronized (backend) { + return font_size; + } + } + + static void set_fonts() { + } + + public static void set_font_size(int new_font_size) { + synchronized (backend) { + font_size = new_font_size; + backend.putInt(fontSizePreference, font_size); + flush_preferences(); + AltosUILib.set_fonts(font_size); + for (AltosFontListener l : font_listeners) + l.font_size_changed(font_size); + } + } + + public static void register_font_listener(AltosFontListener l) { + synchronized (backend) { + font_listeners.add(l); + } + } + + public static void unregister_font_listener(AltosFontListener l) { + synchronized (backend) { + font_listeners.remove(l); + } + } + + public static void set_look_and_feel(String new_look_and_feel) { + try { + UIManager.setLookAndFeel(new_look_and_feel); + } catch (Exception e) { + } + synchronized(backend) { + look_and_feel = new_look_and_feel; + backend.putString(lookAndFeelPreference, look_and_feel); + flush_preferences(); + for (AltosUIListener l : ui_listeners) + l.ui_changed(look_and_feel); + } + } + + public static String look_and_feel() { + synchronized (backend) { + return look_and_feel; + } + } + + public static void register_ui_listener(AltosUIListener l) { + synchronized(backend) { + ui_listeners.add(l); + } + } + + public static void unregister_ui_listener(AltosUIListener l) { + synchronized (backend) { + ui_listeners.remove(l); + } + } + public static void set_serial_debug(boolean new_serial_debug) { + AltosLink.set_debug(new_serial_debug); + synchronized (backend) { + serial_debug = new_serial_debug; + backend.putBoolean(serialDebugPreference, serial_debug); + flush_preferences(); + } + } + + public static boolean serial_debug() { + synchronized (backend) { + return serial_debug; + } + } + +} diff --git a/altosuilib/AltosUIPreferencesBackend.java b/altosuilib/AltosUIPreferencesBackend.java new file mode 100644 index 00000000..c6c05e55 --- /dev/null +++ b/altosuilib/AltosUIPreferencesBackend.java @@ -0,0 +1,101 @@ +/* + * Copyright © 2012 Mike Beattie + * + * 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.altosuilib; + +import java.io.File; +import java.util.prefs.*; +import org.altusmetrum.AltosLib.*; +import javax.swing.filechooser.FileSystemView; + +public class AltosUIPreferencesBackend implements AltosPreferencesBackend { + + private Preferences _preferences = null; + + public AltosUIPreferencesBackend() { + _preferences = Preferences.userRoot().node("/org/altusmetrum/altosui"); + } + + public AltosUIPreferencesBackend(Preferences in_preferences) { + _preferences = in_preferences; + } + + public String getString(String key, String def) { + return _preferences.get(key, def); + } + public void putString(String key, String value) { + _preferences.put(key, value); + } + + public int getInt(String key, int def) { + return _preferences.getInt(key, def); + } + public void putInt(String key, int value) { + _preferences.putInt(key, value); + } + + public double getDouble(String key, double def) { + return _preferences.getDouble(key, def); + } + public void putDouble(String key, double value) { + _preferences.putDouble(key, value); + } + + public boolean getBoolean(String key, boolean def) { + return _preferences.getBoolean(key, def); + } + public void putBoolean(String key, boolean value) { + _preferences.putBoolean(key, value); + } + + public boolean nodeExists(String key) { + try { + return _preferences.nodeExists(key); + } catch (BackingStoreException be) { + return false; + } + } + + public AltosPreferencesBackend node(String key) { + return new AltosUIPreferencesBackend(_preferences.node(key)); + } + + public String[] keys() { + try { + return _preferences.keys(); + } catch (BackingStoreException be) { + return null; + } + } + + public void remove(String key) { + _preferences.remove(key); + } + + public void flush() { + try { + _preferences.flush(); + } catch (BackingStoreException ee) { + System.err.printf("Cannot save preferences\n"); + } + } + + public File homeDirectory() { + /* Use the file system view default directory */ + return FileSystemView.getFileSystemView().getDefaultDirectory(); + } +} diff --git a/altosuilib/AltosUIVersion.java.in b/altosuilib/AltosUIVersion.java.in new file mode 100644 index 00000000..6fb3b38b --- /dev/null +++ b/altosuilib/AltosUIVersion.java.in @@ -0,0 +1,22 @@ +/* + * Copyright © 2011 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.altosuilib; + +public class AltosUIVersion { + public final static String version = "@VERSION@"; +} diff --git a/altosuilib/AltosUnitsListener.java b/altosuilib/AltosUnitsListener.java new file mode 100644 index 00000000..22c66cd4 --- /dev/null +++ b/altosuilib/AltosUnitsListener.java @@ -0,0 +1,22 @@ +/* + * Copyright © 2012 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.altosuilib; + +public interface AltosUnitsListener { + public void units_changed(); +} diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am new file mode 100644 index 00000000..26aee7c4 --- /dev/null +++ b/altosuilib/Makefile.am @@ -0,0 +1,41 @@ +AM_JAVACFLAGS=-encoding UTF-8 -Xlint:deprecation + +JAVAROOT=bin + +CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH="bin:../altoslib/*:../libaltos:$(FREETTS)/*:/usr/share/java/*" + +SRC=. +BIN=bin/org/altusmetrum/AltosUILib + +AltosUILibdir = $(datadir)/java + +AltosUILib_JAVA = \ + AltosConfigureUI.java \ + AltosFontListener.java \ + AltosUIDialog.java \ + AltosUIFrame.java \ + AltosUILib.java \ + AltosUIListener.java \ + AltosUIPreferencesBackend.java \ + AltosUIPreferences.java \ + AltosUIVersion.java \ + AltosUnitsListener.java + +JAR=AltosUILib.jar + +all-local: $(JAR) + +clean-local: + -rm -rf bin $(JAR) + +install-AltosUILibJAVA: $(JAR) + @$(NORMAL_INSTALL) + test -z "$(AltosUILibdir)" || $(MKDIR_P) "$(DESTDIR)$(AltosUILibdir)" + echo " $(INSTALL_DATA)" "$<" "'$(DESTDIR)$(AltosUILibdir)/$(JAR)"; \ + $(INSTALL_DATA) "$<" "$(DESTDIR)$(AltosUILibdir)" + +bin: + mkdir -p bin + +$(JAR): classAltosUILib.stamp + jar cf $@ -C bin org -- cgit v1.2.3 From 2bd6aca54fc465995d6985c8799cd0d016c9a543 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 31 Dec 2012 14:17:26 -0800 Subject: micropeak: Add flight stats pane Shows graph or stats in alternate panes Signed-off-by: Keith Packard --- altosuilib/AltosUILib.java | 18 ++-- micropeak/Makefile.am | 2 + micropeak/MicroData.java | 100 ++++++++++++++++++++++ micropeak/MicroPeak.java | 15 +++- micropeak/MicroStats.java | 184 +++++++++++++++++++++++++++++++++++++++++ micropeak/MicroStatsTable.java | 138 +++++++++++++++++++++++++++++++ 6 files changed, 445 insertions(+), 12 deletions(-) create mode 100644 micropeak/MicroStats.java create mode 100644 micropeak/MicroStatsTable.java (limited to 'altosuilib') diff --git a/altosuilib/AltosUILib.java b/altosuilib/AltosUILib.java index 717678ba..5d5f9aaa 100644 --- a/altosuilib/AltosUILib.java +++ b/altosuilib/AltosUILib.java @@ -24,17 +24,17 @@ import org.altusmetrum.AltosLib.*; public class AltosUILib extends AltosLib { - static final int tab_elt_pad = 5; + public static final int tab_elt_pad = 5; - static Font label_font; - static Font value_font; - static Font status_font; - static Font table_label_font; - static Font table_value_font; + public static Font label_font; + public static Font value_font; + public static Font status_font; + public static Font table_label_font; + public static Font table_value_font; - final static int font_size_small = 1; - final static int font_size_medium = 2; - final static int font_size_large = 3; + final public static int font_size_small = 1; + final public static int font_size_medium = 2; + final public static int font_size_large = 3; static void set_fonts(int size) { int brief_size; diff --git a/micropeak/Makefile.am b/micropeak/Makefile.am index fde981a6..e0de690c 100644 --- a/micropeak/Makefile.am +++ b/micropeak/Makefile.am @@ -13,6 +13,8 @@ micropeak_JAVA= \ MicroFrame.java \ MicroGraph.java \ MicroSerial.java \ + MicroStats.java \ + MicroStatsTable.java \ MicroFileChooser.java \ MicroUSB.java diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index 783ae40f..ec9b83d8 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -22,6 +22,87 @@ import java.io.*; import java.util.*; import org.altusmetrum.AltosLib.*; +abstract class MicroIterator implements Iterator { + int i; + MicroData data; + + public boolean hasNext() { + return i < data.pressures.length; + } + + public MicroIterator (MicroData data) { + this.data = data; + i = 0; + } + + public void remove() { + } +} + +class MicroHeightIterator extends MicroIterator { + public Double next() { + return data.height(i++); + } + + public MicroHeightIterator(MicroData data) { + super(data); + } +} + +class MicroHeightIterable implements Iterable { + MicroData data; + + public Iterator iterator() { + return new MicroHeightIterator(data); + } + + public MicroHeightIterable(MicroData data) { + this.data = data; + } +} + +class MicroSpeedIterator extends MicroIterator { + public Double next() { + return data.speed(i++); + } + public MicroSpeedIterator(MicroData data) { + super(data); + } +} + +class MicroSpeedIterable implements Iterable { + MicroData data; + + public Iterator iterator() { + return new MicroSpeedIterator(data); + } + + public MicroSpeedIterable(MicroData data) { + this.data = data; + } +} + +class MicroAccelIterator extends MicroIterator { + public Double next() { + return data.acceleration(i++); + } + public MicroAccelIterator(MicroData data) { + super(data); + } +} + +class MicroAccelIterable implements Iterable { + MicroData data; + + public Iterator iterator() { + return new MicroAccelIterator(data); + } + + public MicroAccelIterable(MicroData data) { + this.data = data; + } +} + public class MicroData { public int ground_pressure; public int min_pressure; @@ -143,6 +224,18 @@ public class MicroData { return AltosConvert.pressure_to_altitude(pressures[i]); } + public Iterable heights() { + return new MicroHeightIterable(this); + } + + public Iterable speeds() { + return new MicroSpeedIterable(this); + } + + public Iterable accels() { + return new MicroAccelIterable(this); + } + int fact(int n) { if (n == 0) return 1; @@ -266,5 +359,12 @@ public class MicroData { throw new IOException(); } } + + public MicroData() { + ground_pressure = 101000; + min_pressure = 101000; + pressures = new int[1]; + pressures[0] = 101000; + } } diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index 463238c8..c69f7167 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -30,13 +30,16 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene File filename; MicroGraph graph; + MicroStatsTable stats; MicroData data; - Container pane; + Container container; + JTabbedPane pane; private void RunFile(InputStream input) { try { data = new MicroData(input); graph.setData(data); + stats.setData(data); } catch (IOException ioe) { } try { @@ -90,7 +93,8 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene AltosUIPreferences.set_component(this); - pane = getContentPane(); + container = getContentPane(); + pane = new JTabbedPane(); setTitle("MicroPeak"); @@ -129,9 +133,14 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene }); graph = new MicroGraph(); - pane.add(graph.panel); + stats = new MicroStatsTable(); + pane.add(graph.panel, "Graph"); + pane.add(stats, "Statistics"); pane.doLayout(); pane.validate(); + container.add(pane); + container.doLayout(); + container.validate(); doLayout(); validate(); Insets i = getInsets(); diff --git a/micropeak/MicroStats.java b/micropeak/MicroStats.java new file mode 100644 index 00000000..6ae8a2b2 --- /dev/null +++ b/micropeak/MicroStats.java @@ -0,0 +1,184 @@ +/* + * Copyright © 2011 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.micropeak; + +import java.io.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroStats { + double coast_height; + double coast_time; + + double apogee_height; + double apogee_time; + + double landed_height; + double landed_time; + + double max_speed; + double max_accel; + + MicroData data; + + void find_landing() { + landed_height = 0; + + int t = 0; + for (double height : data.heights()) { + landed_height = height; + t++; + } + landed_time = data.time(t); + + t = 0; + boolean above = false; + for (double height : data.heights()) { + if (height > landed_height + 10) { + above = true; + } else { + if (above && height < landed_height + 2) { + above = false; + landed_time = data.time(t); + } + } + t++; + } + } + + void find_apogee() { + apogee_height = 0; + apogee_time = 0; + + int t = 0; + for (double height : data.heights()) { + if (height > apogee_height) { + apogee_height = height; + apogee_time = data.time(t); + } + t++; + } + } + + void find_coast() { + coast_height = 0; + coast_time = 0; + + int t = 0; + for (double accel : data.accels()) { + if (accel < -9.8) + break; + t++; + } + coast_time = data.time(t); + + int coast_t = t; + t = 0; + for (double height : data.heights()) { + if (t >= coast_t) { + coast_height = height; + break; + } + t++; + } + } + + void find_max_speed() { + max_speed = 0; + int t = 0; + for (double speed : data.speeds()) { + if (data.time(t) > apogee_time) + break; + if (speed > max_speed) + max_speed = speed; + t++; + } + } + + void find_max_accel() { + max_accel = 0; + + int t = 0; + for (double accel : data.accels()) { + if (data.time(t) > apogee_time) + break; + if (accel > max_accel) + max_accel = accel; + t++; + } + } + + double boost_duration() { + return coast_time; + } + + double boost_height() { + return coast_height; + } + + double boost_speed() { + return coast_height / coast_time; + } + + double boost_accel() { + return boost_speed() / boost_duration(); + } + + double coast_duration() { + return apogee_time - coast_time; + } + + double coast_height() { + return apogee_height - coast_height; + } + + double coast_speed() { + return coast_height() / coast_duration(); + } + + double coast_accel() { + return coast_speed() / coast_duration(); + } + + double descent_duration() { + return landed_time - apogee_time; + } + + double descent_height() { + return apogee_height - landed_height; + } + + double descent_speed() { + return descent_height() / descent_duration(); + } + + public MicroStats(MicroData data) { + + this.data = data; + + find_coast(); + find_apogee(); + find_landing(); + find_max_speed(); + find_max_accel(); + } + + public MicroStats() { + this(new MicroData()); + } +} diff --git a/micropeak/MicroStatsTable.java b/micropeak/MicroStatsTable.java new file mode 100644 index 00000000..f373e25d --- /dev/null +++ b/micropeak/MicroStatsTable.java @@ -0,0 +1,138 @@ +/* + * Copyright © 2011 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.micropeak; + +import java.awt.*; +import javax.swing.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroStatsTable extends JComponent { + GridBagLayout layout; + + class MicroStat { + JLabel label; + JTextField[] texts; + + public void set_values(String ... values) { + for (int j = 0; j < values.length; j++) { + texts[j].setText(values[j]); + } + } + + public MicroStat(GridBagLayout layout, int y, String label_text, String ... values) { + GridBagConstraints c = new GridBagConstraints(); + c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad); + c.weighty = 1; + + label = new JLabel(label_text); + label.setFont(AltosUILib.label_font); + label.setHorizontalAlignment(SwingConstants.LEFT); + c.gridx = 0; c.gridy = y; + c.anchor = GridBagConstraints.WEST; + c.fill = GridBagConstraints.VERTICAL; + c.weightx = 0; + layout.setConstraints(label, c); + add(label); + + texts = new JTextField[values.length]; + for (int j = 0; j < values.length; j++) { + JTextField value = new JTextField(values[j]); + value.setFont(AltosUILib.value_font); + value.setHorizontalAlignment(SwingConstants.RIGHT); + texts[j] = value; + c.gridx = j+1; c.gridy = y; + c.anchor = GridBagConstraints.EAST; + c.fill = GridBagConstraints.BOTH; + c.weightx = 1; + layout.setConstraints(value, c); + add(value); + } + } + } + + MicroStat max_height, max_speed; + MicroStat max_accel, avg_accel; + MicroStat boost_duration; + MicroStat coast_duration; + MicroStat descent_speed; + MicroStat descent_duration; + MicroStat flight_time; + + public void setStats(MicroStats stats) { + max_height.set_values(String.format("%5.0f m", stats.apogee_height), + String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.apogee_height))); + max_speed.set_values(String.format("%5.0f m/s", stats.max_speed), + String.format("%5.0f mph", AltosConvert.meters_to_mph(stats.max_speed)), + String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed))); + max_accel.set_values(String.format("%5.0f m/s²", stats.max_accel), + String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)), + String.format("%5.0f G", AltosConvert.meters_to_g(stats.max_accel))); + avg_accel.set_values(String.format("%5.0f m/s²", stats.boost_accel(), + String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())), + String.format("%5.0f G", AltosConvert.meters_to_g(stats.boost_accel())))); + boost_duration.set_values(String.format("%6.1f s", stats.boost_duration())); + coast_duration.set_values(String.format("%6.1f s", stats.coast_duration())); + descent_speed.set_values(String.format("%5.0f m/s", stats.descent_speed()), + String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.descent_speed()))); + descent_duration.set_values(String.format("%6.1f s", stats.descent_duration())); + flight_time.set_values(String.format("%6.1f s", stats.landed_time)); + } + + public void setData(MicroData data) { + setStats(new MicroStats(data)); + } + + public MicroStatsTable(MicroStats stats) { + layout = new GridBagLayout(); + + setLayout(layout); + int y = 0; + max_height = new MicroStat(layout, y++, "Maximum height", + String.format("%5.0f m", stats.apogee_height), + String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.apogee_height))); + max_speed = new MicroStat(layout, y++, "Maximum speed", + String.format("%5.0f m/s", stats.max_speed), + String.format("%5.0f mph", AltosConvert.meters_to_mph(stats.max_speed)), + String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed))); + max_accel = new MicroStat(layout, y++, "Maximum boost acceleration", + String.format("%5.0f m/s²", stats.max_accel), + String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)), + String.format("%5.0f G", AltosConvert.meters_to_g(stats.max_accel))); + avg_accel = new MicroStat(layout, y++, "Average boost acceleration", + String.format("%5.0f m/s²", stats.boost_accel(), + String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())), + String.format("%5.0f G", AltosConvert.meters_to_g(stats.boost_accel())))); + boost_duration = new MicroStat(layout, y++, "Boost duration", + String.format("%6.0f s", stats.boost_duration())); + coast_duration = new MicroStat(layout, y++, "Coast duration", + String.format("%6.1f s", stats.coast_duration())); + descent_speed = new MicroStat(layout, y++, "Descent rate", + String.format("%5.0f m/s", stats.descent_speed()), + String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.descent_speed()))); + descent_duration = new MicroStat(layout, y++, "Descent duration", + String.format("%6.1f s", stats.descent_duration())); + flight_time = new MicroStat(layout, y++, "Flight Time", + String.format("%6.0f s", stats.landed_time)); + } + + public MicroStatsTable() { + this(new MicroStats()); + } + +} \ No newline at end of file -- cgit v1.2.3 From d83587c3c66b730cc54ca153714eee520ee40b2c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 1 Jan 2013 15:30:11 -0800 Subject: micropeak is code complete now. Added save and download functionality. Removed 'new' from file menu. Signed-off-by: Keith Packard --- altoslib/AltosLib.java | 1 + altosuilib/AltosDevice.java | 30 +++++++ altosuilib/AltosDeviceDialog.java | 163 ++++++++++++++++++++++++++++++++++++++ altosuilib/AltosUSBDevice.java | 112 ++++++++++++++++++++++++++ altosuilib/Makefile.am | 3 + libaltos/libaltos.c | 4 + micropeak/Makefile.am | 3 + micropeak/MicroData.java | 15 +++- micropeak/MicroDeviceDialog.java | 50 ++++++++++++ micropeak/MicroDownload.java | 139 ++++++++++++++++++++++++++++++++ micropeak/MicroGraph.java | 5 ++ micropeak/MicroPeak.java | 81 +++++++++++++++---- micropeak/MicroSave.java | 102 ++++++++++++++++++++++++ micropeak/MicroSerial.java | 12 ++- micropeak/MicroUSB.java | 19 +++-- 15 files changed, 708 insertions(+), 31 deletions(-) create mode 100644 altosuilib/AltosDevice.java create mode 100644 altosuilib/AltosDeviceDialog.java create mode 100644 altosuilib/AltosUSBDevice.java create mode 100644 micropeak/MicroDeviceDialog.java create mode 100644 micropeak/MicroDownload.java create mode 100644 micropeak/MicroSave.java (limited to 'altosuilib') diff --git a/altoslib/AltosLib.java b/altoslib/AltosLib.java index 07516aeb..67138450 100644 --- a/altoslib/AltosLib.java +++ b/altoslib/AltosLib.java @@ -97,6 +97,7 @@ public class AltosLib { public final static int product_any = 0x10000; public final static int product_basestation = 0x10000 + 1; public final static int product_altimeter = 0x10000 + 2; + public final static int product_micropeak_serial = 0x10000 + 3; /* Bluetooth "identifier" (bluetooth sucks) */ public final static String bt_product_telebt = "TeleBT"; diff --git a/altosuilib/AltosDevice.java b/altosuilib/AltosDevice.java new file mode 100644 index 00000000..69b025ba --- /dev/null +++ b/altosuilib/AltosDevice.java @@ -0,0 +1,30 @@ +/* + * Copyright © 2011 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.altosuilib; + +import libaltosJNI.*; + +public interface AltosDevice { + public abstract String toString(); + public abstract String toShortString(); + public abstract int getSerial(); + public abstract String getPath(); + public abstract boolean matchProduct(int product); + public abstract String getErrorString(); + public SWIGTYPE_p_altos_file open(); +} diff --git a/altosuilib/AltosDeviceDialog.java b/altosuilib/AltosDeviceDialog.java new file mode 100644 index 00000000..82620b8b --- /dev/null +++ b/altosuilib/AltosDeviceDialog.java @@ -0,0 +1,163 @@ +/* + * 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 org.altusmetrum.altosuilib; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + +public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionListener { + + private AltosDevice value; + private JList list; + private JButton cancel_button; + private JButton select_button; + private JButton manage_bluetooth_button; + private Frame frame; + private int product; + + public AltosDevice getValue() { + return value; + } + + public abstract AltosDevice[] devices(); + + private void update_devices() { + AltosDevice[] devices = devices(); + list.setListData(devices); + select_button.setEnabled(devices.length > 0); + } + + public AltosDeviceDialog (Frame in_frame, Component location, int in_product) { + super(in_frame, "Device Selection", true); + + product = in_product; + frame = in_frame; + value = null; + + AltosDevice[] devices = devices(); + + cancel_button = new JButton("Cancel"); + cancel_button.setActionCommand("cancel"); + cancel_button.addActionListener(this); + +// manage_bluetooth_button = new JButton("Manage Bluetooth"); +// manage_bluetooth_button.setActionCommand("manage"); +// manage_bluetooth_button.addActionListener(this); + + select_button = new JButton("Select"); + select_button.setActionCommand("select"); + select_button.addActionListener(this); + if (devices.length == 0) + select_button.setEnabled(false); + getRootPane().setDefaultButton(select_button); + + list = new JList(devices) { + //Subclass JList to workaround bug 4832765, which can cause the + //scroll pane to not let the user easily scroll up to the beginning + //of the list. An alternative would be to set the unitIncrement + //of the JScrollBar to a fixed value. You wouldn't get the nice + //aligned scrolling, but it should work. + public int getScrollableUnitIncrement(Rectangle visibleRect, + int orientation, + int direction) { + int row; + if (orientation == SwingConstants.VERTICAL && + direction < 0 && (row = getFirstVisibleIndex()) != -1) { + Rectangle r = getCellBounds(row, row); + if ((r.y == visibleRect.y) && (row != 0)) { + Point loc = r.getLocation(); + loc.y--; + int prevIndex = locationToIndex(loc); + Rectangle prevR = getCellBounds(prevIndex, prevIndex); + + if (prevR == null || prevR.y >= r.y) { + return 0; + } + return prevR.height; + } + } + return super.getScrollableUnitIncrement( + visibleRect, orientation, direction); + } + }; + + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + list.setLayoutOrientation(JList.HORIZONTAL_WRAP); + list.setVisibleRowCount(-1); + list.addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + select_button.doClick(); //emulate button click + } + } + }); + JScrollPane listScroller = new JScrollPane(list); + listScroller.setPreferredSize(new Dimension(400, 80)); + listScroller.setAlignmentX(LEFT_ALIGNMENT); + + //Create a container so that we can add a title around + //the scroll pane. Can't add a title directly to the + //scroll pane because its background would be white. + //Lay out the label and scroll pane from top to bottom. + JPanel listPane = new JPanel(); + listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS)); + + JLabel label = new JLabel("Select Device"); + label.setLabelFor(list); + listPane.add(label); + listPane.add(Box.createRigidArea(new Dimension(0,5))); + listPane.add(listScroller); + listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); + + //Lay out the buttons from left to right. + JPanel buttonPane = new JPanel(); + buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); + buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); + buttonPane.add(Box.createHorizontalGlue()); + buttonPane.add(cancel_button); + buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); +// buttonPane.add(manage_bluetooth_button); +// buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); + buttonPane.add(select_button); + + //Put everything together, using the content pane's BorderLayout. + Container contentPane = getContentPane(); + contentPane.add(listPane, BorderLayout.CENTER); + contentPane.add(buttonPane, BorderLayout.PAGE_END); + + //Initialize values. + if (devices != null && devices.length != 0) + list.setSelectedValue(devices[0], true); + pack(); + setLocationRelativeTo(location); + } + + //Handle clicks on the Set and Cancel buttons. + public void actionPerformed(ActionEvent e) { + if ("select".equals(e.getActionCommand())) + value = (AltosDevice)(list.getSelectedValue()); +// if ("manage".equals(e.getActionCommand())) { +// AltosBTManage.show(frame, AltosBTKnown.bt_known()); +// update_devices(); +// return; +// } + setVisible(false); + } + +} diff --git a/altosuilib/AltosUSBDevice.java b/altosuilib/AltosUSBDevice.java new file mode 100644 index 00000000..2f4e0dc6 --- /dev/null +++ b/altosuilib/AltosUSBDevice.java @@ -0,0 +1,112 @@ +/* + * 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 org.altusmetrum.altosuilib; + +import java.util.*; +import libaltosJNI.*; + +public class AltosUSBDevice extends altos_device implements AltosDevice { + + public String toString() { + String name = getName(); + if (name == null) + name = "Altus Metrum"; + return String.format("%-20.20s %4d %s", + name, getSerial(), getPath()); + } + + public String toShortString() { + String name = getName(); + if (name == null) + name = "Altus Metrum"; + return String.format("%s %d %s", + name, getSerial(), getPath()); + + } + + public String getErrorString() { + altos_error error = new altos_error(); + + libaltos.altos_get_last_error(error); + return String.format("%s (%d)", error.getString(), error.getCode()); + } + + public SWIGTYPE_p_altos_file open() { + return libaltos.altos_open(this); + } + + private boolean isAltusMetrum() { + if (getVendor() != AltosUILib.vendor_altusmetrum) + return false; + if (getProduct() < AltosUILib.product_altusmetrum_min) + return false; + if (getProduct() > AltosUILib.product_altusmetrum_max) + return false; + return true; + } + + public boolean matchProduct(int want_product) { + + if (!isAltusMetrum()) + return false; + + if (want_product == AltosUILib.product_any) + return true; + + if (want_product == AltosUILib.product_basestation) + return matchProduct(AltosUILib.product_teledongle) || + matchProduct(AltosUILib.product_teleterra) || + matchProduct(AltosUILib.product_telebt) || + matchProduct(AltosUILib.product_megadongle); + + if (want_product == AltosUILib.product_altimeter) + return matchProduct(AltosUILib.product_telemetrum) || + matchProduct(AltosUILib.product_megametrum); + + int have_product = getProduct(); + + if (have_product == AltosUILib.product_altusmetrum) /* old devices match any request */ + return true; + + if (want_product == have_product) + return true; + + return false; + } + + static java.util.List list(int product) { + if (!AltosUILib.load_library()) + return null; + + SWIGTYPE_p_altos_list list = libaltos.altos_list_start(); + + ArrayList device_list = new ArrayList(); + if (list != null) { + for (;;) { + AltosUSBDevice device = new AltosUSBDevice(); + if (libaltos.altos_list_next(list, device) == 0) + break; + if (device.matchProduct(product)) + device_list.add(device); + } + libaltos.altos_list_finish(list); + } + + return device_list; + } +} \ No newline at end of file diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am index 26aee7c4..e2ff8cb5 100644 --- a/altosuilib/Makefile.am +++ b/altosuilib/Makefile.am @@ -11,6 +11,9 @@ AltosUILibdir = $(datadir)/java AltosUILib_JAVA = \ AltosConfigureUI.java \ + AltosDevice.java \ + AltosDeviceDialog.java \ + AltosUSBDevice.java \ AltosFontListener.java \ AltosUIDialog.java \ AltosUIFrame.java \ diff --git a/libaltos/libaltos.c b/libaltos/libaltos.c index d7b266cf..6e884c80 100644 --- a/libaltos/libaltos.c +++ b/libaltos/libaltos.c @@ -116,6 +116,7 @@ altos_open(struct altos_device *device) return NULL; } + printf ("open\n"); // altos_set_last_error(12, "yeah yeah, failed again"); // free(file); // return NULL; @@ -148,6 +149,8 @@ altos_open(struct altos_device *device) return NULL; } cfmakeraw(&term); + cfsetospeed(&term, B9600); + cfsetispeed(&term, B9600); #ifdef USE_POLL term.c_cc[VMIN] = 1; term.c_cc[VTIME] = 0; @@ -609,6 +612,7 @@ altos_list_next(struct altos_list *list, struct altos_device *device) { struct altos_usbdev *dev; if (list->current >= list->ndev) { + printf ("end\n"); return 0; } dev = list->dev[list->current]; diff --git a/micropeak/Makefile.am b/micropeak/Makefile.am index e0de690c..a54b78a5 100644 --- a/micropeak/Makefile.am +++ b/micropeak/Makefile.am @@ -10,12 +10,15 @@ micropeakdir=$(datadir)/java micropeak_JAVA= \ MicroPeak.java \ MicroData.java \ + MicroDownload.java \ MicroFrame.java \ MicroGraph.java \ + MicroSave.java \ MicroSerial.java \ MicroStats.java \ MicroStatsTable.java \ MicroFileChooser.java \ + MicroDeviceDialog.java \ MicroUSB.java JFREECHART_CLASS= \ diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index ec9b83d8..8ccd5fd8 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -110,6 +110,7 @@ public class MicroData { private double time_step; private double ground_altitude; private ArrayList bytes; + String name; class FileEndedException extends Exception { @@ -310,12 +311,18 @@ public class MicroData { public void save (OutputStream f) throws IOException { for (int c : bytes) f.write(c); + f.write('\n'); } - public MicroData (InputStream f) throws IOException { + public void set_name(String name) { + this.name = name; + } + + public MicroData (InputStream f, String name) throws IOException, InterruptedException { + this.name = name; bytes = new ArrayList(); if (!find_header(f)) - throw new IOException(); + throw new IOException("No MicroPeak data header found"); try { file_crc = 0xffff; ground_pressure = get_32(f); @@ -354,9 +361,9 @@ public class MicroData { time_step = 0.192; } catch (FileEndedException fe) { - throw new IOException(); + throw new IOException("File Ended Unexpectedly"); } catch (NonHexcharException ne) { - throw new IOException(); + throw new IOException("Non hexadecimal character found"); } } diff --git a/micropeak/MicroDeviceDialog.java b/micropeak/MicroDeviceDialog.java new file mode 100644 index 00000000..7b8a630c --- /dev/null +++ b/micropeak/MicroDeviceDialog.java @@ -0,0 +1,50 @@ +/* + * Copyright © 2012 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.micropeak; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import org.altusmetrum.altosuilib.*; + +public class MicroDeviceDialog extends AltosDeviceDialog { + + public AltosDevice[] devices() { + java.util.List list = MicroUSB.list(); + int num_devices = list.size(); + AltosDevice[] devices = new AltosDevice[num_devices]; + + for (int i = 0; i < num_devices; i++) + devices[i] = list.get(i); + return devices; + } + + public MicroDeviceDialog (Frame in_frame, Component location) { + super(in_frame, location, 0); + } + + public static AltosDevice show (Component frameComp) { + Frame frame = JOptionPane.getFrameForComponent(frameComp); + MicroDeviceDialog dialog; + + dialog = new MicroDeviceDialog (frame, frameComp); + dialog.setVisible(true); + return dialog.getValue(); + } +} diff --git a/micropeak/MicroDownload.java b/micropeak/MicroDownload.java new file mode 100644 index 00000000..2e328b4a --- /dev/null +++ b/micropeak/MicroDownload.java @@ -0,0 +1,139 @@ +/* + * Copyright © 2012 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.micropeak; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.io.*; +import java.util.concurrent.*; +import java.util.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroDownload extends AltosUIDialog implements Runnable, ActionListener { + MicroPeak owner; + Container pane; + AltosDevice device; + JButton cancel; + MicroData data; + MicroSerial serial; + + private void done_internal() { + setVisible(false); + if (data != null) { + owner = owner.SetData(data); + MicroSave save = new MicroSave(owner, data); + if (save.runDialog()) + owner.SetName(data.name); + } + dispose(); + } + + public void done() { + Runnable r = new Runnable() { + public void run() { + try { + done_internal(); + } catch (Exception ex) { + } + } + }; + SwingUtilities.invokeLater(r); + } + + public void run() { + try { + data = new MicroData(serial, device.toShortString()); + serial.close(); + } catch (FileNotFoundException fe) { + } catch (IOException ioe) { + } catch (InterruptedException ie) { + } + done(); + } + + Thread serial_thread; + + public void start() { + try { + serial = new MicroSerial(device); + } catch (FileNotFoundException fe) { + return; + } + serial_thread = new Thread(this); + serial_thread.start(); + } + + public void actionPerformed(ActionEvent ae) { + System.out.printf ("command %s\n", ae.getActionCommand()); + if (serial_thread != null) { + System.out.printf ("Interrupting serial_thread\n"); + serial.close(); + serial_thread.interrupt(); + } + } + + public MicroDownload(MicroPeak owner, AltosDevice device) { + super (owner, "Download MicroPeak Data", false); + + GridBagConstraints c; + Insets il = new Insets(4,4,4,4); + Insets ir = new Insets(4,4,4,4); + + this.owner = owner; + this.device = device; + + 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; + JLabel device_label = new JLabel("Device:"); + pane.add(device_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; + JLabel device_value = new JLabel(device.toString()); + pane.add(device_value, c); + + cancel = new JButton("Cancel"); + c = new GridBagConstraints(); + c.fill = GridBagConstraints.NONE; + c.anchor = GridBagConstraints.CENTER; + c.gridx = 0; c.gridy = 1; + c.gridwidth = GridBagConstraints.REMAINDER; + Insets ic = new Insets(4,4,4,4); + c.insets = ic; + pane.add(cancel, c); + + cancel.addActionListener(this); + + pack(); + setLocationRelativeTo(owner); + setVisible(true); + this.start(); + } +} diff --git a/micropeak/MicroGraph.java b/micropeak/MicroGraph.java index 38f54fe0..b9b084f8 100644 --- a/micropeak/MicroGraph.java +++ b/micropeak/MicroGraph.java @@ -111,8 +111,13 @@ public class MicroGraph implements AltosUnitsListener { } } + public void setName (String name) { + chart.setTitle(name); + } + public void setData (MicroData data) { this.data = data; + chart.setTitle(data.name); resetData(); } diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index c69f7167..5e375057 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -34,13 +34,36 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene MicroData data; Container container; JTabbedPane pane; + static int number_of_windows; - private void RunFile(InputStream input) { + MicroPeak SetData(MicroData data) { + MicroPeak mp = this; + if (this.data != null) { + mp = new MicroPeak(); + return mp.SetData(data); + } + this.data = data; + graph.setData(data); + stats.setData(data); + setTitle(data.name); + return this; + } + + void SetName(String name) { + graph.setName(name); + setTitle(name); + } + + private void RunFile(InputStream input, String name) { try { - data = new MicroData(input); - graph.setData(data); - stats.setData(data); + MicroData data = new MicroData(input, name); + SetData(data); } catch (IOException ioe) { + JOptionPane.showMessageDialog(this, + ioe.getMessage(), + "File Read Error", + JOptionPane.ERROR_MESSAGE); + } catch (InterruptedException ie) { } try { input.close(); @@ -50,8 +73,12 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene private void OpenFile(File filename) { try { - RunFile (new FileInputStream(filename)); + RunFile (new FileInputStream(filename), filename.getName()); } catch (FileNotFoundException fne) { + JOptionPane.showMessageDialog(this, + fne.getMessage(), + "Cannot open file", + JOptionPane.ERROR_MESSAGE); } } @@ -60,30 +87,44 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene InputStream input = chooser.runDialog(); if (input != null) - RunFile(input); + RunFile(input, chooser.filename); } private void Preferences() { new AltosConfigureUI(this); } - + private void DownloadData() { - java.util.List devices = MicroUSB.list(); - for (MicroUSB device : devices) - System.out.printf("device %s\n", device.toString()); + AltosDevice device = MicroDeviceDialog.show(this); + + if (device != null) + new MicroDownload(this, device); } + private void Save() { + if (data == null) { + JOptionPane.showMessageDialog(this, + "No data available", + "No data", + JOptionPane.INFORMATION_MESSAGE); + return; + } + MicroSave save = new MicroSave (this, data); + if (save.runDialog()) + SetName(data.name); + } + public void actionPerformed(ActionEvent ev) { if ("Exit".equals(ev.getActionCommand())) System.exit(0); else if ("Open".equals(ev.getActionCommand())) SelectFile(); - else if ("New".equals(ev.getActionCommand())) - new MicroPeak(); else if ("Download".equals(ev.getActionCommand())) DownloadData(); else if ("Preferences".equals(ev.getActionCommand())) Preferences(); + else if ("Save a Copy".equals(ev.getActionCommand())) + Save(); } public void itemStateChanged(ItemEvent e) { @@ -91,6 +132,8 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene public MicroPeak() { + ++number_of_windows; + AltosUIPreferences.set_component(this); container = getContentPane(); @@ -104,10 +147,6 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); - JMenuItem newAction = new JMenuItem("New"); - fileMenu.add(newAction); - newAction.addActionListener(this); - JMenuItem openAction = new JMenuItem("Open"); fileMenu.add(openAction); openAction.addActionListener(this); @@ -116,6 +155,10 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene fileMenu.add(downloadAction); downloadAction.addActionListener(this); + JMenuItem saveAction = new JMenuItem("Save a Copy"); + fileMenu.add(saveAction); + saveAction.addActionListener(this); + JMenuItem preferencesAction = new JMenuItem("Preferences"); fileMenu.add(preferencesAction); preferencesAction.addActionListener(this); @@ -128,7 +171,11 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { - System.exit(0); + setVisible(false); + dispose(); + --number_of_windows; + if (number_of_windows == 0) + System.exit(0); } }); diff --git a/micropeak/MicroSave.java b/micropeak/MicroSave.java new file mode 100644 index 00000000..cb4b4221 --- /dev/null +++ b/micropeak/MicroSave.java @@ -0,0 +1,102 @@ +/* + * Copyright © 2012 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.micropeak; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.io.*; +import java.util.concurrent.*; +import java.util.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; + +public class MicroSave extends JFileChooser { + + JFrame frame; + MicroData data; + + public boolean runDialog() { + int ret; + + for (;;) { + ret = showSaveDialog(frame); + if (ret != APPROVE_OPTION) + return false; + File file; + String filename; + file = getSelectedFile(); + if (file == null) + continue; + if (!file.getName().contains(".")) { + String fullname = file.getPath(); + file = new File(fullname.concat(".mpd")); + } + filename = file.getName(); + if (file.exists()) { + if (file.isDirectory()) { + JOptionPane.showMessageDialog(frame, + String.format("\"%s\" is a directory", + filename), + "Directory", + JOptionPane.ERROR_MESSAGE); + continue; + } + int r = JOptionPane.showConfirmDialog(frame, + String.format("\"%s\" already exists. Overwrite?", + filename), + "Overwrite file?", + JOptionPane.YES_NO_OPTION); + if (r != JOptionPane.YES_OPTION) + continue; + + if (!file.canWrite()) { + JOptionPane.showMessageDialog(frame, + String.format("\"%s\" is not writable", + filename), + "File not writable", + JOptionPane.ERROR_MESSAGE); + continue; + } + } + try { + FileOutputStream fos = new FileOutputStream(file); + data.save(fos); + fos.close(); + data.set_name(filename); + return true; + } catch (FileNotFoundException fe) { + JOptionPane.showMessageDialog(frame, + fe.getMessage(), + "Cannot create file", + JOptionPane.ERROR_MESSAGE); + } catch (IOException ioe) { + } + } + } + + public MicroSave(JFrame frame, MicroData data) { + this.frame = frame; + this.data = data; + setDialogTitle("Save MicroPeak Data File"); + setFileFilter(new FileNameExtensionFilter("MicroPeak data file", + "mpd")); + setCurrentDirectory(AltosUIPreferences.logdir()); + } +} diff --git a/micropeak/MicroSerial.java b/micropeak/MicroSerial.java index a1a77a1d..15ef8582 100644 --- a/micropeak/MicroSerial.java +++ b/micropeak/MicroSerial.java @@ -27,6 +27,10 @@ public class MicroSerial extends InputStream { public int read() { int c = libaltos.altos_getchar(file, 0); + if (Thread.interrupted()) + return -1; + if (c == -1) + return -1; if (AltosUIPreferences.serial_debug) System.out.printf("%c", c); return c; @@ -39,12 +43,12 @@ public class MicroSerial extends InputStream { } } - public MicroSerial(MicroUSB usb) throws FileNotFoundException { - file = usb.open(); + public MicroSerial(AltosDevice device) throws FileNotFoundException { + file = device.open(); if (file == null) { - final String message = usb.getErrorString(); + final String message = device.getErrorString(); throw new FileNotFoundException(String.format("%s (%s)", - usb.toShortString(), + device.toShortString(), message)); } } diff --git a/micropeak/MicroUSB.java b/micropeak/MicroUSB.java index d48610fe..244f7bc0 100644 --- a/micropeak/MicroUSB.java +++ b/micropeak/MicroUSB.java @@ -16,10 +16,12 @@ */ package org.altusmetrum.micropeak; + import java.util.*; import libaltosJNI.*; +import org.altusmetrum.altosuilib.*; -public class MicroUSB extends altos_device { +public class MicroUSB extends altos_device implements AltosDevice { static boolean initialized = false; static boolean loaded_library = false; @@ -48,16 +50,16 @@ public class MicroUSB extends altos_device { String name = getName(); if (name == null) name = "Altus Metrum"; - return String.format("%-20.20s %4d %s", - name, getSerial(), getPath()); + return String.format("%-20.20s %s", + name, getPath()); } public String toShortString() { String name = getName(); if (name == null) name = "Altus Metrum"; - return String.format("%s %d %s", - name, getSerial(), getPath()); + return String.format("%s %s", + name, getPath()); } @@ -75,11 +77,15 @@ public class MicroUSB extends altos_device { private boolean isMicro() { if (getVendor() != 0x0403) return false; - if (getProduct() != 0x6001) + if (getProduct() != 0x6015) return false; return true; } + public boolean matchProduct(int product) { + return isMicro(); + } + static java.util.List list() { if (!load_library()) return null; @@ -92,6 +98,7 @@ public class MicroUSB extends altos_device { MicroUSB device = new MicroUSB(); if (libaltos.altos_list_next(list, device) == 0) break; + System.out.printf("Device %s\n", device.toString()); if (device.isMicro()) device_list.add(device); } -- cgit v1.2.3 From 9efc57e4052e3c11218973f7666ad18ea5cf2a5a Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 1 Jan 2013 23:15:14 -0800 Subject: Rename AltosConfigureUI to AltosUIConfigure Leave AltosConfigureUI for AltosUI Signed-off-by: Keith Packard --- altosuilib/AltosConfigureUI.java | 393 --------------------------------------- altosuilib/AltosUIConfigure.java | 393 +++++++++++++++++++++++++++++++++++++++ altosuilib/Makefile.am | 2 +- micropeak/MicroPeak.java | 2 +- 4 files changed, 395 insertions(+), 395 deletions(-) delete mode 100644 altosuilib/AltosConfigureUI.java create mode 100644 altosuilib/AltosUIConfigure.java (limited to 'altosuilib') diff --git a/altosuilib/AltosConfigureUI.java b/altosuilib/AltosConfigureUI.java deleted file mode 100644 index a4b644bf..00000000 --- a/altosuilib/AltosConfigureUI.java +++ /dev/null @@ -1,393 +0,0 @@ -/* - * 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 org.altusmetrum.altosuilib; - -import java.awt.*; -import java.awt.event.*; -import java.beans.*; -import javax.swing.*; -import javax.swing.event.*; - -class DelegatingRenderer implements ListCellRenderer { - - // ... - public static void install(JComboBox comboBox) { - DelegatingRenderer renderer = new DelegatingRenderer(comboBox); - renderer.initialise(); - comboBox.setRenderer(renderer); - } - - // ... - private final JComboBox comboBox; - - // ... - private ListCellRenderer delegate; - - // ... - private DelegatingRenderer(JComboBox comboBox) { - this.comboBox = comboBox; - } - - // ... - private void initialise() { - delegate = new JComboBox().getRenderer(); - comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() { - - public void propertyChange(PropertyChangeEvent evt) { - delegate = new JComboBox().getRenderer(); - } - }); - } - - // ... - public Component getListCellRendererComponent(JList list, - Object value, int index, boolean isSelected, boolean cellHasFocus) { - - return delegate.getListCellRendererComponent(list, - ((UIManager.LookAndFeelInfo) value).getName(), - index, isSelected, cellHasFocus); - } -} - -public class AltosConfigureUI - extends AltosUIDialog - implements DocumentListener -{ - JFrame owner; - Container pane; - - JRadioButton enable_voice; - JButton test_voice; - JButton close; - - JButton configure_log; - JTextField log_directory; - - JLabel callsign_label; - JTextField callsign_value; - - JRadioButton imperial_units; - - JLabel font_size_label; - JComboBox font_size_value; - - JLabel look_and_feel_label; - JComboBox look_and_feel_value; - - JRadioButton serial_debug; - - JButton manage_bluetooth; - JButton manage_frequencies; - - int row; - - final static String[] font_size_names = { "Small", "Medium", "Large" }; - - /* DocumentListener interface methods */ - public void changedUpdate(DocumentEvent e) { - if (callsign_value != null) - AltosUIPreferences.set_callsign(callsign_value.getText()); - } - - public void insertUpdate(DocumentEvent e) { - changedUpdate(e); - } - - public void removeUpdate(DocumentEvent e) { - changedUpdate(e); - } - - public GridBagConstraints constraints (int x, int width, int fill) { - GridBagConstraints c = new GridBagConstraints(); - Insets insets = new Insets(4, 4, 4, 4); - - c.insets = insets; - c.fill = fill; - if (width == 3) - c.anchor = GridBagConstraints.CENTER; - else - c.anchor = GridBagConstraints.WEST; - c.gridx = x; - c.gridwidth = width; - c.gridy = row; - return c; - } - - public GridBagConstraints constraints(int x, int width) { - return constraints(x, width, GridBagConstraints.NONE); - } - - public void add_voice() { -// GridBagConstraints c = new GridBagConstraints(); -// -// /* Voice settings */ -// c.gridx = 0; -// c.gridy = row; -// c.gridwidth = 1; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(new JLabel("Voice"), c); -// -// enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice()); -// enable_voice.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// JRadioButton item = (JRadioButton) e.getSource(); -// boolean enabled = item.isSelected(); -// AltosUIPreferences.set_voice(enabled); -// if (enabled) -// voice.speak_always("Enable voice."); -// else -// voice.speak_always("Disable voice."); -// } -// }); -// c.gridx = 1; -// c.gridy = row; -// c.gridwidth = 1; -// c.weightx = 1; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(enable_voice, c); -// enable_voice.setToolTipText("Enable/Disable all audio in-flight announcements"); -// -// c.gridx = 2; -// c.gridy = row++; -// c.gridwidth = 1; -// c.weightx = 1; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.EAST; -// test_voice = new JButton("Test Voice"); -// test_voice.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// voice.speak("That's one small step for man; one giant leap for mankind."); -// } -// }); -// pane.add(test_voice, c); -// test_voice.setToolTipText("Play a stock audio clip to check volume"); -// row++; - } - - public void add_log_dir() { - /* Log directory settings */ - pane.add(new JLabel("Log Directory"), constraints(0, 1)); - - configure_log = new JButton(AltosUIPreferences.logdir().getPath()); - configure_log.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - AltosUIPreferences.ConfigureLog(); - configure_log.setText(AltosUIPreferences.logdir().getPath()); - } - }); - pane.add(configure_log, constraints(1, 2)); - configure_log.setToolTipText("Which directory flight logs are stored in"); - row++; - } - - public void add_callsign() { -// /* Callsign setting */ -// pane.add(new JLabel("Callsign"), constraints(0, 1)); -// -// callsign_value = new JTextField(AltosUIPreferences.callsign()); -// callsign_value.getDocument().addDocumentListener(this); -// callsign_value.setToolTipText("Callsign sent in packet mode"); -// pane.add(callsign_value, constraints(1, 2, GridBagConstraints.BOTH)); -// row++; - } - - public void add_units() { - /* Imperial units setting */ - pane.add(new JLabel("Imperial Units"), constraints(0, 1)); - - imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units()); - imperial_units.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - JRadioButton item = (JRadioButton) e.getSource(); - boolean enabled = item.isSelected(); - AltosUIPreferences.set_imperial_units(enabled); - } - }); - imperial_units.setToolTipText("Use Imperial units instead of metric"); - pane.add(imperial_units, constraints(1, 2)); - row++; - } - - public void add_font_size() { - /* Font size setting */ - pane.add(new JLabel("Font size"), constraints(0, 1)); - - font_size_value = new JComboBox(font_size_names); - int font_size = AltosUIPreferences.font_size(); - font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small); - font_size_value.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - int size = font_size_value.getSelectedIndex() + AltosUILib.font_size_small; - - AltosUIPreferences.set_font_size(size); - } - }); - pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH)); - font_size_value.setToolTipText("Font size used in telemetry window"); - row++; - } - - public void add_look_and_feel() { - /* Look & Feel setting */ - pane.add(new JLabel("Look & feel"), constraints(0, 1)); - - /* - class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer { - - public LookAndFeelRenderer() { - super(); - } - - public Component getListCellRendererComponent( - JList list, - Object value, - int index, - boolean isSelected, - boolean cellHasFocus) - { - super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - setText(((UIManager.LookAndFeelInfo) value).getName()); - return this; - } - } - */ - - final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels(); - - look_and_feel_value = new JComboBox(look_and_feels); - - DelegatingRenderer.install(look_and_feel_value); - - String look_and_feel = AltosUIPreferences.look_and_feel(); - for (int i = 0; i < look_and_feels.length; i++) - if (look_and_feel.equals(look_and_feels[i].getClassName())) - look_and_feel_value.setSelectedIndex(i); - - look_and_feel_value.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - int id = look_and_feel_value.getSelectedIndex(); - - AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName()); - } - }); - pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH)); - look_and_feel_value.setToolTipText("Look&feel used for new windows"); - row++; - } - - public void add_serial_debug() { - GridBagConstraints c = new GridBagConstraints(); - - /* Serial debug setting */ - pane.add(new JLabel("Serial Debug"), constraints(0, 1)); - - serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug()); - serial_debug.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - JRadioButton item = (JRadioButton) e.getSource(); - boolean enabled = item.isSelected(); - AltosUIPreferences.set_serial_debug(enabled); - } - }); - serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console"); - } - - public void add_bluetooth() { -// GridBagConstraints c = new GridBagConstraints(); -// c.gridx = 1; -// c.gridy = row++; -// c.gridwidth = 3; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(serial_debug, c); -// -// manage_bluetooth = new JButton("Manage Bluetooth"); -// manage_bluetooth.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// AltosBTManage.show(owner, AltosBTKnown.bt_known()); -// } -// }); -// c.gridx = 0; -// c.gridy = row; -// c.gridwidth = 2; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(manage_bluetooth, c); - } - - public void add_frequencies() { -// GridBagConstraints c = new GridBagConstraints(); -// manage_frequencies = new JButton("Manage Frequencies"); -// manage_frequencies.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// AltosConfigFreqUI.show(owner); -// } -// }); -// manage_frequencies.setToolTipText("Configure which values are shown in frequency menus"); -// c.gridx = 2; -// c.gridx = 2; -// c.gridy = row++; -// c.gridwidth = 2; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(manage_frequencies, c); - } - - public AltosConfigureUI(JFrame in_owner) { - super(in_owner, "Configure AltosUI", false); - - owner = in_owner; - pane = getContentPane(); - pane.setLayout(new GridBagLayout()); - - row = 0; - - /* Nice label at the top */ - pane.add(new JLabel ("Configure AltOS UI"), - constraints(0, 3)); - row++; - - pane.add(new JLabel (String.format("AltOS version %s", AltosUIVersion.version)), - constraints(0, 3)); - row++; - - add_voice(); - add_log_dir(); - add_callsign(); - add_units(); - add_font_size(); - add_look_and_feel(); - add_bluetooth(); - add_frequencies(); - - /* And a close button at the bottom */ - close = new JButton("Close"); - close.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - setVisible(false); - } - }); - pane.add(close, constraints(0, 3)); - - pack(); - setLocationRelativeTo(owner); - setVisible(true); - } -} diff --git a/altosuilib/AltosUIConfigure.java b/altosuilib/AltosUIConfigure.java new file mode 100644 index 00000000..ef44f0be --- /dev/null +++ b/altosuilib/AltosUIConfigure.java @@ -0,0 +1,393 @@ +/* + * 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 org.altusmetrum.altosuilib; + +import java.awt.*; +import java.awt.event.*; +import java.beans.*; +import javax.swing.*; +import javax.swing.event.*; + +class DelegatingRenderer implements ListCellRenderer { + + // ... + public static void install(JComboBox comboBox) { + DelegatingRenderer renderer = new DelegatingRenderer(comboBox); + renderer.initialise(); + comboBox.setRenderer(renderer); + } + + // ... + private final JComboBox comboBox; + + // ... + private ListCellRenderer delegate; + + // ... + private DelegatingRenderer(JComboBox comboBox) { + this.comboBox = comboBox; + } + + // ... + private void initialise() { + delegate = new JComboBox().getRenderer(); + comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() { + + public void propertyChange(PropertyChangeEvent evt) { + delegate = new JComboBox().getRenderer(); + } + }); + } + + // ... + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, boolean cellHasFocus) { + + return delegate.getListCellRendererComponent(list, + ((UIManager.LookAndFeelInfo) value).getName(), + index, isSelected, cellHasFocus); + } +} + +public class AltosUIConfigure + extends AltosUIDialog + implements DocumentListener +{ + JFrame owner; + Container pane; + + JRadioButton enable_voice; + JButton test_voice; + JButton close; + + JButton configure_log; + JTextField log_directory; + + JLabel callsign_label; + JTextField callsign_value; + + JRadioButton imperial_units; + + JLabel font_size_label; + JComboBox font_size_value; + + JLabel look_and_feel_label; + JComboBox look_and_feel_value; + + JRadioButton serial_debug; + + JButton manage_bluetooth; + JButton manage_frequencies; + + int row; + + final static String[] font_size_names = { "Small", "Medium", "Large" }; + + /* DocumentListener interface methods */ + public void changedUpdate(DocumentEvent e) { + if (callsign_value != null) + AltosUIPreferences.set_callsign(callsign_value.getText()); + } + + public void insertUpdate(DocumentEvent e) { + changedUpdate(e); + } + + public void removeUpdate(DocumentEvent e) { + changedUpdate(e); + } + + public GridBagConstraints constraints (int x, int width, int fill) { + GridBagConstraints c = new GridBagConstraints(); + Insets insets = new Insets(4, 4, 4, 4); + + c.insets = insets; + c.fill = fill; + if (width == 3) + c.anchor = GridBagConstraints.CENTER; + else + c.anchor = GridBagConstraints.WEST; + c.gridx = x; + c.gridwidth = width; + c.gridy = row; + return c; + } + + public GridBagConstraints constraints(int x, int width) { + return constraints(x, width, GridBagConstraints.NONE); + } + + public void add_voice() { +// GridBagConstraints c = new GridBagConstraints(); +// +// /* Voice settings */ +// c.gridx = 0; +// c.gridy = row; +// c.gridwidth = 1; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(new JLabel("Voice"), c); +// +// enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice()); +// enable_voice.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// JRadioButton item = (JRadioButton) e.getSource(); +// boolean enabled = item.isSelected(); +// AltosUIPreferences.set_voice(enabled); +// if (enabled) +// voice.speak_always("Enable voice."); +// else +// voice.speak_always("Disable voice."); +// } +// }); +// c.gridx = 1; +// c.gridy = row; +// c.gridwidth = 1; +// c.weightx = 1; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(enable_voice, c); +// enable_voice.setToolTipText("Enable/Disable all audio in-flight announcements"); +// +// c.gridx = 2; +// c.gridy = row++; +// c.gridwidth = 1; +// c.weightx = 1; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.EAST; +// test_voice = new JButton("Test Voice"); +// test_voice.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// voice.speak("That's one small step for man; one giant leap for mankind."); +// } +// }); +// pane.add(test_voice, c); +// test_voice.setToolTipText("Play a stock audio clip to check volume"); +// row++; + } + + public void add_log_dir() { + /* Log directory settings */ + pane.add(new JLabel("Log Directory"), constraints(0, 1)); + + configure_log = new JButton(AltosUIPreferences.logdir().getPath()); + configure_log.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + AltosUIPreferences.ConfigureLog(); + configure_log.setText(AltosUIPreferences.logdir().getPath()); + } + }); + pane.add(configure_log, constraints(1, 2)); + configure_log.setToolTipText("Which directory flight logs are stored in"); + row++; + } + + public void add_callsign() { +// /* Callsign setting */ +// pane.add(new JLabel("Callsign"), constraints(0, 1)); +// +// callsign_value = new JTextField(AltosUIPreferences.callsign()); +// callsign_value.getDocument().addDocumentListener(this); +// callsign_value.setToolTipText("Callsign sent in packet mode"); +// pane.add(callsign_value, constraints(1, 2, GridBagConstraints.BOTH)); +// row++; + } + + public void add_units() { + /* Imperial units setting */ + pane.add(new JLabel("Imperial Units"), constraints(0, 1)); + + imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units()); + imperial_units.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JRadioButton item = (JRadioButton) e.getSource(); + boolean enabled = item.isSelected(); + AltosUIPreferences.set_imperial_units(enabled); + } + }); + imperial_units.setToolTipText("Use Imperial units instead of metric"); + pane.add(imperial_units, constraints(1, 2)); + row++; + } + + public void add_font_size() { + /* Font size setting */ + pane.add(new JLabel("Font size"), constraints(0, 1)); + + font_size_value = new JComboBox(font_size_names); + int font_size = AltosUIPreferences.font_size(); + font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small); + font_size_value.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int size = font_size_value.getSelectedIndex() + AltosUILib.font_size_small; + + AltosUIPreferences.set_font_size(size); + } + }); + pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH)); + font_size_value.setToolTipText("Font size used in telemetry window"); + row++; + } + + public void add_look_and_feel() { + /* Look & Feel setting */ + pane.add(new JLabel("Look & feel"), constraints(0, 1)); + + /* + class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer { + + public LookAndFeelRenderer() { + super(); + } + + public Component getListCellRendererComponent( + JList list, + Object value, + int index, + boolean isSelected, + boolean cellHasFocus) + { + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + setText(((UIManager.LookAndFeelInfo) value).getName()); + return this; + } + } + */ + + final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels(); + + look_and_feel_value = new JComboBox(look_and_feels); + + DelegatingRenderer.install(look_and_feel_value); + + String look_and_feel = AltosUIPreferences.look_and_feel(); + for (int i = 0; i < look_and_feels.length; i++) + if (look_and_feel.equals(look_and_feels[i].getClassName())) + look_and_feel_value.setSelectedIndex(i); + + look_and_feel_value.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + int id = look_and_feel_value.getSelectedIndex(); + + AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName()); + } + }); + pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH)); + look_and_feel_value.setToolTipText("Look&feel used for new windows"); + row++; + } + + public void add_serial_debug() { + GridBagConstraints c = new GridBagConstraints(); + + /* Serial debug setting */ + pane.add(new JLabel("Serial Debug"), constraints(0, 1)); + + serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug()); + serial_debug.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JRadioButton item = (JRadioButton) e.getSource(); + boolean enabled = item.isSelected(); + AltosUIPreferences.set_serial_debug(enabled); + } + }); + serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console"); + } + + public void add_bluetooth() { +// GridBagConstraints c = new GridBagConstraints(); +// c.gridx = 1; +// c.gridy = row++; +// c.gridwidth = 3; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(serial_debug, c); +// +// manage_bluetooth = new JButton("Manage Bluetooth"); +// manage_bluetooth.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// AltosBTManage.show(owner, AltosBTKnown.bt_known()); +// } +// }); +// c.gridx = 0; +// c.gridy = row; +// c.gridwidth = 2; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(manage_bluetooth, c); + } + + public void add_frequencies() { +// GridBagConstraints c = new GridBagConstraints(); +// manage_frequencies = new JButton("Manage Frequencies"); +// manage_frequencies.addActionListener(new ActionListener() { +// public void actionPerformed(ActionEvent e) { +// AltosConfigFreqUI.show(owner); +// } +// }); +// manage_frequencies.setToolTipText("Configure which values are shown in frequency menus"); +// c.gridx = 2; +// c.gridx = 2; +// c.gridy = row++; +// c.gridwidth = 2; +// c.fill = GridBagConstraints.NONE; +// c.anchor = GridBagConstraints.WEST; +// pane.add(manage_frequencies, c); + } + + public AltosUIConfigure(JFrame in_owner) { + super(in_owner, "Configure AltosUI", false); + + owner = in_owner; + pane = getContentPane(); + pane.setLayout(new GridBagLayout()); + + row = 0; + + /* Nice label at the top */ + pane.add(new JLabel ("Configure AltOS UI"), + constraints(0, 3)); + row++; + + pane.add(new JLabel (String.format("AltOS version %s", AltosUIVersion.version)), + constraints(0, 3)); + row++; + + add_voice(); + add_log_dir(); + add_callsign(); + add_units(); + add_font_size(); + add_look_and_feel(); + add_bluetooth(); + add_frequencies(); + + /* And a close button at the bottom */ + close = new JButton("Close"); + close.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + setVisible(false); + } + }); + pane.add(close, constraints(0, 3)); + + pack(); + setLocationRelativeTo(owner); + setVisible(true); + } +} diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am index e2ff8cb5..d93d9415 100644 --- a/altosuilib/Makefile.am +++ b/altosuilib/Makefile.am @@ -10,7 +10,7 @@ BIN=bin/org/altusmetrum/AltosUILib AltosUILibdir = $(datadir)/java AltosUILib_JAVA = \ - AltosConfigureUI.java \ + AltosUIConfigure.java \ AltosDevice.java \ AltosDeviceDialog.java \ AltosUSBDevice.java \ diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index 17eae228..544f3ae0 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -91,7 +91,7 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene } private void Preferences() { - new AltosConfigureUI(this); + new AltosUIConfigure(this); } private void DownloadData() { -- cgit v1.2.3 From 5ce43661834920c3a8f3a1b6e1c555fb952b512d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 2 Jan 2013 09:06:41 -0800 Subject: altosui: Use altosuilib for configuration Start moving to shared UI code Signed-off-by: Keith Packard --- altosui/AltosConfigureUI.java | 358 ++++----------------------------------- altosui/Makefile.am | 28 +-- altosuilib/AltosUIConfigure.java | 153 ++--------------- altosuilib/Makefile.am | 27 ++- 4 files changed, 82 insertions(+), 484 deletions(-) (limited to 'altosuilib') diff --git a/altosui/AltosConfigureUI.java b/altosui/AltosConfigureUI.java index c576b052..0e411b03 100644 --- a/altosui/AltosConfigureUI.java +++ b/altosui/AltosConfigureUI.java @@ -22,86 +22,17 @@ import java.awt.event.*; import java.beans.*; import javax.swing.*; import javax.swing.event.*; - -class DelegatingRenderer implements ListCellRenderer { - - // ... - public static void install(JComboBox comboBox) { - DelegatingRenderer renderer = new DelegatingRenderer(comboBox); - renderer.initialise(); - comboBox.setRenderer(renderer); - } - - // ... - private final JComboBox comboBox; - - // ... - private ListCellRenderer delegate; - - // ... - private DelegatingRenderer(JComboBox comboBox) { - this.comboBox = comboBox; - } - - // ... - private void initialise() { - delegate = new JComboBox().getRenderer(); - comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() { - - public void propertyChange(PropertyChangeEvent evt) { - delegate = new JComboBox().getRenderer(); - } - }); - } - - // ... - public Component getListCellRendererComponent(JList list, - Object value, int index, boolean isSelected, boolean cellHasFocus) { - - return delegate.getListCellRendererComponent(list, - ((UIManager.LookAndFeelInfo) value).getName(), - index, isSelected, cellHasFocus); - } -} +import org.altusmetrum.altosuilib.*; public class AltosConfigureUI - extends AltosDialog + extends AltosUIConfigure implements DocumentListener { - JFrame owner; AltosVoice voice; - Container pane; - - JRadioButton enable_voice; - JButton test_voice; - JButton close; - - JButton configure_log; - JTextField log_directory; - - JLabel callsign_label; - JTextField callsign_value; - - JRadioButton imperial_units; - - JLabel font_size_label; - JComboBox font_size_value; - JLabel look_and_feel_label; - JComboBox look_and_feel_value; - - JRadioButton serial_debug; - - JButton manage_bluetooth; - JButton manage_frequencies; - - final static String[] font_size_names = { "Small", "Medium", "Large" }; + public JTextField callsign_value; /* DocumentListener interface methods */ - public void changedUpdate(DocumentEvent e) { - AltosUIPreferences.set_callsign(callsign_value.getText()); - } - public void insertUpdate(DocumentEvent e) { changedUpdate(e); } @@ -110,49 +41,17 @@ public class AltosConfigureUI changedUpdate(e); } - public AltosConfigureUI(JFrame in_owner, AltosVoice in_voice) { - super(in_owner, "Configure AltosUI", false); - - GridBagConstraints c; - - Insets insets = new Insets(4, 4, 4, 4); - - int row = 0; - - owner = in_owner; - voice = in_voice; - pane = getContentPane(); - pane.setLayout(new GridBagLayout()); - - c = new GridBagConstraints(); - c.insets = insets; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - - /* Nice label at the top */ - c.gridx = 0; - c.gridy = row++; - c.gridwidth = 3; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.CENTER; - pane.add(new JLabel ("Configure AltOS UI"), c); + public void changedUpdate(DocumentEvent e) { + if (callsign_value != null) + AltosUIPreferences.set_callsign(callsign_value.getText()); + } - c.gridx = 0; - c.gridy = row++; - c.gridwidth = 3; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.CENTER; - pane.add(new JLabel (String.format("AltOS version %s", AltosVersion.version)), c); + public void add_voice() { /* Voice settings */ - c.gridx = 0; - c.gridy = row; - c.gridwidth = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(new JLabel("Voice"), c); + pane.add(new JLabel("Voice"), constraints(0, 1)); - enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice()); + JRadioButton enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice()); enable_voice.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JRadioButton item = (JRadioButton) e.getSource(); @@ -164,246 +63,57 @@ public class AltosConfigureUI voice.speak_always("Disable voice."); } }); - c.gridx = 1; - c.gridy = row; - c.gridwidth = 1; - c.weightx = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(enable_voice, c); + pane.add(enable_voice, constraints(1, 1)); enable_voice.setToolTipText("Enable/Disable all audio in-flight announcements"); - c.gridx = 2; - c.gridy = row++; - c.gridwidth = 1; - c.weightx = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.EAST; - test_voice = new JButton("Test Voice"); + JButton test_voice = new JButton("Test Voice"); test_voice.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { voice.speak("That's one small step for man; one giant leap for mankind."); } }); - pane.add(test_voice, c); + pane.add(test_voice, constraints(2, 1)); test_voice.setToolTipText("Play a stock audio clip to check volume"); + row++; + } - /* Log directory settings */ - c.gridx = 0; - c.gridy = row; - c.gridwidth = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(new JLabel("Log Directory"), c); - - configure_log = new JButton(AltosUIPreferences.logdir().getPath()); - configure_log.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - AltosUIPreferences.ConfigureLog(); - configure_log.setText(AltosUIPreferences.logdir().getPath()); - } - }); - c.gridx = 1; - c.gridy = row++; - c.gridwidth = 2; - c.fill = GridBagConstraints.BOTH; - c.anchor = GridBagConstraints.WEST; - pane.add(configure_log, c); - configure_log.setToolTipText("Which directory flight logs are stored in"); - + public void add_callsign() { /* Callsign setting */ - c.gridx = 0; - c.gridy = row; - c.gridwidth = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(new JLabel("Callsign"), c); + pane.add(new JLabel("Callsign"), constraints(0, 1)); - callsign_value = new JTextField(AltosUIPreferences.callsign()); + JTextField callsign_value = new JTextField(AltosUIPreferences.callsign()); callsign_value.getDocument().addDocumentListener(this); - c.gridx = 1; - c.gridy = row++; - c.gridwidth = 2; - c.fill = GridBagConstraints.BOTH; - c.anchor = GridBagConstraints.WEST; - pane.add(callsign_value, c); callsign_value.setToolTipText("Callsign sent in packet mode"); + pane.add(callsign_value, constraints(1, 2, GridBagConstraints.BOTH)); + row++; + } - /* Imperial units setting */ - c.gridx = 0; - c.gridy = row; - c.gridwidth = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(new JLabel("Imperial Units"), c); - - imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units()); - imperial_units.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - JRadioButton item = (JRadioButton) e.getSource(); - boolean enabled = item.isSelected(); - AltosUIPreferences.set_imperial_units(enabled); - } - }); - imperial_units.setToolTipText("Use Imperial units instead of metric"); - - c.gridx = 1; - c.gridy = row++; - c.gridwidth = 3; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(imperial_units, c); - - /* Font size setting */ - c.gridx = 0; - c.gridy = row; - c.gridwidth = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(new JLabel("Font size"), c); - - font_size_value = new JComboBox(font_size_names); - int font_size = AltosUIPreferences.font_size(); - font_size_value.setSelectedIndex(font_size - Altos.font_size_small); - font_size_value.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - int size = font_size_value.getSelectedIndex() + Altos.font_size_small; - - AltosUIPreferences.set_font_size(size); - } - }); - c.gridx = 1; - c.gridy = row++; - c.gridwidth = 2; - c.fill = GridBagConstraints.BOTH; - c.anchor = GridBagConstraints.WEST; - pane.add(font_size_value, c); - font_size_value.setToolTipText("Font size used in telemetry window"); - - /* Look & Feel setting */ - c.gridx = 0; - c.gridy = row; - c.gridwidth = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(new JLabel("Look & feel"), c); - - /* - class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer { - - public LookAndFeelRenderer() { - super(); - } - - public Component getListCellRendererComponent( - JList list, - Object value, - int index, - boolean isSelected, - boolean cellHasFocus) - { - super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); - setText(((UIManager.LookAndFeelInfo) value).getName()); - return this; - } - } - */ - - final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels(); - - look_and_feel_value = new JComboBox(look_and_feels); - - DelegatingRenderer.install(look_and_feel_value); - - String look_and_feel = AltosUIPreferences.look_and_feel(); - for (int i = 0; i < look_and_feels.length; i++) - if (look_and_feel.equals(look_and_feels[i].getClassName())) - look_and_feel_value.setSelectedIndex(i); - - look_and_feel_value.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - int id = look_and_feel_value.getSelectedIndex(); - - AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName()); - } - }); - c.gridx = 1; - c.gridy = row++; - c.gridwidth = 2; - c.fill = GridBagConstraints.BOTH; - c.anchor = GridBagConstraints.WEST; - pane.add(look_and_feel_value, c); - look_and_feel_value.setToolTipText("Look&feel used for new windows"); - - /* Serial debug setting */ - c.gridx = 0; - c.gridy = row; - c.gridwidth = 1; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(new JLabel("Serial Debug"), c); - - serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug()); - serial_debug.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - JRadioButton item = (JRadioButton) e.getSource(); - boolean enabled = item.isSelected(); - AltosUIPreferences.set_serial_debug(enabled); - } - }); - serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console"); - - c.gridx = 1; - c.gridy = row++; - c.gridwidth = 3; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(serial_debug, c); - - manage_bluetooth = new JButton("Manage Bluetooth"); + public void add_bluetooth() { + JButton manage_bluetooth = new JButton("Manage Bluetooth"); manage_bluetooth.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AltosBTManage.show(owner, AltosBTKnown.bt_known()); } }); - c.gridx = 0; - c.gridy = row; - c.gridwidth = 2; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(manage_bluetooth, c); + pane.add(manage_bluetooth, constraints(0, 2)); + /* in the same row as add_frequencies, so don't bump row */ + } - manage_frequencies = new JButton("Manage Frequencies"); + public void add_frequencies() { + JButton manage_frequencies = new JButton("Manage Frequencies"); manage_frequencies.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AltosConfigFreqUI.show(owner); } }); manage_frequencies.setToolTipText("Configure which values are shown in frequency menus"); - c.gridx = 2; - c.gridx = 2; - c.gridy = row++; - c.gridwidth = 2; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.WEST; - pane.add(manage_frequencies, c); + pane.add(manage_frequencies, constraints(2, 1)); + row++; + } - /* And a close button at the bottom */ - close = new JButton("Close"); - close.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - setVisible(false); - } - }); - c.gridx = 0; - c.gridy = row++; - c.gridwidth = 3; - c.fill = GridBagConstraints.NONE; - c.anchor = GridBagConstraints.CENTER; - pane.add(close, c); + public AltosConfigureUI(JFrame owner, AltosVoice voice) { + super(owner); - pack(); - setLocationRelativeTo(owner); - setVisible(true); + this.voice = voice; } } diff --git a/altosui/Makefile.am b/altosui/Makefile.am index a42426cd..494539ae 100644 --- a/altosui/Makefile.am +++ b/altosui/Makefile.am @@ -6,7 +6,7 @@ man_MANS=altosui.1 altoslibdir=$(libdir)/altos -CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH=".:classes:../altoslib/*:../libaltos:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar:$(FREETTS)/freetts.jar" +CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH="./*:$(JAVAROOT):../libaltos:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar:$(FREETTS)/freetts.jar" bin_SCRIPTS=altosui @@ -109,6 +109,9 @@ FREETTS_CLASS= \ ALTOSLIB_CLASS=\ AltosLib.jar +ALTOSUILIB_CLASS=\ + altosuilib.jar + LIBALTOS= \ libaltos.so \ libaltos.dylib \ @@ -172,7 +175,7 @@ LINUX_DIST=Altos-Linux-$(VERSION).tar.bz2 MACOSX_DIST=Altos-Mac-$(VERSION).zip WINDOWS_DIST=Altos-Windows-$(VERSION_DASH).exe -FAT_FILES=$(FATJAR) $(ALTOSLIB_CLASS) $(FREETTS_CLASS) $(JFREECHART_CLASS) $(JCOMMON_CLASS) +FAT_FILES=$(FATJAR) $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) $(FREETTS_CLASS) $(JFREECHART_CLASS) $(JCOMMON_CLASS) LINUX_FILES=$(FAT_FILES) libaltos.so $(FIRMWARE) $(DOC) LINUX_EXTRA=altosui-fat @@ -187,7 +190,7 @@ all-local: classes/altosui $(JAR) altosui altosui-test altosui-jdb clean-local: -rm -rf classes $(JAR) $(FATJAR) \ - $(LINUX_DIST) $(MACOSX_DIST) windows $(WINDOWS_DIST) $(ALTOSLIB_CLASS) $(FREETTS_CLASS) \ + $(LINUX_DIST) $(MACOSX_DIST) windows $(WINDOWS_DIST) $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) $(FREETTS_CLASS) \ $(JFREECHART_CLASS) $(JCOMMON_CLASS) $(LIBALTOS) Manifest.txt Manifest-fat.txt altos-windows.log \ altosui altosui-test altosui-jdb macosx linux @@ -229,13 +232,13 @@ install-altosuiJAVA: altosui.jar classes/altosui: mkdir -p classes/altosui -$(JAR): classaltosui.stamp Manifest.txt $(JAVA_ICONS) $(ALTOSLIB_CLASS) +$(JAR): classaltosui.stamp Manifest.txt $(JAVA_ICONS) $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) jar cfm $@ Manifest.txt \ $(ICONJAR) \ -C classes altosui \ -C ../libaltos libaltosJNI -$(FATJAR): classaltosui.stamp Manifest-fat.txt $(ALTOSLIB_CLASS) $(FREETTS_CLASS) $(JFREECHART_CLASS) $(JCOMMON_CLASS) $(LIBALTOS) $(JAVA_ICONS) +$(FATJAR): classaltosui.stamp Manifest-fat.txt $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) $(FREETTS_CLASS) $(JFREECHART_CLASS) $(JCOMMON_CLASS) $(LIBALTOS) $(JAVA_ICONS) jar cfm $@ Manifest-fat.txt \ $(ICONJAR) \ -C classes altosui \ @@ -243,25 +246,25 @@ $(FATJAR): classaltosui.stamp Manifest-fat.txt $(ALTOSLIB_CLASS) $(FREETTS_CLASS Manifest.txt: Makefile echo 'Main-Class: altosui.AltosUI' > $@ - echo "Class-Path: AltosLib.jar $(FREETTS)/freetts.jar $(JCOMMON)/jcommon.jar $(JFREECHART)/jfreechart.jar" >> $@ + echo "Class-Path: $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) $(FREETTS)/freetts.jar $(JCOMMON)/jcommon.jar $(JFREECHART)/jfreechart.jar" >> $@ Manifest-fat.txt: echo 'Main-Class: altosui.AltosUI' > $@ - echo "Class-Path: AltosLib.jar freetts.jar jcommon.jar jfreechart.jar" >> $@ + echo "Class-Path: $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) freetts.jar jcommon.jar jfreechart.jar" >> $@ altosui: Makefile echo "#!/bin/sh" > $@ - echo 'exec java -cp "$(FREETTS)/freetts.jar:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar" -Djava.library.path="$(altoslibdir)" -jar "$(altosuidir)/altosui.jar" "$$@"' >> $@ + echo 'exec java -Djava.library.path="$(altoslibdir)" -jar "$(altosuidir)/altosui.jar" "$$@"' >> $@ chmod +x $@ altosui-test: Makefile echo "#!/bin/sh" > $@ - echo 'exec java -cp "./*:../libaltos:$(FREETTS)/freetts.jar:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar" -Djava.library.path="../libaltos/.libs" -jar altosui.jar "$$@"' >> $@ + echo 'exec java -Djava.library.path="../libaltos/.libs" -jar altosui.jar "$$@"' >> $@ chmod +x $@ altosui-jdb: Makefile echo "#!/bin/sh" > $@ - echo 'exec jdb -classpath "classes:../libaltos:$(FREETTS)/freetts.jar:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar" -Djava.library.path="../libaltos/.libs" altosui/AltosUI "$$@"' >> $@ + echo 'exec jdb -classpath "classes:./*:../libaltos:$(FREETTS)/freetts.jar:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar" -Djava.library.path="../libaltos/.libs" altosui/AltosUI "$$@"' >> $@ chmod +x $@ libaltos.so: build-libaltos @@ -298,6 +301,10 @@ $(ALTOSLIB_CLASS): -rm -f "$@" $(LN_S) ../altoslib/"$@" . +$(ALTOSUILIB_CLASS): + -rm -f "$@" + $(LN_S) ../altosuilib/"$@" . + $(FREETTS_CLASS): -rm -f "$@" $(LN_S) "$(FREETTS)"/"$@" . @@ -329,6 +336,7 @@ $(MACOSX_DIST): $(MACOSX_FILES) $(MACOSX_EXTRA) cp -p $(FATJAR) macosx/AltosUI.app/Contents/Resources/Java/altosui.jar cp -p libaltos.dylib macosx/AltosUI.app/Contents/Resources/Java cp -p $(ALTOSLIB_CLASS) macosx/AltosUI.app/Contents/Resources/Java + cp -p $(ALTOSUILIB_CLASS) macosx/AltosUI.app/Contents/Resources/Java cp -p $(FREETTS_CLASS) macosx/AltosUI.app/Contents/Resources/Java cp -p $(JFREECHART_CLASS) macosx/AltosUI.app/Contents/Resources/Java cp -p $(JCOMMON_CLASS) macosx/AltosUI.app/Contents/Resources/Java diff --git a/altosuilib/AltosUIConfigure.java b/altosuilib/AltosUIConfigure.java index ef44f0be..6c9a841e 100644 --- a/altosuilib/AltosUIConfigure.java +++ b/altosuilib/AltosUIConfigure.java @@ -66,52 +66,14 @@ class DelegatingRenderer implements ListCellRenderer { public class AltosUIConfigure extends AltosUIDialog - implements DocumentListener { - JFrame owner; - Container pane; + public JFrame owner; + public Container pane; - JRadioButton enable_voice; - JButton test_voice; - JButton close; - - JButton configure_log; - JTextField log_directory; - - JLabel callsign_label; - JTextField callsign_value; - - JRadioButton imperial_units; - - JLabel font_size_label; - JComboBox font_size_value; - - JLabel look_and_feel_label; - JComboBox look_and_feel_value; - - JRadioButton serial_debug; - - JButton manage_bluetooth; - JButton manage_frequencies; - - int row; + public int row; final static String[] font_size_names = { "Small", "Medium", "Large" }; - /* DocumentListener interface methods */ - public void changedUpdate(DocumentEvent e) { - if (callsign_value != null) - AltosUIPreferences.set_callsign(callsign_value.getText()); - } - - public void insertUpdate(DocumentEvent e) { - changedUpdate(e); - } - - public void removeUpdate(DocumentEvent e) { - changedUpdate(e); - } - public GridBagConstraints constraints (int x, int width, int fill) { GridBagConstraints c = new GridBagConstraints(); Insets insets = new Insets(4, 4, 4, 4); @@ -120,6 +82,8 @@ public class AltosUIConfigure c.fill = fill; if (width == 3) c.anchor = GridBagConstraints.CENTER; + else if (x == 2) + c.anchor = GridBagConstraints.EAST; else c.anchor = GridBagConstraints.WEST; c.gridx = x; @@ -133,59 +97,13 @@ public class AltosUIConfigure } public void add_voice() { -// GridBagConstraints c = new GridBagConstraints(); -// -// /* Voice settings */ -// c.gridx = 0; -// c.gridy = row; -// c.gridwidth = 1; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(new JLabel("Voice"), c); -// -// enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice()); -// enable_voice.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// JRadioButton item = (JRadioButton) e.getSource(); -// boolean enabled = item.isSelected(); -// AltosUIPreferences.set_voice(enabled); -// if (enabled) -// voice.speak_always("Enable voice."); -// else -// voice.speak_always("Disable voice."); -// } -// }); -// c.gridx = 1; -// c.gridy = row; -// c.gridwidth = 1; -// c.weightx = 1; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(enable_voice, c); -// enable_voice.setToolTipText("Enable/Disable all audio in-flight announcements"); -// -// c.gridx = 2; -// c.gridy = row++; -// c.gridwidth = 1; -// c.weightx = 1; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.EAST; -// test_voice = new JButton("Test Voice"); -// test_voice.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// voice.speak("That's one small step for man; one giant leap for mankind."); -// } -// }); -// pane.add(test_voice, c); -// test_voice.setToolTipText("Play a stock audio clip to check volume"); -// row++; } public void add_log_dir() { /* Log directory settings */ pane.add(new JLabel("Log Directory"), constraints(0, 1)); - configure_log = new JButton(AltosUIPreferences.logdir().getPath()); + final JButton configure_log = new JButton(AltosUIPreferences.logdir().getPath()); configure_log.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AltosUIPreferences.ConfigureLog(); @@ -198,21 +116,13 @@ public class AltosUIConfigure } public void add_callsign() { -// /* Callsign setting */ -// pane.add(new JLabel("Callsign"), constraints(0, 1)); -// -// callsign_value = new JTextField(AltosUIPreferences.callsign()); -// callsign_value.getDocument().addDocumentListener(this); -// callsign_value.setToolTipText("Callsign sent in packet mode"); -// pane.add(callsign_value, constraints(1, 2, GridBagConstraints.BOTH)); -// row++; } public void add_units() { /* Imperial units setting */ pane.add(new JLabel("Imperial Units"), constraints(0, 1)); - imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units()); + JRadioButton imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units()); imperial_units.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JRadioButton item = (JRadioButton) e.getSource(); @@ -229,7 +139,7 @@ public class AltosUIConfigure /* Font size setting */ pane.add(new JLabel("Font size"), constraints(0, 1)); - font_size_value = new JComboBox(font_size_names); + final JComboBox font_size_value = new JComboBox(font_size_names); int font_size = AltosUIPreferences.font_size(); font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small); font_size_value.addActionListener(new ActionListener() { @@ -271,7 +181,7 @@ public class AltosUIConfigure final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels(); - look_and_feel_value = new JComboBox(look_and_feels); + final JComboBox look_and_feel_value = new JComboBox(look_and_feels); DelegatingRenderer.install(look_and_feel_value); @@ -298,7 +208,7 @@ public class AltosUIConfigure /* Serial debug setting */ pane.add(new JLabel("Serial Debug"), constraints(0, 1)); - serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug()); + JRadioButton serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug()); serial_debug.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JRadioButton item = (JRadioButton) e.getSource(); @@ -307,47 +217,18 @@ public class AltosUIConfigure } }); serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console"); + c.gridx = 1; + c.gridy = row++; + c.gridwidth = 3; + c.fill = GridBagConstraints.NONE; + c.anchor = GridBagConstraints.WEST; + pane.add(serial_debug, c); } public void add_bluetooth() { -// GridBagConstraints c = new GridBagConstraints(); -// c.gridx = 1; -// c.gridy = row++; -// c.gridwidth = 3; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(serial_debug, c); -// -// manage_bluetooth = new JButton("Manage Bluetooth"); -// manage_bluetooth.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// AltosBTManage.show(owner, AltosBTKnown.bt_known()); -// } -// }); -// c.gridx = 0; -// c.gridy = row; -// c.gridwidth = 2; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(manage_bluetooth, c); } public void add_frequencies() { -// GridBagConstraints c = new GridBagConstraints(); -// manage_frequencies = new JButton("Manage Frequencies"); -// manage_frequencies.addActionListener(new ActionListener() { -// public void actionPerformed(ActionEvent e) { -// AltosConfigFreqUI.show(owner); -// } -// }); -// manage_frequencies.setToolTipText("Configure which values are shown in frequency menus"); -// c.gridx = 2; -// c.gridx = 2; -// c.gridy = row++; -// c.gridwidth = 2; -// c.fill = GridBagConstraints.NONE; -// c.anchor = GridBagConstraints.WEST; -// pane.add(manage_frequencies, c); } public AltosUIConfigure(JFrame in_owner) { @@ -378,7 +259,7 @@ public class AltosUIConfigure add_frequencies(); /* And a close button at the bottom */ - close = new JButton("Close"); + JButton close = new JButton("Close"); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am index d93d9415..d4aa0116 100644 --- a/altosuilib/Makefile.am +++ b/altosuilib/Makefile.am @@ -2,14 +2,13 @@ AM_JAVACFLAGS=-encoding UTF-8 -Xlint:deprecation JAVAROOT=bin -CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH="bin:../altoslib/*:../libaltos:$(FREETTS)/*:/usr/share/java/*" +CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH="$(JAVAROOT):../altoslib/*:../libaltos:/usr/share/java/*" SRC=. -BIN=bin/org/altusmetrum/AltosUILib -AltosUILibdir = $(datadir)/java +altosuilibdir = $(datadir)/java -AltosUILib_JAVA = \ +altosuilib_JAVA = \ AltosUIConfigure.java \ AltosDevice.java \ AltosDeviceDialog.java \ @@ -24,21 +23,21 @@ AltosUILib_JAVA = \ AltosUIVersion.java \ AltosUnitsListener.java -JAR=AltosUILib.jar +JAR=altosuilib.jar all-local: $(JAR) clean-local: - -rm -rf bin $(JAR) + -rm -rf $(JAVAROOT) $(JAR) -install-AltosUILibJAVA: $(JAR) +install-altosuilibjava: $(JAR) @$(NORMAL_INSTALL) - test -z "$(AltosUILibdir)" || $(MKDIR_P) "$(DESTDIR)$(AltosUILibdir)" - echo " $(INSTALL_DATA)" "$<" "'$(DESTDIR)$(AltosUILibdir)/$(JAR)"; \ - $(INSTALL_DATA) "$<" "$(DESTDIR)$(AltosUILibdir)" + test -z "$(altosuilibdir)" || $(MKDIR_P) "$(DESTDIR)$(altosuilibdir)" + echo " $(INSTALL_DATA)" "$<" "'$(DESTDIR)$(altosuilibdir)/$(JAR)"; \ + $(INSTALL_DATA) "$<" "$(DESTDIR)$(altosuilibdir)" -bin: - mkdir -p bin +$(JAVAROOT): + mkdir -p $(JAVAROOT) -$(JAR): classAltosUILib.stamp - jar cf $@ -C bin org +$(JAR): classaltosuilib.stamp + jar cf $@ -C $(JAVAROOT) . -- cgit v1.2.3 From 8af405f1ac4d1b930f10465fd0270a49176f16d1 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 2 Jan 2013 09:31:35 -0800 Subject: altosui: Use shared AltosDeviceDialog Signed-off-by: Keith Packard --- altosui/AltosBTDevice.java | 1 + altosui/AltosBTKnown.java | 1 + altosui/AltosConfig.java | 5 +- altosui/AltosConfigTD.java | 4 +- altosui/AltosDebug.java | 1 + altosui/AltosDeviceDialog.java | 185 --------------------------------- altosui/AltosDeviceUIDialog.java | 70 +++++++++++++ altosui/AltosEepromManage.java | 3 +- altosui/AltosFlash.java | 1 + altosui/AltosFlashUI.java | 3 +- altosui/AltosIdleMonitorUI.java | 3 +- altosui/AltosIgniteUI.java | 3 +- altosui/AltosLaunch.java | 1 + altosui/AltosLaunchUI.java | 3 +- altosui/AltosScanUI.java | 3 +- altosui/AltosSerial.java | 1 + altosui/AltosSerialInUseException.java | 1 + altosui/AltosUI.java | 3 +- altosui/AltosUSBDevice.java | 1 + altosui/Makefile.am | 3 +- altosuilib/AltosDeviceDialog.java | 35 +++---- 21 files changed, 114 insertions(+), 217 deletions(-) delete mode 100644 altosui/AltosDeviceDialog.java create mode 100644 altosui/AltosDeviceUIDialog.java (limited to 'altosuilib') diff --git a/altosui/AltosBTDevice.java b/altosui/AltosBTDevice.java index 03e7cbec..222b3c97 100644 --- a/altosui/AltosBTDevice.java +++ b/altosui/AltosBTDevice.java @@ -17,6 +17,7 @@ package altosui; import libaltosJNI.*; +import org.altusmetrum.altosuilib.*; public class AltosBTDevice extends altos_bt_device implements AltosDevice { diff --git a/altosui/AltosBTKnown.java b/altosui/AltosBTKnown.java index ae04ac8c..606c0349 100644 --- a/altosui/AltosBTKnown.java +++ b/altosui/AltosBTKnown.java @@ -18,6 +18,7 @@ package altosui; import java.util.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class AltosBTKnown implements Iterable { LinkedList devices = new LinkedList(); diff --git a/altosui/AltosConfig.java b/altosui/AltosConfig.java index e1ffebb4..1cd61a89 100644 --- a/altosui/AltosConfig.java +++ b/altosui/AltosConfig.java @@ -21,8 +21,9 @@ import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.concurrent.*; -import org.altusmetrum.AltosLib.*; import java.text.*; +import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class AltosConfig implements ActionListener { @@ -270,7 +271,7 @@ public class AltosConfig implements ActionListener { public AltosConfig(JFrame given_owner) { owner = given_owner; - device = AltosDeviceDialog.show(owner, Altos.product_any); + device = AltosDeviceUIDialog.show(owner, Altos.product_any); if (device != null) { try { serial_line = new AltosSerial(device); diff --git a/altosui/AltosConfigTD.java b/altosui/AltosConfigTD.java index e7b9b81f..794f8103 100644 --- a/altosui/AltosConfigTD.java +++ b/altosui/AltosConfigTD.java @@ -21,8 +21,8 @@ import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.concurrent.*; - import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class AltosConfigTD implements ActionListener { @@ -315,7 +315,7 @@ public class AltosConfigTD implements ActionListener { version = new string_ref("unknown"); product = new string_ref("unknown"); - device = AltosDeviceDialog.show(owner, Altos.product_basestation); + device = AltosDeviceUIDialog.show(owner, Altos.product_basestation); if (device != null) { try { serial_line = new AltosSerial(device); diff --git a/altosui/AltosDebug.java b/altosui/AltosDebug.java index 16b10c3a..482f4c36 100644 --- a/altosui/AltosDebug.java +++ b/altosui/AltosDebug.java @@ -18,6 +18,7 @@ package altosui; import java.io.*; +import org.altusmetrum.altosuilib.*; public class AltosDebug extends AltosSerial { diff --git a/altosui/AltosDeviceDialog.java b/altosui/AltosDeviceDialog.java deleted file mode 100644 index 0aeadae6..00000000 --- a/altosui/AltosDeviceDialog.java +++ /dev/null @@ -1,185 +0,0 @@ -/* - * 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 javax.swing.*; -import java.awt.*; -import java.awt.event.*; - -public class AltosDeviceDialog extends AltosDialog implements ActionListener { - - private AltosDevice value; - private JList list; - private JButton cancel_button; - private JButton select_button; - private JButton manage_bluetooth_button; - private Frame frame; - private int product; - - private AltosDevice getValue() { - return value; - } - - private AltosDevice[] devices() { - java.util.List usb_devices = AltosUSBDevice.list(product); - int num_devices = usb_devices.size(); - java.util.List bt_devices = AltosBTKnown.bt_known().list(product); - num_devices += bt_devices.size(); - AltosDevice[] devices = new AltosDevice[num_devices]; - - for (int i = 0; i < usb_devices.size(); i++) - devices[i] = usb_devices.get(i); - int off = usb_devices.size(); - for (int j = 0; j < bt_devices.size(); j++) - devices[off + j] = bt_devices.get(j); - return devices; - } - - private void update_devices() { - AltosDevice[] devices = devices(); - list.setListData(devices); - select_button.setEnabled(devices.length > 0); - } - - private AltosDeviceDialog (Frame in_frame, Component location, int in_product) { - super(in_frame, "Device Selection", true); - - product = in_product; - frame = in_frame; - value = null; - - AltosDevice[] devices = devices(); - - cancel_button = new JButton("Cancel"); - cancel_button.setActionCommand("cancel"); - cancel_button.addActionListener(this); - - manage_bluetooth_button = new JButton("Manage Bluetooth"); - manage_bluetooth_button.setActionCommand("manage"); - manage_bluetooth_button.addActionListener(this); - - select_button = new JButton("Select"); - select_button.setActionCommand("select"); - select_button.addActionListener(this); - if (devices.length == 0) - select_button.setEnabled(false); - getRootPane().setDefaultButton(select_button); - - list = new JList(devices) { - //Subclass JList to workaround bug 4832765, which can cause the - //scroll pane to not let the user easily scroll up to the beginning - //of the list. An alternative would be to set the unitIncrement - //of the JScrollBar to a fixed value. You wouldn't get the nice - //aligned scrolling, but it should work. - public int getScrollableUnitIncrement(Rectangle visibleRect, - int orientation, - int direction) { - int row; - if (orientation == SwingConstants.VERTICAL && - direction < 0 && (row = getFirstVisibleIndex()) != -1) { - Rectangle r = getCellBounds(row, row); - if ((r.y == visibleRect.y) && (row != 0)) { - Point loc = r.getLocation(); - loc.y--; - int prevIndex = locationToIndex(loc); - Rectangle prevR = getCellBounds(prevIndex, prevIndex); - - if (prevR == null || prevR.y >= r.y) { - return 0; - } - return prevR.height; - } - } - return super.getScrollableUnitIncrement( - visibleRect, orientation, direction); - } - }; - - list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - list.setLayoutOrientation(JList.HORIZONTAL_WRAP); - list.setVisibleRowCount(-1); - list.addMouseListener(new MouseAdapter() { - public void mouseClicked(MouseEvent e) { - if (e.getClickCount() == 2) { - select_button.doClick(); //emulate button click - } - } - }); - JScrollPane listScroller = new JScrollPane(list); - listScroller.setPreferredSize(new Dimension(400, 80)); - listScroller.setAlignmentX(LEFT_ALIGNMENT); - - //Create a container so that we can add a title around - //the scroll pane. Can't add a title directly to the - //scroll pane because its background would be white. - //Lay out the label and scroll pane from top to bottom. - JPanel listPane = new JPanel(); - listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS)); - - JLabel label = new JLabel("Select Device"); - label.setLabelFor(list); - listPane.add(label); - listPane.add(Box.createRigidArea(new Dimension(0,5))); - listPane.add(listScroller); - listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); - - //Lay out the buttons from left to right. - JPanel buttonPane = new JPanel(); - buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); - buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); - buttonPane.add(Box.createHorizontalGlue()); - buttonPane.add(cancel_button); - buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); - buttonPane.add(manage_bluetooth_button); - buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); - buttonPane.add(select_button); - - //Put everything together, using the content pane's BorderLayout. - Container contentPane = getContentPane(); - contentPane.add(listPane, BorderLayout.CENTER); - contentPane.add(buttonPane, BorderLayout.PAGE_END); - - //Initialize values. - if (devices != null && devices.length != 0) - list.setSelectedValue(devices[0], true); - pack(); - setLocationRelativeTo(location); - } - - //Handle clicks on the Set and Cancel buttons. - public void actionPerformed(ActionEvent e) { - if ("select".equals(e.getActionCommand())) - value = (AltosDevice)(list.getSelectedValue()); - if ("manage".equals(e.getActionCommand())) { - AltosBTManage.show(frame, AltosBTKnown.bt_known()); - update_devices(); - return; - } - setVisible(false); - } - - public static AltosDevice show (Component frameComp, int product) { - - Frame frame = JOptionPane.getFrameForComponent(frameComp); - AltosDeviceDialog dialog; - - dialog = new AltosDeviceDialog(frame, frameComp, product); - dialog.setVisible(true); - return dialog.getValue(); - } -} diff --git a/altosui/AltosDeviceUIDialog.java b/altosui/AltosDeviceUIDialog.java new file mode 100644 index 00000000..7ed599a3 --- /dev/null +++ b/altosui/AltosDeviceUIDialog.java @@ -0,0 +1,70 @@ +/* + * 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 javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import org.altusmetrum.altosuilib.*; + +public class AltosDeviceUIDialog extends AltosDeviceDialog { + + public AltosDevice[] devices() { + java.util.List usb_devices = AltosUSBDevice.list(product); + int num_devices = usb_devices.size(); + java.util.List bt_devices = AltosBTKnown.bt_known().list(product); + num_devices += bt_devices.size(); + AltosDevice[] devices = new AltosDevice[num_devices]; + + for (int i = 0; i < usb_devices.size(); i++) + devices[i] = usb_devices.get(i); + int off = usb_devices.size(); + for (int j = 0; j < bt_devices.size(); j++) + devices[off + j] = bt_devices.get(j); + return devices; + } + + public void add_bluetooth() { + JButton manage_bluetooth_button = new JButton("Manage Bluetooth"); + manage_bluetooth_button.setActionCommand("manage"); + manage_bluetooth_button.addActionListener(this); + buttonPane.add(manage_bluetooth_button); + buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); + } + + public void actionPerformed(ActionEvent e) { + super.actionPerformed(e); + if ("manage".equals(e.getActionCommand())) { + AltosBTManage.show(frame, AltosBTKnown.bt_known()); + update_devices(); + } + } + + public AltosDeviceUIDialog (Frame in_frame, Component location, int in_product) { + super(in_frame, location, in_product); + } + + public static AltosDevice show (Component frameComp, int product) { + Frame frame = JOptionPane.getFrameForComponent(frameComp); + AltosDeviceUIDialog dialog; + + dialog = new AltosDeviceUIDialog(frame, frameComp, product); + dialog.setVisible(true); + return dialog.getValue(); + } +} diff --git a/altosui/AltosEepromManage.java b/altosui/AltosEepromManage.java index b8de77da..cc9adb0c 100644 --- a/altosui/AltosEepromManage.java +++ b/altosui/AltosEepromManage.java @@ -22,6 +22,7 @@ import javax.swing.*; import java.io.*; import java.util.concurrent.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class AltosEepromManage implements ActionListener { @@ -197,7 +198,7 @@ public class AltosEepromManage implements ActionListener { //boolean running = false; frame = given_frame; - device = AltosDeviceDialog.show(frame, Altos.product_any); + device = AltosDeviceUIDialog.show(frame, Altos.product_any); remote = false; diff --git a/altosui/AltosFlash.java b/altosui/AltosFlash.java index 313af70b..7a98ee14 100644 --- a/altosui/AltosFlash.java +++ b/altosui/AltosFlash.java @@ -20,6 +20,7 @@ package altosui; import java.awt.event.*; import javax.swing.*; import java.io.*; +import org.altusmetrum.altosuilib.*; public class AltosFlash { File file; diff --git a/altosui/AltosFlashUI.java b/altosui/AltosFlashUI.java index 3ccfa76c..878150f6 100644 --- a/altosui/AltosFlashUI.java +++ b/altosui/AltosFlashUI.java @@ -23,6 +23,7 @@ import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.io.*; import java.util.concurrent.*; +import org.altusmetrum.altosuilib.*; public class AltosFlashUI extends AltosDialog @@ -175,7 +176,7 @@ public class AltosFlashUI } boolean select_debug_dongle() { - debug_dongle = AltosDeviceDialog.show(frame, Altos.product_any); + debug_dongle = AltosDeviceUIDialog.show(frame, Altos.product_any); if (debug_dongle == null) return false; diff --git a/altosui/AltosIdleMonitorUI.java b/altosui/AltosIdleMonitorUI.java index 6f696009..6d3450b6 100644 --- a/altosui/AltosIdleMonitorUI.java +++ b/altosui/AltosIdleMonitorUI.java @@ -23,6 +23,7 @@ import javax.swing.*; import java.io.*; import java.util.concurrent.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener, AltosIdleMonitorListener { AltosDevice device; @@ -90,7 +91,7 @@ public class AltosIdleMonitorUI extends AltosFrame implements AltosFlightDisplay public AltosIdleMonitorUI(JFrame in_owner) throws FileNotFoundException, AltosSerialInUseException, TimeoutException, InterruptedException { - device = AltosDeviceDialog.show(in_owner, Altos.product_any); + device = AltosDeviceUIDialog.show(in_owner, Altos.product_any); remote = false; if (!device.matchProduct(Altos.product_altimeter)) remote = true; diff --git a/altosui/AltosIgniteUI.java b/altosui/AltosIgniteUI.java index ec331259..f1742203 100644 --- a/altosui/AltosIgniteUI.java +++ b/altosui/AltosIgniteUI.java @@ -24,6 +24,7 @@ import java.io.*; import java.text.*; import java.util.concurrent.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class AltosIgniteUI extends AltosDialog @@ -304,7 +305,7 @@ public class AltosIgniteUI private boolean open() { command_queue = new LinkedBlockingQueue(); - device = AltosDeviceDialog.show(owner, Altos.product_any); + device = AltosDeviceUIDialog.show(owner, Altos.product_any); if (device != null) { IgniteHandler handler = new IgniteHandler(owner); Thread t = new Thread(handler); diff --git a/altosui/AltosLaunch.java b/altosui/AltosLaunch.java index de19221e..0bad80aa 100644 --- a/altosui/AltosLaunch.java +++ b/altosui/AltosLaunch.java @@ -20,6 +20,7 @@ package altosui; import java.io.*; import java.util.concurrent.*; import java.awt.*; +import org.altusmetrum.altosuilib.*; public class AltosLaunch { AltosDevice device; diff --git a/altosui/AltosLaunchUI.java b/altosui/AltosLaunchUI.java index 39b986c0..68c978dc 100644 --- a/altosui/AltosLaunchUI.java +++ b/altosui/AltosLaunchUI.java @@ -23,6 +23,7 @@ import javax.swing.*; import java.io.*; import java.text.*; import java.util.concurrent.*; +import org.altusmetrum.altosuilib.*; class FireButton extends JButton { protected void processMouseEvent(MouseEvent e) { @@ -370,7 +371,7 @@ public class AltosLaunchUI private boolean open() { command_queue = new LinkedBlockingQueue(); - device = AltosDeviceDialog.show(owner, Altos.product_any); + device = AltosDeviceUIDialog.show(owner, Altos.product_any); if (device != null) { LaunchHandler handler = new LaunchHandler(owner); Thread t = new Thread(handler); diff --git a/altosui/AltosScanUI.java b/altosui/AltosScanUI.java index 2a6e140a..6a8db0e0 100644 --- a/altosui/AltosScanUI.java +++ b/altosui/AltosScanUI.java @@ -26,6 +26,7 @@ import java.util.*; import java.text.*; import java.util.concurrent.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; class AltosScanResult { String callsign; @@ -327,7 +328,7 @@ public class AltosScanUI } private boolean open() { - device = AltosDeviceDialog.show(owner, Altos.product_basestation); + device = AltosDeviceUIDialog.show(owner, Altos.product_basestation); if (device == null) return false; try { diff --git a/altosui/AltosSerial.java b/altosui/AltosSerial.java index 771fdd5d..78d862d0 100644 --- a/altosui/AltosSerial.java +++ b/altosui/AltosSerial.java @@ -26,6 +26,7 @@ import java.util.*; import java.awt.*; import javax.swing.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; import libaltosJNI.*; diff --git a/altosui/AltosSerialInUseException.java b/altosui/AltosSerialInUseException.java index 7380f331..932a3684 100644 --- a/altosui/AltosSerialInUseException.java +++ b/altosui/AltosSerialInUseException.java @@ -16,6 +16,7 @@ */ package altosui; +import org.altusmetrum.altosuilib.*; public class AltosSerialInUseException extends Exception { public AltosDevice device; diff --git a/altosui/AltosUI.java b/altosui/AltosUI.java index dcc0de60..a385b8b3 100644 --- a/altosui/AltosUI.java +++ b/altosui/AltosUI.java @@ -23,6 +23,7 @@ import javax.swing.*; import java.io.*; import java.util.concurrent.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class AltosUI extends AltosFrame { public AltosVoice voice = new AltosVoice(); @@ -241,7 +242,7 @@ public class AltosUI extends AltosFrame { } private void ConnectToDevice() { - AltosDevice device = AltosDeviceDialog.show(AltosUI.this, + AltosDevice device = AltosDeviceUIDialog.show(AltosUI.this, Altos.product_basestation); if (device != null) diff --git a/altosui/AltosUSBDevice.java b/altosui/AltosUSBDevice.java index 3af7a7fa..5c6a8976 100644 --- a/altosui/AltosUSBDevice.java +++ b/altosui/AltosUSBDevice.java @@ -18,6 +18,7 @@ package altosui; import java.util.*; import libaltosJNI.*; +import org.altusmetrum.altosuilib.*; public class AltosUSBDevice extends altos_device implements AltosDevice { diff --git a/altosui/Makefile.am b/altosui/Makefile.am index b3aa1f55..593eeb0a 100644 --- a/altosui/Makefile.am +++ b/altosui/Makefile.am @@ -32,8 +32,7 @@ altosui_JAVA = \ AltosCSVUI.java \ AltosDebug.java \ AltosDescent.java \ - AltosDeviceDialog.java \ - AltosDevice.java \ + AltosDeviceUIDialog.java \ AltosUSBDevice.java \ AltosDisplayThread.java \ AltosEepromDelete.java \ diff --git a/altosuilib/AltosDeviceDialog.java b/altosuilib/AltosDeviceDialog.java index 82620b8b..cde545a7 100644 --- a/altosuilib/AltosDeviceDialog.java +++ b/altosuilib/AltosDeviceDialog.java @@ -27,22 +27,24 @@ public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionL private JList list; private JButton cancel_button; private JButton select_button; - private JButton manage_bluetooth_button; - private Frame frame; - private int product; - + public Frame frame; + public int product; + public JPanel buttonPane; + public AltosDevice getValue() { return value; } public abstract AltosDevice[] devices(); - private void update_devices() { + public void update_devices() { AltosDevice[] devices = devices(); list.setListData(devices); select_button.setEnabled(devices.length > 0); } + public void add_bluetooth() { } + public AltosDeviceDialog (Frame in_frame, Component location, int in_product) { super(in_frame, "Device Selection", true); @@ -56,10 +58,6 @@ public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionL cancel_button.setActionCommand("cancel"); cancel_button.addActionListener(this); -// manage_bluetooth_button = new JButton("Manage Bluetooth"); -// manage_bluetooth_button.setActionCommand("manage"); -// manage_bluetooth_button.addActionListener(this); - select_button = new JButton("Select"); select_button.setActionCommand("select"); select_button.addActionListener(this); @@ -126,14 +124,15 @@ public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionL listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //Lay out the buttons from left to right. - JPanel buttonPane = new JPanel(); + buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); buttonPane.add(Box.createHorizontalGlue()); buttonPane.add(cancel_button); buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); -// buttonPane.add(manage_bluetooth_button); -// buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); + + add_bluetooth(); + buttonPane.add(select_button); //Put everything together, using the content pane's BorderLayout. @@ -150,14 +149,12 @@ public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionL //Handle clicks on the Set and Cancel buttons. public void actionPerformed(ActionEvent e) { - if ("select".equals(e.getActionCommand())) + if ("select".equals(e.getActionCommand())) { value = (AltosDevice)(list.getSelectedValue()); -// if ("manage".equals(e.getActionCommand())) { -// AltosBTManage.show(frame, AltosBTKnown.bt_known()); -// update_devices(); -// return; -// } - setVisible(false); + setVisible(false); + } + if ("cancel".equals(e.getActionCommand())) + setVisible(false); } } -- cgit v1.2.3 From eb670e9b7576563d747ae5c9416371f145455ec1 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 2 Jan 2013 09:50:09 -0800 Subject: altosui: Remove duplicate AltosUSBDevice Signed-off-by: Keith Packard --- altosui/AltosUSBDevice.java | 112 ----------------------------------------- altosui/Makefile.am | 1 - altosuilib/AltosUSBDevice.java | 4 +- 3 files changed, 2 insertions(+), 115 deletions(-) delete mode 100644 altosui/AltosUSBDevice.java (limited to 'altosuilib') diff --git a/altosui/AltosUSBDevice.java b/altosui/AltosUSBDevice.java deleted file mode 100644 index 5c6a8976..00000000 --- a/altosui/AltosUSBDevice.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.util.*; -import libaltosJNI.*; -import org.altusmetrum.altosuilib.*; - -public class AltosUSBDevice extends altos_device implements AltosDevice { - - public String toString() { - String name = getName(); - if (name == null) - name = "Altus Metrum"; - return String.format("%-20.20s %4d %s", - name, getSerial(), getPath()); - } - - public String toShortString() { - String name = getName(); - if (name == null) - name = "Altus Metrum"; - return String.format("%s %d %s", - name, getSerial(), getPath()); - - } - - public String getErrorString() { - altos_error error = new altos_error(); - - libaltos.altos_get_last_error(error); - return String.format("%s (%d)", error.getString(), error.getCode()); - } - - public SWIGTYPE_p_altos_file open() { - return libaltos.altos_open(this); - } - - private boolean isAltusMetrum() { - if (getVendor() != Altos.vendor_altusmetrum) - return false; - if (getProduct() < Altos.product_altusmetrum_min) - return false; - if (getProduct() > Altos.product_altusmetrum_max) - return false; - return true; - } - - public boolean matchProduct(int want_product) { - - if (!isAltusMetrum()) - return false; - - if (want_product == Altos.product_any) - return true; - - if (want_product == Altos.product_basestation) - return matchProduct(Altos.product_teledongle) || - matchProduct(Altos.product_teleterra) || - matchProduct(Altos.product_telebt) || - matchProduct(Altos.product_megadongle); - - if (want_product == Altos.product_altimeter) - return matchProduct(Altos.product_telemetrum) || - matchProduct(Altos.product_megametrum); - - int have_product = getProduct(); - - if (have_product == Altos.product_altusmetrum) /* old devices match any request */ - return true; - - if (want_product == have_product) - return true; - - return false; - } - - static java.util.List list(int product) { - if (!Altos.load_library()) - return null; - - SWIGTYPE_p_altos_list list = libaltos.altos_list_start(); - - ArrayList device_list = new ArrayList(); - if (list != null) { - for (;;) { - AltosUSBDevice device = new AltosUSBDevice(); - if (libaltos.altos_list_next(list, device) == 0) - break; - if (device.matchProduct(product)) - device_list.add(device); - } - libaltos.altos_list_finish(list); - } - - return device_list; - } -} \ No newline at end of file diff --git a/altosui/Makefile.am b/altosui/Makefile.am index b97222de..539e14f6 100644 --- a/altosui/Makefile.am +++ b/altosui/Makefile.am @@ -33,7 +33,6 @@ altosui_JAVA = \ AltosDebug.java \ AltosDescent.java \ AltosDeviceUIDialog.java \ - AltosUSBDevice.java \ AltosDisplayThread.java \ AltosEepromDelete.java \ AltosEepromDownload.java \ diff --git a/altosuilib/AltosUSBDevice.java b/altosuilib/AltosUSBDevice.java index 2f4e0dc6..bab16fb0 100644 --- a/altosuilib/AltosUSBDevice.java +++ b/altosuilib/AltosUSBDevice.java @@ -50,7 +50,7 @@ public class AltosUSBDevice extends altos_device implements AltosDevice { return libaltos.altos_open(this); } - private boolean isAltusMetrum() { + public boolean isAltusMetrum() { if (getVendor() != AltosUILib.vendor_altusmetrum) return false; if (getProduct() < AltosUILib.product_altusmetrum_min) @@ -89,7 +89,7 @@ public class AltosUSBDevice extends altos_device implements AltosDevice { return false; } - static java.util.List list(int product) { + static public java.util.List list(int product) { if (!AltosUILib.load_library()) return null; -- cgit v1.2.3 From 1979063928f1cdfc75c01ec098164c2822a5138d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 2 Jan 2013 16:07:49 -0800 Subject: altosuilib: Fix install issues on Linux altosuilib.jar wasn't getting installed micropeak was using the wrong name Signed-off-by: Keith Packard --- altosuilib/Makefile.am | 6 +++--- micropeak/Makefile.am | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'altosuilib') diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am index d4aa0116..da5fb848 100644 --- a/altosuilib/Makefile.am +++ b/altosuilib/Makefile.am @@ -30,11 +30,11 @@ all-local: $(JAR) clean-local: -rm -rf $(JAVAROOT) $(JAR) -install-altosuilibjava: $(JAR) +install-altosuilibJAVA: $(JAR) @$(NORMAL_INSTALL) test -z "$(altosuilibdir)" || $(MKDIR_P) "$(DESTDIR)$(altosuilibdir)" - echo " $(INSTALL_DATA)" "$<" "'$(DESTDIR)$(altosuilibdir)/$(JAR)"; \ - $(INSTALL_DATA) "$<" "$(DESTDIR)$(altosuilibdir)" + echo " $(INSTALL_DATA)" "$(JAR)" "'$(DESTDIR)$(altosuilibdir)/$(JAR)"; \ + $(INSTALL_DATA) "$(JAR)" "$(DESTDIR)$(altosuilibdir)" $(JAVAROOT): mkdir -p $(JAVAROOT) diff --git a/micropeak/Makefile.am b/micropeak/Makefile.am index 89ff2fb3..c452db97 100644 --- a/micropeak/Makefile.am +++ b/micropeak/Makefile.am @@ -44,7 +44,7 @@ ALTOSLIB_CLASS=\ AltosLib.jar ALTOSUILIB_CLASS=\ - AltosUILib.jar + altosuilib.jar # Icons ICONDIR=$(top_srcdir)/icon @@ -221,9 +221,9 @@ $(WINDOWS_DIST): $(WINDOWS_FILES) micropeak-windows.nsi Manifest.txt: Makefile echo 'Main-Class: org.altusmetrum.micropeak.MicroPeak' > $@ - echo "Class-Path: AltosLib.jar AltosUILib.jar $(JCOMMON)/jcommon.jar $(JFREECHART)/jfreechart.jar" >> $@ + echo "Class-Path: $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) $(JCOMMON)/jcommon.jar $(JFREECHART)/jfreechart.jar" >> $@ Manifest-fat.txt: echo 'Main-Class: org.altusmetrum.micropeak.MicroPeak' > $@ - echo "Class-Path: AltosLib.jar AltosUILib.jar jcommon.jar jfreechart.jar" >> $@ + echo "Class-Path: $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) jcommon.jar jfreechart.jar" >> $@ -- cgit v1.2.3