summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/GameListPanel.java
blob: eebe3f50d1d83b13cece56d55a4488f5f8f4418d (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.List;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.Pair;
import jrummikub.view.IGameListPanel;

class GameListPanel extends JPanel implements IGameListPanel {
	private static final long serialVersionUID = 1L;

	private JLabel title;
	private JList gameList;
	private JButton joinButton;
	private JButton openNewGameButton;
	private JButton cancelButton;

	private Event1<GameData> joinEvent = new Event1<GameData>();
	private Event openNewGameEvent = new Event();
	private Event cancelEvent = new Event();

	private List<GameData> games = Collections.emptyList();

	/**
	 * Creates new game list panel
	 */
	GameListPanel() {
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1;
		c.weighty = 0;

		title = new JLabel();
		title.setFont(title.getFont().deriveFont(16.0f));
		title.putClientProperty("html.disable", Boolean.TRUE);
		add(title, c);

		add(Box.createVerticalStrut(3), c);

		createGameList();

		add(Box.createVerticalStrut(3), c);

		addButtons(c);

		setBorder(new CompoundBorder(new LineBorder(Color.BLACK),
				new EmptyBorder(10, 10, 10, 10)));
	}

	/**
	 * Creates the game list to be displayed in the panel
	 */
	private void createGameList() {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1;
		c.weighty = 1;

		gameList = new JList();
		gameList.setCellRenderer(new GameDataCellRenderer());

		gameList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					join();
				}
			}
		});

		gameList.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				Object data = gameList.getSelectedValue();

				if (data instanceof GameData) {
					GameData gameData = (GameData) data;
					Pair<Integer, Integer> playerSlotCount = getPlayerSlotCount(gameData);
					joinButton.setEnabled(playerSlotCount.getFirst() < playerSlotCount
							.getSecond());
				} else {
					joinButton.setEnabled(false);
				}
			}
		});

		add(new JScrollPane(gameList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), c);
	}

	/**
	 * Adds all three buttons to the panel
	 * 
	 * @param c
	 *            constraints for button positioning
	 */
	private void addButtons(GridBagConstraints c) {
		joinButton = new JButton("Beitreten");
		c.gridwidth = 1;
		joinButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				join();
			}
		});
		joinButton.setEnabled(false);
		add(joinButton, c);

		c.weightx = 0;
		add(Box.createHorizontalStrut(10), c);

		openNewGameButton = new JButton("Neues Spiel");
		c.weightx = 1;
		add(openNewGameButton, c);
		openNewGameButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				openNewGameEvent.emit();
			}
		});

		c.weightx = 0;
		add(Box.createHorizontalStrut(10), c);

		cancelButton = new JButton("Abbrechen");
		c.weightx = 1;
		c.weighty = 0;
		c.gridwidth = GridBagConstraints.REMAINDER;
		add(cancelButton, c);
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				cancelEvent.emit();
			}
		});
	}

	/**
	 * Resets panel if all games have been removed from list
	 */
	void reset() {
		games.clear();
	}

	@Override
	public void setChannelName(String name) {
		title.setText("Spiele in " + name + ":");
	}

	@Override
	public void setGameList(List<GameData> games) {
		this.games = games;

		Object currentGame = gameList.getSelectedValue();

		gameList.setListData(games.toArray());

		if (games.contains(currentGame)) {
			gameList.setSelectedValue(currentGame, false);
		} else if (!games.isEmpty()) {
			gameList.setSelectedIndex(0);
		}
	}

	@Override
	public IEvent getOpenNewGameEvent() {
		return openNewGameEvent;
	}

	@Override
	public IEvent1<GameData> getJoinEvent() {
		return joinEvent;
	}

	@Override
	public IEvent getCancelEvent() {
		return cancelEvent;
	}

	/**
	 * Emits a join event if a user selects a game and dedides to join it
	 */
	private void join() {
		Object data = gameList.getSelectedValue();

		if (data instanceof GameData) {
			joinEvent.emit((GameData) data);
		}
	}

	/**
	 * Counts the empty slots for network players in a game
	 * 
	 * @param gameData
	 *            of the game
	 * @return Pair of occupied spaces and vacant spaces
	 */
	private static Pair<Integer, Integer> getPlayerSlotCount(GameData gameData) {
		int total = gameData.getGameSettings().getPlayerList().size();
		int occupied = total;

		for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) {
			if (player.getType() == Type.VACANT) {
				occupied--;
			}
		}

		return new Pair<Integer, Integer>(occupied, total);
	}

	/**
	 * Class presenting the game data in a readable way
	 */
	private static class GameDataCellRenderer extends JPanel implements
			ListCellRenderer {
		private static final long serialVersionUID = -892701906163443927L;
		JLabel hostLabel, playerCountLabel;

		/**
		 * Create new game data cell renderer
		 */
		GameDataCellRenderer() {
			setLayout(new GridBagLayout());
			GridBagConstraints c = new GridBagConstraints();
			c.fill = GridBagConstraints.BOTH;
			c.gridwidth = 1;
			c.weightx = 1;
			c.weighty = 1;

			hostLabel = new JLabel();
			hostLabel.setOpaque(true);
			hostLabel.setHorizontalAlignment(JLabel.LEFT);
			add(hostLabel, c);

			playerCountLabel = new JLabel();
			playerCountLabel.setOpaque(true);
			playerCountLabel.setHorizontalAlignment(JLabel.RIGHT);

			c.gridwidth = GridBagConstraints.REMAINDER;
			add(playerCountLabel, c);
		}

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			String host = "", playerCount = "";

			if (value instanceof GameData
					&& ((GameData) value).getGameSettings() != null) {
				GameData gameData = (GameData) value;

				host = gameData.getHost();
				Pair<Integer, Integer> playerSlotCount = getPlayerSlotCount(gameData);

				playerCount = playerSlotCount.getFirst() + "/"
						+ playerSlotCount.getSecond();
			} else {
				host = String.valueOf(value);
			}

			hostLabel.setText(host);
			playerCountLabel.setText(playerCount);

			if (isSelected) {
				hostLabel.setBackground(list.getSelectionBackground());
				hostLabel.setForeground(list.getSelectionForeground());
				playerCountLabel.setBackground(list.getSelectionBackground());
				playerCountLabel.setForeground(list.getSelectionForeground());
			} else {
				hostLabel.setBackground(list.getBackground());
				hostLabel.setForeground(list.getForeground());
				playerCountLabel.setBackground(list.getBackground());
				playerCountLabel.setForeground(list.getForeground());
			}

			setEnabled(list.isEnabled());
			setFont(list.getFont());
			return this;
		}
	}
}