Example usage for org.eclipse.jdt.core IJavaElement PACKAGE_FRAGMENT

List of usage examples for org.eclipse.jdt.core IJavaElement PACKAGE_FRAGMENT

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement PACKAGE_FRAGMENT.

Prototype

int PACKAGE_FRAGMENT

To view the source code for org.eclipse.jdt.core IJavaElement PACKAGE_FRAGMENT.

Click Source Link

Document

Constant representing a package fragment.

Usage

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

License:Open Source License

public static ICompilationUnit findUnitByFileName(IJavaElement javaElem, String filePath) throws Exception {
    ICompilationUnit unit = null;// w w w . j  a  v  a2 s . co m

    if (!javaElem.getOpenable().isOpen()) {
        javaElem.getOpenable().open(null);
    }

    IJavaElement[] elems = null;

    if (javaElem instanceof IParent) {
        IParent parent = (IParent) javaElem;
        elems = parent.getChildren();
    }

    if (elems == null) {
        return null;
    }

    for (IJavaElement elem : elems) {
        if (elem.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
            IPackageFragmentRoot root = (IPackageFragmentRoot) elem;

            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                unit = findUnitByFileName(elem, filePath);

                if (unit != null) {
                    return unit;
                }
            }
        } else if ((elem.getElementType() == IJavaElement.PACKAGE_FRAGMENT)
                || (elem.getElementType() == IJavaElement.JAVA_PROJECT)) {
            unit = findUnitByFileName(elem, filePath);

            if (unit != null) {
                return unit;
            }
        } else if (elem.getElementType() == IJavaElement.COMPILATION_UNIT) {
            ICompilationUnit compUnit = (ICompilationUnit) elem;

            if (compUnit.getPath().toString().equals(filePath)) {
                compUnit.open(null);

                return compUnit;
            }
        }
    }

    return null;
}

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

License:Open Source License

/**
 * find compilationunit by annotation// www.  ja v  a 2  s  .  co  m
 * 
 * @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;
}

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

License:Open Source License

/**
 * Sets the extra default values from the given 'compilationUnit' argument
 * //from w  ww  . ja va  2  s .c  o m
 * @param compilationUnit
 *            the selected {@link ICompilationUnit}.
 */
private void setDefaultValues(final ICompilationUnit compilationUnit) {
    setPackageFragmentRoot(
            (IPackageFragmentRoot) compilationUnit.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT), true);
    final IPackageFragment packageFragment = (IPackageFragment) compilationUnit
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    setPackageFragment(packageFragment, true);
    setTypeName(getSuggestedApplicationTypeName(packageFragment), true);
}

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

License:Open Source License

@Test
public void shouldProposeOtherApplicationNameWhenSelectingProject() throws CoreException, InterruptedException {
    // given//from   w  ww  .  j  a  v a 2s  . co  m
    final ICompilationUnit restApplication2CompilationUnit = metamodelMonitor.createCompilationUnit(
            "RestApplication2.txt", "org.jboss.tools.ws.jaxrs.sample.services", "RestApplication2.java");
    final JaxrsApplicationCreationWizardPage wizardPage = new JaxrsApplicationCreationWizardPage(true);
    final IStructuredSelection selection = new StructuredSelection(
            restApplication2CompilationUnit.getAncestor(IJavaElement.PACKAGE_FRAGMENT));
    // when
    wizardPage.init(selection);
    wizardPage.setApplicationMode(JaxrsApplicationCreationWizardPage.APPLICATION_JAVA);
    wizardPage.createType(new NullProgressMonitor());
    // then
    final IType createdType = wizardPage.getCreatedType();
    assertThat(createdType, notNullValue());
    // trigger a clean build before asserting the new JAX-RS elements
    metamodelMonitor.buildProject(IncrementalProjectBuilder.FULL_BUILD);
    // 0 new element: metamodel has no application
    assertThat(createdType, notNullValue());
    assertThat(createdType.getFullyQualifiedName(),
            equalTo("org.jboss.tools.ws.jaxrs.sample.services.RestApplication3"));
}

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

License:Open Source License

/**
 * Computes the suggested package fragment from the given compilation unit.
 * //from w ww  .j  ava2s  .c  o  m
 * @param compilationUnit
 *            the {@link ICompilationUnit} to process
 * @return {@link IPackageFragment} the suggested package fragment
 */
public static IPackageFragment getSuggestedPackage(final ICompilationUnit compilationUnit) {
    final IPackageFragment selectedPackage = (IPackageFragment) compilationUnit
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    if (!selectedPackage.isDefaultPackage()) {
        try {
            final IPackageFragment parentPackageFragment = selectedPackage.getJavaProject()
                    .findPackageFragment(selectedPackage.getPath().removeLastSegments(1));
            return getSuggestedPackage(parentPackageFragment);
        } catch (JavaModelException e) {
            Logger.error("Failed to retrieve parent package for '" + selectedPackage.getElementName() + "'", e);
        }
    }
    return getSuggestedPackage(selectedPackage);
}

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

License:Open Source License

/**
 * find compilationunit by annotation/* w  w w .  j av  a 2  s  . c o m*/
 * 
 * @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) {
        JBossJAXWSUIPlugin.getDefault().logError(e);
    }
    return units;
}

From source file:org.jetbrains.kotlin.wizards.NewUnitWizardPage.java

License:Apache License

private String getSourceFolderFromSelection() {
    String defaultFolder = DEFAULT_SOURCE_FOLDER;

    if (selection.isEmpty()) {
        return defaultFolder;
    }/*from   w  ww .  ja v a2  s.  co  m*/

    Object selectedObject = selection.getFirstElement();

    if (selectedObject instanceof IJavaElement) {
        IJavaElement selectedJavaElement = (IJavaElement) selectedObject;
        switch (selectedJavaElement.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
            return getDefaultSrcByProject((IJavaProject) selectedJavaElement);

        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return selectedJavaElement.getPath().toPortableString();

        case IJavaElement.PACKAGE_FRAGMENT:
        case IJavaElement.COMPILATION_UNIT:
            return selectedJavaElement.getPath().uptoSegment(2).toPortableString();
        }
    } else if (selectedObject instanceof IResource) {
        IResource selectedResource = (IResource) selectedObject;
        switch (selectedResource.getType()) {
        case IResource.FOLDER:
            return getDefaultSrcByProject(JavaCore.create(selectedResource.getProject()));

        case IResource.FILE:
            return selectedResource.getFullPath().uptoSegment(2).toPortableString();
        }
    }

    return defaultFolder;
}

From source file:org.jetbrains.kotlin.wizards.NewUnitWizardPage.java

License:Apache License

private String getPackageFromSelection() {
    String defaultPackage = DEFAULT_PACKAGE;

    if (selection.isEmpty()) {
        return defaultPackage;
    }//from w  ww  .  j a  v a  2s . c o m

    Object selectedObject = selection.getFirstElement();

    if (selectedObject instanceof IJavaElement) {
        IJavaElement selectedJavaElement = (IJavaElement) selectedObject;
        switch (selectedJavaElement.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT:
            return selectedJavaElement.getElementName();

        case IJavaElement.COMPILATION_UNIT:
            try {
                return selectedJavaElement.getJavaProject()
                        .findPackageFragment(selectedJavaElement.getPath().makeAbsolute().removeLastSegments(1))
                        .getElementName();
            } catch (Exception e) {
                KotlinLogger.logAndThrow(e);
            }
            break;
        }
    } else if (selectedObject instanceof IResource) {
        IResource selectedResource = (IResource) selectedObject;
        switch (selectedResource.getType()) {
        case IResource.FILE:
            try {
                return JavaCore.create(selectedResource.getProject())
                        .findPackageFragment(
                                selectedResource.getFullPath().makeAbsolute().removeLastSegments(1))
                        .getElementName();
            } catch (Exception e) {
                KotlinLogger.logAndThrow(e);
            }
            break;
        }
    }

    return defaultPackage;
}

From source file:org.limy.eclipse.common.jdt.LimyJavaUtils.java

License:Open Source License

/**
 * JavavfSJavat@C? results i[?B/*from   w w w  .  j a va 2s . co  m*/
 * @param results i[?
 * @param javaElement Javavf
 * @throws CoreException RAO
 */
private static void appendForIParent(Collection<IJavaElement> results, IJavaElement javaElement)
        throws CoreException {

    int type = javaElement.getElementType();
    if (type == IJavaElement.JAVA_PROJECT || type == IJavaElement.PACKAGE_FRAGMENT_ROOT
            || type == IJavaElement.PACKAGE_FRAGMENT) {

        IParent parent = (IParent) javaElement;
        for (IJavaElement child : parent.getChildren()) {
            // JavapbP?[W???ATupbP?[Wr
            appendAllJavas(results, child);
        }
    }
}

From source file:org.limy.eclipse.qalab.common.LimyQalabJavaUtils.java

License:Open Source License

/**
 * JavavfbinfBNg?B//from  w ww.ja  va2 s . c o  m
 * <p>
 * pbP?[Wvf : binfBNgTufBNg<br>
 * \?[XfBNg : binfBNg<br>
 * </p>
 * @param el Javavf
 * @return binfBNg?i?pX?j
 * @throws CoreException 
 */
public static String getBinDirPath(IJavaElement el) throws CoreException {

    if (el.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) el.getParent();
        String path = LimyQalabJavaUtils.getExternalLocation(root);
        return new File(path, el.getElementName().replace('.', '/')).getAbsolutePath();
    }
    if (el.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
        return LimyQalabJavaUtils.getExternalLocation((IPackageFragmentRoot) el);
    }
    return null;
}