package com.google.gwt.eclipse.wtp.projects;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.wst.common.project.facet.core.FacetedProjectFramework;
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.IFacetedProjectWorkingCopy;
import org.eclipse.wst.common.project.facet.core.IProjectFacet;
import org.eclipse.wst.common.project.facet.core.events.IFacetedProjectEvent;
import org.eclipse.wst.common.project.facet.core.events.IFacetedProjectListener;

import com.google.gdt.eclipse.core.projects.IWebAppProjectCreator;
import com.google.gwt.eclipse.wtp.GwtWtpPlugin;
import com.google.gwt.eclipse.wtp.utils.GwtFacetUtils;

public class WtpFacetCreatorParicipant implements IWebAppProjectCreator.Participant {

  private IJavaProject javaProject;
 
  @Override
  public void updateWebAppProjectCreator(IWebAppProjectCreator webAppProjectCreator) {
    // Just in case exit early
    javaProject = webAppProjectCreator.getCreatedJavaProject();
    if (javaProject == null) {
      GwtWtpPlugin.logError("Not installing the GWT Facet b/c it's not a Java project");
      return;
    }

    // wait till the faceted project is setup
    listenForFacetedProjectChange();
  }

  private void listenForFacetedProjectChange() {
    FacetedProjectFramework.addListener(new IFacetedProjectListener() {
      @Override
      public void handleEvent(IFacetedProjectEvent event) {
        IFacetedProject facetedProject = event.getProject();
        runJob(facetedProject);
      }
    }, IFacetedProjectEvent.Type.PROJECT_MODIFIED);
  }

  private void runJob(final IFacetedProject facetedProject) {
    Job job = new Job("Install GWT Facet for Project") {
      @Override
      protected IStatus run(IProgressMonitor monitor) {
        installGwtFacet(facetedProject, monitor);

        return Status.OK_STATUS;
      }
    };
    job.schedule();
  }

  private void installGwtFacet(IFacetedProject facetedProject, IProgressMonitor monitor) {
    if (facetedProject == null) {
      GwtWtpPlugin.logError("Project is not faceted yet.");
      return;
    }

    IProjectFacet gwtFacet = GwtFacetUtils.getGwtFacet();

    IFacetedProjectWorkingCopy workingCopy = facetedProject.createWorkingCopy();
    workingCopy.addProjectFacet(gwtFacet.getDefaultVersion());
    try {
      workingCopy.commitChanges(monitor);
    } catch (CoreException e) {
      GwtWtpPlugin.logError("Could not commit facet to project.", e);
    }
  }

}
@@ -1036,16 +1036,72 @@
 roject;%0A
+  private IFacetedProjectListener projectFacetListener;%0A
  %0A  @Ove
@@ -1574,43 +1574,28 @@
     
-FacetedProjectFramework.add
+projectFacet
 Listener
 (new
@@ -1590,17 +1590,19 @@
 Listener
-(
+ = 
 new IFac
@@ -1697,24 +1697,155 @@
 nt event) %7B%0A
+        // prevent recursion and only do this once.%0A        FacetedProjectFramework.removeListener(projectFacetListener);%0A        %0A
         IFac
@@ -1938,16 +1938,78 @@
  %7D%0A    %7D
+;%0A    FacetedProjectFramework.addListener(projectFacetListener
 , IFacet
@@ -2047,16 +2047,26 @@
 IFIED);%0A
+    %0A    %0A
   %7D%0A%0A  p

Remove project configurator listener