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_1.*;
27 import org.altusmetrum.altosuilib_1.*;
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 (InterruptedException ie) {
76 private void OpenFile(File filename) {
78 SetData(ReadFile(filename));
79 } catch (FileNotFoundException fne) {
80 JOptionPane.showMessageDialog(this,
83 JOptionPane.ERROR_MESSAGE);
84 } catch (IOException ioe) {
85 JOptionPane.showMessageDialog(this,
88 JOptionPane.ERROR_MESSAGE);
92 private void SelectFile() {
93 MicroFileChooser chooser = new MicroFileChooser(this);
94 File file = chooser.runDialog();
100 private void Preferences() {
101 new AltosUIConfigure(this);
104 private void DownloadData() {
105 AltosDevice device = MicroDeviceDialog.show(this);
108 new MicroDownload(this, device);
111 private void no_data() {
112 JOptionPane.showMessageDialog(this,
115 JOptionPane.INFORMATION_MESSAGE);
118 private void Save() {
123 MicroSave save = new MicroSave (this, data);
124 if (save.runDialog())
128 private void Export() {
133 MicroExport export = new MicroExport (this, data);
137 private static void CommandGraph(File file) {
138 MicroPeak m = new MicroPeak();
142 private static void CommandExport(File file) {
144 MicroData d = ReadFile(file);
146 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
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());
156 } catch (IOException ie) {
157 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
161 private void Close() {
165 if (number_of_windows == 0)
169 public void actionPerformed(ActionEvent ev) {
170 if ("Exit".equals(ev.getActionCommand()))
172 else if ("Close".equals(ev.getActionCommand()))
174 else if ("Open".equals(ev.getActionCommand()))
176 else if ("Download".equals(ev.getActionCommand()))
178 else if ("Export".equals(ev.getActionCommand()))
180 else if ("Preferences".equals(ev.getActionCommand()))
182 else if ("Save a Copy".equals(ev.getActionCommand()))
186 public void itemStateChanged(ItemEvent e) {
193 AltosUIPreferences.set_component(this);
195 container = getContentPane();
196 pane = new JTabbedPane();
198 setTitle("MicroPeak");
200 JMenuBar menuBar = new JMenuBar();
201 setJMenuBar(menuBar);
203 JMenu fileMenu = new JMenu("File");
204 menuBar.add(fileMenu);
206 JMenuItem openAction = new JMenuItem("Open");
207 fileMenu.add(openAction);
208 openAction.addActionListener(this);
210 JMenuItem downloadAction = new JMenuItem("Download");
211 fileMenu.add(downloadAction);
212 downloadAction.addActionListener(this);
214 JMenuItem saveAction = new JMenuItem("Save a Copy");
215 fileMenu.add(saveAction);
216 saveAction.addActionListener(this);
218 JMenuItem exportAction = new JMenuItem("Export");
219 fileMenu.add(exportAction);
220 exportAction.addActionListener(this);
222 JMenuItem preferencesAction = new JMenuItem("Preferences");
223 fileMenu.add(preferencesAction);
224 preferencesAction.addActionListener(this);
226 JMenuItem closeAction = new JMenuItem("Close");
227 fileMenu.add(closeAction);
228 closeAction.addActionListener(this);
230 JMenuItem exitAction = new JMenuItem("Exit");
231 fileMenu.add(exitAction);
232 exitAction.addActionListener(this);
234 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
235 addWindowListener(new WindowAdapter() {
237 public void windowClosing(WindowEvent e) {
238 statsTable.tell_closing();
243 enable = new AltosUIEnable();
244 graph = new MicroGraph(enable);
245 statsTable = new MicroStatsTable();
246 raw = new MicroRaw();
247 pane.add(graph.panel, "Graph");
248 pane.add(enable, "Configure Graph");
249 pane.add(statsTable, "Statistics");
250 JScrollPane scroll = new JScrollPane(raw);
251 pane.add(scroll, "Raw Data");
255 container.doLayout();
256 container.validate();
259 Insets i = getInsets();
260 Dimension ps = pane.getPreferredSize();
261 ps.width += i.left + i.right;
262 ps.height += i.top + i.bottom;
263 // setPreferredSize(ps);
268 public static void help(int code) {
269 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
270 System.out.printf(" Options:\n");
271 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
272 System.out.printf(" --graph\tgraph a flight\n");
276 public static void main(final String[] args) {
277 boolean opened = false;
278 boolean graphing = true;
281 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
282 } catch (Exception e) {
285 for (int i = 0; i < args.length; i++) {
286 if (args[i].equals("--help"))
288 else if (args[i].equals("--export"))
290 else if (args[i].equals("--graph"))
292 else if (args[i].startsWith("--"))
295 File file = new File(args[i]);
302 } catch (Exception e) {
303 System.err.printf("Error processing \"%s\": %s\n",
304 file.getName(), e.getMessage());