GameListPanel.niceness++ :D

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@538 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-21 04:23:21 +02:00
parent c708a0b648
commit 71aae25108
3 changed files with 83 additions and 37 deletions

View file

@ -41,7 +41,7 @@ public class MockView implements IView {
public boolean isLoginPanelVisible = false; public boolean isLoginPanelVisible = false;
/** */ /** */
public boolean isGameListPanelVisible = false; public boolean isGameListPanelVisible = false;
/** */ /** */
public boolean isQuitWarningPanelVisible = false; public boolean isQuitWarningPanelVisible = false;
/** */ /** */
@ -172,11 +172,11 @@ public class MockView implements IView {
@Override @Override
public void showSidePanel(boolean show) { public void showSidePanel(boolean show) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
public void showQuitWarningPanel(boolean show) { public void showQuitWarningPanel(boolean show) {
isQuitWarningPanelVisible =show; isQuitWarningPanelVisible = show;
} }
@Override @Override
@ -192,7 +192,7 @@ public class MockView implements IView {
@Override @Override
public void showConnectPanel(boolean show) { public void showConnectPanel(boolean show) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override @Override
@ -313,6 +313,6 @@ public class MockView implements IView {
@Override @Override
public void setMayPause(boolean mayPause) { public void setMayPause(boolean mayPause) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
} }

View file

@ -6,11 +6,12 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JList; import javax.swing.JList;
@ -20,6 +21,8 @@ import javax.swing.ListCellRenderer;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder; import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type; import jrummikub.model.PlayerSettings.Type;
@ -28,6 +31,7 @@ import jrummikub.util.Event1;
import jrummikub.util.GameData; import jrummikub.util.GameData;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
import jrummikub.util.IEvent1; import jrummikub.util.IEvent1;
import jrummikub.util.Pair;
import jrummikub.view.IGameListPanel; import jrummikub.view.IGameListPanel;
class GameListPanel extends JPanel implements IGameListPanel { class GameListPanel extends JPanel implements IGameListPanel {
@ -60,35 +64,66 @@ class GameListPanel extends JPanel implements IGameListPanel {
add(Box.createVerticalStrut(3), c); add(Box.createVerticalStrut(3), c);
gameList = new JList(); createGameList();
gameList.setCellRenderer(new GameDataCellRenderer());
c.weighty = 1;
add(new JScrollPane(gameList, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), c);
c.weighty = 0;
add(Box.createVerticalStrut(3), c); add(Box.createVerticalStrut(3), c);
addButtons(c); addButtons(c);
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(
new EmptyBorder(10, 10, 10, 10))); 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<Integer, Integer> 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) { private void addButtons(GridBagConstraints c) {
joinButton = new JButton("Beitreten"); joinButton = new JButton("Beitreten");
c.gridwidth = 1; c.gridwidth = 1;
add(joinButton, c);
joinButton.addActionListener(new ActionListener() { joinButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Object data = gameList.getSelectedValue(); join();
if (data instanceof GameData) {
joinEvent.emit((GameData) data);
}
} }
}); });
joinButton.setEnabled(false);
add(joinButton, c);
c.weightx = 0; c.weightx = 0;
add(Box.createHorizontalStrut(10), c); add(Box.createHorizontalStrut(10), c);
@ -99,7 +134,6 @@ class GameListPanel extends JPanel implements IGameListPanel {
openNewGameButton.addActionListener(new ActionListener() { openNewGameButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
openNewGameEvent.emit(); openNewGameEvent.emit();
} }
}); });
@ -135,14 +169,12 @@ class GameListPanel extends JPanel implements IGameListPanel {
Object currentGame = gameList.getSelectedValue(); Object currentGame = gameList.getSelectedValue();
DefaultListModel model = new DefaultListModel(); gameList.setListData(games.toArray());
for (GameData game : games) {
model.addElement(game);
}
gameList.setModel(model);
if (games.contains(currentGame)) { if (games.contains(currentGame)) {
gameList.setSelectedValue(currentGame, false); gameList.setSelectedValue(currentGame, false);
} else if (!games.isEmpty()) {
gameList.setSelectedIndex(0);
} }
} }
@ -161,6 +193,27 @@ class GameListPanel extends JPanel implements IGameListPanel {
return cancelEvent; return cancelEvent;
} }
private void join() {
Object data = gameList.getSelectedValue();
if (data instanceof GameData) {
joinEvent.emit((GameData) data);
}
}
private static Pair<Integer, Integer> 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<Integer, Integer>(occupied, total);
}
private static class GameDataCellRenderer extends JPanel implements private static class GameDataCellRenderer extends JPanel implements
ListCellRenderer { ListCellRenderer {
private static final long serialVersionUID = -892701906163443927L; private static final long serialVersionUID = -892701906163443927L;
@ -197,17 +250,10 @@ class GameListPanel extends JPanel implements IGameListPanel {
GameData gameData = (GameData) value; GameData gameData = (GameData) value;
host = gameData.getHost(); host = gameData.getHost();
int total = gameData.getGameSettings().getPlayerList().size(); Pair<Integer, Integer> playerSlotCount = getPlayerSlotCount(gameData);
int occupied = total;
for (PlayerSettings player : gameData.getGameSettings() playerCount = playerSlotCount.getFirst() + "/"
.getPlayerList()) { + playerSlotCount.getSecond();
if (player.getType() == Type.VACANT) {
occupied--;
}
}
playerCount = occupied + "/" + total;
} else { } else {
host = String.valueOf(value); host = String.valueOf(value);
} }

View file

@ -459,7 +459,7 @@ public class View extends JFrame implements IView {
rescaleSubpanel(settingsPanel, 1 / 2.0, 1 / 2.0, 475, 300); rescaleSubpanel(settingsPanel, 1 / 2.0, 1 / 2.0, 475, 300);
rescaleSubpanel(scorePanel, 3 / 4.0, 1 / 2.0, 450, 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(gameListPanel, 1 / 2.0, 1 / 2.0, 475, 300);
rescaleSubpanel(quitWarningPanel, 1 / 2.0, 1 / 6.0, 400, 150); rescaleSubpanel(quitWarningPanel, 1 / 2.0, 1 / 6.0, 400, 150);
quitWarningPanel.setLocation(0, 0); quitWarningPanel.setLocation(0, 0);