Example usage for org.eclipse.jdt.core IPackageFragmentRoot getResource

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getResource

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getResource.

Prototype

IResource getResource();

Source Link

Document

Returns the innermost resource enclosing this element.

Usage

From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java

License:Open Source License

private void appendFolderLabel(IPackageFragmentRoot root, long flags) {
    IResource resource = root.getResource();
    if (resource == null) {
        appendExternalArchiveLabel(root, flags);
        return;/*from w  ww. ja  va  2  s  .  co  m*/
    }

    boolean rootQualified = getFlag(flags, JavaElementLabels.ROOT_QUALIFIED);
    boolean referencedQualified = getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)
            && isReferenced(root);
    if (rootQualified) {
        fBuffer.append(root.getPath().makeRelative().toString());
    } else {
        IPath projectRelativePath = resource.getProjectRelativePath();
        if (projectRelativePath.segmentCount() == 0) {
            fBuffer.append(resource.getName());
            referencedQualified = false;
        } else {
            fBuffer.append(projectRelativePath.toString());
        }

        int offset = fBuffer.length();
        if (referencedQualified) {
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            fBuffer.append(resource.getProject().getName());
        } else if (getFlag(flags, JavaElementLabels.ROOT_POST_QUALIFIED)) {
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            fBuffer.append(root.getParent().getElementName());
        } else {
            return;
        }
        if (getFlag(flags, JavaElementLabels.COLORIZE)) {
            fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
        }
    }
}

From source file:org.eclipse.e4.demo.simpleide.jdt.internal.editor.viewer.JavaElementLabelComposer.java

License:Open Source License

private void appendInternalArchiveLabel(IPackageFragmentRoot root, long flags) {
    IResource resource = root.getResource();
    boolean rootQualified = getFlag(flags, JavaElementLabels.ROOT_QUALIFIED);
    if (rootQualified) {
        fBuffer.append(root.getPath().makeRelative().toString());
    } else {/*from  ww w  .  j  ava  2  s  . c  om*/
        fBuffer.append(root.getElementName());
        int offset = fBuffer.length();
        boolean referencedPostQualified = getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED);
        if (referencedPostQualified && isReferenced(root)) {
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            fBuffer.append(resource.getParent().getFullPath().makeRelative().toString());
        } else if (getFlag(flags, JavaElementLabels.ROOT_POST_QUALIFIED)) {
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            fBuffer.append(root.getParent().getPath().makeRelative().toString());
        }
        if (referencedPostQualified) {
            try {
                IClasspathEntry referencingEntry = JavaModelUtil.getClasspathEntry(root).getReferencingEntry();
                if (referencingEntry != null) {
                    fBuffer.append(messages.JavaElementLabels_onClassPathOf(Name.CLASS_PATH.toString(),
                            referencingEntry.getPath().lastSegment()));
                }
            } catch (JavaModelException e) {
                // ignore
            }
        }
        if (getFlag(flags, JavaElementLabels.COLORIZE)) {
            fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
        }
    }
}

From source file:org.eclipse.e4.tools.ui.designer.utils.ClassLoaderHelper.java

License:Open Source License

private static URL findResourceURL(IJavaProject javaProject, Set<IJavaProject> visited, boolean isFirstProject,
        String name) {/* ww  w .  j  a  v a 2 s .com*/
    if (visited.contains(javaProject))
        return null;
    visited.add(javaProject);
    try {
        IPath outPath = javaProject.getProject().getLocation().removeLastSegments(1)
                .append(javaProject.getOutputLocation());
        outPath = outPath.addTrailingSeparator();
        {
            URL url = toURL(outPath.append(name));
            if (url != null) {
                return url;
            }
        }
        for (IPackageFragmentRoot fragment : javaProject.getPackageFragmentRoots()) {
            if (fragment.getKind() == IPackageFragmentRoot.K_SOURCE) {
                URL url = toURL(fragment.getResource().getLocation().append(name));
                if (url != null) {
                    return url;
                }
            }
        }
        // urls.add(out);
        IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY: {
                // TODO
                IClasspathEntry resolveEntry = JavaCore.getResolvedClasspathEntry(entry);
                File file = resolveEntry.getPath().toFile();
                IPath path = resolveEntry.getPath();
                if (!file.exists()) {
                    String projectName = path.segment(0);
                    IProject project = javaProject.getProject().getWorkspace().getRoot()
                            .getProject(projectName);
                    path = project.getLocation().append(path.removeFirstSegments(1));
                }
                String spec = "jar:file:" + path.toString() + "!/" + name;
                try {
                    URL url2 = new URL(spec);
                    url2.getContent();
                    return url2;
                } catch (Exception e) {
                }
            }
                break;
            case IClasspathEntry.CPE_CONTAINER:

                break;
            case IClasspathEntry.CPE_VARIABLE: {
                {
                    // TODO
                    URL url = toURL(outPath.append(name));
                    if (url != null) {
                        return url;
                    }
                }
            }
                break;
            case IClasspathEntry.CPE_PROJECT: {
                if (isFirstProject || entry.isExported()) {
                    URL url = findResourceURL(getJavaProject(entry), visited, false, name);
                    if (url != null) {
                        return url;
                    }
                }
                break;
            }
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.eclipse.e4.xwt.tools.ui.designer.loader.XWTVisualLoader.java

License:Open Source License

public synchronized Control loadWithOptions(URL url, Map<String, Object> options) throws Exception {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    String fileStr = url.getFile();
    if (fileStr.indexOf(PathHelper.WHITE_SPACE_ASCII) != -1) {
        fileStr = fileStr.replace(PathHelper.WHITE_SPACE_ASCII, " ");
    }//from   ww  w .j  a va 2 s  .c  om
    IFile file = root.getFileForLocation(new Path(fileStr));
    if (file != null) {
        try {
            // the url given an binary file of project, we need find the source file of it and the load and open.
            IProject project = file.getProject();
            String fullPath = file.getFullPath().toString();
            IJavaProject javaProject = JavaCore.create(project);
            String outputPath = javaProject.getOutputLocation().toString();
            if (fullPath != null && outputPath != null && fullPath.startsWith(outputPath)) {
                String fileSourcePath = fullPath.substring(outputPath.length());
                IPackageFragmentRoot[] allPackageFragmentRoots = javaProject.getAllPackageFragmentRoots();
                for (IPackageFragmentRoot pRoot : allPackageFragmentRoots) {
                    if (pRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        IFolder resource = (IFolder) pRoot.getResource();
                        IFile sourceFile = resource.getFile(new Path(fileSourcePath));
                        if (sourceFile != null && sourceFile.exists()) {
                            file = sourceFile;
                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
        }
    }
    if (file != null) {
        IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        IEditorPart activeEditor = activePage.getActiveEditor();
        try {
            XWTDesigner designer = (XWTDesigner) activePage.openEditor(new FileEditorInput(file),
                    XWTDesigner.EDITOR_ID, false);
            XamlDocument xamlDocument = (XamlDocument) designer.getDocumentRoot();
            XWTModelBuilder builder = null;
            if (xamlDocument == null) {
                builder = new XWTModelBuilder();
                builder.doLoad(designer, null);
                xamlDocument = builder.getDiagram();
            }
            Control control = (Control) new XWTProxy(file).load(xamlDocument.getRootElement(), options);
            if (builder != null) {
                builder.dispose();
            }
            return control;
        } finally {
            activePage.activate(activeEditor);
        }
    }
    return null;
}

From source file:org.eclipse.e4.xwt.tools.ui.designer.wizards.contents.SourceContents.java

License:Open Source License

public SourceContents(TextValueModel checkedItems, String path, String propertyFilePath, String name,
        String porpertyFileName, StringBuffer historyContent, IPackageFragmentRoot root) {
    this.checkedItems = checkedItems;
    this.path = path;
    this.propertyFilePath = propertyFilePath;
    this.porpertyFileName = porpertyFileName;
    this.historyContent = historyContent;
    sourceKey = root.getResource().toString();
    if (name != null) {
        this.name = name.substring(0, name.length() - 5);
    }//from w w  w. j av  a 2  s . com
}

From source file:org.eclipse.e4.xwt.tools.ui.designer.wizards.ExternalizeStringsWizard.java

License:Open Source License

private void getDefaultAccessorContents() {
    IFile inputFile = designer.getFile();

    IContainer container = inputFile.getParent();
    IJavaElement javaElement = JavaCore.create(container);
    IPackageFragmentRoot defaultRoot = null;
    if (javaElement != null && javaElement.exists()) {
        IJavaProject javaProject = JavaCore.create(inputFile.getProject());
        try {//ww w .ja v  a  2 s  .  com
            IPackageFragmentRoot fragmentRoot[] = javaProject.getAllPackageFragmentRoots();
            for (int i = 0; i < fragmentRoot.length; i++) {
                if (fragmentRoot[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                    defaultRoot = fragmentRoot[i];
                    for (IJavaElement element : defaultRoot.getChildren()) {
                        if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
                            javaElement = element;
                        }
                    }
                    break;
                }
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
        }
    }
    if (javaElement == null || !(javaElement instanceof IPackageFragment)) {
        String projectName = inputFile.getProject().getName();
        externalizeStringsWizardPage.setErrorMessage("The project " + projectName + " has not source folder.");
        return;
    }

    externalizeStringsWizardPage.setDefaultRoot(defaultRoot);
    externalizeStringsWizardPage.setDefaultFolder(defaultRoot.getResource().getFullPath().toString());
    externalizeStringsWizardPage.setDefaultPackage(javaElement.getElementName());
}

From source file:org.eclipse.edt.ide.ui.internal.externaltype.wizards.javatype.ExternalTypeFromJavaPage.java

License:Open Source License

private ClassLoader getURLClassLoader() {
    if (urlClassLoader == null) {
        List<URL> classPathURLs = new ArrayList<URL>();

        try {//Add Java class path.
            IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();

            for (IPackageFragmentRoot pRoot : roots) {
                IJavaProject refProject = pRoot.getParent().getJavaProject();
                IPath proRoot = refProject.getProject().getLocation();

                if (pRoot.isArchive() && pRoot.isExternal()) {
                    classPathURLs.add(pRoot.getResolvedClasspathEntry().getPath().toFile().toURI().toURL());
                } else if (pRoot.isArchive() && pRoot.getResource() != null) {
                    classPathURLs.add(proRoot.append(pRoot.getResource().getProjectRelativePath()).toFile()
                            .toURI().toURL());
                } else { //source folder
                    IPath outputRelPath = refProject.getOutputLocation().removeFirstSegments(1);
                    classPathURLs.add(proRoot.append(outputRelPath).toFile().toURI().toURL());
                }//from   w ww.jav a2  s .  c om
            }
        } catch (Throwable ee) {
            ee.printStackTrace();
        }

        ClassLoader parent = Thread.currentThread().getContextClassLoader();
        URL[] urlPaths = new URL[classPathURLs.size()];
        urlClassLoader = new URLClassLoader(classPathURLs.toArray(urlPaths), parent);
    }

    return urlClassLoader;
}

From source file:org.eclipse.fx.ide.ui.util.RelativeFileLocator.java

License:Open Source License

public static File locateFile(URI uri, String filePath) {
    filePath = filePath.trim();/*  w ww.  ja  va  2s  .  com*/
    IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(uri.segment(1));
    IJavaProject jp = JavaCore.create(p);

    // Absolute to project
    if (filePath.startsWith("/")) {
        filePath = filePath.substring(1); // Remove the leading /
        IFile f = p.getFile(filePath);
        if (f.exists()) {
            return f.getLocation().toFile().getAbsoluteFile();
        } else if (jp != null) {
            try {
                for (IPackageFragmentRoot r : jp.getPackageFragmentRoots()) {
                    if (r.isArchive()) {
                        //TODO We should allow to load styles from the referenced jars
                    } else if (r.getResource() instanceof IFolder) {
                        IFolder folder = (IFolder) r.getResource();
                        if (folder.exists()) {
                            f = folder.getFile(filePath);
                            if (f.exists()) {
                                return f.getLocation().toFile().getAbsoluteFile();
                            }
                        }
                    }
                }
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } else {
        URI fileUri = null;

        try {
            fileUri = URI.createURI(filePath);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (fileUri != null && fileUri.isPlatformResource()) {
            Path path = new Path(fileUri.toPlatformString(true));
            IWorkspaceRoot root = jp.getProject().getWorkspace().getRoot();
            IFile file = root.getFile(path);
            if (file.exists()) {
                return file.getLocation().toFile().getAbsoluteFile();
            }
        } else {
            IPath path = null;
            for (int i = 2; i < uri.segmentCount() - 1; i++) {
                if (path == null) {
                    path = new Path(uri.segment(i));
                } else {
                    path = path.append(uri.segment(i));
                }
            }

            if (path != null) {
                IFile f = p.getFile(path.append(filePath));
                if (f.exists()) {
                    return f.getLocation().toFile().getAbsoluteFile();
                }
            }
        }
    }

    return null;
}

From source file:org.eclipse.incquery.patternlanguage.emf.ui.validation.GenmodelBasedEMFPatternLanguageJavaValidator.java

License:Open Source License

protected String getActualPackageName(PatternModel model) {
    URI fileURI = model.eResource().getURI();
    for (Pair<IStorage, IProject> storage : storage2UriMapper.getStorages(fileURI)) {
        if (storage.getFirst() instanceof IFile) {
            IPath fileWorkspacePath = storage.getFirst().getFullPath();
            IJavaProject javaProject = JavaCore.create(storage.getSecond());
            if (javaProject != null && javaProject.exists() && javaProject.isOpen()) {
                try {
                    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                        if (!root.isArchive() && !root.isExternal()) {
                            IResource resource = root.getResource();
                            if (resource != null) {
                                IPath sourceFolderPath = resource.getFullPath();
                                if (sourceFolderPath.isPrefixOf(fileWorkspacePath)) {
                                    IPath classpathRelativePath = fileWorkspacePath
                                            .makeRelativeTo(sourceFolderPath);
                                    return classpathRelativePath.removeLastSegments(1).toString().replace("/",
                                            ".");
                                }//from  w  w w  . j  a v a2 s.  c  o m
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    logger.error("Error resolving package declaration for Pattern Model", e);
                }
            }
        }
    }
    return null;
}

From source file:org.eclipse.jpt.jpa.eclipselink.ui.internal.entity.data.model.EclipseLinkDynamicEntityTemplateModel.java

License:Open Source License

public IFile getMappingXmlFile() {
    IFile ormFile = null;/*from  www. j a v  a2  s . co  m*/
    IProject project = getProject();
    IPackageFragmentRoot[] sourceFragments = J2EEProjectUtilities.getSourceContainers(project);
    for (IPackageFragmentRoot packageFragmentRoot : sourceFragments) {
        ormFile = project
                .getFile(packageFragmentRoot.getResource().getName() + File.separator + getMappingXMLName());
        if (ormFile.exists()) {
            break;
        }
    }
    return ormFile;
}