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