package dev.kske.chess.ui; import java.awt.Component; import java.awt.Font; import java.io.File; import java.util.*; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.prefs.Preferences; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import dev.kske.chess.io.EngineUtil; /** * Project: Chess
* File: DialogUtil.java
* Created: 24.07.2019
* * @since Chess v0.3-alpha * @author Kai S. K. Engelbart */ public class DialogUtil { private DialogUtil() {} /** * Saves the last accessed folder for loading and saving game files. */ private static Preferences preferences = Preferences.userNodeForPackage(DialogUtil.class); /** * Displays a parameterized file opening dialog. * * @param parent the parent component of the dialog * @param action the action executed with the selected files a its argument * @param filters the file extension filters passed to the dialog */ public static void showFileSelectionDialog( Component parent, Consumer> action, Collection filters ) { JFileChooser fileChooser = createFileChooser(filters); if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { action.accept(Arrays.asList(fileChooser.getSelectedFile())); preferences.put("path", fileChooser.getSelectedFile().getParent()); } } /** * Displays a parameterized file saving dialog. * * @param parent the parent component of the dialog * @param action the action executed with the selected file a its argument * @param filters the file extension filters passed to the dialog */ public static void showFileSaveDialog( Component parent, Consumer action, Collection filters ) { JFileChooser fileChooser = createFileChooser(filters); if (fileChooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) { action.accept( new File( fileChooser.getSelectedFile().getAbsolutePath() + "." + ((FileNameExtensionFilter) fileChooser .getFileFilter()).getExtensions()[0] ) ); preferences.put("path", fileChooser.getSelectedFile().getParent()); } } private static JFileChooser createFileChooser(Collection filters) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory( new File(preferences.get("path", System.getProperty("user.home"))) ); fileChooser.setAcceptAllFileFilterUsed(false); filters.forEach(fileChooser::addChoosableFileFilter); return fileChooser; } /** * Displays a dialog in which the user can select the player types for a * game.
*
* The dialog will always display {@code Natural Player} and * {@code AIPlayer}, * as well as all engine names stored by {@link EngineUtil}. * * @param parent the parent component of the dialog * @param action the action executed with the two selected names as * arguments */ public static void showGameConfigurationDialog( Component parent, BiConsumer action ) { JPanel dialogPanel = new JPanel(); List options = new ArrayList<>(Arrays.asList("Natural Player", "AI Player")); EngineUtil.getEngineInfos().forEach(info -> options.add(info.name)); JLabel lblWhite = new JLabel("White:"); lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblWhite.setBounds(10, 11, 49, 14); dialogPanel.add(lblWhite); JComboBox cbWhite = new JComboBox<>(); cbWhite.setModel(new DefaultComboBoxModel<>(options.toArray())); cbWhite.setBounds(98, 9, 159, 22); dialogPanel.add(cbWhite); JLabel lblBlack = new JLabel("Black:"); lblBlack.setFont(new Font("Tahoma", Font.PLAIN, 14)); lblBlack.setBounds(10, 38, 49, 14); dialogPanel.add(lblBlack); JComboBox cbBlack = new JComboBox<>(); cbBlack.setModel(new DefaultComboBoxModel<>(options.toArray())); cbBlack.setBounds(98, 36, 159, 22); dialogPanel.add(cbBlack); JOptionPane.showMessageDialog( parent, dialogPanel, "Game configuration", JOptionPane.QUESTION_MESSAGE ); action.accept( options.get(cbWhite.getSelectedIndex()), options.get(cbBlack.getSelectedIndex()) ); } }