altos/drivers: Use stdbool in ao_ms5607.c
[fw/altos] / altosuilib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_13;
20
21 import javax.swing.*;
22 import javax.swing.border.*;
23 import java.awt.*;
24 import java.awt.event.*;
25 import org.altusmetrum.altoslib_13.*;
26
27 class AltosEepromItem implements ActionListener {
28         AltosEepromLog  log;
29         JLabel          label;
30         JCheckBox       download;
31         JCheckBox       delete;
32         JCheckBox       graph;
33
34         public void actionPerformed(ActionEvent e) {
35                 log.download_selected = download.isSelected();
36                 log.delete_selected = delete.isSelected();
37                 log.graph_selected = graph.isSelected();
38         }
39
40         public AltosEepromItem(AltosEepromLog in_log) {
41                 log = in_log;
42
43                 String  text;
44                 if (log.flight >= 0)
45                         text = String.format("Flight #%02d", log.flight);
46                 else
47                         text = String.format("Corrupt #%02d", -log.flight);
48
49                 label = new JLabel(text);
50
51                 download = new JCheckBox("", log.download_selected);
52                 download.addActionListener(this);
53
54                 delete = new JCheckBox("", log.delete_selected);
55                 delete.addActionListener(this);
56
57                 graph = new JCheckBox("", log.graph_selected);
58                 graph.addActionListener(this);
59         }
60 }
61
62 public class AltosEepromSelect extends AltosUIDialog implements ActionListener {
63         //private JList                 list;
64         private JFrame                  frame;
65         JButton                         ok;
66         JButton                         cancel;
67         boolean                         success;
68
69         /* Listen for events from our buttons */
70         public void actionPerformed(ActionEvent e) {
71                 String  cmd = e.getActionCommand();
72
73                 if (cmd.equals("ok"))
74                         success = true;
75                 setVisible(false);
76         }
77
78         public boolean run() {
79                 success = false;
80                 setLocationRelativeTo(frame);
81                 setVisible(true);
82                 return success;
83         }
84
85         public AltosEepromSelect (JFrame in_frame,
86                                   AltosEepromList flights,
87                                   boolean has_graph) {
88
89                 super(in_frame, String.format("Flight list for serial %d", flights.config_data.serial), true);
90                 frame = in_frame;
91
92                 /* Create the container for the dialog */
93                 Container contentPane = getContentPane();
94
95                 /* First, we create a pane containing the dialog's header/title */
96                 JLabel  selectLabel = new JLabel(String.format ("Select flights"), SwingConstants.CENTER);
97
98                 JPanel  labelPane = new JPanel();
99                 labelPane.setLayout(new BoxLayout(labelPane, BoxLayout.X_AXIS));
100                 labelPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
101                 labelPane.add(Box.createHorizontalGlue());
102                 labelPane.add(selectLabel);
103                 labelPane.add(Box.createHorizontalGlue());
104
105                 /* Add the header to the container. */
106                 contentPane.add(labelPane, BorderLayout.PAGE_START);
107
108
109                 /* Now we create the evilness that is a GridBag for the flight details */
110                 GridBagConstraints c;
111                 Insets i = new Insets(4,4,4,4);
112                 JPanel flightPane = new JPanel();
113                 flightPane.setLayout(new GridBagLayout());
114                 flightPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
115
116                 /* Flight Header */
117                 c = new GridBagConstraints();
118                 c.gridx = 0; c.gridy = 0;
119                 c.fill = GridBagConstraints.NONE;
120                 c.weightx = 0.5;
121                 c.anchor = GridBagConstraints.CENTER;
122                 c.insets = i;
123                 JLabel flightHeaderLabel = new JLabel("Flight");
124                 flightPane.add(flightHeaderLabel, c);
125
126                 /* Download Header */
127                 c = new GridBagConstraints();
128                 c.gridx = 1; c.gridy = 0;
129                 c.fill = GridBagConstraints.NONE;
130                 c.weightx = 0.5;
131                 c.anchor = GridBagConstraints.CENTER;
132                 c.insets = i;
133                 JLabel downloadHeaderLabel = new JLabel("Download");
134                 flightPane.add(downloadHeaderLabel, c);
135
136                 /* Delete Header */
137                 c = new GridBagConstraints();
138                 c.gridx = 2; c.gridy = 0;
139                 c.fill = GridBagConstraints.NONE;
140                 c.weightx = 0.5;
141                 c.anchor = GridBagConstraints.CENTER;
142                 c.insets = i;
143                 JLabel deleteHeaderLabel = new JLabel("Delete");
144                 flightPane.add(deleteHeaderLabel, c);
145
146                 if (has_graph) {
147                         /* Graph Header */
148                         c = new GridBagConstraints();
149                         c.gridx = 3; c.gridy = 0;
150                         c.fill = GridBagConstraints.NONE;
151                         c.weightx = 0.5;
152                         c.anchor = GridBagConstraints.CENTER;
153                         c.insets = i;
154                         JLabel graphHeaderLabel = new JLabel("Graph");
155                         flightPane.add(graphHeaderLabel, c);
156                 }
157
158                 /* Add the flights to the GridBag */
159                 AltosEepromItem item;
160                 int itemNumber = 1;
161                 for (AltosEepromLog flight : flights) {
162                         /* Create a flight object with handlers and
163                          * appropriate UI items
164                          */
165                         item = new AltosEepromItem(flight);
166
167                         /* Add a decriptive label for the flight */
168                         c = new GridBagConstraints();
169                         c.gridx = 0; 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.label, c);
175
176                         /* Add download checkbox for the flight */
177                         c = new GridBagConstraints();
178                         c.gridx = 1; 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.download, c);
184
185                         /* Add delete checkbox for the flight */
186                         c = new GridBagConstraints();
187                         c.gridx = 2; c.gridy = itemNumber;
188                         c.fill = GridBagConstraints.NONE;
189                         c.weightx = 0.5;
190                         c.anchor = GridBagConstraints.CENTER;
191                         c.insets = i;
192                         flightPane.add(item.delete, c);
193
194                         if (has_graph) {
195                                 /* Add graph checkbox for the flight */
196                                 c = new GridBagConstraints();
197                                 c.gridx = 3; c.gridy = itemNumber;
198                                 c.fill = GridBagConstraints.NONE;
199                                 c.weightx = 0.5;
200                                 c.anchor = GridBagConstraints.CENTER;
201                                 c.insets = i;
202                                 flightPane.add(item.graph, c);
203                         }
204
205                         itemNumber++;
206                 }
207
208                 /* Add the GridBag to the container */
209                 contentPane.add(flightPane, BorderLayout.CENTER);
210
211                 /* Create the dialog buttons */
212                 ok = new JButton("OK");
213                 ok.addActionListener(this);
214                 ok.setActionCommand("ok");
215
216                 cancel = new JButton("Cancel");
217                 cancel.addActionListener(this);
218                 cancel.setActionCommand("cancel");
219
220                 JPanel  buttonPane = new JPanel();
221                 buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
222                 buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
223                 buttonPane.add(Box.createHorizontalGlue());
224                 buttonPane.add(cancel);
225                 buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
226                 buttonPane.add(ok);
227
228                 /* Add the buttons to the container */
229                 contentPane.add(buttonPane, BorderLayout.PAGE_END);
230
231                 /* Pack the window! */
232                 pack();
233         }
234 }