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

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

Introduction

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

Prototype

IJavaProject getJavaProject();

Source Link

Document

Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).

Usage

From source file:org.eclipse.ajdt.internal.buildpath.AddToInpathAction.java

License:Open Source License

public void selectionChanged(IAction action, ISelection sel) {
    boolean enable = false;
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) sel;
        Object element = selection.getFirstElement();
        try {/*from  ww  w .j  a  va2 s .  co m*/
            if (element instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) element;
                project = root.getJavaProject().getProject();
                cpEntry = root.getRawClasspathEntry();
                jarFile = null;
                fileName = root.getElementName();
                enable = !AspectJCorePreferences.isOnInpath(cpEntry);
            } else {
                jarFile = getJARFile(selection);
                if (jarFile != null) {
                    cpEntry = null;
                    project = jarFile.getProject();
                    enable = (!AspectJCorePreferences.isOnInpath(project,
                            jarFile.getFullPath().toPortableString()) && !checkIfAddingOutjar(project));
                    fileName = jarFile.getName();
                }
            }
        } catch (JavaModelException e) {
        }
        action.setEnabled(enable);
    }
}

From source file:org.eclipse.ajdt.internal.buildpath.RemoveFromAspectpathAction.java

License:Open Source License

public void selectionChanged(IAction action, ISelection sel) {
    boolean enable = false;
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) sel;
        Object element = selection.getFirstElement();
        try {//from w ww .  j av a2 s . c  om
            if (element instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) element;
                project = root.getJavaProject().getProject();
                cpEntry = root.getRawClasspathEntry();
                enable = AspectJCorePreferences.isOnAspectpath(cpEntry);
            }
        } catch (JavaModelException e) {
        }
        action.setEnabled(enable);
    }
}

From source file:org.eclipse.ajdt.internal.buildpath.RemoveFromInpathAction.java

License:Open Source License

public void selectionChanged(IAction action, ISelection sel) {
    boolean enable = false;
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) sel;
        Object element = selection.getFirstElement();
        try {/* www . j  av a2 s  .c om*/
            if (element instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) element;
                project = root.getJavaProject().getProject();
                cpEntry = root.getRawClasspathEntry();
                enable = AspectJCorePreferences.isOnInpath(cpEntry);
            }
        } catch (JavaModelException e) {
        }
        action.setEnabled(enable);
    }
}

From source file:org.eclipse.ajdt.internal.buildpath.UpdateAspectpathRestriction.java

License:Open Source License

public void selectionChanged(IAction action, ISelection sel) {
    boolean enable = false;
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) sel;
        Object element = selection.getFirstElement();
        try {/*from  w  ww. j av a2  s  . co m*/
            if (element instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) element;
                project = root.getJavaProject().getProject();
                cpEntry = root.getRawClasspathEntry();
                if (cpEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    fileName = root.getElementName();
                    enable = AspectJCorePreferences.isOnAspectpath(cpEntry);
                } else {
                    fileName = null;
                    cpEntry = null;
                    project = null;
                    enable = false;
                }
            } else {
                enable = false;
            }
        } catch (JavaModelException e) {
        }
        action.setEnabled(enable);
    }
}

From source file:org.eclipse.ajdt.internal.buildpath.UpdateInpathRestriction.java

License:Open Source License

public void selectionChanged(IAction action, ISelection sel) {
    boolean enable = false;
    if (sel instanceof IStructuredSelection) {
        IStructuredSelection selection = (IStructuredSelection) sel;
        Object element = selection.getFirstElement();
        try {/*from w w  w .  java 2  s  .  co m*/
            if (element instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) element;
                project = root.getJavaProject().getProject();
                cpEntry = root.getRawClasspathEntry();
                if (cpEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    fileName = root.getElementName();
                    enable = AspectJCorePreferences.isOnInpath(cpEntry);
                } else {
                    fileName = null;
                    cpEntry = null;
                    project = null;
                    enable = false;
                }
            } else {
                enable = false;
            }
        } catch (JavaModelException e) {
        }
        action.setEnabled(enable);
    }
}

From source file:org.eclipse.ajdt.internal.launching.LTWUtils.java

License:Open Source License

/**
 * Generate one aop-ajc.xml file for each source directory in the given project.
 * The aop-ajc.xml files will list all concrete aspects included in the active
 * build configuration./* w  ww  .j  av  a 2s . c o  m*/
 * @param project
 */
public static void generateLTWConfigFile(IJavaProject project) {
    try {
        // Get all the source folders in the project
        IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            IPackageFragmentRoot root = roots[i];
            if (!(root instanceof JarPackageFragmentRoot) && root.getJavaProject().equals(project)) {
                List aspects = getAspects(root);
                String path;
                if (root.getElementName().trim().equals("")) { //$NON-NLS-1$
                    path = AOP_XML_LOCATION;
                } else {
                    path = root.getElementName().trim().concat("/").concat(AOP_XML_LOCATION); //$NON-NLS-1$
                }
                IFile ltwConfigFile = (IFile) project.getProject().findMember(path);

                // If the source folder does not already contain an aop-ajc.xml file:
                if (ltwConfigFile == null) {
                    if (aspects.size() != 0) { // If there are aspects in the list

                        // Create the META-INF folder and the aop-ajc.xml file
                        IFolder metainf = (IFolder) ((Workspace) ResourcesPlugin.getWorkspace()).newResource(
                                project.getPath().append("/" + root.getElementName() + "/META-INF"), //$NON-NLS-1$ //$NON-NLS-2$
                                IResource.FOLDER);
                        IFile aopFile = (IFile) ((Workspace) ResourcesPlugin.getWorkspace())
                                .newResource(project.getPath().append(path), IResource.FILE);
                        if (metainf == null || !metainf.exists()) {
                            metainf.create(true, true, null);
                        }
                        aopFile.create(new ByteArrayInputStream(new byte[0]), true, null);
                        project.getProject().refreshLocal(4, null);

                        // Add the xml content to the aop-ajc.xml file
                        addAspectsToLTWConfigFile(false, aspects, aopFile);
                        copyToOutputFolder(aopFile, project, root.getRawClasspathEntry());
                    }

                    // Otherwise update the existing file   
                } else {
                    addAspectsToLTWConfigFile(true, aspects, ltwConfigFile);
                    copyToOutputFolder(ltwConfigFile, project, root.getRawClasspathEntry());
                }
            }
        }
    } catch (Exception e) {
    }

}

From source file:org.eclipse.ajdt.internal.launching.LTWUtils.java

License:Open Source License

/**
 * Get a list of all the aspects found in the given source
 * directory, which are included in the current build.
 * @param root/*  w  w w  . j  ava2s . c om*/
 * @return List of AspectElements
 * @throws CoreException
 */
public static List<IType> getAspects(final IPackageFragmentRoot root) throws CoreException {
    final List<IType> aspects = new ArrayList();
    final Set<IFile> includedFiles = BuildConfig.getIncludedSourceFiles(root.getJavaProject().getProject());
    root.getResource().accept(new IResourceVisitor() {

        public boolean visit(IResource resource) {
            if (includedFiles.contains(resource)) {
                AJCompilationUnit ajcu = AJCompilationUnitManager.INSTANCE
                        .getAJCompilationUnit((IFile) resource);
                if (ajcu != null) {
                    try {
                        IType[] types = ajcu.getAllAspects();
                        for (int i = 0; i < types.length; i++) {
                            aspects.add(types[i]);
                        }
                    } catch (JavaModelException e) {
                    }
                } else {
                    ICompilationUnit cu = JavaCore.createCompilationUnitFrom((IFile) resource);
                    if (cu != null) {
                        Set<IType> types = AJProjectModelFactory.getInstance().getModelForJavaElement(cu)
                                .aspectsForFile(cu);

                        for (IType element : types) {
                            aspects.add(element);
                        }
                    }
                }
            }
            return resource.getType() == IResource.FOLDER || resource.getType() == IResource.PROJECT;
        }
    });
    return aspects;
}

From source file:org.eclipse.ajdt.internal.ui.ajdocexport.AJdocProjectContentProvider.java

License:Open Source License

public Object getParent(Object element) {

    IJavaElement parent = ((IJavaElement) element).getParent();
    if (parent instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) parent;
        if (root.getPath().equals(root.getJavaProject().getProject().getFullPath())) {
            return root.getJavaProject();
        }/*from w  w  w . ja  v a  2 s  . c  o m*/
    }
    return parent;
}

From source file:org.eclipse.ajdt.internal.ui.wizards.exports.AJJarPackageWizardPage.java

License:Open Source License

private Set removeContainedChildren(Set elements) {
    Set newList = new HashSet(elements.size());
    Set javaElementResources = getCorrespondingContainers(elements);
    Iterator iter = elements.iterator();
    boolean removedOne = false;
    while (iter.hasNext()) {
        Object element = iter.next();
        Object parent;/*w  w  w .  j  a  v a2 s  .  c om*/
        if (element instanceof IResource)
            parent = ((IResource) element).getParent();
        else if (element instanceof IJavaElement) {
            parent = ((IJavaElement) element).getParent();
            if (parent instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) parent;
                try {
                    if (pkgRoot.getCorrespondingResource() instanceof IProject)
                        parent = pkgRoot.getJavaProject();
                } catch (JavaModelException ex) {
                    // leave parent as is
                }
            }
        } else {
            // unknown type
            newList.add(element);
            continue;
        }
        if (element instanceof IJavaModel || ((!(parent instanceof IJavaModel))
                && (elements.contains(parent) || javaElementResources.contains(parent))))
            removedOne = true;
        else
            newList.add(element);
    }
    if (removedOne)
        return removeContainedChildren(newList);
    else
        return newList;
}

From source file:org.eclipse.ajdt.internal.ui.wizards.NewTypeWizardPage.java

License:Open Source License

protected IStatus containerChanged() {
    IStatus status = super.containerChanged();
    IPackageFragmentRoot root = getPackageFragmentRoot();
    if ((fTypeKind == ANNOTATION_TYPE || fTypeKind == ENUM_TYPE) && !status.matches(IStatus.ERROR)) {
        if (root != null && !JavaModelUtil.is50OrHigher(root.getJavaProject())) {
            // error as createType will fail otherwise (bug 96928)
            return new StatusInfo(IStatus.ERROR,
                    Messages.format(NewWizardMessages.NewTypeWizardPage_warning_NotJDKCompliant,
                            BasicElementLabels.getJavaElementName(root.getJavaProject().getElementName())));
        }/*from w  w  w  . j  a va2  s. c  o m*/
        if (fTypeKind == ENUM_TYPE) {
            try {
                // if findType(...) == null then Enum is unavailable
                if (findType(root.getJavaProject(), "java.lang.Enum") == null) //$NON-NLS-1$
                    return new StatusInfo(IStatus.WARNING,
                            NewWizardMessages.NewTypeWizardPage_warning_EnumClassNotFound);
            } catch (JavaModelException e) {
                JavaPlugin.log(e);
            }
        }
    }

    fCurrPackageCompletionProcessor.setPackageFragmentRoot(root);
    if (root != null) {
        fEnclosingTypeCompletionProcessor.setPackageFragment(root.getPackageFragment("")); //$NON-NLS-1$
    }
    return status;
}