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 stats;
39 static int number_of_windows;
41 MicroPeak SetData(MicroData data) {
43 if (this.data != null) {
45 return mp.SetData(data);
48 graph.setDataSet(data);
55 void SetName(String name) {
60 private static MicroData ReadFile(File filename) throws IOException, FileNotFoundException {
61 MicroData data = null;
62 FileInputStream fis = new FileInputStream(filename);
64 data = new MicroData((InputStream) fis, filename.getName());
65 } catch (InterruptedException ie) {
73 private void OpenFile(File filename) {
75 SetData(ReadFile(filename));
76 } catch (FileNotFoundException fne) {
77 JOptionPane.showMessageDialog(this,
80 JOptionPane.ERROR_MESSAGE);
81 } catch (IOException ioe) {
82 JOptionPane.showMessageDialog(this,
85 JOptionPane.ERROR_MESSAGE);
89 private void SelectFile() {
90 MicroFileChooser chooser = new MicroFileChooser(this);
91 File file = chooser.runDialog();
97 private void Preferences() {
98 new AltosUIConfigure(this);
101 private void DownloadData() {
102 AltosDevice device = MicroDeviceDialog.show(this);
105 new MicroDownload(this, device);
108 private void no_data() {
109 JOptionPane.showMessageDialog(this,
112 JOptionPane.INFORMATION_MESSAGE);
115 private void Save() {
120 MicroSave save = new MicroSave (this, data);
121 if (save.runDialog())
125 private void Export() {
130 MicroExport export = new MicroExport (this, data);
134 private static void CommandGraph(File file) {
135 MicroPeak m = new MicroPeak();
139 private static void CommandExport(File file) {
141 MicroData d = ReadFile(file);
143 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
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());
153 } catch (IOException ie) {
154 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
158 private void Close() {
162 if (number_of_windows == 0)
166 public void actionPerformed(ActionEvent ev) {
167 if ("Exit".equals(ev.getActionCommand()))
169 else if ("Close".equals(ev.getActionCommand()))
171 else if ("Open".equals(ev.getActionCommand()))
173 else if ("Download".equals(ev.getActionCommand()))
175 else if ("Export".equals(ev.getActionCommand()))
177 else if ("Preferences".equals(ev.getActionCommand()))
179 else if ("Save a Copy".equals(ev.getActionCommand()))
183 public void itemStateChanged(ItemEvent e) {
190 AltosUIPreferences.set_component(this);
192 container = getContentPane();
193 pane = new JTabbedPane();
195 setTitle("MicroPeak");
197 JMenuBar menuBar = new JMenuBar();
198 setJMenuBar(menuBar);
200 JMenu fileMenu = new JMenu("File");
201 menuBar.add(fileMenu);
203 JMenuItem openAction = new JMenuItem("Open");
204 fileMenu.add(openAction);
205 openAction.addActionListener(this);
207 JMenuItem downloadAction = new JMenuItem("Download");
208 fileMenu.add(downloadAction);
209 downloadAction.addActionListener(this);
211 JMenuItem saveAction = new JMenuItem("Save a Copy");
212 fileMenu.add(saveAction);
213 saveAction.addActionListener(this);
215 JMenuItem exportAction = new JMenuItem("Export");
216 fileMenu.add(exportAction);
217 exportAction.addActionListener(this);
219 JMenuItem preferencesAction = new JMenuItem("Preferences");
220 fileMenu.add(preferencesAction);
221 preferencesAction.addActionListener(this);
223 JMenuItem closeAction = new JMenuItem("Close");
224 fileMenu.add(closeAction);
225 closeAction.addActionListener(this);
227 JMenuItem exitAction = new JMenuItem("Exit");
228 fileMenu.add(exitAction);
229 exitAction.addActionListener(this);
231 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
232 addWindowListener(new WindowAdapter() {
234 public void windowClosing(WindowEvent e) {
235 stats.tell_closing();
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");
252 container.doLayout();
253 container.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);
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");
273 public static void main(final String[] args) {
274 boolean opened = false;
275 boolean graphing = true;
278 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
279 } catch (Exception e) {
282 for (int i = 0; i < args.length; i++) {
283 if (args[i].equals("--help"))
285 else if (args[i].equals("--export"))
287 else if (args[i].equals("--graph"))
289 else if (args[i].startsWith("--"))
292 File file = new File(args[i]);
299 } catch (Exception e) {
300 System.err.printf("Error processing \"%s\": %s\n",
301 file.getName(), e.getMessage());