View Javadoc
1   /******************************************************************************
2    * InviterListPane.java - Display the List of inviters
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.2  2010/03/03 12:19:49  inim
10   * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
11   *
12   * Revision 1.1  2010/02/04 05:57:53  inim
13   * Mavenized project folder layout
14   *
15   * Revision 1.5  2009/03/12 16:35:45  dick
16   * Fire the GET_PLAYER_REPORT event if double-click a player.
17   *
18   * Revision 1.4  2009/02/17 14:44:40  dick
19   * Turn off DEBUG.
20   *
21   * Revision 1.3  2009/02/14 15:39:40  dick
22   * BuckoFIBS is released under the GNU license.
23   * Javadoc.
24   *
25   * Revision 1.2  2009/02/01 09:02:03  dick
26   * PlayerNameRenderer becomes InviterNameRenderer in this package.
27   *
28   * Revision 1.1  2009/01/28 19:39:15  dick
29   * package com.buckosoft.fibs.gui.inviterList becomes com.buckosoft.fibs.BuckoFIBS.gui.inviterList.
30   *
31   * Revision 1.4  2009/01/27 19:16:16  dick
32   * playerName gets a custom renderer so we can display warnings.
33   *
34   * Revision 1.3  2008/12/11 20:29:26  dick
35   * Context menu functional.
36   *
37   * Revision 1.2  2008/12/09 19:39:18  dick
38   * Use the InviterTableModel, not a PlayerTableModel.
39   */
40  
41  /* 
42   * This program is free software: you can redistribute it and/or modify
43   * it under the terms of the GNU General Public License as published by
44   * the Free Software Foundation, either version 3 of the License, or
45   * (at your option) any later version.
46   *
47   * This program is distributed in the hope that it will be useful,
48   * but WITHOUT ANY WARRANTY; without even the implied warranty of
49   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50   * GNU General Public License for more details.
51   *
52   * You should have received a copy of the GNU General Public License
53   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
54   *
55   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
56   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
57   * 
58   */
59  package com.buckosoft.fibs.BuckoFIBS.gui.inviterList;
60  
61  import java.awt.GridBagConstraints;
62  import java.awt.GridBagLayout;
63  import java.awt.Point;
64  import java.awt.event.ActionEvent;
65  import java.awt.event.ActionListener;
66  import java.awt.event.MouseAdapter;
67  import java.awt.event.MouseEvent;
68  import java.awt.event.MouseListener;
69  
70  import javax.swing.JPanel;
71  import javax.swing.JScrollPane;
72  import javax.swing.JTable;
73  import javax.swing.ListSelectionModel;
74  
75  import com.buckosoft.fibs.BuckoFIBS.CommandDispatcher;
76  import com.buckosoft.fibs.domain.Player;
77  
78  /** Display the list of Inviters
79   * @author Dick Balaska
80   * @since 2008/12/08
81   * @version $Revision$ <br> $Date$
82   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/com/buckosoft/fibs/BuckoFIBS/gui/inviterList/InviterListPane.java">cvs InviterListPane.java</a>
83   */
84  public class InviterListPane extends JPanel  implements ActionListener{
85  	private	final static boolean DEBUG = false;
86  	private static final long serialVersionUID = 1L;
87  	private JScrollPane			inviterListScrollPane = null;
88  	private JTable				inviterListTable = null;
89  	private	InviterTableModel 	inviterTableModel;
90  	private	CommandDispatcher	commandDispatcher = null;
91  	private	InviterListPopupMenu inviterListPopupMenu = null;
92  
93  
94  	/** This is the default constructor
95  	 */
96  	public InviterListPane() {
97  		super();
98  		initialize();
99  	}
100 
101 	/** This method initializes this
102 	 */
103 	private void initialize() {
104 		getInviterListPopupMenu();
105 		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
106 		gridBagConstraints1.fill = GridBagConstraints.BOTH;
107 		gridBagConstraints1.gridy = 1;
108 		gridBagConstraints1.weightx = 1.0;
109 		gridBagConstraints1.weighty = 1.0;
110 		gridBagConstraints1.gridx = 0;
111 		this.setSize(300, 200);
112 		this.setLayout(new GridBagLayout());
113 		this.add(getInviterListScrollPane(), gridBagConstraints1);
114 
115 	}
116 
117 	/** Return a reference to our tableModel
118 	 * @return The InviterTableModel
119 	 */
120 	public InviterTableModel getInviterTableModel() {
121 		return(inviterTableModel);
122 	}
123 
124 	/** Set the reference to the CommandDispatcher
125 	 * @param commandDispatcher The application CommandDispatcher
126 	 */
127 	public void setCommandDispatcher(CommandDispatcher commandDispatcher) {
128 		this.commandDispatcher = commandDispatcher;
129 	}
130 
131 	/** Called directly from the menu.
132 	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
133 	 */
134 	public void actionPerformed(ActionEvent e) {
135 		Player p =	(Player)inviterListTable.getModel().getValueAt(inviterListTable.getSelectedRow(), 0);
136 		String playerName = p.getName();
137 		if (DEBUG)
138 			System.out.println("Action performed: " + e.getActionCommand() + " on " + playerName);
139 		if (e.getActionCommand() == "Accept") {
140 			this.commandDispatcher.dispatch(CommandDispatcher.Command.ACCEPT_INVITATION, playerName);
141 		} else {
142 			System.out.println("Unhandled Action: " + e.getActionCommand() + " on " + playerName);			
143 		}
144 	}
145 
146 	private	void	onMousePressed(MouseEvent e) {
147 		if (e.getButton() != MouseEvent.BUTTON1)
148 			return;
149 		if (e.getClickCount() != 2)
150 			return;
151 		Player p = (Player)inviterListTable.getModel().getValueAt(inviterListTable.convertRowIndexToModel(inviterListTable.getSelectedRow()), 0);
152 		if (p == null)
153 			return;
154 		String playerName = p.getName();
155 		if (DEBUG)
156 			System.out.println("dblclick performed: on " + playerName);
157 		this.commandDispatcher.dispatch(CommandDispatcher.Command.GET_PLAYER_REPORT, playerName);
158 	}
159 	/**
160 	 * This method initializes playerListScrollPane	
161 	 * 	
162 	 * @return javax.swing.JScrollPane	
163 	 */
164 	private JScrollPane getInviterListScrollPane() {
165 		if (inviterListScrollPane == null) {
166 			inviterListScrollPane = new JScrollPane();
167 			inviterListScrollPane.setViewportView(getInviterListTable());
168 
169 		}
170 		return inviterListScrollPane;
171 	}
172 	/** This method initializes playerListTable	
173 	 * @return javax.swing.JTable	
174 	 */
175 	private JTable getInviterListTable() {
176 		if (inviterListTable == null) {
177 			inviterTableModel = new InviterTableModel();
178 			inviterListTable = new JTable(inviterTableModel);
179 			inviterListTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
180 			InviterNameRenderer playerRenderer = new InviterNameRenderer();
181 			inviterListTable.setDefaultRenderer(com.buckosoft.fibs.domain.Player.class, playerRenderer);
182 			playerRenderer.setBackground(inviterListTable.getBackground());
183 			playerRenderer.setSelectedBackground(inviterListTable.getSelectionBackground());
184 			//Add listener to components that can bring up popup menus.
185 			MouseListener popupListener = new PopupListener();
186 			inviterListTable.addMouseListener(popupListener);
187 
188 			inviterListTable.addMouseListener(new java.awt.event.MouseAdapter() {   
189 				public void mousePressed(java.awt.event.MouseEvent e) {    
190 					onMousePressed(e);
191 				}
192 			});
193 		}
194 		return inviterListTable;
195 	}
196 
197 	private	InviterListPopupMenu getInviterListPopupMenu() { 
198 		if (inviterListPopupMenu == null) {
199 			inviterListPopupMenu = new InviterListPopupMenu();
200 			inviterListPopupMenu.setMenuHandler(this);
201 
202 		}
203 		return(inviterListPopupMenu);
204 	}
205 
206 	class PopupListener extends MouseAdapter {
207 		public void mousePressed(MouseEvent e) {
208 			maybeShowPopup(e);
209 		}
210 
211 		public void mouseReleased(MouseEvent e) {
212 			maybeShowPopup(e);
213 		}
214 
215 		private void maybeShowPopup(MouseEvent e) {
216 			if (e.isPopupTrigger()) {
217 				int row = inviterListTable.rowAtPoint(new Point(e.getX(), e.getY()));
218 				if (row != -1)
219 					inviterListTable.getSelectionModel().setSelectionInterval(row, row);
220 				//if (DEBUG)
221 				//	System.out.println("selected row = " + row);
222 				inviterListPopupMenu.show(e.getComponent(),
223 						e.getX(), e.getY());
224 			}
225 		}
226 	}
227 }