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);
141 new MicroDownload(this, device);
144 private void no_data() {
145 JOptionPane.showMessageDialog(this,
148 JOptionPane.INFORMATION_MESSAGE);
151 private void Save() {
156 MicroSave save = new MicroSave (this, data);
157 if (save.runDialog())
161 private void Export() {
166 MicroExport export = new MicroExport (this, data);
170 private static void CommandGraph(File file) {
171 MicroPeak m = new MicroPeak();
175 private static void CommandExport(File file) {
177 MicroData d = ReadFile(file);
179 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
181 System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath());
182 MicroExport.export(csv, d);
183 } catch (FileNotFoundException fe) {
184 System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage());
185 } catch (IOException ie) {
186 System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage());
189 } catch (IOException ie) {
190 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
194 private void Close() {
198 if (number_of_windows == 0)
202 public void actionPerformed(ActionEvent ev) {
203 if (open_command.equals(ev.getActionCommand()))
205 else if (save_command.equals(ev.getActionCommand()))
207 else if (export_command.equals(ev.getActionCommand()))
209 else if (preferences_command.equals(ev.getActionCommand()))
211 else if (close_command.equals(ev.getActionCommand()))
213 else if (exit_command.equals(ev.getActionCommand()))
215 else if (download_command.equals(ev.getActionCommand()))
219 public void itemStateChanged(ItemEvent e) {
222 /* OSXAdapter interfaces */
223 public void macosx_file_handler(String path) {
224 CommandGraph(new File(path));
227 public void macosx_quit_handler() {
231 public void macosx_preferences_handler() {
235 public void filter_changed(double speed_filter, double accel_filter) {
236 data.flight_series.set_filter(speed_filter, accel_filter);
237 graph.filter_changed();
238 data.flight_stats = new AltosFlightStats(data.flight_series);
239 statsTable.filter_changed(data.flight_stats);
242 public double speed_filter() {
243 if (data != null && data.flight_series != null)
244 return data.flight_series.speed_filter_width;
248 public double accel_filter() {
249 if (data != null && data.flight_series != null)
250 return data.flight_series.accel_filter_width;
254 private void add_menu(JMenu menu, String label, String action) {
255 JMenuItem item = new JMenuItem(label);
257 item.addActionListener(this);
258 item.setActionCommand(action);
262 private void make_menu(String label, String[][] items) {
263 JMenu menu = new JMenu(label);
264 for (int i = 0; i < items.length; i++) {
266 if (items[i][1].equals("exit"))
268 if (items[i][1].equals("preferences"))
271 add_menu(menu, items[i][0], items[i][1]);
280 register_for_macosx_events();
282 AltosUIPreferences.set_component(this);
284 container = getContentPane();
285 pane = new JTabbedPane();
287 setTitle("MicroPeak");
289 menu_bar = new JMenuBar();
290 setJMenuBar(menu_bar);
292 make_menu("File", file_menu_entries);
295 make_menu(download_label, download_menu_entries);
297 JButton download_button = new JButton (download_label);
298 download_button.setActionCommand(download_command);
299 download_button.addActionListener(this);
300 menu_bar.add(download_button);
303 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
304 addWindowListener(new WindowAdapter() {
306 public void windowClosing(WindowEvent e) {
307 statsTable.tell_closing();
313 enable = new AltosUIEnable(this);
315 graph = new AltosGraph(enable);
316 statsTable = new AltosFlightStatsTable();
317 raw = new MicroRaw();
318 pane.add(graph.panel, "Graph");
319 pane.add(enable, "Configure Graph");
320 pane.add(statsTable, "Statistics");
321 JScrollPane scroll = new JScrollPane(raw);
322 pane.add(scroll, "Raw Data");
326 container.doLayout();
327 container.validate();
330 Insets i = getInsets();
331 Dimension ps = pane.getPreferredSize();
332 ps.width += i.left + i.right;
333 ps.height += i.top + i.bottom;
339 public static void help(int code) {
340 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
341 System.out.printf(" Options:\n");
342 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
343 System.out.printf(" --graph\tgraph a flight\n");
347 public static void main(final String[] args) {
348 boolean opened = false;
349 boolean graphing = true;
352 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
353 } catch (Exception e) {
356 for (int i = 0; i < args.length; i++) {
357 if (args[i].equals("--help"))
359 else if (args[i].equals("--export"))
361 else if (args[i].equals("--graph"))
363 else if (args[i].startsWith("--"))
366 File file = new File(args[i]);
373 } catch (Exception e) {
374 System.err.printf("Error processing \"%s\": %s %s\n",
375 file.getName(), e.toString(), e.getMessage());