Package org.playframework.playclipse.builder

Source Code of org.playframework.playclipse.builder.PlayBuilder$ResourceVisitor

package org.playframework.playclipse.builder;

import java.io.ByteArrayInputStream;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.playframework.playclipse.FilesAccess;
import org.playframework.playclipse.PlayPlugin;
import org.playframework.playclipse.editors.html.HTMLEditor;
import org.playframework.playclipse.editors.route.RouteEditor;

import bran.japidplugin.TemplateTransformer;

public class PlayBuilder extends IncrementalProjectBuilder implements IPropertyChangeListener {

  /**
   *
   */
  private static final QualifiedName JAPID_DIR_CREATED = new QualifiedName("bran.play.japid", "japid.dir.created");

  public PlayBuilder() {
    super();
    PlayPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
  }

  class ResourceVisitor implements IResourceVisitor {
    @Override
    public boolean visit(IResource resource) {
      if (!(resource instanceof IFile))
        return true;
      IFile file = (IFile) resource;
      if (resource.getName().equals("routes")) {
        deleteMarkers(file);
        (new RouteChecker(file, RouteEditor.MISSING_ROUTE)).check();
        return false;
      }
     
      if (TemplateChecker.isTemplate(resource.getFullPath())) {
        deleteMarkers(file);
        (new TemplateChecker(file, HTMLEditor.MISSING_ACTION)).check();
        return false;
      }
      return true;
    }
  }

  void checkRoute() {
    IFile file = getProject().getFile("conf/routes");
    if (file.exists()) {
      deleteMarkers(file);
      new RouteChecker(file, RouteEditor.MISSING_ROUTE).check();
    }
  }
  public static final String BUILDER_ID = "org.playframework.playclipse.PlayBuilder";

  @Override
  protected IProject[] build(int kind, @SuppressWarnings("rawtypes") Map args, IProgressMonitor monitor) throws CoreException {
//    refreshJapidviews(monitor);
   
    IResourceDelta delta = getDelta(getProject());
    switch (kind) {
    case FULL_BUILD:
      System.out.println("-- full build");
      fullBuild(monitor);
      break;
    case INCREMENTAL_BUILD:
      System.out.println("-- inc build");
      if (delta == null) {
        fullBuild(monitor);
      } else {
        incrementalBuild(delta, monitor);
      }
      break;
    case AUTO_BUILD: // auto-incremental
      System.out.println("-- auto build");
      if (delta == null) {
        fullBuild(monitor);
      } else {
        incrementalBuild(delta, monitor);
      }
      break;
    default:
      break;

    }
    return null;
  }
 
 

  private void incrementalBuild(IResourceDelta delta, IProgressMonitor monitor) {
    try {
      ensureJapidViewsDir();
      delta.accept(new JapidDeltaVisitor());
      refreshJapidviews(monitor);
      checkRoute();
    } catch (CoreException e) {
      PlayPlugin.showError(e);
    }
  }



  /**
   * @param monitor
   * @throws CoreException
   */
  private void refreshJapidviews(IProgressMonitor monitor) throws CoreException {
    IFolder folder = getProject().getFolder("app/japidviews");
    if (folder.exists() ) {
      folder.refreshLocal(IFolder.DEPTH_INFINITE, monitor);
    }
    else {
      PlayPlugin.showError("app/japidviews folder does not exist.");
    }
  }

  private void log(String string) {
    System.out.println("PlayBuilder. " + string);
  }



  private void ensureJapidViewsDir() {
    IProject proj = getProject();
    FilesAccess.prepareFolder(proj.getFolder("app/japidviews/_layouts"));
    FilesAccess.prepareFolder(proj.getFolder("app/japidviews/_tags"));
    FilesAccess.prepareFolder(proj.getFolder("app/japidviews/_javatags"));
    FilesAccess.prepareFolder(proj.getFolder("app/japidviews/_notifiers"));
    IFile file = null;
//    file = proj.getFile("app/japidviews/_javatags/JapidWebUtil.java");
//    if (!file.exists()) {
//      try {
//        file.create(new ByteArrayInputStream(JAPID_WEB_UTIL.getBytes("UTF-8")), true, null);
//      } catch (Exception e) {
//        PlayPlugin.showError(e);
//      }
//    }

    try {
      String dirCreated = proj.getPersistentProperty(JAPID_DIR_CREATED);
      if (dirCreated != null) {
        // don't recreate the sample files
      }
      else {
        // create a sample layout and tag
        file = proj.getFile("app/japidviews/_layouts/SampleLayout.html");
        if (!file.exists()) {
          try {
            file.create(new ByteArrayInputStream(SAMPLE_LAYOUT.getBytes("UTF-8")), true, null);
          } catch (Exception e) {
            PlayPlugin.showError(e);
          }
        }
       
        file = proj.getFile("app/japidviews/_tags/SampleTag.html");
        if (!file.exists()) {
          try {
            file.create(new ByteArrayInputStream(SAMPLE_TAG.getBytes("UTF-8")), true, null);
          } catch (Exception e) {
            PlayPlugin.showError(e);
          }
        }

        proj.setPersistentProperty(JAPID_DIR_CREATED, Boolean.TRUE.toString());
      }
    } catch (CoreException e1) {
    }
   
  }
 
  private static final String SAMPLE_TAG = "`  args String a\n" +
      "Hi $a!\n" +
      "";
  private static final String SAMPLE_LAYOUT = "A sample layout generated by the Japid Eclipse plugin. Will be auto-regenerated.\n" +
      "<p>\n" +
      "#{get 'title'/};\n" +
      "</p>\n" +
      "<div>\n" +
      "#{doLayout /}\n" +
      "</div>\n" +
      "";

  private static final String JAPID_WEB_UTIL = "package japidviews._javatags;\n" +
      "\n" +
      "/**\n" +
      " * a well-know place to add all the static method you want to use in your\n" +
      " * templates.\n" +
      " * \n" +
      " * All the public static methods will be automatically \"import static \" to the\n" +
      " * generated Java classes by the Japid compiler.\n" +
      " * \n" +
      " */\n" +
      "public class JapidWebUtil {\n" +
      "  public static String hi() {\n" +
      "    return \"Hi\";\n" +
      "  }\n" +
      "  // your utility methods...\n" +
      "  \n" +
      "}\n" +
      "";

  void checkRoute(IFile file) {
  }

  private void deleteMarkers(IFile file) {
    try {
      file.deleteMarkers(IMarker.PROBLEM, false, IResource.DEPTH_ZERO);
    } catch (CoreException ce) {
    }
  }

  protected void fullBuild(final IProgressMonitor monitor) throws CoreException {
    ensureJapidViewsDir();
    TemplateTransformer.resetImports(getProject());
    try {
      getProject().accept(new ResourceVisitor());
      JapidFullBuildCollector batchCompiler = new JapidFullBuildCollector();
      getProject().accept(batchCompiler);
      batchCompiler.build(monitor);
    } catch (CoreException e) {
      e.printStackTrace();
    }
  }

  private static Set<String> observedProperties = new HashSet<String>();
  static {
    observedProperties.add(RouteEditor.MISSING_ROUTE);
    observedProperties.add(HTMLEditor.MISSING_ACTION);
  }

  @Override
  public void propertyChange(PropertyChangeEvent event) {
    for (String property : observedProperties) {
      if (event.getProperty().equals(property)) {
        try {
          fullBuild(null);
        } catch (CoreException e) {
        }
        return;
      }
    }
  }



  @Override
  protected void clean(IProgressMonitor monitor) throws CoreException {
    try {
      getProject().accept(new JapidCleanVisitor());
    } catch (CoreException e) {
      PlayPlugin.showError(e);
    }
  }

  public static void packageRenamingRefactor(Renaming renaming) {
    packageRenaming = renaming;
  }

  static Renaming packageRenaming = null;
 
}
TOP

Related Classes of org.playframework.playclipse.builder.PlayBuilder$ResourceVisitor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.