Skip to content

Commit

Permalink
Config Dialog - Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
zsalch committed Nov 16, 2018
1 parent c7299d4 commit 1991ac4
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/main/java/featurecat/lizzie/gui/ConfigDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
Expand All @@ -28,6 +29,7 @@
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
Expand All @@ -38,6 +40,7 @@

public class ConfigDialog extends JDialog {
public final ResourceBundle resourceBundle = ResourceBundle.getBundle("l10n.DisplayStrings");
private String osName;
private JTextField txtEngine;
private JTextField txtEngine1;
private JTextField txtEngine2;
Expand All @@ -62,18 +65,6 @@ public class ConfigDialog extends JDialog {
private JCheckBox chkPrintEngineLog;
private JSONObject leelazConfig;

/** Launch the application. */
public static void main(String[] args) {
try {
ConfigDialog dialog = new ConfigDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

/** Create the dialog. */
public ConfigDialog() {
setTitle(resourceBundle.getString("LizzieConfig.title.config"));
setModalityType(ModalityType.APPLICATION_MODAL);
Expand Down Expand Up @@ -465,6 +456,7 @@ protected DocumentFilter getDocumentFilter() {
String.valueOf(leelazConfig.getInt("max-game-thinking-time-seconds")));
chkPrintEngineLog.setSelected(leelazConfig.getBoolean("print-comms"));
curPath = (new File("")).getAbsoluteFile().toPath();
osName = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
setLocationRelativeTo(getOwner());
}

Expand All @@ -473,6 +465,13 @@ private String getEngineLine() {
File engineFile = null;
File weightFile = null;
JFileChooser chooser = new JFileChooser(".");
FileNameExtensionFilter filter = null;
if (isWindows()) {
filter =
new FileNameExtensionFilter(
resourceBundle.getString("LizzieConfig.title.engine"), "exe", "bat");
chooser.setFileFilter(filter);
}
chooser.setMultiSelectionEnabled(false);
chooser.setDialogTitle(resourceBundle.getString("LizzieConfig.prompt.selectEngine"));
setVisible(false);
Expand All @@ -484,6 +483,9 @@ private String getEngineLine() {
enginePath = relativizePath(engineFile.toPath());
getCommandHelp();
chooser.setDialogTitle(resourceBundle.getString("LizzieConfig.prompt.selectWeight"));
if (isWindows()) {
chooser.removeChoosableFileFilter(filter);
}
result = chooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
weightFile = chooser.getSelectedFile();
Expand Down Expand Up @@ -572,7 +574,7 @@ private class DigitOnlyFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr)
throws BadLocationException {
String newStr = string.replaceAll("\\D++", "");
String newStr = string != null ? string.replaceAll("\\D++", "") : "";
if (!newStr.isEmpty()) {
fb.insertString(offset, newStr, attr);
}
Expand All @@ -581,10 +583,14 @@ public void insertString(FilterBypass fb, int offset, String string, AttributeSe
@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
throws BadLocationException {
String newStr = text.replaceAll("\\D++", "");
String newStr = text != null ? text.replaceAll("\\D++", "") : "";
if (!newStr.isEmpty()) {
fb.replace(offset, length, newStr, attrs);
}
}
}

public boolean isWindows() {
return !osName.contains("darwin") && osName.contains("win");
}
}

0 comments on commit 1991ac4

Please sign in to comment.