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_7.*;
27 import org.altusmetrum.altosuilib_7.*;
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) {
193 /* OSXAdapter interfaces */
194 public void macosx_file_handler(String path) {
195 CommandGraph(new File(path));
198 public void macosx_quit_handler() {
202 public void macosx_preferences_handler() {
210 register_for_macosx_events();
212 AltosUIPreferences.set_component(this);
214 container = getContentPane();
215 pane = new JTabbedPane();
217 setTitle("MicroPeak");
219 JMenuBar menuBar = new JMenuBar();
220 setJMenuBar(menuBar);
222 JMenu fileMenu = new JMenu("File");
223 menuBar.add(fileMenu);
225 JMenuItem openAction = new JMenuItem("Open");
226 fileMenu.add(openAction);
227 openAction.addActionListener(this);
229 JMenuItem downloadAction = new JMenuItem("Download");
230 fileMenu.add(downloadAction);
231 downloadAction.addActionListener(this);
233 JMenuItem saveAction = new JMenuItem("Save a Copy");
234 fileMenu.add(saveAction);
235 saveAction.addActionListener(this);
237 JMenuItem exportAction = new JMenuItem("Export");
238 fileMenu.add(exportAction);
239 exportAction.addActionListener(this);
241 JMenuItem preferencesAction = new JMenuItem("Preferences");
242 fileMenu.add(preferencesAction);
243 preferencesAction.addActionListener(this);
245 JMenuItem closeAction = new JMenuItem("Close");
246 fileMenu.add(closeAction);
247 closeAction.addActionListener(this);
249 JMenuItem exitAction = new JMenuItem("Exit");
250 fileMenu.add(exitAction);
251 exitAction.addActionListener(this);
253 JButton downloadButton = new JButton ("Download");
254 downloadButton.addActionListener(this);
255 menuBar.add(downloadButton);
257 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
258 addWindowListener(new WindowAdapter() {
260 public void windowClosing(WindowEvent e) {
261 statsTable.tell_closing();
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");
278 container.doLayout();
279 container.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);
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");
299 public static void main(final String[] args) {
300 boolean opened = false;
301 boolean graphing = true;
304 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
305 } catch (Exception e) {
308 for (int i = 0; i < args.length; i++) {
309 if (args[i].equals("--help"))
311 else if (args[i].equals("--export"))
313 else if (args[i].equals("--graph"))
315 else if (args[i].startsWith("--"))
318 File file = new File(args[i]);
325 } catch (Exception e) {
326 System.err.printf("Error processing \"%s\": %s\n",
327 file.getName(), e.getMessage());