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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.hibernate.eclipse.jdt.ui.wizards.NewHibernateMappingPreviewPage.java

License:Open Source License

/**
 * Try to create one change according with input file (fileSrc).
 * In case of success change be added into cc and returns true.
 * @param cc/*w w w  .  j  a  va  2s.co m*/
 * @param proj
 * @param fileSrc
 * @return
 */
protected boolean updateOneChange(final CompositeChange cc, final IJavaProject proj, File fileSrc) {
    boolean res = false;
    if (!fileSrc.exists()) {
        return res;
    }
    if (fileSrc.isDirectory()) {
        return res;
    }
    final IPath place2Gen = getRootPlace2Gen().append(proj.getElementName());
    final IPath filePathFrom = new Path(fileSrc.getPath());
    final IPath filePathTo_Proj = filePathFrom.makeRelativeTo(place2Gen);
    final IPath filePathTo_Show = proj.getPath().append(filePathTo_Proj);
    final IResource res2Update = proj.getProject().findMember(filePathTo_Proj);
    if (res2Update != null) {
        final ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
        ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(filePathTo_Show, LocationKind.IFILE);
        if (textFileBuffer == null) {
            try {
                bufferManager.connect(filePathTo_Show, LocationKind.IFILE, null);
                paths2Disconnect.add(filePathTo_Show);
            } catch (CoreException e) {
                HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e); //$NON-NLS-1$
            }
            textFileBuffer = bufferManager.getTextFileBuffer(filePathTo_Show, LocationKind.IFILE);
        }
        if (textFileBuffer != null) {
            IDocument documentChange = textFileBuffer.getDocument();
            //
            String str = readInto(fileSrc);
            TextEdit textEdit = new ReplaceEdit(0, documentChange.getLength(), str.toString());
            //
            TextFileChange change = new TextFileChange(filePathTo_Show.toString(), (IFile) res2Update);
            change.setSaveMode(TextFileChange.LEAVE_DIRTY);
            change.setEdit(textEdit);
            cc.add(change);
            //
            res = true;
        }
    } else {
        String str = readInto(fileSrc);
        CreateTextFileChange change = new CreateTextFileChange(filePathTo_Show, str.toString(), null,
                "hbm.xml"); //$NON-NLS-1$
        cc.add(change);
        //
        res = true;
    }
    return res;
}

From source file:org.infinitest.eclipse.workspace.JavaProjectSet.java

License:Open Source License

@Override
public ProjectFacade findProject(IPath path) {
    for (IJavaProject project : openProjects()) {
        if (project.getPath().equals(path)) {
            return createProjectFacade(project);
        }/* w w  w .  j  a  va 2s .c o m*/
    }

    return null;
}

From source file:org.infinitest.eclipse.workspace.JavaProjectTestSupport.java

License:Open Source License

public static IJavaProject createMockProject(String projectPath, IClasspathEntry... entries)
        throws JavaModelException, URISyntaxException {
    IJavaProject javaProject = mock(IJavaProject.class);
    IProject project = mock(IProject.class);
    when(project.getLocationURI()).thenReturn(new URI(projectPath));
    when(javaProject.getProject()).thenReturn(project);
    when(javaProject.getPath()).thenReturn(new Path(projectPath));
    expectProjectLocationFor(javaProject, projectPath);
    expectClasspathFor(javaProject, entries);
    outputLocationExpectation(javaProject, projectPath);
    return javaProject;
}

From source file:org.isoe.fwk.runtime.diagraph.layout.workbench.action.LayoutWorkbenchAction.java

License:Open Source License

private void logProject(IJavaProject javaProject) {
    path = javaProject.getPath().toFile().getAbsolutePath();
    parentPath = new File(path).getParentFile().getAbsolutePath();
    if (LOG)/*from w  w w.  j a  v  a2s.  c o  m*/
        clog(parentPath + " > " + path);
}

From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java

License:Open Source License

/**
 * @param javaProject//from w  ww  . j a  v a 2 s .  c  om
 * @param folder
 * @return <code>true</code> if the supplied folder was successfully added
 *         to the supplied Java project
 */
public static boolean addFolderToProjectClasspath(IJavaProject javaProject, IResource folder) {
    IClasspathEntry[] entries;
    try {
        entries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);

        IPath srcPath = javaProject.getPath().append(folder.getProjectRelativePath());
        IClasspathEntry srcEntry = JavaCore.newSourceEntry(srcPath, null);

        newEntries[entries.length] = JavaCore.newSourceEntry(srcEntry.getPath());
        javaProject.setRawClasspath(newEntries, null);
        return true;
    } catch (JavaModelException e) {
        return false;
    }
}

From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java

License:Open Source License

/**
 * @param javaProject/*  w  w  w .j  a v a 2s  .c  o m*/
 * @param folder
 * @return <code>true</code> if the supplied folder is part of the supplied
 *         Java project's classpath
 */
public static boolean findFolderOnProjectClasspath(IJavaProject javaProject, IResource folder) {
    IClasspathEntry[] entries;
    try {
        IPath srcPath = javaProject.getPath().append(folder.getProjectRelativePath());
        entries = javaProject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            if (entry.getPath().equals(srcPath)) {
                return true;
            }
        }
        return false;
    } catch (JavaModelException e) {
        return false;
    }
}

From source file:org.jboss.tools.arquillian.ui.internal.utils.ArquillianUIUtil.java

License:Open Source License

private static IFile getNewFile(IJavaProject javaProject, String arquillianProperties) throws CoreException {
    IProject project = javaProject.getProject();
    if (project.hasNature(IMavenConstants.NATURE_ID)) {
        IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(project,
                new NullProgressMonitor());
        MavenProject mavenProject = facade.getMavenProject(new NullProgressMonitor());
        Build build = mavenProject.getBuild();
        String testDirectory = null;
        List<Resource> testResources = build.getTestResources();
        if (testResources != null && testResources.size() > 0) {
            testDirectory = testResources.get(0).getDirectory();
        } else {/*  www  .  j av a2 s .c  om*/
            testDirectory = build.getTestSourceDirectory();
        }
        File testDir = new File(testDirectory);
        if (testDir.isDirectory()) {
            File arquillianFile = new File(testDir, arquillianProperties);
            IPath path = new Path(arquillianFile.getAbsolutePath());
            IFile iFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
            if (!iFile.getParent().exists()) {
                IPath projectPath = javaProject.getProject().getLocation();
                IPath iFilePath = iFile.getLocation();
                if (iFilePath.toString().startsWith(projectPath.toString())) {
                    String s = iFilePath.toString().substring(projectPath.toString().length());
                    path = new Path(s);
                    return javaProject.getProject().getFile(path);
                }
            }
            return iFile;
        }
    }
    IPath path = null;
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            if (roots == null) {
                continue;
            }
            for (IPackageFragmentRoot root : roots) {
                path = root.getPath();
                break;
            }
        }
    }
    if (path == null) {
        throw new CoreException(new Status(IStatus.ERROR, ArquillianUIActivator.PLUGIN_ID, "Invalid project"));
    }
    IFolder folder = javaProject.getProject().getFolder(path);
    if (!folder.exists()) {
        IPath projectPath = javaProject.getPath();
        path = path.makeRelativeTo(projectPath);
        folder = javaProject.getProject().getFolder(path);
    }
    return folder.getFile(arquillianProperties);
}

From source file:org.jboss.tools.fuse.transformation.editor.internal.util.Util.java

License:Open Source License

public static void ensureSourceFolderExists(IJavaProject javaProject, String folderName,
        IProgressMonitor monitor) throws Exception {
    boolean exists = false;
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IPath path = javaProject.getPath().append(folderName);
    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().equals(path)) {
            exists = true;//from ww w . ja  v  a2  s. c  o m
            File folder = javaProject.getResource().getLocation().append(folderName).toFile();
            if (!folder.exists()) {
                if (!folder.mkdirs()) {
                    throw new Exception(Messages.bind(Messages.Util_UnableToCreateSourceFolder, folder));
                }
                javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
            }
            break;
        }
    }
    if (!exists) {
        IClasspathEntry[] newEntries = Arrays.copyOf(entries, entries.length + 1);
        newEntries[entries.length] = JavaCore.newSourceEntry(path);
        javaProject.setRawClasspath(newEntries, monitor);
        javaProject.save(monitor, true);
    }
}

From source file:org.jboss.tools.jsf.vpe.jsf.test.jbide.OpenOnInJarPackageFragment_JBIDE5682.java

License:Open Source License

public void testOpenOnInJarPackageFragment() throws Throwable {
    IProject project = ProjectsLoader.getInstance().getProject(JsfAllTests.IMPORT_JSF_20_PROJECT_NAME);
    IJavaProject javaProject = JavaCore.create(project);
    IPackageFragmentRoot fragmentRoot = javaProject
            .getPackageFragmentRoot(javaProject.getPath().toString() + "/" //$NON-NLS-1$
                    + JAR_LIB_PATH);/* w  w  w .j  a v a2  s  .  c  o  m*/
    JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) fragmentRoot;
    JarEntryFile fileInJar = new JarEntryFile(TEST_FILE);
    PackageFragment fragment = jarPackageFragmentRoot.getPackageFragment(FRAGMENT_PATH);
    fileInJar.setParent(fragment);
    JarEntryEditorInput editorInput = new JarEntryEditorInput(fileInJar);
    OpenOnUtil.checkOpenOnInEditor(editorInput, getEditorId(fileInJar.getName()), 78, 44, "IfHandler.class"); //$NON-NLS-1$
}

From source file:org.jboss.tools.maven.core.MavenCoreActivator.java

License:Open Source License

public static String getSourceDirectory(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IPath path = null;//from  w  w w . j a  va 2s.  c  om
    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            path = entries[i].getPath();
            break;
        }
    }
    if (path == null) {
        return null;
    }
    path = path.makeRelativeTo(javaProject.getPath());
    String value = path.toString();
    if (value.startsWith(SEPARATOR)) {
        return BASEDIR + path.toString();
    } else {
        return BASEDIR + SEPARATOR + path.toString();
    }
}