1 package at.ac.tuwien.sbc.valesriegler.group.gui;
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.GridLayout;
6 import java.awt.Insets;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.concurrent.atomic.AtomicInteger;
13 import javax.swing.JButton;
14 import javax.swing.JFrame;
15 import javax.swing.JLabel;
16 import javax.swing.JOptionPane;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JSpinner;
20 import javax.swing.JTable;
21 import javax.swing.SpinnerNumberModel;
22 import javax.swing.border.TitledBorder;
24 import at.ac.tuwien.sbc.valesriegler.group.Group;
27 * Base Frame of the Group UI
29 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
32 public class GroupFrame extends JFrame {
33 private int idCounter = 1;
35 private GroupOverviewModel groupModel;
39 JPanel wrapper = new JPanel();
40 GridLayout wrapperLayout = new GridLayout(2, 1);
41 wrapper.setLayout(wrapperLayout);
43 // Create Wizard-like panel for the creation of groups
44 initGroupCreation(wrapper);
46 // Create the panel for the group overview table
47 initGroupOverview(wrapper);
49 setContentPane(wrapper);
50 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
53 private void initGroupCreation(JPanel wrapper) {
54 final JPanel creationPanel = new JPanel();
55 final JPanel chooseGroupSizePanel = new JPanel();
56 JLabel creationLabel = new JLabel("How many members should the group have?");
57 JButton next = new JButton("Next");
58 SpinnerNumberModel model = new SpinnerNumberModel(1,1,4,1);
59 final JSpinner spinner = new JSpinner(model);
61 // When 'next' is clicked the second, final panel of the Group Creation Wizard should be shown
62 next.addActionListener(new ActionListener() {
64 public void actionPerformed(ActionEvent e) {
65 int numberMembers = (int) spinner.getValue();
66 final GroupCreationPanel groupCreationPanel = new GroupCreationPanel(numberMembers);
67 GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel, groupCreationPanel);
69 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
70 groupCreationHandler.showGroupCreationDetailPanel();
76 GridLayout creationPanelLayout = new GridLayout(3,1);
77 chooseGroupSizePanel.setLayout(creationPanelLayout);
78 creationPanel.setBorder(new TitledBorder("Create Groups"));
80 creationPanel.add(chooseGroupSizePanel);
81 chooseGroupSizePanel.add(creationLabel);
82 chooseGroupSizePanel.add(spinner);
83 chooseGroupSizePanel.add(next);
85 wrapper.add(creationPanel);
88 private void initGroupOverview(JPanel wrapper) {
89 JPanel overviewPanel = new JPanel(new GridBagLayout());
91 groupModel = new GroupOverviewModel();
92 JTable overviewTable = new JTable(groupModel);
93 JScrollPane scrollPane = new JScrollPane(overviewTable);
95 overviewPanel.setBorder(new TitledBorder("Group Overview"));
96 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
98 GridBagConstraints c = new GridBagConstraints();
102 c.insets = new Insets(10, 10, 10, 10);
103 c.fill = GridBagConstraints.BOTH;
104 overviewPanel.add(scrollPane, c);
105 wrapper.add(overviewPanel);
110 * Handles the Wizard-like creation of groups.
112 * Give an instance of this class as a callback to the Panel from which
113 * a group creation request should be issued
115 class GroupCreationHandler {
116 private JPanel creationPanel;
117 private JPanel chooseGroupSizePanel;
118 private JPanel groupCreationPanel;
120 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
121 this.creationPanel = creationPanel;
122 this.chooseGroupSizePanel = chooseGroupSizePanel;
123 this.groupCreationPanel = groupCreationPanel;
126 public void showGroupCreationDetailPanel() {
127 chooseGroupSizePanel.setVisible(false);
128 creationPanel.add(groupCreationPanel);
131 public void handleGroupCreation(GroupCreationRequest gc) {
132 chooseGroupSizePanel.setVisible(true);
133 creationPanel.remove(groupCreationPanel);
134 creationPanel.repaint();
139 private void createGroups(GroupCreationRequest gc) {
140 List<Group> newGroups = new ArrayList<>();
141 for(int i=0; i<gc.numberOfGroups; i++) {
142 Group group = new Group();
143 group.setId(idCounter);
144 group.setSize(gc.size);
145 group.setPizzas(gc.pizzaTypes);
146 idCounter = idCounter + 1;
148 newGroups.add(group);
150 groupModel.addItems(newGroups);
153 public void handleCancel() {
154 chooseGroupSizePanel.setVisible(true);
155 creationPanel.remove(groupCreationPanel);
156 creationPanel.repaint();