7995d32b80b0d9f3a6a8b564b08da22e27e7938e
[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 for flight %d set to %b\n", log.flight, log.download);
42                 } else if (e.getSource() == delete) {
43                         log.delete = delete.isSelected();
44                         System.out.printf("delete for flight %d set to %b\n", log.flight, log.delete);
45                 }
46         }
47
48         public AltosEepromItem(AltosEepromLog in_log) {
49                 log = in_log;
50
51                 label = new JLabel(String.format("Flight #%02d - %04d-%02d-%02d",
52                                                  log.flight, log.year, log.month, log.day));
53                 label.setPreferredSize(new Dimension(170, 15));
54                 add(label);
55                 download = new JCheckBox("", log.download);
56                 download.addActionListener(this);
57                 download.setPreferredSize(new Dimension(100, 15));
58                 download.setHorizontalAlignment(SwingConstants.CENTER);
59                 add(download);
60                 delete = new JCheckBox("", log.delete);
61                 delete.addActionListener(this);
62                 delete.setPreferredSize(new Dimension(70, 15));
63                 delete.setHorizontalAlignment(SwingConstants.CENTER);
64                 add(delete);
65         }
66 }
67
68 public class AltosEepromSelect extends JDialog implements ActionListener {
69         private JList                   list;
70         private JFrame                  frame;
71         JButton                         ok;
72         JButton                         cancel;
73         boolean                         success;
74
75         /* Listen for events from our buttons */
76         public void actionPerformed(ActionEvent e) {
77                 String  cmd = e.getActionCommand();
78
79                 if (cmd.equals("ok"))
80                         success = true;
81                 setVisible(false);
82         }
83
84         public boolean run() {
85                 success = false;
86                 setLocationRelativeTo(frame);
87                 setVisible(true);
88                 return success;
89         }
90
91         public AltosEepromSelect (JFrame in_frame,
92                                   AltosEepromList flights) {
93
94                 super(in_frame, String.format("Flight list for serial %d", flights.config_data.serial), true);
95                 frame = in_frame;
96
97                 JLabel  selectLabel = new JLabel("Select flights to download and/or delete", SwingConstants.CENTER);
98
99                 JPanel  labelPane = new JPanel();
100                 labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.X_AXIS));
101                 labelPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
102                 labelPane.add(Box.createHorizontalGlue());
103                 labelPane.add(selectLabel);
104                 labelPane.add(Box.createHorizontalGlue());
105
106                 JLabel  flightHeaderLabel   = new JLabel("Flight", SwingConstants.CENTER);
107                 flightHeaderLabel.setPreferredSize(new Dimension(170, 15));
108
109                 JLabel  downloadHeaderLabel = new JLabel("Download", SwingConstants.CENTER);
110                 downloadHeaderLabel.setPreferredSize(new Dimension(100, 15));
111
112                 JLabel  deleteHeaderLabel   = new JLabel("Delete", SwingConstants.CENTER);
113                 deleteHeaderLabel.setPreferredSize(new Dimension(70, 15));
114
115                 JPanel  headerPane = new JPanel();
116                 headerPane.add(flightHeaderLabel);
117                 headerPane.add(downloadHeaderLabel);
118                 headerPane.add(deleteHeaderLabel);
119
120                 JPanel  flightPane = new JPanel();
121                 flightPane.setLayout(new BoxLayout(flightPane, BoxLayout.Y_AXIS));
122                 flightPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
123                 flightPane.add(headerPane);
124                 for (AltosEepromLog flight : flights) {
125                         flightPane.add(new AltosEepromItem(flight));
126                 }
127
128                 ok = new JButton("OK");
129                 ok.addActionListener(this);
130                 ok.setActionCommand("ok");
131
132                 cancel = new JButton("Cancel");
133                 cancel.addActionListener(this);
134                 cancel.setActionCommand("cancel");
135
136                 JPanel  buttonPane = new JPanel();
137                 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
138                 buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
139                 buttonPane.add(Box.createHorizontalGlue());
140                 buttonPane.add(cancel);
141                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
142                 buttonPane.add(ok);
143
144                 Container contentPane = getContentPane();
145
146                 contentPane.add(labelPane, BorderLayout.PAGE_START);
147                 contentPane.add(flightPane, BorderLayout.CENTER);
148                 contentPane.add(buttonPane, BorderLayout.PAGE_END);
149
150                 pack();
151         }
152 }