Search in sources :

Example 86 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project bndtools by bndtools.

the class PackageDecorator method updateDecoration.

public static void updateDecoration(IProject project, Project model) throws Exception {
    if (!project.isOpen()) {
        return;
    }
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        // project is not a java project
        return;
    }
    boolean changed = false;
    for (IClasspathEntry cpe : javaProject.getRawClasspath()) {
        if (cpe.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            continue;
        }
        for (IPackageFragmentRoot pkgRoot : javaProject.findPackageFragmentRoots(cpe)) {
            assert pkgRoot.getKind() == IPackageFragmentRoot.K_SOURCE;
            IResource pkgRootResource = pkgRoot.getCorrespondingResource();
            if (pkgRootResource == null) {
                continue;
            }
            File pkgRootFile = pkgRootResource.getLocation().toFile();
            boolean pkgInSourcePath = model.getSourcePath().contains(pkgRootFile);
            for (IJavaElement child : pkgRoot.getChildren()) {
                IPackageFragment pkg = (IPackageFragment) child;
                assert pkg.getKind() == IPackageFragmentRoot.K_SOURCE;
                IResource pkgResource = pkg.getCorrespondingResource();
                if (pkgResource == null) {
                    continue;
                }
                String text = pkgResource.getPersistentProperty(packageDecoratorKey);
                if (pkgInSourcePath) {
                    String pkgName = pkg.getElementName();
                    // Decorate if exported package
                    Attrs pkgAttrs = model.getExports().getByFQN(pkgName);
                    if (pkgAttrs != null) {
                        StringBuilder sb = new StringBuilder(" ").append(Version.parseVersion(pkgAttrs.getVersion()));
                        pkgAttrs = model.getImports().getByFQN(pkgName);
                        if (pkgAttrs != null) {
                            String versionRange = pkgAttrs.getVersion();
                            if (versionRange != null) {
                                sb.append('↔').append(versionRange);
                            }
                        }
                        String version = sb.toString();
                        if (!version.equals(text)) {
                            pkgResource.setPersistentProperty(packageDecoratorKey, version);
                            changed = true;
                        }
                        continue;
                    }
                    // Decorate if non-empty, non-contained package
                    if (pkg.containsJavaResources() && !model.getContained().containsFQN(pkgName)) {
                        if (!excluded.equals(text)) {
                            pkgResource.setPersistentProperty(packageDecoratorKey, excluded);
                            changed = true;
                        }
                        continue;
                    }
                }
                // Clear decoration
                if (text != null) {
                    pkgResource.setPersistentProperty(packageDecoratorKey, null);
                    changed = true;
                }
            }
        }
    }
    // If decoration change, update display
    if (changed) {
        Display display = PlatformUI.getWorkbench().getDisplay();
        SWTConcurrencyUtil.execForDisplay(display, true, new Runnable() {

            @Override
            public void run() {
                PlatformUI.getWorkbench().getDecoratorManager().update(packageDecoratorId);
            }
        });
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Attrs(aQute.bnd.header.Attrs) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) File(java.io.File) IResource(org.eclipse.core.resources.IResource) Display(org.eclipse.swt.widgets.Display)

Example 87 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project bndtools by bndtools.

the class PackageDropAdapter method performDrop.

@Override
public boolean performDrop(Object data) {
    int insertionIndex = -1;
    Object target = getCurrentTarget();
    if (target != null) {
        insertionIndex = indexOf(target);
        int loc = getCurrentLocation();
        if (loc == LOCATION_ON || loc == LOCATION_AFTER)
            insertionIndex++;
    }
    List<T> newEntries = new ArrayList<T>();
    if (data instanceof IResource[]) {
        for (IResource resource : (IResource[]) data) {
            IJavaElement javaElement = JavaCore.create(resource);
            if (javaElement instanceof IPackageFragment) {
                newEntries.add(createNewEntry(javaElement.getElementName()));
            }
        }
    } else if (data instanceof IStructuredSelection) {
        Iterator<?> iterator = ((IStructuredSelection) data).iterator();
        while (iterator.hasNext()) {
            Object element = iterator.next();
            if (element instanceof IPackageFragment) {
                IPackageFragment pkg = (IPackageFragment) element;
                newEntries.add(createNewEntry(pkg.getElementName()));
            } else if (element instanceof Capability) {
                Capability cap = (Capability) element;
                String namespace = cap.getNamespace();
                if (PackageNamespace.PACKAGE_NAMESPACE.equals(namespace)) {
                    String pkgName = (String) cap.getAttributes().get(namespace);
                    newEntries.add(createNewEntry(pkgName));
                }
            } else if (element instanceof Requirement) {
                String pkgName = getPackageNameFromRequirement((Requirement) element);
                if (pkgName != null)
                    newEntries.add(createNewEntry(pkgName));
            } else if (element instanceof RequirementWrapper) {
                String pkgName = getPackageNameFromRequirement(((RequirementWrapper) element).requirement);
                if (pkgName != null)
                    newEntries.add(createNewEntry(pkgName));
            }
        }
    }
    addRows(insertionIndex, newEntries);
    return true;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Capability(org.osgi.resource.Capability) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Requirement(org.osgi.resource.Requirement) Iterator(java.util.Iterator) RequirementWrapper(bndtools.model.resolution.RequirementWrapper) IResource(org.eclipse.core.resources.IResource)

Example 88 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project bndtools by bndtools.

the class ComponentMarker method parseChildrenForComponents.

private static void parseChildrenForComponents(IPackageFragment pkg) throws JavaModelException, CoreException {
    for (IJavaElement e : pkg.getChildren()) {
        if (e instanceof ICompilationUnit) {
            ICompilationUnit compUnit = (ICompilationUnit) e;
            if (!isComponentInImports(compUnit)) {
                continue;
            }
            compUnit.getResource().deleteMarkers(BndtoolsConstants.MARKER_COMPONENT, true, IResource.DEPTH_ONE);
            findAndMarkComponentAnnotations(compUnit);
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit)

Example 89 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project bndtools by bndtools.

the class AbstractLaunchShortcut method launch.

@Override
public void launch(ISelection selection, String mode) {
    IStructuredSelection is = (IStructuredSelection) selection;
    int size = is.size();
    if (size == 1 && is.getFirstElement() != null) {
        try {
            Object selected = is.getFirstElement();
            launchSelectedObject(selected, mode);
        } catch (CoreException e) {
            ErrorDialog.openError(null, "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error configuring launch.", e));
        }
    } else if (size > 1) {
        // only support multiple IJavaElements
        List<IJavaElement> elements = new ArrayList<>();
        Iterator<?> iterator = is.iterator();
        while (iterator.hasNext()) {
            Object element = iterator.next();
            if (element instanceof IJavaElement) {
                elements.add((IJavaElement) element);
            }
        }
        if (!elements.isEmpty()) {
            try {
                launchJavaElements(elements, mode);
            } catch (CoreException e) {
                ErrorDialog.openError(null, "Error", null, new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error launching elements.", e));
            }
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IJavaElement(org.eclipse.jdt.core.IJavaElement) CoreException(org.eclipse.core.runtime.CoreException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 90 with IJavaElement

use of org.eclipse.jdt.core.IJavaElement in project bndtools by bndtools.

the class AbstractLaunchShortcut method launchSelectedObject.

protected void launchSelectedObject(Object selected, String mode) throws CoreException {
    if (selected instanceof IJavaElement) {
        launchJavaElements(Collections.singletonList((IJavaElement) selected), mode);
    } else if (selected instanceof IResource && Project.BNDFILE.equals(((IResource) selected).getName())) {
        IProject project = ((IResource) selected).getProject();
        launchProject(project, mode);
    } else if (selected instanceof IFile && ((IFile) selected).getName().endsWith(LaunchConstants.EXT_BNDRUN)) {
        IFile bndRunFile = (IFile) selected;
        launchBndRun(bndRunFile, mode);
    } else if (selected instanceof IAdaptable) {
        IAdaptable adaptable = (IAdaptable) selected;
        IJavaElement javaElement = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
        if (javaElement != null) {
            launchJavaElements(Collections.singletonList(javaElement), mode);
        } else {
            IResource resource = (IResource) adaptable.getAdapter(IResource.class);
            if (resource != null && resource != selected)
                launchSelectedObject(resource, mode);
        }
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IAdaptable(org.eclipse.core.runtime.IAdaptable) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject)

Aggregations

IJavaElement (org.eclipse.jdt.core.IJavaElement)226 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 IType (org.eclipse.jdt.core.IType)44 ArrayList (java.util.ArrayList)35 IJavaProject (org.eclipse.jdt.core.IJavaProject)35 IMethod (org.eclipse.jdt.core.IMethod)34 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)32 JavaModelException (org.eclipse.jdt.core.JavaModelException)32 IResource (org.eclipse.core.resources.IResource)30 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)28 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)23 IField (org.eclipse.jdt.core.IField)16 IMember (org.eclipse.jdt.core.IMember)14 Test (org.junit.Test)14 CoreException (org.eclipse.core.runtime.CoreException)13 StringTokenizer (java.util.StringTokenizer)11 ISourceRange (org.eclipse.jdt.core.ISourceRange)11 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)11 IPath (org.eclipse.core.runtime.IPath)10 HashMap (java.util.HashMap)9