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;
12 import javax.swing.JButton;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JPanel;
16 import javax.swing.JScrollPane;
17 import javax.swing.JSpinner;
18 import javax.swing.JTable;
19 import javax.swing.ScrollPaneConstants;
20 import javax.swing.SpinnerNumberModel;
21 import javax.swing.border.TitledBorder;
23 import at.ac.tuwien.sbc.valesriegler.group.Group;
24 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
27 * Base Frame of the Group UI
29 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
32 @SuppressWarnings("serial")
33 public class GroupFrame extends JFrame {
34 public GroupFrame(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, groupModel);
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
59 // Wizard should be shown
60 next.addActionListener(new ActionListener() {
62 public void actionPerformed(ActionEvent e) {
63 int numberMembers = (int) spinner.getValue();
64 final GroupCreationPanel groupCreationPanel = new GroupCreationPanel(numberMembers);
65 GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel,
68 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
69 groupCreationHandler.showGroupCreationDetailPanel();
74 GridLayout creationPanelLayout = new GridLayout(3, 1);
75 chooseGroupSizePanel.setLayout(creationPanelLayout);
76 creationPanel.setBorder(new TitledBorder("Create Groups"));
78 creationPanel.add(chooseGroupSizePanel);
79 chooseGroupSizePanel.add(creationLabel);
80 chooseGroupSizePanel.add(spinner);
81 chooseGroupSizePanel.add(next);
83 wrapper.add(creationPanel);
86 private void initGroupOverview(JPanel wrapper, GroupOverviewModel groupModel) {
87 JPanel overviewPanel = new JPanel(new GridBagLayout());
89 JTable overviewTable = new JTable(groupModel);
90 JScrollPane scrollPane = new JScrollPane(overviewTable);
92 overviewPanel.setBorder(new TitledBorder("Group Overview"));
93 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
95 GridBagConstraints c = new GridBagConstraints();
99 c.insets = new Insets(10, 10, 10, 10);
100 c.fill = GridBagConstraints.BOTH;
101 overviewPanel.add(scrollPane, c);
102 wrapper.add(overviewPanel);
106 * Handles the Wizard-like creation of groups.
108 * Give an instance of this class as a callback to the Panel from which a
109 * group creation request should be issued
111 class GroupCreationHandler {
112 private JPanel creationPanel;
113 private JPanel chooseGroupSizePanel;
114 private JPanel groupCreationPanel;
116 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
117 this.creationPanel = creationPanel;
118 this.chooseGroupSizePanel = chooseGroupSizePanel;
119 this.groupCreationPanel = groupCreationPanel;
122 public void showGroupCreationDetailPanel() {
123 chooseGroupSizePanel.setVisible(false);
124 creationPanel.add(groupCreationPanel);
127 public void handleGroupCreation(GroupCreationRequest gc) {
128 chooseGroupSizePanel.setVisible(true);
129 creationPanel.remove(groupCreationPanel);
130 creationPanel.repaint();
135 public void createGroups(GroupCreationRequest gc) {
136 List<Group> newGroups = new ArrayList<>();
137 for (int i = 0; i < gc.numberOfGroups; i++) {
138 Group group = new Group();
139 group.getGroupData().setSize(gc.size);
140 group.getGroupData().setPizzas(gc.pizzaTypes);
142 newGroups.add(group);
144 GroupAgent.getInstance().getGroupModel().addItems(newGroups);
147 public void handleCancel() {
148 chooseGroupSizePanel.setVisible(true);
149 creationPanel.remove(groupCreationPanel);
150 creationPanel.repaint();