summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/GameListPanel.java
blob: d22dc02a23f52d388ff7cfa9ec2a59f19cac4ffc (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
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.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.DefaultListModel;
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 jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.view.IGameListPanel;

class GameListPanel extends JPanel implements IGameListPanel {
	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 = new ArrayList<GameData>();

	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));
		add(title, c);

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

		gameList = new JList();
		gameList.setCellRenderer(new GameDataCellRenderer());
		c.weighty = 1;
		add(new JScrollPane(gameList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), c);

		c.weighty = 0;
		add(Box.createVerticalStrut(3), c);

		joinButton = new JButton("Beitreten");
		c.gridwidth = 1;
		add(joinButton, c);
		joinButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				// joinEvent.emit();
			}
		});

		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) {
				// TODO Auto-generated method stub
				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();
			}
		});

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

	void reset() {
		games.clear();
	}

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

	@Override
	public void addGame(GameData game) {
		if (!games.contains(game)) {
			games.add(game);
		}

		updateModel();
	}

	@Override
	public void removeGame(GameData game) {
		if (games.remove(game)) {
			updateModel();
		}
	}

	private void updateModel() {
		DefaultListModel model = new DefaultListModel();

		for (GameData game : games) {
			model.addElement(game);
		}

		gameList.setModel(model);
	}

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

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

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

	private static class GameDataCellRenderer extends JPanel implements
			ListCellRenderer {
		JLabel hostLabel, playerCountLabel;

		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();
				/*playerCount = gameData.getCurrentPlayerCount() + "/"
						+ gameData.getMaxPlayerCount();*/
				playerCount = String.valueOf(gameData.getGameSettings().getPlayerList().size());
			} 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;
		}
	}
}