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;
11 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
12 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
15 * Panel which allows the selection of pizzas and the number of groups to create
17 * Features a Create- and Cancel-Button which call the respective methods of the
18 * {@link GroupCreationHandler} set by {@link #setCreateAndCancelHandler(GroupCreationHandler)}
20 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
23 @SuppressWarnings("serial")
24 public class GroupCreationDetailsPanel extends JPanel {
26 private GroupCreationHandler handler;
27 private int numberMembers;
28 private JSpinner numberOfGroupsSpinner;
29 private List<JComboBox> combos = new ArrayList<JComboBox>();
30 private JTextField addressField = new JTextField();
31 private final JComboBox pizzeriaDropdown = new JComboBox();
33 public GroupCreationDetailsPanel(int numberMembers, boolean withAddressField) {
34 this.numberMembers = numberMembers;
36 GridLayout layout = new GridLayout(0, 2);
39 for(int i=1; i<=numberMembers; i++) {
40 JLabel memberLabel = new JLabel(i + ". Person");
41 JComboBox pizzaDropdown = new JComboBox();
42 combos.add(pizzaDropdown);
43 for(PizzaType type : PizzaType.values()) {
44 pizzaDropdown.addItem(type);
46 this.add(memberLabel);
47 this.add(pizzaDropdown);
49 JLabel numberOfGroupsLabel = new JLabel("Number of groups to create: ");
50 SpinnerNumberModel model = new SpinnerNumberModel(1,1,100,1);
51 numberOfGroupsSpinner = new JSpinner(model);
53 this.add(numberOfGroupsLabel);
54 this.add(numberOfGroupsSpinner);
56 final JLabel pizzeriaLabel = new JLabel("Pizzeria: ");
58 final List<String> pizzeriaIdentifiers = GroupAgent.getInstance().getPizzeriaIdentifiers();
59 for (String pizzeriaIdentifier : pizzeriaIdentifiers) {
60 pizzeriaDropdown.addItem(pizzeriaIdentifier);
63 this.add(pizzeriaLabel);
64 this.add(pizzeriaDropdown);
66 if(withAddressField) {
67 final JLabel addressLabel = new JLabel("Address: ");
68 this.add(addressLabel);
69 this.add(addressField);
72 JButton create = new JButton("Create");
73 JButton cancel = new JButton("Cancel");
78 addCreateHandler(create);
79 addCancelHandler(cancel);
83 private void addCancelHandler(JButton cancel) {
84 cancel.addActionListener(new ActionListener() {
87 public void actionPerformed(ActionEvent e) {
88 handler.handleCancel();
93 private void addCreateHandler(JButton create) {
94 create.addActionListener(new ActionListener() {
97 public void actionPerformed(ActionEvent e) {
98 List<PizzaType> types = new ArrayList<>();
99 for(JComboBox combo : combos) {
100 types.add((PizzaType) combo.getSelectedItem());
102 GroupCreationDetailsRequest gcr = new GroupCreationDetailsRequest(numberMembers, (int) numberOfGroupsSpinner.getValue(), types, addressField.getText(), (String) pizzeriaDropdown.getSelectedItem());
103 handler.handleGroupCreation(gcr);
108 public void setCreateAndCancelHandler(
109 GroupCreationHandler groupCreationCallback) {
110 this.handler = groupCreationCallback;