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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.micropeak;
21 import java.awt.event.*;
24 import java.util.concurrent.*;
26 import org.altusmetrum.AltosLib.*;
27 import org.altusmetrum.altosuilib.*;
29 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener {
33 MicroStatsTable stats;
38 static int number_of_windows;
40 MicroPeak SetData(MicroData data) {
42 if (this.data != null) {
44 return mp.SetData(data);
54 void SetName(String name) {
59 private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException {
60 MicroData data = null;
61 FileInputStream fis = new FileInputStream(filename);
63 data = new MicroData((InputStream) fis, filename.getName());
64 } catch (InterruptedException ie) {
72 private void OpenFile(File filename) {
74 SetData(ReadFile(filename));
75 } catch (FileNotFoundException fne) {
76 JOptionPane.showMessageDialog(this,
79 JOptionPane.ERROR_MESSAGE);
80 } catch (IOException ioe) {
81 JOptionPane.showMessageDialog(this,
84 JOptionPane.ERROR_MESSAGE);
88 private void SelectFile() {
89 MicroFileChooser chooser = new MicroFileChooser(this);
90 File file = chooser.runDialog();
96 private void Preferences() {
97 new AltosUIConfigure(this);
100 private void DownloadData() {
101 AltosDevice device = MicroDeviceDialog.show(this);
104 new MicroDownload(this, device);
107 private void no_data() {
108 JOptionPane.showMessageDialog(this,
111 JOptionPane.INFORMATION_MESSAGE);
114 private void Save() {
119 MicroSave save = new MicroSave (this, data);
120 if (save.runDialog())
124 private void Export() {
129 MicroExport export = new MicroExport (this, data);
133 private static void CommandGraph(File file) {
134 MicroPeak m = new MicroPeak();
138 private static void CommandExport(File file) {
140 MicroData d = ReadFile(file);
142 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
144 System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath());
145 MicroExport.export(csv, d);
146 } catch (FileNotFoundException fe) {
147 System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage());
148 } catch (IOException ie) {
149 System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage());
152 } catch (IOException ie) {
153 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
157 private void Close() {
161 if (number_of_windows == 0)
165 public void actionPerformed(ActionEvent ev) {
166 if ("Exit".equals(ev.getActionCommand()))
168 else if ("Close".equals(ev.getActionCommand()))
170 else if ("Open".equals(ev.getActionCommand()))
172 else if ("Download".equals(ev.getActionCommand()))
174 else if ("Export".equals(ev.getActionCommand()))
176 else if ("Preferences".equals(ev.getActionCommand()))
178 else if ("Save a Copy".equals(ev.getActionCommand()))
182 public void itemStateChanged(ItemEvent e) {
189 AltosUIPreferences.set_component(this);
191 container = getContentPane();
192 pane = new JTabbedPane();
194 setTitle("MicroPeak");
196 JMenuBar menuBar = new JMenuBar();
197 setJMenuBar(menuBar);
199 JMenu fileMenu = new JMenu("File");
200 menuBar.add(fileMenu);
202 JMenuItem openAction = new JMenuItem("Open");
203 fileMenu.add(openAction);
204 openAction.addActionListener(this);
206 JMenuItem downloadAction = new JMenuItem("Download");
207 fileMenu.add(downloadAction);
208 downloadAction.addActionListener(this);
210 JMenuItem saveAction = new JMenuItem("Save a Copy");
211 fileMenu.add(saveAction);
212 saveAction.addActionListener(this);
214 JMenuItem exportAction = new JMenuItem("Export");
215 fileMenu.add(exportAction);
216 exportAction.addActionListener(this);
218 JMenuItem preferencesAction = new JMenuItem("Preferences");
219 fileMenu.add(preferencesAction);
220 preferencesAction.addActionListener(this);
222 JMenuItem closeAction = new JMenuItem("Close");
223 fileMenu.add(closeAction);
224 closeAction.addActionListener(this);
226 JMenuItem exitAction = new JMenuItem("Exit");
227 fileMenu.add(exitAction);
228 exitAction.addActionListener(this);
230 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
231 addWindowListener(new WindowAdapter() {
233 public void windowClosing(WindowEvent e) {
238 graph = new MicroGraph();
239 stats = new MicroStatsTable();
240 raw = new MicroRaw();
241 pane.add(graph.panel, "Graph");
242 pane.add(stats, "Statistics");
243 JScrollPane scroll = new JScrollPane(raw);
244 pane.add(scroll, "Raw Data");
248 container.doLayout();
249 container.validate();
252 Insets i = getInsets();
253 Dimension ps = pane.getPreferredSize();
254 ps.width += i.left + i.right;
255 ps.height += i.top + i.bottom;
256 // setPreferredSize(ps);
258 setLocationByPlatform(true);
262 public static void help(int code) {
263 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
264 System.out.printf(" Options:\n");
265 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
266 System.out.printf(" --graph\tgraph a flight\n");
270 public static void main(final String[] args) {
271 boolean opened = false;
272 boolean graphing = true;
275 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
276 } catch (Exception e) {
279 for (int i = 0; i < args.length; i++) {
280 if (args[i].equals("--help"))
282 else if (args[i].equals("--export"))
284 else if (args[i].equals("--graph"))
286 else if (args[i].startsWith("--"))
289 File file = new File(args[i]);
296 } catch (Exception e) {
297 System.err.printf("Error processing \"%s\": %s\n",
298 file.getName(), e.getMessage());