// Path: codedefects.core/src/org/nbheaven/sqe/codedefects/core/util/SQECodedefectSupport.java
// public final class SQECodedefectSupport {
// 
//     public static Collection<Project> getQualityEnhancedProjects() {
//         Collection<Project> projects = new HashSet<>();
//         for (Project project : OpenProjects.getDefault().getOpenProjects()) {
//             if (isQualityAwareProject(project)) {
//                 projects.add(project);
//             }
//         }
//         return projects;
//     }
// 
//     public static final boolean isQualityAwareProject(Project project) {
//         return null != retrieveSession(project, QualitySession.class);
//     }
// 
//     public static final boolean isQualityAwareProject(Project project, QualityProvider provider) {
//         return null != retrieveSession(project, provider);
//     }
// 
//     public static <T extends QualitySession> T retrieveSession(Project project, Class<T> sessionClass) {
//         return (project != null && sessionClass != null) ? project.getLookup().lookup(sessionClass) : null;
//     }
// 
//     public static QualitySession retrieveSession(Project project, QualityProvider provider) {
//         return provider != null ? retrieveSession(project, provider.getQualitySessionClass()) : null;
//     }
// 
//     public static Collection<? extends QualitySession> retrieveSessions(Project project) {
//         return project != null ? project.getLookup().lookupAll(QualitySession.class) : Collections.emptyList();
//     }
// 
//     public static QualitySession retrieveSessionFromDataObject(DataObject dataObject, QualityProvider provider) {
//         return provider != null ? retrieveSessionFromDataObject(dataObject, provider.getQualitySessionClass()) : null;
//     }
// 
//     public static <T extends QualitySession> T retrieveSessionFromDataObject(DataObject dataObject, Class<T> sessionClass) {
//         Project project = SQEProjectSupport.findProjectByDataObject(dataObject);
//         return project != null ? retrieveSession(project, sessionClass) : null;
//     }
// 
//     public static QualitySession retrieveSessionFromFileObject(FileObject fileObject, QualityProvider provider) {
//         return provider != null ? retrieveSessionFromFileObject(fileObject, provider.getQualitySessionClass()) : null;
//     }
// 
//     public static <T extends QualitySession> T retrieveSessionFromFileObject(FileObject fileObject, Class<T> sessionClass) {
//         Project project = SQEProjectSupport.findProjectByFileObject(fileObject);
//         return project != null ? retrieveSession(project, sessionClass) : null;
//     }
// 
// //    public static boolean isQualityProviderActive(FileObject fileObject, QualityProvider provider) {
// //        return provider != null && isQualityProviderEnabled(fileObject, provider.getQualitySessionClass());
// //    }
// //
// //    public static boolean isQualityProviderActive(Project project, QualityProvider provider) {
// //        return provider != null && isQualityProviderEnabled(project, provider.getQualitySessionClass());
// //    }
//     public static boolean isQualityProviderEnabledForDataObject(DataObject dataObject, Class<? extends QualitySession> sessionClass) {
//         return isQualityProviderEnabled(retrieveSessionFromDataObject(dataObject, sessionClass));
//     }
// 
//     public static boolean isQualityProviderEnabledForFileObject(FileObject fileObject, Class<? extends QualitySession> sessionClass) {
//         return isQualityProviderEnabled(retrieveSessionFromFileObject(fileObject, sessionClass));
//     }
// 
//     public static boolean isQualityProviderEnabled(Project project, QualityProvider provider) {
//         return provider != null && isQualityProviderEnabled(retrieveSession(project, provider.getQualitySessionClass()));
//     }
// 
//     public static boolean isQualityProviderEnabled(Project project, Class<? extends QualitySession> sessionClass) {
//         return isQualityProviderEnabled(retrieveSession(project, sessionClass));
//     }
// 
//     private static boolean isQualityProviderEnabled(QualitySession session) {
//         return session != null && session.isEnabled();
//     }
// 
//     public static boolean isAnnotateProjectResultEnabled(Project project, QualityProvider provider) {
//         return provider != null &&  isAnnotateProjectResultEnabled(retrieveSession(project, provider.getQualitySessionClass()));
//     }
// 
//     private static boolean isAnnotateProjectResultEnabled(QualitySession session) {
//         return session != null && session.isAnnotateProjectResultEnabled();
//     }
// 
// }


import org.netbeans.spi.tasklist.PushTaskScanner;
import org.netbeans.spi.tasklist.Task;
import org.netbeans.spi.tasklist.TaskScanningScope;
import org.openide.filesystems.FileObject;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.nbheaven.sqe.codedefects.core.util.SQECodedefectSupport;
import org.nbheaven.sqe.core.utilities.SQEProjectSupport;
import org.nbheaven.sqe.tools.checkstyle.codedefects.core.CheckstyleResult;
import org.nbheaven.sqe.tools.checkstyle.codedefects.core.CheckstyleResult.ClassKey;
import org.nbheaven.sqe.tools.checkstyle.codedefects.core.CheckstyleSession;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;

    @Override
    public synchronized void setScope(TaskScanningScope taskScanningScope, Callback callback) {
        if (taskScanningScope == null || callback == null) {
            return;
        }

        for (FileObject fileObject : taskScanningScope.getLookup().lookupAll(FileObject.class)) {
            if (SQECodedefectSupport.isQualityProviderEnabledForFileObject(fileObject, CheckstyleSession.class) && JavaSource.forFileObject(fileObject) != null) {
                CheckstyleResult result = getResult(fileObject);
                if (result == null) {
                    continue;
                }
                Project project = SQEProjectSupport.findProjectByFileObject(fileObject);
                Map<ClassKey, Collection<AuditEvent>> instanceByClass = result.getInstanceByClass();
                CheckstyleResult.ClassKey key = new CheckstyleResult.ClassKey(project, fileObject);
                Collection<AuditEvent> auditEvents = instanceByClass.get(key);
                if (auditEvents != null) { // SQE-57
                    callback.setTasks(fileObject, new LinkedList<>(getTasks(auditEvents, fileObject)));
                }
            }
        }

        for (Project project : taskScanningScope.getLookup().lookupAll(Project.class)) {
            CheckstyleResult result = getResult(project);
            if (result != null) {
                List<Task> tasks = new LinkedList<>();
                for (Map.Entry<ClassKey, Collection<AuditEvent>> classEntry : result.getInstanceByClass().entrySet()) {
                    tasks.addAll(getTasks(classEntry.getValue(), classEntry.getKey().getFileObject()));
                }
// Path: codedefects.core/src/org/nbheaven/sqe/codedefects/core/util/SQECodedefectSupport.java
// public final class SQECodedefectSupport {
// 
//     public static Collection<Project> getQualityEnhancedProjects() {
//         Collection<Project> projects = new HashSet<>();
//         for (Project project : OpenProjects.getDefault().getOpenProjects()) {
//             if (isQualityAwareProject(project)) {
//                 projects.add(project);
//             }
//         }
//         return projects;
//     }
// 
//     public static final boolean isQualityAwareProject(Project project) {
//         return null != retrieveSession(project, QualitySession.class);
//     }
// 
//     public static final boolean isQualityAwareProject(Project project, QualityProvider provider) {
//         return null != retrieveSession(project, provider);
//     }
// 
//     public static <T extends QualitySession> T retrieveSession(Project project, Class<T> sessionClass) {
//         return (project != null && sessionClass != null) ? project.getLookup().lookup(sessionClass) : null;
//     }
// 
//     public static QualitySession retrieveSession(Project project, QualityProvider provider) {
//         return provider != null ? retrieveSession(project, provider.getQualitySessionClass()) : null;
//     }
// 
//     public static Collection<? extends QualitySession> retrieveSessions(Project project) {
//         return project != null ? project.getLookup().lookupAll(QualitySession.class) : Collections.emptyList();
//     }
// 
//     public static QualitySession retrieveSessionFromDataObject(DataObject dataObject, QualityProvider provider) {
//         return provider != null ? retrieveSessionFromDataObject(dataObject, provider.getQualitySessionClass()) : null;
//     }
// 
//     public static <T extends QualitySession> T retrieveSessionFromDataObject(DataObject dataObject, Class<T> sessionClass) {
//         Project project = SQEProjectSupport.findProjectByDataObject(dataObject);
//         return project != null ? retrieveSession(project, sessionClass) : null;
//     }
// 
//     public static QualitySession retrieveSessionFromFileObject(FileObject fileObject, QualityProvider provider) {
//         return provider != null ? retrieveSessionFromFileObject(fileObject, provider.getQualitySessionClass()) : null;
//     }
// 
//     public static <T extends QualitySession> T retrieveSessionFromFileObject(FileObject fileObject, Class<T> sessionClass) {
//         Project project = SQEProjectSupport.findProjectByFileObject(fileObject);
//         return project != null ? retrieveSession(project, sessionClass) : null;
//     }
// 
// //    public static boolean isQualityProviderActive(FileObject fileObject, QualityProvider provider) {
// //        return provider != null && isQualityProviderEnabled(fileObject, provider.getQualitySessionClass());
// //    }
// //
// //    public static boolean isQualityProviderActive(Project project, QualityProvider provider) {
// //        return provider != null && isQualityProviderEnabled(project, provider.getQualitySessionClass());
// //    }
//     public static boolean isQualityProviderEnabledForDataObject(DataObject dataObject, Class<? extends QualitySession> sessionClass) {
//         return isQualityProviderEnabled(retrieveSessionFromDataObject(dataObject, sessionClass));
//     }
// 
//     public static boolean isQualityProviderEnabledForFileObject(FileObject fileObject, Class<? extends QualitySession> sessionClass) {
//         return isQualityProviderEnabled(retrieveSessionFromFileObject(fileObject, sessionClass));
//     }
// 
//     public static boolean isQualityProviderEnabled(Project project, QualityProvider provider) {
//         return provider != null && isQualityProviderEnabled(retrieveSession(project, provider.getQualitySessionClass()));
//     }
// 
//     public static boolean isQualityProviderEnabled(Project project, Class<? extends QualitySession> sessionClass) {
//         return isQualityProviderEnabled(retrieveSession(project, sessionClass));
//     }
// 
//     private static boolean isQualityProviderEnabled(QualitySession session) {
//         return session != null && session.isEnabled();
//     }
// 
//     public static boolean isAnnotateProjectResultEnabled(Project project, QualityProvider provider) {
//         return provider != null &&  isAnnotateProjectResultEnabled(retrieveSession(project, provider.getQualitySessionClass()));
//     }
// 
//     private static boolean isAnnotateProjectResultEnabled(QualitySession session) {
//         return session != null && session.isAnnotateProjectResultEnabled();
//     }
// 
// }


// Path: tools.checkstyle.codedefects.tasklist/src/org/nbheaven/sqe/tools/checkstyle/codedefects/tasklist/CheckstyleTaskProvider.java
import org.netbeans.spi.tasklist.PushTaskScanner;
import org.netbeans.spi.tasklist.Task;
import org.netbeans.spi.tasklist.TaskScanningScope;
import org.openide.filesystems.FileObject;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.nbheaven.sqe.codedefects.core.util.SQECodedefectSupport;
import org.nbheaven.sqe.core.utilities.SQEProjectSupport;
import org.nbheaven.sqe.tools.checkstyle.codedefects.core.CheckstyleResult;
import org.nbheaven.sqe.tools.checkstyle.codedefects.core.CheckstyleResult.ClassKey;
import org.nbheaven.sqe.tools.checkstyle.codedefects.core.CheckstyleSession;
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;


    @Override
    public synchronized void setScope(TaskScanningScope taskScanningScope, Callback callback) {
        if (taskScanningScope == null || callback == null) {
            return;
        }

        for (FileObject fileObject : taskScanningScope.getLookup().lookupAll(FileObject.class)) {
            if (SQECodedefectSupport.isQualityProviderEnabledForFileObject(fileObject, CheckstyleSession.class) && JavaSource.forFileObject(fileObject) != null) {
                CheckstyleResult result = getResult(fileObject);
                if (result == null) {
                    continue;
                }
                Project project = SQEProjectSupport.findProjectByFileObject(fileObject);
                Map<ClassKey, Collection<AuditEvent>> instanceByClass = result.getInstanceByClass();
                CheckstyleResult.ClassKey key = new CheckstyleResult.ClassKey(project, fileObject);
                Collection<AuditEvent> auditEvents = instanceByClass.get(key);
                if (auditEvents != null) { // SQE-57
                    callback.setTasks(fileObject, new LinkedList<>(getTasks(auditEvents, fileObject)));
                }
            }
        }

        for (Project project : taskScanningScope.getLookup().lookupAll(Project.class)) {
            CheckstyleResult result = getResult(project);
            if (result != null) {
                List<Task> tasks = new LinkedList<>();
                for (Map.Entry<ClassKey, Collection<AuditEvent>> classEntry : result.getInstanceByClass().entrySet()) {
                    tasks.addAll(getTasks(classEntry.getValue(), classEntry.getKey().getFileObject()));
                }
                callback.setTasks(project.getProjectDirectory(), tasks);