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