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);
}
});
}
}
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;
}
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);
}
}
}
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));
}
}
}
}
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);
}
}
}
Aggregations