From 9da9adc2718928de2af65a68cddbcc636cc3e9e8 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 25 Dec 2012 14:45:49 -0800 Subject: Add file chooser for MicroPeak Needs reasonable directory tracking Signed-off-by: Keith Packard --- micropeak/MicroFileChooser.java | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 micropeak/MicroFileChooser.java (limited to 'micropeak/MicroFileChooser.java') diff --git a/micropeak/MicroFileChooser.java b/micropeak/MicroFileChooser.java new file mode 100644 index 00000000..0fd63a27 --- /dev/null +++ b/micropeak/MicroFileChooser.java @@ -0,0 +1,65 @@ +/* + * 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 javax.swing.filechooser.FileNameExtensionFilter; +import java.io.*; +import org.altusmetrum.AltosLib.*; + +public class MicroFileChooser extends JFileChooser { + JFrame frame; + String filename; + File file; + + public String filename() { + return filename; + } + + public File file() { + return file; + } + + public InputStream runDialog() { + int ret; + + ret = showOpenDialog(frame); + if (ret == APPROVE_OPTION) { + file = getSelectedFile(); + if (file == null) + return null; + filename = file.getName(); + try { + return new FileInputStream(file); + } catch (FileNotFoundException fe) { + JOptionPane.showMessageDialog(frame, + fe.getMessage(), + "Cannot open file", + JOptionPane.ERROR_MESSAGE); + } + } + return null; + } + + public MicroFileChooser(JFrame in_frame) { + frame = in_frame; + setDialogTitle("Select MicroPeak Data File"); + setFileFilter(new FileNameExtensionFilter("MicroPeak data file", + "mpd")); + } +} -- cgit v1.2.3 From 56a1210a7b04a3623d19ec282f26fecc79c126dd Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Mon, 31 Dec 2012 11:42:57 -0800 Subject: micropeak: Use altosuilib This removes a pile of code stolen from altosui Signed-off-by: Keith Packard --- micropeak/.gitignore | 6 + micropeak/Makefile.am | 22 ++-- micropeak/MicroFileChooser.java | 2 + micropeak/MicroFontListener.java | 22 ---- micropeak/MicroFrame.java | 45 +------ micropeak/MicroGraph.java | 82 +++++++++--- micropeak/MicroPeak.java | 21 +++- micropeak/MicroPreferences.java | 221 --------------------------------- micropeak/MicroPreferencesBackend.java | 101 --------------- micropeak/MicroSerial.java | 4 +- micropeak/MicroUIListener.java | 22 ---- 11 files changed, 109 insertions(+), 439 deletions(-) create mode 100644 micropeak/.gitignore delete mode 100644 micropeak/MicroFontListener.java delete mode 100644 micropeak/MicroPreferences.java delete mode 100644 micropeak/MicroPreferencesBackend.java delete mode 100644 micropeak/MicroUIListener.java (limited to 'micropeak/MicroFileChooser.java') diff --git a/micropeak/.gitignore b/micropeak/.gitignore new file mode 100644 index 00000000..fc99b31c --- /dev/null +++ b/micropeak/.gitignore @@ -0,0 +1,6 @@ +*.jar +Manifest.txt +classes +*.stamp +micropeak +micropeak-test diff --git a/micropeak/Makefile.am b/micropeak/Makefile.am index 2cfd2ad3..fde981a6 100644 --- a/micropeak/Makefile.am +++ b/micropeak/Makefile.am @@ -1,7 +1,7 @@ JAVAROOT=classes AM_JAVACFLAGS=-encoding UTF-8 -Xlint:deprecation -CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH=".:classes:../altoslib/*:../libaltos:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar" +CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH=".:classes:../altoslib/*:../altosuilib/*:../libaltos:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar" bin_SCRIPTS=micropeak @@ -14,10 +14,6 @@ micropeak_JAVA= \ MicroGraph.java \ MicroSerial.java \ MicroFileChooser.java \ - MicroPreferences.java \ - MicroPreferencesBackend.java \ - MicroFontListener.java \ - MicroUIListener.java \ MicroUSB.java JFREECHART_CLASS= \ @@ -38,6 +34,9 @@ LIBALTOS= \ ALTOSLIB_CLASS=\ AltosLib.jar +ALTOSUILIB_CLASS=\ + AltosUILib.jar + # Icons ICONDIR=$(top_srcdir)/icon @@ -62,6 +61,7 @@ all-local: micropeak-test $(JAR) clean-local: -rm -rf classes $(JAR) $(FATJAR) \ $(ALTOSLIB_CLASS) \ + $(ALTOSUILIB_CLASS) \ $(JFREECHART_CLASS) $(JCOMMON_CLASS) $(LIBALTOS) Manifest.txt \ micropeak micropeak-test macosx linux windows @@ -75,13 +75,13 @@ micropeak-test: Makefile echo 'exec java -cp "./*:../libaltos/*:$(JCOMMON)/jcommon.jar:$(JFREECHART)/jfreechart.jar" -Djava.library.path="../libaltos/.libs" -jar micropeak.jar "$$@"' >> $@ chmod +x $@ -$(JAR): classmicropeak.stamp Manifest.txt $(JAVA_ICONS) $(ALTOSLIB_CLASS) +$(JAR): classmicropeak.stamp Manifest.txt $(JAVA_ICONS) $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) jar cfm $@ Manifest.txt \ $(ICONJAR) \ -C classes org \ -C ../libaltos libaltosJNI -$(FATJAR): classmicropeak.stamp Manifest-fat.txt $(ALTOSLIB_CLASS) $(JFREECHART_CLASS) $(JCOMMON_CLASS) $(JAVA_ICONS) +$(FATJAR): classmicropeak.stamp Manifest-fat.txt $(ALTOSLIB_CLASS) $(ALTOSUILIB_CLASS) $(JFREECHART_CLASS) $(JCOMMON_CLASS) $(JAVA_ICONS) jar cfm $@ Manifest-fat.txt \ $(ICONJAR) \ -C classes org \ @@ -122,6 +122,10 @@ $(ALTOSLIB_CLASS): -rm -f "$@" $(LN_S) ../altoslib/"$@" . +$(ALTOSUILIB_CLASS): + -rm -f "$@" + $(LN_S) ../altosuilib/"$@" . + $(JFREECHART_CLASS): -rm -f "$@" $(LN_S) "$(JFREECHART)"/"$@" . @@ -132,9 +136,9 @@ $(JCOMMON_CLASS): Manifest.txt: Makefile echo 'Main-Class: org.altusmetrum.micropeak.MicroPeak' > $@ - echo "Class-Path: AltosLib.jar $(JCOMMON)/jcommon.jar $(JFREECHART)/jfreechart.jar" >> $@ + echo "Class-Path: AltosLib.jar AltosUILib.jar $(JCOMMON)/jcommon.jar $(JFREECHART)/jfreechart.jar" >> $@ Manifest-fat.txt: echo 'Main-Class: org.altusmetrum.micropeak.MicroPeak' > $@ - echo "Class-Path: AltosLib.jar jcommon.jar jfreechart.jar" >> $@ + echo "Class-Path: AltosLib.jar AltosUILib.jar jcommon.jar jfreechart.jar" >> $@ diff --git a/micropeak/MicroFileChooser.java b/micropeak/MicroFileChooser.java index 0fd63a27..d2540987 100644 --- a/micropeak/MicroFileChooser.java +++ b/micropeak/MicroFileChooser.java @@ -21,6 +21,7 @@ import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.io.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class MicroFileChooser extends JFileChooser { JFrame frame; @@ -61,5 +62,6 @@ public class MicroFileChooser extends JFileChooser { setDialogTitle("Select MicroPeak Data File"); setFileFilter(new FileNameExtensionFilter("MicroPeak data file", "mpd")); + setCurrentDirectory(AltosUIPreferences.logdir()); } } diff --git a/micropeak/MicroFontListener.java b/micropeak/MicroFontListener.java deleted file mode 100644 index a902584c..00000000 --- a/micropeak/MicroFontListener.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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; - -public interface MicroFontListener { - void font_size_changed(int font_size); -} diff --git a/micropeak/MicroFrame.java b/micropeak/MicroFrame.java index a9b9a37a..03e3af0c 100644 --- a/micropeak/MicroFrame.java +++ b/micropeak/MicroFrame.java @@ -21,21 +21,10 @@ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; +import org.altusmetrum.altosuilib.*; -class MicroFrameListener extends WindowAdapter { - public void windowClosing (WindowEvent e) { - MicroPreferences.unregister_ui_listener((MicroFrame) e.getWindow()); - } -} - -public class MicroFrame extends JFrame implements MicroUIListener { - - public void ui_changed(String look_and_feel) { - SwingUtilities.updateComponentTreeUI(this); - this.pack(); - } - - static final String[] icon_names = { +public class MicroFrame extends AltosUIFrame { + static String[] micro_icon_names = { "/micropeak-16.png", "/micropeak-32.png", "/micropeak-48.png", @@ -44,31 +33,5 @@ public class MicroFrame extends JFrame implements MicroUIListener { "/micropeak-256.png" }; - public void set_icon() { - ArrayList icons = new ArrayList(); - - for (int i = 0; i < icon_names.length; i++) { - java.net.URL imgURL = MicroPeak.class.getResource(icon_names[i]); - if (imgURL != null) - icons.add(new ImageIcon(imgURL).getImage()); - } - - setIconImages(icons); - } - - public MicroFrame() { - super(); - MicroPreferences.set_component(this); - MicroPreferences.register_ui_listener(this); - addWindowListener(new MicroFrameListener()); - set_icon(); - } - - public MicroFrame(String name) { - super(name); - MicroPreferences.set_component(this); - MicroPreferences.register_ui_listener(this); - addWindowListener(new MicroFrameListener()); - set_icon(); - } + static { set_icon_names(micro_icon_names); } } diff --git a/micropeak/MicroGraph.java b/micropeak/MicroGraph.java index 9192cad9..38f54fe0 100644 --- a/micropeak/MicroGraph.java +++ b/micropeak/MicroGraph.java @@ -34,15 +34,47 @@ import org.jfree.chart.labels.*; import org.jfree.data.xy.*; import org.jfree.data.*; -public class MicroGraph { +class MicroSeries extends XYSeries { + NumberAxis axis; + String label; + String units; + Color color; + + String label() { + return String.format("%s (%s)", label, units); + } + + void set_units(String units) { + this.units = units; + + axis.setLabel(label()); + } + + public MicroSeries (String label, String units, Color color) { + super(label); + this.label = label; + this.units = units; + this.color = color; + + axis = new NumberAxis(label()); + axis.setLabelPaint(color); + axis.setTickLabelPaint(color); + } +} + +public class MicroGraph implements AltosUnitsListener { XYPlot plot; JFreeChart chart; ChartPanel panel; NumberAxis xAxis; - XYSeries heightSeries; - XYSeries speedSeries; - XYSeries accelSeries; + MicroSeries heightSeries; + MicroSeries speedSeries; + MicroSeries accelSeries; + + static final private Color red = new Color(194,31,31); + static final private Color green = new Color(31,194,31); + static final private Color blue = new Color(31,31,194); MicroData data; @@ -50,40 +82,50 @@ public class MicroGraph { return panel; } - private void addSeries(XYSeries series, int index, String label, String units) { + private MicroSeries addSeries(int index, String label, String units, Color color) { + MicroSeries series = new MicroSeries(label, units, color); XYSeriesCollection dataset = new XYSeriesCollection(series); - NumberAxis axis = new NumberAxis(String.format("%s (%s)", label, units)); XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false); + renderer.setSeriesPaint(0, color); renderer.setPlot(plot); renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})", units), new java.text.DecimalFormat("0.00"), new java.text.DecimalFormat("0.00"))); - plot.setRangeAxis(index, axis); + plot.setRangeAxis(index, series.axis); plot.setDataset(index, dataset); plot.setRenderer(index, renderer); plot.mapDatasetToRangeAxis(index, index); + return series; } - public void setData (MicroData data) { + public void resetData() { heightSeries.clear(); speedSeries.clear(); accelSeries.clear(); for (int i = 0; i < data.pressures.length; i++) { double x = data.time(i); - heightSeries.add(x, data.height(i)); - speedSeries.add(x, data.speed(i)); - accelSeries.add(x, data.acceleration(i)); + heightSeries.add(x, AltosConvert.height.value(data.height(i))); + speedSeries.add(x, AltosConvert.speed.value(data.speed(i))); + accelSeries.add(x, AltosConvert.accel.value(data.acceleration(i))); } } - public MicroGraph(MicroData data) { - + public void setData (MicroData data) { this.data = data; + resetData(); + } - heightSeries = new XYSeries("Height"); - speedSeries = new XYSeries("Speed"); - accelSeries = new XYSeries("Acceleration"); + public void units_changed(boolean imperial_units) { + if (data != null) { + heightSeries.set_units(AltosConvert.height.show_units()); + speedSeries.set_units(AltosConvert.speed.show_units()); + accelSeries.set_units(AltosConvert.accel.show_units()); + resetData(); + } + } + + public MicroGraph() { xAxis = new NumberAxis("Time (s)"); @@ -95,9 +137,9 @@ public class MicroGraph { plot.setDomainPannable(true); plot.setRangePannable(true); - addSeries(heightSeries, 0, "Height", "m"); - addSeries(speedSeries, 1, "Speed", "m/s"); - addSeries(accelSeries, 2, "Acceleration", "m/s²"); + heightSeries = addSeries(0, "Height", AltosConvert.height.show_units(), red); + speedSeries = addSeries(1, "Speed", AltosConvert.speed.show_units(), green); + accelSeries = addSeries(2, "Acceleration", AltosConvert.accel.show_units(), blue); chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT, plot, true); @@ -106,5 +148,7 @@ public class MicroGraph { panel = new ChartPanel(chart); panel.setMouseWheelEnabled(true); panel.setPreferredSize(new java.awt.Dimension(800, 500)); + + AltosPreferences.register_units_listener(this); } } \ No newline at end of file diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index c9074348..463238c8 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -24,6 +24,7 @@ import java.io.*; import java.util.concurrent.*; import java.util.*; import org.altusmetrum.AltosLib.*; +import org.altusmetrum.altosuilib.*; public class MicroPeak extends MicroFrame implements ActionListener, ItemListener { @@ -59,6 +60,10 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene RunFile(input); } + private void Preferences() { + new AltosConfigureUI(this); + } + private void DownloadData() { java.util.List devices = MicroUSB.list(); for (MicroUSB device : devices) @@ -66,7 +71,6 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene } public void actionPerformed(ActionEvent ev) { - System.out.printf("action %s %s\n", ev.getActionCommand(), ev.paramString()); if ("Exit".equals(ev.getActionCommand())) System.exit(0); else if ("Open".equals(ev.getActionCommand())) @@ -75,6 +79,8 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene new MicroPeak(); else if ("Download".equals(ev.getActionCommand())) DownloadData(); + else if ("Preferences".equals(ev.getActionCommand())) + Preferences(); } public void itemStateChanged(ItemEvent e) { @@ -82,7 +88,7 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene public MicroPeak() { - this.filename = filename; + AltosUIPreferences.set_component(this); pane = getContentPane(); @@ -106,6 +112,10 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene fileMenu.add(downloadAction); downloadAction.addActionListener(this); + JMenuItem preferencesAction = new JMenuItem("Preferences"); + fileMenu.add(preferencesAction); + preferencesAction.addActionListener(this); + JMenuItem exitAction = new JMenuItem("Exit"); fileMenu.add(exitAction); exitAction.addActionListener(this); @@ -118,7 +128,7 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene } }); - graph = new MicroGraph(data); + graph = new MicroGraph(); pane.add(graph.panel); pane.doLayout(); pane.validate(); @@ -136,6 +146,11 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene public static void main(final String[] args) { boolean opened = false; + try { + UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel()); + } catch (Exception e) { + } + for (int i = 0; i < args.length; i++) { MicroPeak m = new MicroPeak(); m.OpenFile(new File(args[i])); diff --git a/micropeak/MicroPreferences.java b/micropeak/MicroPreferences.java deleted file mode 100644 index 70c2557c..00000000 --- a/micropeak/MicroPreferences.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * 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 java.util.*; -import java.awt.Component; -import javax.swing.*; -import java.awt.*; -import org.altusmetrum.AltosLib.*; - -public class MicroPreferences extends AltosPreferences { - - 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); - } - - /* 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 = font_size_medium; - - static LinkedList ui_listeners; - - static String look_and_feel = null; - - /* Serial debug */ - static boolean serial_debug; - - public static void init() { - AltosPreferences.init(new MicroPreferencesBackend()); - - font_listeners = new LinkedList(); - - font_size = backend.getInt(fontSizePreference, font_size_medium); - set_fonts(font_size); - look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName()); - - ui_listeners = new LinkedList(); - serial_debug = backend.getBoolean(serialDebugPreference, false); - } - - static { init(); } - - 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(); - set_fonts(font_size); - for (MicroFontListener l : font_listeners) - l.font_size_changed(font_size); - } - } - - public static void register_font_listener(MicroFontListener l) { - synchronized (backend) { - font_listeners.add(l); - } - } - - public static void unregister_font_listener(MicroFontListener 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 (MicroUIListener 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(MicroUIListener l) { - synchronized(backend) { - ui_listeners.add(l); - } - } - - public static void unregister_ui_listener(MicroUIListener l) { - synchronized (backend) { - ui_listeners.remove(l); - } - } - public static void set_serial_debug(boolean 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/micropeak/MicroPreferencesBackend.java b/micropeak/MicroPreferencesBackend.java deleted file mode 100644 index 7d92f6be..00000000 --- a/micropeak/MicroPreferencesBackend.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * 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.micropeak; - -import java.io.File; -import java.util.prefs.*; -import org.altusmetrum.AltosLib.*; -import javax.swing.filechooser.FileSystemView; - -public class MicroPreferencesBackend implements AltosPreferencesBackend { - - private Preferences _preferences = null; - - public MicroPreferencesBackend() { - _preferences = Preferences.userRoot().node("/org/altusmetrum/altosui"); - } - - public MicroPreferencesBackend(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 MicroPreferencesBackend(_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/micropeak/MicroSerial.java b/micropeak/MicroSerial.java index 8546276e..a1a77a1d 100644 --- a/micropeak/MicroSerial.java +++ b/micropeak/MicroSerial.java @@ -16,16 +16,18 @@ */ package org.altusmetrum.micropeak; + import java.util.*; import java.io.*; import libaltosJNI.*; +import org.altusmetrum.altosuilib.*; public class MicroSerial extends InputStream { SWIGTYPE_p_altos_file file; public int read() { int c = libaltos.altos_getchar(file, 0); - if (MicroPreferences.serial_debug) + if (AltosUIPreferences.serial_debug) System.out.printf("%c", c); return c; } diff --git a/micropeak/MicroUIListener.java b/micropeak/MicroUIListener.java deleted file mode 100644 index 9aed8dae..00000000 --- a/micropeak/MicroUIListener.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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; - -public interface MicroUIListener { - public void ui_changed(String look_and_feel); -} -- cgit v1.2.3 From f20781010a6560b7b359af269c502d098917c446 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 3 Jan 2013 17:31:01 -0800 Subject: micropeak: Add command line export option micropeak --export will create full of useful data. Signed-off-by: Keith Packard --- micropeak/MicroData.java | 9 ++++ micropeak/MicroExport.java | 15 +++---- micropeak/MicroFileChooser.java | 18 ++------ micropeak/MicroPeak.java | 94 ++++++++++++++++++++++++++++++++--------- micropeak/MicroSave.java | 10 +++-- 5 files changed, 100 insertions(+), 46 deletions(-) (limited to 'micropeak/MicroFileChooser.java') diff --git a/micropeak/MicroData.java b/micropeak/MicroData.java index 836d3c35..fdfb2dc4 100644 --- a/micropeak/MicroData.java +++ b/micropeak/MicroData.java @@ -263,6 +263,15 @@ public class MicroData { f.write('\n'); } + public void export (Writer f) throws IOException { + PrintWriter pw = new PrintWriter(f); + pw.printf(" Time, Press, Height, Speed, Accel\n"); + for (MicroDataPoint point : points()) { + pw.printf("%6.3f,%6.0f,%7.1f,%7.2f,%7.2f\n", + point.time, point.pressure, point.height, point.speed, point.accel); + } + } + public void set_name(String name) { this.name = name; } diff --git a/micropeak/MicroExport.java b/micropeak/MicroExport.java index 219184da..4b83bb4d 100644 --- a/micropeak/MicroExport.java +++ b/micropeak/MicroExport.java @@ -31,6 +31,12 @@ public class MicroExport extends JFileChooser { JFrame frame; MicroData data; + public static void export(File file, MicroData data) throws FileNotFoundException, IOException { + FileWriter fw = new FileWriter(file); + data.export(fw); + fw.close(); + } + public boolean runDialog() { int ret; @@ -76,14 +82,7 @@ public class MicroExport extends JFileChooser { } } try { - FileWriter fw = new FileWriter(file); - PrintWriter pw = new PrintWriter(fw); - pw.printf(" Time, Press, Height, Speed, Accel\n"); - for (MicroDataPoint point : data.points()) { - pw.printf("%6.3f,%6.0f,%7.1f,%7.2f,%7.2f\n", - point.time, point.pressure, point.height, point.speed, point.accel); - } - fw.close(); + export(file, data); return true; } catch (FileNotFoundException fe) { JOptionPane.showMessageDialog(frame, diff --git a/micropeak/MicroFileChooser.java b/micropeak/MicroFileChooser.java index d2540987..21ddb0f8 100644 --- a/micropeak/MicroFileChooser.java +++ b/micropeak/MicroFileChooser.java @@ -36,24 +36,12 @@ public class MicroFileChooser extends JFileChooser { return file; } - public InputStream runDialog() { + public File runDialog() { int ret; ret = showOpenDialog(frame); - if (ret == APPROVE_OPTION) { - file = getSelectedFile(); - if (file == null) - return null; - filename = file.getName(); - try { - return new FileInputStream(file); - } catch (FileNotFoundException fe) { - JOptionPane.showMessageDialog(frame, - fe.getMessage(), - "Cannot open file", - JOptionPane.ERROR_MESSAGE); - } - } + if (ret == APPROVE_OPTION) + return getSelectedFile(); return null; } diff --git a/micropeak/MicroPeak.java b/micropeak/MicroPeak.java index d4252fa9..185fa67e 100644 --- a/micropeak/MicroPeak.java +++ b/micropeak/MicroPeak.java @@ -56,40 +56,41 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene setTitle(name); } - private void RunFile(InputStream input, String name) { + private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException { + MicroData data = null; + FileInputStream fis = new FileInputStream(filename); try { - MicroData data = new MicroData(input, name); - SetData(data); - } catch (IOException ioe) { - JOptionPane.showMessageDialog(this, - ioe.getMessage(), - "File Read Error", - JOptionPane.ERROR_MESSAGE); + data = new MicroData((InputStream) fis, filename.getName()); } catch (InterruptedException ie) { + data = null; + } finally { + fis.close(); } - try { - input.close(); - } catch (IOException ioe) { - } + return data; } private void OpenFile(File filename) { try { - RunFile (new FileInputStream(filename), filename.getName()); + SetData(ReadFile(filename)); } catch (FileNotFoundException fne) { JOptionPane.showMessageDialog(this, fne.getMessage(), "Cannot open file", JOptionPane.ERROR_MESSAGE); + } catch (IOException ioe) { + JOptionPane.showMessageDialog(this, + ioe.getMessage(), + "File Read Error", + JOptionPane.ERROR_MESSAGE); } } private void SelectFile() { MicroFileChooser chooser = new MicroFileChooser(this); - InputStream input = chooser.runDialog(); + File file = chooser.runDialog(); - if (input != null) - RunFile(input, chooser.filename); + if (file != null) + OpenFile(file); } private void Preferences() { @@ -109,6 +110,7 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene "No data", JOptionPane.INFORMATION_MESSAGE); } + private void Save() { if (data == null) { no_data(); @@ -128,6 +130,30 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene export.runDialog(); } + private static void CommandGraph(File file) { + MicroPeak m = new MicroPeak(); + m.OpenFile(file); + } + + private static void CommandExport(File file) { + try { + MicroData d = ReadFile(file); + if (d != null) { + File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv")); + try { + System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath()); + MicroExport.export(csv, d); + } catch (FileNotFoundException fe) { + System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage()); + } catch (IOException ie) { + System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage()); + } + } + } catch (IOException ie) { + System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage()); + } + } + private void Close() { setVisible(false); dispose(); @@ -233,8 +259,17 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene setVisible(true); } + public static void help(int code) { + System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n"); + System.out.printf(" Options:\n"); + System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n"); + System.out.printf(" --graph\tgraph a flight\n"); + System.exit(code); + } + public static void main(final String[] args) { boolean opened = false; + boolean graphing = true; try { UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel()); @@ -242,11 +277,30 @@ public class MicroPeak extends MicroFrame implements ActionListener, ItemListene } for (int i = 0; i < args.length; i++) { - MicroPeak m = new MicroPeak(); - m.OpenFile(new File(args[i])); - opened = true; + System.out.printf ("Arg %d: %s\n", i, args[i]); + if (args[i].equals("--help")) + help(0); + else if (args[i].equals("--export")) + graphing = false; + else if (args[i].equals("--graph")) + graphing = true; + else if (args[i].startsWith("--")) + help(1); + else { + File file = new File(args[i]); + try { + if (graphing) + CommandGraph(file); + else + CommandExport(file); + opened = true; + } catch (Exception e) { + System.err.printf("Error processing \"%s\": %s\n", + file.getName(), e.getMessage()); + } + } } if (!opened) new MicroPeak(); } -} \ No newline at end of file +} diff --git a/micropeak/MicroSave.java b/micropeak/MicroSave.java index cb4b4221..2664f170 100644 --- a/micropeak/MicroSave.java +++ b/micropeak/MicroSave.java @@ -32,6 +32,12 @@ public class MicroSave extends JFileChooser { JFrame frame; MicroData data; + public static void save(File file, MicroData data) throws FileNotFoundException, IOException { + FileOutputStream fos = new FileOutputStream(file); + data.save(fos); + fos.close(); + } + public boolean runDialog() { int ret; @@ -76,9 +82,7 @@ public class MicroSave extends JFileChooser { } } try { - FileOutputStream fos = new FileOutputStream(file); - data.save(fos); - fos.close(); + save(file, data); data.set_name(filename); return true; } catch (FileNotFoundException fe) { -- cgit v1.2.3