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