From 8b9793611a51046f834b79586bad4b5d9f35bf63 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 14 Oct 2019 06:31:03 +0200 Subject: [PATCH] Added file chooser dialog, renamed GameConfigurationDialog to DialogUtil --- src/dev/kske/chess/pgn/PGNGame.java | 2 + ...nfigurationDialog.java => DialogUtil.java} | 19 +++++++-- src/dev/kske/chess/ui/FENDropTarget.java | 2 +- src/dev/kske/chess/ui/MenuBar.java | 41 ++++++++++++++----- 4 files changed, 48 insertions(+), 16 deletions(-) rename src/dev/kske/chess/ui/{GameConfigurationDialog.java => DialogUtil.java} (74%) diff --git a/src/dev/kske/chess/pgn/PGNGame.java b/src/dev/kske/chess/pgn/PGNGame.java index e800a3c..e9d3a7a 100644 --- a/src/dev/kske/chess/pgn/PGNGame.java +++ b/src/dev/kske/chess/pgn/PGNGame.java @@ -74,4 +74,6 @@ public class PGNGame { public void setTag(String tagName, String tagValue) { tagPairs.put(tagName, tagValue); } + + public Board getBoard() { return board; } } diff --git a/src/dev/kske/chess/ui/GameConfigurationDialog.java b/src/dev/kske/chess/ui/DialogUtil.java similarity index 74% rename from src/dev/kske/chess/ui/GameConfigurationDialog.java rename to src/dev/kske/chess/ui/DialogUtil.java index a9cc9eb..0fa52e2 100644 --- a/src/dev/kske/chess/ui/GameConfigurationDialog.java +++ b/src/dev/kske/chess/ui/DialogUtil.java @@ -1,28 +1,39 @@ package dev.kske.chess.ui; +import java.awt.Component; import java.awt.Font; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.BiConsumer; +import java.util.function.Consumer; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JDialog; +import javax.swing.JFileChooser; import javax.swing.JLabel; /** * Project: Chess
- * File: GameConfigurationDialog.java
+ * File: DialogUtil.java
* Created: 24.07.2019
* Author: Kai S. K. Engelbart */ -public class GameConfigurationDialog { +public class DialogUtil { - private GameConfigurationDialog() {} + private DialogUtil() {} - public static void show(BiConsumer action) { + public static void showFileSelectionDialog(Component parent, Consumer action) { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); + if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) + action.accept(fileChooser.getSelectedFile()); + } + + public static void showGameConfigurationDialog(BiConsumer action) { new JDialog() { private static final long serialVersionUID = -5768339760489440385L; diff --git a/src/dev/kske/chess/ui/FENDropTarget.java b/src/dev/kske/chess/ui/FENDropTarget.java index 9d8fd2d..23a5aef 100644 --- a/src/dev/kske/chess/ui/FENDropTarget.java +++ b/src/dev/kske/chess/ui/FENDropTarget.java @@ -36,7 +36,7 @@ public class FENDropTarget extends DropTargetAdapter { try (BufferedReader br = new BufferedReader(new FileReader(file))) { final GamePane gamePane = mainWindow.addGamePane(); final String fen = br.readLine(); - GameConfigurationDialog.show((whiteName, blackName) -> { + DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> { final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, fen); gamePane.setGame(game); game.start(); diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index 990cb84..24a8bb5 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -31,17 +31,36 @@ public class MenuBar extends JMenuBar { } private void initGameMenu() { - JMenu gameMenu = new JMenu("Game"); - JMenuItem newGameMenuItem = new JMenuItem("New Game"); - newGameMenuItem.addActionListener((evt) -> { - GameConfigurationDialog.show((whiteName, blackName) -> { - GamePane gamePane = mainWindow.addGamePane(); - Game game = new Game(gamePane.getBoardPane(), whiteName, blackName); - gamePane.setGame(game); - game.start(); - }); - }); + JMenu gameMenu = new JMenu("Game"); + + JMenuItem newGameMenuItem = new JMenuItem("New Game"); + newGameMenuItem.addActionListener((evt) -> DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> { + GamePane gamePane = mainWindow.addGamePane(); + Game game = new Game(gamePane.getBoardPane(), whiteName, blackName); + gamePane.setGame(game); + game.start(); + })); gameMenu.add(newGameMenuItem); + + JMenuItem loadFileMenu = new JMenuItem("Load game file"); + loadFileMenu.addActionListener((evt) -> DialogUtil.showFileSelectionDialog(mainWindow, (file) -> { + final String extension = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase(); + switch (extension) { + case ".fen": + // TODO: Load board from FEN + break; + case ".pgn": + // TODO: Load board from PGN + break; + default: + JOptionPane.showMessageDialog(mainWindow, + "The file extension '" + extension + "' is not supported!", + "File loading error", + JOptionPane.ERROR_MESSAGE); + } + })); + gameMenu.add(loadFileMenu); + add(gameMenu); newGameMenuItem.doClick(); } @@ -80,7 +99,7 @@ public class MenuBar extends JMenuBar { loadFromFENMenuItem.addActionListener((evt) -> { final GamePane gamePane = mainWindow.addGamePane(); final String fen = JOptionPane.showInputDialog("Enter a FEN string: "); - GameConfigurationDialog.show((whiteName, blackName) -> { + DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> { final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, fen); gamePane.setGame(game); game.start();