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 } catch (InterruptedException ie) {
75 private void OpenFile(File filename) {
77 SetData(ReadFile(filename));
78 } catch (FileNotFoundException fne) {
79 JOptionPane.showMessageDialog(this,
82 JOptionPane.ERROR_MESSAGE);
83 } catch (IOException ioe) {
84 JOptionPane.showMessageDialog(this,
87 JOptionPane.ERROR_MESSAGE);
91 private void SelectFile() {
92 MicroFileChooser chooser = new MicroFileChooser(this);
93 File file = chooser.runDialog();
99 private void Preferences() {
100 new AltosUIConfigure(this);
103 private void DownloadData() {
104 AltosDevice device = MicroDeviceDialog.show(this);
107 new MicroDownload(this, device);
110 private void no_data() {
111 JOptionPane.showMessageDialog(this,
114 JOptionPane.INFORMATION_MESSAGE);
117 private void Save() {
122 MicroSave save = new MicroSave (this, data);
123 if (save.runDialog())
127 private void Export() {
132 MicroExport export = new MicroExport (this, data);
136 private static void CommandGraph(File file) {
137 MicroPeak m = new MicroPeak();
141 private static void CommandExport(File file) {
143 MicroData d = ReadFile(file);
145 File csv = new File(AltosLib.replace_extension(file.getPath(), ".csv"));
147 System.out.printf ("Export \"%s\" to \"%s\"\n", file.getPath(), csv.getPath());
148 MicroExport.export(csv, d);
149 } catch (FileNotFoundException fe) {
150 System.err.printf("Cannot create file \"%s\" (%s)\n", csv.getName(), fe.getMessage());
151 } catch (IOException ie) {
152 System.err.printf("Cannot write file \"%s\" (%s)\n", csv.getName(), ie.getMessage());
155 } catch (IOException ie) {
156 System.err.printf("Cannot read file \"%s\" (%s)\n", file.getName(), ie.getMessage());
160 private void Close() {
164 if (number_of_windows == 0)
168 public void actionPerformed(ActionEvent ev) {
169 if ("Exit".equals(ev.getActionCommand()))
171 else if ("Close".equals(ev.getActionCommand()))
173 else if ("Open".equals(ev.getActionCommand()))
175 else if ("Download".equals(ev.getActionCommand()))
177 else if ("Export".equals(ev.getActionCommand()))
179 else if ("Preferences".equals(ev.getActionCommand()))
181 else if ("Save a Copy".equals(ev.getActionCommand()))
185 public void itemStateChanged(ItemEvent e) {
192 AltosUIPreferences.set_component(this);
194 container = getContentPane();
195 pane = new JTabbedPane();
197 setTitle("MicroPeak");
199 JMenuBar menuBar = new JMenuBar();
200 setJMenuBar(menuBar);
202 JMenu fileMenu = new JMenu("File");
203 menuBar.add(fileMenu);
205 JMenuItem openAction = new JMenuItem("Open");
206 fileMenu.add(openAction);
207 openAction.addActionListener(this);
209 JMenuItem downloadAction = new JMenuItem("Download");
210 fileMenu.add(downloadAction);
211 downloadAction.addActionListener(this);
213 JMenuItem saveAction = new JMenuItem("Save a Copy");
214 fileMenu.add(saveAction);
215 saveAction.addActionListener(this);
217 JMenuItem exportAction = new JMenuItem("Export");
218 fileMenu.add(exportAction);
219 exportAction.addActionListener(this);
221 JMenuItem preferencesAction = new JMenuItem("Preferences");
222 fileMenu.add(preferencesAction);
223 preferencesAction.addActionListener(this);
225 JMenuItem closeAction = new JMenuItem("Close");
226 fileMenu.add(closeAction);
227 closeAction.addActionListener(this);
229 JMenuItem exitAction = new JMenuItem("Exit");
230 fileMenu.add(exitAction);
231 exitAction.addActionListener(this);
233 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
234 addWindowListener(new WindowAdapter() {
236 public void windowClosing(WindowEvent e) {
237 statsTable.tell_closing();
242 enable = new AltosUIEnable();
243 graph = new MicroGraph(enable);
244 statsTable = new MicroStatsTable();
245 raw = new MicroRaw();
246 pane.add(graph.panel, "Graph");
247 pane.add(enable, "Configure Graph");
248 pane.add(statsTable, "Statistics");
249 JScrollPane scroll = new JScrollPane(raw);
250 pane.add(scroll, "Raw Data");
254 container.doLayout();
255 container.validate();
258 Insets i = getInsets();
259 Dimension ps = pane.getPreferredSize();
260 ps.width += i.left + i.right;
261 ps.height += i.top + i.bottom;
262 // setPreferredSize(ps);
267 public static void help(int code) {
268 System.out.printf("Usage: micropeak [OPTION] ... [FILE]...\n");
269 System.out.printf(" Options:\n");
270 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
271 System.out.printf(" --graph\tgraph a flight\n");
275 public static void main(final String[] args) {
276 boolean opened = false;
277 boolean graphing = true;
280 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
281 } catch (Exception e) {
284 for (int i = 0; i < args.length; i++) {
285 if (args[i].equals("--help"))
287 else if (args[i].equals("--export"))
289 else if (args[i].equals("--graph"))
291 else if (args[i].startsWith("--"))
294 File file = new File(args[i]);
301 } catch (Exception e) {
302 System.err.printf("Error processing \"%s\": %s\n",
303 file.getName(), e.getMessage());