Search in sources :

Example 6 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class XMLJavaHyperlinkDetector method isJspJavaContent.

private boolean isJspJavaContent(IDocument document, IRegion region) {
    JSPTranslation translation = null;
    IStructuredModel model = null;
    try {
        model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
        if (model instanceof IDOMModel) {
            IDOMModel xmlModel = (IDOMModel) model;
            if (xmlModel != null) {
                final IDOMDocument xmlDoc = xmlModel.getDocument();
                final JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
                if (adapter != null) {
                    translation = adapter.getJSPTranslation();
                    if (translation != null) {
                        int javaOffset = translation.getJavaOffset(region.getOffset());
                        if (javaOffset > -1) {
                            return true;
                        }
                    }
                }
            }
        }
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
    return false;
}
Also used : IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)

Example 7 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPJavaSelectionProvider method getSelection.

static IJavaElement[] getSelection(ITextEditor textEditor) {
    IJavaElement[] elements = null;
    IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    if (selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) selection;
        // get the JSP translation object for this editor's document
        IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
        try {
            if (model instanceof IDOMModel) {
                IDOMModel xmlModel = (IDOMModel) model;
                IDOMDocument xmlDoc = xmlModel.getDocument();
                JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
                if (adapter != null) {
                    JSPTranslation translation = adapter.getJSPTranslation();
                    elements = translation.getElementsFromJspRange(textSelection.getOffset(), textSelection.getOffset() + textSelection.getLength());
                }
            }
        } finally {
            if (model != null)
                model.releaseFromRead();
        }
    }
    if (elements == null) {
        elements = new IJavaElement[0];
    }
    return elements;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ISelection(org.eclipse.jface.viewers.ISelection) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslationAdapter(org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 8 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPTypeMoveRequestor method getRenameText.

/**
 * @see org.eclipse.jst.jsp.ui.internal.java.refactoring.BasicRefactorSearchRequestor#getRenameText(org.eclipse.jst.jsp.core.internal.java.search.JavaSearchDocumentDelegate, org.eclipse.jdt.core.search.SearchMatch)
 */
protected String getRenameText(JavaSearchDocumentDelegate searchDoc, SearchMatch javaMatch) {
    String renameText = getElement().getElementName();
    JSPTranslation trans = searchDoc.getJspTranslation();
    String matchText = trans.getJavaText().substring(javaMatch.getOffset(), javaMatch.getOffset() + javaMatch.getLength());
    // if it's an import or jsp:useBean, we need to add the package name as well
    if (trans.isImport(javaMatch.getOffset()) || trans.isUseBean(javaMatch.getOffset()) || isFullyQualified(matchText)) {
        if (// $NON-NLS-1$
        !getNewName().equals(""))
            // getNewName() is the pkg name
            // $NON-NLS-1$
            renameText = getNewName() + "." + renameText;
    }
    // if the rename text is the same as the match text then, don't want to bother renaming anything
    if (renameText.equals(matchText)) {
        renameText = null;
    }
    return renameText;
}
Also used : JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation)

Example 9 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPTypeRenameRequestor method getRenameText.

protected String getRenameText(JavaSearchDocumentDelegate searchDoc, SearchMatch javaMatch) {
    String renameText = getNewName();
    String pkg = getType().getPackageFragment().getElementName();
    IJavaElement parent = getType().getParent();
    String parentName = (parent != null) ? parent.getElementName() : null;
    JSPTranslation trans = searchDoc.getJspTranslation();
    String matchText = trans.getJavaText().substring(javaMatch.getOffset(), javaMatch.getOffset() + javaMatch.getLength());
    // else if starts with parent name such as "MyClass.Enum" then need to add the parent name as well
    if (trans.isImport(javaMatch.getOffset()) || isFullyQualified(matchText)) {
        if (// $NON-NLS-1$
        !pkg.equals(""))
            // $NON-NLS-1$
            renameText = pkg + "." + renameText;
    } else if (parentName != null && matchText.startsWith(parentName)) {
        // $NON-NLS-1$
        renameText = parentName + "." + renameText;
    }
    return renameText;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation)

Example 10 with JSPTranslation

use of org.eclipse.jst.jsp.core.internal.java.JSPTranslation in project webtools.sourceediting by eclipse.

the class JSPCompletionProcessor method computeCompletionProposals.

/**
 * The same as the normal <code>computeCompeltaionProposals</code> except the calculated
 * java position is offset by the given extra offset.
 *
 * @param viewer
 *            the viewer whose document is used to compute the proposals
 * @param documentPosition
 *            an offset within the document for which completions should
 *            be computed
 * @param javaPositionExtraOffset
 * 				the extra offset for the java position
 * @return an array of completion proposals or <code>null</code> if no
 *         proposals are possible
 */
protected ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int pos, int javaPositionExtraOffset) {
    initialize(pos);
    JSPProposalCollector collector = null;
    IDOMModel xmlModel = null;
    try {
        if (viewer instanceof StructuredTextViewer)
            fViewer = (StructuredTextViewer) viewer;
        xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(fViewer.getDocument());
        IDOMDocument xmlDoc = xmlModel.getDocument();
        if (fTranslationAdapter == null || xmlModel.getId() != fModelId) {
            fTranslationAdapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
            fModelId = xmlModel.getId();
        }
        if (fTranslationAdapter != null) {
            JSPTranslation translation = fTranslationAdapter.getJSPTranslation();
            fJavaPosition = translation.getJavaOffset(getDocumentPosition()) + javaPositionExtraOffset;
            if (DEBUG)
                System.out.println(debug(translation));
            try {
                ICompilationUnit cu = translation.getCompilationUnit();
                // or without a valid position
                if (cu == null || -1 == fJavaPosition)
                    return new ICompletionProposal[0];
                collector = getProposalCollector(cu, translation);
                synchronized (cu) {
                    cu.codeComplete(fJavaPosition, collector, (WorkingCopyOwner) null);
                }
            } catch (CoreException coreEx) {
                // a possible Java Model Exception due to not being a Web
                // (Java) Project
                coreEx.printStackTrace();
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
    // throw out exceptions on code assist.
    } finally {
        if (xmlModel != null) {
            xmlModel.releaseFromRead();
        }
    }
    ICompletionProposal[] results = new ICompletionProposal[0];
    if (collector != null) {
        results = collector.getJSPCompletionProposals();
        if (results == null || results.length < 1)
            fErrorMessage = JSPUIMessages.Java_Content_Assist_is_not_UI_;
    }
    return results;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJSPTranslation(org.eclipse.jst.jsp.core.internal.java.IJSPTranslation) JSPTranslation(org.eclipse.jst.jsp.core.internal.java.JSPTranslation) CoreException(org.eclipse.core.runtime.CoreException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) StructuredTextViewer(org.eclipse.wst.sse.ui.internal.StructuredTextViewer) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

JSPTranslation (org.eclipse.jst.jsp.core.internal.java.JSPTranslation)24 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)21 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)14 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)12 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)9 IFile (org.eclipse.core.resources.IFile)6 IJavaElement (org.eclipse.jdt.core.IJavaElement)6 IPath (org.eclipse.core.runtime.IPath)5 Path (org.eclipse.core.runtime.Path)5 DOMModelForJSP (org.eclipse.jst.jsp.core.internal.domdocument.DOMModelForJSP)4 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 CoreException (org.eclipse.core.runtime.CoreException)3 IDocument (org.eclipse.jface.text.IDocument)3 Position (org.eclipse.jface.text.Position)3 ArrayList (java.util.ArrayList)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2