micropeak: Add 'Close' menu item. Fix start location
[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 AltosConfigureUI(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 Save() {
105                 if (data == null) {
106                         JOptionPane.showMessageDialog(this,
107                                                       "No data available",
108                                                       "No data",
109                                                       JOptionPane.INFORMATION_MESSAGE);
110                         return;
111                 }
112                 MicroSave       save = new MicroSave (this, data);
113                 if (save.runDialog())
114                         SetName(data.name);
115         }
116         
117         private void Close() {
118                 setVisible(false);
119                 dispose();
120                 --number_of_windows;
121                 if (number_of_windows == 0)
122                         System.exit(0);
123         }
124
125         public void actionPerformed(ActionEvent ev) {
126                 if ("Exit".equals(ev.getActionCommand()))
127                         System.exit(0);
128                 else if ("Close".equals(ev.getActionCommand()))
129                         Close();
130                 else if ("Open".equals(ev.getActionCommand()))
131                         SelectFile();
132                 else if ("Download".equals(ev.getActionCommand()))
133                         DownloadData();
134                 else if ("Preferences".equals(ev.getActionCommand()))
135                         Preferences();
136                 else if ("Save a Copy".equals(ev.getActionCommand()))
137                         Save();
138         }
139
140         public void itemStateChanged(ItemEvent e) {
141         }
142
143         public MicroPeak() {
144
145                 ++number_of_windows;
146
147                 AltosUIPreferences.set_component(this);
148
149                 container = getContentPane();
150                 pane = new JTabbedPane();
151
152                 setTitle("MicroPeak");
153
154                 JMenuBar menuBar = new JMenuBar();
155                 setJMenuBar(menuBar);
156
157                 JMenu fileMenu = new JMenu("File");
158                 menuBar.add(fileMenu);
159
160                 JMenuItem openAction = new JMenuItem("Open");
161                 fileMenu.add(openAction);
162                 openAction.addActionListener(this);
163
164                 JMenuItem downloadAction = new JMenuItem("Download");
165                 fileMenu.add(downloadAction);
166                 downloadAction.addActionListener(this);
167
168                 JMenuItem saveAction = new JMenuItem("Save a Copy");
169                 fileMenu.add(saveAction);
170                 saveAction.addActionListener(this);
171
172                 JMenuItem preferencesAction = new JMenuItem("Preferences");
173                 fileMenu.add(preferencesAction);
174                 preferencesAction.addActionListener(this);
175
176                 JMenuItem closeAction = new JMenuItem("Close");
177                 fileMenu.add(closeAction);
178                 closeAction.addActionListener(this);
179
180                 JMenuItem exitAction = new JMenuItem("Exit");
181                 fileMenu.add(exitAction);
182                 exitAction.addActionListener(this);
183
184                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
185                 addWindowListener(new WindowAdapter() {
186                         @Override
187                         public void windowClosing(WindowEvent e) {
188                                 Close();
189                         }
190                 });
191
192                 graph = new MicroGraph();
193                 stats = new MicroStatsTable();
194                 pane.add(graph.panel, "Graph");
195                 pane.add(stats, "Statistics");
196                 pane.doLayout();
197                 pane.validate();
198                 container.add(pane);
199                 container.doLayout();
200                 container.validate();
201                 doLayout();
202                 validate();
203                 Insets i = getInsets();
204                 Dimension ps = pane.getPreferredSize();
205                 ps.width += i.left + i.right;
206                 ps.height += i.top + i.bottom;
207 //              setPreferredSize(ps);
208                 setSize(ps);
209                 setLocationByPlatform(true);
210                 setVisible(true);
211         }
212
213         public static void main(final String[] args) {
214                 boolean opened = false;
215
216                 try {
217                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
218                 } catch (Exception e) {
219                 }
220
221                 for (int i = 0; i < args.length; i++) {
222                         MicroPeak m = new MicroPeak();
223                         m.OpenFile(new File(args[i]));
224                         opened = true;
225                 }
226                 if (!opened)
227                         new MicroPeak();
228         }
229 }