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:org.jboss.tools.common.text.ext.hyperlink.AbstractHyperlink.java

License:Open Source License

protected IEditorInput createEditorInput(String fileString) {
    String jarName = fileString.substring(0, fileString.indexOf("!")); //$NON-NLS-1$

    IFile[] fs = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(jarName));
    if (fs == null || fs.length == 0)
        return null;

    IProject p = fs[0].getProject();/* w  ww .j  a v  a2s . c o  m*/

    IJavaProject jp = EclipseResourceUtil.getJavaProject(p);

    if (jp == null)
        return null;

    IPackageFragmentRoot root = jp.getPackageFragmentRoot(fs[0]);

    String entryName = fileString.substring(fileString.indexOf("!") + 2, fileString.length()); //$NON-NLS-1$
    JarEntryFile f = new JarEntryFile(entryName);
    f.setParent(root);
    JarEntryEditorInput jarEditorInput = new JarEntryEditorInput(f) {
        public boolean equals(Object arg) {
            return this.toString().equals(arg.toString());
        }
    };
    return jarEditorInput;
}

From source file:org.jboss.tools.fuse.transformation.editor.wizards.NewTransformationTestWizard.java

License:Open Source License

/**
 * {@inheritDoc}//from ww w .jav a 2s .c  o  m
 *
 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
 *      org.eclipse.jface.viewers.IStructuredSelection)
 */
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
    _page.setTypeName(Messages.NewTransformationTestWizard_pageTypeName, true);
    // what are we passing in? assume we're right-clicking on the dozer file
    if (selection.size() != 1) {
        return;
    }
    _page.init(selection);
    Object selectedObject = selection.getFirstElement();
    if (selectedObject != null) {
        if (selectedObject instanceof IResource) {
            project = ((IResource) selectedObject).getProject();
        }
        IFile camelConfigFile = null;
        if (selectedObject instanceof IFile) {
            IFile selectedFile = (IFile) selectedObject;
            boolean isCamelConfig = CamelFileTypeHelper.isSupportedCamelFile(project,
                    selectedFile.getProjectRelativePath().toPortableString());
            if (isCamelConfig) {
                camelConfigFile = selectedFile;
            }
        }
        IJavaProject javaProject = null;
        if (project != null) {
            _page.setProject(project);
            javaProject = JavaCore.create(project);
        }
        if (project != null && camelConfigFile == null) {
            IResource findCamelContext = project
                    .findMember("src/main/resources/META-INF/spring/camel-context.xml"); //$NON-NLS-1$
            if (findCamelContext != null) {
                camelConfigFile = (IFile) findCamelContext;
            }
        }
        if (camelConfigFile != null) {
            _page.setCamelConfigFile(camelConfigFile);
            File file = new File(camelConfigFile.getLocationURI());
            _page.setBuilder(new CamelConfigBuilder(file));
        }
        if (javaProject != null) {
            _page.setJavaProject(javaProject);

            IFolder srcFolder = javaProject.getProject().getFolder("src/test/java"); //$NON-NLS-1$
            if (!JavaUtil.findFolderOnProjectClasspath(javaProject, srcFolder)) {
                JavaUtil.addFolderToProjectClasspath(javaProject, srcFolder);
            }
            if (!srcFolder.exists()) {
                try {
                    srcFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
                } catch (CoreException e) {
                    e.printStackTrace();
                }
            }

            IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(srcFolder);
            _page.setPackageFragmentRoot(root, true);
        }
    }
}

From source file:org.jboss.tools.jsf.jsf2.bean.build.JSF2ProjectBuilder.java

License:Open Source License

protected void buildJars(Map<String, XModelObject> newJars) throws CoreException {
    IJavaProject jp = EclipseResourceUtil.getJavaProject(getJSF2Project().getProject());
    if (jp == null)
        return;//w w w. j a va 2s. c o m
    FileSet fileSet = new FileSet();

    for (String jar : newJars.keySet()) {
        Path path = new Path(jar);
        IPackageFragmentRoot root = jp.getPackageFragmentRoot(jar);
        if (root == null)
            continue;
        if (!root.exists()) {
            IFile f = EclipseResourceUtil.getFile(jar);
            if (f != null && f.exists()) {
                root = jp.getPackageFragmentRoot(f);
            } else {
                f = EclipseResourceUtil.getFile(jar + "/META-INF/web-fragment.xml");
                if (f != null && f.exists()) {
                    root = jp.getPackageFragmentRoot(f.getParent().getParent());
                }
            }
        }
        if (root == null || !root.exists())
            continue;
        IJavaElement[] es = root.getChildren();
        for (IJavaElement e : es) {
            if (e instanceof IPackageFragment) {
                IPackageFragment pf = (IPackageFragment) e;
                IClassFile[] cs = pf.getClassFiles();
                for (IClassFile c : cs) {
                    fileSet.add(path, c.getType());
                }
            }
        }
    }
    build(fileSet, getJSF2Project());
}

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

License:Open Source License

/**
 * Function for checking openOn functionality in jar file;
 * /*  w  w  w.j ava 2s  . c o  m*/
 * @param jarFilePath
 * @param jarEntryPath
 * @param line
 * @param position
 * @param expectedResult
 * @throws Throwable
 * 
 * @author mareshkau
 */
private static final void checkOpenOnFromJarFile(final String jarFilePath, final String jarEntryPath,
        final int line, final int position, final String expectedResult) throws Throwable {

    IProject project = ProjectsLoader.getInstance().getProject(JsfAllTests.IMPORT_JBIDE3247_PROJECT_NAME);
    IJavaProject javaProject = JavaCore.create(project);

    IPackageFragmentRoot root = javaProject
            .getPackageFragmentRoot(project.getLocation().toString() + "/" + jarFilePath); //$NON-NLS-1$

    JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
    JarEntryFile fileInJar = new JarEntryFile(jarEntryPath);
    fileInJar.setParent(jarRoot);
    JarEntryEditorInput jarEditorInput = new JarEntryEditorInput(fileInJar);
    OpenOnUtil.checkOpenOnInEditor(jarEditorInput, getEditorId(fileInJar.getName()), line, position,
            expectedResult);
}

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

License:Open Source License

public void testCorrectDoctypeOnFileFromJarArchive() throws Throwable {
    IProject project = ProjectsLoader.getInstance().getProject(JsfAllTests.IMPORT_JBIDE3247_PROJECT_NAME);
    /*/*from  w  w w  .  j a  v  a  2  s . c o m*/
     * Project should exist in the workspace 
     */
    assertNotNull("Project was not found in the workspace: " //$NON-NLS-1$
            + JsfAllTests.IMPORT_JBIDE3247_PROJECT_NAME, project);
    IFile jarArchive = (IFile) project.findMember(JAR_NAME);
    /*
     * Jar file should exist in the project. 
     */
    assertNotNull("File was not found in the project: " //$NON-NLS-1$
            + JsfAllTests.IMPORT_JBIDE3247_PROJECT_NAME + "/" + JAR_NAME, //$NON-NLS-1$
            jarArchive);
    IJavaProject javaProject = JavaCore.create(project);
    /*
     * Project should be correctly transformed. 
     */
    assertNotNull("Cannot process java project:" //$NON-NLS-1$
            + JsfAllTests.IMPORT_JBIDE3247_PROJECT_NAME, javaProject);

    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(jarArchive.getLocation().toString());
    /*
     * Root element should have the correct type.
     */
    if (root instanceof JarPackageFragmentRoot) {
        JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
        IPackageFragment pf = jarRoot.getPackageFragment(PACKAGE_NAME);
        JarEntryFile jarFile = new JarEntryFile(SHORT_NAME);
        jarFile.setParent(pf);
        JarEntryEditorInput jarEditorInput = new JarEntryEditorInput(jarFile);
        JSPMultiPageEditor editor = openEditor(jarEditorInput);
        /*
         * Verify that the editor is opened.
         */
        assertNotNull(
                "Visual Page Editor with a file from the jar archive should have been opened but it wasn't.", //$NON-NLS-1$
                editor);
        /*
         * Get the DOM document
         */
        nsIDOMDocument document = TestUtil.getVpeVisualDocument(editor);
        nsIDOMDocumentType doctype = document.getDoctype();
        /*
         * Doctype should present for the current file.
         */
        assertNotNull("Doctype should present for the specified file: " //$NON-NLS-1$
                + JsfAllTests.IMPORT_JBIDE3247_PROJECT_NAME + "/" //$NON-NLS-1$
                + JAR_NAME + "/" + FILE_NAME, doctype); //$NON-NLS-1$

        /*
         * Doctype should have the correct type.
         */
        assertEquals("Doctype should have the correct type: \" HTML \", but was: " + doctype.getNodeName(), //$NON-NLS-1$
                "HTML", doctype.getNodeName()); //$NON-NLS-1$
    } else {
        /*
         * Fail the test when we cannot process jar file correctly.
         */
        fail("Jar file cannot be processed. Jar file: " //$NON-NLS-1$
                + JsfAllTests.IMPORT_JBIDE3247_PROJECT_NAME + "/" //$NON-NLS-1$
                + JAR_NAME);
    }
}

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);/*from  www .  j a va2  s .co  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.jsf.vpe.jsf.test.jbide.OpenOnTLDPackedInJar_JBIDE5693.java

License:Open Source License

public void testOpenOnTLDPackedInJar() throws Throwable {
    IProject project = ProjectsLoader.getInstance().getProject(JsfAllTests.IMPORT_JSF_20_PROJECT_NAME);
    IJavaProject javaProject = JavaCore.create(project);
    IPackageFragmentRoot fragmentRoot = javaProject.getPackageFragmentRoot("" + project.getLocation() + "/" //$NON-NLS-1$ //$NON-NLS-2$
            + JAR_LIB_PATH);/*from   w w  w.j a  v a 2 s . com*/
    JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) fragmentRoot;
    JarEntryDirectory entryDirectory = new JarEntryDirectory(DIR);
    entryDirectory.setParent(jarPackageFragmentRoot);
    JarEntryFile fileInJar = new JarEntryFile(TEST_FILE);
    fileInJar.setParent(entryDirectory);
    JarEntryEditorInput editorInput = new JarEntryEditorInput(fileInJar);
    OpenOnUtil.checkOpenOnInEditor(editorInput, getEditorId(fileInJar.getName()), 71, 15,
            "CoreValidator.class"); //$NON-NLS-1$

}

From source file:org.jboss.tools.pde.sourcelookup.ui.tests.IntegrationTest.java

License:Open Source License

@Test
public void basicSingleJarTest() throws Exception {
    final String id = "org.foo.bar";
    final String version = "1.0.0";
    final String jarName = id + "_" + version + ".jar";
    final String sourceJarName = id + ".source" + "_" + version + ".jar";
    final String resourceDir = "/resources/";
    final String sourceRepoRelPath = resourceDir + "valid-source-repo";

    IJavaProject jProject = createJavaProject("integration-test");

    // binary jar file location
    String jarPath = FileLocator.toFileURL(getClass().getResource(resourceDir + jarName)).getPath();
    IPath binJarPath = org.eclipse.core.runtime.Path.fromOSString(jarPath);

    // Add the binary jar to the classpath of the java project
    addBinaries(jProject, binJarPath);/*w ww  .  j  ava  2  s. co m*/

    // Set the default source attachment as the binary jar
    // This is necessary to trigger our PDE source lookup
    IPackageFragmentRoot pfr = jProject.getPackageFragmentRoot(jarPath);
    pfr.attachSource(binJarPath, null, new NullProgressMonitor());

    // source jar artifact repository location
    URI sourceLoc = getSourceRepoURL(sourceRepoRelPath);

    // Inform the provisioning agent of the artifact repository containing
    // the source bundle
    addP2Repositories(getSourceRepoURL(resourceDir + "invalid-source-repo"), sourceLoc);

    // Simulate opening the classfile in the editor
    // TODO: Could we ever get JavaUI.openInEditor working with Tycho ?
    //      IJavaElement jElement = jProject.findType("org.foo.bar.Main");
    //      JavaUI.openInEditor(jElement);
    DownloadSourcesActionDelegate delegate = new DownloadSourcesActionDelegate();
    delegate.selectionChanged(null, new StructuredSelection(jProject.getPackageFragmentRoot(jarPath)));
    delegate.run(null);

    // Expect to find the downloaded jar at this location
    File downloadedSourceJarFile = sourcesDirectory.resolve(sourceJarName).toFile();
    while (!downloadedSourceJarFile.exists()) {
        Thread.sleep(1000);
    }
}

From source file:org.jboss.tools.seam.ui.wizard.SelectJavaPackageAction.java

License:Open Source License

@Override
public void run() {
    String projectName = (String) getFieldEditor().getData(ISeamParameter.SEAM_PROJECT_NAME);
    if (projectName == null) {
        return;//from  w  w  w  . ja v  a  2s  .c  o  m
    }
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    if (project == null) {
        SeamGuiPlugin.getPluginLog().logError("Can't find java project with name: " + projectName);
        return;
    }
    SeamProjectsSet seamPrjSet = new SeamProjectsSet(project);
    IProject ejbProject = seamPrjSet.getEjbProject();
    if (ejbProject != null) {
        project = ejbProject;
    }
    IJavaProject javaProject = EclipseResourceUtil.getJavaProject(project);
    if (javaProject == null) {
        SeamGuiPlugin.getPluginLog().logError("Can't find java project with name: " + projectName);
        return;
    }
    IPackageFragmentRoot packageFragmentRoot = null;
    IPackageFragmentRoot[] roots;
    try {
        roots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                packageFragmentRoot = roots[i];
                break;
            }
        }
    } catch (JavaModelException e) {
        SeamGuiPlugin.getPluginLog().logError(e);
    }
    if (packageFragmentRoot == null) {
        packageFragmentRoot = javaProject.getPackageFragmentRoot(javaProject.getResource());
    }
    if (packageFragmentRoot == null) {
        SeamGuiPlugin.getPluginLog().logError("Can't find src folder for project " + projectName);
        return;
    }
    IJavaElement[] packages = null;
    try {
        packages = packageFragmentRoot.getChildren();
    } catch (JavaModelException e) {
        SeamGuiPlugin.getPluginLog().logError(e);
    }
    if (packages == null) {
        packages = new IJavaElement[0];
    }

    String initialValue = getFieldEditor().getValue().toString();
    IJavaElement initialElement = null;
    ArrayList<IJavaElement> packagesWithoutDefaultPackage = new ArrayList<IJavaElement>();
    for (IJavaElement packageElement : packages) {
        String packageName = packageElement.getElementName();
        if (packageName.length() > 0) {
            packagesWithoutDefaultPackage.add(packageElement);
            if (packageName.equals(initialValue)) {
                initialElement = packageElement;
            }
        }
    }

    packages = packagesWithoutDefaultPackage.toArray(new IJavaElement[packagesWithoutDefaultPackage.size()]);
    ElementListSelectionDialog dialog = new ElementListSelectionDialog(Display.getCurrent().getActiveShell(),
            new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT));
    dialog.setTitle(J2EEUIMessages.PACKAGE_SELECTION_DIALOG_TITLE);
    dialog.setMessage(J2EEUIMessages.PACKAGE_SELECTION_DIALOG_DESC);
    dialog.setEmptyListMessage(J2EEUIMessages.PACKAGE_SELECTION_DIALOG_MSG_NONE);
    dialog.setElements(packages);
    if (initialElement != null) {
        dialog.setInitialSelections(new Object[] { initialElement });
    }
    if (dialog.open() == Window.OK) {
        IPackageFragment fragment = (IPackageFragment) dialog.getFirstResult();
        if (fragment != null) {
            getFieldEditor().setValue(fragment.getElementName());
        } else {
            getFieldEditor().setValue("");
        }
    }
}

From source file:org.jboss.tools.ws.creation.core.utils.JBossWSCreationUtils.java

License:Open Source License

/**
 * find compilationunit by annotation/*from ww  w .j  ava 2 s.  c  om*/
 * 
 * @param project
 * @param annotation
 * @return
 */
public static List<ICompilationUnit> findJavaUnitsByAnnotation(IJavaProject project, String annotation,
        String packageName) {
    List<ICompilationUnit> units = new LinkedList<ICompilationUnit>();
    try {
        IPath path = addPackagetoPath(project, packageName);
        if (path == null) {
            IResource[] resources = JBossWSCreationUtils.getJavaSourceRoots(project);
            if (resources != null && resources.length > 0) {
                IJavaElement[] elements = project.getPackageFragmentRoot(resources[0]).getChildren();
                for (IJavaElement element : elements) {
                    if (IJavaElement.PACKAGE_FRAGMENT == element.getElementType()) {
                        findInPackageFragment(units, element.getPath(), project, annotation);
                    }
                }
            }
        } else {
            findInPackageFragment(units, path, project, annotation);
        }
    } catch (JavaModelException e) {
        JBossWSCreationCorePlugin.getDefault().logError(e);
    }
    return units;
}