From 3d8877ddbd87182a44a433e1f6dd27ef621bd68e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 3 Aug 2019 21:48:10 +0200 Subject: [PATCH] Implemented LogFrame, added menu item for opening it --- src/dev/kske/chess/board/Board.java | 5 +++++ src/dev/kske/chess/board/Log.java | 10 ++++++---- src/dev/kske/chess/ui/LogFrame.java | 28 +++++++++++++++++++++------- src/dev/kske/chess/ui/MenuBar.java | 6 ++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/dev/kske/chess/board/Board.java b/src/dev/kske/chess/board/Board.java index e1d3c2d..2d8ab94 100644 --- a/src/dev/kske/chess/board/Board.java +++ b/src/dev/kske/chess/board/Board.java @@ -510,6 +510,11 @@ public class Board implements Cloneable { */ public Piece[][] getBoardArr() { return boardArr; } + /** + * @return The move log + */ + public Log getLog() { return log; } + /** * @return The active color for the next move */ diff --git a/src/dev/kske/chess/board/Log.java b/src/dev/kske/chess/board/Log.java index 8a66a33..e29c61e 100644 --- a/src/dev/kske/chess/board/Log.java +++ b/src/dev/kske/chess/board/Log.java @@ -53,7 +53,7 @@ public class Log implements Cloneable { moves.clear(); activeColor = Color.WHITE; } - + @Override public Object clone() { Log log = null; @@ -69,12 +69,14 @@ public class Log implements Cloneable { public Color getActiveColor() { return activeColor; } + public List getLoggedMoves() { return moves; } + public static class LoggedMove { - public final Move move; - public final Piece capturedPiece; + public final Move move; + public final Piece capturedPiece; public final Position enPassant; - public final int fullmoveCounter, halfmoveClock; + public final int fullmoveCounter, halfmoveClock; public LoggedMove(Move move, Piece capturedPiece, Position enPassant, int fullmoveCounter, int halfmoveClock) { this.move = move; diff --git a/src/dev/kske/chess/ui/LogFrame.java b/src/dev/kske/chess/ui/LogFrame.java index 79b885e..c903764 100644 --- a/src/dev/kske/chess/ui/LogFrame.java +++ b/src/dev/kske/chess/ui/LogFrame.java @@ -1,14 +1,17 @@ package dev.kske.chess.ui; import java.awt.BorderLayout; +import java.util.List; import javax.swing.JFrame; 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.Move; +import dev.kske.chess.board.Log; +import dev.kske.chess.board.Log.LoggedMove; /** * Project: Chess
@@ -23,12 +26,16 @@ public class LogFrame extends JFrame { private JPanel mcontentPane; private JTable mtable; + private Log log; + /** * Create the frame. */ - public LogFrame() { + public LogFrame(Log log) { + this.log = log; + setTitle("Move History"); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 300); mcontentPane = new JPanel(); mcontentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); @@ -36,12 +43,19 @@ public class LogFrame extends JFrame { setContentPane(mcontentPane); mtable = new JTable(); - mtable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "White", "Black" })); mtable.setEnabled(false); - mcontentPane.add(mtable, BorderLayout.CENTER); + mcontentPane.add(new JScrollPane(mtable), BorderLayout.CENTER); + update(); } - public void add(Move move) { - + // TODO: call this method after a move + public void update() { + final List moves = log.getLoggedMoves(); + String[][] data = new String[moves.size() / 2 + moves.size() % 2][2]; + for (int i = 0; i < data.length; i++) { + data[i][0] = moves.get(i * 2).move.toString(); + if (i < data.length - 1) data[i][1] = moves.get(i * 2 + 1).move.toString(); + } + mtable.setModel(new DefaultTableModel(data, new String[] { "White", "Black" })); } } diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index e564be6..ca1e6d3 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -70,6 +70,12 @@ public class MenuBar extends JMenuBar { private void initToolsMenu() { JMenu toolsMenu = new JMenu("Tools"); + // TODO: prevent multiple initializations + JMenuItem showLogMenuItem = new JMenuItem("Show move log"); + showLogMenuItem + .addActionListener((evt) -> new LogFrame(mainWindow.getGame().getBoard().getLog()).setVisible(true)); + toolsMenu.add(showLogMenuItem); + JMenuItem exportFENMenuItem = new JMenuItem("Export board to FEN"); exportFENMenuItem.addActionListener((evt) -> Toolkit.getDefaultToolkit() .getSystemClipboard()