]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationPanel.java
Pizzeria GUI implemented
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / GroupCreationPanel.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.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;
15
16 import at.ac.tuwien.sbc.valesriegler.common.PizzaType;
17 import at.ac.tuwien.sbc.valesriegler.group.gui.GroupFrame.GroupCreationHandler;
18
19 /**
20  * Panel which allows the selection of pizzas and the number of groups to create
21  * <p />
22  * Features a Create- and Cancel-Button which call the respective methods of the 
23  * {@link GroupCreationHandler} set by {@link #setCreateAndCancelHandler(GroupCreationHandler)}
24  * 
25  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
26  *
27  */
28 @SuppressWarnings("serial")
29 public class GroupCreationPanel extends JPanel {
30         
31         private GroupCreationHandler handler;
32         private int numberMembers;
33         private JSpinner numberOfGroupsSpinner;
34         private List<JComboBox> combos = new ArrayList<JComboBox>();
35         
36         public GroupCreationPanel(int numberMembers) {
37                 this.numberMembers = numberMembers;
38                 
39                 GridLayout layout = new GridLayout(0, 2);
40                 setLayout(layout);
41                 
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);
48                         }
49                         this.add(memberLabel);
50                         this.add(pizzaDropdown);
51                 }
52                 JLabel numberOfGroupsLabel = new JLabel("Number of groups to create: ");
53                 SpinnerNumberModel model = new SpinnerNumberModel(1,1,100,1);
54             numberOfGroupsSpinner = new JSpinner(model);
55             
56             this.add(numberOfGroupsLabel);
57             this.add(numberOfGroupsSpinner);
58             
59             JButton create = new JButton("Create");
60             JButton cancel = new JButton("Cancel");
61             
62             this.add(create);
63             this.add(cancel);
64             
65             addCreateHandler(create);
66             addCancelHandler(cancel);
67         }
68
69
70         private void addCancelHandler(JButton cancel) {
71                 cancel.addActionListener(new ActionListener() {
72                         
73                         @Override
74                         public void actionPerformed(ActionEvent e) {
75                                 handler.handleCancel();
76                         }
77                 });
78         }
79
80         private void addCreateHandler(JButton create) {
81                 create.addActionListener(new ActionListener() {
82                         
83                         @Override
84                         public void actionPerformed(ActionEvent e) {
85                                 List<PizzaType> types = new ArrayList<>();
86                                 for(JComboBox combo : combos) {
87                                         types.add((PizzaType) combo.getSelectedItem());
88                                 }
89                                 GroupCreationRequest gcr = new GroupCreationRequest(numberMembers, (int) numberOfGroupsSpinner.getValue(), types);
90                                 handler.handleGroupCreation(gcr);
91                         }
92                 });
93         }
94
95         public void setCreateAndCancelHandler(
96                         GroupCreationHandler groupCreationCallback) {
97                 this.handler = groupCreationCallback;
98         }
99
100 }