]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationDetailsPanel.java
[XVSM] Waiters, Cooks and Pizzerias can be parameterized with a Space port. Group...
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / GroupCreationDetailsPanel.java
1 package at.ac.tuwien.sbc.valesriegler.group.gui;
2
3 import java.awt.GridLayout;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import javax.swing.*;
10
11 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
12 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
13
14 /**
15  * Panel which allows the selection of pizzas and the number of groups to create
16  * <p />
17  * Features a Create- and Cancel-Button which call the respective methods of the 
18  * {@link GroupCreationHandler} set by {@link #setCreateAndCancelHandler(GroupCreationHandler)}
19  * 
20  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
21  *
22  */
23 @SuppressWarnings("serial")
24 public class GroupCreationDetailsPanel extends JPanel {
25         
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();
32
33     public GroupCreationDetailsPanel(int numberMembers, boolean withAddressField) {
34                 this.numberMembers = numberMembers;
35                 
36                 GridLayout layout = new GridLayout(0, 2);
37                 setLayout(layout);
38                 
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);
45                         }
46                         this.add(memberLabel);
47                         this.add(pizzaDropdown);
48                 }
49                 JLabel numberOfGroupsLabel = new JLabel("Number of groups to create: ");
50                 SpinnerNumberModel model = new SpinnerNumberModel(1,1,100,1);
51             numberOfGroupsSpinner = new JSpinner(model);
52             
53             this.add(numberOfGroupsLabel);
54             this.add(numberOfGroupsSpinner);
55
56         final JLabel pizzeriaLabel = new JLabel("Pizzeria: ");
57
58         final List<String> pizzeriaIdentifiers = GroupAgent.getInstance().getPizzeriaIdentifiers();
59         for (String pizzeriaIdentifier : pizzeriaIdentifiers) {
60             pizzeriaDropdown.addItem(pizzeriaIdentifier);
61         }
62
63         this.add(pizzeriaLabel);
64         this.add(pizzeriaDropdown);
65
66         if(withAddressField) {
67             final JLabel addressLabel = new JLabel("Address: ");
68             this.add(addressLabel);
69             this.add(addressField);
70         }
71
72         JButton create = new JButton("Create");
73             JButton cancel = new JButton("Cancel");
74             
75             this.add(create);
76             this.add(cancel);
77
78             addCreateHandler(create);
79             addCancelHandler(cancel);
80         }
81
82
83         private void addCancelHandler(JButton cancel) {
84                 cancel.addActionListener(new ActionListener() {
85                         
86                         @Override
87                         public void actionPerformed(ActionEvent e) {
88                                 handler.handleCancel();
89                         }
90                 });
91         }
92
93         private void addCreateHandler(JButton create) {
94                 create.addActionListener(new ActionListener() {
95                         
96                         @Override
97                         public void actionPerformed(ActionEvent e) {
98                                 List<PizzaType> types = new ArrayList<>();
99                                 for(JComboBox combo : combos) {
100                                         types.add((PizzaType) combo.getSelectedItem());
101                                 }
102                                 GroupCreationDetailsRequest gcr = new GroupCreationDetailsRequest(numberMembers, (int) numberOfGroupsSpinner.getValue(), types, addressField.getText(), (String) pizzeriaDropdown.getSelectedItem());
103                                 handler.handleGroupCreation(gcr);
104                         }
105                 });
106         }
107
108         public void setCreateAndCancelHandler(
109                         GroupCreationHandler groupCreationCallback) {
110                 this.handler = groupCreationCallback;
111         }
112
113 }