altoslib: Remove some debug printfs
[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         /* OSXAdapter interfaces */
194         public void macosx_file_handler(String path) {
195                 CommandGraph(new File(path));
196         }
197
198         public void macosx_quit_handler() {
199                 System.exit(0);
200         }
201
202         public void macosx_preferences_handler() {
203                 Preferences();
204         }
205
206         public MicroPeak() {
207
208                 ++number_of_windows;
209
210                 register_for_macosx_events();
211
212                 AltosUIPreferences.set_component(this);
213
214                 container = getContentPane();
215                 pane = new JTabbedPane();
216
217                 setTitle("MicroPeak");
218
219                 JMenuBar menuBar = new JMenuBar();
220                 setJMenuBar(menuBar);
221
222                 JMenu fileMenu = new JMenu("File");
223                 menuBar.add(fileMenu);
224
225                 JMenuItem openAction = new JMenuItem("Open");
226                 fileMenu.add(openAction);
227                 openAction.addActionListener(this);
228
229                 JMenuItem downloadAction = new JMenuItem("Download");
230                 fileMenu.add(downloadAction);
231                 downloadAction.addActionListener(this);
232
233                 JMenuItem saveAction = new JMenuItem("Save a Copy");
234                 fileMenu.add(saveAction);
235                 saveAction.addActionListener(this);
236
237                 JMenuItem exportAction = new JMenuItem("Export");
238                 fileMenu.add(exportAction);
239                 exportAction.addActionListener(this);
240
241                 JMenuItem preferencesAction = new JMenuItem("Preferences");
242                 fileMenu.add(preferencesAction);
243                 preferencesAction.addActionListener(this);
244
245                 JMenuItem closeAction = new JMenuItem("Close");
246                 fileMenu.add(closeAction);
247                 closeAction.addActionListener(this);
248
249                 JMenuItem exitAction = new JMenuItem("Exit");
250                 fileMenu.add(exitAction);
251                 exitAction.addActionListener(this);
252
253                 JButton downloadButton = new JButton ("Download");
254                 downloadButton.addActionListener(this);
255                 menuBar.add(downloadButton);
256
257                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
258                 addWindowListener(new WindowAdapter() {
259                         @Override
260                         public void windowClosing(WindowEvent e) {
261                                 statsTable.tell_closing();
262                                 Close();
263                         }
264                 });
265
266                 enable = new AltosUIEnable();
267                 graph = new MicroGraph(enable);
268                 statsTable = new MicroStatsTable();
269                 raw = new MicroRaw();
270                 pane.add(graph.panel, "Graph");
271                 pane.add(enable, "Configure Graph");
272                 pane.add(statsTable, "Statistics");
273                 JScrollPane scroll = new JScrollPane(raw);
274                 pane.add(scroll, "Raw Data");
275                 pane.doLayout();
276                 pane.validate();
277                 container.add(pane);
278                 container.doLayout();
279                 container.validate();
280                 doLayout();
281                 validate();
282                 Insets i = getInsets();
283                 Dimension ps = pane.getPreferredSize();
284                 ps.width += i.left + i.right;
285                 ps.height += i.top + i.bottom;
286 //              setPreferredSize(ps);
287                 setSize(ps);
288                 setVisible(true);
289         }
290
291         public static void help(int code) {
292                 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
293                 System.out.printf("  Options:\n");
294                 System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
295                 System.out.printf("    --graph\tgraph a flight\n");
296                 System.exit(code);
297         }
298
299         public static void main(final String[] args) {
300                 boolean opened = false;
301                 boolean graphing = true;
302
303                 try {
304                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
305                 } catch (Exception e) {
306                 }
307
308                 for (int i = 0; i < args.length; i++) {
309                         if (args[i].equals("--help"))
310                                 help(0);
311                         else if (args[i].equals("--export"))
312                                 graphing = false;
313                         else if (args[i].equals("--graph"))
314                                 graphing = true;
315                         else if (args[i].startsWith("--"))
316                                 help(1);
317                         else {
318                                 File    file = new File(args[i]);
319                                 try {
320                                         if (graphing)
321                                                 CommandGraph(file);
322                                         else
323                                                 CommandExport(file);
324                                         opened = true;
325                                 } catch (Exception e) {
326                                         System.err.printf("Error processing \"%s\": %s\n",
327                                                           file.getName(), e.getMessage());
328                                 }
329                         }
330                 }
331                 if (!opened)
332                         new MicroPeak();
333         }
334 }