summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/ScorePanel.java
blob: 39ae285c1bf253f3f355b9a3e5e75646f1ede2a9 (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
package jrummikub.view.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.border.LineBorder;

import jrummikub.model.PlayerSettings;
import jrummikub.model.Score;
import jrummikub.view.IScorePanel;

@SuppressWarnings("serial")
class ScorePanel extends JPanel implements IScorePanel {
	private final static ImageIcon STAR_ICON = new ImageIcon(
			ScorePanel.class.getResource("/jrummikub/resource/star.png"));
	private final static ImageIcon EMPTY_ICON = new ImageIcon(new BufferedImage(
			16, 16, BufferedImage.TYPE_INT_ARGB));

	private Iterable<PlayerSettings> players;
	private Iterable<Score> scores;
	private Score accumulatedScore;

	private JScrollPane scrollPane;
	private JPanel innerPanel;

	public ScorePanel() {
		setBorder(new LineBorder(Color.BLACK));
		setLayout(new BorderLayout(0, 5));

		JLabel titleLabel = new JLabel("Auswertung");
		titleLabel.setHorizontalAlignment(JLabel.CENTER);
		titleLabel.setFont(titleLabel.getFont().deriveFont(20.0f));
		add(titleLabel, BorderLayout.NORTH);

		innerPanel = new JPanel();
		innerPanel.setLayout(new GridBagLayout());

		scrollPane = new JScrollPane(innerPanel,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

		add(scrollPane);
	}

	@Override
	public void setPlayers(Iterable<PlayerSettings> players) {
		this.players = players;
	}

	@Override
	public void setScores(Iterable<Score> scores) {
		this.scores = scores;
	}

	@Override
	public void setAccumulatedScore(Score accumulatedScore) {
		this.accumulatedScore = accumulatedScore;
	}

	@Override
	public void update() {
		innerPanel.removeAll();

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 0.0;
		c.gridwidth = GridBagConstraints.REMAINDER;

		addPlayerNames();

		innerPanel.add(new JSeparator(), c);

		int n = 0;
		for (Iterator<Score> it = scores.iterator(); it.hasNext();) {
			addScoreRow(it.next(), ++n);
		}

		innerPanel.add(new JSeparator(), c);

		addAccumulatedScore();

		c.weighty = 1;
		innerPanel.add(Box.createVerticalGlue(), c);
	}

	private void addPlayerNames() {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.3;
		c.weighty = 0.0;
		c.insets = new Insets(0, 7, 0, 7);

		JLabel roundHead = new JLabel("Runde");
		roundHead.setHorizontalAlignment(JLabel.CENTER);
		innerPanel.add(roundHead, c);

		c.weightx = 1.0;

		for (Iterator<PlayerSettings> it = players.iterator(); it.hasNext();) {
			PlayerSettings player = it.next();

			if (!it.hasNext()) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}

			JLabel playerNameLabel = new JLabel(player.getName());
			playerNameLabel.setIcon(ImageUtil.createColorIcon(player.getColor(), 12,
					1));
			playerNameLabel.setHorizontalAlignment(JLabel.CENTER);

			innerPanel.add(playerNameLabel, c);
		}
	}

	private void addScoreRow(Score score, int n) {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.3;
		c.weighty = 0.0;
		c.gridwidth = 1;
		c.insets = new Insets(0, 7, 0, 7);

		JLabel roundLabel = new JLabel(Integer.toString(n));
		roundLabel.setHorizontalAlignment(JLabel.CENTER);
		innerPanel.add(roundLabel, c);

		c.weightx = 1.0;

		for (int i = 0; i < score.getPoints().size(); ++i) {
			if (i == score.getPoints().size() - 1) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}

			boolean won = score.getWinners().get(i);
			JLabel scoreLabel = new JLabel(Integer.toString(score.getPoints().get(i)));
			scoreLabel.setIcon(won ? STAR_ICON : EMPTY_ICON);
			scoreLabel.setFont(scoreLabel.getFont().deriveFont(won ? Font.BOLD : 0));
			scoreLabel.setHorizontalAlignment(JLabel.CENTER);
			innerPanel.add(scoreLabel, c);
		}
	}

	private void addAccumulatedScore() {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.3;
		c.weighty = 0.0;
		c.insets = new Insets(0, 7, 0, 7);

		JLabel accumLabel = new JLabel("Gesamt");
		accumLabel.setHorizontalAlignment(JLabel.CENTER);
		innerPanel.add(accumLabel, c);

		c.weightx = 1.0;

		for (int i = 0; i < accumulatedScore.getPoints().size(); ++i) {
			if (i == accumulatedScore.getPoints().size() - 1) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}

			boolean won = accumulatedScore.getWinners().get(i);
			JLabel scoreLabel = new JLabel(Integer.toString(accumulatedScore
					.getPoints().get(i)));
			scoreLabel.setIcon(won ? STAR_ICON : EMPTY_ICON);
			scoreLabel.setFont(scoreLabel.getFont().deriveFont(won ? Font.BOLD : 0));
			scoreLabel.setHorizontalAlignment(JLabel.CENTER);
			innerPanel.add(scoreLabel, c);
		}
	}
}