diff --git a/launcher/src/main/java/com/skcraft/launcher/dialog/ConfigurationDialog.java b/launcher/src/main/java/com/skcraft/launcher/dialog/ConfigurationDialog.java index 9081920..abcc599 100644 --- a/launcher/src/main/java/com/skcraft/launcher/dialog/ConfigurationDialog.java +++ b/launcher/src/main/java/com/skcraft/launcher/dialog/ConfigurationDialog.java @@ -9,6 +9,7 @@ package com.skcraft.launcher.dialog; import com.skcraft.launcher.Configuration; import com.skcraft.launcher.Launcher; import com.skcraft.launcher.dialog.component.BetterComboBox; +import com.skcraft.launcher.launch.AddJavaRuntime; import com.skcraft.launcher.launch.JavaRuntime; import com.skcraft.launcher.launch.JavaRuntimeFinder; import com.skcraft.launcher.persistence.Persistence; @@ -17,9 +18,12 @@ import com.skcraft.launcher.util.SharedLocale; import lombok.NonNull; import javax.swing.*; +import javax.swing.filechooser.FileFilter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.File; +import java.util.Arrays; /** * A dialog to modify configuration options. @@ -74,9 +78,19 @@ public class ConfigurationDialog extends JDialog { setLocationRelativeTo(owner); JavaRuntime[] javaRuntimes = JavaRuntimeFinder.getAvailableRuntimes().toArray(new JavaRuntime[0]); - jvmRuntime.setModel(new DefaultComboBoxModel<>(javaRuntimes)); + DefaultComboBoxModel model = new DefaultComboBoxModel<>(javaRuntimes); + + // Put the runtime from the config in the model if it isn't + boolean configRuntimeFound = Arrays.stream(javaRuntimes).anyMatch(r -> config.getJavaRuntime().equals(r)); + if (!configRuntimeFound) { + model.insertElementAt(config.getJavaRuntime(), 0); + } + + jvmRuntime.setModel(model); jvmRuntime.setSelectedItem(config.getJavaRuntime()); + jvmRuntime.addItem(AddJavaRuntime.ADD_RUNTIME_SENTINEL); + mapper.map(jvmArgsText, "jvmArgs"); mapper.map(minMemorySpinner, "minMemory"); mapper.map(maxMemorySpinner, "maxMemory"); @@ -156,6 +170,28 @@ public class ConfigurationDialog extends JDialog { ConsoleFrame.showMessages(); } }); + + jvmRuntime.addActionListener(e -> { + // A little fun hack... + if (jvmRuntime.getSelectedItem() == AddJavaRuntime.ADD_RUNTIME_SENTINEL) { + jvmRuntime.setSelectedItem(null); + jvmRuntime.setPopupVisible(false); + + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setFileFilter(new JavaRuntimeFileFilter()); + chooser.setDialogTitle("Choose a Java executable"); + + int result = chooser.showOpenDialog(this); + if (result == JFileChooser.APPROVE_OPTION) { + JavaRuntime runtime = JavaRuntimeFinder.getRuntimeFromPath(chooser.getSelectedFile().getAbsolutePath()); + + MutableComboBoxModel model = (MutableComboBoxModel) jvmRuntime.getModel(); + model.insertElementAt(runtime, 0); + jvmRuntime.setSelectedItem(runtime); + } + } + }); } /** @@ -168,4 +204,16 @@ public class ConfigurationDialog extends JDialog { Persistence.commitAndForget(config); dispose(); } + + static class JavaRuntimeFileFilter extends FileFilter { + @Override + public boolean accept(File f) { + return f.isDirectory() || f.getName().startsWith("java") && f.canExecute(); + } + + @Override + public String getDescription() { + return "Java runtime executables"; + } + } } diff --git a/launcher/src/main/java/com/skcraft/launcher/launch/AddJavaRuntime.java b/launcher/src/main/java/com/skcraft/launcher/launch/AddJavaRuntime.java new file mode 100644 index 0000000..ebe81c2 --- /dev/null +++ b/launcher/src/main/java/com/skcraft/launcher/launch/AddJavaRuntime.java @@ -0,0 +1,19 @@ +package com.skcraft.launcher.launch; + +import java.io.File; + +/** + * Special sentinel subclass used to represent the "add custom runtime" entry in the dropdown + */ +public class AddJavaRuntime extends JavaRuntime { + public AddJavaRuntime() { + super(new File(""), "", false); + } + + @Override + public String toString() { + return "Add undetected Java runtime..."; + } + + public static final AddJavaRuntime ADD_RUNTIME_SENTINEL = new AddJavaRuntime(); +} diff --git a/launcher/src/main/java/com/skcraft/launcher/launch/JavaRuntimeFinder.java b/launcher/src/main/java/com/skcraft/launcher/launch/JavaRuntimeFinder.java index 209c4ed..e64420c 100644 --- a/launcher/src/main/java/com/skcraft/launcher/launch/JavaRuntimeFinder.java +++ b/launcher/src/main/java/com/skcraft/launcher/launch/JavaRuntimeFinder.java @@ -39,6 +39,7 @@ public final class JavaRuntimeFinder { "SOFTWARE\\Mojang\\InstalledProducts\\Minecraft Launcher", "InstallLocation"); launcherDir = new File(launcherPath); + if (!launcherDir.exists()) throw new Exception(); // i know i know } catch (Throwable ignored) { launcherDir = new File(System.getenv("APPDATA"), ".minecraft"); } @@ -46,6 +47,7 @@ public final class JavaRuntimeFinder { try { getEntriesFromRegistry(entries, "SOFTWARE\\JavaSoft\\Java Runtime Environment"); getEntriesFromRegistry(entries, "SOFTWARE\\JavaSoft\\Java Development Kit"); + getEntriesFromRegistry(entries, "SOFTWARE\\JavaSoft\\JDK"); } catch (Throwable ignored) { } } else if (env.getPlatform() == Platform.LINUX) {