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

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Iterator;

import javax.swing.JLabel;
import javax.swing.JPanel;
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 Iterable<PlayerSettings> players;
	private Iterable<Score> scores;
	private Score accumulatedScore;

	public ScorePanel() {
		setLayout(new GridBagLayout());
		setBorder(new LineBorder(Color.BLACK));
	}

	@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() {
		this.removeAll();
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.gridwidth = GridBagConstraints.REMAINDER;
		
		addPlayerNames();

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

	private void addPlayerNames() {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		
		JLabel roundHead = new JLabel("Runde");
		roundHead.setHorizontalAlignment(JLabel.CENTER);
		add(roundHead, c);

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

	private void addScoreRow(Score score, int n) {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.gridwidth = 1;
		
		JLabel roundLabel = new JLabel(Integer.toString(n));
		roundLabel.setHorizontalAlignment(JLabel.CENTER);
		add(roundLabel, c);

		for (int i = 0; i < score.getPoints().size(); ++i) {
			if (i == score.getPoints().size()-1) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}
			
			JLabel scoreLabel = new JLabel(Integer.toString(score.getPoints().get(i)));
			scoreLabel.setHorizontalAlignment(JLabel.CENTER);
			add(scoreLabel, c);
		}
	}

	private void addAccumulatedScore() {
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		
		JLabel accumLabel = new JLabel("Gesamt");
		accumLabel.setHorizontalAlignment(JLabel.CENTER);
		add(accumLabel, c);
		
		for (int i = 0; i < accumulatedScore.getPoints().size(); ++i) {
			if (i == accumulatedScore.getPoints().size()-1) {
				c.gridwidth = GridBagConstraints.REMAINDER;
			}
			
			JLabel scoreLabel = new JLabel(Integer.toString(accumulatedScore.getPoints().get(i)));
			scoreLabel.setHorizontalAlignment(JLabel.CENTER);
			add(scoreLabel, c);
		}
	}
}