2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.micropeak;
22 import java.awt.event.*;
25 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_14.*;
28 import org.altusmetrum.altosuilib_14.*;
30 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener, AltosFilterListener {
35 AltosFlightStatsTable statsTable;
41 static int number_of_windows;
44 final static String open_command = "open";
45 final static String save_command = "save";
46 final static String export_command = "export";
47 final static String preferences_command = "preferences";
48 final static String close_command = "close";
49 final static String exit_command = "exit";
51 static final String[][] file_menu_entries = new String[][] {
52 { "Open", open_command },
53 { "Save a Copy", save_command },
54 { "Export Data", export_command },
55 { "Preferences", preferences_command },
56 { "Close", close_command },
57 { "Exit", exit_command },
61 final static String download_command = "download";
62 final static String download_label = "Download";
64 static final String[][] download_menu_entries = new String[][] {
65 { download_label, download_command }
68 MicroPeak SetData(MicroData data) {
70 if (this.data != null) {
72 return mp.SetData(data);
75 if (data.flight_series == null)
76 System.out.printf("no data in flight\n");
77 if (data.flight_stats == null)
78 System.out.printf("no stats in flight\n");
79 graph.set_data(data.flight_stats, data.flight_series);
80 statsTable.set_stats(data.flight_stats);
86 void SetName(String name) {
91 private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException {
92 MicroData data = null;
93 FileInputStream fis = new FileInputStream(filename);
95 data = new MicroData((InputStream) fis, filename.getName());
96 AltosUIPreferences.set_last_logdir(filename);
97 } catch (MicroData.NonHexcharException nhe) {
99 } catch (MicroData.FileEndedException nhe) {
101 } catch (InterruptedException ie) {
109 private void OpenFile(File filename) {
111 SetData(ReadFile(filename));
112 } catch (FileNotFoundException fne) {
113 JOptionPane.showMessageDialog(this,
116 JOptionPane.ERROR_MESSAGE);
117 } catch (IOException ioe) {
118 JOptionPane.showMessageDialog(this,
121 JOptionPane.ERROR_MESSAGE);
125 private void SelectFile() {
126 MicroFileChooser chooser = new MicroFileChooser(this);
127 File file = chooser.runDialog();
133 private void Preferences() {
134 new AltosUIConfigure(this);
137 private void DownloadData() {
138 AltosDevice device = MicroDeviceDialog.show(this);
139 MicroSerial serial = null;
141 serial = new MicroSerial(device);
142 } catch (FileNotFoundException fe) {
143 JOptionPane.showMessageDialog(this,
145 "Cannot open device",
146 JOptionPane.ERROR_MESSAGE);
150 new MicroDownload(this, device, serial);
153 private void no_data() {
154 JOptionPane.showMessageDialog(this,
157 JOptionPane.INFORMATION_MESSAGE);
160 private void Save() {
165 MicroSave save = new MicroSave (this, data);
166 if (save.runDialog())
170 private void Export() {
175 MicroExport export = new MicroExport (this, data);
179 private static void CommandGraph(File file) {
180 MicroPeak m = new MicroPeak();
184 private static void CommandExport(File file) {
186 MicroData d = ReadFile(file);
188 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
190 System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath());
191 MicroExport.export(csv, d);
192 } catch (FileNotFoundException fe) {
193 System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage());
194 } catch (IOException ie) {
195 System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage());
198 } catch (IOException ie) {
199 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
203 private void Close() {
207 if (number_of_windows == 0)
211 public void actionPerformed(ActionEvent ev) {
212 if (open_command.equals(ev.getActionCommand()))
214 else if (save_command.equals(ev.getActionCommand()))
216 else if (export_command.equals(ev.getActionCommand()))
218 else if (preferences_command.equals(ev.getActionCommand()))
220 else if (close_command.equals(ev.getActionCommand()))
222 else if (exit_command.equals(ev.getActionCommand()))
224 else if (download_command.equals(ev.getActionCommand()))
228 public void itemStateChanged(ItemEvent e) {
231 /* OSXAdapter interfaces */
232 public void macosx_file_handler(String path) {
233 CommandGraph(new File(path));
236 public void macosx_quit_handler() {
240 public void macosx_preferences_handler() {
244 public void filter_changed(double speed_filter, double accel_filter) {
245 data.flight_series.set_filter(speed_filter, accel_filter);
246 graph.filter_changed();
247 data.flight_stats = new AltosFlightStats(data.flight_series);
248 statsTable.filter_changed(data.flight_stats);
251 public double speed_filter() {
252 if (data != null && data.flight_series != null)
253 return data.flight_series.speed_filter_width;
257 public double accel_filter() {
258 if (data != null && data.flight_series != null)
259 return data.flight_series.accel_filter_width;
263 private void add_menu(JMenu menu, String label, String action) {
264 JMenuItem item = new JMenuItem(label);
266 item.addActionListener(this);
267 item.setActionCommand(action);
271 private void make_menu(String label, String[][] items) {
272 JMenu menu = new JMenu(label);
273 for (int i = 0; i < items.length; i++) {
275 if (items[i][1].equals("exit"))
277 if (items[i][1].equals("preferences"))
280 add_menu(menu, items[i][0], items[i][1]);
289 register_for_macosx_events();
291 AltosUIPreferences.set_component(this);
293 container = getContentPane();
294 pane = new JTabbedPane();
296 setTitle("MicroPeak");
298 menu_bar = new JMenuBar();
299 setJMenuBar(menu_bar);
301 make_menu("File", file_menu_entries);
304 make_menu(download_label, download_menu_entries);
306 JButton download_button = new JButton (download_label);
307 download_button.setActionCommand(download_command);
308 download_button.addActionListener(this);
309 menu_bar.add(download_button);
312 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
313 addWindowListener(new WindowAdapter() {
315 public void windowClosing(WindowEvent e) {
316 statsTable.tell_closing();
322 enable = new AltosUIEnable(this);
324 graph = new AltosGraph(enable);
325 statsTable = new AltosFlightStatsTable();
326 raw = new MicroRaw();
327 pane.add(graph.panel, "Graph");
328 pane.add(enable, "Configure Graph");
329 pane.add(statsTable, "Statistics");
330 JScrollPane scroll = new JScrollPane(raw);
331 pane.add(scroll, "Raw Data");
335 container.doLayout();
336 container.validate();
339 Insets i = getInsets();
340 Dimension ps = pane.getPreferredSize();
341 ps.width += i.left + i.right;
342 ps.height += i.top + i.bottom;
348 public static void help(int code) {
349 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
350 System.out.printf(" Options:\n");
351 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
352 System.out.printf(" --graph\tgraph a flight\n");
356 public static void main(final String[] args) {
357 boolean opened = false;
358 boolean graphing = true;
361 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
362 } catch (Exception e) {
365 for (int i = 0; i < args.length; i++) {
366 if (args[i].equals("--help"))
368 else if (args[i].equals("--export"))
370 else if (args[i].equals("--graph"))
372 else if (args[i].startsWith("--"))
375 File file = new File(args[i]);
382 } catch (Exception e) {
383 System.err.printf("Error processing \"%s\": %s %s\n",
384 file.getName(), e.toString(), e.getMessage());