Search in sources :

Example 51 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project linuxtools by eclipse.

the class STLink2SourceSupport method getFileForPathImpl.

private static IFile getFileForPathImpl(IPath path, IProject project) {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    if (path.isAbsolute()) {
        return root.getFileForLocation(path);
    }
    if (project != null && project.exists()) {
        ICProject cproject = CoreModel.getDefault().create(project);
        if (cproject != null) {
            try {
                ISourceRoot[] roots = cproject.getAllSourceRoots();
                for (ISourceRoot sourceRoot : roots) {
                    IContainer r = sourceRoot.getResource();
                    IResource res = r.findMember(path);
                    if (res != null && res.exists() && res instanceof IFile) {
                        return (IFile) res;
                    }
                }
                IOutputEntry[] entries = cproject.getOutputEntries();
                for (IOutputEntry pathEntry : entries) {
                    IPath p = pathEntry.getPath();
                    IResource r = root.findMember(p);
                    if (r instanceof IContainer) {
                        IContainer parent = (IContainer) r;
                        IResource res = parent.findMember(path);
                        if (res != null && res.exists() && res instanceof IFile) {
                            return (IFile) res;
                        }
                    }
                }
            } catch (CModelException e) {
                Activator.getDefault().getLog().log(e.getStatus());
            }
        }
    }
    // no match found...try and see if we are dealing with a link
    IPath realPath = project.getLocation().append(path).makeAbsolute();
    URI realURI = URIUtil.toURI(realPath.toString());
    try {
        FindLinkedResourceVisitor visitor = new STLink2SourceSupport.FindLinkedResourceVisitor(realURI);
        project.accept(visitor, IResource.DEPTH_INFINITE);
        // If we find a match, make note of the target and the real C project.
        if (visitor.foundElement()) {
            return (IFile) visitor.getResource();
        }
    } catch (CoreException e) {
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) ICProject(org.eclipse.cdt.core.model.ICProject) IPath(org.eclipse.core.runtime.IPath) CModelException(org.eclipse.cdt.core.model.CModelException) URI(java.net.URI) ISourceRoot(org.eclipse.cdt.core.model.ISourceRoot) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) IOutputEntry(org.eclipse.cdt.core.model.IOutputEntry) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 52 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project linuxtools by eclipse.

the class RemoteProxyCMainTab method updateWorkingDirFromConfig.

protected void updateWorkingDirFromConfig(ILaunchConfiguration config) {
    if (workingDirText != null) {
        String projectDir = EMPTY_STRING;
        try {
            projectDir = config.getAttribute(ATTR_REMOTE_WORKING_DIRECTORY_NAME, EMPTY_STRING);
        } catch (CoreException ce) {
            ProfileLaunchPlugin.log(ce);
        }
        if (projectDir.equals(EMPTY_STRING)) {
            if (this.fProjText != null) {
                IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                IProject project = root.getProject(this.fProjText.getText());
                try {
                    projectDir = RemoteProxyManager.getInstance().getRemoteProjectLocation(project);
                } catch (CoreException e) {
                    setErrorMessage(fPreviouslyCheckedWorkingDirErrorMsg = ProxyLaunchMessages.error_accessing_working_directory);
                }
            }
        }
        workingDirText.setText(projectDir);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IProject(org.eclipse.core.resources.IProject)

Example 53 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project linuxtools by eclipse.

the class CProjectHelper method createCProject2.

/**
 * Creates a ICProject.
 */
private static ICProject createCProject2(final String projectName, String binFolderName) throws CoreException {
    final IWorkspace ws = ResourcesPlugin.getWorkspace();
    final ICProject[] newProject = new ICProject[1];
    ws.run((IWorkspaceRunnable) monitor -> {
        IWorkspaceRoot root = ws.getRoot();
        IProject project = root.getProject(projectName);
        if (!project.exists()) {
            project.create(null);
        } else {
            project.refreshLocal(IResource.DEPTH_INFINITE, null);
        }
        if (!project.isOpen()) {
            project.open(null);
        }
        if (!project.hasNature(CProjectNature.C_NATURE_ID)) {
            String projectId = PLUGIN_ID + ".TestProject";
            addNatureToProject(project, CProjectNature.C_NATURE_ID, null);
            CCorePlugin.getDefault().mapCProjectOwner(project, projectId, false);
        }
        addDefaultBinaryParser(project);
        newProject[0] = CCorePlugin.getDefault().getCoreModel().create(project);
    }, null);
    return newProject[0];
}
Also used : CProjectNature(org.eclipse.cdt.core.CProjectNature) ICConfigExtensionReference(org.eclipse.cdt.core.settings.model.ICConfigExtensionReference) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) CoreException(org.eclipse.core.runtime.CoreException) ICProjectDescription(org.eclipse.cdt.core.settings.model.ICProjectDescription) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CCProjectNature(org.eclipse.cdt.core.CCProjectNature) IStatus(org.eclipse.core.runtime.IStatus) CCorePlugin(org.eclipse.cdt.core.CCorePlugin) IProject(org.eclipse.core.resources.IProject) IWorkspace(org.eclipse.core.resources.IWorkspace) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IResource(org.eclipse.core.resources.IResource) Assert.fail(org.junit.Assert.fail) ICProject(org.eclipse.cdt.core.model.ICProject) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) ICProject(org.eclipse.cdt.core.model.ICProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) IProject(org.eclipse.core.resources.IProject)

Example 54 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project linuxtools by eclipse.

the class ResourceHelper method cleanUp.

/**
 * Clean-up any files created as part of a unit test.
 * This method removes *all* Workspace IResources and any external
 * files / folders created with the #createWorkspaceFile #createWorkspaceFolder
 * methods in this class
 */
public static void cleanUp() throws CoreException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    root.refreshLocal(IResource.DEPTH_INFINITE, NULL_MONITOR);
    // Delete all external files & folders created using ResourceHelper
    for (String loc : externalFilesCreated) {
        File f = new File(loc);
        if (f.exists())
            deleteRecursive(f);
    }
    externalFilesCreated.clear();
    // Remove IResources created by this helper
    for (IResource r : resourcesCreated) {
        if (r.exists()) {
            try {
                r.delete(true, NULL_MONITOR);
            } catch (CoreException e) {
            // Ignore
            }
        }
    }
    resourcesCreated.clear();
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) File(java.io.File) IResource(org.eclipse.core.resources.IResource)

Example 55 with IWorkspaceRoot

use of org.eclipse.core.resources.IWorkspaceRoot in project linuxtools by eclipse.

the class ProfileUIUtils method openEditorAndSelect.

/**
 * Opens the specified file in an editor (or selects an already open
 * editor) and highlights the specified line.
 * @param result - result of performing source lookup with a org.eclipse.debug.core.model.ISourceLocator
 * @param line - line number to select, 0 to not select a line
 * @throws PartInitException - Failed to open editor
 * @throws BadLocationException - Line number not valid in file
 * @see DebugUITools#lookupSource(Object, org.eclipse.debug.core.model.ISourceLocator)
 */
public static void openEditorAndSelect(ISourceLookupResult result, int line) throws PartInitException, BadLocationException {
    IEditorInput input = result.getEditorInput();
    String editorID = result.getEditorId();
    if (input == null || editorID == null) {
        // Consult the CDT DebugModelPresentation
        Object sourceElement = result.getSourceElement();
        if (sourceElement != null) {
            // Resolve IResource in case we get a LocalFileStorage object
            if (sourceElement instanceof LocalFileStorage) {
                IPath filePath = ((LocalFileStorage) sourceElement).getFullPath();
                URI fileURI = URIUtil.toURI(filePath);
                IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                IFile[] files = root.findFilesForLocationURI(fileURI);
                if (files.length > 0) {
                    // Take the first match
                    sourceElement = files[0];
                }
            }
            IDebugModelPresentation pres = DebugUITools.newDebugModelPresentation(CDebugCorePlugin.getUniqueIdentifier());
            input = pres.getEditorInput(sourceElement);
            editorID = pres.getEditorId(input, sourceElement);
            pres.dispose();
        }
    }
    if (input != null && editorID != null) {
        // Open the editor
        IWorkbenchPage activePage = ProfileUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IEditorPart editor = IDE.openEditor(activePage, input, editorID);
        // Select the line
        if (editor instanceof ITextEditor) {
            ITextEditor textEditor = (ITextEditor) editor;
            if (line > 0) {
                IDocumentProvider provider = textEditor.getDocumentProvider();
                IDocument document = provider.getDocument(textEditor.getEditorInput());
                // zero-indexed
                IRegion lineRegion = document.getLineInformation(line - 1);
                textEditor.selectAndReveal(lineRegion.getOffset(), lineRegion.getLength());
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IPath(org.eclipse.core.runtime.IPath) LocalFileStorage(org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage) IDebugModelPresentation(org.eclipse.debug.ui.IDebugModelPresentation) IEditorPart(org.eclipse.ui.IEditorPart) URI(java.net.URI) IRegion(org.eclipse.jface.text.IRegion) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)172 IProject (org.eclipse.core.resources.IProject)69 IPath (org.eclipse.core.runtime.IPath)60 IResource (org.eclipse.core.resources.IResource)57 IFile (org.eclipse.core.resources.IFile)53 CoreException (org.eclipse.core.runtime.CoreException)50 IWorkspace (org.eclipse.core.resources.IWorkspace)34 File (java.io.File)30 Path (org.eclipse.core.runtime.Path)29 IContainer (org.eclipse.core.resources.IContainer)26 URI (java.net.URI)18 IFolder (org.eclipse.core.resources.IFolder)17 IOException (java.io.IOException)15 IProjectDescription (org.eclipse.core.resources.IProjectDescription)13 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)13 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 IStatus (org.eclipse.core.runtime.IStatus)11 IJavaProject (org.eclipse.jdt.core.IJavaProject)11 Location (ch.acanda.eclipse.pmd.domain.Location)10