summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/GameListPanel.java
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2011-06-11 00:41:06 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2011-06-11 00:41:06 +0200
commite176dc46082c2e73daf82cafb5e6e10653db64eb (patch)
treed4b6b1cd0533c7a8d4bb2a6a2c93434d29bffe3e /src/jrummikub/view/impl/GameListPanel.java
parentcc4797fd8b894cc7e02346dc93f891cccb4c0a09 (diff)
downloadJRummikub-e176dc46082c2e73daf82cafb5e6e10653db64eb.tar
JRummikub-e176dc46082c2e73daf82cafb5e6e10653db64eb.zip
Display game list in GameListPanel
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@407 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub/view/impl/GameListPanel.java')
-rw-r--r--src/jrummikub/view/impl/GameListPanel.java108
1 files changed, 105 insertions, 3 deletions
diff --git a/src/jrummikub/view/impl/GameListPanel.java b/src/jrummikub/view/impl/GameListPanel.java
index d509a6e..eae424a 100644
--- a/src/jrummikub/view/impl/GameListPanel.java
+++ b/src/jrummikub/view/impl/GameListPanel.java
@@ -1,17 +1,23 @@
package jrummikub.view.impl;
import java.awt.Color;
+import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
+import java.awt.GridLayout;
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;
@@ -28,10 +34,13 @@ class GameListPanel extends JPanel implements IGameListPanel {
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();
@@ -41,15 +50,21 @@ class GameListPanel extends JPanel implements IGameListPanel {
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);
- joinButton = new JButton("Beitreten");
c.weighty = 0;
+ add(Box.createVerticalStrut(3), c);
+
+ joinButton = new JButton("Beitreten");
c.gridwidth = 1;
add(joinButton, c);
joinButton.addActionListener(new ActionListener() {
@@ -89,8 +104,43 @@ class GameListPanel extends JPanel implements IGameListPanel {
}
});
- setBorder(new CompoundBorder(new LineBorder(Color.BLACK),
- new EmptyBorder(10, 10, 10, 10)));
+ 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
@@ -108,4 +158,56 @@ class GameListPanel extends JPanel implements IGameListPanel {
return cancelEvent;
}
+ private static class GameDataCellRenderer extends JPanel implements
+ ListCellRenderer {
+ JLabel hostLabel, playerCountLabel;
+
+ GameDataCellRenderer() {
+ setLayout(new GridLayout(1, 2));
+ hostLabel = new JLabel();
+ hostLabel.setOpaque(true);
+ hostLabel.setHorizontalAlignment(JLabel.LEFT);
+ add(hostLabel);
+
+ playerCountLabel = new JLabel();
+ playerCountLabel.setOpaque(true);
+ playerCountLabel.setHorizontalAlignment(JLabel.RIGHT);
+ add(playerCountLabel);
+ }
+
+ @Override
+ public Component getListCellRendererComponent(JList list, Object value,
+ int index, boolean isSelected, boolean cellHasFocus) {
+ String host = "", playerCount = "";
+
+ if (value instanceof GameData) {
+ GameData gameData = (GameData) value;
+
+ host = gameData.getHost();
+ playerCount = gameData.getCurrentPlayerCount() + "/"
+ + gameData.getMaxPlayerCount();
+ } 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;
+ }
+ }
}