From 71aae2510862a4db08395079a37e1d3ddb9df5ce Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 21 Jun 2011 04:23:21 +0200 Subject: GameListPanel.niceness++ :D git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@538 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/view/impl/GameListPanel.java | 108 ++++++++++++++++++++--------- src/jrummikub/view/impl/View.java | 2 +- 2 files changed, 78 insertions(+), 32 deletions(-) (limited to 'src/jrummikub/view/impl') diff --git a/src/jrummikub/view/impl/GameListPanel.java b/src/jrummikub/view/impl/GameListPanel.java index e1e1c9d..d5328af 100644 --- a/src/jrummikub/view/impl/GameListPanel.java +++ b/src/jrummikub/view/impl/GameListPanel.java @@ -6,11 +6,12 @@ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.util.Collections; import java.util.List; import javax.swing.Box; -import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JList; @@ -20,6 +21,8 @@ import javax.swing.ListCellRenderer; import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; @@ -28,6 +31,7 @@ import jrummikub.util.Event1; import jrummikub.util.GameData; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; +import jrummikub.util.Pair; import jrummikub.view.IGameListPanel; class GameListPanel extends JPanel implements IGameListPanel { @@ -60,35 +64,66 @@ class GameListPanel extends JPanel implements IGameListPanel { 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); + createGameList(); - c.weighty = 0; add(Box.createVerticalStrut(3), c); addButtons(c); - 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))); + } + + private void createGameList() { + GridBagConstraints c = new GridBagConstraints(); + c.fill = GridBagConstraints.BOTH; + c.gridwidth = GridBagConstraints.REMAINDER; + c.weightx = 1; + c.weighty = 1; + + gameList = new JList(); + gameList.setCellRenderer(new GameDataCellRenderer()); + + gameList.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (e.getClickCount() == 2) { + join(); + } + } + }); + + gameList.addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + Object data = gameList.getSelectedValue(); + + if (data instanceof GameData) { + GameData gameData = (GameData) data; + Pair playerSlotCount = getPlayerSlotCount(gameData); + joinButton.setEnabled(playerSlotCount.getFirst() < playerSlotCount + .getSecond()); + } else { + joinButton.setEnabled(false); + } + } + }); + + add(new JScrollPane(gameList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, + JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), c); } private void addButtons(GridBagConstraints c) { joinButton = new JButton("Beitreten"); c.gridwidth = 1; - add(joinButton, c); joinButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { - Object data = gameList.getSelectedValue(); - - if (data instanceof GameData) { - joinEvent.emit((GameData) data); - } + join(); } }); + joinButton.setEnabled(false); + add(joinButton, c); c.weightx = 0; add(Box.createHorizontalStrut(10), c); @@ -99,7 +134,6 @@ class GameListPanel extends JPanel implements IGameListPanel { openNewGameButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { - // TODO Auto-generated method stub openNewGameEvent.emit(); } }); @@ -135,14 +169,12 @@ class GameListPanel extends JPanel implements IGameListPanel { Object currentGame = gameList.getSelectedValue(); - DefaultListModel model = new DefaultListModel(); - for (GameData game : games) { - model.addElement(game); - } - gameList.setModel(model); + gameList.setListData(games.toArray()); if (games.contains(currentGame)) { gameList.setSelectedValue(currentGame, false); + } else if (!games.isEmpty()) { + gameList.setSelectedIndex(0); } } @@ -161,6 +193,27 @@ class GameListPanel extends JPanel implements IGameListPanel { return cancelEvent; } + private void join() { + Object data = gameList.getSelectedValue(); + + if (data instanceof GameData) { + joinEvent.emit((GameData) data); + } + } + + private static Pair getPlayerSlotCount(GameData gameData) { + int total = gameData.getGameSettings().getPlayerList().size(); + int occupied = total; + + for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) { + if (player.getType() == Type.VACANT) { + occupied--; + } + } + + return new Pair(occupied, total); + } + private static class GameDataCellRenderer extends JPanel implements ListCellRenderer { private static final long serialVersionUID = -892701906163443927L; @@ -197,17 +250,10 @@ class GameListPanel extends JPanel implements IGameListPanel { GameData gameData = (GameData) value; host = gameData.getHost(); - int total = gameData.getGameSettings().getPlayerList().size(); - int occupied = total; - - for (PlayerSettings player : gameData.getGameSettings() - .getPlayerList()) { - if (player.getType() == Type.VACANT) { - occupied--; - } - } + Pair playerSlotCount = getPlayerSlotCount(gameData); - playerCount = occupied + "/" + total; + playerCount = playerSlotCount.getFirst() + "/" + + playerSlotCount.getSecond(); } else { host = String.valueOf(value); } diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index 69d0d25..5d870f9 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -459,7 +459,7 @@ public class View extends JFrame implements IView { rescaleSubpanel(settingsPanel, 1 / 2.0, 1 / 2.0, 475, 300); rescaleSubpanel(scorePanel, 3 / 4.0, 1 / 2.0, 450, 300); - rescaleSubpanel(loginPanel, 1 / 3.0, 1 / 3.0, 200, 200); + rescaleSubpanel(loginPanel, 1 / 3.0, 1 / 3.0, 350, 200); rescaleSubpanel(gameListPanel, 1 / 2.0, 1 / 2.0, 475, 300); rescaleSubpanel(quitWarningPanel, 1 / 2.0, 1 / 6.0, 400, 150); quitWarningPanel.setLocation(0, 0); -- cgit v1.2.3