Display game list in GameListPanel

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@407 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-11 00:41:06 +02:00
parent cc4797fd8b
commit e176dc4608
5 changed files with 173 additions and 42 deletions

View file

@ -26,4 +26,22 @@ public class MockGameListPanel implements IGameListPanel {
return joinEvent;
}
@Override
public void removeGame(GameData game) {
// TODO Auto-generated method stub
}
@Override
public void addGame(GameData game) {
// TODO Auto-generated method stub
}
@Override
public void setChannelName(String name) {
// TODO Auto-generated method stub
}
}

View file

@ -2,6 +2,7 @@ package jrummikub.control.network;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import jrummikub.util.Connection;
import jrummikub.util.Event;
@ -19,17 +20,22 @@ public class NetworkControl {
private List<Connection> connections = new ArrayList<Connection>();
private Event stopNetworkEvent = new Event();
public NetworkControl(LoginData loginData, final IView view) {
public NetworkControl(final LoginData loginData, final IView view) {
this.view = view;
connectionControl = new ConnectionControl(loginData);
connections.add(connectionControl.getConnectedEvent().add(
new IListener() {
@Override
public void handle() {
view.showGameListPanel(true);
}
}));
connections.add(connectionControl.getConnectedEvent().add(new IListener() {
@Override
public void handle() {
view.getGameListPanel().setChannelName(loginData.getChannelName());
view.showGameListPanel(true);
GameData testData = new GameData(UUID.randomUUID(), "NeoRaider");
testData.setCurrentPlayerCount(2);
testData.setMaxPlayerCount(4);
view.getGameListPanel().addGame(testData);
}
}));
connections.add(connectionControl.getConnectionFailedEvent().add(
new IListener() {

View file

@ -2,11 +2,8 @@ package jrummikub.view;
import java.util.UUID;
import jrummikub.util.Event;
import jrummikub.util.Event1;
import jrummikub.util.IEvent;
import jrummikub.util.IEvent1;
import jrummikub.view.IGameListPanel.GameData;
public interface IGameListPanel {
public static class GameData {
@ -51,4 +48,10 @@ public interface IGameListPanel {
public IEvent getCancelEvent();
public IEvent1<GameData> getJoinEvent();
}
public void removeGame(GameData game);
public void addGame(GameData game);
public void setChannelName(String name);
}

View file

@ -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;
}
}
}

View file

@ -154,8 +154,7 @@ public class View extends JFrame implements IView {
showSettingsPanel(false);
showLoginPanel(false);
showGameListPanel(false);
getHandPanel().setStones(
Collections.<Pair<Stone, Position>> emptyList());
getHandPanel().setStones(Collections.<Pair<Stone, Position>> emptyList());
getTablePanel().setStoneSets(
Collections.<Pair<StoneSet, Position>> emptyList());
setSelectedStones(Collections.<Stone> emptyList());
@ -306,8 +305,8 @@ public class View extends JFrame implements IView {
mainLayer.add(table);
playerPanel = new PlayerPanel();
playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0,
0, Color.BLACK));
playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0,
Color.BLACK));
mainLayer.add(playerPanel);
startTurnPanel = new StartTurnPanel();
@ -380,6 +379,10 @@ public class View extends JFrame implements IView {
@Override
public void showGameListPanel(boolean show) {
if (show) {
gameListPanel.reset();
}
gameListPanel.setVisible(show);
}
@ -427,24 +430,24 @@ public class View extends JFrame implements IView {
@SuppressWarnings("unchecked")
private List<Pair<Stone, Position>> createDecorationStones() {
Pair<Stone, Position> stoneJ = new Pair<Stone, Position>(new Stone(
-'J', StoneColor.BLACK), new Position(2.5f, 0));
Pair<Stone, Position> stoneR = new Pair<Stone, Position>(new Stone(
-'R', StoneColor.ORANGE), new Position(3.5f, 0));
Pair<Stone, Position> stoneu1 = new Pair<Stone, Position>(new Stone(
-'u', StoneColor.BLUE), new Position(4.5f, 0));
Pair<Stone, Position> stonem1 = new Pair<Stone, Position>(new Stone(
-'m', StoneColor.RED), new Position(5.5f, 0));
Pair<Stone, Position> stonem2 = new Pair<Stone, Position>(new Stone(
-'m', StoneColor.GREEN), new Position(6.5f, 0));
Pair<Stone, Position> stonei = new Pair<Stone, Position>(new Stone(
-'i', StoneColor.VIOLET), new Position(7.5f, 0));
Pair<Stone, Position> stonek = new Pair<Stone, Position>(new Stone(
-'k', StoneColor.AQUA), new Position(8.5f, 0));
Pair<Stone, Position> stoneu2 = new Pair<Stone, Position>(new Stone(
-'u', StoneColor.GRAY), new Position(9.5f, 0));
Pair<Stone, Position> stoneb = new Pair<Stone, Position>(new Stone(
-'b', StoneColor.BLACK), new Position(10.5f, 0));
Pair<Stone, Position> stoneJ = new Pair<Stone, Position>(new Stone(-'J',
StoneColor.BLACK), new Position(2.5f, 0));
Pair<Stone, Position> stoneR = new Pair<Stone, Position>(new Stone(-'R',
StoneColor.ORANGE), new Position(3.5f, 0));
Pair<Stone, Position> stoneu1 = new Pair<Stone, Position>(new Stone(-'u',
StoneColor.BLUE), new Position(4.5f, 0));
Pair<Stone, Position> stonem1 = new Pair<Stone, Position>(new Stone(-'m',
StoneColor.RED), new Position(5.5f, 0));
Pair<Stone, Position> stonem2 = new Pair<Stone, Position>(new Stone(-'m',
StoneColor.GREEN), new Position(6.5f, 0));
Pair<Stone, Position> stonei = new Pair<Stone, Position>(new Stone(-'i',
StoneColor.VIOLET), new Position(7.5f, 0));
Pair<Stone, Position> stonek = new Pair<Stone, Position>(new Stone(-'k',
StoneColor.AQUA), new Position(8.5f, 0));
Pair<Stone, Position> stoneu2 = new Pair<Stone, Position>(new Stone(-'u',
StoneColor.GRAY), new Position(9.5f, 0));
Pair<Stone, Position> stoneb = new Pair<Stone, Position>(new Stone(-'b',
StoneColor.BLACK), new Position(10.5f, 0));
Pair<Stone, Position> stone1 = new Pair<Stone, Position>(new Stone(
StoneColor.RED), new Position(2, 1));
@ -459,9 +462,9 @@ public class View extends JFrame implements IView {
Pair<Stone, Position> stone6 = new Pair<Stone, Position>(new Stone(
StoneColor.BLACK), new Position(11, 1));
return Arrays.asList(stoneJ, stoneR, stoneu1, stonem1, stonem2, stonei,
stonek, stoneu2, stoneb, stone1, stone2, stone3, stone4,
stone5, stone6);
return Arrays
.asList(stoneJ, stoneR, stoneu1, stonem1, stonem2, stonei, stonek,
stoneu2, stoneb, stone1, stone2, stone3, stone4, stone5, stone6);
}
@Override
@ -478,8 +481,7 @@ public class View extends JFrame implements IView {
&& type != BottomPanelType.WIN_PANEL && type != null);
if (type == BottomPanelType.START_GAME_PANEL) {
table.setStoneSets(Collections
.<Pair<StoneSet, Position>> emptyList());
table.setStoneSets(Collections.<Pair<StoneSet, Position>> emptyList());
playerPanel.getHandPanel().setStones(createDecorationStones());
}