From 1f2cedd45517f3e5ed969afa0c6d21db7302ec40 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 20 Oct 2019 11:51:56 +0200 Subject: [PATCH] Replaced game configuration dialog by a JOptionPane --- src/dev/kske/chess/ui/DialogUtil.java | 70 +++++------- src/dev/kske/chess/ui/FENDropTarget.java | 2 +- src/dev/kske/chess/ui/MenuBar.java | 131 ++++++++++++----------- 3 files changed, 94 insertions(+), 109 deletions(-) diff --git a/src/dev/kske/chess/ui/DialogUtil.java b/src/dev/kske/chess/ui/DialogUtil.java index 0fa52e2..a25711d 100644 --- a/src/dev/kske/chess/ui/DialogUtil.java +++ b/src/dev/kske/chess/ui/DialogUtil.java @@ -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: Chess
@@ -33,55 +33,33 @@ public class DialogUtil { action.accept(fileChooser.getSelectedFile()); } - public static void showGameConfigurationDialog(BiConsumer action) { - new JDialog() { + public static void showGameConfigurationDialog(Component parent, BiConsumer action) { + JPanel dialogPanel = new JPanel(); - private static final long serialVersionUID = -5768339760489440385L; + List options = new ArrayList<>(Arrays.asList("Natural Player", "AI Player")); + EngineUtil.getEngineInfos().forEach(info -> options.add(info.name)); - { - setTitle("Game Configuration"); - setBounds(100, 100, 281, 142); - setModal(true); - setLocationRelativeTo(null); - getContentPane().setLayout(null); + JLabel lblWhite = new JLabel("White:"); + lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14)); + lblWhite.setBounds(10, 11, 49, 14); + dialogPanel.add(lblWhite); - List options = new ArrayList<>(Arrays.asList("Natural Player", "AI Player")); - EngineUtil.getEngineInfos().forEach(info -> options.add(info.name)); + JComboBox cbWhite = new JComboBox<>(); + cbWhite.setModel(new DefaultComboBoxModel(options.toArray())); + cbWhite.setBounds(98, 9, 159, 22); + dialogPanel.add(cbWhite); - JLabel lblWhite = new JLabel("White:"); - lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14)); - lblWhite.setBounds(10, 11, 49, 14); - getContentPane().add(lblWhite); + JLabel lblBlack = new JLabel("Black:"); + lblBlack.setFont(new Font("Tahoma", Font.PLAIN, 14)); + lblBlack.setBounds(10, 38, 49, 14); + dialogPanel.add(lblBlack); - JComboBox cbWhite = new JComboBox<>(); - cbWhite.setModel(new DefaultComboBoxModel(options.toArray())); - cbWhite.setBounds(98, 9, 159, 22); - getContentPane().add(cbWhite); + JComboBox cbBlack = new JComboBox<>(); + cbBlack.setModel(new DefaultComboBoxModel(options.toArray())); + cbBlack.setBounds(98, 36, 159, 22); + dialogPanel.add(cbBlack); - JLabel lblBlack = new JLabel("Black:"); - lblBlack.setFont(new Font("Tahoma", Font.PLAIN, 14)); - lblBlack.setBounds(10, 38, 49, 14); - getContentPane().add(lblBlack); - - JComboBox cbBlack = new JComboBox<>(); - cbBlack.setModel(new DefaultComboBoxModel(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); + JOptionPane.showMessageDialog(parent, dialogPanel, "Game configuration", JOptionPane.QUESTION_MESSAGE); + action.accept(options.get(cbWhite.getSelectedIndex()), options.get(cbBlack.getSelectedIndex())); } } diff --git a/src/dev/kske/chess/ui/FENDropTarget.java b/src/dev/kske/chess/ui/FENDropTarget.java index 37fcdb2..b8ffc6d 100644 --- a/src/dev/kske/chess/ui/FENDropTarget.java +++ b/src/dev/kske/chess/ui/FENDropTarget.java @@ -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(); diff --git a/src/dev/kske/chess/ui/MenuBar.java b/src/dev/kske/chess/ui/MenuBar.java index 1798ed4..c7c74d0 100644 --- a/src/dev/kske/chess/ui/MenuBar.java +++ b/src/dev/kske/chess/ui/MenuBar.java @@ -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 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 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) { + + } }