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_11.*;
28 import org.altusmetrum.altosuilib_11.*;
30 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener {
35 MicroStatsTable statsTable;
41 static int number_of_windows;
43 MicroPeak SetData(MicroData data) {
45 if (this.data != null) {
47 return mp.SetData(data);
50 stats = new MicroStats(data);
51 graph.setDataSet(data);
52 statsTable.setStats(stats);
58 void SetName(String name) {
63 private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException {
64 MicroData data = null;
65 FileInputStream fis = new FileInputStream(filename);
67 data = new MicroData((InputStream) fis, filename.getName());
68 AltosUIPreferences.set_last_logdir(filename);
69 } catch (MicroData.NonHexcharException nhe) {
71 } catch (MicroData.FileEndedException nhe) {
73 } catch (InterruptedException ie) {
81 private void OpenFile(File filename) {
83 SetData(ReadFile(filename));
84 } catch (FileNotFoundException fne) {
85 JOptionPane.showMessageDialog(this,
88 JOptionPane.ERROR_MESSAGE);
89 } catch (IOException ioe) {
90 JOptionPane.showMessageDialog(this,
93 JOptionPane.ERROR_MESSAGE);
97 private void SelectFile() {
98 MicroFileChooser chooser = new MicroFileChooser(this);
99 File file = chooser.runDialog();
105 private void Preferences() {
106 new AltosUIConfigure(this);
109 private void DownloadData() {
110 AltosDevice device = MicroDeviceDialog.show(this);
113 new MicroDownload(this, device);
116 private void no_data() {
117 JOptionPane.showMessageDialog(this,
120 JOptionPane.INFORMATION_MESSAGE);
123 private void Save() {
128 MicroSave save = new MicroSave (this, data);
129 if (save.runDialog())
133 private void Export() {
138 MicroExport export = new MicroExport (this, data);
142 private static void CommandGraph(File file) {
143 MicroPeak m = new MicroPeak();
147 private static void CommandExport(File file) {
149 MicroData d = ReadFile(file);
151 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
153 System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath());
154 MicroExport.export(csv, d);
155 } catch (FileNotFoundException fe) {
156 System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage());
157 } catch (IOException ie) {
158 System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage());
161 } catch (IOException ie) {
162 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
166 private void Close() {
170 if (number_of_windows == 0)
174 public void actionPerformed(ActionEvent ev) {
175 if ("Exit".equals(ev.getActionCommand()))
177 else if ("Close".equals(ev.getActionCommand()))
179 else if ("Open".equals(ev.getActionCommand()))
181 else if ("Download".equals(ev.getActionCommand()))
183 else if ("Export".equals(ev.getActionCommand()))
185 else if ("Preferences".equals(ev.getActionCommand()))
187 else if ("Save a Copy".equals(ev.getActionCommand()))
191 public void itemStateChanged(ItemEvent e) {
194 /* OSXAdapter interfaces */
195 public void macosx_file_handler(String path) {
196 CommandGraph(new File(path));
199 public void macosx_quit_handler() {
203 public void macosx_preferences_handler() {
211 register_for_macosx_events();
213 AltosUIPreferences.set_component(this);
215 container = getContentPane();
216 pane = new JTabbedPane();
218 setTitle("MicroPeak");
220 JMenuBar menuBar = new JMenuBar();
221 setJMenuBar(menuBar);
223 JMenu fileMenu = new JMenu("File");
224 menuBar.add(fileMenu);
226 JMenuItem openAction = new JMenuItem("Open");
227 fileMenu.add(openAction);
228 openAction.addActionListener(this);
230 JMenuItem downloadAction = new JMenuItem("Download");
231 fileMenu.add(downloadAction);
232 downloadAction.addActionListener(this);
234 JMenuItem saveAction = new JMenuItem("Save a Copy");
235 fileMenu.add(saveAction);
236 saveAction.addActionListener(this);
238 JMenuItem exportAction = new JMenuItem("Export");
239 fileMenu.add(exportAction);
240 exportAction.addActionListener(this);
242 JMenuItem preferencesAction = new JMenuItem("Preferences");
243 fileMenu.add(preferencesAction);
244 preferencesAction.addActionListener(this);
246 JMenuItem closeAction = new JMenuItem("Close");
247 fileMenu.add(closeAction);
248 closeAction.addActionListener(this);
250 JMenuItem exitAction = new JMenuItem("Exit");
251 fileMenu.add(exitAction);
252 exitAction.addActionListener(this);
254 JButton downloadButton = new JButton ("Download");
255 downloadButton.addActionListener(this);
256 menuBar.add(downloadButton);
258 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
259 addWindowListener(new WindowAdapter() {
261 public void windowClosing(WindowEvent e) {
262 statsTable.tell_closing();
267 enable = new AltosUIEnable();
268 graph = new MicroGraph(enable);
269 statsTable = new MicroStatsTable();
270 raw = new MicroRaw();
271 pane.add(graph.panel, "Graph");
272 pane.add(enable, "Configure Graph");
273 pane.add(statsTable, "Statistics");
274 JScrollPane scroll = new JScrollPane(raw);
275 pane.add(scroll, "Raw Data");
279 container.doLayout();
280 container.validate();
283 Insets i = getInsets();
284 Dimension ps = pane.getPreferredSize();
285 ps.width += i.left + i.right;
286 ps.height += i.top + i.bottom;
287 // setPreferredSize(ps);
292 public static void help(int code) {
293 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
294 System.out.printf(" Options:\n");
295 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
296 System.out.printf(" --graph\tgraph a flight\n");
300 public static void main(final String[] args) {
301 boolean opened = false;
302 boolean graphing = true;
305 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
306 } catch (Exception e) {
309 for (int i = 0; i < args.length; i++) {
310 if (args[i].equals("--help"))
312 else if (args[i].equals("--export"))
314 else if (args[i].equals("--graph"))
316 else if (args[i].startsWith("--"))
319 File file = new File(args[i]);
326 } catch (Exception e) {
327 System.err.printf("Error processing \"%s\": %s\n",
328 file.getName(), e.getMessage());