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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
79
80
81
82
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
95
96 public InviterListPane() {
97 super();
98 initialize();
99 }
100
101
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
118
119
120 public InviterTableModel getInviterTableModel() {
121 return(inviterTableModel);
122 }
123
124
125
126
127 public void setCommandDispatcher(CommandDispatcher commandDispatcher) {
128 this.commandDispatcher = commandDispatcher;
129 }
130
131
132
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
161
162
163
164 private JScrollPane getInviterListScrollPane() {
165 if (inviterListScrollPane == null) {
166 inviterListScrollPane = new JScrollPane();
167 inviterListScrollPane.setViewportView(getInviterListTable());
168
169 }
170 return inviterListScrollPane;
171 }
172
173
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
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
221
222 inviterListPopupMenu.show(e.getComponent(),
223 e.getX(), e.getY());
224 }
225 }
226 }
227 }