Example usage for org.eclipse.jdt.core IJavaProject getPackageFragmentRoot

List of usage examples for org.eclipse.jdt.core IJavaProject getPackageFragmentRoot

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getPackageFragmentRoot.

Prototype

IPackageFragmentRoot getPackageFragmentRoot(IResource resource);

Source Link

Document

Returns a package fragment root for the given resource, which must either be a folder representing the top of a package hierarchy, or a ZIP archive (e.g.

Usage

From source file:com.windowtester.swt.codegen.wizards.NewTestTypeWizardPage.java

License:Open Source License

public IProject createJavaProject(String projectName, String sourcePath, IProgressMonitor monitor)
        throws CoreException {
    IProject project = getWorkspaceRoot().getProject(projectName);
    final IFolder sourceFolder;
    final IJavaProject javaProject;
    if (!project.exists()) {
        sourceFolder = project.getFolder(new Path(sourcePath));

        ProjectUtil.createProject(project, null, monitor);
        ProjectUtil.addNature(project, JavaCore.NATURE_ID, monitor);
        javaProject = JavaCore.create(project);
        ProjectUtil.addDefaultEntries(javaProject, sourceFolder, monitor);
    } else {/*from  w  w  w  .  j a v a 2s  . c  om*/
        javaProject = JavaCore.create(project);
        sourceFolder = project.getFolder(new Path(sourcePath));
        if (!sourceFolder.exists()) {
            ProjectUtil.addDefaultEntries(javaProject, sourceFolder, monitor);
        }
    }

    Display display = PlatformUI.getWorkbench().getDisplay();
    display.syncExec(new Runnable() {
        public void run() {
            setPackageFragmentRoot(javaProject.getPackageFragmentRoot(sourceFolder), true);
        }
    });
    return project;
}

From source file:de.akra.idocit.java.utils.JavaTestUtils.java

License:Apache License

/**
 * Creates and initializes a IJavaProject within the currently running test workspace.
 * /*from   ww  w.ja v  a 2 s.  c o  m*/
 * @destination The current test workspace.
 * @param projectName
 *            [PRIMARY_KEY]
 * @param filesToAdd
 *            [ATTRIBUTE] files to add to the test workspace
 * @return [OBJECT] the created project.
 * @throws CoreException
 * @throws FileNotFoundException
 * @thematicgrid Putting Operations
 */
public static IProject initProjectInWorkspace(final String projectName, final Collection<File> filesToAdd)
        throws CoreException, FileNotFoundException {
    /*
     * The implementation of the initialization of the Test-Java Project has been
     * guided by http://sdqweb.ipd.kit.edu/wiki/JDT_Tutorial:
     * _Creating_Eclipse_Java_Projects_Programmatically.
     * 
     * Thanks to the authors.
     */

    // Create Java Project
    final IProgressMonitor progressMonitor = new NullProgressMonitor();
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    final IProject project = root.getProject(projectName);
    project.create(progressMonitor);
    project.open(progressMonitor);

    final IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
    final IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(progressMonitor);

    // Add Java Runtime to the project
    final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    // Add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // Create source folder
    final IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, progressMonitor);

    final IPackageFragmentRoot fragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    final IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(fragmentRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    // Create the package
    final IFolder packageFolder = srcFolder.getFolder("source");
    packageFolder.create(true, true, progressMonitor);

    // Create Java files
    for (final File file : filesToAdd) {
        final IFile customerWorkspaceFile = packageFolder.getFile(file.getName());
        customerWorkspaceFile.create(new FileInputStream(file), true, progressMonitor);
    }

    project.refreshLocal(IProject.DEPTH_INFINITE, progressMonitor);
    return project;
}

From source file:de.akra.idocit.ui.components.DocumentationEditorTest.java

License:Apache License

@Before
public void setupWorkspace() throws CoreException, IOException {
    /*/*from   w  w  w. j  a  v  a2s . c  o m*/
     * The implementation of the initialization of the Test-Java Project has been
     * guided by http://sdqweb.ipd.kit.edu/wiki/JDT_Tutorial:
     * _Creating_Eclipse_Java_Projects_Programmatically.
     * 
     * Thanks to the authors.
     */

    // Create Java Project
    IProgressMonitor progressMonitor = new NullProgressMonitor();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(PROJECT_NAME);
    project.create(progressMonitor);
    project.open(progressMonitor);

    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
    IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(progressMonitor);

    // Add Java Runtime to the project
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    // Add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // Create source folder
    IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, progressMonitor);

    IPackageFragmentRoot fragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(fragmentRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    // Create the package
    IFolder packageFolder = srcFolder.getFolder("source");
    packageFolder.create(true, true, progressMonitor);

    // Create Java file
    File customerFile = new File(SOURCE_DIR + "EmptyInterface.java");
    IFile customerWorkspaceFile = packageFolder.getFile("EmptyInterface.java");

    FileInputStream javaStream = null;

    try {
        javaStream = new FileInputStream(customerFile);
        customerWorkspaceFile.create(javaStream, true, progressMonitor);
    } finally {
        if (javaStream != null) {
            javaStream.close();
        }
    }

    project.refreshLocal(IProject.DEPTH_INFINITE, progressMonitor);
}

From source file:de.loskutov.bco.ui.JdtUtils.java

License:Open Source License

/**
 * @param project/*from  w  w  w .j  a va  2 s .  c o m*/
 * @param pack
 * @return true if 'pack' argument is package root
 * @throws JavaModelException
 */
private static boolean isPackageRoot(IJavaProject project, IResource pack) throws JavaModelException {
    boolean isRoot = false;
    if (project == null || pack == null || !(pack instanceof IContainer)) {
        return isRoot;
    }
    IPackageFragmentRoot root = project.getPackageFragmentRoot(pack);
    IClasspathEntry clPathEntry = null;
    if (root != null) {
        clPathEntry = root.getRawClasspathEntry();
    }
    isRoot = clPathEntry != null;
    return isRoot;
}

From source file:de.loskutov.eclipse.jdepend.views.TreeObject.java

License:Open Source License

public static boolean isPackageRoot(IJavaProject project, IResource pack) throws JavaModelException {
    boolean isRoot = false;
    if (project == null || pack == null) {
        return isRoot;
    }//from www.  j  a v a 2 s  . co m
    IPackageFragmentRoot root = project.getPackageFragmentRoot(pack);
    IClasspathEntry clPathEntry = null;
    if (root != null) {
        clPathEntry = root.getRawClasspathEntry();
    }
    isRoot = clPathEntry != null;
    return isRoot;
}

From source file:de.ovgu.featureide.core.framework.FrameworkProjectCreator.java

License:Open Source License

/**
 * Creates a new subproject inside a folder
 * /*from  w w  w.j  ava  2  s  .  c  o m*/
 * @param name - project name
 * @param destination - folder which contains the subproject
 * @throws CoreException
 */
public static void createSubprojectFolder(String name, IFolder destination) throws CoreException {
    final IProjectDescription description = ResourcesPlugin.getWorkspace().newProjectDescription(name);
    description.setLocation(destination.getLocation());

    final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
    if (project.exists()) {
        return;
    }
    project.create(description, null);
    project.open(null);

    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);

    final IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(null);

    final IFolder binFolder = project.getFolder("bin");
    binFolder.create(true, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (final LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
    final IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    final IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
    final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    final int oldLength = oldEntries.length;
    final IClasspathEntry[] newEntries = new IClasspathEntry[oldLength + 2];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldLength] = JavaCore.newSourceEntry(srcRoot.getPath());
    newEntries[oldLength + 1] = JavaCore.newProjectEntry(destination.getProject().getFullPath());
    javaProject.setRawClasspath(newEntries, null);

    final IFile infoXML = destination.getFile("info.xml");
    if (!infoXML.exists()) {
        createInfoXML(destination);
    }
}

From source file:de.ovgu.featureide.core.mpl.io.writer.JavaProjectWriter.java

License:Open Source License

public void buildJavaProject(IFile featureListFile, String name) {
    if (interfaceProject != null) {
        //TODO MPL: save as ids
        int[] featureList = interfaceProject.getFeatureIDs((new FeatureListReader(featureListFile)).read());
        if (featureList != null) {
            final ProjectSignatures projectSignatures = interfaceProject.getProjectSignatures();

            SignatureIterator it = projectSignatures.createIterator();
            it.addFilter(new FeatureFilter(featureList));
            it.addFilter(new ViewTagFilter(interfaceProject.getFilterViewTag()));
            ProjectStructure javaSig = new ProjectStructure(it);

            IJavaProject javaProject = createJavaProject(interfaceProject.getProjectReference().getName()
                    + "_java_" + name.substring(name.lastIndexOf('_') + 1));
            if (javaProject != null) {
                for (AbstractClassFragment cls : javaSig.getClasses()) {
                    IFolder sourceFolder = javaProject.getProject().getFolder(Path.fromPortableString("src"));
                    try {
                        IPackageFragment featurePackage = javaProject.getPackageFragmentRoot(sourceFolder)
                                .createPackageFragment(cls.getSignature().getPackage(), true, null);
                        featurePackage.createCompilationUnit(cls.getSignature().getName() + ".java",
                                cls.toString(), true, null);
                    } catch (JavaModelException e) {
                        MPLPlugin.getDefault().logError(e);
                    }//  w  w w  .j a v  a2  s .com
                }
                MPLPlugin.getDefault().logInfo("Created Java Project");
            }
        }
    }
}

From source file:eu.artist.migration.modernization.uml2java.repackaged.gen.java.services.WorkspaceServices.java

License:Open Source License

/**
 * Creates a project from scratch in the workspace.
 * /*ww w  .  j  a va2 s.co  m*/
 * @param eObject
 *            The model element
 */
public void createDefaultProject(EObject eObject) {
    if (!EMFPlugin.IS_ECLIPSE_RUNNING) {
        return;
    }

    IProgressMonitor monitor = new NullProgressMonitor();
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    try {
        IWorkspaceRoot workspaceRoot = workspace.getRoot();

        String projectName = UML2JavaConfigurationHolder.getDefaultProjectName(eObject);
        IProject project = workspaceRoot.getProject(projectName);

        if (project.exists() && project.isAccessible()) {
            if (!project.isOpen()) {
                project.open(monitor);
            }
        } else {
            project.create(new NullProgressMonitor());
            project.open(new NullProgressMonitor());

            IContainer intputContainer = project;

            String sourceFolderName = UML2JavaConfigurationHolder.getSourceFolderPath(eObject);
            StringTokenizer stringTokenizer = new StringTokenizer(sourceFolderName, "/");
            while (stringTokenizer.hasMoreTokens()) {
                String token = stringTokenizer.nextToken();
                IFolder src = intputContainer.getFolder(new Path(token));
                if (!src.exists()) {
                    src.create(true, true, monitor);
                }

                intputContainer = src;
            }

            IContainer outputContainer = project;

            String outputFolderName = UML2JavaConfigurationHolder.getOutputFolderPath(eObject);
            stringTokenizer = new StringTokenizer(outputFolderName, "/");
            while (stringTokenizer.hasMoreTokens()) {
                String token = stringTokenizer.nextToken();
                IFolder out = outputContainer.getFolder(new Path(token));
                if (!out.exists()) {
                    out.create(true, true, monitor);
                }

                outputContainer = out;
            }

            IProjectDescription description = project.getDescription();
            String[] natures = new String[] {};
            if (IUML2JavaConstants.Default.DEFAULT_COMPONENT_ARTIFACTS_TYPE_OSGI
                    .equals(UML2JavaConfigurationHolder.getComponentBasedArchitecture(eObject))
                    || IUML2JavaConstants.Default.DEFAULT_COMPONENT_ARTIFACTS_TYPE_ECLIPSE
                            .equals(UML2JavaConfigurationHolder.getComponentBasedArchitecture(eObject))) {
                natures = new String[] { JavaCore.NATURE_ID, IUML2JavaConstants.PDE_PLUGIN_NATURE_ID };
            } else {
                natures = new String[] { JavaCore.NATURE_ID, };
            }
            description.setNatureIds(natures);
            project.setDescription(description, monitor);

            IJavaProject javaProject = JavaCore.create(project);

            List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
            IExecutionEnvironmentsManager executionEnvironmentsManager = JavaRuntime
                    .getExecutionEnvironmentsManager();
            IExecutionEnvironment[] executionEnvironments = executionEnvironmentsManager
                    .getExecutionEnvironments();

            String defaultJREExecutionEnvironment = UML2JavaConfigurationHolder
                    .getJREExecutionEnvironment(eObject);
            for (IExecutionEnvironment iExecutionEnvironment : executionEnvironments) {
                if (defaultJREExecutionEnvironment.equals(iExecutionEnvironment.getId())) {
                    entries.add(
                            JavaCore.newContainerEntry(JavaRuntime.newJREContainerPath(iExecutionEnvironment)));
                    break;
                }
            }

            javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

            IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
            IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
            System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);

            javaProject.setOutputLocation(outputContainer.getFullPath(), monitor);

            IPackageFragmentRoot packageRoot = javaProject
                    .getPackageFragmentRoot(intputContainer.getFullPath().toString());
            newEntries[oldEntries.length] = JavaCore.newSourceEntry(packageRoot.getPath(), new Path[] {},
                    new Path[] {}, outputContainer.getFullPath());

            javaProject.setRawClasspath(newEntries, null);

            IFile buildPropertiesFile = project.getFile("build.properties");
            if (!buildPropertiesFile.exists()) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(
                        "#################################################################################"
                                + System.getProperty("line.separator"));
                stringBuilder.append("## " + UML2JavaConfigurationHolder.getCopyrightAndLicense(eObject)
                        + System.getProperty("line.separator"));
                stringBuilder.append(
                        "#################################################################################"
                                + System.getProperty("line.separator"));
                stringBuilder.append("source.. = " + UML2JavaConfigurationHolder.getSourceFolderPath(eObject)
                        + System.getProperty("line.separator"));
                stringBuilder.append("output.. = " + UML2JavaConfigurationHolder.getOutputFolderPath(eObject)
                        + System.getProperty("line.separator"));
                stringBuilder.append("" + System.getProperty("line.separator"));
                buildPropertiesFile.create(new ByteArrayInputStream(stringBuilder.toString().getBytes()), true,
                        monitor);
            }
        }
    } catch (CoreException coreException) {
        AcceleoEnginePlugin.log(coreException, true);
    }
}

From source file:fede.workspace.eclipse.java.manager.JavaProjectContentManager.java

License:Apache License

/**
 * Gets the java source element.//from  w w w  . j a  v  a2s. c om
 * 
 * @param cxt
 *            the cxt
 * 
 * @return the java source element
 */
public IPackageFragmentRoot getJavaSourceElement(ContextVariable cxt) {
    if (!sourcefolder.isNull()) {

        IFolder source = getProject(cxt).getFolder(sourcefolder.compute(cxt, getOwnerItem()));
        IJavaProject jp = getJavaProject(cxt);
        if (jp != null) {
            return jp.getPackageFragmentRoot(source);
        }
    }
    return null;
}

From source file:fr.imag.adele.cadse.cadseg.contents.CadseDefinitionContent.java

License:Apache License

/**
 * Gets the java source element./*from ww  w . ja v a 2  s.  co  m*/
 * 
 * @param cxt
 *            the cxt
 * 
 * @return the java source element
 */
public IPackageFragmentRoot getCustomJavaSourceElement(ContextVariable cxt) {
    IFolder source = getProject(cxt).getFolder("src");
    IJavaProject jp = getJavaProject(cxt);
    if (jp != null) {
        return jp.getPackageFragmentRoot(source);
    }
    return null;
}