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_12.*;
28 import org.altusmetrum.altosuilib_12.*;
30 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener {
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() {
213 register_for_macosx_events();
215 AltosUIPreferences.set_component(this);
217 container = getContentPane();
218 pane = new JTabbedPane();
220 setTitle("MicroPeak");
222 JMenuBar menuBar = new JMenuBar();
223 setJMenuBar(menuBar);
225 JMenu fileMenu = new JMenu("File");
226 menuBar.add(fileMenu);
228 JMenuItem openAction = new JMenuItem("Open");
229 fileMenu.add(openAction);
230 openAction.addActionListener(this);
232 JMenuItem downloadAction = new JMenuItem("Download");
233 fileMenu.add(downloadAction);
234 downloadAction.addActionListener(this);
236 JMenuItem saveAction = new JMenuItem("Save a Copy");
237 fileMenu.add(saveAction);
238 saveAction.addActionListener(this);
240 JMenuItem exportAction = new JMenuItem("Export");
241 fileMenu.add(exportAction);
242 exportAction.addActionListener(this);
244 JMenuItem preferencesAction = new JMenuItem("Preferences");
245 fileMenu.add(preferencesAction);
246 preferencesAction.addActionListener(this);
248 JMenuItem closeAction = new JMenuItem("Close");
249 fileMenu.add(closeAction);
250 closeAction.addActionListener(this);
252 JMenuItem exitAction = new JMenuItem("Exit");
253 fileMenu.add(exitAction);
254 exitAction.addActionListener(this);
256 JButton downloadButton = new JButton ("Download");
257 downloadButton.addActionListener(this);
258 menuBar.add(downloadButton);
260 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
261 addWindowListener(new WindowAdapter() {
263 public void windowClosing(WindowEvent e) {
264 statsTable.tell_closing();
269 enable = new AltosUIEnable();
271 graph = new AltosGraph(enable);
272 statsTable = new AltosFlightStatsTable();
273 raw = new MicroRaw();
274 pane.add(graph.panel, "Graph");
275 pane.add(enable, "Configure Graph");
276 pane.add(statsTable, "Statistics");
277 JScrollPane scroll = new JScrollPane(raw);
278 pane.add(scroll, "Raw Data");
282 container.doLayout();
283 container.validate();
286 Insets i = getInsets();
287 Dimension ps = pane.getPreferredSize();
288 ps.width += i.left + i.right;
289 ps.height += i.top + i.bottom;
290 // setPreferredSize(ps);
295 public static void help(int code) {
296 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
297 System.out.printf(" Options:\n");
298 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
299 System.out.printf(" --graph\tgraph a flight\n");
303 public static void main(final String[] args) {
304 boolean opened = false;
305 boolean graphing = true;
308 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
309 } catch (Exception e) {
312 for (int i = 0; i < args.length; i++) {
313 if (args[i].equals("--help"))
315 else if (args[i].equals("--export"))
317 else if (args[i].equals("--graph"))
319 else if (args[i].startsWith("--"))
322 File file = new File(args[i]);
329 } catch (Exception e) {
330 System.err.printf("Error processing \"%s\": %s %s\n",
331 file.getName(), e.toString(), e.getMessage());