c9074348a057aa9e8f7344b62173c4a1d7e13a6f
[fw/altos] / micropeak / MicroPeak.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.micropeak;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.util.concurrent.*;
25 import java.util.*;
26 import org.altusmetrum.AltosLib.*;
27
28 public class MicroPeak extends MicroFrame implements ActionListener, ItemListener {
29
30         File            filename;
31         MicroGraph      graph;
32         MicroData       data;
33         Container       pane;
34
35         private void RunFile(InputStream input) {
36                 try {
37                         data = new MicroData(input);
38                         graph.setData(data);
39                 } catch (IOException ioe) {
40                 }
41                 try {
42                         input.close();
43                 } catch (IOException ioe) {
44                 }
45         }
46
47         private void OpenFile(File filename) {
48                 try {
49                         RunFile (new FileInputStream(filename));
50                 } catch (FileNotFoundException fne) {
51                 }
52         }
53
54         private void SelectFile() {
55                 MicroFileChooser        chooser = new MicroFileChooser(this);
56                 InputStream             input = chooser.runDialog();
57
58                 if (input != null)
59                         RunFile(input);
60         }
61
62         private void DownloadData() {
63                 java.util.List<MicroUSB>        devices = MicroUSB.list();
64                 for (MicroUSB device : devices)
65                         System.out.printf("device %s\n", device.toString());
66         }
67
68         public void actionPerformed(ActionEvent ev) {
69                 System.out.printf("action %s %s\n", ev.getActionCommand(), ev.paramString());
70                 if ("Exit".equals(ev.getActionCommand()))
71                         System.exit(0);
72                 else if ("Open".equals(ev.getActionCommand()))
73                         SelectFile();
74                 else if ("New".equals(ev.getActionCommand()))
75                         new MicroPeak();
76                 else if ("Download".equals(ev.getActionCommand()))
77                         DownloadData();
78         }
79
80         public void itemStateChanged(ItemEvent e) {
81         }
82
83         public MicroPeak() {
84
85                 this.filename = filename;
86
87                 pane = getContentPane();
88
89                 setTitle("MicroPeak");
90
91                 JMenuBar menuBar = new JMenuBar();
92                 setJMenuBar(menuBar);
93
94                 JMenu fileMenu = new JMenu("File");
95                 menuBar.add(fileMenu);
96
97                 JMenuItem newAction = new JMenuItem("New");
98                 fileMenu.add(newAction);
99                 newAction.addActionListener(this);
100
101                 JMenuItem openAction = new JMenuItem("Open");
102                 fileMenu.add(openAction);
103                 openAction.addActionListener(this);
104
105                 JMenuItem downloadAction = new JMenuItem("Download");
106                 fileMenu.add(downloadAction);
107                 downloadAction.addActionListener(this);
108
109                 JMenuItem exitAction = new JMenuItem("Exit");
110                 fileMenu.add(exitAction);
111                 exitAction.addActionListener(this);
112
113                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
114                 addWindowListener(new WindowAdapter() {
115                         @Override
116                         public void windowClosing(WindowEvent e) {
117                                 System.exit(0);
118                         }
119                 });
120
121                 graph = new MicroGraph(data);
122                 pane.add(graph.panel);
123                 pane.doLayout();
124                 pane.validate();
125                 doLayout();
126                 validate();
127                 Insets i = getInsets();
128                 Dimension ps = pane.getPreferredSize();
129                 ps.width += i.left + i.right;
130                 ps.height += i.top + i.bottom;
131                 setPreferredSize(ps);
132                 setSize(ps);
133                 setVisible(true);
134         }
135
136         public static void main(final String[] args) {
137                 boolean opened = false;
138
139                 for (int i = 0; i < args.length; i++) {
140                         MicroPeak m = new MicroPeak();
141                         m.OpenFile(new File(args[i]));
142                         opened = true;
143                 }
144                 if (!opened)
145                         new MicroPeak();
146         }
147 }