Merge remote-tracking branch 'mjb/master'
[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_1.*;
27 import org.altusmetrum.altosuilib_1.*;
28
29 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener {
30
31         File            filename;
32         MicroGraph      graph;
33         AltosUIEnable   enable;
34         MicroStatsTable statsTable;
35         MicroRaw        raw;
36         MicroData       data;
37         MicroStats      stats;
38         Container       container;
39         JTabbedPane     pane;
40         static int      number_of_windows;
41
42         MicroPeak SetData(MicroData data) {
43                 MicroPeak       mp = this;
44                 if (this.data != null) {
45                         mp = new MicroPeak();
46                         return mp.SetData(data);
47                 }
48                 this.data = data;
49                 stats = new MicroStats(data);
50                 graph.setDataSet(data);
51                 statsTable.setStats(stats);
52                 raw.setData(data);
53                 setTitle(data.name);
54                 return this;
55         }
56
57         void SetName(String name) {
58                 graph.setName(name);
59                 setTitle(name);
60         }
61
62         private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException {
63                 MicroData       data = null;
64                 FileInputStream fis = new FileInputStream(filename);
65                 try {
66                         data = new MicroData((InputStream) fis, filename.getName());
67                 } catch (InterruptedException ie) {
68                         data = null;
69                 } finally {
70                         fis.close();
71                 }
72                 return data;
73         }
74
75         private void OpenFile(File filename) {
76                 try {
77                         SetData(ReadFile(filename));
78                 } catch (FileNotFoundException fne) {
79                         JOptionPane.showMessageDialog(this,
80                                                       fne.getMessage(),
81                                                       "Cannot open file",
82                                                       JOptionPane.ERROR_MESSAGE);
83                 } catch (IOException ioe) {
84                         JOptionPane.showMessageDialog(this,
85                                                       ioe.getMessage(),
86                                                       "File Read Error",
87                                                       JOptionPane.ERROR_MESSAGE);
88                 }
89         }
90
91         private void SelectFile() {
92                 MicroFileChooser        chooser = new MicroFileChooser(this);
93                 File                    file = chooser.runDialog();
94
95                 if (file != null)
96                         OpenFile(file);
97         }
98
99         private void Preferences() {
100                 new AltosUIConfigure(this);
101         }
102
103         private void DownloadData() {
104                 AltosDevice     device = MicroDeviceDialog.show(this);
105                 
106                 if (device != null)
107                         new MicroDownload(this, device);
108         }
109
110         private void no_data() {
111                         JOptionPane.showMessageDialog(this,
112                                                       "No data available",
113                                                       "No data",
114                                                       JOptionPane.INFORMATION_MESSAGE);
115         }
116
117         private void Save() {
118                 if (data == null) {
119                         no_data();
120                         return;
121                 }
122                 MicroSave       save = new MicroSave (this, data);
123                 if (save.runDialog())
124                         SetName(data.name);
125         }
126         
127         private void Export() {
128                 if (data == null) {
129                         no_data();
130                         return;
131                 }
132                 MicroExport     export = new MicroExport (this, data);
133                 export.runDialog();
134         }
135
136         private static void CommandGraph(File file) {
137                 MicroPeak m = new MicroPeak();
138                 m.OpenFile(file);
139         }
140
141         private static void CommandExport(File file) {
142                 try {
143                         MicroData d = ReadFile(file);
144                         if (d != null) {
145                                 File    csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
146                                 try {
147                                         System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath());
148                                         MicroExport.export(csv, d);
149                                 } catch (FileNotFoundException fe) {
150                                         System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage());
151                                 } catch (IOException ie) {
152                                         System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage());
153                                 }
154                         }
155                 } catch (IOException ie) {
156                         System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
157                 }
158         }
159
160         private void Close() {
161                 setVisible(false);
162                 dispose();
163                 --number_of_windows;
164                 if (number_of_windows == 0)
165                         System.exit(0);
166         }
167
168         public void actionPerformed(ActionEvent ev) {
169                 if ("Exit".equals(ev.getActionCommand()))
170                         System.exit(0);
171                 else if ("Close".equals(ev.getActionCommand()))
172                         Close();
173                 else if ("Open".equals(ev.getActionCommand()))
174                         SelectFile();
175                 else if ("Download".equals(ev.getActionCommand()))
176                         DownloadData();
177                 else if ("Export".equals(ev.getActionCommand()))
178                         Export();
179                 else if ("Preferences".equals(ev.getActionCommand()))
180                         Preferences();
181                 else if ("Save a Copy".equals(ev.getActionCommand()))
182                         Save();
183         }
184
185         public void itemStateChanged(ItemEvent e) {
186         }
187
188         public MicroPeak() {
189
190                 ++number_of_windows;
191
192                 AltosUIPreferences.set_component(this);
193
194                 container = getContentPane();
195                 pane = new JTabbedPane();
196
197                 setTitle("MicroPeak");
198
199                 JMenuBar menuBar = new JMenuBar();
200                 setJMenuBar(menuBar);
201
202                 JMenu fileMenu = new JMenu("File");
203                 menuBar.add(fileMenu);
204
205                 JMenuItem openAction = new JMenuItem("Open");
206                 fileMenu.add(openAction);
207                 openAction.addActionListener(this);
208
209                 JMenuItem downloadAction = new JMenuItem("Download");
210                 fileMenu.add(downloadAction);
211                 downloadAction.addActionListener(this);
212
213                 JMenuItem saveAction = new JMenuItem("Save a Copy");
214                 fileMenu.add(saveAction);
215                 saveAction.addActionListener(this);
216
217                 JMenuItem exportAction = new JMenuItem("Export");
218                 fileMenu.add(exportAction);
219                 exportAction.addActionListener(this);
220
221                 JMenuItem preferencesAction = new JMenuItem("Preferences");
222                 fileMenu.add(preferencesAction);
223                 preferencesAction.addActionListener(this);
224
225                 JMenuItem closeAction = new JMenuItem("Close");
226                 fileMenu.add(closeAction);
227                 closeAction.addActionListener(this);
228
229                 JMenuItem exitAction = new JMenuItem("Exit");
230                 fileMenu.add(exitAction);
231                 exitAction.addActionListener(this);
232
233                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
234                 addWindowListener(new WindowAdapter() {
235                         @Override
236                         public void windowClosing(WindowEvent e) {
237                                 statsTable.tell_closing();
238                                 Close();
239                         }
240                 });
241
242                 enable = new AltosUIEnable();
243                 graph = new MicroGraph(enable);
244                 statsTable = new MicroStatsTable();
245                 raw = new MicroRaw();
246                 pane.add(graph.panel, "Graph");
247                 pane.add(enable, "Configure Graph");
248                 pane.add(statsTable, "Statistics");
249                 JScrollPane scroll = new JScrollPane(raw);
250                 pane.add(scroll, "Raw Data");
251                 pane.doLayout();
252                 pane.validate();
253                 container.add(pane);
254                 container.doLayout();
255                 container.validate();
256                 doLayout();
257                 validate();
258                 Insets i = getInsets();
259                 Dimension ps = pane.getPreferredSize();
260                 ps.width += i.left + i.right;
261                 ps.height += i.top + i.bottom;
262 //              setPreferredSize(ps);
263                 setSize(ps);
264                 setVisible(true);
265         }
266
267         public static void help(int code) {
268                 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
269                 System.out.printf("  Options:\n");
270                 System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
271                 System.out.printf("    --graph\tgraph a flight\n");
272                 System.exit(code);
273         }
274
275         public static void main(final String[] args) {
276                 boolean opened = false;
277                 boolean graphing = true;
278
279                 try {
280                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
281                 } catch (Exception e) {
282                 }
283
284                 for (int i = 0; i < args.length; i++) {
285                         if (args[i].equals("--help"))
286                                 help(0);
287                         else if (args[i].equals("--export"))
288                                 graphing = false;
289                         else if (args[i].equals("--graph"))
290                                 graphing = true;
291                         else if (args[i].startsWith("--"))
292                                 help(1);
293                         else {
294                                 File    file = new File(args[i]);
295                                 try {
296                                         if (graphing)
297                                                 CommandGraph(file);
298                                         else
299                                                 CommandExport(file);
300                                         opened = true;
301                                 } catch (Exception e) {
302                                         System.err.printf("Error processing \"%s\": %s\n",
303                                                           file.getName(), e.getMessage());
304                                 }
305                         }
306                 }
307                 if (!opened)
308                         new MicroPeak();
309         }
310 }