summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/SidePanel.java
blob: 9e83c916e5f4a27ee34b1074fa012acff54eb043 (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
313
314
315
316
317
318
319
320
321
322
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JViewport;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

import jrummikub.model.GameSettings;
import jrummikub.view.ISidePanel;

@SuppressWarnings("serial")
class SidePanel extends JPanel implements ISidePanel {
	InfoPanel infoPanel;
	PlayerListPanel playerListPanel;
	BottomScrollPane playerListScrollPane;
	private JLabel initialMeldLabel;
	private JLabel setNumberLabel;
	private JLabel highestValueLabel;
	private JLabel handStonesLabel;
	private JLabel jokerLabel;
	private JLabel noLimitsLabel;
	private JLabel colorLabel;

	public SidePanel() {
		setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();

		infoPanel = new InfoPanel(createRuleInfoPanel());
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 1;
		c.fill = GridBagConstraints.BOTH;
		infoPanel.setBorder(new MatteBorder(0, 0, 1, 1, Color.BLACK));
		add(infoPanel, c);

		playerListPanel = new PlayerListPanel();
		c.gridx = 0;
		c.gridy = 1;
		c.weighty = 1;
		c.weightx = 1;
		c.fill = GridBagConstraints.BOTH;

		playerListScrollPane = new BottomScrollPane(playerListPanel);
		playerListScrollPane
				.setBorder(new MatteBorder(0, 0, 0, 1, Color.BLACK));

		add(playerListScrollPane, c);

	}

	@Override
	public void setGameSettings(GameSettings settings) {
		initialMeldLabel.setText("" + settings.getInitialMeldThreshold());
		setNumberLabel.setText("" + settings.getStoneSetNumber());
		highestValueLabel.setText("1 - " + settings.getHighestValue());
		handStonesLabel.setText("" + settings.getNumberOfStonesDealt());
		jokerLabel.setText("" + settings.getJokerNumber());
		colorLabel.setText("" + settings.getStoneColors().size());
		noLimitsLabel.setVisible(settings.isNoLimits());
	}

	private JPanel createRuleInfoPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());		
		initialMeldLabel = createRuleLine(panel, "Auslegeschranke", 0);
		setNumberLabel = createRuleLine(panel, "Steins\u00e4tze", 1);
		highestValueLabel = createRuleLine(panel,"Steinwert", 2);
		handStonesLabel = createRuleLine(panel, "Startsteine", 3);
		jokerLabel = createRuleLine(panel, "Joker", 4);
		colorLabel = createRuleLine(panel, "Farben", 5);
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 6;
		c.gridwidth = 2;
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.CENTER;
		noLimitsLabel = new JLabel("No Limits");
		noLimitsLabel.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(noLimitsLabel, c);

		return panel;
	}
	
	private JLabel createRuleLine(JPanel panel, String name, int line) {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = line;
		c.weightx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.WEST;
		panel.add(new JLabel(name + ": "), c);
		JLabel label = new JLabel("");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		c.weightx = 0;
		c.gridx = 1;
		c.anchor = GridBagConstraints.EAST;
		panel.add(label, c);
		return label;
	}

	@SuppressWarnings("serial")
	class BottomScrollPane extends JPanel {
		JComponent content;
		JViewport viewport;
		JScrollBar scrollBar;
		boolean scrollToBottom;

		public BottomScrollPane(JComponent content) {
			setLayout(new GridBagLayout());
			GridBagConstraints c = new GridBagConstraints();
			c.weightx = 1;
			c.weighty = 1;
			c.fill = GridBagConstraints.BOTH;
			viewport = new JViewport();
			add(viewport, c);
			this.content = content;
			viewport.setView(content);
			scrollBar = new JScrollBar(JScrollBar.VERTICAL);
			scrollBar.setBorder(new LineBorder(Color.BLACK, 0));
			c.weightx = 0;
			add(scrollBar, c);

			ComponentAdapter resizeListener = new ComponentAdapter() {
				@Override
				public void componentResized(ComponentEvent e) {
					onResize();
				}
			};

			addComponentListener(resizeListener);

			content.addComponentListener(resizeListener);

			scrollBar.addAdjustmentListener(new AdjustmentListener() {
				@Override
				public void adjustmentValueChanged(AdjustmentEvent arg0) {
					scrollViewport();
				}
			});
			scrollToBottom();
		}

		public void scrollToBottom() {
			scrollToBottom = true;
		}

		private void onResize() {
			int oldValue = 0;
			if (!scrollToBottom) {
				oldValue = scrollBar.getMaximum()
						- scrollBar.getVisibleAmount() - scrollBar.getValue();
			}
			scrollToBottom = false;

			int max = content.getHeight();
			int extent = viewport.getHeight();
			int value = Math.max(0, max - extent - oldValue);
			scrollBar.setVisible(extent != max);
			scrollBar.setValues(value, extent, 0, max);
			scrollViewport();
		}

		private void scrollViewport() {
			viewport.setViewPosition(new Point(0, scrollBar.getValue()));
		}
	}

	static class InfoPanel extends JPanel {
		JPanel ruleInfoPanel;
		JCheckBox showRules;

		public InfoPanel(JPanel ruleInfo) {
			ruleInfoPanel = ruleInfo;
			setLayout(new GridBagLayout());
			GridBagConstraints c = new GridBagConstraints();
			showRules = new JCheckBox("Regeln");
			showRules.setSelected(true);
			c.gridx = 0;
			c.weightx = 1;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.anchor = GridBagConstraints.WEST;
			setupTriangleIcons(showRules);
			c.insets = new Insets(0, 4, 0, 4);
			add(showRules, c);

			showRules.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg) {
					boolean selected = showRules.isSelected();
					ruleInfoPanel.setVisible(selected);
					showRules.setPressedIcon(selected ? showRules.getIcon()
							: showRules.getSelectedIcon());
				}
			});

			c.insets = new Insets(4, 8, 4, 8);
			add(ruleInfoPanel, c);
		}

		private void setupTriangleIcons(JCheckBox test) {
			BufferedImage img1 = new BufferedImage(8, 8,
					BufferedImage.TYPE_3BYTE_BGR);
			BufferedImage img2 = new BufferedImage(8, 8,
					BufferedImage.TYPE_3BYTE_BGR);

			Graphics2D g = img1.createGraphics();

			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g.setColor(getBackground());
			g.fillRect(0, 0, 8, 8);
			g.setColor(Color.BLACK);

			Polygon p = new Polygon();
			p.addPoint(0, 0);
			p.addPoint(8, 4);
			p.addPoint(0, 8);
			g.fillPolygon(p);

			g = img2.createGraphics();

			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			g.setColor(getBackground());
			g.fillRect(0, 0, 8, 8);
			g.setColor(Color.BLACK);

			p = new Polygon();
			p.addPoint(0, 0);
			p.addPoint(4, 8);
			p.addPoint(8, 0);
			g.fillPolygon(p);

			test.setIcon(new ImageIcon(img1));
			test.setSelectedIcon(new ImageIcon(img2));
			test.setPressedIcon(new ImageIcon(img1));
			test.setRolloverEnabled(false);
			test.setFocusPainted(false);
		}
	}

	class PlayerListItem extends JPanel {
		JLabel playerName;

		public PlayerListItem() {
			setLayout(new GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = 0;
			c.weightx = 1;
			c.fill = GridBagConstraints.HORIZONTAL;
			c.insets = new Insets(5, 5, 5, 5);

			playerName = new JLabel("<html>Horst<br>&gt; 9000 Steine");
			add(playerName, c);
		}

	}

	class PlayerListPanel extends JPanel {
		JPanel startSpacer;

		public PlayerListPanel() {
			setBackground(Color.GRAY);
			setLayout(new GridBagLayout());

			GridBagConstraints c = new GridBagConstraints();
			startSpacer = new JPanel();
			startSpacer.setBackground(Color.GRAY);
			startSpacer.setPreferredSize(new Dimension(0, 0));
			c.gridx = 0;
			c.gridy = 0;
			c.weightx = 1;
			c.weighty = 1;
			c.fill = GridBagConstraints.BOTH;
			add(startSpacer, c);
			c.weighty = 0;
			c.fill = GridBagConstraints.HORIZONTAL;

			for (int i = 1; i <= 4; i++) {
				c.gridx = 0;
				c.gridy = i;
				c.insets = new Insets(i == 1 ? 0 : 1, 0, 0, 0);
				add(new PlayerListItem(), c);
			}
		}

		@Override
		public Dimension getPreferredSize() {
			Dimension oldPref = super.getPreferredSize();
			return new Dimension(0, oldPref.height);
		}
	}

}