96 lines
3.5 KiB
Java
96 lines
3.5 KiB
Java
package dev.kske.chess.ui;
|
|
|
|
import java.awt.Component;
|
|
import java.awt.Font;
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.function.BiConsumer;
|
|
import java.util.function.Consumer;
|
|
import java.util.prefs.Preferences;
|
|
|
|
import javax.swing.DefaultComboBoxModel;
|
|
import javax.swing.JComboBox;
|
|
import javax.swing.JFileChooser;
|
|
import javax.swing.JLabel;
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.JPanel;
|
|
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);
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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()));
|
|
}
|
|
}
|