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;
25 import at.ac.tuwien.sbc.valesriegler.types.Order;
28 * Base Frame of the Group UI
30 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
33 @SuppressWarnings("serial")
34 public class GroupFrame extends JFrame {
35 public GroupFrame(GroupOverviewModel groupModel) {
37 JPanel wrapper = new JPanel();
38 GridLayout wrapperLayout = new GridLayout(2, 1);
39 wrapper.setLayout(wrapperLayout);
41 // Create Wizard-like panel for the creation of groups
42 initGroupCreation(wrapper);
44 // Create the panel for the group overview table
45 initGroupOverview(wrapper, groupModel);
47 setContentPane(wrapper);
48 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51 private void initGroupCreation(JPanel wrapper) {
52 final JPanel creationPanel = new JPanel();
53 final JPanel chooseGroupSizePanel = new JPanel();
54 JLabel creationLabel = new JLabel("How many members should the group have?");
55 JButton next = new JButton("Next");
56 SpinnerNumberModel model = new SpinnerNumberModel(1, 1, 4, 1);
57 final JSpinner spinner = new JSpinner(model);
59 // When 'next' is clicked the second, final panel of the Group Creation
60 // Wizard should be shown
61 next.addActionListener(new ActionListener() {
63 public void actionPerformed(ActionEvent e) {
64 int numberMembers = (int) spinner.getValue();
65 final GroupCreationPanel groupCreationPanel = new GroupCreationPanel(numberMembers);
66 GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel,
69 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
70 groupCreationHandler.showGroupCreationDetailPanel();
75 GridLayout creationPanelLayout = new GridLayout(3, 1);
76 chooseGroupSizePanel.setLayout(creationPanelLayout);
77 creationPanel.setBorder(new TitledBorder("Create Groups"));
79 creationPanel.add(chooseGroupSizePanel);
80 chooseGroupSizePanel.add(creationLabel);
81 chooseGroupSizePanel.add(spinner);
82 chooseGroupSizePanel.add(next);
84 wrapper.add(creationPanel);
87 private void initGroupOverview(JPanel wrapper, GroupOverviewModel groupModel) {
88 JPanel overviewPanel = new JPanel(new GridBagLayout());
90 JTable overviewTable = new JTable(groupModel);
91 JScrollPane scrollPane = new JScrollPane(overviewTable);
93 overviewPanel.setBorder(new TitledBorder("Group Overview"));
94 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
96 GridBagConstraints c = new GridBagConstraints();
100 c.insets = new Insets(10, 10, 10, 10);
101 c.fill = GridBagConstraints.BOTH;
102 overviewPanel.add(scrollPane, c);
103 wrapper.add(overviewPanel);
107 * Handles the Wizard-like creation of groups.
109 * Give an instance of this class as a callback to the Panel from which a
110 * group creation request should be issued
112 class GroupCreationHandler {
113 private JPanel creationPanel;
114 private JPanel chooseGroupSizePanel;
115 private JPanel groupCreationPanel;
117 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
118 this.creationPanel = creationPanel;
119 this.chooseGroupSizePanel = chooseGroupSizePanel;
120 this.groupCreationPanel = groupCreationPanel;
123 public void showGroupCreationDetailPanel() {
124 chooseGroupSizePanel.setVisible(false);
125 creationPanel.add(groupCreationPanel);
128 public void handleGroupCreation(GroupCreationRequest gc) {
129 chooseGroupSizePanel.setVisible(true);
130 creationPanel.remove(groupCreationPanel);
131 creationPanel.repaint();
136 public void createGroups(GroupCreationRequest gc) {
137 List<Group> newGroups = new ArrayList<>();
138 for (int i = 0; i < gc.numberOfGroups; i++) {
139 Group group = new Group();
140 group.getGroupData().setSize(gc.size);
141 Order order = new Order(group, gc.pizzaTypes);
142 group.getGroupData().setOrder(order);
144 newGroups.add(group);
146 GroupAgent.getInstance().getGroupModel().addItems(newGroups);
149 public void handleCancel() {
150 chooseGroupSizePanel.setVisible(true);
151 creationPanel.remove(groupCreationPanel);
152 creationPanel.repaint();