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