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_13.*;
28 import org.altusmetrum.altosuilib_13.*;
30 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener, AltosFilterListener {
35 AltosFlightStatsTable statsTable;
40 static int number_of_windows;
42 MicroPeak SetData(MicroData data) {
44 if (this.data != null) {
46 return mp.SetData(data);
49 if (data.flight_series == null)
50 System.out.printf("no data in flight\n");
51 if (data.flight_stats == null)
52 System.out.printf("no stats in flight\n");
53 graph.set_data(data.flight_stats, data.flight_series);
54 statsTable.set_stats(data.flight_stats);
60 void SetName(String name) {
65 private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException {
66 MicroData data = null;
67 FileInputStream fis = new FileInputStream(filename);
69 data = new MicroData((InputStream) fis, filename.getName());
70 AltosUIPreferences.set_last_logdir(filename);
71 } catch (MicroData.NonHexcharException nhe) {
73 } catch (MicroData.FileEndedException nhe) {
75 } catch (InterruptedException ie) {
83 private void OpenFile(File filename) {
85 SetData(ReadFile(filename));
86 } catch (FileNotFoundException fne) {
87 JOptionPane.showMessageDialog(this,
90 JOptionPane.ERROR_MESSAGE);
91 } catch (IOException ioe) {
92 JOptionPane.showMessageDialog(this,
95 JOptionPane.ERROR_MESSAGE);
99 private void SelectFile() {
100 MicroFileChooser chooser = new MicroFileChooser(this);
101 File file = chooser.runDialog();
107 private void Preferences() {
108 new AltosUIConfigure(this);
111 private void DownloadData() {
112 AltosDevice device = MicroDeviceDialog.show(this);
115 new MicroDownload(this, device);
118 private void no_data() {
119 JOptionPane.showMessageDialog(this,
122 JOptionPane.INFORMATION_MESSAGE);
125 private void Save() {
130 MicroSave save = new MicroSave (this, data);
131 if (save.runDialog())
135 private void Export() {
140 MicroExport export = new MicroExport (this, data);
144 private static void CommandGraph(File file) {
145 MicroPeak m = new MicroPeak();
149 private static void CommandExport(File file) {
151 MicroData d = ReadFile(file);
153 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
155 System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath());
156 MicroExport.export(csv, d);
157 } catch (FileNotFoundException fe) {
158 System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage());
159 } catch (IOException ie) {
160 System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage());
163 } catch (IOException ie) {
164 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
168 private void Close() {
172 if (number_of_windows == 0)
176 public void actionPerformed(ActionEvent ev) {
177 if ("Exit".equals(ev.getActionCommand()))
179 else if ("Close".equals(ev.getActionCommand()))
181 else if ("Open".equals(ev.getActionCommand()))
183 else if ("Download".equals(ev.getActionCommand()))
185 else if ("Export".equals(ev.getActionCommand()))
187 else if ("Preferences".equals(ev.getActionCommand()))
189 else if ("Save a Copy".equals(ev.getActionCommand()))
193 public void itemStateChanged(ItemEvent e) {
196 /* OSXAdapter interfaces */
197 public void macosx_file_handler(String path) {
198 CommandGraph(new File(path));
201 public void macosx_quit_handler() {
205 public void macosx_preferences_handler() {
209 public void filter_changed(double speed_filter, double accel_filter) {
210 data.flight_series.set_filter(speed_filter, accel_filter);
211 graph.filter_changed();
212 data.flight_stats = new AltosFlightStats(data.flight_series);
213 statsTable.filter_changed(data.flight_stats);
216 public double speed_filter() {
217 if (data != null && data.flight_series != null)
218 return data.flight_series.speed_filter_width;
222 public double accel_filter() {
223 if (data != null && data.flight_series != null)
224 return data.flight_series.accel_filter_width;
232 register_for_macosx_events();
234 AltosUIPreferences.set_component(this);
236 container = getContentPane();
237 pane = new JTabbedPane();
239 setTitle("MicroPeak");
241 JMenuBar menuBar = new JMenuBar();
242 setJMenuBar(menuBar);
244 JMenu fileMenu = new JMenu("File");
245 menuBar.add(fileMenu);
247 JMenuItem openAction = new JMenuItem("Open");
248 fileMenu.add(openAction);
249 openAction.addActionListener(this);
251 JMenuItem downloadAction = new JMenuItem("Download");
252 fileMenu.add(downloadAction);
253 downloadAction.addActionListener(this);
255 JMenuItem saveAction = new JMenuItem("Save a Copy");
256 fileMenu.add(saveAction);
257 saveAction.addActionListener(this);
259 JMenuItem exportAction = new JMenuItem("Export");
260 fileMenu.add(exportAction);
261 exportAction.addActionListener(this);
263 JMenuItem preferencesAction = new JMenuItem("Preferences");
264 fileMenu.add(preferencesAction);
265 preferencesAction.addActionListener(this);
267 JMenuItem closeAction = new JMenuItem("Close");
268 fileMenu.add(closeAction);
269 closeAction.addActionListener(this);
271 JMenuItem exitAction = new JMenuItem("Exit");
272 fileMenu.add(exitAction);
273 exitAction.addActionListener(this);
275 JButton downloadButton = new JButton ("Download");
276 downloadButton.addActionListener(this);
277 menuBar.add(downloadButton);
279 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
280 addWindowListener(new WindowAdapter() {
282 public void windowClosing(WindowEvent e) {
283 statsTable.tell_closing();
289 enable = new AltosUIEnable(this);
291 graph = new AltosGraph(enable);
292 statsTable = new AltosFlightStatsTable();
293 raw = new MicroRaw();
294 pane.add(graph.panel, "Graph");
295 pane.add(enable, "Configure Graph");
296 pane.add(statsTable, "Statistics");
297 JScrollPane scroll = new JScrollPane(raw);
298 pane.add(scroll, "Raw Data");
302 container.doLayout();
303 container.validate();
306 Insets i = getInsets();
307 Dimension ps = pane.getPreferredSize();
308 ps.width += i.left + i.right;
309 ps.height += i.top + i.bottom;
310 // setPreferredSize(ps);
315 public static void help(int code) {
316 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
317 System.out.printf(" Options:\n");
318 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
319 System.out.printf(" --graph\tgraph a flight\n");
323 public static void main(final String[] args) {
324 boolean opened = false;
325 boolean graphing = true;
328 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
329 } catch (Exception e) {
332 for (int i = 0; i < args.length; i++) {
333 if (args[i].equals("--help"))
335 else if (args[i].equals("--export"))
337 else if (args[i].equals("--graph"))
339 else if (args[i].startsWith("--"))
342 File file = new File(args[i]);
349 } catch (Exception e) {
350 System.err.printf("Error processing \"%s\": %s %s\n",
351 file.getName(), e.toString(), e.getMessage());