Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline LobbyGameTable class #12154

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import games.strategy.engine.lobby.client.ui.action.FetchChatHistory;
import games.strategy.engine.lobby.client.ui.action.ShowPlayersAction;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
Expand All @@ -20,19 +20,24 @@
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter;
import javax.swing.SortOrder;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableRowSorter;
import org.triplea.lobby.common.GameDescription;
import org.triplea.swing.MouseListenerBuilder;
import org.triplea.swing.SwingAction;

class LobbyGamePanel extends JPanel {
private static final long serialVersionUID = -2576314388949606337L;
private final JFrame parent;
private final JButton joinGame;
private final JButton joinGameButton;
private final LobbyGameTableModel gameTableModel;
private final LobbyClient lobbyClient;
private final JTable gameTable;
private final URI lobbyUri;
private final JTable gameTable;

LobbyGamePanel(
final JFrame parent,
Expand All @@ -44,10 +49,46 @@ class LobbyGamePanel extends JPanel {
this.gameTableModel = lobbyGameTableModel;
this.lobbyUri = lobbyUri;

final JButton hostGame = new JButton("Host Game");
joinGame = new JButton("Join Game");
final JButton hostGameButton = new JButton("Host Game");
joinGameButton = new JButton("Join Game");

gameTable =
new JTable(gameTableModel) {
@Override
// Custom renderer to show 'bot' rows in italic font
public Component prepareRenderer(
final TableCellRenderer renderer, final int rowIndex, final int colIndex) {

final Component component = super.prepareRenderer(renderer, rowIndex, colIndex);
final GameDescription gameDescription =
lobbyGameTableModel.get(convertRowIndexToModel(rowIndex));
component.setFont(
gameDescription.isBot()
? UIManager.getDefaults().getFont("Table.font").deriveFont(Font.ITALIC)
: UIManager.getDefaults().getFont("Table.font"));
return component;
}
};
gameTable
.getSelectionModel()
.addListSelectionListener(
e -> {
final boolean selected = gameTable.getSelectedRow() >= 0;
joinGameButton.setEnabled(selected);
});
gameTable.addMouseListener(
new MouseListenerBuilder()
.mouseClicked(this::mouseClicked)
.mousePressed(this::mousePressed)
.mouseReleased(this::mouseOnGamesList)
.build());

final TableRowSorter<LobbyGameTableModel> tableSorter = new TableRowSorter<>(gameTableModel);
// by default, sort by host
final int hostColumn = gameTableModel.getColumnIndex(LobbyGameTableModel.Column.Host);
tableSorter.setSortKeys(List.of(new RowSorter.SortKey(hostColumn, SortOrder.DESCENDING)));
gameTable.setRowSorter(tableSorter);

gameTable = new LobbyGameTable(gameTableModel);
// only allow one row to be selected
gameTable.setColumnSelectionAllowed(false);
gameTable.setCellSelectionEnabled(false);
Expand Down Expand Up @@ -93,26 +134,13 @@ class LobbyGamePanel extends JPanel {
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
final JToolBar toolBar = new JToolBar();
toolBar.add(hostGame);
toolBar.add(joinGame);
toolBar.add(hostGameButton);
toolBar.add(joinGameButton);
toolBar.setFloatable(false);
add(toolBar, BorderLayout.SOUTH);

hostGame.addActionListener(e -> hostGame(lobbyUri));
joinGame.addActionListener(e -> joinGame());
gameTable
.getSelectionModel()
.addListSelectionListener(
e -> {
final boolean selected = gameTable.getSelectedRow() >= 0;
joinGame.setEnabled(selected);
});
gameTable.addMouseListener(
new MouseListenerBuilder()
.mouseClicked(this::mouseClicked)
.mousePressed(this::mousePressed)
.mouseReleased(this::mouseOnGamesList)
.build());
hostGameButton.addActionListener(e -> hostGame(lobbyUri));
joinGameButton.addActionListener(e -> joinGame());
}

private void mouseClicked(final MouseEvent mouseEvent) {
Expand Down Expand Up @@ -163,25 +191,19 @@ private void mouseOnGamesList(final MouseEvent mouseEvent) {
.forEach(menu::add);

if (lobbyClient.isModerator()) {
final Collection<Action> generalAdminActions = getGeneralAdminGamesListContextActions();
if (!generalAdminActions.isEmpty()) {
menu.addSeparator();
generalAdminActions.forEach(menu::add);
}
menu.addSeparator();
List.of(
SwingAction.of("Show Chat History", e -> showChatHistory()),
SwingAction.of("Boot Game", e -> bootGame()),
SwingAction.of("Shutdown", e -> shutdown()))
.forEach(menu::add);
}

if (menu.getComponentCount() > 0) {
menu.show(gameTable, mouseEvent.getX(), mouseEvent.getY());
}
}

private Collection<Action> getGeneralAdminGamesListContextActions() {
return List.of(
SwingAction.of("Show Chat History", e -> showChatHistory()),
SwingAction.of("Boot Game", e -> bootGame()),
SwingAction.of("Shutdown", e -> shutdown()));
}

private void joinGame() {
final int selectedIndex = gameTable.getSelectedRow();
if (selectedIndex == -1) {
Expand Down

This file was deleted.