package org.bndtools.builder;

import java.util.Collection;

import org.bndtools.api.BndtoolsConstants;
import org.bndtools.api.ILogger;
import org.bndtools.api.Logger;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

import aQute.bnd.build.Project;
import aQute.bnd.build.Workspace;
import bndtools.central.Central;

public class CnfWatcher implements IResourceChangeListener {
    private static final ILogger logger = Logger.getLogger(CnfWatcher.class);
    private static final Path CNFPATH = new Path("/cnf");

    static CnfWatcher install() {
        CnfWatcher cnfw = new CnfWatcher();
        ResourcesPlugin.getWorkspace().addResourceChangeListener(cnfw);
        return cnfw;
    }

    @Override
    public void resourceChanged(IResourceChangeEvent event) {

        if (event.getType() != IResourceChangeEvent.POST_CHANGE)
            return;

        IResourceDelta delta = event.getDelta();
        if (delta.findMember(CNFPATH) == null)
            return;

        try {
            final Workspace workspace = Central.getWorkspace();
            Collection<Project> allProjects = workspace.getAllProjects();
            if (allProjects.isEmpty())
                return;

            Project p = allProjects.iterator().next();
            DeltaWrapper dw = new DeltaWrapper(p, delta, new BuildLogger(0));
            if (dw.hasCnfChanged()) {

                workspace.clear();
                workspace.forceRefresh();
                workspace.getPlugins();

                BndtoolsBuilder.dirty.addAll(workspace.getAllProjects());

                WorkspaceJob j = new WorkspaceJob("Update errors on workspace") {

                    @Override
                    public IStatus runInWorkspace(IProgressMonitor arg0) throws CoreException {
                        try {
                            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                            IProject project = root.getProject(Workspace.CNFDIR);
                            if (project != null) {
                                MarkerSupport ms = new MarkerSupport(project);
                                ms.deleteMarkers("*");
                                ms.setMarkers(workspace, BndtoolsConstants.MARKER_BND_WORKSPACE_PROBLEM);
                            }
                            return Status.OK_STATUS;
                        } catch (Exception e) {
                            return new Status(IStatus.ERROR, BndtoolsConstants.CORE_PLUGIN_ID, "updating errors for workspace", e);
                        }
                    }
                };
                j.schedule();
            }
        } catch (Exception e) {
            logger.logError("Detecting changes in cnf failed, ignoring", e);
        }
    }
}

@@ -154,32 +154,84 @@
 ols.api.Logger;%0A
+import org.bndtools.utils.workspace.WorkspaceUtils;%0A
 import org.eclip
@@ -427,58 +427,8 @@
 ta;%0A
-import org.eclipse.core.resources.IWorkspaceRoot;%0A
 impo
@@ -664,46 +664,8 @@
 us;%0A
-import org.eclipse.core.runtime.Path;%0A
 impo
@@ -969,38 +969,45 @@
 nal 
-Path CNFPATH = new Path(%22/cnf%22
+CnfWatcher INSTANCE = new CnfWatcher(
 );%0A%0A
@@ -1044,52 +1044,8 @@
 ) %7B%0A
-        CnfWatcher cnfw = new CnfWatcher();%0A
     
@@ -1109,185 +1109,432 @@
 ner(
-cnfw);%0A        return cnfw;%0A    %7D%0A%0A    @Override%0A    public void resourceChanged(IResourceChangeEvent event) %7B%0A%0A        if (event.getType() != IResourceChangeEvent.POST_CHANGE)%0A
+INSTANCE, IResourceChangeEvent.POST_CHANGE);%0A        return INSTANCE;%0A    %7D%0A%0A    private CnfWatcher() %7B%7D%0A%0A    @Override%0A    public void resourceChanged(IResourceChangeEvent event) %7B%0A        try %7B%0A            final Workspace workspace = Central.getWorkspace();%0A            final IProject cnfProject = WorkspaceUtils.findCnfProject(ResourcesPlugin.getWorkspace().getRoot(), workspace);%0A            if (cnfProject == null)%0A    
     
@@ -1542,32 +1542,36 @@
        return;%0A%0A
+    
         IResourc
@@ -1607,24 +1607,28 @@
 ();%0A        
+   
+ 
 if (delta.fi
@@ -1640,124 +1640,67 @@
 ber(
-CNFPATH) == null)%0A            return;%0A%0A        try %7B%0A            final Workspace workspace = Central.getWorkspace();
+cnfProject.getFullPath()) == null)%0A                return;%0A
 %0A   
@@ -2001,25 +2001,24 @@
 hanged()) %7B%0A
-%0A
             
@@ -2168,30 +2168,17 @@
 .addAll(
-workspace.getA
+a
 llProjec
@@ -2179,18 +2179,16 @@
 Projects
-()
 );%0A%0A    
@@ -2265,17 +2265,16 @@
 ace%22) %7B%0A
-%0A
         
@@ -2425,237 +2425,8 @@
 y %7B%0A
-                            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();%0A                            IProject project = root.getProject(Workspace.CNFDIR);%0A                            if (project != null) %7B%0A    
     
@@ -2486,17 +2486,20 @@
 Support(
-p
+cnfP
 roject);
@@ -2499,20 +2499,16 @@
 oject);%0A
-    
         
@@ -2578,20 +2578,16 @@
         
-    
 ms.setMa
@@ -2656,38 +2656,8 @@
 M);%0A
-                            %7D%0A
     
@@ -2826,23 +2826,16 @@
 ools
-Constants.CORE_
+Builder.
 PLUG

Fix NPE when cnf project was deleted from the workspace