micropeak: Add flight stats pane
[fw/altos] / micropeak / MicroPeak.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.micropeak;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.util.concurrent.*;
25 import java.util.*;
26 import org.altusmetrum.AltosLib.*;
27 import org.altusmetrum.altosuilib.*;
28
29 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener {
30
31         File            filename;
32         MicroGraph      graph;
33         MicroStatsTable stats;
34         MicroData       data;
35         Container       container;
36         JTabbedPane     pane;
37
38         private void RunFile(InputStream input) {
39                 try {
40                         data = new MicroData(input);
41                         graph.setData(data);
42                         stats.setData(data);
43                 } catch (IOException ioe) {
44                 }
45                 try {
46                         input.close();
47                 } catch (IOException ioe) {
48                 }
49         }
50
51         private void OpenFile(File filename) {
52                 try {
53                         RunFile (new FileInputStream(filename));
54                 } catch (FileNotFoundException fne) {
55                 }
56         }
57
58         private void SelectFile() {
59                 MicroFileChooser        chooser = new MicroFileChooser(this);
60                 InputStream             input = chooser.runDialog();
61
62                 if (input != null)
63                         RunFile(input);
64         }
65
66         private void Preferences() {
67                 new AltosConfigureUI(this);
68         }
69                 
70         private void DownloadData() {
71                 java.util.List<MicroUSB>        devices = MicroUSB.list();
72                 for (MicroUSB device : devices)
73                         System.out.printf("device %s\n", device.toString());
74         }
75
76         public void actionPerformed(ActionEvent ev) {
77                 if ("Exit".equals(ev.getActionCommand()))
78                         System.exit(0);
79                 else if ("Open".equals(ev.getActionCommand()))
80                         SelectFile();
81                 else if ("New".equals(ev.getActionCommand()))
82                         new MicroPeak();
83                 else if ("Download".equals(ev.getActionCommand()))
84                         DownloadData();
85                 else if ("Preferences".equals(ev.getActionCommand()))
86                         Preferences();
87         }
88
89         public void itemStateChanged(ItemEvent e) {
90         }
91
92         public MicroPeak() {
93
94                 AltosUIPreferences.set_component(this);
95
96                 container = getContentPane();
97                 pane = new JTabbedPane();
98
99                 setTitle("MicroPeak");
100
101                 JMenuBar menuBar = new JMenuBar();
102                 setJMenuBar(menuBar);
103
104                 JMenu fileMenu = new JMenu("File");
105                 menuBar.add(fileMenu);
106
107                 JMenuItem newAction = new JMenuItem("New");
108                 fileMenu.add(newAction);
109                 newAction.addActionListener(this);
110
111                 JMenuItem openAction = new JMenuItem("Open");
112                 fileMenu.add(openAction);
113                 openAction.addActionListener(this);
114
115                 JMenuItem downloadAction = new JMenuItem("Download");
116                 fileMenu.add(downloadAction);
117                 downloadAction.addActionListener(this);
118
119                 JMenuItem preferencesAction = new JMenuItem("Preferences");
120                 fileMenu.add(preferencesAction);
121                 preferencesAction.addActionListener(this);
122
123                 JMenuItem exitAction = new JMenuItem("Exit");
124                 fileMenu.add(exitAction);
125                 exitAction.addActionListener(this);
126
127                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
128                 addWindowListener(new WindowAdapter() {
129                         @Override
130                         public void windowClosing(WindowEvent e) {
131                                 System.exit(0);
132                         }
133                 });
134
135                 graph = new MicroGraph();
136                 stats = new MicroStatsTable();
137                 pane.add(graph.panel, "Graph");
138                 pane.add(stats, "Statistics");
139                 pane.doLayout();
140                 pane.validate();
141                 container.add(pane);
142                 container.doLayout();
143                 container.validate();
144                 doLayout();
145                 validate();
146                 Insets i = getInsets();
147                 Dimension ps = pane.getPreferredSize();
148                 ps.width += i.left + i.right;
149                 ps.height += i.top + i.bottom;
150                 setPreferredSize(ps);
151                 setSize(ps);
152                 setVisible(true);
153         }
154
155         public static void main(final String[] args) {
156                 boolean opened = false;
157
158                 try {
159                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
160                 } catch (Exception e) {
161                 }
162
163                 for (int i = 0; i < args.length; i++) {
164                         MicroPeak m = new MicroPeak();
165                         m.OpenFile(new File(args[i]));
166                         opened = true;
167                 }
168                 if (!opened)
169                         new MicroPeak();
170         }
171 }