altosui: Add eeprom 'manage' ui to download and delete multiple flights
[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 extends JPanel implements ActionListener {
32         AltosEepromLog  log;
33         JCheckBox       download;
34         JCheckBox       delete;
35         JLabel          label;
36
37         public void actionPerformed(ActionEvent e) {
38                 System.out.printf("eeprom item action %s %d\n", e.getActionCommand(), e.getID());
39                 if (e.getSource() == download) {
40                         log.download = download.isSelected();
41                         System.out.printf("download set to %b\n", log.download);
42                 } else if (e.getSource() == delete) {
43                         log.delete = delete.isSelected();
44                         System.out.printf("delete set to %b\n", log.delete);
45                 }
46         }
47
48         public AltosEepromItem(AltosEepromLog in_log) {
49                 log = in_log;
50
51                 download = new JCheckBox("Download", log.download);
52                 download.addActionListener(this);
53                 add(download);
54                 delete = new JCheckBox("Delete", log.delete);
55                 delete.addActionListener(this);
56                 add(delete);
57                 label = new JLabel(String.format("Flight %d %4d-%02d-%02d",
58                                                  log.flight, log.year, log.month, log.day));
59                 add(label);
60         }
61 }
62
63 public class AltosEepromSelect extends JDialog implements ActionListener {
64         private JList                   list;
65         private JFrame                  frame;
66         JButton                         ok;
67         JButton                         cancel;
68         boolean                         success;
69
70         /* Listen for events from our buttons */
71         public void actionPerformed(ActionEvent e) {
72                 String  cmd = e.getActionCommand();
73
74                 if (cmd.equals("ok"))
75                         success = true;
76                 setVisible(false);
77         }
78
79         public boolean run() {
80                 success = false;
81                 setLocationRelativeTo(frame);
82                 setVisible(true);
83                 return success;
84         }
85
86         public AltosEepromSelect (JFrame in_frame,
87                                   AltosEepromList flights) {
88
89                 super(in_frame, String.format("Flight list for serial %d", flights.config_data.serial), true);
90                 frame = in_frame;
91
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                 JPanel  flightPane = new JPanel();
102                 flightPane.setLayout(new BoxLayout(flightPane, BoxLayout.Y_AXIS));
103                 flightPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
104                 for (AltosEepromLog flight : flights) {
105                         flightPane.add(new AltosEepromItem(flight));
106                 }
107
108                 ok = new JButton("OK");
109                 ok.addActionListener(this);
110                 ok.setActionCommand("ok");
111
112                 cancel = new JButton("Cancel");
113                 cancel.addActionListener(this);
114                 cancel.setActionCommand("cancel");
115
116                 JPanel  buttonPane = new JPanel();
117                 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
118                 buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
119                 buttonPane.add(Box.createHorizontalGlue());
120                 buttonPane.add(cancel);
121                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
122                 buttonPane.add(ok);
123
124                 Container contentPane = getContentPane();
125
126                 contentPane.add(labelPane, BorderLayout.PAGE_START);
127                 contentPane.add(flightPane, BorderLayout.CENTER);
128                 contentPane.add(buttonPane, BorderLayout.PAGE_END);
129
130                 pack();
131         }
132 }