altos: Rename telemetry to telemetry_orig
[fw/altos] / altosui / AltosEepromSelect.java
1 /*
2  * Copyright © 2011 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 altosui;
19
20 import java.lang.*;
21 import java.util.*;
22 import javax.swing.*;
23 import javax.swing.border.*;
24 import java.awt.*;
25 import java.awt.event.*;
26 import libaltosJNI.libaltos;
27 import libaltosJNI.altos_device;
28 import libaltosJNI.SWIGTYPE_p_altos_file;
29 import libaltosJNI.SWIGTYPE_p_altos_list;
30
31 class AltosEepromItem implements ActionListener {
32         AltosEepromLog  log;
33         JLabel          label;
34         JCheckBox       download;
35         JCheckBox       delete;
36
37         public void actionPerformed(ActionEvent e) {
38                 if (e.getSource() == download) {
39                         log.download = download.isSelected();
40                 } else if (e.getSource() == delete) {
41                         log.delete = delete.isSelected();
42                 }
43         }
44
45         public AltosEepromItem(AltosEepromLog in_log) {
46                 log = in_log;
47
48                 String  text;
49                 if (log.year != 0)
50                         text = String.format("Flight #%02d - %04d-%02d-%02d",
51                                              log.flight, log.year, log.month, log.day);
52                 else
53                         text = String.format("Flight #%02d", log.flight);
54
55                 label = new JLabel(text);
56
57                 download = new JCheckBox("", log.download);
58                 download.addActionListener(this);
59
60                 delete = new JCheckBox("", log.delete);
61                 delete.addActionListener(this);
62         }
63 }
64
65 public class AltosEepromSelect extends JDialog implements ActionListener {
66         private JList                   list;
67         private JFrame                  frame;
68         JButton                         ok;
69         JButton                         cancel;
70         boolean                         success;
71
72         /* Listen for events from our buttons */
73         public void actionPerformed(ActionEvent e) {
74                 String  cmd = e.getActionCommand();
75
76                 if (cmd.equals("ok"))
77                         success = true;
78                 setVisible(false);
79         }
80
81         public boolean run() {
82                 success = false;
83                 setLocationRelativeTo(frame);
84                 setVisible(true);
85                 return success;
86         }
87
88         public AltosEepromSelect (JFrame in_frame,
89                                   AltosEepromList flights) {
90
91                 super(in_frame, String.format("Flight list for serial %d", flights.config_data.serial), true);
92                 frame = in_frame;
93
94                 /* Create the container for the dialog */
95                 Container contentPane = getContentPane();
96
97                 /* First, we create a pane containing the dialog's header/title */
98                 JLabel  selectLabel = new JLabel("Select flights to download and/or delete", SwingConstants.CENTER);
99
100                 JPanel  labelPane = new JPanel();
101                 labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.X_AXIS));
102                 labelPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
103                 labelPane.add(Box.createHorizontalGlue());
104                 labelPane.add(selectLabel);
105                 labelPane.add(Box.createHorizontalGlue());
106
107                 /* Add the header to the container. */
108                 contentPane.add(labelPane, BorderLayout.PAGE_START);
109
110
111                 /* Now we create the evilness that is a GridBag for the flight details */
112                 GridBagConstraints c;
113                 Insets i = new Insets(4,4,4,4);
114                 JPanel flightPane = new JPanel();
115                 flightPane.setLayout(new GridBagLayout());
116                 flightPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
117
118                 /* Flight Header */
119                 c = new GridBagConstraints();
120                 c.gridx = 0; c.gridy = 0;
121                 c.fill = GridBagConstraints.NONE;
122                 c.weightx = 0.5;
123                 c.anchor = GridBagConstraints.CENTER;
124                 c.insets = i;
125                 JLabel flightHeaderLabel = new JLabel("Flight");
126                 flightPane.add(flightHeaderLabel, c);
127
128                 /* Download Header */
129                 c = new GridBagConstraints();
130                 c.gridx = 1; c.gridy = 0;
131                 c.fill = GridBagConstraints.NONE;
132                 c.weightx = 0.5;
133                 c.anchor = GridBagConstraints.CENTER;
134                 c.insets = i;
135                 JLabel downloadHeaderLabel = new JLabel("Download");
136                 flightPane.add(downloadHeaderLabel, c);
137
138                 /* Delete Header */
139                 c = new GridBagConstraints();
140                 c.gridx = 2; c.gridy = 0;
141                 c.fill = GridBagConstraints.NONE;
142                 c.weightx = 0.5;
143                 c.anchor = GridBagConstraints.CENTER;
144                 c.insets = i;
145                 JLabel deleteHeaderLabel = new JLabel("Delete");
146                 flightPane.add(deleteHeaderLabel, c);
147
148                 /* Add the flights to the GridBag */
149                 AltosEepromItem item;
150                 int itemNumber = 1;
151                 for (AltosEepromLog flight : flights) {
152                         /* Create a flight object with handlers and
153                          * appropriate UI items
154                          */
155                         item = new AltosEepromItem(flight);
156
157                         /* Add a decriptive label for the flight */
158                         c = new GridBagConstraints();
159                         c.gridx = 0; c.gridy = itemNumber;
160                         c.fill = GridBagConstraints.NONE;
161                         c.weightx = 0.5;
162                         c.anchor = GridBagConstraints.CENTER;
163                         c.insets = i;
164                         flightPane.add(item.label, c);
165
166                         /* Add a download checkbox for the flight */
167                         c = new GridBagConstraints();
168                         c.gridx = 1; c.gridy = itemNumber;
169                         c.fill = GridBagConstraints.NONE;
170                         c.weightx = 0.5;
171                         c.anchor = GridBagConstraints.CENTER;
172                         c.insets = i;
173                         flightPane.add(item.download, c);
174
175                         /* Add a delete checkbox for the flight */
176                         c = new GridBagConstraints();
177                         c.gridx = 2; c.gridy = itemNumber;
178                         c.fill = GridBagConstraints.NONE;
179                         c.weightx = 0.5;
180                         c.anchor = GridBagConstraints.CENTER;
181                         c.insets = i;
182                         flightPane.add(item.delete, c);
183
184                         itemNumber++;
185                 }
186
187                 /* Add the GridBag to the container */
188                 contentPane.add(flightPane, BorderLayout.CENTER);
189
190                 /* Create the dialog buttons */
191                 ok = new JButton("OK");
192                 ok.addActionListener(this);
193                 ok.setActionCommand("ok");
194
195                 cancel = new JButton("Cancel");
196                 cancel.addActionListener(this);
197                 cancel.setActionCommand("cancel");
198
199                 JPanel  buttonPane = new JPanel();
200                 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
201                 buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
202                 buttonPane.add(Box.createHorizontalGlue());
203                 buttonPane.add(cancel);
204                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
205                 buttonPane.add(ok);
206
207                 /* Add the buttons to the container */
208                 contentPane.add(buttonPane, BorderLayout.PAGE_END);
209
210                 /* Pack the window! */
211                 pack();
212         }
213 }