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