]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupCreationHandler.java
Add wizard for creating delivery groups to GroupFrame
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / GroupCreationHandler.java
1 package at.ac.tuwien.sbc.valesriegler.group.gui;
2
3 import at.ac.tuwien.sbc.valesriegler.group.Group;
4 import at.ac.tuwien.sbc.valesriegler.group.GroupAgent;
5 import at.ac.tuwien.sbc.valesriegler.types.Order;
6 import at.ac.tuwien.sbc.valesriegler.types.PizzaOrder;
7 import at.ac.tuwien.sbc.valesriegler.types.PizzaType;
8
9 import javax.swing.*;
10 import java.util.ArrayList;
11 import java.util.List;
12
13 /**
14  * Handles the Wizard-like creation of groups.
15  * <p />
16  * Give an instance of this class as a callback to the Panel from which a
17  * group creation request should be issued
18  */
19 class GroupCreationHandler {
20     private final boolean isDeliveryGroup;
21     private JPanel creationPanel;
22     private JPanel chooseGroupSizePanel;
23     private JPanel groupCreationPanel;
24
25     public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel, boolean isDeliveryGroup) {
26         this.isDeliveryGroup = isDeliveryGroup;
27         this.creationPanel = creationPanel;
28         this.chooseGroupSizePanel = chooseGroupSizePanel;
29         this.groupCreationPanel = groupCreationPanel;
30     }
31
32     public void showGroupCreationDetailPanel() {
33         chooseGroupSizePanel.setVisible(false);
34         creationPanel.add(groupCreationPanel);
35     }
36
37     public void handleGroupCreation(GroupCreationDetailsRequest gc) {
38         chooseGroupSizePanel.setVisible(true);
39         creationPanel.remove(groupCreationPanel);
40         creationPanel.repaint();
41
42         createGroups(gc);
43     }
44
45     public void createGroups(GroupCreationDetailsRequest gc) {
46         List<Group> newGroups = new ArrayList<>();
47         for (int i = 0; i < gc.numberOfGroups; i++) {
48             Group group = new Group();
49             group.getGroupData().setSize(gc.size);
50             List<PizzaOrder> pizzaOrders = new ArrayList<>();
51             for (PizzaType pt : gc.pizzaTypes) {
52                 pizzaOrders.add(new PizzaOrder(pt));
53             }
54             Order order = new Order(group, pizzaOrders);
55 // the order id has to be set by the waiter
56             /*for (PizzaOrder pizzaOrder : order.getOrderedPizzas()) {
57                 pizzaOrder.setOrderId(order.getId());
58             }*/
59             group.getGroupData().setOrder(order);
60             order.setNumberOfPizzas(order.getOrderedPizzas().size());
61
62             newGroups.add(group);
63         }
64         GroupOverviewModel groupModel = GroupAgent.getInstance().getGroupModel();
65         groupModel.addItems(newGroups);
66         GroupAgent.getInstance().onGroupsCreated(newGroups, isDeliveryGroup);
67     }
68
69     public void handleCancel() {
70         chooseGroupSizePanel.setVisible(true);
71         creationPanel.remove(groupCreationPanel);
72         creationPanel.repaint();
73     }
74 }