/* * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.intellij.codeInsight.daemon.impl; import com.intellij.ProjectTopics; import com.intellij.codeInsight.daemon.ProjectSdkSetupValidator; import com.intellij.openapi.extensions.Extensions; import com.intellij.openapi.fileEditor.FileEditor; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectBundle; import com.intellij.openapi.roots.ModuleRootEvent; import com.intellij.openapi.roots.ModuleRootListener; import com.intellij.openapi.util.Key; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.ui.EditorNotificationPanel; import com.intellij.ui.EditorNotifications; import org.jetbrains.annotations.NotNull; /** * @author Danila Ponomarenko */ public class SdkSetupNotificationProvider extends EditorNotifications.Provider<EditorNotificationPanel> implements DumbAware { static final Key<EditorNotificationPanel> KEY = Key.create("SdkSetupNotification"); private final Project myProject; public SdkSetupNotificationProvider(Project project, final EditorNotifications notifications) { myProject = project; myProject.getMessageBus().connect(project).subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() { @Override public void rootsChanged(ModuleRootEvent event) { notifications.updateAllNotifications(); } }); } @NotNull @Override public Key<EditorNotificationPanel> getKey() { return KEY; } @Override public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file, @NotNull FileEditor fileEditor) { final ProjectSdkSetupValidator[] validators = Extensions.getExtensions(ProjectSdkSetupValidator.PROJECT_SDK_SETUP_VALIDATOR_EP); for (ProjectSdkSetupValidator validator : validators) { if (validator.isApplicableFor(myProject, file)) { final String errorMessage = validator.getErrorMessage(myProject, file); if (errorMessage != null) { return createPanel(errorMessage, () -> validator.doFix(myProject, file)); } return null; } } return null; } @NotNull private static EditorNotificationPanel createPanel(@NotNull String message, @NotNull Runnable fix) { EditorNotificationPanel panel = new EditorNotificationPanel(); panel.setText(message); panel.createActionLabel(ProjectBundle.message("project.sdk.setup"), fix); return panel; } }