List of usage examples for org.eclipse.jdt.core IJavaElement getAncestor
IJavaElement getAncestor(int ancestorType);
From source file:org.eclipse.jst.jsf.ui.internal.jspeditor.JavaElementHyperlink.java
License:Open Source License
public String getHyperlinkText() { //Bug 254452 - JSF hyperlink provider shows "unknown hyperlink" when in list final IJavaElement element = determineJavaElement(); if (element != null) { final ICompilationUnit compilationUnit = (ICompilationUnit) element .getAncestor(IJavaElement.COMPILATION_UNIT); if (compilationUnit != null) { return NLS.bind(Messages.Hyperlink_Open_JavaType, compilationUnit.getElementName()); }//ww w. j av a2 s. co m return Messages.Hyperlink_Open_JavaFile; } return Messages.Hyperlink_Open_JavaElement; }
From source file:org.eclipse.linuxtools.changelog.parsers.java.JavaParser.java
License:Open Source License
/** * @see IParserChangeLogContrib#parseCurrentFunction(IEditorPart) *///from ww w . j a v a2 s. c o m public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException { String currentElementName; int elementType; // Get the working copy and connect to input. IWorkingCopyManager manager = JavaUI.getWorkingCopyManager(); manager.connect(input); // Retrieve the Java Element in question. // The following internal access is done because the getWorkingCopy() method // for the WorkingCopyManager returns null for StorageEditorInput, however, // there is a working copy available through the ICompilationUnitDocumentProvider. // ICompilationUnit workingCopy = manager.getWorkingCopy(input); ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider(); // Retrieve the Java Element in question. ICompilationUnit workingCopy = x.getWorkingCopy(input); if (workingCopy == null) return ""; IJavaElement method = workingCopy.getElementAt(offset); manager.disconnect(input); // no element selected if (method == null) return ""; // Get the current element name, to test it. currentElementName = method.getElementName(); // Element doesn't have a name. Can go no further. if (currentElementName == null) return ""; // Get the Element Type to test. elementType = method.getElementType(); switch (elementType) { case IJavaElement.METHOD: case IJavaElement.FIELD: break; case IJavaElement.COMPILATION_UNIT: return ""; case IJavaElement.INITIALIZER: return STATIC_INITIALIZER_NAME; // So it's not a method, field, type, or static initializer. Where are we? default: IJavaElement tmpMethodType; if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null) && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) { return ""; } else { // In a class, but not in a method. Return class name instead. method = tmpMethodType; currentElementName = method.getElementName(); } } // Build all ancestor classes. // Append all ancestor class names to string IJavaElement tmpParent = method.getParent(); boolean firstLoop = true; while (tmpParent != null) { IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE); if (tmpParentClass != null) { String tmpParentClassName = tmpParentClass.getElementName(); if (tmpParentClassName == null) return ""; currentElementName = tmpParentClassName + "." + currentElementName; } else { // cut root class name int rootClassPos = currentElementName.indexOf("."); if (rootClassPos >= 0) currentElementName = currentElementName.substring(rootClassPos + 1); if (firstLoop) return ""; else return currentElementName; } tmpParent = tmpParentClass.getParent(); firstLoop = false; } return ""; }
From source file:org.eclipse.linuxtools.internal.changelog.parsers.java.JavaParser.java
License:Open Source License
@Override public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException { String currentElementName;//ww w .j a va 2 s . c o m int elementType; // Get the working copy and connect to input. IWorkingCopyManager manager = JavaUI.getWorkingCopyManager(); manager.connect(input); // Retrieve the Java Element in question. // The following internal access is done because the getWorkingCopy() // method // for the WorkingCopyManager returns null for StorageEditorInput, // however, // there is a working copy available through the // ICompilationUnitDocumentProvider. ICompilationUnitDocumentProvider x = (ICompilationUnitDocumentProvider) JavaUI.getDocumentProvider(); // Retrieve the Java Element in question. ICompilationUnit workingCopy = x.getWorkingCopy(input); if (workingCopy == null) { return ""; } IJavaElement method = workingCopy.getElementAt(offset); manager.disconnect(input); // no element selected if (method == null) { return ""; } // Get the current element name, to test it. currentElementName = method.getElementName(); // Element doesn't have a name. Can go no further. if (currentElementName == null) { return ""; } // Get the Element Type to test. elementType = method.getElementType(); switch (elementType) { case IJavaElement.METHOD: case IJavaElement.FIELD: break; case IJavaElement.COMPILATION_UNIT: return ""; case IJavaElement.INITIALIZER: return STATIC_INITIALIZER_NAME; // So it's not a method, field, type, or static initializer. Where // are we? default: IJavaElement tmpMethodType; if (((tmpMethodType = method.getAncestor(IJavaElement.METHOD)) == null) && ((tmpMethodType = method.getAncestor(IJavaElement.TYPE)) == null)) { return ""; } else { // In a class, but not in a method. Return class name instead. method = tmpMethodType; currentElementName = method.getElementName(); } } // Build all ancestor classes. // Append all ancestor class names to string IJavaElement tmpParent = method.getParent(); boolean firstLoop = true; while (tmpParent != null) { IJavaElement tmpParentClass = tmpParent.getAncestor(IJavaElement.TYPE); if (tmpParentClass != null) { String tmpParentClassName = tmpParentClass.getElementName(); if (tmpParentClassName == null) { return ""; } currentElementName = tmpParentClassName + "." + currentElementName; } else { // cut root class name int rootClassPos = currentElementName.indexOf('.'); if (rootClassPos >= 0) { currentElementName = currentElementName.substring(rootClassPos + 1); } if (firstLoop) { return ""; } else { return currentElementName; } } tmpParent = tmpParentClass.getParent(); firstLoop = false; } return ""; }
From source file:org.eclipse.mylyn.internal.java.ui.JavaStructureBridge.java
License:Open Source License
/** * Some copying from://from w ww . j av a2s.co m * * @see org.eclipse.jdt.ui.ProblemsLabelDecorator */ public boolean containsProblem(IInteractionElement node) { try { IJavaElement element = (IJavaElement) getObjectForHandle(node.getHandleIdentifier()); switch (element.getElementType()) { case IJavaElement.JAVA_PROJECT: case IJavaElement.PACKAGE_FRAGMENT_ROOT: return getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_INFINITE, null); case IJavaElement.PACKAGE_FRAGMENT: case IJavaElement.COMPILATION_UNIT: case IJavaElement.CLASS_FILE: return getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_ONE, null); case IJavaElement.PACKAGE_DECLARATION: case IJavaElement.IMPORT_DECLARATION: case IJavaElement.IMPORT_CONTAINER: case IJavaElement.TYPE: case IJavaElement.INITIALIZER: case IJavaElement.METHOD: case IJavaElement.FIELD: case IJavaElement.LOCAL_VARIABLE: ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { return getErrorTicksFromMarkers(element.getResource(), IResource.DEPTH_ONE, null); } } } catch (CoreException e) { // ignore } return false; }
From source file:org.eclipse.mylyn.internal.java.ui.LandmarkMarkerManager.java
License:Open Source License
private LandmarkUpdateOperation createRemoveLandmarkMarkerOperation(final IInteractionElement node) { if (node == null) { return null; }//from ww w . j a v a 2s . c o m if (JavaStructureBridge.CONTENT_TYPE.equals(node.getContentType())) { IJavaElement element = JavaCore.create(node.getHandleIdentifier()); if (!element.exists()) { return null; } if (element.getAncestor(IJavaElement.COMPILATION_UNIT) != null // stuff // from .class files && element instanceof ISourceReference) { try { IResource resource = element.getUnderlyingResource(); LandmarkUpdateOperation runnable = new LandmarkUpdateOperation(resource) { public void run(IProgressMonitor monitor) throws CoreException { if (getResource() != null) { try { if (markerMap.containsKey(node)) { long id = markerMap.get(node); IMarker marker = getResource().getMarker(id); if (marker != null) { marker.delete(); } } } catch (NullPointerException e) { // FIXME avoid NPE StatusHandler.log(new Status(IStatus.ERROR, JavaUiBridgePlugin.ID_PLUGIN, "Could not update marker", e)); //$NON-NLS-1$ } } } }; return runnable; } catch (JavaModelException e) { // ignore the Java Model errors } } } return null; }
From source file:org.eclipse.objectteams.otdt.core.ext.MarkableFactory.java
License:Open Source License
public static IMarkableJavaElement createMarkable(IJavaElement javaElement) { IClassFile classFile = (IClassFile) javaElement.getAncestor(IJavaElement.CLASS_FILE); if (classFile != null) return new JavaElementMarkable(classFile); else//from www.j ava 2 s. c o m return new ResourceMarkable(javaElement.getResource()); }
From source file:org.eclipse.objectteams.otdt.internal.core.search.matching.ReferenceToTeamLocator.java
License:Open Source License
/** * A match was found, perform final check: * if a role was specified check the current compilation unit's first type against the role name. * If successful report as a type reference match regarding this first type. *//*from www . jav a 2s . co m*/ @Override protected void matchReportImportRef(ImportReference importRef, Binding binding, IJavaElement element, int accuracy, MatchLocator locator) throws CoreException { if (locator.encloses(element)) { ICompilationUnit cu = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { IType[] types = cu.getTypes(); if (types != null && types.length > 0) { // only now we have the info to check the role name: if (this.pattern.roleName != null && !new String(this.pattern.roleName).equals(types[0].getElementName())) return; int offset = importRef.sourceStart; int length = importRef.sourceEnd - offset + 1; this.match = locator.newTypeReferenceMatch(types[0], null/*binding*/, accuracy, offset, length, importRef); if (this.match != null) locator.report(this.match); } } } }
From source file:org.eclipse.objectteams.otdt.internal.ui.handlers.OpenBindingEditorHandler.java
License:Open Source License
/** * Finds the outermost containing team for the given selection. * @param selection The current selection (can be null). * @return The best matching team for the selection, or null if no team found. *///from ww w . java 2 s.co m protected IOTType getRootTeam(ISelection selection) { IOTType selectedTeam = null; if (!(selection instanceof IStructuredSelection)) return null; Object element = ((IStructuredSelection) selection).getFirstElement(); if (!(element instanceof IJavaElement)) return null; IJavaElement jElement = (IJavaElement) element; ICompilationUnit cu = (ICompilationUnit) jElement.getAncestor(IType.COMPILATION_UNIT); if (cu == null) return null; IType type = cu.findPrimaryType(); IOTType otType = OTModelManager.getOTElement(type); if (otType == null) return null; if (otType.isTeam()) selectedTeam = otType; else if (otType instanceof IRoleFileType) selectedTeam = ((IRoleFileType) otType).getTeam(); return selectedTeam; }
From source file:org.eclipse.objectteams.otdt.internal.ui.wizards.NewTypeWizardPage.java
License:Open Source License
@Override protected IJavaElement getInitialJavaElement(IStructuredSelection structuredSelection) { IJavaElement element = super.getInitialJavaElement(structuredSelection); if (element != null && element.getElementType() == IJavaElement.COMPILATION_UNIT) { // try to improve: IWorkbenchWindow window = JavaPlugin.getActiveWorkbenchWindow(); if (window != null) { ISelection selection = window.getSelectionService().getSelection(); if (selection instanceof ITextSelection) { ITextSelection textSelection = (ITextSelection) selection; try { IJavaElement selected = ((ICompilationUnit) element) .getElementAt(textSelection.getOffset()); if (selected != null) { selected = selected.getAncestor(IJavaElement.TYPE); if (selected != null) { if (((IType) selected).isLocal()) selected = ((IType) selected).getDeclaringType(); return selected; }//from w w w. j a v a 2s . c om } } catch (JavaModelException e) { /* nop */ } } } } return element; }
From source file:org.eclipse.objectteams.otdt.internal.ui.wizards.NewTypeWizardPage.java
License:Open Source License
/** * initializes the package and enclosing type dialog fields * depending on the given initial selected IJavaElement * (that is the IJavaElement which was selected in the Package Explorer, * when the request to open the wizard occured) *//*from www . java 2s. co m*/ protected void initPackageAndEnclosingType(IJavaElement initialSelectedElem) { IType potentialEnclosingType = null; IType typeInCU = (IType) initialSelectedElem.getAncestor(IJavaElement.TYPE); if (typeInCU != null) { if (typeInCU.getCompilationUnit() != null) { potentialEnclosingType = typeInCU; } } else { ICompilationUnit cu = (ICompilationUnit) initialSelectedElem.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { potentialEnclosingType = cu.findPrimaryType(); } } //default case IPackageFragment packageFragment = (IPackageFragment) initialSelectedElem .getAncestor(IJavaElement.PACKAGE_FRAGMENT); String packName = (packageFragment == null) ? "" //$NON-NLS-1$ : packageFragment.getElementName(); setPackageFragmentName(packName); setEnclosingTypeName(""); //$NON-NLS-1$ if (potentialEnclosingType != null) { if (OTModelManager.hasOTElementFor(potentialEnclosingType)) { IOTType potentialEnclosingOTType = OTModelManager.getOTElement(potentialEnclosingType); boolean hasChanges = false; if (potentialEnclosingOTType.isTeam()) { handleTeamSelected(potentialEnclosingOTType); hasChanges = true; } else //if potentialEnclosingOTType.isRole() { handleRoleSelected(potentialEnclosingOTType); hasChanges = true; } if (hasChanges) { } } else try { if (potentialEnclosingType.isClass()) { handleClassSelected(potentialEnclosingType); } } catch (JavaModelException ex) { OTDTUIPlugin.logException("", ex); //$NON-NLS-1$ } } }