micropeak: Add CSV export
[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         MicroStatsTable stats;
34         MicroData       data;
35         Container       container;
36         JTabbedPane     pane;
37         static int      number_of_windows;
38
39         MicroPeak SetData(MicroData data) {
40                 MicroPeak       mp = this;
41                 if (this.data != null) {
42                         mp = new MicroPeak();
43                         return mp.SetData(data);
44                 }
45                 this.data = data;
46                 graph.setData(data);
47                 stats.setData(data);
48                 setTitle(data.name);
49                 return this;
50         }
51
52         void SetName(String name) {
53                 graph.setName(name);
54                 setTitle(name);
55         }
56
57         private void RunFile(InputStream input, String name) {
58                 try {
59                         MicroData data = new MicroData(input, name);
60                         SetData(data);
61                 } catch (IOException ioe) {
62                         JOptionPane.showMessageDialog(this,
63                                                       ioe.getMessage(),
64                                                       "File Read Error",
65                                                       JOptionPane.ERROR_MESSAGE);
66                 } catch (InterruptedException ie) {
67                 }
68                 try {
69                         input.close();
70                 } catch (IOException ioe) {
71                 }
72         }
73
74         private void OpenFile(File filename) {
75                 try {
76                         RunFile (new FileInputStream(filename), filename.getName());
77                 } catch (FileNotFoundException fne) {
78                         JOptionPane.showMessageDialog(this,
79                                                       fne.getMessage(),
80                                                       "Cannot open file",
81                                                       JOptionPane.ERROR_MESSAGE);
82                 }
83         }
84
85         private void SelectFile() {
86                 MicroFileChooser        chooser = new MicroFileChooser(this);
87                 InputStream             input = chooser.runDialog();
88
89                 if (input != null)
90                         RunFile(input, chooser.filename);
91         }
92
93         private void Preferences() {
94                 new AltosUIConfigure(this);
95         }
96
97         private void DownloadData() {
98                 AltosDevice     device = MicroDeviceDialog.show(this);
99                 
100                 if (device != null)
101                         new MicroDownload(this, device);
102         }
103
104         private void no_data() {
105                         JOptionPane.showMessageDialog(this,
106                                                       "No data available",
107                                                       "No data",
108                                                       JOptionPane.INFORMATION_MESSAGE);
109         }
110         private void Save() {
111                 if (data == null) {
112                         no_data();
113                         return;
114                 }
115                 MicroSave       save = new MicroSave (this, data);
116                 if (save.runDialog())
117                         SetName(data.name);
118         }
119         
120         private void Export() {
121                 if (data == null) {
122                         no_data();
123                         return;
124                 }
125                 MicroExport     export = new MicroExport (this, data);
126                 export.runDialog();
127         }
128
129         private void Close() {
130                 setVisible(false);
131                 dispose();
132                 --number_of_windows;
133                 if (number_of_windows == 0)
134                         System.exit(0);
135         }
136
137         public void actionPerformed(ActionEvent ev) {
138                 if ("Exit".equals(ev.getActionCommand()))
139                         System.exit(0);
140                 else if ("Close".equals(ev.getActionCommand()))
141                         Close();
142                 else if ("Open".equals(ev.getActionCommand()))
143                         SelectFile();
144                 else if ("Download".equals(ev.getActionCommand()))
145                         DownloadData();
146                 else if ("Export".equals(ev.getActionCommand()))
147                         Export();
148                 else if ("Preferences".equals(ev.getActionCommand()))
149                         Preferences();
150                 else if ("Save a Copy".equals(ev.getActionCommand()))
151                         Save();
152         }
153
154         public void itemStateChanged(ItemEvent e) {
155         }
156
157         public MicroPeak() {
158
159                 ++number_of_windows;
160
161                 AltosUIPreferences.set_component(this);
162
163                 container = getContentPane();
164                 pane = new JTabbedPane();
165
166                 setTitle("MicroPeak");
167
168                 JMenuBar menuBar = new JMenuBar();
169                 setJMenuBar(menuBar);
170
171                 JMenu fileMenu = new JMenu("File");
172                 menuBar.add(fileMenu);
173
174                 JMenuItem openAction = new JMenuItem("Open");
175                 fileMenu.add(openAction);
176                 openAction.addActionListener(this);
177
178                 JMenuItem downloadAction = new JMenuItem("Download");
179                 fileMenu.add(downloadAction);
180                 downloadAction.addActionListener(this);
181
182                 JMenuItem saveAction = new JMenuItem("Save a Copy");
183                 fileMenu.add(saveAction);
184                 saveAction.addActionListener(this);
185
186                 JMenuItem exportAction = new JMenuItem("Export");
187                 fileMenu.add(exportAction);
188                 exportAction.addActionListener(this);
189
190                 JMenuItem preferencesAction = new JMenuItem("Preferences");
191                 fileMenu.add(preferencesAction);
192                 preferencesAction.addActionListener(this);
193
194                 JMenuItem closeAction = new JMenuItem("Close");
195                 fileMenu.add(closeAction);
196                 closeAction.addActionListener(this);
197
198                 JMenuItem exitAction = new JMenuItem("Exit");
199                 fileMenu.add(exitAction);
200                 exitAction.addActionListener(this);
201
202                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
203                 addWindowListener(new WindowAdapter() {
204                         @Override
205                         public void windowClosing(WindowEvent e) {
206                                 Close();
207                         }
208                 });
209
210                 graph = new MicroGraph();
211                 stats = new MicroStatsTable();
212                 pane.add(graph.panel, "Graph");
213                 pane.add(stats, "Statistics");
214                 pane.doLayout();
215                 pane.validate();
216                 container.add(pane);
217                 container.doLayout();
218                 container.validate();
219                 doLayout();
220                 validate();
221                 Insets i = getInsets();
222                 Dimension ps = pane.getPreferredSize();
223                 ps.width += i.left + i.right;
224                 ps.height += i.top + i.bottom;
225 //              setPreferredSize(ps);
226                 setSize(ps);
227                 setLocationByPlatform(true);
228                 setVisible(true);
229         }
230
231         public static void main(final String[] args) {
232                 boolean opened = false;
233
234                 try {
235                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
236                 } catch (Exception e) {
237                 }
238
239                 for (int i = 0; i < args.length; i++) {
240                         MicroPeak m = new MicroPeak();
241                         m.OpenFile(new File(args[i]));
242                         opened = true;
243                 }
244                 if (!opened)
245                         new MicroPeak();
246         }
247 }