Replaced game configuration dialog by a JOptionPane

This commit is contained in:
Kai S. K. Engelbart 2019-10-20 11:51:56 +02:00
parent cbf110c28d
commit 258db80f36
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
3 changed files with 94 additions and 109 deletions

View File

@ -10,11 +10,11 @@ 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;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* Project: <strong>Chess</strong><br>
@ -33,17 +33,8 @@ public class DialogUtil {
action.accept(fileChooser.getSelectedFile());
}
public static void showGameConfigurationDialog(BiConsumer<String, String> action) {
new JDialog() {
private static final long serialVersionUID = -5768339760489440385L;
{
setTitle("Game Configuration");
setBounds(100, 100, 281, 142);
setModal(true);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
public static void showGameConfigurationDialog(Component parent, BiConsumer<String, String> action) {
JPanel dialogPanel = new JPanel();
List<String> options = new ArrayList<>(Arrays.asList("Natural Player", "AI Player"));
EngineUtil.getEngineInfos().forEach(info -> options.add(info.name));
@ -51,37 +42,24 @@ public class DialogUtil {
JLabel lblWhite = new JLabel("White:");
lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblWhite.setBounds(10, 11, 49, 14);
getContentPane().add(lblWhite);
dialogPanel.add(lblWhite);
JComboBox<Object> cbWhite = new JComboBox<>();
cbWhite.setModel(new DefaultComboBoxModel<Object>(options.toArray()));
cbWhite.setBounds(98, 9, 159, 22);
getContentPane().add(cbWhite);
dialogPanel.add(cbWhite);
JLabel lblBlack = new JLabel("Black:");
lblBlack.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblBlack.setBounds(10, 38, 49, 14);
getContentPane().add(lblBlack);
dialogPanel.add(lblBlack);
JComboBox<Object> cbBlack = new JComboBox<>();
cbBlack.setModel(new DefaultComboBoxModel<Object>(options.toArray()));
cbBlack.setBounds(98, 36, 159, 22);
getContentPane().add(cbBlack);
dialogPanel.add(cbBlack);
JButton btnStart = new JButton("Start");
btnStart.addActionListener((evt) -> {
dispose();
JOptionPane.showMessageDialog(parent, dialogPanel, "Game configuration", JOptionPane.QUESTION_MESSAGE);
action.accept(options.get(cbWhite.getSelectedIndex()), options.get(cbBlack.getSelectedIndex()));
});
btnStart.setBounds(20, 73, 89, 23);
getContentPane().add(btnStart);
JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener((evt) -> dispose());
btnCancel.setBounds(157, 73, 89, 23);
getContentPane().add(btnCancel);
}
}.setVisible(true);
}
}

View File

@ -37,7 +37,7 @@ public class FENDropTarget extends DropTargetAdapter {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
final GamePane gamePane = mainWindow.addGamePane();
final String fen = br.readLine();
DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> {
DialogUtil.showGameConfigurationDialog(null, (whiteName, blackName) -> {
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, new Board(fen));
gamePane.setGame(game);
game.start();

View File

@ -2,6 +2,7 @@ package dev.kske.chess.ui;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@ -40,7 +41,8 @@ public class MenuBar extends JMenuBar {
JMenu gameMenu = new JMenu("Game");
JMenuItem newGameMenuItem = new JMenuItem("New Game");
newGameMenuItem.addActionListener((evt) -> DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> {
newGameMenuItem
.addActionListener((evt) -> DialogUtil.showGameConfigurationDialog(mainWindow, (whiteName, blackName) -> {
GamePane gamePane = mainWindow.addGamePane();
Game game = new Game(gamePane.getBoardPane(), whiteName, blackName);
gamePane.setGame(game);
@ -49,66 +51,7 @@ public class MenuBar extends JMenuBar {
gameMenu.add(newGameMenuItem);
JMenuItem loadFileMenu = new JMenuItem("Load game file");
loadFileMenu.addActionListener((evt) -> DialogUtil.showFileSelectionDialog(mainWindow, (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((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
.showMessageDialog(mainWindow, comboBox, "Select a game", JOptionPane.QUESTION_MESSAGE);
DialogUtil.showGameConfigurationDialog((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);
}
}));
loadFileMenu.addActionListener((evt) -> DialogUtil.showFileSelectionDialog(mainWindow, this::loadFile));
gameMenu.add(loadFileMenu);
add(gameMenu);
@ -149,7 +92,7 @@ public class MenuBar extends JMenuBar {
loadFromFENMenuItem.addActionListener((evt) -> {
final GamePane gamePane = mainWindow.addGamePane();
final String fen = JOptionPane.showInputDialog("Enter a FEN string: ");
DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> {
DialogUtil.showGameConfigurationDialog(mainWindow, (whiteName, blackName) -> {
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, new Board(fen));
gamePane.setGame(game);
game.start();
@ -159,4 +102,68 @@ public class MenuBar extends JMenuBar {
add(toolsMenu);
}
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) {
}
}