]> git.somenet.org - pub/jan/sbc.git/blob - src/main/java/at/ac/tuwien/sbc/valesriegler/group/gui/GroupFrame.java
UI Fix: Handle the vertical size of tables correctly
[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 import java.util.concurrent.atomic.AtomicInteger;
12
13 import javax.swing.JButton;
14 import javax.swing.JFrame;
15 import javax.swing.JLabel;
16 import javax.swing.JOptionPane;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JSpinner;
20 import javax.swing.JTable;
21 import javax.swing.SpinnerNumberModel;
22 import javax.swing.border.TitledBorder;
23
24 import at.ac.tuwien.sbc.valesriegler.group.Group;
25
26 /**
27  * Base Frame of the Group UI
28  * 
29  * @author Gregor Riegler <gregor DOT riegler AT gmail DOT com>
30  *
31  */
32 public class GroupFrame extends JFrame {
33         private int idCounter = 1;
34         
35         private GroupOverviewModel groupModel;
36
37         public GroupFrame() {
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);
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 Wizard should be shown
62             next.addActionListener(new ActionListener() {
63                         @Override
64                         public void actionPerformed(ActionEvent e) {
65                                 int numberMembers = (int) spinner.getValue();
66                                 final GroupCreationPanel groupCreationPanel = new GroupCreationPanel(numberMembers);
67                                 GroupCreationHandler groupCreationHandler = new GroupCreationHandler(creationPanel, chooseGroupSizePanel, groupCreationPanel);
68                                 
69                                 groupCreationPanel.setCreateAndCancelHandler(groupCreationHandler);
70                                 groupCreationHandler.showGroupCreationDetailPanel();
71                                 
72                         
73                         }
74                 });
75         
76             GridLayout creationPanelLayout = new GridLayout(3,1);
77             chooseGroupSizePanel.setLayout(creationPanelLayout);
78             creationPanel.setBorder(new TitledBorder("Create Groups"));
79             
80             creationPanel.add(chooseGroupSizePanel);
81                 chooseGroupSizePanel.add(creationLabel);
82                 chooseGroupSizePanel.add(spinner);
83                 chooseGroupSizePanel.add(next);
84                 
85                 wrapper.add(creationPanel);
86         }
87
88         private void initGroupOverview(JPanel wrapper) {
89                 JPanel overviewPanel = new JPanel(new GridBagLayout());
90         
91                 groupModel = new GroupOverviewModel();
92                 JTable overviewTable = new JTable(groupModel);
93                 JScrollPane scrollPane = new JScrollPane(overviewTable);
94                 
95                 overviewPanel.setBorder(new TitledBorder("Group Overview"));
96                 scrollPane.setVerticalScrollBarPolicy(JScrollPane.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         /** 
110          * Handles the Wizard-like creation of groups.
111          * <p />
112          * Give an instance of this class as a callback to the Panel from which 
113          * a group creation request should be issued
114          */
115         class GroupCreationHandler {
116                 private JPanel creationPanel;
117                 private JPanel chooseGroupSizePanel;
118                 private JPanel groupCreationPanel;
119
120                 public GroupCreationHandler(JPanel creationPanel, JPanel chooseGroupSizePanel, JPanel groupCreationPanel) {
121                         this.creationPanel = creationPanel;
122                         this.chooseGroupSizePanel = chooseGroupSizePanel;
123                         this.groupCreationPanel = groupCreationPanel;
124                 }
125                 
126                 public void showGroupCreationDetailPanel() {
127                         chooseGroupSizePanel.setVisible(false);
128                         creationPanel.add(groupCreationPanel);
129                 }
130
131                 public void handleGroupCreation(GroupCreationRequest gc) {
132                         chooseGroupSizePanel.setVisible(true);
133                         creationPanel.remove(groupCreationPanel);
134                         creationPanel.repaint();
135                         
136                         createGroups(gc);
137                 }
138                 
139                 private void createGroups(GroupCreationRequest gc) {
140                         List<Group> newGroups = new ArrayList<>();
141                         for(int i=0; i<gc.numberOfGroups; i++) {
142                                 Group group = new Group();
143                                 group.setId(idCounter);
144                                 group.setSize(gc.size);
145                                 group.setPizzas(gc.pizzaTypes);
146                                 idCounter = idCounter + 1;
147                                 
148                                 newGroups.add(group);
149                         }
150                         groupModel.addItems(newGroups);
151                 }
152
153                 public void handleCancel() {
154                         chooseGroupSizePanel.setVisible(true);
155                         creationPanel.remove(groupCreationPanel);
156                         creationPanel.repaint();
157                 }
158         }
159
160 }