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 84158c2dac
commit 0148fbe570
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 java.util.function.Consumer;
import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
@ -33,55 +33,33 @@ public class DialogUtil {
action.accept(fileChooser.getSelectedFile()); action.accept(fileChooser.getSelectedFile());
} }
public static void showGameConfigurationDialog(BiConsumer<String, String> action) { public static void showGameConfigurationDialog(Component parent, BiConsumer<String, String> action) {
new JDialog() { JPanel dialogPanel = new JPanel();
private static final long serialVersionUID = -5768339760489440385L; List<String> options = new ArrayList<>(Arrays.asList("Natural Player", "AI Player"));
EngineUtil.getEngineInfos().forEach(info -> options.add(info.name));
{ JLabel lblWhite = new JLabel("White:");
setTitle("Game Configuration"); lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14));
setBounds(100, 100, 281, 142); lblWhite.setBounds(10, 11, 49, 14);
setModal(true); dialogPanel.add(lblWhite);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
List<String> options = new ArrayList<>(Arrays.asList("Natural Player", "AI Player")); JComboBox<Object> cbWhite = new JComboBox<>();
EngineUtil.getEngineInfos().forEach(info -> options.add(info.name)); cbWhite.setModel(new DefaultComboBoxModel<Object>(options.toArray()));
cbWhite.setBounds(98, 9, 159, 22);
dialogPanel.add(cbWhite);
JLabel lblWhite = new JLabel("White:"); JLabel lblBlack = new JLabel("Black:");
lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblBlack.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblWhite.setBounds(10, 11, 49, 14); lblBlack.setBounds(10, 38, 49, 14);
getContentPane().add(lblWhite); dialogPanel.add(lblBlack);
JComboBox<Object> cbWhite = new JComboBox<>(); JComboBox<Object> cbBlack = new JComboBox<>();
cbWhite.setModel(new DefaultComboBoxModel<Object>(options.toArray())); cbBlack.setModel(new DefaultComboBoxModel<Object>(options.toArray()));
cbWhite.setBounds(98, 9, 159, 22); cbBlack.setBounds(98, 36, 159, 22);
getContentPane().add(cbWhite); dialogPanel.add(cbBlack);
JLabel lblBlack = new JLabel("Black:"); JOptionPane.showMessageDialog(parent, dialogPanel, "Game configuration", JOptionPane.QUESTION_MESSAGE);
lblBlack.setFont(new Font("Tahoma", Font.PLAIN, 14)); action.accept(options.get(cbWhite.getSelectedIndex()), options.get(cbBlack.getSelectedIndex()));
lblBlack.setBounds(10, 38, 49, 14);
getContentPane().add(lblBlack);
JComboBox<Object> cbBlack = new JComboBox<>();
cbBlack.setModel(new DefaultComboBoxModel<Object>(options.toArray()));
cbBlack.setBounds(98, 36, 159, 22);
getContentPane().add(cbBlack);
JButton btnStart = new JButton("Start");
btnStart.addActionListener((evt) -> {
dispose();
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))) { 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();
DialogUtil.showGameConfigurationDialog((whiteName, blackName) -> { DialogUtil.showGameConfigurationDialog(null, (whiteName, blackName) -> {
final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, new Board(fen)); final Game game = new Game(gamePane.getBoardPane(), whiteName, blackName, new Board(fen));
gamePane.setGame(game); gamePane.setGame(game);
game.start(); game.start();

View File

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