Replaced LogPanel by a JList inside GamePane with a custom cell renderer

This commit is contained in:
Kai S. K. Engelbart 2019-10-13 21:34:22 +02:00
parent 3bb4fd211c
commit a76e3cc904
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
3 changed files with 85 additions and 87 deletions

View File

@ -3,13 +3,26 @@ package dev.kske.chess.ui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import dev.kske.chess.board.MoveNode;
import dev.kske.chess.board.Piece.Color;
import dev.kske.chess.event.Event;
import dev.kske.chess.event.EventBus;
import dev.kske.chess.event.MoveEvent;
import dev.kske.chess.event.Subscribable;
import dev.kske.chess.game.Game;
import dev.kske.chess.game.NaturalPlayer;
@ -25,7 +38,6 @@ public class GamePane extends JComponent {
private JButton btnRestart, btnSwapColors;
private BoardPane boardPane;
private LogPanel logPanel;
private Game game;
private Color activeColor;
private JPanel moveSelectionPanel;
@ -39,8 +51,8 @@ public class GamePane extends JComponent {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 450, 1, 0 };
gridBagLayout.rowHeights = new int[] { 33, 267, 1, 0 };
gridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 0.0, Double.MIN_VALUE };
gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0 };
gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, Double.MIN_VALUE };
setLayout(gridBagLayout);
JPanel toolPanel = new JPanel();
@ -58,6 +70,7 @@ public class GamePane extends JComponent {
toolPanel.add(btnSwapColors);
GridBagConstraints gbc_toolPanel = new GridBagConstraints();
gbc_toolPanel.insets = new Insets(0, 0, 5, 5);
gbc_toolPanel.anchor = GridBagConstraints.NORTH;
gbc_toolPanel.fill = GridBagConstraints.HORIZONTAL;
gbc_toolPanel.gridx = 0;
@ -67,6 +80,7 @@ public class GamePane extends JComponent {
moveSelectionPanel = new JPanel();
GridBagConstraints gbc_moveSelectionPanel = new GridBagConstraints();
gbc_moveSelectionPanel.insets = new Insets(0, 0, 5, 0);
gbc_moveSelectionPanel.fill = GridBagConstraints.BOTH;
gbc_moveSelectionPanel.gridx = 2;
gbc_moveSelectionPanel.gridy = 0;
@ -89,6 +103,7 @@ public class GamePane extends JComponent {
moveSelectionPanel.add(btnLast);
boardPane = new BoardPane();
GridBagConstraints gbc_boardPane = new GridBagConstraints();
gbc_boardPane.insets = new Insets(0, 0, 5, 5);
gbc_boardPane.fill = GridBagConstraints.BOTH;
gbc_boardPane.gridx = 0;
gbc_boardPane.gridy = 1;
@ -96,6 +111,7 @@ public class GamePane extends JComponent {
JPanel numberPanel = new JPanel(new GridLayout(8, 1));
GridBagConstraints gbc_numberPanel = new GridBagConstraints();
gbc_numberPanel.insets = new Insets(0, 0, 5, 5);
gbc_numberPanel.anchor = GridBagConstraints.WEST;
gbc_numberPanel.fill = GridBagConstraints.VERTICAL;
gbc_numberPanel.gridx = 1;
@ -104,6 +120,7 @@ public class GamePane extends JComponent {
JPanel letterPanel = new JPanel(new GridLayout(1, 8));
GridBagConstraints gbc_letterPanel = new GridBagConstraints();
gbc_letterPanel.insets = new Insets(0, 0, 0, 5);
gbc_letterPanel.anchor = GridBagConstraints.NORTH;
gbc_letterPanel.fill = GridBagConstraints.HORIZONTAL;
gbc_letterPanel.gridx = 0;
@ -117,16 +134,39 @@ public class GamePane extends JComponent {
letterLabel.setHorizontalAlignment(JLabel.CENTER);
letterPanel.add(letterLabel);
}
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 2;
gbc_scrollPane.gridy = 1;
add(scrollPane, gbc_scrollPane);
// Initialize LogPanel
logPanel = new LogPanel();
GridBagConstraints gbc_logPanel = new GridBagConstraints();
gbc_logPanel.anchor = GridBagConstraints.EAST;
gbc_logPanel.fill = GridBagConstraints.VERTICAL;
gbc_logPanel.gridx = 2;
gbc_logPanel.gridy = 1;
gbc_logPanel.gridheight = 2;
add(logPanel, gbc_logPanel);
JList<MoveNode> pgnList = new JList<>();
pgnList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
pgnList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
pgnList.setVisibleRowCount(0);
pgnList.setCellRenderer(new MoveNodeRenderer());
scrollPane.setViewportView(pgnList);
EventBus.getInstance().register(new Subscribable() {
// TODO: Clean on restart
@Override
public void handle(Event<?> event) {
if (game.getBoard().getLog() == null || game.getBoard().getLog().isEmpty()) return;
DefaultListModel<MoveNode> model = new DefaultListModel<>();
game.getBoard().getLog().forEach(node -> model.addElement(node));
pgnList.setModel(model);
}
@Override
public Set<Class<?>> supports() {
return new HashSet<>(Arrays.asList(MoveEvent.class));
}
});
}
/**
@ -150,6 +190,5 @@ public class GamePane extends JComponent {
this.game = game;
btnSwapColors.setEnabled(game.getPlayers().get(Color.WHITE) instanceof NaturalPlayer
^ game.getPlayers().get(Color.BLACK) instanceof NaturalPlayer);
logPanel.setLog(game.getBoard().getLog());
}
}

View File

@ -1,74 +0,0 @@
package dev.kske.chess.ui;
import java.awt.BorderLayout;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import dev.kske.chess.board.Log;
import dev.kske.chess.board.MoveNode;
import dev.kske.chess.event.Event;
import dev.kske.chess.event.EventBus;
import dev.kske.chess.event.MoveEvent;
import dev.kske.chess.event.Subscribable;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>LogPanel.java</strong><br>
* Created: <strong>17.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class LogPanel extends JPanel implements Subscribable {
private static final long serialVersionUID = 1932671698254197119L;
private JTable mtable;
private Log log;
/**
* Create the frame.
*/
public LogPanel() {
setBorder(new EmptyBorder(5, 5, 5, 5));
setLayout(new BorderLayout(0, 0));
mtable = new JTable();
mtable.setEnabled(false);
add(new JScrollPane(mtable), BorderLayout.CENTER);
EventBus.getInstance().register(this);
}
@Override
public Set<Class<?>> supports() {
return new HashSet<>(Arrays.asList(MoveEvent.class));
}
// TODO: Implement support for variation selection
@Override
public void handle(Event<?> event) {
if (log == null || log.isEmpty()) return;
final DefaultTableModel model = new DefaultTableModel(new String[] { "White", "Black" }, 0);
for (Iterator<MoveNode> iter = log.iterator(); iter.hasNext();) {
String[] row = new String[] { iter.next().move.toLAN(), "" };
if (iter.hasNext()) row[1] = iter.next().move.toLAN();
model.addRow(row);
}
mtable.setModel(model);
}
public Log getLog() { return log; }
public void setLog(Log log) {
this.log = log;
handle(null);
}
}

View File

@ -0,0 +1,33 @@
package dev.kske.chess.ui;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.border.EmptyBorder;
import dev.kske.chess.board.MoveNode;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>MoveNodeRenderer.java</strong><br>
* Created: <strong>9 Oct 2019</strong><br>
*
* @author Kai S. K. Engelbart
*/
public class MoveNodeRenderer extends JLabel implements ListCellRenderer<MoveNode> {
private static final long serialVersionUID = 5242015788752442446L;
@Override
public Component getListCellRendererComponent(JList<? extends MoveNode> list, MoveNode node, int index,
boolean isSelected, boolean cellHasFocus) {
setBorder(new EmptyBorder(5, 5, 5, 5));
setText(node.move.toLAN());
setBackground(isSelected ? Color.red : Color.white);
setOpaque(true);
return this;
}
}