Search in sources :

Example 1 with IImportContainer

use of org.eclipse.jdt.core.IImportContainer in project webtools.sourceediting by eclipse.

the class AddImportHandler method execute.

/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IEditorSite site = HandlerUtil.getActiveEditor(event).getEditorSite();
    final ISelectionProvider provider = site.getSelectionProvider();
    final ISelection selection = provider != null ? provider.getSelection() : null;
    if (selection instanceof IStructuredSelection && selection instanceof ITextSelection) {
        final IStructuredSelection structuredSelection = (IStructuredSelection) selection;
        final int offset = ((ITextSelection) selection).getOffset();
        final Object firstElement = structuredSelection.getFirstElement();
        if (firstElement instanceof IDOMNode) {
            final IDOMModel model = ((IDOMNode) firstElement).getModel();
            INodeAdapter adapter = model.getDocument().getAdapterFor(IJSPTranslation.class);
            if (adapter != null) {
                JSPTranslationAdapter translationAdapter = (JSPTranslationAdapter) model.getDocument().getAdapterFor(IJSPTranslation.class);
                final JSPTranslationExtension translation = translationAdapter.getJSPTranslation();
                translation.reconcileCompilationUnit();
                final ICompilationUnit cu = translation.getCompilationUnit();
                CompilationUnit astRoot = SharedASTProvider.getAST(cu, SharedASTProvider.WAIT_YES, null);
                if (astRoot != null) {
                    final ASTNode node = NodeFinder.perform(astRoot, translation.getJavaOffset(offset), 0);
                    if (node != null) {
                        SimpleName name = null;
                        if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
                            name = (SimpleName) node;
                        } else if (node.getNodeType() == ASTNode.QUALIFIED_NAME) {
                            name = ((QualifiedName) node).getName();
                        }
                        if (name != null) {
                            IBinding binding = name.resolveBinding();
                            if (binding instanceof ITypeBinding && (binding.getJavaElement() == null || !binding.getJavaElement().exists())) {
                                // Look it up!
                                ITypeBinding typeBinding = (ITypeBinding) binding;
                                final IImportContainer importContainer = cu.getImportContainer();
                                if (!importContainer.getImport(typeBinding.getQualifiedName()).exists()) {
                                    final List typesFound = new ArrayList();
                                    final TypeNameMatchRequestor collector = new TypeNameMatcher(typesFound);
                                    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { cu.getJavaProject() });
                                    final int mode = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
                                    try {
                                        new SearchEngine().searchAllTypeNames(null, mode, name.getIdentifier().toCharArray(), mode, IJavaSearchConstants.CLASS_AND_INTERFACE, scope, collector, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
                                        final int length = typesFound.size();
                                        final List elements = new ArrayList();
                                        for (int i = 0; i < length; i++) {
                                            final TypeNameMatch match = (TypeNameMatch) typesFound.get(i);
                                            final int modifiers = match.getModifiers();
                                            if (!Flags.isPrivate(modifiers) && !Flags.isPackageDefault(modifiers)) {
                                                elements.add(match);
                                            }
                                        }
                                        TypeNameMatch match = null;
                                        // If there's only one match, insert it; otherwise, open the dialog to choose from the list
                                        if (elements.size() == 1) {
                                            match = (TypeNameMatch) elements.get(0);
                                        } else if (elements.size() > 1) {
                                            ElementListSelectionDialog dialog = new ElementListSelectionDialog(site.getShell(), LABEL_PROVIDER);
                                            dialog.setElements(elements.toArray(new TypeNameMatch[elements.size()]));
                                            dialog.setTitle(JSPUIMessages.AddImportHandler_title);
                                            dialog.setMessage(JSPUIMessages.AddImportHandler_label);
                                            if (dialog.open() == Window.OK) {
                                                final Object result = dialog.getFirstResult();
                                                if (result instanceof TypeNameMatch) {
                                                    match = (TypeNameMatch) result;
                                                }
                                            }
                                        }
                                        addImport(match, model.getStructuredDocument());
                                    } catch (JavaModelException e) {
                                        // $NON-NLS-1$
                                        Logger.logException("Exception while determining import.", e);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : INodeAdapter(org.eclipse.wst.sse.core.internal.provisional.INodeAdapter) JavaModelException(org.eclipse.jdt.core.JavaModelException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) JSPTranslationExtension(org.eclipse.jst.jsp.core.internal.java.JSPTranslationExtension) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) ElementListSelectionDialog(org.eclipse.ui.dialogs.ElementListSelectionDialog) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IImportContainer(org.eclipse.jdt.core.IImportContainer) ISelection(org.eclipse.jface.viewers.ISelection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) ITextSelection(org.eclipse.jface.text.ITextSelection) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) IEditorSite(org.eclipse.ui.IEditorSite)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IImportContainer (org.eclipse.jdt.core.IImportContainer)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)1 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)1 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)1 TypeNameMatchRequestor (org.eclipse.jdt.core.search.TypeNameMatchRequestor)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 ISelection (org.eclipse.jface.viewers.ISelection)1 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)1