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