Added file chooser dialog, renamed GameConfigurationDialog to DialogUtil
This commit is contained in:
parent
a76e3cc904
commit
a0b4405976
@ -74,4 +74,6 @@ public class PGNGame {
|
|||||||
public void setTag(String tagName, String tagValue) {
|
public void setTag(String tagName, String tagValue) {
|
||||||
tagPairs.put(tagName, tagValue);
|
tagPairs.put(tagName, tagValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Board getBoard() { return board; }
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,39 @@
|
|||||||
package dev.kske.chess.ui;
|
package dev.kske.chess.ui;
|
||||||
|
|
||||||
|
import java.awt.Component;
|
||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import javax.swing.DefaultComboBoxModel;
|
import javax.swing.DefaultComboBoxModel;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JComboBox;
|
import javax.swing.JComboBox;
|
||||||
import javax.swing.JDialog;
|
import javax.swing.JDialog;
|
||||||
|
import javax.swing.JFileChooser;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>Chess</strong><br>
|
* Project: <strong>Chess</strong><br>
|
||||||
* File: <strong>GameConfigurationDialog.java</strong><br>
|
* File: <strong>DialogUtil.java</strong><br>
|
||||||
* Created: <strong>24.07.2019</strong><br>
|
* Created: <strong>24.07.2019</strong><br>
|
||||||
* Author: <strong>Kai S. K. Engelbart</strong>
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
||||||
*/
|
*/
|
||||||
public class GameConfigurationDialog {
|
public class DialogUtil {
|
||||||
|
|
||||||
private GameConfigurationDialog() {}
|
private DialogUtil() {}
|
||||||
|
|
||||||
public static void show(BiConsumer<String, String> action) {
|
public static void showFileSelectionDialog(Component parent, Consumer<File> 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<String, String> action) {
|
||||||
new JDialog() {
|
new JDialog() {
|
||||||
|
|
||||||
private static final long serialVersionUID = -5768339760489440385L;
|
private static final long serialVersionUID = -5768339760489440385L;
|
@ -36,7 +36,7 @@ public class FENDropTarget extends DropTargetAdapter {
|
|||||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||||
final GamePane gamePane = mainWindow.addGamePane();
|
final GamePane gamePane = mainWindow.addGamePane();
|
||||||
final String fen = br.readLine();
|
final String fen = br.readLine();
|
||||||
GameConfigurationDialog.show((whiteName, blackName) -> {
|
DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> {
|
||||||
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, fen);
|
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, fen);
|
||||||
gamePane.setGame(game);
|
gamePane.setGame(game);
|
||||||
game.start();
|
game.start();
|
||||||
|
@ -32,16 +32,35 @@ public class MenuBar extends JMenuBar {
|
|||||||
|
|
||||||
private void initGameMenu() {
|
private void initGameMenu() {
|
||||||
JMenu gameMenu = new JMenu("Game");
|
JMenu gameMenu = new JMenu("Game");
|
||||||
|
|
||||||
JMenuItem newGameMenuItem = new JMenuItem("New Game");
|
JMenuItem newGameMenuItem = new JMenuItem("New Game");
|
||||||
newGameMenuItem.addActionListener((evt) -> {
|
newGameMenuItem.addActionListener((evt) -> DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> {
|
||||||
GameConfigurationDialog.show((whiteName, blackName) -> {
|
|
||||||
GamePane gamePane = mainWindow.addGamePane();
|
GamePane gamePane = mainWindow.addGamePane();
|
||||||
Game game = new Game(gamePane.getBoardPane(), whiteName, blackName);
|
Game game = new Game(gamePane.getBoardPane(), whiteName, blackName);
|
||||||
gamePane.setGame(game);
|
gamePane.setGame(game);
|
||||||
game.start();
|
game.start();
|
||||||
});
|
}));
|
||||||
});
|
|
||||||
gameMenu.add(newGameMenuItem);
|
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);
|
add(gameMenu);
|
||||||
newGameMenuItem.doClick();
|
newGameMenuItem.doClick();
|
||||||
}
|
}
|
||||||
@ -80,7 +99,7 @@ public class MenuBar extends JMenuBar {
|
|||||||
loadFromFENMenuItem.addActionListener((evt) -> {
|
loadFromFENMenuItem.addActionListener((evt) -> {
|
||||||
final GamePane gamePane = mainWindow.addGamePane();
|
final GamePane gamePane = mainWindow.addGamePane();
|
||||||
final String fen = JOptionPane.showInputDialog("Enter a FEN string: ");
|
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);
|
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, fen);
|
||||||
gamePane.setGame(game);
|
gamePane.setGame(game);
|
||||||
game.start();
|
game.start();
|
||||||
|
Reference in New Issue
Block a user