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