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