package org.jacoco.coverage.actions;

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import com.intellij.testIntegration.TestFinderHelper;
import org.jacoco.coverage.util.CoverageUtils;

import java.io.File;
import java.util.List;

public class AnnotateFileWithCoverageAction extends AnAction {


    @Override
    public void actionPerformed(AnActionEvent e) {
        DataContext dataContext = e.getDataContext();
        Project project = PlatformDataKeys.PROJECT.getData(dataContext);
        if (project == null) {
            return;
        }

        showInBrowser(e, project);
    }

    private void showInBrowser(AnActionEvent e, Project project) {
        PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);

        if (psiFile instanceof PsiJavaFile) {
            PsiJavaFile javaFile = (PsiJavaFile) psiFile;
            if (TestFinderHelper.isTest(javaFile)) {

                List<PsiElement> classesForTest = TestFinderHelper.findClassesForTest(javaFile);
                for (PsiElement element : classesForTest) {
                    if (element != null && element.getContainingFile() instanceof PsiJavaFile) {
                        javaFile = (PsiJavaFile) element.getContainingFile();
                        break;
                    }
                }
            }

            File coverageRootPath = CoverageUtils.getCoverageReportRoot(project);
            if (coverageRootPath.exists()) {
                String packageName = javaFile.getPackageName();
                File url = new File(coverageRootPath, packageName);
                VirtualFile virtualFile = javaFile.getVirtualFile();
                if (virtualFile != null) {
                    url = new File(url, virtualFile.getName() + ".html");
                    if (url.exists()){
                        BrowserUtil.launchBrowser(url.toURI().getPath());
                    }
                }
            }
        }
    }
}

@@ -68,16 +68,217 @@
 erUtil;%0A
+import com.intellij.notification.Notification;%0Aimport com.intellij.notification.NotificationListener;%0Aimport com.intellij.notification.NotificationType;%0Aimport com.intellij.notification.Notifications;%0A
 import c
@@ -610,16 +610,99 @@
 eUtils;%0A
+import org.jetbrains.annotations.NotNull;%0A%0Aimport javax.swing.event.HyperlinkEvent;
 %0Aimport 
@@ -803,16 +803,87 @@
 tion %7B%0A%0A
+    private ActionManager actionManager = ActionManager.getInstance();%0A
 %0A    @Ov
@@ -2195,32 +2195,74 @@
 tVirtualFile();%0A
+                boolean wasFound = false;%0A
                 
@@ -2399,16 +2399,17 @@
 xists())
+ 
 %7B%0A      
@@ -2500,51 +2500,1497 @@
     
-%7D%0A                %7D%0A            %7D%0A        %7D
+    wasFound = true;%0A                    %7D%0A                %7D%0A                if (!wasFound) %7B%0A                    Notifications.Bus.notify(getMissingFileNotification(virtualFile), project);%0A                %7D%0A            %7D else %7B%0A                Notifications.Bus.notify(getMissingReportNotification(e), project);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private Notification getMissingFileNotification(VirtualFile url) %7B%0A        String title = %22File not found%22;%0A        String content = %22No coverage was found for %22 + (url != null ? url.getPresentableUrl() : %22%3Cnull%3E%22);%0A%0A        return new Notification(CoverageUtils.PLUGIN_TITLE, title, content, NotificationType.ERROR);%0A    %7D%0A%0A    private Notification getMissingReportNotification(final AnActionEvent e) %7B%0A        return new Notification(%0A                CoverageUtils.PLUGIN_TITLE,%0A                %22Coverage report missing%22,%0A                %22Please compile the report by clicking %3Ca href='#'%3Ehere%3C/a%3E and then retry.%22,%0A                NotificationType.ERROR,%0A                new NotificationListener() %7B%0A                    @Override%0A                    public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) %7B%0A                        if (HyperlinkEvent.EventType.ACTIVATED.equals(event.getEventType())) %7B%0A                            actionManager.tryToExecute(new GenerateCoverageReportAction(), e.getInputEvent(), null, null, true);%0A                        %7D%0A                    %7D%0A                %7D);
 %0A   

Add notification when missing report while trying to view file coverage