View Javadoc
1   /******************************************************************************
2    * GroupPopupSubmenu.java - The group submenu for putting a player in groups
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.2  2011/01/01 06:22:09  dick
10   * Javadoc.
11   *
12   * Revision 1.1  2011/01/01 00:17:56  dick
13   * The group submenu for putting a player in groups
14   *
15   */
16  
17  /* 
18   * This program is free software: you can redistribute it and/or modify
19   * it under the terms of the GNU General Public License as published by
20   * the Free Software Foundation, either version 3 of the License, or
21   * (at your option) any later version.
22   *
23   * This program is distributed in the hope that it will be useful,
24   * but WITHOUT ANY WARRANTY; without even the implied warranty of
25   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26   * GNU General Public License for more details.
27   *
28   * You should have received a copy of the GNU General Public License
29   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30   *
31   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
32   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
33   * 
34   */
35  package com.buckosoft.fibs.BuckoFIBS.gui.playerList.group;
36  
37  import java.awt.Dimension;
38  import java.awt.event.ActionEvent;
39  import java.awt.event.ActionListener;
40  import java.util.LinkedList;
41  
42  import javax.swing.JCheckBoxMenuItem;
43  import javax.swing.JMenu;
44  
45  import com.buckosoft.fibs.BuckoFIBS.GroupManager;
46  import com.buckosoft.fibs.domain.Player;
47  
48  /** The group submenu for putting a player in groups
49   * @author Dick Balaska
50   * @since 2010/12/31
51   * @version $Revision$ <br> $Date$
52   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/gui/playerList/group/GroupPopupSubmenu.java">cvs GroupPopupSubmenu.java</a>
53   */
54  public class GroupPopupSubmenu extends JMenu implements ActionListener {
55  	private	final static boolean DEBUG = false;
56  	private static final long serialVersionUID = 1L;
57  
58  	private GroupManager groupManager;
59  	
60  	
61  	/** A small helper class that contains a group name and its selected status.
62  	 * Used for building the GroupPopupSubmenu
63  	 * @author Dick Balaska
64  	 * @since 2010/12/31
65  	 * @version $Revision$ <br> $Date$
66  	 * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/gui/playerList/group/GroupPopupSubmenu.java">cvs GroupPopupSubmenu.java</a>
67  	 */
68  	public class PopupGroup {
69  		/** The name of the group to display */
70  		public String name;
71  
72  		/** Draw this entry as selected */
73  		public boolean selected;
74  	}
75  
76  	/** Default constructor and initialize. 
77  	 */
78  	public GroupPopupSubmenu() {
79  		super();
80  		initialize();
81  	}
82  
83  	/** Called when a menu entry from this menu is selected.
84  	 * @param e What just happened?
85  	 */
86  	public void actionPerformed(ActionEvent e) {
87  		//this.groupManager.menuEvent(e);
88  		String s = e.getActionCommand();
89  		int i = s.indexOf('-');
90  		String gn = s.substring(0, i);
91  		String pn = s.substring(i+1);
92  		if (DEBUG)
93  			System.out.println("menu: '" + s + "' gn='" + gn + "' pn='" + pn + "'");
94  		this.groupManager.popupMenuItemSelected(gn, pn);
95  	}
96  
97  	/**
98  	 * This method initializes this
99  	 * 
100 	 */
101 	private void initialize() {
102         this.setSize(new Dimension(73, 40));
103         this.setText("Groups");
104 	}
105 
106 	/**
107 	 * @return the gameManager
108 	 */
109 	public GroupManager getGroupManager() {
110 		return groupManager;
111 	}
112 
113 	/** Set our reference to the object that will receive menu events
114 	 * @param groupManager the groupManager to set
115 	 */
116 	public void setGroupManager(GroupManager groupManager) {
117 		this.groupManager = groupManager;
118 	}
119 	
120 	/** Empty and recreate the entries in the popup menu for this player.
121 	 * @param player The player who we right-clicked on.
122 	 */
123 	public void repopulate(Player player) {
124 		if (DEBUG)
125 			System.out.println("GroupPopupSubmenu: repopulate for player " + player.getId() + " '" + player.getName() + "'");
126 		this.removeAll();
127 		LinkedList<PopupGroup> lpg = this.groupManager.getGroupsForPlayer(this, player.getId());
128 		for (PopupGroup pg : lpg) {
129 			if (pg.name.equals("All"))
130 				continue;
131 			JCheckBoxMenuItem mi  = new JCheckBoxMenuItem();
132 			mi.setText(pg.name);
133 			mi.setSelected(pg.selected);
134 			//mi.setSelected(true);
135 			mi.setActionCommand(pg.name + "-" + player.getName());
136 			mi.setToolTipText("Add this player to this group.");
137 			mi.addActionListener(this);
138 			this.add(mi);
139 		}
140 	}
141 }