View Javadoc
1   /******************************************************************************
2    * GroupSelectPane.java - Display the pane the allows the user to select active groups
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.4  2011/01/04 17:42:39  dick
10   * Don't disable isIgnore() groups.
11   *
12   * Revision 1.3  2011/01/01 20:41:33  dick
13   * We have to use buttons instead of labels in order to figure out what we clicked on.
14   * I don't like the way they look, but they work.
15   *
16   * Revision 1.2  2011/01/01 06:23:51  dick
17   * Enable/disable groups on selecting All.
18   *
19   * Revision 1.1  2010/12/31 05:34:51  dick
20   * Display the pane the allows the user to select active groups.
21   *
22   */
23  
24  /* 
25   * This program is free software: you can redistribute it and/or modify
26   * it under the terms of the GNU General Public License as published by
27   * the Free Software Foundation, either version 3 of the License, or
28   * (at your option) any later version.
29   *
30   * This program is distributed in the hope that it will be useful,
31   * but WITHOUT ANY WARRANTY; without even the implied warranty of
32   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33   * GNU General Public License for more details.
34   *
35   * You should have received a copy of the GNU General Public License
36   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37   *
38   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
39   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
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  /** Display the pane the allows the user to select active groups.
62   * @author Dick Balaska
63   * @since 2010/12/30
64   * @version $Revision$ <br> $Date$
65   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/gui/playerList/PlayerListPane.java">cvs PlayerListPane.java</a>
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  //	private JLabel jLabelAll = null;
73  //	private JLabel jLabelAddNewGroup = null;
74  	private JScrollPane scrollPane = null;  //  @jve:decl-index=0:visual-constraint="17,265"
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  	 * This is the default constructor
90  	 */
91  	public GroupSelectPane() {
92  		super();
93  		initialize();
94  	}
95  
96  /*	public GroupSelectPane(JComponent parent) {
97  		super(parent);
98  		initialize();
99  	}
100 */
101 	/**
102 	 * This method initializes this
103 	 * 
104 	 * @return void
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 	 * This method initializes jPanelMainPane	
123 	 * 	
124 	 * @return javax.swing.JPanel	
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 	/** Set our reference to the GroupManager.
135 	 * @param groupManager The one and only GroupManager
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 	 * This method initializes jCheckBoxAll	
147 	 * 	
148 	 * @return javax.swing.JCheckBox	
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 	/** Entry point when the pane is shown and we need to populate the display with groups.
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 		//gridBagConstraintsR.anchor = GridBagConstraints.WEST;
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 	/** After the GroupManager finishes populating the pane, Finish with the "Add new Group" button.
227 	 */
228 	public void populateFinish() {
229 		GridBagConstraints gridBagConstraints = new GridBagConstraints();
230 		gridBagConstraints.gridwidth = 2;
231 		gridBagConstraints.gridx = 0;
232 		//gridBagConstraints.gridy = 1;
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 	/** Here when the "All" checkbox is toggled
265 	 * @param checked true = checked
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 	/** Depending on certain events, items get enabled and disabled.  Process them here.
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()) {		// Always enable the ignores
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 }