-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- if a jdk isn't found on the system a notification suggests the user to fix the JDK differently
- Loading branch information
Showing
5 changed files
with
143 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/main/java/de/gebit/plugins/autoconfig/sdk/JDKResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package de.gebit.plugins.autoconfig.sdk; | ||
|
||
import com.intellij.notification.Notification; | ||
import com.intellij.notification.NotificationGroup; | ||
import com.intellij.notification.NotificationGroupManager; | ||
import com.intellij.notification.NotificationType; | ||
import com.intellij.openapi.progress.ProgressIndicator; | ||
import com.intellij.openapi.progress.ProgressManager; | ||
import com.intellij.openapi.project.DumbAwareAction; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.projectRoots.JavaSdk; | ||
import com.intellij.openapi.projectRoots.ProjectJdkTable; | ||
import com.intellij.openapi.projectRoots.SdkType; | ||
import com.intellij.openapi.projectRoots.impl.*; | ||
import com.intellij.openapi.roots.ui.configuration.UnknownSdk; | ||
import com.intellij.openapi.ui.MessageType; | ||
import de.gebit.plugins.autoconfig.util.Notifications; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Utility class to resolve missing JDKs | ||
*/ | ||
public class JDKResolver { | ||
|
||
private JDKResolver() { | ||
// nothing | ||
} | ||
|
||
/** | ||
* Ensures, that the currently missing JDK is made available for the project. If the JDK is not found on the system | ||
* it tries to assist the user in applying the IDEs suggestions. | ||
* | ||
* @param jdkName the name of the JDK to resolve | ||
* @param project the project to resolve the JDK for | ||
*/ | ||
public static void resolveMissingJDK(String jdkName, Project project) { | ||
final NotificationGroup notifier = Notifications.getNotifier(); | ||
final NotificationGroup sdkNotification = NotificationGroupManager.getInstance() | ||
.getNotificationGroup("AutoconfigSDK"); | ||
|
||
final Notification notification = notifier.createNotification( | ||
"JDK \"" + jdkName + "\" not found. Autoconfig is trying to resolve this SDK automatically. If this fails, please configure the SDK manually.", | ||
MessageType.WARNING); | ||
notification.notify(project); | ||
|
||
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> { | ||
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator(); | ||
final UnknownSdkCollector unknownSdkCollector = new UnknownSdkCollector(project) { | ||
@NotNull | ||
@Override | ||
public UnknownSdkSnapshot collectSdksBlocking() { | ||
return new UnknownSdkSnapshot(Set.of(), List.of(new UnknownSdk() { | ||
@Override | ||
public @NotNull String getSdkName() { | ||
return jdkName; | ||
} | ||
|
||
@Override | ||
public @NotNull SdkType getSdkType() { | ||
return JavaSdk.getInstance(); | ||
} | ||
}), Arrays.stream(ProjectJdkTable.getInstance().getAllJdks()).toList()); | ||
} | ||
}; | ||
|
||
final List<UnknownSdkFix> unknownSdkFixes = UnknownSdkTracker.getInstance(project) | ||
.collectUnknownSdks(unknownSdkCollector, progressIndicator); | ||
final List<UnknownSdkFix> otherFixes = UnknownSdkTracker.getInstance(project) | ||
.applyAutoFixesAndNotify(unknownSdkFixes, progressIndicator); | ||
|
||
if (otherFixes.isEmpty()) { | ||
// we've found an instant fix for this JDK | ||
notification.expire(); | ||
} | ||
|
||
for (UnknownSdkFix unknownSdkFix : otherFixes) { | ||
final UnknownSdkFixAction suggestedFixAction = unknownSdkFix.getSuggestedFixAction(); | ||
if (suggestedFixAction != null) { | ||
// it seems like we've found a fix for this JDK, but it's not an instant fix, e.g. downloading a jdk. | ||
// we'll prompt the user to apply the fix | ||
final Notification fixableSdkError = sdkNotification.createNotification("Fixable missing JDK", | ||
suggestedFixAction.getActionDetailedText(), NotificationType.WARNING); | ||
fixableSdkError.addAction(DumbAwareAction.create(suggestedFixAction.getActionShortText(), evt -> { | ||
suggestedFixAction.applySuggestionAsync(project); | ||
fixableSdkError.expire(); | ||
notification.expire(); | ||
})); | ||
fixableSdkError.setImportantSuggestion(true); | ||
fixableSdkError.setSuggestionType(true); | ||
fixableSdkError.notify(project); | ||
} else { | ||
// neither we nor IntelliJ were able to find a fix for the requested JDK. This isn't going anywhere. | ||
sdkNotification.createNotification("Unfixable missing JDK!", | ||
unknownSdkFix.getNotificationText() + "\nPlease install it manually and add it to your SDK list.", | ||
NotificationType.ERROR).notify(project); | ||
} | ||
} | ||
}, "Autoconfig: Resolving JDK \"" + jdkName + "\"", true, project); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters