From a76e3cc9043fe776a1041c49ccaeb18970c60fb2 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 13 Oct 2019 21:34:22 +0200 Subject: [PATCH] Replaced LogPanel by a JList inside GamePane with a custom cell renderer --- src/dev/kske/chess/ui/GamePane.java | 65 ++++++++++++++---- src/dev/kske/chess/ui/LogPanel.java | 74 --------------------- src/dev/kske/chess/ui/MoveNodeRenderer.java | 33 +++++++++ 3 files changed, 85 insertions(+), 87 deletions(-) delete mode 100644 src/dev/kske/chess/ui/LogPanel.java create mode 100644 src/dev/kske/chess/ui/MoveNodeRenderer.java diff --git a/src/dev/kske/chess/ui/GamePane.java b/src/dev/kske/chess/ui/GamePane.java index 6887a5f..96cbefa 100644 --- a/src/dev/kske/chess/ui/GamePane.java +++ b/src/dev/kske/chess/ui/GamePane.java @@ -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 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 model = new DefaultListModel<>(); + game.getBoard().getLog().forEach(node -> model.addElement(node)); + pgnList.setModel(model); + } + + @Override + public Set> 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()); } } diff --git a/src/dev/kske/chess/ui/LogPanel.java b/src/dev/kske/chess/ui/LogPanel.java deleted file mode 100644 index e0bff7c..0000000 --- a/src/dev/kske/chess/ui/LogPanel.java +++ /dev/null @@ -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: Chess
- * File: LogPanel.java
- * Created: 17.07.2019
- * Author: Kai S. K. Engelbart - */ -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> 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 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); - } -} diff --git a/src/dev/kske/chess/ui/MoveNodeRenderer.java b/src/dev/kske/chess/ui/MoveNodeRenderer.java new file mode 100644 index 0000000..26b1b99 --- /dev/null +++ b/src/dev/kske/chess/ui/MoveNodeRenderer.java @@ -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: Chess
+ * File: MoveNodeRenderer.java
+ * Created: 9 Oct 2019
+ * + * @author Kai S. K. Engelbart + */ +public class MoveNodeRenderer extends JLabel implements ListCellRenderer { + + private static final long serialVersionUID = 5242015788752442446L; + + @Override + public Component getListCellRendererComponent(JList 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; + } +}