]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java
Ask for number of tables on start of the Pizzeria GUI
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / GroupFrame.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 import java.util.concurrent.atomic.AtomicInteger;
9
10 import javax.swing.JButton;
11 import javax.swing.JFrame;
12 import javax.swing.JLabel;
13 import javax.swing.JOptionPane;
14 import javax.swing.JPanel;
15 import javax.swing.JScrollPane;
16 import javax.swing.JSpinner;
17 import javax.swing.JTable;
18 import javax.swing.SpinnerNumberModel;
19 import javax.swing.border.TitledBorder;
20
21 import at.ac.tuwien.sbc.valesriegler.group.Group;
22
23 /**
24  * Base Frame of the Group UI
25  * 
26  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
27  *
28  */
29 public class GroupFrame extends JFrame {
30         private int idCounter = 1;
31         
32         private GroupOverviewModel groupModel;
33
34         public GroupFrame() {
35                 super("Groups");
36                 JPanel wrapper = new JPanel();
37                 GridLayout wrapperLayout = new GridLayout(2, 1);
38                 wrapper.setLayout(wrapperLayout);
39                 
40                 // Create Wizard-like panel for the creation of groups
41                 initGroupCreation(wrapper);
42                 
43                 // Create the panel for the group overview table
44                 initGroupOverview(wrapper);
45                 
46                 setContentPane(wrapper);
47                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48         }
49
50         private void initGroupCreation(JPanel wrapper) {
51                 final JPanel creationPanel = new JPanel();
52                 final JPanel chooseGroupSizePanel = new JPanel();
53                 JLabel creationLabel = new JLabel("How many members should the group have?");
54                 JButton next = new JButton("Next");
55                 SpinnerNumberModel model = new SpinnerNumberModel(1,1,4,1);
56             final JSpinner spinner = new JSpinner(model);
57
58             // When 'next' is clicked the second, final panel of the Group Creation Wizard should be shown
59             next.addActionListener(new ActionListener() {
60                         @Override
61                         public void actionPerformed(ActionEvent e) {
62                                 int numberMembers = (int) spinner.getValue();
63                                 final GroupCreationPanel groupCreationPanel = new GroupCreationPanel(numberMembers);
64                                 GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel, groupCreationPanel);
65                                 
66                                 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
67                                 groupCreationHandler.showGroupCreationDetailPanel();
68                                 
69                         
70                         }
71                 });
72         
73             GridLayout creationPanelLayout = new GridLayout(3,1);
74             chooseGroupSizePanel.setLayout(creationPanelLayout);
75             creationPanel.setBorder(new TitledBorder("Create Groups"));
76             
77             creationPanel.add(chooseGroupSizePanel);
78                 chooseGroupSizePanel.add(creationLabel);
79                 chooseGroupSizePanel.add(spinner);
80                 chooseGroupSizePanel.add(next);
81                 
82                 wrapper.add(creationPanel);
83         }
84
85         private void initGroupOverview(JPanel wrapper) {
86                 JPanel overviewPanel = new JPanel();
87                 groupModel = new GroupOverviewModel();
88                 JTable overviewTable = new JTable(groupModel);
89                 JScrollPane scrollPane = new JScrollPane(overviewTable);
90                 
91                 overviewPanel.setBorder(new TitledBorder("Group Overview"));
92                 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
93                 
94                 overviewPanel.add(scrollPane);
95                 wrapper.add(overviewPanel);
96         }
97         
98          
99         /** 
100          * Handles the Wizard-like creation of groups.
101          * <p />
102          * Give an instance of this class as a callback to the Panel from which 
103          * a group creation request should be issued
104          */
105         class GroupCreationHandler {
106                 private JPanel creationPanel;
107                 private JPanel chooseGroupSizePanel;
108                 private JPanel groupCreationPanel;
109
110                 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
111                         this.creationPanel = creationPanel;
112                         this.chooseGroupSizePanel = chooseGroupSizePanel;
113                         this.groupCreationPanel = groupCreationPanel;
114                 }
115                 
116                 public void showGroupCreationDetailPanel() {
117                         chooseGroupSizePanel.setVisible(false);
118                         creationPanel.add(groupCreationPanel);
119                 }
120
121                 public void handleGroupCreation(GroupCreationRequest gc) {
122                         chooseGroupSizePanel.setVisible(true);
123                         creationPanel.remove(groupCreationPanel);
124                         creationPanel.repaint();
125                         
126                         createGroups(gc);
127                 }
128                 
129                 private void createGroups(GroupCreationRequest gc) {
130                         List<Group> newGroups = new ArrayList<>();
131                         for(int i=0; i<gc.numberOfGroups; i++) {
132                                 Group group = new Group();
133                                 group.setId(idCounter);
134                                 group.setSize(gc.size);
135                                 group.setPizzas(gc.pizzaTypes);
136                                 idCounter = idCounter + 1;
137                                 
138                                 newGroups.add(group);
139                         }
140                         groupModel.addItems(newGroups);
141                 }
142
143                 public void handleCancel() {
144                         chooseGroupSizePanel.setVisible(true);
145                         creationPanel.remove(groupCreationPanel);
146                         creationPanel.repaint();
147                 }
148         }
149
150 }