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

import org.eclipse.core.resources.IProject;
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.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.ProjectFacetsManager;

import com.google.gdt.eclipse.core.projects.IWebAppProjectCreator;
import com.google.gwt.eclipse.core.runtime.GwtSdk;
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) {
    javaProject = webAppProjectCreator.getCreatedJavaProject();
    if (javaProject == null) {
      return;
    }

    runJob(webAppProjectCreator);
  }

  private void runJob(final IWebAppProjectCreator webAppProjectCreator) {
    Job job = new Job("Install GWT Facet for Project") {
      @Override
      protected IStatus run(IProgressMonitor monitor) {
        installGwtFacet(webAppProjectCreator, monitor);
        
        return Status.OK_STATUS;
      }
    };
    job.schedule();
  }

  private void installGwtFacet(IWebAppProjectCreator webAppProjectCreator, IProgressMonitor monitor) {
    IProject project = webAppProjectCreator.getCreatedJavaProject().getProject();
    
    IFacetedProject facetedProject = null;
    try {
      facetedProject = ProjectFacetsManager.create(project);
    } catch (CoreException e) {
      GwtWtpPlugin.logError("Could not faceted project", e);
      return;
    }
    
    if (facetedProject == null) {
      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);
    }
  }

}
@@ -43,52 +43,8 @@
 s;%0A%0A
-import org.eclipse.core.resources.IProject;%0A
 impo
@@ -293,32 +293,106 @@
 e.IJavaProject;%0A
+import org.eclipse.wst.common.project.facet.core.FacetedProjectFramework;%0A
 import org.eclip
@@ -631,34 +631,122 @@
 et.core.
-ProjectFacetsManag
+events.IFacetedProjectEvent;%0Aimport org.eclipse.wst.common.project.facet.core.events.IFacetedProjectListen
 er;%0A%0Aimp
@@ -813,59 +813,8 @@
 or;%0A
-import com.google.gwt.eclipse.core.runtime.GwtSdk;%0A
 impo
@@ -1036,16 +1036,17 @@
 roject;%0A
+ 
 %0A  @Over
@@ -1133,24 +1133,55 @@
 tCreator) %7B%0A
+    // Just in case exit early%0A
     javaProj
@@ -1273,132 +1273,613 @@
     
-return;%0A    %7D%0A%0A    runJob(webAppProjectCreator);%0A  %7D%0A%0A  private void runJob(final IWebAppProjectCreator webAppProjectCreator
+GwtWtpPlugin.logError(%22Not installing the GWT Facet b/c it's not a Java project%22);%0A      return;%0A    %7D%0A%0A    // wait till the faceted project is setup%0A    listenForFacetedProjectChange();%0A  %7D%0A%0A  private void listenForFacetedProjectChange() %7B%0A    FacetedProjectFramework.addListener(new IFacetedProjectListener() %7B%0A      @Override%0A      public void handleEvent(IFacetedProjectEvent event) %7B%0A        IFacetedProject facetedProject = event.getProject();%0A        runJob(facetedProject);%0A      %7D%0A    %7D, IFacetedProjectEvent.Type.PROJECT_MODIFIED);%0A  %7D%0A%0A  private void runJob(final IFacetedProject facetedProject
 ) %7B%0A
@@ -2031,36 +2031,30 @@
 wtFacet(
-webAppProjectCreator
+facetedProject
 , monito
@@ -2057,24 +2057,16 @@
 nitor);%0A
-        
 %0A       
@@ -2167,186 +2167,75 @@
 et(I
-WebAppProjectCreator webAppProjectCreator, IProgressMonitor monitor) %7B%0A    IProject project = webAppProjectCreator.getCreatedJavaProject().getProject();%0A    %0A    IFacetedProject 
+FacetedProject facetedProject, IProgressMonitor monitor) %7B%0A    if (
 face
@@ -2250,237 +2250,77 @@
 ct =
+=
  null
-;%0A    try %7B%0A      facetedProject = ProjectFacetsManager.create(project);%0A    %7D catch (CoreException e) %7B%0A      GwtWtpPlugin.logError(%22Could not faceted project%22, e);%0A      return;%0A    %7D%0A    %0A    if (facetedProject == null) %7B
+) %7B%0A      GwtWtpPlugin.logError(%22Project is not faceted yet.%22);
 %0A   
@@ -2336,20 +2336,16 @@
 ;%0A    %7D%0A
-    
 %0A    IPr
@@ -2395,20 +2395,16 @@
 acet();%0A
-    
 %0A    IFa

Fix adding GWT facet on project creation.