mirror of
https://github.com/SKCraft/Launcher.git
synced 2024-11-24 12:16:28 +01:00
Add option to Java selection dropdowns to choose a runtime from path
Bit of an experiment, but this commit allows users to manually find a Java runtime on the filesystem and save it to config
This commit is contained in:
parent
cd063304fb
commit
d1184c32b2
@ -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<JavaRuntime> 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<JavaRuntime> model = (MutableComboBoxModel<JavaRuntime>) 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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user