2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.micropeak;
21 import java.awt.event.*;
24 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_4.*;
27 import org.altusmetrum.altosuilib_2.*;
29 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener {
34 MicroStatsTable statsTable;
40 static int number_of_windows;
42 MicroPeak SetData(MicroData data) {
44 if (this.data != null) {
46 return mp.SetData(data);
49 stats = new MicroStats(data);
50 graph.setDataSet(data);
51 statsTable.setStats(stats);
57 void SetName(String name) {
62 private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException {
63 MicroData data = null;
64 FileInputStream fis = new FileInputStream(filename);
66 data = new MicroData((InputStream) fis, filename.getName());
67 AltosUIPreferences.set_last_logdir(filename);
68 } catch (MicroData.NonHexcharException nhe) {
70 } catch (MicroData.FileEndedException nhe) {
72 } catch (InterruptedException ie) {
80 private void OpenFile(File filename) {
82 SetData(ReadFile(filename));
83 } catch (FileNotFoundException fne) {
84 JOptionPane.showMessageDialog(this,
87 JOptionPane.ERROR_MESSAGE);
88 } catch (IOException ioe) {
89 JOptionPane.showMessageDialog(this,
92 JOptionPane.ERROR_MESSAGE);
96 private void SelectFile() {
97 MicroFileChooser chooser = new MicroFileChooser(this);
98 File file = chooser.runDialog();
104 private void Preferences() {
105 new AltosUIConfigure(this);
108 private void DownloadData() {
109 AltosDevice device = MicroDeviceDialog.show(this);
112 new MicroDownload(this, device);
115 private void no_data() {
116 JOptionPane.showMessageDialog(this,
119 JOptionPane.INFORMATION_MESSAGE);
122 private void Save() {
127 MicroSave save = new MicroSave (this, data);
128 if (save.runDialog())
132 private void Export() {
137 MicroExport export = new MicroExport (this, data);
141 private static void CommandGraph(File file) {
142 MicroPeak m = new MicroPeak();
146 private static void CommandExport(File file) {
148 MicroData d = ReadFile(file);
150 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
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());
160 } catch (IOException ie) {
161 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
165 private void Close() {
169 if (number_of_windows == 0)
173 public void actionPerformed(ActionEvent ev) {
174 if ("Exit".equals(ev.getActionCommand()))
176 else if ("Close".equals(ev.getActionCommand()))
178 else if ("Open".equals(ev.getActionCommand()))
180 else if ("Download".equals(ev.getActionCommand()))
182 else if ("Export".equals(ev.getActionCommand()))
184 else if ("Preferences".equals(ev.getActionCommand()))
186 else if ("Save a Copy".equals(ev.getActionCommand()))
190 public void itemStateChanged(ItemEvent e) {
197 AltosUIPreferences.set_component(this);
199 container = getContentPane();
200 pane = new JTabbedPane();
202 setTitle("MicroPeak");
204 JMenuBar menuBar = new JMenuBar();
205 setJMenuBar(menuBar);
207 JMenu fileMenu = new JMenu("File");
208 menuBar.add(fileMenu);
210 JMenuItem openAction = new JMenuItem("Open");
211 fileMenu.add(openAction);
212 openAction.addActionListener(this);
214 JMenuItem downloadAction = new JMenuItem("Download");
215 fileMenu.add(downloadAction);
216 downloadAction.addActionListener(this);
218 JMenuItem saveAction = new JMenuItem("Save a Copy");
219 fileMenu.add(saveAction);
220 saveAction.addActionListener(this);
222 JMenuItem exportAction = new JMenuItem("Export");
223 fileMenu.add(exportAction);
224 exportAction.addActionListener(this);
226 JMenuItem preferencesAction = new JMenuItem("Preferences");
227 fileMenu.add(preferencesAction);
228 preferencesAction.addActionListener(this);
230 JMenuItem closeAction = new JMenuItem("Close");
231 fileMenu.add(closeAction);
232 closeAction.addActionListener(this);
234 JMenuItem exitAction = new JMenuItem("Exit");
235 fileMenu.add(exitAction);
236 exitAction.addActionListener(this);
238 JButton downloadButton = new JButton ("Download");
239 downloadButton.addActionListener(this);
240 menuBar.add(downloadButton);
242 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
243 addWindowListener(new WindowAdapter() {
245 public void windowClosing(WindowEvent e) {
246 statsTable.tell_closing();
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");
263 container.doLayout();
264 container.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);
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");
284 public static void main(final String[] args) {
285 boolean opened = false;
286 boolean graphing = true;
289 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
290 } catch (Exception e) {
293 for (int i = 0; i < args.length; i++) {
294 if (args[i].equals("--help"))
296 else if (args[i].equals("--export"))
298 else if (args[i].equals("--graph"))
300 else if (args[i].startsWith("--"))
303 File file = new File(args[i]);
310 } catch (Exception e) {
311 System.err.printf("Error processing \"%s\": %s\n",
312 file.getName(), e.getMessage());