1 package at.ac.tuwien.sbc.valesriegler.group.gui;
3 import java.awt.GridLayout;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.util.ArrayList;
9 import javax.swing.JButton;
10 import javax.swing.JComboBox;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13 import javax.swing.JSpinner;
14 import javax.swing.SpinnerNumberModel;
16 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame.GroupCreationHandler;
17 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
20 * Panel which allows the selection of pizzas and the number of groups to create
22 * Features a Create- and Cancel-Button which call the respective methods of the
23 * {@link GroupCreationHandler} set by {@link #setCreateAndCancelHandler(GroupCreationHandler)}
25 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
28 @SuppressWarnings("serial")
29 public class GroupCreationPanel extends JPanel {
31 private GroupCreationHandler handler;
32 private int numberMembers;
33 private JSpinner numberOfGroupsSpinner;
34 private List<JComboBox> combos = new ArrayList<JComboBox>();
36 public GroupCreationPanel(int numberMembers) {
37 this.numberMembers = numberMembers;
39 GridLayout layout = new GridLayout(0, 2);
42 for(int i=1; i<=numberMembers; i++) {
43 JLabel memberLabel = new JLabel(i + ". Person");
44 JComboBox pizzaDropdown = new JComboBox();
45 combos.add(pizzaDropdown);
46 for(PizzaType type : PizzaType.values()) {
47 pizzaDropdown.addItem(type);
49 this.add(memberLabel);
50 this.add(pizzaDropdown);
52 JLabel numberOfGroupsLabel = new JLabel("Number of groups to create: ");
53 SpinnerNumberModel model = new SpinnerNumberModel(1,1,100,1);
54 numberOfGroupsSpinner = new JSpinner(model);
56 this.add(numberOfGroupsLabel);
57 this.add(numberOfGroupsSpinner);
59 JButton create = new JButton("Create");
60 JButton cancel = new JButton("Cancel");
65 addCreateHandler(create);
66 addCancelHandler(cancel);
70 private void addCancelHandler(JButton cancel) {
71 cancel.addActionListener(new ActionListener() {
74 public void actionPerformed(ActionEvent e) {
75 handler.handleCancel();
80 private void addCreateHandler(JButton create) {
81 create.addActionListener(new ActionListener() {
84 public void actionPerformed(ActionEvent e) {
85 List<PizzaType> types = new ArrayList<>();
86 for(JComboBox combo : combos) {
87 types.add((PizzaType) combo.getSelectedItem());
89 GroupCreationRequest gcr = new GroupCreationRequest(numberMembers, (int) numberOfGroupsSpinner.getValue(), types);
90 handler.handleGroupCreation(gcr);
95 public void setCreateAndCancelHandler(
96 GroupCreationHandler groupCreationCallback) {
97 this.handler = groupCreationCallback;