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 org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import at.ac.tuwien.sbc.valesriegler.group.Group;
27 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
28 import at.ac.tuwien.sbc.valesriegler.pizzeria.gui.PizzeriaFrame;
29 import at.ac.tuwien.sbc.valesriegler.types.Order;
30 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
31 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
34 * Base Frame of the Group UI
36 * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
39 @SuppressWarnings("serial")
40 public class GroupFrame extends JFrame {
41 private static final Logger log = LoggerFactory.getLogger(GroupFrame.class);
43 public GroupFrame(GroupOverviewModel groupModel) {
45 JPanel wrapper = new JPanel();
46 GridLayout wrapperLayout = new GridLayout(2, 1);
47 wrapper.setLayout(wrapperLayout);
49 // Create Wizard-like panel for the creation of groups
50 initGroupCreation(wrapper);
52 // Create the panel for the group overview table
53 initGroupOverview(wrapper, groupModel);
55 setContentPane(wrapper);
56 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
59 private void initGroupCreation(JPanel wrapper) {
60 final JPanel creationPanel = new JPanel();
61 final JPanel chooseGroupSizePanel = new JPanel();
62 JLabel creationLabel = new JLabel("How many members should the group have?");
63 JButton next = new JButton("Next");
64 SpinnerNumberModel model = new SpinnerNumberModel(1, 1, 4, 1);
65 final JSpinner spinner = new JSpinner(model);
67 // When 'next' is clicked the second, final panel of the Group Creation
68 // Wizard should be shown
69 next.addActionListener(new ActionListener() {
71 public void actionPerformed(ActionEvent e) {
72 int numberMembers = (int) spinner.getValue();
73 final GroupCreationPanel groupCreationPanel = new GroupCreationPanel(numberMembers);
74 GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel,
77 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
78 groupCreationHandler.showGroupCreationDetailPanel();
83 GridLayout creationPanelLayout = new GridLayout(3, 1);
84 chooseGroupSizePanel.setLayout(creationPanelLayout);
85 creationPanel.setBorder(new TitledBorder("Create Groups"));
87 creationPanel.add(chooseGroupSizePanel);
88 chooseGroupSizePanel.add(creationLabel);
89 chooseGroupSizePanel.add(spinner);
90 chooseGroupSizePanel.add(next);
92 wrapper.add(creationPanel);
95 private void initGroupOverview(JPanel wrapper, GroupOverviewModel groupModel) {
96 JPanel overviewPanel = new JPanel(new GridBagLayout());
98 JTable overviewTable = new JTable(groupModel);
99 JScrollPane scrollPane = new JScrollPane(overviewTable);
101 overviewPanel.setBorder(new TitledBorder("Group Overview"));
102 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
104 GridBagConstraints c = new GridBagConstraints();
108 c.insets = new Insets(10, 10, 10, 10);
109 c.fill = GridBagConstraints.BOTH;
110 overviewPanel.add(scrollPane, c);
111 wrapper.add(overviewPanel);
115 * Handles the Wizard-like creation of groups.
117 * Give an instance of this class as a callback to the Panel from which a
118 * group creation request should be issued
120 class GroupCreationHandler {
121 private JPanel creationPanel;
122 private JPanel chooseGroupSizePanel;
123 private JPanel groupCreationPanel;
125 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
126 this.creationPanel = creationPanel;
127 this.chooseGroupSizePanel = chooseGroupSizePanel;
128 this.groupCreationPanel = groupCreationPanel;
131 public void showGroupCreationDetailPanel() {
132 chooseGroupSizePanel.setVisible(false);
133 creationPanel.add(groupCreationPanel);
136 public void handleGroupCreation(GroupCreationRequest gc) {
137 chooseGroupSizePanel.setVisible(true);
138 creationPanel.remove(groupCreationPanel);
139 creationPanel.repaint();
144 public void createGroups(GroupCreationRequest gc) {
145 List<Group> newGroups = new ArrayList<>();
146 for (int i = 0; i < gc.numberOfGroups; i++) {
147 Group group = new Group();
148 group.getGroupData().setSize(gc.size);
149 List<PizzaOrder> pizzaOrders = new ArrayList<>();
150 for (PizzaType pt : gc.pizzaTypes) {
151 pizzaOrders.add(new PizzaOrder(pt));
153 Order order = new Order(group, pizzaOrders);
154 group.getGroupData().setOrder(order);
156 newGroups.add(group);
158 GroupOverviewModel groupModel = GroupAgent.getInstance().getGroupModel();
159 groupModel.addItems(newGroups);
160 GroupAgent.getInstance().onGroupsCreated(newGroups);
163 public void handleCancel() {
164 chooseGroupSizePanel.setVisible(true);
165 creationPanel.remove(groupCreationPanel);
166 creationPanel.repaint();