4ad78896b3445b4a100ec2cbd5cbb8256a8085cc
[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       action;
36         JCheckBox       delete;
37
38         public void actionPerformed(ActionEvent e) {
39                 log.selected = action.isSelected();
40         }
41
42         public AltosEepromItem(AltosEepromLog in_log) {
43                 log = in_log;
44
45                 String  text;
46                 if (log.year != 0)
47                         text = String.format("Flight #%02d - %04d-%02d-%02d",
48                                              log.flight, log.year, log.month, log.day);
49                 else
50                         text = String.format("Flight #%02d", log.flight);
51
52                 label = new JLabel(text);
53
54                 action = new JCheckBox("", log.selected);
55                 action.addActionListener(this);
56         }
57 }
58
59 public class AltosEepromSelect extends AltosDialog 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                                   String action) {
85
86                 super(in_frame, String.format("Flight list for serial %d", flights.config_data.serial), true);
87                 frame = in_frame;
88
89                 /* Create the container for the dialog */
90                 Container contentPane = getContentPane();
91
92                 /* First, we create a pane containing the dialog's header/title */
93                 JLabel  selectLabel = new JLabel(String.format ("Select flights to %s", action), SwingConstants.CENTER);
94
95                 JPanel  labelPane = new JPanel();
96                 labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.X_AXIS));
97                 labelPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
98                 labelPane.add(Box.createHorizontalGlue());
99                 labelPane.add(selectLabel);
100                 labelPane.add(Box.createHorizontalGlue());
101
102                 /* Add the header to the container. */
103                 contentPane.add(labelPane, BorderLayout.PAGE_START);
104
105
106                 /* Now we create the evilness that is a GridBag for the flight details */
107                 GridBagConstraints c;
108                 Insets i = new Insets(4,4,4,4);
109                 JPanel flightPane = new JPanel();
110                 flightPane.setLayout(new GridBagLayout());
111                 flightPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
112
113                 /* Flight Header */
114                 c = new GridBagConstraints();
115                 c.gridx = 0; c.gridy = 0;
116                 c.fill = GridBagConstraints.NONE;
117                 c.weightx = 0.5;
118                 c.anchor = GridBagConstraints.CENTER;
119                 c.insets = i;
120                 JLabel flightHeaderLabel = new JLabel("Flight");
121                 flightPane.add(flightHeaderLabel, c);
122
123                 /* Download Header */
124                 c = new GridBagConstraints();
125                 c.gridx = 1; c.gridy = 0;
126                 c.fill = GridBagConstraints.NONE;
127                 c.weightx = 0.5;
128                 c.anchor = GridBagConstraints.CENTER;
129                 c.insets = i;
130                 JLabel downloadHeaderLabel = new JLabel(action);
131                 flightPane.add(downloadHeaderLabel, c);
132
133                 /* Add the flights to the GridBag */
134                 AltosEepromItem item;
135                 int itemNumber = 1;
136                 for (AltosEepromLog flight : flights) {
137                         /* Create a flight object with handlers and
138                          * appropriate UI items
139                          */
140                         item = new AltosEepromItem(flight);
141
142                         /* Add a decriptive label for the flight */
143                         c = new GridBagConstraints();
144                         c.gridx = 0; c.gridy = itemNumber;
145                         c.fill = GridBagConstraints.NONE;
146                         c.weightx = 0.5;
147                         c.anchor = GridBagConstraints.CENTER;
148                         c.insets = i;
149                         flightPane.add(item.label, c);
150
151                         /* Add action checkbox for the flight */
152                         c = new GridBagConstraints();
153                         c.gridx = 1; 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.action, c);
159
160                         itemNumber++;
161                 }
162
163                 /* Add the GridBag to the container */
164                 contentPane.add(flightPane, BorderLayout.CENTER);
165
166                 /* Create the dialog buttons */
167                 ok = new JButton("OK");
168                 ok.addActionListener(this);
169                 ok.setActionCommand("ok");
170
171                 cancel = new JButton("Cancel");
172                 cancel.addActionListener(this);
173                 cancel.setActionCommand("cancel");
174
175                 JPanel  buttonPane = new JPanel();
176                 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
177                 buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
178                 buttonPane.add(Box.createHorizontalGlue());
179                 buttonPane.add(cancel);
180                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
181                 buttonPane.add(ok);
182
183                 /* Add the buttons to the container */
184                 contentPane.add(buttonPane, BorderLayout.PAGE_END);
185
186                 /* Pack the window! */
187                 pack();
188         }
189 }