2019-07-16 11:58:51 +02:00
|
|
|
package dev.kske.chess.ui;
|
|
|
|
|
2019-07-24 07:41:45 +02:00
|
|
|
import java.awt.Toolkit;
|
|
|
|
import java.awt.datatransfer.StringSelection;
|
2019-10-20 11:51:56 +02:00
|
|
|
import java.io.File;
|
2019-10-18 17:45:13 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Files;
|
2019-07-24 07:41:45 +02:00
|
|
|
|
2019-10-20 08:52:51 +02:00
|
|
|
import javax.swing.JComboBox;
|
2019-07-16 11:58:51 +02:00
|
|
|
import javax.swing.JMenu;
|
|
|
|
import javax.swing.JMenuBar;
|
|
|
|
import javax.swing.JMenuItem;
|
2019-07-21 14:35:14 +02:00
|
|
|
import javax.swing.JOptionPane;
|
2019-07-16 11:58:51 +02:00
|
|
|
|
2019-10-20 08:52:51 +02:00
|
|
|
import dev.kske.chess.board.Board;
|
2019-07-16 11:58:51 +02:00
|
|
|
import dev.kske.chess.game.Game;
|
2019-10-20 08:52:51 +02:00
|
|
|
import dev.kske.chess.pgn.PGNDatabase;
|
|
|
|
import dev.kske.chess.pgn.PGNGame;
|
2019-07-16 11:58:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>Chess</strong><br>
|
|
|
|
* File: <strong>MenuBar.java</strong><br>
|
|
|
|
* Created: <strong>16.07.2019</strong><br>
|
|
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
|
|
*/
|
|
|
|
public class MenuBar extends JMenuBar {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -7221583703531248228L;
|
|
|
|
|
2019-08-23 22:00:30 +02:00
|
|
|
private final MainWindow mainWindow;
|
2019-07-16 11:58:51 +02:00
|
|
|
|
|
|
|
public MenuBar(MainWindow mainWindow) {
|
2019-08-23 22:00:30 +02:00
|
|
|
this.mainWindow = mainWindow;
|
2019-07-16 11:58:51 +02:00
|
|
|
|
|
|
|
initGameMenu();
|
2019-07-23 11:54:43 +02:00
|
|
|
initEngineMenu();
|
2019-07-24 07:41:45 +02:00
|
|
|
initToolsMenu();
|
2019-07-16 11:58:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initGameMenu() {
|
2019-10-14 06:31:03 +02:00
|
|
|
JMenu gameMenu = new JMenu("Game");
|
|
|
|
|
|
|
|
JMenuItem newGameMenuItem = new JMenuItem("New Game");
|
2019-10-20 11:51:56 +02:00
|
|
|
newGameMenuItem
|
|
|
|
.addActionListener((evt) -> DialogUtil.showGameConfigurationDialog(mainWindow, (whiteName, blackName) -> {
|
2019-10-14 06:31:03 +02:00
|
|
|
GamePane gamePane = mainWindow.addGamePane();
|
|
|
|
Game game = new Game(gamePane.getBoardPane(), whiteName, blackName);
|
|
|
|
gamePane.setGame(game);
|
|
|
|
game.start();
|
|
|
|
}));
|
2019-07-25 07:21:07 +02:00
|
|
|
gameMenu.add(newGameMenuItem);
|
2019-10-14 06:31:03 +02:00
|
|
|
|
|
|
|
JMenuItem loadFileMenu = new JMenuItem("Load game file");
|
2019-10-20 11:51:56 +02:00
|
|
|
loadFileMenu.addActionListener((evt) -> DialogUtil.showFileSelectionDialog(mainWindow, this::loadFile));
|
2019-10-14 06:31:03 +02:00
|
|
|
gameMenu.add(loadFileMenu);
|
|
|
|
|
2019-07-16 11:58:51 +02:00
|
|
|
add(gameMenu);
|
2019-09-08 20:18:45 +02:00
|
|
|
newGameMenuItem.doClick();
|
2019-07-16 11:58:51 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 11:54:43 +02:00
|
|
|
private void initEngineMenu() {
|
|
|
|
JMenu engineMenu = new JMenu("Engine");
|
2019-07-23 16:28:53 +02:00
|
|
|
|
2019-07-23 11:54:43 +02:00
|
|
|
JMenuItem addEngineMenuItem = new JMenuItem("Add engine");
|
|
|
|
addEngineMenuItem.addActionListener((evt) -> {
|
|
|
|
String enginePath = JOptionPane.showInputDialog(getParent(),
|
2019-09-06 13:24:58 +02:00
|
|
|
"Enter the path to a UCI-compatible chess engine:",
|
|
|
|
"Engine selection",
|
2019-07-23 11:54:43 +02:00
|
|
|
JOptionPane.QUESTION_MESSAGE);
|
2019-07-30 06:18:24 +02:00
|
|
|
if (enginePath != null) EngineUtil.addEngine(enginePath);
|
2019-07-23 11:54:43 +02:00
|
|
|
});
|
2019-07-23 16:28:53 +02:00
|
|
|
|
2019-07-30 06:18:24 +02:00
|
|
|
JMenuItem showInfoMenuItem = new JMenuItem("Show engine info");
|
|
|
|
|
|
|
|
engineMenu.add(addEngineMenuItem);
|
|
|
|
engineMenu.add(showInfoMenuItem);
|
2019-07-23 11:54:43 +02:00
|
|
|
add(engineMenu);
|
|
|
|
}
|
2019-07-23 16:28:53 +02:00
|
|
|
|
2019-07-24 07:41:45 +02:00
|
|
|
private void initToolsMenu() {
|
|
|
|
JMenu toolsMenu = new JMenu("Tools");
|
|
|
|
|
|
|
|
JMenuItem exportFENMenuItem = new JMenuItem("Export board to FEN");
|
2019-09-08 20:18:45 +02:00
|
|
|
exportFENMenuItem.addActionListener((evt) -> {
|
|
|
|
final String fen = mainWindow.getSelectedGamePane().getGame().getBoard().toFEN();
|
|
|
|
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(fen), null);
|
|
|
|
JOptionPane.showMessageDialog(mainWindow, String.format("FEN-string copied to clipboard!%n%s", fen));
|
|
|
|
});
|
2019-07-24 07:41:45 +02:00
|
|
|
toolsMenu.add(exportFENMenuItem);
|
|
|
|
|
2019-08-05 21:02:54 +02:00
|
|
|
JMenuItem loadFromFENMenuItem = new JMenuItem("Load board from FEN");
|
|
|
|
loadFromFENMenuItem.addActionListener((evt) -> {
|
2019-09-08 20:18:45 +02:00
|
|
|
final GamePane gamePane = mainWindow.addGamePane();
|
|
|
|
final String fen = JOptionPane.showInputDialog("Enter a FEN string: ");
|
2019-10-20 11:51:56 +02:00
|
|
|
DialogUtil.showGameConfigurationDialog(mainWindow, (whiteName, blackName) -> {
|
2019-10-20 08:52:51 +02:00
|
|
|
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, new Board(fen));
|
2019-09-08 20:18:45 +02:00
|
|
|
gamePane.setGame(game);
|
|
|
|
game.start();
|
|
|
|
});
|
2019-08-05 21:02:54 +02:00
|
|
|
});
|
|
|
|
toolsMenu.add(loadFromFENMenuItem);
|
|
|
|
|
2019-07-24 07:41:45 +02:00
|
|
|
add(toolsMenu);
|
|
|
|
}
|
2019-10-20 11:51:56 +02:00
|
|
|
|
|
|
|
private void loadFile(File file) {
|
|
|
|
final String name = file.getName().substring(0, file.getName().lastIndexOf('.'));
|
|
|
|
final String extension = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase();
|
|
|
|
switch (extension) {
|
|
|
|
case ".fen":
|
|
|
|
try {
|
|
|
|
final GamePane gamePane = mainWindow.addGamePane(name);
|
|
|
|
final String fen = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
|
|
|
|
DialogUtil.showGameConfigurationDialog(mainWindow, (whiteName, blackName) -> {
|
|
|
|
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, new Board(fen));
|
|
|
|
gamePane.setGame(game);
|
|
|
|
game.start();
|
|
|
|
});
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
JOptionPane.showMessageDialog(mainWindow,
|
|
|
|
"Failed to load the file " + file.getName() + ": " + e.toString(),
|
|
|
|
"File loading error",
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ".pgn":
|
|
|
|
try {
|
|
|
|
final GamePane gamePane = mainWindow.addGamePane(name);
|
|
|
|
PGNDatabase pgnDB = new PGNDatabase();
|
|
|
|
pgnDB.load(file);
|
|
|
|
if (pgnDB.getGames().size() > 0) {
|
|
|
|
String[] gameNames = new String[pgnDB.getGames().size()];
|
|
|
|
for (int i = 0; i < gameNames.length; i++) {
|
|
|
|
final PGNGame game = pgnDB.getGames().get(i);
|
|
|
|
gameNames[i] = String.format("%s vs %s: %s",
|
|
|
|
game.getTag("White"),
|
|
|
|
game.getTag("Black"),
|
|
|
|
game.getTag("Result"));
|
|
|
|
}
|
|
|
|
JComboBox<String> comboBox = new JComboBox<>(gameNames);
|
|
|
|
JOptionPane
|
|
|
|
.showInputDialog(mainWindow, comboBox, "Select a game", JOptionPane.QUESTION_MESSAGE);
|
|
|
|
DialogUtil.showGameConfigurationDialog(mainWindow, (whiteName, blackName) -> {
|
|
|
|
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName,
|
|
|
|
pgnDB.getGames().get(comboBox.getSelectedIndex()).getBoard());
|
|
|
|
game.start();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
JOptionPane.showMessageDialog(mainWindow,
|
|
|
|
"Failed to load the file " + file.getName() + ": " + e.toString(),
|
|
|
|
"File loading error",
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
JOptionPane.showMessageDialog(mainWindow,
|
|
|
|
"The file extension '" + extension + "' is not supported!",
|
|
|
|
"File loading error",
|
|
|
|
JOptionPane.ERROR_MESSAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadFENFile(File file) {
|
|
|
|
|
|
|
|
}
|
2019-07-16 11:58:51 +02:00
|
|
|
}
|