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