This repository has been archived on 2021-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
chess/src/dev/kske/chess/ui/MenuBar.java

90 lines
2.5 KiB
Java

package dev.kske.chess.ui;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import dev.kske.chess.game.Game;
/**
* 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;
private final MainWindow mainWindow;
private final BoardPane boardPane;
public MenuBar(MainWindow mainWindow) {
this.mainWindow = mainWindow;
boardPane = mainWindow.getBoardPane();
initGameMenu();
initEngineMenu();
initToolsMenu();
}
private void initGameMenu() {
JMenu gameMenu = new JMenu("Game");
JMenuItem newGameMenuItem = new JMenuItem("New Game");
newGameMenuItem.addActionListener((evt) -> {
GameConfigurationDialog dialog = new GameConfigurationDialog();
dialog.setVisible(true);
if (dialog.isStartGame()) startGame(new Game(boardPane, dialog.getWhiteName(), dialog.getBlackName()));
});
gameMenu.add(newGameMenuItem);
add(gameMenu);
// Start a game
startGame(new Game(boardPane, "Natural Player", "Natural Player"));
}
private void initEngineMenu() {
JMenu engineMenu = new JMenu("Engine");
JMenuItem addEngineMenuItem = new JMenuItem("Add engine");
addEngineMenuItem.addActionListener((evt) -> {
String enginePath = JOptionPane.showInputDialog(getParent(),
"Enter the path to a UCI-compatible chess engine:",
"Engine selection",
JOptionPane.QUESTION_MESSAGE);
if (enginePath != null) EngineUtil.addEngine(enginePath);
});
JMenuItem showInfoMenuItem = new JMenuItem("Show engine info");
engineMenu.add(addEngineMenuItem);
engineMenu.add(showInfoMenuItem);
add(engineMenu);
}
private void initToolsMenu() {
JMenu toolsMenu = new JMenu("Tools");
JMenuItem exportFENMenuItem = new JMenuItem("Export board to FEN");
exportFENMenuItem.addActionListener((evt) -> Toolkit.getDefaultToolkit()
.getSystemClipboard()
.setContents(new StringSelection(mainWindow.getGame().getBoard().toFEN()), null));
toolsMenu.add(exportFENMenuItem);
add(toolsMenu);
}
private void startGame(Game game) {
mainWindow.setGame(game);
// Update board and board component
game.reset();
game.start();
}
}