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