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