1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 package com.buckosoft.fibs.BuckoFIBS.gui.playerList.group;
43
44 import java.awt.BorderLayout;
45 import java.awt.GridBagConstraints;
46 import java.awt.GridBagLayout;
47 import java.awt.Insets;
48 import java.awt.event.ItemEvent;
49 import java.util.LinkedList;
50
51 import javax.swing.JButton;
52 import javax.swing.JCheckBox;
53 import javax.swing.JLabel;
54 import javax.swing.JPanel;
55 import javax.swing.JScrollPane;
56
57 import com.buckosoft.fibs.BuckoFIBS.GroupManager;
58 import com.buckosoft.fibs.BuckoFIBS.gui.MainDialog;
59 import com.buckosoft.fibs.domain.GroupOfPlayers;
60
61
62
63
64
65
66
67 public class GroupSelectPane extends JPanel {
68 private static final boolean DEBUG = false;
69 private static final long serialVersionUID = 1L;
70 private JPanel jPanelMainPane = null;
71 private JCheckBox jCheckBoxAll = null;
72
73
74 private JScrollPane scrollPane = null;
75 private GroupManager groupManager = null;
76 private MainDialog mainDialog = null;
77
78 private LinkedList<CheckWithName> checkWithNameList = new LinkedList<CheckWithName>();
79
80 class CheckWithName {
81 int id;
82 JCheckBox checkBox;
83 JButton label;
84 String name;
85 GroupOfPlayers group;
86 }
87
88
89
90
91 public GroupSelectPane() {
92 super();
93 initialize();
94 }
95
96
97
98
99
100
101
102
103
104
105
106 private void initialize() {
107 System.out.println("GroupSelectPane:<init>");
108 this.setLayout(new BorderLayout());
109 this.setSize(300, 200);
110 this.add(getScrollPane());
111 }
112
113 private JScrollPane getScrollPane() {
114 if (scrollPane == null) {
115 scrollPane = new JScrollPane();
116 scrollPane.setViewportView(getJPanelMainPane());
117 }
118 return(scrollPane);
119 }
120
121
122
123
124
125
126 private JPanel getJPanelMainPane() {
127 if (jPanelMainPane == null) {
128 jPanelMainPane = new JPanel();
129 jPanelMainPane.setLayout(new GridBagLayout());
130 }
131 return jPanelMainPane;
132 }
133
134
135
136
137 public void setGroupManager(GroupManager groupManager) {
138 this.groupManager = groupManager;
139 this.groupManager.setGroupSelectPane(this);
140 }
141
142 public void setMainDialog(MainDialog mainDialog) {
143 this.mainDialog = mainDialog;
144 }
145
146
147
148
149
150 private JCheckBox getJCheckBoxAll() {
151 if (jCheckBoxAll == null) {
152 jCheckBoxAll = new JCheckBox();
153 }
154 return jCheckBoxAll;
155 }
156
157 public void repopulate() {
158 jPanelMainPane = null;
159 scrollPane.setViewportView(getJPanelMainPane());
160 }
161
162
163
164 public void populatePane() {
165 this.groupManager.populatePane();
166 }
167
168 public void addGroup(GroupOfPlayers groupOfPlayers) {
169 GridBagConstraints gridBagConstraintsR = new GridBagConstraints();
170 gridBagConstraintsR.insets = new Insets(0, 0, 0, 1);
171 gridBagConstraintsR.gridx = 1;
172
173 gridBagConstraintsR.gridy = this.checkWithNameList.size();
174 GridBagConstraints gridBagConstraintsL = new GridBagConstraints();
175 gridBagConstraintsL.gridx = 0;
176 gridBagConstraintsL.gridy = this.checkWithNameList.size();
177 gridBagConstraintsL.insets = new Insets(0, 1, 0, 0);
178
179 String s = groupOfPlayers.getGroupName();
180 JCheckBox cb;
181 if (s.equals("All"))
182 cb = getJCheckBoxAll();
183 else {
184 cb = new JCheckBox();
185 cb.setName(s);
186 }
187 cb.addItemListener(new java.awt.event.ItemListener() {
188 public void itemStateChanged(java.awt.event.ItemEvent e) {
189 if (DEBUG)
190 System.out.println("Group toggled: ");
191 groupChecked(e);
192 }
193 });
194 cb.getModel().setSelected(groupOfPlayers.isActive());
195 jPanelMainPane.add(cb, gridBagConstraintsL);
196 JButton l = null;
197 if (s.equals("All")) {
198 JLabel jl = new JLabel();
199 jl.setText(s);
200 jPanelMainPane.add(jl, gridBagConstraintsR);
201 } else {
202 l = new JButton();
203 l.setBorder(null);
204 l.addActionListener(new java.awt.event.ActionListener() {
205 public void actionPerformed(java.awt.event.ActionEvent e) {
206 onEditGroup((JButton)e.getSource());
207 }
208 });
209 gridBagConstraintsR.fill = GridBagConstraints.HORIZONTAL;
210 if (groupOfPlayers.isIgnore())
211 l.setText("!" + s);
212 else
213 l.setText(s);
214 jPanelMainPane.add(l, gridBagConstraintsR);
215 }
216 CheckWithName cwn = new CheckWithName();
217 cwn.id = groupOfPlayers.getId();
218 cwn.checkBox = cb;
219 cwn.name = s;
220 cwn.label = l;
221 cwn.group = groupOfPlayers;
222 checkWithNameList.add(cwn);
223
224 }
225
226
227
228 public void populateFinish() {
229 GridBagConstraints gridBagConstraints = new GridBagConstraints();
230 gridBagConstraints.gridwidth = 2;
231 gridBagConstraints.gridx = 0;
232
233 gridBagConstraints.insets = new Insets(10, 0, 0, 0);
234 gridBagConstraints.anchor = GridBagConstraints.SOUTH;
235 JLabel l = new JLabel();
236 l.setText("Add New Group...");
237 l.addMouseListener(new java.awt.event.MouseAdapter() {
238 public void mouseClicked(java.awt.event.MouseEvent e) {
239 handleAddNewGroupButton();
240 }
241 });
242 jPanelMainPane.add(l, gridBagConstraints);
243 updateDisabledItems();
244 }
245 public void savePaneToList() {
246 for (CheckWithName cwn : checkWithNameList) {
247 this.groupManager.updateGroupOfPlayers(cwn.id, cwn.name, cwn.checkBox.getModel().isSelected());
248 }
249 }
250
251 private void handleAddNewGroupButton() {
252 EditGroupDialog egd = new EditGroupDialog(mainDialog, this);
253 egd.setVisible(true);
254 }
255
256 public void addNewGroupFromUser(GroupOfPlayers groupOfPlayers) {
257 this.groupManager.addNewGroupFromUser(groupOfPlayers);
258 }
259
260 public void editGroupFromUser(GroupOfPlayers groupOfPlayers) {
261 this.groupManager.editGroupFromUser(groupOfPlayers);
262 }
263
264
265
266
267 private void groupChecked(ItemEvent e) {
268 updateDisabledItems();
269 for (CheckWithName cwn : checkWithNameList) {
270 if (cwn.checkBox == e.getItem()) {
271 this.groupManager.updateGroupOfPlayers(cwn.id, cwn.name, cwn.checkBox.getModel().isSelected());
272 }
273 }
274 }
275
276
277
278 private void updateDisabledItems() {
279 boolean allMode = this.getJCheckBoxAll().getModel().isSelected();
280 for (CheckWithName cwn : checkWithNameList) {
281 if (cwn.name.equals("All"))
282 continue;
283 if (!cwn.group.isIgnore()) {
284 cwn.checkBox.setEnabled(!allMode);
285 cwn.label.setEnabled(!allMode);
286 }
287 }
288 }
289
290 private void onEditGroup(JButton e) {
291 for (CheckWithName cwn : checkWithNameList) {
292 if (cwn.name.equals("All"))
293 continue;
294 if (cwn.label == e) {
295 EditGroupDialog egd = new EditGroupDialog(mainDialog, this);
296 egd.setGroupOfPlayers(this.groupManager.getGroupOfPlayers(cwn.id));
297 egd.setVisible(true);
298 return;
299 }
300 }
301 }
302 }