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/main/java/dev/kske/chess/ui/DialogUtil.java

140 lines
4.3 KiB
Java

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: <strong>Chess</strong><br>
* File: <strong>DialogUtil.java</strong><br>
* Created: <strong>24.07.2019</strong><br>
*
* @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<List<File>> action,
Collection<FileNameExtensionFilter> 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<File> action,
Collection<FileNameExtensionFilter> 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<FileNameExtensionFilter> 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.<br>
* <br>
* 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<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));
JLabel lblWhite = new JLabel("White:");
lblWhite.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblWhite.setBounds(10, 11, 49, 14);
dialogPanel.add(lblWhite);
JComboBox<Object> 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<Object> 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())
);
}
}