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