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

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

Introduction

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

Prototype

IPackageFragment getPackageFragment(String packageName);

Source Link

Document

Returns the package fragment with the given package name.

Usage

From source file:org.jboss.tools.common.model.ui.jarproperties.JarPropertiesTest.java

License:Open Source License

JarEntryFile getJarEntryFileForResource(IProject p, IResource file, String packageName, String fileName) {
    JarEntryFile f = new JarEntryFile(fileName);
    JarEntryResource current = f;/*from ww w  . ja  v  a2  s . c o m*/

    IJavaProject jp = EclipseResourceUtil.getJavaProject(p);
    if (jp == null)
        return null;

    IPackageFragmentRoot root = jp.getPackageFragmentRoot(file);
    if (root == null)
        return null;

    if (current != null && !"META-INF".equalsIgnoreCase(current.getName()) && packageName.length() > 0) {
        IPackageFragment pf = root.getPackageFragment(packageName);
        f.setParent(pf);
    } else {
        current.setParent(root);
    }

    return f;
}

From source file:org.jboss.tools.ws.creation.core.commands.ImplementationClassCreationCommand.java

License:Open Source License

private boolean findImplClass(String claName) throws JavaModelException {
    boolean b = false;
    IPackageFragmentRoot root = JBossWSCreationUtils.getPackageFragmentRoot(project,
            model.getJavaSourceFolder());
    String implPackageName = getImplPackageName();
    IPackageFragment pack = root.getPackageFragment(implPackageName);
    if (pack.getCompilationUnit(claName + ".java").exists()) { //$NON-NLS-1$
        b = true;// w  w w.  j a v  a 2 s  .  co  m
    }
    return b;
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

/**
 * @return/* w  ww .  j  a va 2  s  . c o  m*/
 * @throws JavaModelException
 */
public static IPackageFragment createPackage(IJavaProject javaProject, String pkgName)
        throws JavaModelException {
    IFolder folder = javaProject.getProject().getFolder("src/main/java");
    IPackageFragmentRoot packageFragmentRoot = javaProject.getPackageFragmentRoot(folder);
    IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(pkgName);
    if (!packageFragment.exists()) {
        packageFragment = packageFragmentRoot.createPackageFragment("org.jboss.tools.ws.jaxrs.sample", false,
                new NullProgressMonitor());
    }
    Assert.assertTrue("Target package does not exist", packageFragment.exists());
    return packageFragment;
}

From source file:org.jboss.tools.ws.jaxrs.core.WorkbenchUtils.java

License:Open Source License

/**
 * Create a compilation unit from the given filename content, in the given package, with the given name
 * /*from  w  w  w . j a  v a 2 s .co  m*/
 * @param fileName
 *            the filename containing the source code, in the /resources folder of the test bundle, or null if the
 *            created compilation unit must remain empty after its creation
 * @param pkg
 *            the target package
 * @param unitName
 *            the target compilation unit name
 * @return the created compilation unit
 * @throws JavaModelException
 */
public static ICompilationUnit createCompilationUnit(IJavaProject javaProject, String fileName, String pkg,
        String unitName, Bundle bundle) throws JavaModelException {
    String contents = "";
    if (fileName != null) {
        contents = getResourceContent(fileName, bundle);
    }
    IPackageFragmentRoot sourceFolder = javaProject
            .findPackageFragmentRoot(javaProject.getProject().getFullPath().append("src/main/java"));
    IPackageFragment packageFragment = sourceFolder.getPackageFragment(pkg);
    ICompilationUnit foocompilationUnit = packageFragment.createCompilationUnit(unitName, contents, true,
            new NullProgressMonitor());
    saveAndClose(foocompilationUnit);
    return foocompilationUnit;
}

From source file:org.jboss.tools.ws.jaxrs.ui.wizards.JaxrsElementCreationUtils.java

License:Open Source License

/**
 * Computes the suggested package fragment from the given package fragment.
 * /*from  w ww.  j  a va2  s .c  om*/
 * @param selectedPackage
 *            the {@link IPackageFragment} to process
 * @return {@link IPackageFragment} the suggested package fragment
 */
public static IPackageFragment getSuggestedPackage(final IPackageFragment selectedPackage) {
    final IPath selectedPackagePath = selectedPackage.getPath();
    final IPackageFragmentRoot selectedSourceFolder = (IPackageFragmentRoot) selectedPackage
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (selectedPackage.isDefaultPackage()) {
        return selectedSourceFolder.getPackageFragment("rest");
    } else {
        final String suggestedPackageName = selectedPackagePath.append("rest")
                .makeRelativeTo(selectedSourceFolder.getPath()).toString().replace("/", ".");
        return selectedSourceFolder.getPackageFragment(suggestedPackageName);
    }
}

From source file:org.jboss.tools.ws.jaxrs.ui.wizards.JaxrsResourceCreationWizardPage.java

License:Open Source License

@Override
protected IStatus containerChanged() {
    final IPackageFragmentRoot root = getPackageFragmentRoot();
    if (root != null) {
        targetClassCompletionProcessor.setPackageFragment(root.getPackageFragment("")); //$NON-NLS-1$
    }//from w  ww .j  a va 2  s. com
    return super.containerChanged();
}

From source file:org.jboss.tools.ws.jaxws.ui.utils.JBossWSUIUtils.java

License:Open Source License

public static IStatus validatePackageName(String name, IJavaElement context) {
    IStatus status = null;//from w ww  .  j a  va  2s  .co m
    if (context == null || !context.exists()) {
        status = JavaConventions.validatePackageName(name, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
        if (status != null && !status.isOK()) {
            return status;
        }
    }
    String[] sourceComplianceLevels = getSourceComplianceLevels(context);
    status = JavaConventions.validatePackageName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
    if (status != null && status.getSeverity() == IStatus.ERROR) {
        return status;
    }

    IPackageFragmentRoot[] roots = null;
    try {
        IResource[] srcFolders = JBossWSCreationUtils.getJavaSourceRoots(context.getJavaProject());
        roots = new IPackageFragmentRoot[srcFolders.length];
        int i = 0;
        for (IResource src : srcFolders) {
            roots[i] = context.getJavaProject().getPackageFragmentRoot(src);
            i++;
        }
    } catch (JavaModelException e) {
        JBossWSUIPlugin.log(e);
    }
    for (IPackageFragmentRoot root : roots) {
        if (root != null) {
            IPackageFragment pack = root.getPackageFragment(name);
            try {
                IPath rootPath = root.getPath();
                IPath outputPath = root.getJavaProject().getOutputLocation();
                if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
                    // if the bin folder is inside of our root, don't allow
                    // to name a package
                    // like the bin folder
                    IPath packagePath = pack.getPath();
                    if (outputPath.isPrefixOf(packagePath)) {
                        status = StatusUtils.warningStatus(
                                JBossJAXWSUIMessages.Error_JBossWS_GenerateWizard_IsOutputFolder);
                        return status;
                    }
                }
                if (pack.exists()) {
                    if (pack.containsJavaResources() || !pack.hasSubpackages()) {
                        status = StatusUtils
                                .warningStatus(JBossJAXWSUIMessages.Error_JBossWS_GenerateWizard_PackageExists);
                    } else {
                        status = StatusUtils.warningStatus(
                                JBossJAXWSUIMessages.Error_JBossWS_GenerateWizard_PackageNotShown);
                    }
                    return status;
                } else {
                    if (pack.getResource() == null) {
                        continue;
                    }
                    URI location = pack.getResource().getLocationURI();
                    if (location != null) {
                        IFileStore store = EFS.getStore(location);
                        if (store.fetchInfo().exists()) {
                            status = StatusUtils.warningStatus(
                                    JBossJAXWSUIMessages.Error_JBossWS_GenerateWizard_PackageExistsDifferentCase);
                            return status;
                        }
                    }
                }
            } catch (CoreException e) {
                JBossWSUIPlugin.log(e);
            }
        }
    }
    return status;
}

From source file:org.jboss.tools.ws.ui.utils.JBossWSUIUtils.java

License:Open Source License

public static IStatus validatePackageName(String name, IJavaElement context) {
    IStatus status = null;/*from w  w w.  j a v a  2 s . c  om*/
    if (context == null || !context.exists()) {
        status = JavaConventions.validatePackageName(name, JavaCore.VERSION_1_3, JavaCore.VERSION_1_3);
        if (status != null && !status.isOK()) {
            return status;
        }
    }
    String[] sourceComplianceLevels = getSourceComplianceLevels(context);
    status = JavaConventions.validatePackageName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
    if (status != null && status.getSeverity() == IStatus.ERROR) {
        return status;
    }

    IPackageFragmentRoot[] roots = null;
    try {
        IResource[] srcFolders = JBossWSCreationUtils.getJavaSourceRoots(context.getJavaProject());
        roots = new IPackageFragmentRoot[srcFolders.length];
        int i = 0;
        for (IResource src : srcFolders) {
            roots[i] = context.getJavaProject().getPackageFragmentRoot(src);
            i++;
        }
    } catch (JavaModelException e) {
        JBossWSUIPlugin.log(e);
    }
    for (IPackageFragmentRoot root : roots) {
        if (root != null) {
            IPackageFragment pack = root.getPackageFragment(name);
            try {
                IPath rootPath = root.getPath();
                IPath outputPath = root.getJavaProject().getOutputLocation();
                if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
                    // if the bin folder is inside of our root, don't allow
                    // to name a package
                    // like the bin folder
                    IPath packagePath = pack.getPath();
                    if (outputPath.isPrefixOf(packagePath)) {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_IsOutputFolder);
                        return status;
                    }
                }
                if (pack.exists()) {
                    if (pack.containsJavaResources() || !pack.hasSubpackages()) {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageExists);
                    } else {
                        status = StatusUtils
                                .warningStatus(JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageNotShown);
                    }
                    return status;
                } else {
                    if (pack.getResource() == null) {
                        continue;
                    }
                    URI location = pack.getResource().getLocationURI();
                    if (location != null) {
                        IFileStore store = EFS.getStore(location);
                        if (store.fetchInfo().exists()) {
                            status = StatusUtils.warningStatus(
                                    JBossWSUIMessages.Error_JBossWS_GenerateWizard_PackageExistsDifferentCase);
                            return status;
                        }
                    }
                }
            } catch (CoreException e) {
                JBossWSUIPlugin.log(e);
            }
        }
    }
    return status;
}

From source file:org.mybatis.generator.eclipse.core.callback.EclipseShellCallback.java

License:Apache License

private IPackageFragment getPackage(IPackageFragmentRoot srcFolder, String packageName) throws ShellException {

    IPackageFragment fragment = srcFolder.getPackageFragment(packageName);

    try {//from   ww w  . j a  v  a2s.  c o  m
        if (!fragment.exists()) {
            fragment = srcFolder.createPackageFragment(packageName, true, null);
        }

        fragment.getCorrespondingResource().refreshLocal(IResource.DEPTH_ONE, null);
    } catch (CoreException e) {
        throw new ShellException(e.getStatus().getMessage(), e);
    }

    return fragment;
}

From source file:org.neuro4j.studio.core.diagram.wizards.customblock.CustomBlockNewWizardPage.java

License:Apache License

public void createType(IProgressMonitor monitor, Map<String, String> parameters)
        throws CoreException, InterruptedException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }//  w ww  . java2  s . c o  m

    monitor.beginTask(NewWizardMessages.NewTypeWizardPage_operationdesc, 8);

    IPackageFragmentRoot root = getPackageFragmentRoot();
    IPackageFragment pack = getPackageFragment();
    if (pack == null) {
        pack = root.getPackageFragment(""); //$NON-NLS-1$
    }

    if (!pack.exists()) {
        String packName = pack.getElementName();
        pack = root.createPackageFragment(packName, true, new SubProgressMonitor(monitor, 1));
    } else {
        monitor.worked(1);
    }

    boolean needsSave;
    ICompilationUnit connectedCU = null;

    try {
        String typeName = getTypeName();
        IType createdType;

        String cuName = getTypeName();
        InputStream is = openContentStream();

        String content = readStream(is);
        content = replaceContent(content, "{fileName}", getNameWithoutExt());
        content = replaceContent(content, "{packageFullName}", getPackageFullString(pack));
        content = replaceContent(content, "{staticImport}", getStaticImportString(pack));

        Iterator<String> it = parameters.keySet().iterator();

        while (it.hasNext()) {
            String key = it.next();
            String value = parameters.get(key);
            content = replaceContent(content, key, value);
        }

        ICompilationUnit parentCU = pack.createCompilationUnit(cuName, content, false,
                new SubProgressMonitor(monitor, 2));
        needsSave = true;
        parentCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1));
        connectedCU = parentCU;
        //

        createdType = parentCU.getType(typeName);

        ICompilationUnit cu = createdType.getCompilationUnit();

        if (needsSave) {
            cu.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1));
        } else {
            monitor.worked(1);
        }

    } finally {
        if (connectedCU != null) {
            connectedCU.discardWorkingCopy();
        }
        monitor.done();
    }
}