Example usage for org.eclipse.jdt.core IImportContainer getImport

List of usage examples for org.eclipse.jdt.core IImportContainer getImport

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IImportContainer getImport.

Prototype

IImportDeclaration getImport(String name);

Source Link

Document

Returns the first import declaration in this import container with the given name.

Usage

From source file:org.eclipse.jst.jsp.ui.internal.handlers.AddImportHandler.java

License:Open Source License

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();
                        }/*from   ww w .ja  v  a  2  s. c om*/
                        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) {
                                        Logger.logException("Exception while determining import.", e); //$NON-NLS-1$
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.ws.jaxrs.ui.internal.text.BasicCompletionProposal.java

License:Open Source License

public void apply(IDocument document, char trigger, int offset) {
    try {//from  w ww.j  av  a  2 s.  com
        document.replace(replacementOffset, replacementLength, replacementString);
        final IImportContainer importContainer = compilationUnit.getImportContainer();
        if (this.additionalImport != null && !importContainer.getImport(additionalImport).exists()) {
            final ISourceRange importContainerRange = importContainer.getSourceRange();
            document.replace(importContainerRange.getOffset() + importContainerRange.getLength(), 0,
                    document.getLineDelimiter(0) + "import " + additionalImport + ";");
        }
    } catch (BadLocationException e) {
        Logger.warn("Failed to replace document content with selected proposal", e);
    } catch (JavaModelException e) {
        Logger.warn("Failed to replace document content with selected proposal", e);
    }
}