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;
26 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
27 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
30 * Base Frame of the Group UI
32 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
35 @SuppressWarnings("serial")
36 public class GroupFrame extends JFrame {
37 public GroupFrame(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, groupModel);
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
62 // Wizard should be shown
63 next.addActionListener(new ActionListener() {
65 public void actionPerformed(ActionEvent e) {
66 int numberMembers = (int) spinner.getValue();
67 final GroupCreationPanel groupCreationPanel = new GroupCreationPanel(numberMembers);
68 GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel,
71 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
72 groupCreationHandler.showGroupCreationDetailPanel();
77 GridLayout creationPanelLayout = new GridLayout(3, 1);
78 chooseGroupSizePanel.setLayout(creationPanelLayout);
79 creationPanel.setBorder(new TitledBorder("Create Groups"));
81 creationPanel.add(chooseGroupSizePanel);
82 chooseGroupSizePanel.add(creationLabel);
83 chooseGroupSizePanel.add(spinner);
84 chooseGroupSizePanel.add(next);
86 wrapper.add(creationPanel);
89 private void initGroupOverview(JPanel wrapper, GroupOverviewModel groupModel) {
90 JPanel overviewPanel = new JPanel(new GridBagLayout());
92 JTable overviewTable = new JTable(groupModel);
93 JScrollPane scrollPane = new JScrollPane(overviewTable);
95 overviewPanel.setBorder(new TitledBorder("Group Overview"));
96 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.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);
109 * Handles the Wizard-like creation of groups.
111 * Give an instance of this class as a callback to the Panel from which a
112 * group creation request should be issued
114 class GroupCreationHandler {
115 private JPanel creationPanel;
116 private JPanel chooseGroupSizePanel;
117 private JPanel groupCreationPanel;
119 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
120 this.creationPanel = creationPanel;
121 this.chooseGroupSizePanel = chooseGroupSizePanel;
122 this.groupCreationPanel = groupCreationPanel;
125 public void showGroupCreationDetailPanel() {
126 chooseGroupSizePanel.setVisible(false);
127 creationPanel.add(groupCreationPanel);
130 public void handleGroupCreation(GroupCreationRequest gc) {
131 chooseGroupSizePanel.setVisible(true);
132 creationPanel.remove(groupCreationPanel);
133 creationPanel.repaint();
138 public void createGroups(GroupCreationRequest gc) {
139 List<Group> newGroups = new ArrayList<>();
140 for (int i = 0; i < gc.numberOfGroups; i++) {
141 Group group = new Group();
142 group.getGroupData().setSize(gc.size);
143 List<PizzaOrder> pizzaOrders = new ArrayList<>();
144 for (PizzaType pt : gc.pizzaTypes) {
145 pizzaOrders.add(new PizzaOrder(pt));
147 Order order = new Order(group, pizzaOrders);
148 group.getGroupData().setOrder(order);
150 newGroups.add(group);
152 GroupAgent.getInstance().getGroupModel().addItems(newGroups);
155 public void handleCancel() {
156 chooseGroupSizePanel.setVisible(true);
157 creationPanel.remove(groupCreationPanel);
158 creationPanel.repaint();