List of usage examples for org.eclipse.jdt.core.search SearchEngine createJavaSearchScope
public static IJavaSearchScope createJavaSearchScope(IJavaElement[] elements)
From source file:at.bestsolution.efxclipse.tooling.fxml.editors.FXMLCompletionProposalComputer.java
License:Open Source License
private void createSubtypeProposals(final ContentAssistRequest contentAssistRequest, final CompletionProposalInvocationContext context, IType superType) { IJavaProject jproject = findProject(contentAssistRequest); try {/* w ww . j a v a 2 s. c om*/ IJavaSearchScope hierarchyScope = SearchEngine.createHierarchyScope(superType); IJavaSearchScope projectScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { jproject }); IJavaSearchScope scope = new IntersectingJavaSearchScope(projectScope, hierarchyScope); SearchEngine searchEngine = new SearchEngine(); char[] typeName = null; char[] packageName = null; if (!contentAssistRequest.getMatchString().isEmpty()) { typeName = contentAssistRequest.getMatchString().toCharArray(); } searchEngine.searchAllTypeNames(packageName, SearchPattern.R_PATTERN_MATCH, typeName, SearchPattern.R_PREFIX_MATCH | SearchPattern.R_CAMELCASE_MATCH, IJavaSearchConstants.CLASS, scope, new TypeNameRequestor() { @Override public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { String sPackageName = new String(packageName); int priority = PRIORITY_LOWER_1; if (sPackageName.startsWith("com.sun.javafx")) { priority -= 10; } StyledString s = new StyledString(new String(simpleTypeName)); s.append(" - " + sPackageName, StyledString.QUALIFIER_STYLER); FXMLCompletionProposal prop = createElementProposal(contentAssistRequest, context, sPackageName + "." + new String(simpleTypeName) + " ", s, false, priority, IconKeys.getIcon(IconKeys.CLASS_KEY), FQN_MATCHER); if (prop != null) { prop.setMatcher(FQN_MATCHER); prop.setTextApplier(new IReplacementTextApplier() { @Override public void apply(IDocument document, ConfigurableCompletionProposal proposal) throws BadLocationException { String proposalReplacementString = proposal.getReplacementString(); List<String> s = getImportedTypes(contentAssistRequest); String shortened = proposalReplacementString .substring(proposalReplacementString.lastIndexOf('.') + 1); if (s.contains(proposalReplacementString.trim())) { proposal.setCursorPosition(shortened.length()); document.replace(proposal.getReplacementOffset(), proposal.getReplacementLength(), shortened); } else { DocumentRewriteSession rewriteSession = null; try { if (document instanceof IDocumentExtension4) { rewriteSession = ((IDocumentExtension4) document) .startRewriteSession( DocumentRewriteSessionType.UNRESTRICTED_SMALL); } } finally { if (rewriteSession != null) { ((IDocumentExtension4) document) .stopRewriteSession(rewriteSession); } // if (viewerExtension != null) // viewerExtension.setRedraw(true); } boolean startWithLineBreak = true; boolean endWithLineBreak = false; Document xmlDoc = contentAssistRequest.getNode().getOwnerDocument(); NodeList list = xmlDoc.getChildNodes(); int offset = 0; List<Node> prs = new ArrayList<Node>(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { prs.add(n); } } if (!prs.isEmpty()) { offset = ((IDOMNode) prs.get(prs.size() - 1)).getEndOffset(); } offset = Math.min(proposal.getReplacementOffset(), offset); proposal.setCursorPosition(shortened.length()); document.replace(proposal.getReplacementOffset(), proposal.getReplacementLength(), shortened); String importStatement = (startWithLineBreak ? "\n<?import " : "import ") + proposalReplacementString.trim(); importStatement += "?>"; if (endWithLineBreak) importStatement += "\n\n"; document.replace(offset, 0, importStatement.toString()); proposal.setCursorPosition( proposal.getCursorPosition() + importStatement.length()); } } }); contentAssistRequest.addProposal(prop); } } }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor()); } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:at.bestsolution.efxclipse.tooling.fxml.editors.FXMLCompletionProposalComputer.java
License:Open Source License
@Override protected void addAttributeValueProposals(final ContentAssistRequest contentAssistRequest, final CompletionProposalInvocationContext context) { IDOMNode n = (IDOMNode) contentAssistRequest.getNode(); if (Character.isUpperCase(n.getNodeName().charAt(0))) { NamedNodeMap m = n.getAttributes(); IDOMNode attribute = null;//from www. j av a 2 s. c om for (int i = 0; i < m.getLength(); i++) { IDOMNode a = (IDOMNode) m.item(i); if (a.contains(contentAssistRequest.getStartOffset())) { attribute = a; } } if (attribute != null) { if ("http://javafx.com/fxml".equals(attribute.getNamespaceURI())) { if ("constant".equals(attribute.getLocalName())) { IType type = findType(n.getNodeName(), contentAssistRequest, context); if (type != null) { try { List<IField> fields = new ArrayList<IField>(); collectStaticFields(fields, type); for (IField f : fields) { StyledString s = new StyledString(f.getElementName() + " : " + Signature.getSimpleName(Signature.toString(f.getTypeSignature()))); String owner = ((IType) f.getAncestor(IJavaElement.TYPE)).getElementName(); s.append(" - " + Signature.getSimpleName(owner), StyledString.QUALIFIER_STYLER); FXMLCompletionProposal cp = createProposal(contentAssistRequest, context, "\"" + f.getElementName(), s, IconKeys.getIcon(IconKeys.CLASS_KEY), CLASS_ATTRIBUTE_MATCHER); if (cp != null) { contentAssistRequest.addProposal(cp); } } } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else if ("controller".equals(attribute.getLocalName())) { IJavaProject jproject = findProject(contentAssistRequest); char[] typeName = null; char[] packageName = null; if (!contentAssistRequest.getMatchString().isEmpty()) { if (contentAssistRequest.getMatchString().startsWith("\"")) { typeName = contentAssistRequest.getMatchString().substring(1).toCharArray(); } else { typeName = contentAssistRequest.getMatchString().toCharArray(); } } IJavaSearchScope searchScope = SearchEngine .createJavaSearchScope(new IJavaElement[] { jproject }); SearchEngine searchEngine = new SearchEngine(); try { searchEngine.searchAllTypeNames(packageName, SearchPattern.R_PATTERN_MATCH, typeName, SearchPattern.R_PREFIX_MATCH | SearchPattern.R_CAMELCASE_MATCH, IJavaSearchConstants.TYPE, searchScope, new TypeNameRequestor() { public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { String sPackageName = new String(packageName); int priority = PRIORITY_LOWER_1; if (sPackageName.startsWith("com.sun")) { priority -= 10; } StyledString s = new StyledString(new String(simpleTypeName)); s.append(" - " + sPackageName, StyledString.QUALIFIER_STYLER); FXMLCompletionProposal cp = createProposal(contentAssistRequest, context, "\"" + sPackageName + "." + new String(simpleTypeName), s, IconKeys.getIcon(IconKeys.CLASS_KEY), CLASS_ATTRIBUTE_MATCHER); if (cp != null) { contentAssistRequest.addProposal(cp); } } }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor()); } catch (JavaModelException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else if (attribute.getNodeName().contains(".")) { String[] parts = attribute.getNodeName().split("\\."); IType type = findType(parts[0], contentAssistRequest, context); if (type != null) { IFXClass fxClass = FXPlugin.getClassmodel().findClass(type.getJavaProject(), type); if (fxClass != null) { IFXProperty p = fxClass.getStaticProperty(parts[1]); if (p instanceof IFXPrimitiveProperty) { createAttributeValuePrimitiveProposals(contentAssistRequest, context, (IFXPrimitiveProperty) p); } else if (p instanceof IFXEnumProperty) { createAttributeValueEnumProposals(contentAssistRequest, context, (IFXEnumProperty) p); } else if (p instanceof IFXObjectProperty) { createAttributeValueObjectProposals(contentAssistRequest, context, (IFXObjectProperty) p); } } } } else { IType type = findType(n.getNodeName(), contentAssistRequest, context); if (type != null) { IFXClass fxClass = FXPlugin.getClassmodel().findClass(type.getJavaProject(), type); if (fxClass != null) { IFXProperty p = fxClass.getProperty(attribute.getNodeName()); if (p instanceof IFXPrimitiveProperty) { createAttributeValuePrimitiveProposals(contentAssistRequest, context, (IFXPrimitiveProperty) p); } else if (p instanceof IFXEnumProperty) { createAttributeValueEnumProposals(contentAssistRequest, context, (IFXEnumProperty) p); } else if (p instanceof IFXObjectProperty) { createAttributeValueObjectProposals(contentAssistRequest, context, (IFXObjectProperty) p); } else if (p instanceof IFXEventHandlerProperty) { createAttributeValueEventHandlerProposals(contentAssistRequest, context, (IFXEventHandlerProperty) p); } } } } } } }
From source file:bndtools.editor.components.ComponentNameProposalProvider.java
License:Open Source License
@Override protected List<IContentProposal> doGenerateProposals(String contents, int position) { final String prefix = contents.substring(0, position); IJavaProject javaProject = searchContext.getJavaProject(); final List<IContentProposal> result = new ArrayList<IContentProposal>(100); // Resource matches IProject project = javaProject.getProject(); try {/* w ww. ja v a 2s .c o m*/ project.accept(new IResourceProxyVisitor() { public boolean visit(IResourceProxy proxy) throws CoreException { if (proxy.getType() == IResource.FILE) { if (proxy.getName().toLowerCase().startsWith(prefix) && proxy.getName().toLowerCase().endsWith(XML_SUFFIX)) { result.add(new ResourceProposal(proxy.requestResource())); } return false; } // Recurse into everything else return true; } }, 0); } catch (CoreException e) { Plugin.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error searching for resources.", e)); } // Class matches final IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject }); final TypeNameRequestor requestor = new TypeNameRequestor() { @Override public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { if (!Flags.isAbstract(modifiers) && (Flags.isPublic(modifiers) || Flags.isProtected(modifiers))) { result.add(new JavaContentProposal(new String(packageName), new String(simpleTypeName), false)); } }; }; final IRunnableWithProgress runnable = new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try { new SearchEngine().searchAllTypeNames(null, 0, prefix.toCharArray(), SearchPattern.R_PREFIX_MATCH, IJavaSearchConstants.CLASS, scope, requestor, IJavaSearchConstants.CANCEL_IF_NOT_READY_TO_SEARCH, monitor); } catch (JavaModelException e) { throw new InvocationTargetException(e); } } }; IRunnableContext runContext = searchContext.getRunContext(); try { if (runContext != null) { runContext.run(false, false, runnable); } else { runnable.run(new NullProgressMonitor()); } } catch (InvocationTargetException e) { Plugin.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error searching for classes.", e.getTargetException())); } catch (InterruptedException e) { // Reset the interruption status and continue Thread.currentThread().interrupt(); } return result; }
From source file:bndtools.editor.components.SvcInterfaceProposalProvider.java
License:Open Source License
@Override protected List<IContentProposal> doGenerateProposals(String contents, int position) { final String prefix = contents.substring(0, position); final IJavaSearchScope scope = SearchEngine .createJavaSearchScope(new IJavaElement[] { searchContext.getJavaProject() }); final ArrayList<IContentProposal> result = new ArrayList<IContentProposal>(100); final TypeNameRequestor typeNameRequestor = new TypeNameRequestor() { @Override//from ww w .j a v a2 s . com public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) { boolean isInterface = Flags.isInterface(modifiers); result.add( new JavaContentProposal(new String(packageName), new String(simpleTypeName), isInterface)); } }; IRunnableWithProgress runnable = new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try { new SearchEngine().searchAllTypeNames(null, 0, prefix.toCharArray(), SearchPattern.R_PREFIX_MATCH, IJavaSearchConstants.CLASS_AND_INTERFACE, scope, typeNameRequestor, IJavaSearchConstants.CANCEL_IF_NOT_READY_TO_SEARCH, monitor); } catch (JavaModelException e) { throw new InvocationTargetException(e); } } }; try { if (searchContext.getRunContext() == null) { runnable.run(new NullProgressMonitor()); } else { searchContext.getRunContext().run(false, false, runnable); } } catch (InvocationTargetException e) { Plugin.log(new Status(IStatus.ERROR, Plugin.PLUGIN_ID, 0, "Error searching for Java types.", e.getTargetException())); return Collections.emptyList(); } catch (InterruptedException e) { // Reset interrupted status and return empty Thread.currentThread().interrupt(); return Collections.emptyList(); } return result; }
From source file:bndtools.editor.contents.ExportPatternsListPart.java
License:Open Source License
protected List<ExportedPackage> selectPackagesToAdd() { List<ExportedPackage> added = null; final IPackageFilter filter = new IPackageFilter() { public boolean select(String packageName) { if (packageName.equals("java") || packageName.startsWith("java.")) return false; // TODO: check already included patterns return true; }//from w w w . j av a2s. c o m }; IFormPage page = (IFormPage) getManagedForm().getContainer(); IWorkbenchWindow window = page.getEditorSite().getWorkbenchWindow(); // Prepare the package lister from the Java project IProject project = ResourceUtil.getResource(page.getEditorInput()).getProject(); IJavaProject javaProject = JavaCore.create(project); IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject }); JavaSearchScopePackageLister packageLister = new JavaSearchScopePackageLister(searchScope, window); // Create and open the dialog PackageSelectionDialog dialog = new PackageSelectionDialog(window.getShell(), packageLister, filter, "Select new packages to export from the bundle."); dialog.setSourceOnly(true); dialog.setMultipleSelection(true); if (dialog.open() == Window.OK) { Object[] results = dialog.getResult(); added = new LinkedList<ExportedPackage>(); // Select the results for (Object result : results) { String newPackageName = (String) result; ExportedPackage newPackage = new ExportedPackage(newPackageName, new HashMap<String, String>()); added.add(newPackage); } } return added; }
From source file:bndtools.editor.contents.PrivatePackagesPart.java
License:Open Source License
private void doAddPackages() { // Prepare the exclusion list based on existing private packages final Set<String> packageNameSet = new HashSet<String>(packages); // Create a filter from the exclusion list and packages matching // "java.*", which must not be included in a bundle IPackageFilter filter = new IPackageFilter() { @Override//from w ww . j ava 2 s .c o m public boolean select(String packageName) { return !packageName.equals("java") && !packageName.startsWith("java.") && !packageNameSet.contains(packageName); } }; IFormPage page = (IFormPage) getManagedForm().getContainer(); IWorkbenchWindow window = page.getEditorSite().getWorkbenchWindow(); // Prepare the package lister from the Java project IJavaProject javaProject = getJavaProject(); if (javaProject == null) { MessageDialog.openError(getSection().getShell(), "Error", "Cannot add packages: unable to find a Java project associated with the editor input."); return; } IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject }); JavaSearchScopePackageLister packageLister = new JavaSearchScopePackageLister(searchScope, window); // Create and open the dialog PackageSelectionDialog dialog = new PackageSelectionDialog(getSection().getShell(), packageLister, filter, "Select new packages to include in the bundle."); dialog.setSourceOnly(true); dialog.setMultipleSelection(true); if (dialog.open() == Window.OK) { Object[] results = dialog.getResult(); List<String> added = new LinkedList<String>(); // Select the results for (Object result : results) { String newPackageName = (String) result; if (packages.add(newPackageName)) { added.add(newPackageName); } } // Update the model and view if (!added.isEmpty()) { viewer.add(added.toArray()); markDirty(); } } }
From source file:bndtools.editor.exports.ExportPatternsListPart.java
License:Open Source License
protected List<ExportedPackage> selectPackagesToAdd() { List<ExportedPackage> added = null; final IPackageFilter filter = new IPackageFilter() { @Override//from w w w .j ava2s . c o m public boolean select(String packageName) { if (packageName.equals("java") || packageName.startsWith("java.")) return false; // TODO: check already included patterns return true; } }; IFormPage page = (IFormPage) getManagedForm().getContainer(); IWorkbenchWindow window = page.getEditorSite().getWorkbenchWindow(); // Prepare the package lister from the Java project IProject project = ResourceUtil.getResource(page.getEditorInput()).getProject(); IJavaProject javaProject = JavaCore.create(project); IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject }); JavaSearchScopePackageLister packageLister = new JavaSearchScopePackageLister(searchScope, window); // Create and open the dialog PackageSelectionDialog dialog = new PackageSelectionDialog(window.getShell(), packageLister, filter, "Select new packages to export from the bundle."); dialog.setSourceOnly(true); dialog.setMultipleSelection(true); if (dialog.open() == Window.OK) { Object[] results = dialog.getResult(); added = new LinkedList<ExportedPackage>(); // Select the results for (Object result : results) { String newPackageName = (String) result; ExportedPackage newPackage = new ExportedPackage(newPackageName, new Attrs()); added.add(newPackage); } } return added; }
From source file:bndtools.editor.pkgpatterns.PkgPatternsProposalProvider.java
License:Open Source License
@Override protected Collection<? extends IContentProposal> doGenerateProposals(String contents, int position) { String prefix = contents.substring(0, position); final int replaceFromPos; if (prefix.startsWith("!")) { //$NON-NLS-1$ prefix = prefix.substring(1);/*from w w w. j av a2 s . c o m*/ replaceFromPos = 1; } else { replaceFromPos = 0; } Comparator<PkgPatternProposal> comparator = new Comparator<PkgPatternProposal>() { @Override public int compare(PkgPatternProposal o1, PkgPatternProposal o2) { int result = o1.getPackageFragment().getElementName() .compareTo(o2.getPackageFragment().getElementName()); if (result == 0) { result = Boolean.valueOf(o1.isWildcard()).compareTo(Boolean.valueOf(o2.isWildcard())); } return result; } }; final TreeSet<PkgPatternProposal> result = new TreeSet<PkgPatternProposal>(comparator); final IJavaSearchScope scope = SearchEngine .createJavaSearchScope(new IJavaElement[] { searchContext.getJavaProject() }); final SearchPattern pattern = SearchPattern.createPattern("*" + prefix + "*", IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH); final SearchRequestor requestor = new SearchRequestor() { @Override public void acceptSearchMatch(SearchMatch match) throws CoreException { IPackageFragment pkg = (IPackageFragment) match.getElement(); // Reject the default package and any package starting with // "java." since these cannot be imported if (pkg.isDefaultPackage() || pkg.getElementName().startsWith("java.")) return; result.add(new PkgPatternProposal(pkg, false, replaceFromPos)); result.add(new PkgPatternProposal(pkg, true, replaceFromPos)); } }; IRunnableWithProgress runnable = new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try { new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, monitor); } catch (CoreException e) { throw new InvocationTargetException(e); } } }; try { IRunnableContext runContext = searchContext.getRunContext(); if (runContext != null) { runContext.run(false, false, runnable); } else { runnable.run(new NullProgressMonitor()); } return result; } catch (InvocationTargetException e) { logger.logError("Error searching for packages.", e); return Collections.emptyList(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return Collections.emptyList(); } }
From source file:ca.ubc.cs.ferret.tests.support.TestProject.java
License:Open Source License
public void waitForIndexer() throws JavaModelException { new SearchEngine().searchAllTypeNames(null, null, SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.CLASS, SearchEngine.createJavaSearchScope(new IJavaElement[0]), new TypeNameRequestor() { // nothing needs to be done here...we accept everything }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null); }
From source file:ca.uvic.chisel.javasketch.utils.JavaSketchUtilities.java
License:Open Source License
/** * Attempts to create a Java search scope for the given configuration, if possible. Otherwise, returns null. * @param cf the configuration to create the scope from. * @return the new search scope, or null if none could be created. *///from w w w . jav a 2 s . c o m public static IJavaSearchScope createJavaSearchScope(IProgramSketch sketch) { //get the launch type from the filters first String launchType = sketch.getFilterSettings().getLaunchType(); if (LaunchConfigurationUtilities.ECLIPSE_LAUNCH_TYPE.equals(launchType)) { return createPluginSearchScope(); } IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); ArrayList<IJavaProject> javaProjects = new ArrayList<IJavaProject>(); for (IProject project : projects) { try { if (project.isOpen() && project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) { javaProjects.add(JavaCore.create(project)); } } catch (CoreException e) { //do nothing, just ignore } } if (projects.length > 0) { return SearchEngine.createJavaSearchScope(javaProjects.toArray(new IJavaProject[javaProjects.size()])); } return null; }