e04d6790f048909385fbaaaae6c17c07e62dabe0
[fw/altos] / micropeak / MicroDownload.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 MicroDownload extends AltosUIDialog implements Runnable, ActionListener {
30         MicroPeak       owner;
31         Container       pane;
32         AltosDevice     device;
33         JButton         cancel;
34         MicroData       data;
35         MicroSerial     serial;
36
37         private void done_internal() {
38                 setVisible(false);
39                 if (data != null) {
40                         owner = owner.SetData(data);
41                         MicroSave save = new MicroSave(owner, data);
42                         if (save.runDialog())
43                                 owner.SetName(data.name);
44                 }
45                 dispose();
46         }
47
48         public void done() {
49                 Runnable r = new Runnable() {
50                                 public void run() {
51                                         try {
52                                                 done_internal();
53                                         } catch (Exception ex) {
54                                         }
55                                 }
56                         };
57                 SwingUtilities.invokeLater(r);
58         }
59
60         public void run() {
61                 try {
62                         data = new MicroData(serial, device.toShortString());
63                         serial.close();
64                 } catch (FileNotFoundException fe) {
65                 } catch (IOException ioe) {
66                 } catch (InterruptedException ie) {
67                 }
68                 done();
69         }
70
71         Thread  serial_thread;
72
73         public void start() {
74                 try {
75                         serial = new MicroSerial(device);
76                 } catch (FileNotFoundException fe) {
77                         return;
78                 }
79                 serial_thread = new Thread(this);
80                 serial_thread.start();
81         }
82
83         public void actionPerformed(ActionEvent ae) {
84                 if (serial_thread != null) {
85                         serial.close();
86                         serial_thread.interrupt();
87                 }
88         }
89
90         public MicroDownload(MicroPeak owner, AltosDevice device) {
91                 super (owner, "Download MicroPeak Data", false);
92
93                 GridBagConstraints c;
94                 Insets il = new Insets(4,4,4,4);
95                 Insets ir = new Insets(4,4,4,4);
96
97                 this.owner = owner;
98                 this.device = device;
99
100                 pane = getContentPane();
101                 pane.setLayout(new GridBagLayout());
102
103                 c = new GridBagConstraints();
104                 c.gridx = 0; c.gridy = 0;
105                 c.fill = GridBagConstraints.NONE;
106                 c.anchor = GridBagConstraints.LINE_START;
107                 c.insets = il;
108                 JLabel device_label = new JLabel("Device:");
109                 pane.add(device_label, c);
110
111                 c = new GridBagConstraints();
112                 c.gridx = 1; c.gridy = 0;
113                 c.fill = GridBagConstraints.HORIZONTAL;
114                 c.weightx = 1;
115                 c.anchor = GridBagConstraints.LINE_START;
116                 c.insets = ir;
117                 JLabel device_value = new JLabel(device.toString());
118                 pane.add(device_value, c);
119
120                 cancel = new JButton("Cancel");
121                 c = new GridBagConstraints();
122                 c.fill = GridBagConstraints.NONE;
123                 c.anchor = GridBagConstraints.CENTER;
124                 c.gridx = 0; c.gridy = 1;
125                 c.gridwidth = GridBagConstraints.REMAINDER;
126                 Insets ic = new Insets(4,4,4,4);
127                 c.insets = ic;
128                 pane.add(cancel, c);
129
130                 cancel.addActionListener(this);
131
132                 pack();
133                 setLocationRelativeTo(owner);
134                 setVisible(true);
135                 this.start();
136         }
137 }