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;
8 import java.util.concurrent.atomic.AtomicInteger;
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;
21 import at.ac.tuwien.sbc.valesriegler.group.Group;
24 * Base Frame of the Group UI
26 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
29 public class GroupFrame extends JFrame {
30 private int idCounter = 1;
32 private GroupOverviewModel groupModel;
36 JPanel wrapper = new JPanel();
37 GridLayout wrapperLayout = new GridLayout(2, 1);
38 wrapper.setLayout(wrapperLayout);
40 // Create Wizard-like panel for the creation of groups
41 initGroupCreation(wrapper);
43 // Create the panel for the group overview table
44 initGroupOverview(wrapper);
46 setContentPane(wrapper);
47 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
58 // When 'next' is clicked the second, final panel of the Group Creation Wizard should be shown
59 next.addActionListener(new ActionListener() {
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);
66 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
67 groupCreationHandler.showGroupCreationDetailPanel();
73 GridLayout creationPanelLayout = new GridLayout(3,1);
74 chooseGroupSizePanel.setLayout(creationPanelLayout);
75 creationPanel.setBorder(new TitledBorder("Create Groups"));
77 creationPanel.add(chooseGroupSizePanel);
78 chooseGroupSizePanel.add(creationLabel);
79 chooseGroupSizePanel.add(spinner);
80 chooseGroupSizePanel.add(next);
82 wrapper.add(creationPanel);
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);
91 overviewPanel.setBorder(new TitledBorder("Group Overview"));
92 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
94 overviewPanel.add(scrollPane);
95 wrapper.add(overviewPanel);
100 * Handles the Wizard-like creation of groups.
102 * Give an instance of this class as a callback to the Panel from which
103 * a group creation request should be issued
105 class GroupCreationHandler {
106 private JPanel creationPanel;
107 private JPanel chooseGroupSizePanel;
108 private JPanel groupCreationPanel;
110 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
111 this.creationPanel = creationPanel;
112 this.chooseGroupSizePanel = chooseGroupSizePanel;
113 this.groupCreationPanel = groupCreationPanel;
116 public void showGroupCreationDetailPanel() {
117 chooseGroupSizePanel.setVisible(false);
118 creationPanel.add(groupCreationPanel);
121 public void handleGroupCreation(GroupCreationRequest gc) {
122 chooseGroupSizePanel.setVisible(true);
123 creationPanel.remove(groupCreationPanel);
124 creationPanel.repaint();
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;
138 newGroups.add(group);
140 groupModel.addItems(newGroups);
143 public void handleCancel() {
144 chooseGroupSizePanel.setVisible(true);
145 creationPanel.remove(groupCreationPanel);
146 creationPanel.repaint();