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