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;
12 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
13 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
16 * Panel which allows the selection of pizzas and the number of groups to create
18 * Features a Create- and Cancel-Button which call the respective methods of the
19 * {@link GroupCreationHandler} set by {@link #setCreateAndCancelHandler(GroupCreationHandler)}
21 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
24 @SuppressWarnings("serial")
25 public class GroupCreationDetailsPanel extends JPanel {
27 private GroupCreationHandler handler;
28 private int numberMembers;
29 private JSpinner numberOfGroupsSpinner;
30 private List<JComboBox> combos = new ArrayList<JComboBox>();
31 private JTextField addressField = new JTextField();
32 private final JComboBox pizzeriaDropdown = new JComboBox();
34 public GroupCreationDetailsPanel(int numberMembers, boolean withAddressField) {
35 this.numberMembers = numberMembers;
37 GridLayout layout = new GridLayout(0, 2);
40 for(int i=1; i<=numberMembers; i++) {
41 JLabel memberLabel = new JLabel(i + ". Person");
42 JComboBox pizzaDropdown = new JComboBox();
43 combos.add(pizzaDropdown);
44 for(PizzaType type : PizzaType.values()) {
45 pizzaDropdown.addItem(type);
47 this.add(memberLabel);
48 this.add(pizzaDropdown);
50 JLabel numberOfGroupsLabel = new JLabel("Number of groups to create: ");
51 SpinnerNumberModel model = new SpinnerNumberModel(1,1,100,1);
52 numberOfGroupsSpinner = new JSpinner(model);
54 this.add(numberOfGroupsLabel);
55 this.add(numberOfGroupsSpinner);
57 final JLabel pizzeriaLabel = new JLabel("Pizzeria: ");
59 final Set<String> pizzeriaIdentifiers = GroupAgent.getInstance().getPizzeriaIdentifiers();
60 for (String pizzeriaIdentifier : pizzeriaIdentifiers) {
61 pizzeriaDropdown.addItem(pizzeriaIdentifier);
64 this.add(pizzeriaLabel);
65 this.add(pizzeriaDropdown);
67 if(withAddressField) {
68 final JLabel addressLabel = new JLabel("Address: ");
69 this.add(addressLabel);
70 this.add(addressField);
73 JButton create = new JButton("Create");
74 JButton cancel = new JButton("Cancel");
79 addCreateHandler(create);
80 addCancelHandler(cancel);
84 private void addCancelHandler(JButton cancel) {
85 cancel.addActionListener(new ActionListener() {
88 public void actionPerformed(ActionEvent e) {
89 handler.handleCancel();
94 private void addCreateHandler(JButton create) {
95 create.addActionListener(new ActionListener() {
98 public void actionPerformed(ActionEvent e) {
99 List<PizzaType> types = new ArrayList<PizzaType>();
100 for(JComboBox combo : combos) {
101 types.add((PizzaType) combo.getSelectedItem());
103 GroupCreationDetailsRequest gcr = new GroupCreationDetailsRequest(numberMembers, (int) numberOfGroupsSpinner.getValue(), types, addressField.getText(), (String) pizzeriaDropdown.getSelectedItem());
104 handler.handleGroupCreation(gcr);
109 public void setCreateAndCancelHandler(
110 GroupCreationHandler groupCreationCallback) {
111 this.handler = groupCreationCallback;