List of usage examples for org.eclipse.jdt.core IJavaElement getAncestor
IJavaElement getAncestor(int ancestorType);
From source file:org.seasar.s2junit4plugin.wizard.NewS2JUnit4TypeWizardPage.java
License:Apache License
/** * Initializes all fields provided by the page with a given selection. * /* w w w . ja v a 2s. c om*/ * @param elem the selection used to initialize this page or <code> * null</code> if no selection was available */ protected void initTypePage(IJavaElement elem) { String initSuperclass = "java.lang.Object"; //$NON-NLS-1$ ArrayList initSuperinterfaces = new ArrayList(5); IJavaProject project = null; IPackageFragment pack = null; IType enclosingType = null; if (elem != null) { // evaluate the enclosing type project = elem.getJavaProject(); pack = (IPackageFragment) elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); IType typeInCU = (IType) elem.getAncestor(IJavaElement.TYPE); if (typeInCU != null) { if (typeInCU.getCompilationUnit() != null) { enclosingType = typeInCU; } } else { ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { enclosingType = cu.findPrimaryType(); } } try { IType type = null; if (elem.getElementType() == IJavaElement.TYPE) { type = (IType) elem; if (type.exists()) { String superName = S2JUnit4SuperInterfaceSelectionDialog.getNameWithTypeParameters(type); if (type.isInterface()) { initSuperinterfaces.add(superName); } else { initSuperclass = superName; } } } } catch (JavaModelException e) { JavaPlugin.log(e); // ignore this exception now } } String typeName = ""; //$NON-NLS-1$ ITextSelection selection = getCurrentTextSelection(); if (selection != null) { String text = selection.getText(); if (text != null && validateJavaTypeName(text, project).isOK()) { typeName = text; } } setPackageFragment(pack, true); setEnclosingType(enclosingType, true); setEnclosingTypeSelection(false, true); setTypeName(typeName, true); setSuperClass(initSuperclass, true); setSuperInterfaces(initSuperinterfaces, true); setAddComments(StubUtility.doAddComments(project), true); // from project or workspace }
From source file:org.springframework.ide.eclipse.data.jdt.core.EntityPropertyCompletionProposals.java
License:Open Source License
@Override public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {/*ww w . j a v a 2s .c om*/ if (!(context instanceof JavaContentAssistInvocationContext)) { return Collections.emptyList(); } JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context; if (!SpringCoreUtils.isSpringProject(javaContext.getProject().getProject())) { return Collections.emptyList(); } if (javaContext.getCoreContext() == null) { return Collections.emptyList(); } ICompilationUnit cu = javaContext.getCompilationUnit(); try { int invocationOffset = context.getInvocationOffset(); IJavaElement element = cu.getElementAt(invocationOffset); if (element instanceof SourceMethod) { return computeCompletionProposals((SourceMethod) element, javaContext); } else { if (element != null) { IType type = (IType) element.getAncestor(IJavaElement.TYPE); if (type != null) { return computeCompletionProposals(type, javaContext); } } } } catch (JavaModelException e) { } return Collections.emptyList(); }
From source file:org.springframework.tooling.jdt.ls.extension.JavaLocationHandler.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*w ww.jav a 2 s .co m*/ public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor monitor) throws Exception { Location location = null; Map<String, Object> obj = (Map<String, Object>) arguments.get(0); String uri = (String) obj.get("projectUri"); URI projectUri = URI.create(uri); String bindingKey = (String) obj.get("bindingKey"); Boolean lookInOtherProjects = (Boolean) obj.get("lookInOtherProjects"); IJavaElement element = JavaData.findElement(projectUri, bindingKey, lookInOtherProjects); if (element != null) { location = JDTUtils.toLocation(element); if (location == null) { IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (cf != null) { location = JDTUtils.toLocation(cf); } } } return location; }
From source file:org.springsource.ide.eclipse.commons.frameworks.ui.internal.utils.SelectionUtils.java
License:Open Source License
public static IType getType(IStructuredSelection selection) { try {//from www. j a v a 2 s.c o m Object element = selection.getFirstElement(); if (element instanceof IJavaElement) { IJavaElement javaElement = (IJavaElement) element; IType type = (IType) javaElement.getAncestor(IJavaElement.TYPE); if (type != null) { return type; } if (javaElement instanceof ICompilationUnit) { //Also handle case where a compilatuon unit is selected, in this case, get the //first type declared in it. ICompilationUnit compUnit = (ICompilationUnit) javaElement; IType[] types = compUnit.getAllTypes(); if (types != null && types.length > 0) { return types[0]; } } } } catch (Exception e) { FrameworkCoreActivator.logError("Error getting Java Type from selection", e); } return null; }
From source file:org.springsource.ide.eclipse.commons.frameworks.ui.internal.utils.SelectionUtils.java
License:Open Source License
private static IPackageFragment getPackageFragment(IJavaElement element) { return (IPackageFragment) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT); }
From source file:org.summer.dsl.model.ui.navigation.LinkToOriginDetector.java
License:Open Source License
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { try {/*from w w w . java 2 s . co m*/ // very pessimistic guards - most things should never happen ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class); if (textEditor == null) return null; IEditorInput editorInput = textEditor.getEditorInput(); if (editorInput == null) return null; IJavaElement adaptedJavaElement = (IJavaElement) Platform.getAdapterManager().getAdapter(editorInput, IJavaElement.class); if (adaptedJavaElement == null) return null; ICompilationUnit compilationUnit = (ICompilationUnit) adaptedJavaElement .getAncestor(IJavaElement.COMPILATION_UNIT); if (compilationUnit == null) return null; try { IRegion selectedWord = org.eclipse.jdt.internal.ui.text.JavaWordFinder .findWord(textViewer.getDocument(), region.getOffset()); // the actual implementation - find the referenced Java type under the cursor and link // to its origin if it's contained in a 'derived' resource IJavaElement[] javaElements = compilationUnit.codeSelect(selectedWord.getOffset(), selectedWord.getLength()); for (IJavaElement javaElement : javaElements) { /** * if IDE 3.8 is available the default 'Open Declaration' navigation will already open the original editor * So we don't need the additional hyperlinks. */ boolean provideHyperlinkOnReferences = !is_ORG_ECLIPSE_UI_IDE_3_8_Enabled() || compilationUnit.equals(((IMember) javaElement).getCompilationUnit()); if (javaElement instanceof IMember && provideHyperlinkOnReferences) { IMember selectedMember = (IMember) javaElement; IResource resource = selectedMember.getResource(); if (resource instanceof IFile) { ITrace traceToSource = traceInformation.getTraceToSource((IStorage) resource); if (traceToSource == null) { return null; } Iterable<ILocationInResource> sourceInformation = traceToSource .getAllAssociatedLocations( new TextRegion(selectedWord.getOffset(), selectedWord.getLength())); List<ILocationInResource> sourceInformationAsList = Lists .newArrayList(sourceInformation); if (!canShowMultipleHyperlinks && sourceInformationAsList.size() > 1) return null; List<LinkToOrigin> result = Lists .newArrayListWithCapacity(sourceInformationAsList.size()); for (ILocationInResource source : sourceInformationAsList) { try { URI resourceURI = source.getAbsoluteResourceURI(); if (resourceURI != null) { IResourceServiceProvider serviceProvider = serviceProviderRegistry .getResourceServiceProvider(resourceURI); if (serviceProvider == null) return null; LinkToOriginProvider provider = serviceProvider .get(LinkToOriginProvider.class); LinkToOrigin hyperlink = provider.createLinkToOrigin(source, selectedWord, selectedMember, compilationUnit, result); if (hyperlink != null) { result.add(hyperlink); } } } catch (IllegalArgumentException e) { /* invalid URI - ignore */ } } if (result.isEmpty()) return null; return result.toArray(new IHyperlink[result.size()]); } } } return null; } catch (JavaModelException e) { return null; } } catch (Throwable t) { return null; } }
From source file:org.switchyard.tools.ui.wizards.NewBeanServiceClassWizardPage.java
License:Open Source License
/** * @param selection the current selection *///w w w . j a va2 s .c o m public void init(IStructuredSelection selection) { IJavaElement elem = getInitialJavaElement(selection); ICompilationUnit cu = elem == null ? null : (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { elem = cu.findPrimaryType(); } initContainerPage(getInitialContainerElement(selection, elem)); initTypePage(elem); List<String> superInterfaces = getSuperInterfaces(); if (superInterfaces.size() > 0) { initServiceInterface(superInterfaces.get(0)); } setModifiers(Flags.AccPublic, false); _createTestClassButton.setSelection(false); doStatusUpdate(); }
From source file:org.switchyard.tools.ui.wizards.NewServiceTestClassWizardPage.java
License:Open Source License
/** * @param selection the current selection *//* ww w.j a v a 2 s . com*/ public void init(IStructuredSelection selection) { IJavaElement elem = getInitialJavaElement(selection); ICompilationUnit cu = elem == null ? null : (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { elem = cu.findPrimaryType(); } initContainerPage(getInitialContainerElement(selection, elem)); initTypePage(elem); // clear out super types setSuperClass("", true); //$NON-NLS-1$ setSuperInterfaces(Collections.<String>emptyList(), true); setModifiers(Flags.AccPublic, false); if (getTypeName().length() == 0) { String simpleServiceInterfaceName = getSimpleServiceInterfaceName(); if (simpleServiceInterfaceName.length() > 0) { _oldTypeName = createDefaultClassName(); setTypeName(_oldTypeName, true); } } if (_mavenProjectFacade != null) { IPath[] testSourcePaths = _mavenProjectFacade.getTestCompileSourceLocations(); if (testSourcePaths != null && testSourcePaths.length > 0) { setPackageFragmentRoot( getJavaProject().getPackageFragmentRoot(_project.getFolder(testSourcePaths[0])), true); } } setSuperInterfaces(Collections.<String>emptyList(), false); doStatusUpdate(); }
From source file:x10dt.ui.wizards.NewX10PackageWizardPage.java
License:Open Source License
/** * The wizard owning this page is responsible for calling this method with the current selection. The selection is used to * initialize the fields of the wizard page. * /*from ww w . j a v a 2s . c om*/ * @param selection * used to initialize the fields */ public void init(IStructuredSelection selection) { IJavaElement jelem = getInitialJavaElement(selection); initContainerPage(jelem); String pName = ""; //$NON-NLS-1$ if (jelem != null) { IPackageFragment pf = (IPackageFragment) jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT); if (pf != null && !pf.isDefaultPackage()) pName = pf.getElementName(); } setPackageText(pName, true); updateStatus(new IStatus[] { fContainerStatus, fPackageStatus }); }