Start building MicroPeak GUI tool
[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 JFrame implements ActionListener, ItemListener {
29
30         File            filename;
31         MicroGraph      graph;
32         MicroData       data;
33         Container       pane;
34
35         private void OpenFile(File filename) {
36                 try {
37                         FileInputStream input = new FileInputStream(filename);
38                         try {
39                                 data = new MicroData(input);
40                                 graph = new MicroGraph(data);
41                                 pane.add(graph.panel);
42                         } catch (IOException ioe) {
43                         }
44                         try {
45                                 input.close();
46                         } catch (IOException ioe) {
47                         }
48                 } catch (FileNotFoundException fne) {
49                 }
50         }
51
52         private void SelectFile() {
53         }
54
55         private void DownloadData() {
56                 java.util.List<MicroUSB>        devices = MicroUSB.list();
57                 for (MicroUSB device : devices)
58                         System.out.printf("device %s\n", device.toString());
59         }
60
61         public void actionPerformed(ActionEvent ev) {
62                 System.out.printf("action %s %s\n", ev.getActionCommand(), ev.paramString());
63                 if ("Exit".equals(ev.getActionCommand()))
64                         System.exit(0);
65                 else if ("Open".equals(ev.getActionCommand()))
66                         SelectFile();
67                 else if ("New".equals(ev.getActionCommand()))
68                         new MicroPeak();
69                 else if ("Download".equals(ev.getActionCommand()))
70                         DownloadData();
71         }
72
73         public void itemStateChanged(ItemEvent e) {
74         }
75
76         public MicroPeak(File filename) {
77
78                 this.filename = filename;
79
80                 pane = getContentPane();
81
82 //              JLabel label = new JLabel ("Hello, World");
83 //              pane.add(label);
84
85                 setSize(800, 500);
86
87                 setTitle("MicroPeak");
88
89                 JMenuBar menuBar = new JMenuBar();
90                 setJMenuBar(menuBar);
91
92                 JMenu fileMenu = new JMenu("File");
93                 menuBar.add(fileMenu);
94
95                 JMenuItem newAction = new JMenuItem("New");
96                 fileMenu.add(newAction);
97                 newAction.addActionListener(this);
98
99                 JMenuItem openAction = new JMenuItem("Open");
100                 fileMenu.add(openAction);
101                 openAction.addActionListener(this);
102
103                 JMenuItem downloadAction = new JMenuItem("Download");
104                 fileMenu.add(downloadAction);
105                 downloadAction.addActionListener(this);
106
107                 JMenuItem exitAction = new JMenuItem("Exit");
108                 fileMenu.add(exitAction);
109                 exitAction.addActionListener(this);
110
111                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
112                 addWindowListener(new WindowAdapter() {
113                         @Override
114                         public void windowClosing(WindowEvent e) {
115                                 System.exit(0);
116                         }
117                 });
118
119                 if (filename != null)
120                         this.OpenFile(filename);
121                 setVisible(true);
122         }
123
124         public MicroPeak() {
125                 this(null);
126         }
127
128         public static void main(final String[] args) {
129                 boolean opened = false;
130
131                 for (int i = 0; i < args.length; i++) {
132                         new MicroPeak(new File(args[i]));
133                         opened = true;
134                 }
135                 if (!opened)
136                         new MicroPeak();
137         }
138 }