public static IWorkspaceRoot setupWorkspace()
      throws CoreException, IOException, InvocationTargetException, InterruptedException {
    if (isSetup) {
      clearDoiModel();
      return workspaceRoot;
    }
    taskscape = new InteractionContext(HELPER_CONTEXT_ID, new InteractionContextScaling());

    workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

    project1 = ContextTestUtil.createJavaPluginProjectFromZip("project1", "project1.zip");
    project2 = ContextTestUtil.createJavaPluginProjectFromZip("project2", "project2.zip");

    jdtCoreDomProject = new TestJavaProject("workspace-helper-project");
    IPackageFragment jdtCoreDomPkg = jdtCoreDomProject.createPackage("org.eclipse.jdt.core.dom");
    IType astNodeType =
        jdtCoreDomProject.createType(jdtCoreDomPkg, "ASTNode.java", "public class ASTNode { }");
    astNodeType.createMethod(
        "public final void setSourceRange(int startPosition, int length) { }", null, false, null);
    isSetup = true;

    project1.open(new NullProgressMonitor());
    project2.open(new NullProgressMonitor());
    jdtCoreDomProject.getJavaProject().open(new NullProgressMonitor());

    return workspaceRoot;
  }
 /**
  * Checks if all source files are existing. If not, create them.
  *
  * @param javaProj
  */
 private void checkSourceFolders(final IJavaProject javaProj) {
   if (javaProj == null) return;
   if (javaProj.exists()) {
     try {
       if (!javaProj.isOpen()) {
         javaProj.open(new NullProgressMonitor());
       }
       IClasspathEntry[] entries = javaProj.getRawClasspath();
       for (IClasspathEntry entry : entries) {
         if (IClasspathEntry.CPE_SOURCE == entry.getEntryKind()) {
           IPath path = entry.getPath();
           final IPath folderPath = path.removeFirstSegments(1);
           if (!folderPath.isEmpty()) {
             Display.getDefault()
                 .asyncExec(
                     new Runnable() {
                       public void run() {
                         try {
                           ImportUtils.createFolders(
                               javaProj.getProject(), folderPath, IResource.FORCE);
                         } catch (CoreException e) {
                           _log.error(e.getMessage());
                         }
                       }
                     });
           }
         }
       }
     } catch (JavaModelException e) {
       _log.error("findProjectSources: Could not retrieve project sources:", e); // $NON-NLS-1$
     }
   }
 }
Example #3
0
  /**
   * Finds a compilation unit by looking in all the java project of the supplied name.
   *
   * @param project The name of the project to locate the file in.
   * @param file The src dir relative file path to find.
   * @return The compilation unit or null if not found.
   */
  public static ICompilationUnit findCompilationUnit(String project, String file) throws Exception {
    IPath path = Path.fromOSString(file);

    IJavaProject javaProject = getJavaProject(project);
    javaProject.open(null);
    // javaProject.getResource().refreshLocal(IResource.DEPTH_INFINITE, null);

    ICompilationUnit src = (ICompilationUnit) javaProject.findElement(path);

    return src;
  }
  public static IJavaProject importProject(final String projectPath, final String projectName)
      throws InvocationTargetException, CoreException, IOException {
    importFromZip(getFile(new Path(projectPath)), new Path(projectName), new NullProgressMonitor());

    final IProject testProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    testProject.open(new NullProgressMonitor());
    final IJavaProject jproject = JavaCore.create(testProject);
    if (!testProject.hasNature(JavaCore.NATURE_ID)) {
      addNatureToProject(testProject);
    }
    jproject.open(null);
    return jproject;
  }
Example #5
0
  /**
   * Finds a compilation unit by looking in all the available java projects.
   *
   * @param file The src directory relative file to find.
   * @return The compilation unit or null if not found.
   */
  public static ICompilationUnit findCompilationUnit(String file) throws Exception {
    IPath path = Path.fromOSString(file);
    IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
    for (IProject project : projects) {
      if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject javaProject = getJavaProject(project);
        javaProject.open(null);
        // javaProject.getResource().refreshLocal(IResource.DEPTH_INFINITE, null);

        ICompilationUnit src = (ICompilationUnit) javaProject.findElement(path);
        if (src != null) {
          return src;
        }
      }
    }
    return null;
  }
Example #6
0
  /**
   * Creates a JavaProject prom the given project
   *
   * @param project
   * @return the created JavaProject
   */
  public IJavaProject createJavaProject(IProject project) {
    try {
      // addJavaNature(project);
      IJavaProject javaProject = JavaCore.create(project);
      if (!javaProject.isOpen()) {
        javaProject.open(null);
      }
      createBinFolder(project, javaProject);
      clearSourcePath(javaProject);

      IClasspathEntry sourceFolder = createSourceFolder("src", project);

      addToClasspath(javaProject, sourceFolder);
      addJREContainerToProject(javaProject);
      return javaProject;
    } catch (CoreException e) {
      e.printStackTrace();
    }
    return null;
  }
Example #7
0
  /**
   * Convert the empty project to an Acceleo project.
   *
   * @param project The newly created project.
   * @param selectedJVM The name of the selected JVM (J2SE-1.5 or JavaSE-1.6 recommended).
   * @param allModules The description of the module that need to be created.
   * @param shouldGenerateModules Indicates if we should generate the modules in the project or not.
   *     The wizard container to display the progress monitor
   * @param monitor The monitor.
   */
  public static void convert(
      IProject project,
      String selectedJVM,
      List<AcceleoModule> allModules,
      boolean shouldGenerateModules,
      IProgressMonitor monitor) {
    String generatorName = computeGeneratorName(project.getName());
    AcceleoProject acceleoProject = AcceleowizardmodelFactory.eINSTANCE.createAcceleoProject();
    acceleoProject.setName(project.getName());
    acceleoProject.setGeneratorName(generatorName);

    // Default JRE value
    acceleoProject.setJre(selectedJVM);
    if (acceleoProject.getJre() == null && acceleoProject.getJre().length() == 0) {
      acceleoProject.setJre("J2SE-1.5"); // $NON-NLS-1$		
    }

    if (shouldGenerateModules) {
      for (AcceleoModule acceleoModule : allModules) {
        String parentFolder = acceleoModule.getParentFolder();
        IProject moduleProject =
            ResourcesPlugin.getWorkspace().getRoot().getProject(acceleoModule.getProjectName());
        if (moduleProject.exists()
            && moduleProject.isAccessible()
            && acceleoModule.getModuleElement() != null
            && acceleoModule.getModuleElement().isIsMain()) {
          IPath parentFolderPath = new Path(parentFolder);
          IFolder folder = moduleProject.getFolder(parentFolderPath.removeFirstSegments(1));
          acceleoProject
              .getExportedPackages()
              .add(
                  folder
                      .getProjectRelativePath()
                      .removeFirstSegments(1)
                      .toString()
                      .replaceAll(
                          "/", //$NON-NLS-1$
                          "\\.")); //$NON-NLS-1$
        }
        // Calculate project dependencies
        List<String> metamodelURIs = acceleoModule.getMetamodelURIs();
        for (String metamodelURI : metamodelURIs) {
          // Find the project containing this metamodel and add a dependency to it.
          EPackage ePackage = AcceleoPackageRegistry.INSTANCE.getEPackage(metamodelURI);
          if (ePackage != null && !(ePackage instanceof EcorePackage)) {
            Bundle bundle = AcceleoWorkspaceUtil.getBundle(ePackage.getClass());
            acceleoProject.getPluginDependencies().add(bundle.getSymbolicName());
          }
        }
      }
    }

    try {
      IProjectDescription description = project.getDescription();
      description.setNatureIds(
          new String[] {
            JavaCore.NATURE_ID,
            IBundleProjectDescription.PLUGIN_NATURE,
            IAcceleoConstants.ACCELEO_NATURE_ID,
          });
      project.setDescription(description, monitor);

      IJavaProject iJavaProject = JavaCore.create(project);

      // Compute the JRE
      List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
      IExecutionEnvironmentsManager executionEnvironmentsManager =
          JavaRuntime.getExecutionEnvironmentsManager();
      IExecutionEnvironment[] executionEnvironments =
          executionEnvironmentsManager.getExecutionEnvironments();
      for (IExecutionEnvironment iExecutionEnvironment : executionEnvironments) {
        if (acceleoProject.getJre().equals(iExecutionEnvironment.getId())) {
          entries.add(
              JavaCore.newContainerEntry(JavaRuntime.newJREContainerPath(iExecutionEnvironment)));
          break;
        }
      }

      // PDE Entry (will not be generated anymore)
      entries.add(
          JavaCore.newContainerEntry(
              new Path("org.eclipse.pde.core.requiredPlugins"))); // $NON-NLS-1$

      // Sets the input / output folders
      IFolder target = project.getFolder("src"); // $NON-NLS-1$
      if (!target.exists()) {
        target.create(true, true, monitor);
      }

      IFolder classes = project.getFolder("bin"); // $NON-NLS-1$
      if (!classes.exists()) {
        classes.create(true, true, monitor);
      }

      iJavaProject.setOutputLocation(classes.getFullPath(), monitor);
      IPackageFragmentRoot packageRoot = iJavaProject.getPackageFragmentRoot(target);
      entries.add(
          JavaCore.newSourceEntry(
              packageRoot.getPath(), new Path[] {}, new Path[] {}, classes.getFullPath()));

      iJavaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
      iJavaProject.open(monitor);

      AcceleoProjectUtils.generateFiles(
          acceleoProject, allModules, project, shouldGenerateModules, monitor);

      // Default settings
      AcceleoBuilderSettings settings = new AcceleoBuilderSettings(project);
      settings.setCompilationKind(AcceleoBuilderSettings.COMPILATION_PLATFORM_RESOURCE);
      settings.setResourceKind(AcceleoBuilderSettings.BUILD_XMI_RESOURCE);
      settings.save();
    } catch (CoreException e) {
      AcceleoUIActivator.log(e, true);
    }
  }