Skip to content

Commit

Permalink
Notify users that they need a JDK to import sorts
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Apr 15, 2022
1 parent 195e5ba commit 1fff00f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"java.configuration.updateBuildConfiguration": "interactive",
"java.checkstyle.configuration": "${workspaceFolder}/checkstyle.xml",
"java.checkstyle.version": "9.3"
"java.checkstyle.version": "9.3",
"java.completion.filteredTypes": [
"com.sun.*",
"sun.*",
"jdk.*",
"org.graalvm.*",
"io.micrometer.shaded.*"
]
}
34 changes: 33 additions & 1 deletion src/main/java/io/github/arrayv/main/SortAnalyzer.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.github.arrayv.main;

import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
Expand Down Expand Up @@ -513,6 +516,32 @@ private static String performImportReplacements(String source) {
return source;
}

public static JavaCompiler tryGetJavaCompiler() {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
if (
JOptionPane.showOptionDialog(
null,
"You must install a JDK on your system in order to import sorts.\n" +
"Would you like to download one now?",
"Import Sort",
JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null
) == JOptionPane.YES_OPTION
) {
try {
Desktop.getDesktop().browse(new URI("https://adoptium.net/temurin/releases"));
} catch (IOException e) {
throw new RuntimeException(e);
} catch (URISyntaxException e) {
throw new Error(e);
}
}
}
return compiler;
}

public boolean importSort(File file, boolean showConfirmation) {
// SLightly modified from https://stackoverflow.com/a/40772073/8840278
// Pattern packagePattern = Pattern.compile("package (([a-zA-Z]{1}[a-zA-Z\\d_]*\\.)*[a-zA-Z][a-zA-Z\\d_]*);");
Expand Down Expand Up @@ -541,7 +570,10 @@ public boolean importSort(File file, boolean showConfirmation) {
CACHE_DIR.mkdirs();
final Path CACHE_PATH = CACHE_DIR.toPath();

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaCompiler compiler = tryGetJavaCompiler();
if (compiler == null) {
return false;
}
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> jFiles = fileManager.getJavaFileObjects(file);
int success;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/github/arrayv/prompts/SortPrompt.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ private void jButton1ActionPerformed() {//GEN-FIRST:event_jButton1ActionPerforme
private void jButton2ActionPerformed() {//GEN-FIRST:event_jButton1ActionPerformed
new Thread("ImportSort") {
@Override
public void run(){
public void run() {
if (SortAnalyzer.tryGetJavaCompiler() == null) {
return;
}
File f = new ImportSortDialog().getFile();
if (f == null) {
return;
Expand Down

0 comments on commit 1fff00f

Please sign in to comment.