summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/SettingsPanel.java
blob: 1d3e1b54817dc3ca9d64b759c902355a028b6de8 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
package jrummikub.view.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import jrummikub.model.GameSettings;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.model.StoneColor;
import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.Event2;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.util.IEvent2;
import jrummikub.view.ISettingsPanel;

@SuppressWarnings("serial")
class SettingsPanel extends JPanel implements ISettingsPanel {
	private List<List<Type>> playerTypeChoices = Collections.emptyList();
	private List<Boolean> removeButtonsEnabled = Collections.emptyList();
	private List<Boolean> playerNamesEditable = Collections.emptyList();

	private JTabbedPane tabbedPane;
	private JPanel playerSetupPanel;
	private JPanel playerSettingsViewport;
	private JPanel optionsPanel;
	private JButton addPlayerButton;
	private JLabel errorMessageLabel;
	private JButton startButton;
	private JButton offerButton;

	private List<PlayerSettingsPanel> playerSettingsPanels = new ArrayList<PlayerSettingsPanel>();

	private JButton setVariantDefaultButton;
	private JButton setVariantChildrenButton;
	private JSpinner initialMeldThresholdSpinner;
	private JSpinner stoneSetNumberSpinner;
	private JSpinner highestValueSpinner;
	private JSpinner numberOfStonesDealtSpinner;
	private JSpinner jokerNumberSpinner;
	private JSpinner timeSpinner;
	private JCheckBox noLimitsBox;
	private JButton loadButton;
	private JButton networkButton;
	private JButton backButton;
	private JPanel buttonPanel;

	private JPanel colorSelectionPanel;
	private Map<StoneColor, JToggleButton> colorButtons = new HashMap<StoneColor, JToggleButton>();

	private Event startGameEvent = new Event();
	private Event loadGameEvent = new Event();
	private Event networkGameEvent = new Event();
	private Event offerGameEvent = new Event();
	private Event backEvent = new Event();
	private Event addPlayerEvent = new Event();
	private Event1<Integer> removePlayerEvent = new Event1<Integer>();
	private Event2<Integer, Color> changePlayerColorEvent = new Event2<Integer, Color>();
	private Event2<Integer, String> changePlayerNameEvent = new Event2<Integer, String>();
	private Event2<Integer, Type> changePlayerTypeEvent = new Event2<Integer, Type>();
	private Event1<Integer> changeInitialMeldThresholdEvent = new Event1<Integer>();
	private Event1<Integer> changeJokerNumberEvent = new Event1<Integer>();
	private Event1<Integer> changeStoneSetNumberEvent = new Event1<Integer>();
	private Event1<Integer> changeNumberOfStonesDealtEvent = new Event1<Integer>();
	private Event1<Integer> changeHighestValueEvent = new Event1<Integer>();
	private Event1<Set<StoneColor>> changeStoneColorsEvent = new Event1<Set<StoneColor>>();
	private Event1<Integer> changeTimeEvent = new Event1<Integer>();
	private Event1<Boolean> changeNoLimitsEvent = new Event1<Boolean>();
	private Event setVariantDefaultEvent = new Event();
	private Event setVariantChildrenEvent = new Event();

	@Override
	public IEvent getStartGameEvent() {
		return startGameEvent;
	}

	@Override
	public IEvent getOfferGameEvent() {
		return offerGameEvent;
	}

	IEvent getLoadGameEvent() {
		return loadGameEvent;
	}

	IEvent getNetworkGameEvent() {
		return networkGameEvent;
	}

	@Override
	public IEvent getAddPlayerEvent() {
		return addPlayerEvent;
	}

	@Override
	public IEvent1<Integer> getRemovePlayerEvent() {
		return removePlayerEvent;
	}

	@Override
	public IEvent2<Integer, Color> getChangePlayerColorEvent() {
		return changePlayerColorEvent;
	}

	@Override
	public IEvent2<Integer, String> getChangePlayerNameEvent() {
		return changePlayerNameEvent;
	}

	@Override
	public IEvent1<Integer> getChangeInitialMeldThresholdEvent() {
		return changeInitialMeldThresholdEvent;
	}

	@Override
	public IEvent1<Integer> getChangeStoneSetNumberEvent() {
		return changeStoneSetNumberEvent;
	}

	@Override
	public IEvent1<Integer> getChangeNumberOfStonesDealtEvent() {
		return changeNumberOfStonesDealtEvent;
	}

	@Override
	public IEvent1<Integer> getChangeHighestValueEvent() {
		return changeHighestValueEvent;
	}

	@Override
	public IEvent1<Integer> getChangeJokerNumberEvent() {
		return changeJokerNumberEvent;
	}

	@Override
	public IEvent2<Integer, Type> getChangePlayerTypeEvent() {
		return changePlayerTypeEvent;
	}

	@Override
	public IEvent1<Integer> getChangeTimeEvent() {
		return changeTimeEvent;
	}

	@Override
	public IEvent1<Boolean> getChangeNoLimitsEvent() {
		return changeNoLimitsEvent;
	}

	@Override
	public IEvent1<Set<StoneColor>> getChangeStoneColorsEvent() {
		return changeStoneColorsEvent;
	}

	@Override
	public IEvent getSetVariantDefaultEvent() {
		return setVariantDefaultEvent;
	}

	@Override
	public IEvent getSetVariantChildrenEvent() {
		return setVariantChildrenEvent;
	}

	@Override
	public IEvent getBackEvent() {
		return backEvent;
	}

	@Override
	public void setPlayerTypeChoices(List<List<Type>> choices) {
		playerTypeChoices = choices;
	}

	@Override
	public void setError(SettingsError error) {
		switch (error) {
			case NO_ERROR:
				errorMessageLabel.setText(" ");
				break;
			case DUPLICATE_PLAYER_NAME_ERROR:
				errorMessageLabel
						.setText("Jeder Spielername darf nur einmal verwendet werden.");
				errorMessageLabel.setForeground(Color.RED);
				break;
			case NO_PLAYER_NAME_ERROR:
				errorMessageLabel.setText("Jeder Spieler muss einen Namen haben.");
				errorMessageLabel.setForeground(Color.RED);
				break;
			case NOT_ENOUGH_STONES_ERROR:
				errorMessageLabel
						.setText("Es gibt nicht genug Steine f\u00fcr die gew\u00e4hlte Spieleranzahl.");
				errorMessageLabel.setForeground(Color.RED);
				break;
			case NOT_ENOUGH_COLORS_ERROR:
				errorMessageLabel
						.setText("Es m\u00fcssen mindestens drei Farben ausgew\u00e4hlt werden.");
				errorMessageLabel.setForeground(Color.RED);
				break;
			case COMPUTER_PLAYERS_ONLY_WARNING:
				errorMessageLabel.setText("Es gibt keinen menschlichen Spieler.");
				errorMessageLabel.setForeground(Color.ORANGE.darker());
				break;
			case TOO_HIGH_THRESHOLD_WARNING:
				errorMessageLabel
						.setText("Die gew\u00e4hlte Schranke ist m\u00f6glicherweise zu hoch");
				errorMessageLabel.setForeground(Color.ORANGE.darker());
				break;
		}

	}

	@Override
	public void enableStartGameButton(boolean enable) {
		startButton.setEnabled(enable);
	}

	@Override
	public void enableAddPlayerButton(boolean enable) {
		addPlayerButton.setVisible(enable);
	}

	@Override
	public void enableRemovePlayerButtons(List<Boolean> enable) {
		removeButtonsEnabled = enable;
		Iterator<Boolean> it = enable.iterator();
		for (PlayerSettingsPanel psp : playerSettingsPanels) {
			if (!it.hasNext()) {
				psp.enableRemoveButton(false);
			} else {
				psp.enableRemoveButton(it.next());
			}
		}
	}

	@Override
	public void setPlayerNamesEditable(List<Boolean> editable) {
		playerNamesEditable = editable;

		Iterator<Boolean> it = editable.iterator();
		for (PlayerSettingsPanel psp : playerSettingsPanels) {
			if (!it.hasNext()) {
				psp.setPlayerNameEditable(true);
			} else {
				psp.setPlayerNameEditable(it.next());
			}
		}
	}

	@Override
	public void setGameSettings(GameSettings gameSettings) {
		while (playerSettingsPanels.size() > gameSettings.getPlayerList().size()) {
			removePlayerSettingsPanel();
		}

		for (int i = 0; i < gameSettings.getPlayerList().size(); ++i) {
			List<Type> choices = Collections.emptyList();
			if (i < playerTypeChoices.size()) {
				choices = playerTypeChoices.get(i);
			}
			updatePlayerSettingsPanel(i, gameSettings.getPlayerList().get(i), choices);
		}

		initialMeldThresholdSpinner
				.setValue(gameSettings.getInitialMeldThreshold());
		stoneSetNumberSpinner.setValue(gameSettings.getStoneSetNumber());
		highestValueSpinner.setValue(gameSettings.getHighestValue());
		numberOfStonesDealtSpinner.setValue(gameSettings.getNumberOfStonesDealt());
		jokerNumberSpinner.setValue(gameSettings.getJokerNumber());
		timeSpinner.setValue(gameSettings.getTotalTime());
		noLimitsBox.setSelected(gameSettings.isNoLimits());

		for (StoneColor color : StoneColor.values()) {
			colorButtons.get(color).getModel()
					.setSelected(gameSettings.getStoneColors().contains(color));
		}

		playerSettingsViewport.revalidate();
	}

	private void updatePlayerSettingsPanel(int i, PlayerSettings settings,
			List<Type> choices) {
		if (i < playerSettingsPanels.size()) {
			playerSettingsPanels.get(i).setChoices(choices);
			playerSettingsPanels.get(i).setSettings(settings);
		} else {
			addPlayerSettingsPanel(i, settings, choices);
		}
	}

	private void addPlayerSettingsPanel(int i, PlayerSettings settings,
			List<Type> choices) {
		PlayerSettingsPanel panel = new PlayerSettingsPanel(i, settings, choices);
		playerSettingsPanels.add(panel);
		playerSettingsViewport.add(panel,
				playerSettingsViewport.getComponentCount() - 1);
		enableRemovePlayerButtons(removeButtonsEnabled);
		setPlayerNamesEditable(playerNamesEditable);
	}

	private void removePlayerSettingsPanel() {
		PlayerSettingsPanel p = playerSettingsPanels.remove(playerSettingsPanels
				.size() - 1);
		playerSettingsViewport.remove(p);
	}

	private void createPlayerSetupPanel() {
		playerSetupPanel = new JPanel();
		playerSetupPanel.setLayout(new BorderLayout());

		playerSettingsViewport = new JPanel();
		playerSettingsViewport.setLayout(new BoxLayout(playerSettingsViewport,
				BoxLayout.Y_AXIS));

		JScrollPane scrollPane = new JScrollPane(playerSettingsViewport,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		playerSetupPanel.add(scrollPane);

		JPanel addPlayerPanel = new JPanel();
		addPlayerPanel.setLayout(new FlowLayout(FlowLayout.TRAILING, 0, 2));
		playerSettingsViewport.add(addPlayerPanel);

		addPlayerButton = new JButton("+");
		addPlayerButton.setFont(addPlayerButton.getFont().deriveFont(Font.BOLD));
		addPlayerButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				addPlayerEvent.emit();
			}
		});
		addPlayerPanel.add(addPlayerButton);
	}

	private JComponent makeFiller() {
		return new Box.Filler(new Dimension(0, 0), new Dimension(0, 0),
				new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
	}

	private void createOptionsPanel() {
		optionsPanel = new JPanel();
		GridBagLayout layout = new GridBagLayout();
		optionsPanel.setLayout(layout);

		int row = 0;

		makeOptionLabel(row, "Variante:");
		makeVariantPanel(row++);

		makeOptionLabel(row, "Auslegeschranke:");
		initialMeldThresholdSpinner = makeOptionSpinner(row++, 1, 999, 1,
				changeInitialMeldThresholdEvent);

		makeOptionLabel(row, "Anzahl Steins\u00e4tze:");
		stoneSetNumberSpinner = makeOptionSpinner(row++, 1, 999, 1,
				changeStoneSetNumberEvent);

		makeOptionLabel(row, "H\u00f6chster Steinwert:");
		highestValueSpinner = makeOptionSpinner(row++, 3, 99, 1,
				changeHighestValueEvent);

		makeOptionLabel(row, "Anzahl Startsteine:");
		numberOfStonesDealtSpinner = makeOptionSpinner(row++, 1, 999, 1,
				changeNumberOfStonesDealtEvent);

		makeOptionLabel(row, "Jokeranzahl:");
		jokerNumberSpinner = makeOptionSpinner(row++, 1, 999, 1,
				changeJokerNumberEvent);

		makeOptionLabel(row, "Zeit für Spielzug:");
		timeSpinner = makeOptionSpinner(row++, 1, 999, 1, changeTimeEvent);

		makeOptionLabel(row, "No Limits:");
		noLimitsBox = makeOptionCheckbox(row++, changeNoLimitsEvent);

		makeOptionLabel(row, "Steinfarben:");
		createColorSelectionPanel(row++);

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = row;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		optionsPanel.add(makeFiller(), c);
	}

	private void createColorSelectionPanel(int row) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.WEST;
		c.gridx = 1;
		c.gridy = row;

		colorSelectionPanel = new JPanel();
		optionsPanel.add(colorSelectionPanel, c);

		for (StoneColor color : StoneColor.values()) {
			createColorButton(color);
		}
	}

	private void createColorButton(final StoneColor color) {
		JToggleButton button = new JToggleButton(ImageUtil.createColorIcon(
				StonePainter.getColor(color), 12, 1));
		button.setMargin(new Insets(3, 3, 3, 3));
		button.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				emitColorChangeEvent();
			}
		});

		colorSelectionPanel.add(button);

		colorButtons.put(color, button);
	}

	private void emitColorChangeEvent() {
		Set<StoneColor> colors = new HashSet<StoneColor>();

		for (StoneColor color : StoneColor.values()) {
			if (colorButtons.get(color).getModel().isSelected()) {
				colors.add(color);
			}
		}

		changeStoneColorsEvent.emit(colors);
	}

	private void makeVariantPanel(int row) {
		JPanel panel = makeOptionSubpanel(row, new GridLayout(1, 2, 5, 0));

		setVariantDefaultButton = new JButton("Standard");
		panel.add(setVariantDefaultButton);
		setVariantDefaultButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVariantDefaultEvent.emit();
			}
		});
		setVariantChildrenButton = new JButton("Kinder");
		panel.add(setVariantChildrenButton);
		setVariantChildrenButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setVariantChildrenEvent.emit();
			}
		});
	}

	private JPanel makeOptionSubpanel(int row, LayoutManager layout) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.WEST;
		c.gridx = 1;
		c.gridy = row;
		c.insets = new Insets(2, 2, 2, 2);
		JPanel panel = new JPanel(layout);
		optionsPanel.add(panel, c);
		return panel;
	}

	private JCheckBox makeOptionCheckbox(int row,
			final Event1<Boolean> targetEvent) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.WEST;
		c.gridx = 1;
		c.gridy = row;
		c.insets = new Insets(2, 2, 2, 2);
		final JCheckBox box = new JCheckBox();
		box.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				targetEvent.emit(box.isSelected());
			}
		});
		optionsPanel.add(box, c);
		return box;
	}

	private JSpinner makeOptionSpinner(int row, int min, int max, int step,
			final Event1<Integer> targetEvent) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.WEST;
		c.gridx = 1;
		c.gridy = row;
		c.insets = new Insets(2, 2, 2, 2);
		final JSpinner spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(min, min, max, step));
		spinner
				.setPreferredSize(new Dimension(60, spinner.getMinimumSize().height));
		spinner.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				targetEvent.emit((Integer) spinner.getValue());
			}
		});
		optionsPanel.add(spinner, c);
		return spinner;
	}

	private void makeOptionLabel(int row, String name) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		c.gridx = 0;
		c.gridy = row;
		c.insets = new Insets(4, 4, 4, 4);

		JLabel label = new JLabel(name);
		optionsPanel.add(label, c);
	}

	SettingsPanel() {
		setLayout(new GridBagLayout());

		tabbedPane = new JTabbedPane();

		createPlayerSetupPanel();
		tabbedPane.addTab("Spieler", playerSetupPanel);

		createOptionsPanel();
		JScrollPane scrollPane = new JScrollPane(optionsPanel,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		tabbedPane.addTab("Optionen", scrollPane);

		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.weightx = 1;
		c.weighty = 1;
		add(tabbedPane, c);

		errorMessageLabel = new JLabel(" ");
		c.weighty = 0;
		add(errorMessageLabel, c);

		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridBagLayout());
		add(buttonPanel, c);

		startButton = createButton("Spiel starten", startGameEvent);
		loadButton = createButton("Spiel laden...", loadGameEvent);
		networkButton = createButton("Netzwerkspiel...", networkGameEvent);

		offerButton = createButton("Spiel anbieten", offerGameEvent);
		backButton = createButton("Zur\u00fcck", backEvent);

		addDefaultButtons();

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

	void resetTabbedPane() {
		tabbedPane.setSelectedIndex(0);
	}

	private void addDefaultButtons() {
		buttonPanel.removeAll();
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = 1;
		c.weightx = 1;
		c.weighty = 1;

		buttonPanel.add(startButton, c);
		c.weightx = 0;
		buttonPanel.add(Box.createHorizontalStrut(10), c);
		c.weightx = 1;
		buttonPanel.add(loadButton, c);
		c.weightx = 0;
		buttonPanel.add(Box.createHorizontalStrut(10), c);
		c.weightx = 1;
		c.gridwidth = GridBagConstraints.REMAINDER;
		buttonPanel.add(networkButton, c);
	}

	private void addNetworkButtons() {
		buttonPanel.removeAll();
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = 1;
		c.weightx = 1;
		c.weighty = 1;

		buttonPanel.add(offerButton, c);
		c.weightx = 0;
		buttonPanel.add(Box.createHorizontalStrut(10), c);
		c.weightx = 1;
		c.gridwidth = GridBagConstraints.REMAINDER;
		buttonPanel.add(backButton, c);
	}

	private JButton createButton(String title, final Event event) {
		JButton button = new JButton(title);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				event.emit();
			}
		});
		return button;
	}

	@Override
	public void enableNetworkMode(boolean enable) {
		if (enable) {
			addNetworkButtons();
		} else {
			addDefaultButtons();
		}
	}

	private class PlayerSettingsPanel extends JPanel {
		private int playerNumber;
		private JButton colorButton;
		private JTextField nameField;
		private JButton removeButton;
		private JComboBox playerType;
		private List<Type> playerTypeChoices;

		public void setSettings(PlayerSettings settings) {
			if (!nameField.getText().equals(settings.getName())) {
				nameField.setText(settings.getName());
			}
			int index = playerTypeChoices.indexOf(settings.getType());
			if (index != playerType.getSelectedIndex() && index >= 0
					&& index < playerType.getModel().getSize()) {
				playerType.setSelectedIndex(index);
			}
			colorButton
					.setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2));
		}

		public void setPlayerNameEditable(boolean editable) {
			nameField.setEditable(editable);
		}

		public void enableRemoveButton(boolean enable) {
			removeButton.setEnabled(enable);
		}

		class NameChangeListener implements DocumentListener {
			@Override
			public void insertUpdate(DocumentEvent e) {
				changeName();
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				changeName();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				changeName();
			}
		}

		public PlayerSettingsPanel(int playerNumber, PlayerSettings settings,
				List<Type> choices) {
			setLayout(new GridBagLayout());

			this.playerNumber = playerNumber;
			createControls(settings);
			setChoices(choices);

			setSettings(settings);
		}

		public void setChoices(List<Type> playerTypeChoices) {
			this.playerTypeChoices = playerTypeChoices;

			Vector<String> choices = new Vector<String>();
			for (Type t : playerTypeChoices) {
				switch (t) {
					case COMPUTER:
						choices.add("Computer");
						break;
					case HUMAN:
						choices.add("Mensch");
						break;
					case NETWORK:
						choices.add("Netzwerk");
						break;
				}
			}
			playerType.setModel(new DefaultComboBoxModel(choices));
		}

		private void createControls(PlayerSettings settings) {
			GridBagConstraints c = new GridBagConstraints();
			GridBagConstraints c2 = new GridBagConstraints();
			colorButton = new JButton();
			nameField = new JTextField(settings.getName());

			colorButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					new ColorMenu();
				}
			});
			c.gridy = 0;
			add(colorButton, c);

			nameField.getDocument().addDocumentListener(new NameChangeListener());
			c2.gridy = 0;
			c2.weightx = 1;
			c2.fill = GridBagConstraints.HORIZONTAL;
			add(nameField, c2);

			playerType = new JComboBox();
			playerType.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					changeType();
				}
			});
			playerType.setPreferredSize(new Dimension(120, playerType
					.getPreferredSize().height));
			playerType.setMinimumSize(new Dimension(120,
					playerType.getMinimumSize().height));
			add(playerType, c);

			removeButton = new JButton("\u00d7");
			removeButton.setFont(removeButton.getFont().deriveFont(Font.BOLD));
			removeButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					remove();
				}
			});

			add(removeButton, c);
		}

		@Override
		public Dimension getPreferredSize() {
			Dimension preferredSize = super.getPreferredSize();
			for (Component component : this.getComponents()) {
				preferredSize
						.setSize(preferredSize.width, Math.max(preferredSize.height,
								component.getPreferredSize().height));
			}
			return preferredSize;
		}

		@Override
		public Dimension getMaximumSize() {
			Dimension preferredSize = super.getPreferredSize();
			for (Component component : this.getComponents()) {
				preferredSize
						.setSize(Integer.MAX_VALUE, Math.max(preferredSize.height,
								component.getPreferredSize().height));
			}
			return preferredSize;
		}

		private void changeName() {
			changePlayerNameEvent.emit(playerNumber, nameField.getText());
		}

		private void changeColor(Color c) {
			changePlayerColorEvent.emit(playerNumber, c);
		}

		private void changeType() {
			changePlayerTypeEvent.emit(playerNumber,
					playerTypeChoices.get(playerType.getSelectedIndex()));
		}

		private void remove() {
			removePlayerEvent.emit(playerNumber);
		}

		private class ColorMenu extends JPanel {
			ColorMenu() {
				setLayout(new GridLayout(4, 4, 1, 1));

				JPopupMenu menu = new JPopupMenu();

				for (Color c : PLAYER_COLORS) {
					add(new ColorButton(c, menu));
				}

				menu.setLayout(new BorderLayout());
				menu.add(this);
				menu.show(colorButton, 0, colorButton.getHeight());
			}

			private class ColorButton extends JLabel {
				ColorButton(final Color c, final JPopupMenu menu) {
					final Icon normalIcon = ImageUtil.createColorIcon(c, 16, 1);
					final Icon hoverIcon = ImageUtil.createColorIcon(c, 16, 2);

					setIcon(normalIcon);

					addMouseListener(new MouseAdapter() {
						@Override
						public void mouseEntered(MouseEvent e) {
							setIcon(hoverIcon);
						}

						@Override
						public void mouseExited(MouseEvent e) {
							setIcon(normalIcon);
						}

						@Override
						public void mouseClicked(MouseEvent e) {
							menu.setVisible(false);
							changeColor(c);
						}
					});
				}
			}
		}
	}

}