]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java
Merge branch 'master' of
[pub/jan/sbc.git] / src / main / java / at / ac / tuwien / sbc / valesriegler / group / gui / GroupFrame.java
1 package at.ac.tuwien.sbc.valesriegler.group.gui;
2
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;
11
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;
22
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;
28
29 /**
30  * Base Frame of the Group UI
31  * 
32  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
33  * 
34  */
35 @SuppressWarnings("serial")
36 public class GroupFrame extends JFrame {
37         public GroupFrame(GroupOverviewModel groupModel) {
38                 super("Groups");
39                 JPanel wrapper = new JPanel();
40                 GridLayout wrapperLayout = new GridLayout(2, 1);
41                 wrapper.setLayout(wrapperLayout);
42
43                 // Create Wizard-like panel for the creation of groups
44                 initGroupCreation(wrapper);
45
46                 // Create the panel for the group overview table
47                 initGroupOverview(wrapper, groupModel);
48
49                 setContentPane(wrapper);
50                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51         }
52
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);
60
61                 // When 'next' is clicked the second, final panel of the Group Creation
62                 // Wizard should be shown
63                 next.addActionListener(new ActionListener() {
64                         @Override
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,
69                                                 groupCreationPanel);
70
71                                 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
72                                 groupCreationHandler.showGroupCreationDetailPanel();
73
74                         }
75                 });
76
77                 GridLayout creationPanelLayout = new GridLayout(3, 1);
78                 chooseGroupSizePanel.setLayout(creationPanelLayout);
79                 creationPanel.setBorder(new TitledBorder("Create Groups"));
80
81                 creationPanel.add(chooseGroupSizePanel);
82                 chooseGroupSizePanel.add(creationLabel);
83                 chooseGroupSizePanel.add(spinner);
84                 chooseGroupSizePanel.add(next);
85
86                 wrapper.add(creationPanel);
87         }
88
89         private void initGroupOverview(JPanel wrapper, GroupOverviewModel groupModel) {
90                 JPanel overviewPanel = new JPanel(new GridBagLayout());
91
92                 JTable overviewTable = new JTable(groupModel);
93                 JScrollPane scrollPane = new JScrollPane(overviewTable);
94
95                 overviewPanel.setBorder(new TitledBorder("Group Overview"));
96                 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
97
98                 GridBagConstraints c = new GridBagConstraints();
99                 c.gridx = 0;
100                 c.weightx = 1;
101                 c.weighty = 1;
102                 c.insets = new Insets(10, 10, 10, 10);
103                 c.fill = GridBagConstraints.BOTH;
104                 overviewPanel.add(scrollPane, c);
105                 wrapper.add(overviewPanel);
106         }
107
108         /**
109          * Handles the Wizard-like creation of groups.
110          * <p />
111          * Give an instance of this class as a callback to the Panel from which a
112          * group creation request should be issued
113          */
114         class GroupCreationHandler {
115                 private JPanel creationPanel;
116                 private JPanel chooseGroupSizePanel;
117                 private JPanel groupCreationPanel;
118
119                 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
120                         this.creationPanel = creationPanel;
121                         this.chooseGroupSizePanel = chooseGroupSizePanel;
122                         this.groupCreationPanel = groupCreationPanel;
123                 }
124
125                 public void showGroupCreationDetailPanel() {
126                         chooseGroupSizePanel.setVisible(false);
127                         creationPanel.add(groupCreationPanel);
128                 }
129
130                 public void handleGroupCreation(GroupCreationRequest gc) {
131                         chooseGroupSizePanel.setVisible(true);
132                         creationPanel.remove(groupCreationPanel);
133                         creationPanel.repaint();
134
135                         createGroups(gc);
136                 }
137
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));
146                                 }
147                                 Order order = new Order(group, pizzaOrders);
148                                 group.getGroupData().setOrder(order);
149
150                                 newGroups.add(group);
151                         }
152                         GroupAgent.getInstance().getGroupModel().addItems(newGroups);
153                         GroupAgent.getInstance().onGroupsCreated(newGroups);
154                 }
155
156                 public void handleCancel() {
157                         chooseGroupSizePanel.setVisible(true);
158                         creationPanel.remove(groupCreationPanel);
159                         creationPanel.repaint();
160                 }
161         }
162
163 }