Search in sources :

Example 1 with IJavaScriptElement

use of org.eclipse.wst.jsdt.core.IJavaScriptElement in project webtools.sourceediting by eclipse.

the class JsQueryParticipant method search.

/**
 * @see org.eclipse.wst.jsdt.ui.search.IQueryParticipant#search(org.eclipse.wst.jsdt.ui.search.ISearchRequestor,
 *      org.eclipse.wst.jsdt.ui.search.QuerySpecification,
 *      org.eclipse.core.runtime.IProgressMonitor)
 */
public void search(ISearchRequestor requestor, QuerySpecification querySpecification, IProgressMonitor monitor) throws CoreException {
    // do search based on the particular Java query
    if (querySpecification instanceof ElementQuerySpecification) {
        // element search (eg. from global find references in Java file)
        ElementQuerySpecification elementQuery = (ElementQuerySpecification) querySpecification;
        IJavaScriptElement element = elementQuery.getElement();
        if (JsQueryParticipant.DEBUG) {
            // $NON-NLS-1$
            System.out.println("JSP Query Participant searching on ELEMENT: " + element);
        }
        SearchRequestor jspRequestor = new JsSearchRequestor(requestor);
        // pa_TODO need to adapt JavaSearchScope to a JSPSearchScope
        JsSearchSupport.getInstance().search(element, new JsSearchScope(), jspRequestor);
    } else if (querySpecification instanceof PatternQuerySpecification) {
        // pattern search (eg. from Java search page)
        PatternQuerySpecification patternQuery = (PatternQuerySpecification) querySpecification;
        String pattern = patternQuery.getPattern();
        if (JsQueryParticipant.DEBUG) {
            // $NON-NLS-1$
            System.out.println("JSP Query Participant searching on PATTERN: " + pattern);
        }
        SearchRequestor jspRequestor = new JsSearchRequestor(requestor);
        JsSearchSupport.getInstance().search(pattern, new JsSearchScope(), patternQuery.getSearchFor(), patternQuery.getLimitTo(), SearchPattern.R_PATTERN_MATCH, false, jspRequestor);
    }
}
Also used : SearchRequestor(org.eclipse.wst.jsdt.core.search.SearchRequestor) JsSearchRequestor(org.eclipse.wst.jsdt.web.ui.internal.java.search.JsSearchRequestor) ISearchRequestor(org.eclipse.wst.jsdt.ui.search.ISearchRequestor) JsSearchScope(org.eclipse.wst.jsdt.web.core.javascript.search.JsSearchScope) ElementQuerySpecification(org.eclipse.wst.jsdt.ui.search.ElementQuerySpecification) PatternQuerySpecification(org.eclipse.wst.jsdt.ui.search.PatternQuerySpecification) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) JsSearchRequestor(org.eclipse.wst.jsdt.web.ui.internal.java.search.JsSearchRequestor)

Example 2 with IJavaScriptElement

use of org.eclipse.wst.jsdt.core.IJavaScriptElement in project webtools.sourceediting by eclipse.

the class JSDTHyperlinkDetector method createHyperlink.

private IHyperlink createHyperlink(IJsTranslation jsTranslation, IJavaScriptElement element, IRegion region, IDocument document) {
    IHyperlink link = null;
    if (region != null) {
        // open local variable in the JSP file...
        if (element instanceof ISourceReference) {
            IFile file = null;
            IPath outsidePath = null;
            int jspOffset = 0;
            IStructuredModel sModel = null;
            // try to locate the file in the workspace
            try {
                sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
                if (sModel != null) {
                    // URIResolver resolver = sModel.getResolver();
                    // if (resolver != null) {
                    // String uriString = resolver.getFileBaseLocation();
                    String uriString = sModel.getBaseLocation();
                    file = getFile(uriString);
                // }
                }
            } finally {
                if (sModel != null) {
                    sModel.releaseFromRead();
                }
            }
            // get Java range, translate coordinate to JSP
            try {
                ISourceRange range = null;
                if (jsTranslation != null) {
                    // link to local variable definitions
                    if (element instanceof ILocalVariable) {
                        range = ((ILocalVariable) element).getNameRange();
                        IJavaScriptElement unit = ((ILocalVariable) element).getParent();
                        IJavaScriptUnit myUnit = jsTranslation.getCompilationUnit();
                        while (!(unit instanceof IJavaScriptUnit || unit instanceof IClassFile || unit == null)) {
                            unit = ((JavaElement) unit).getParent();
                        }
                        if (unit instanceof IJavaScriptUnit) {
                            IJavaScriptUnit cu = (IJavaScriptUnit) unit;
                            if (cu != myUnit) {
                                file = getFile(cu.getPath().toString());
                                if (file == null) {
                                    outsidePath = cu.getPath();
                                }
                            }
                        } else if (unit instanceof IClassFile) {
                            IClassFile cu = (IClassFile) unit;
                            if (cu != myUnit) {
                                file = getFile(cu.getPath().toString());
                                if (file == null) {
                                    outsidePath = cu.getPath();
                                }
                            }
                        }
                    } else // linking to fields of the same compilation unit
                    if (element.getElementType() == IJavaScriptElement.FIELD) {
                        Object cu = ((IField) element).getJavaScriptUnit();
                        if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                        }
                    } else // linking to methods of the same compilation unit
                    if (element.getElementType() == IJavaScriptElement.METHOD) {
                        Object cu = ((IFunction) element).getJavaScriptUnit();
                        if (cu != null && cu.equals(jsTranslation.getCompilationUnit())) {
                            range = ((ISourceReference) element).getSourceRange();
                        }
                    }
                }
                if (range != null && file != null) {
                    jspOffset = range.getOffset();
                    if (jspOffset >= 0) {
                        link = new WorkspaceFileHyperlink(region, file, new Region(jspOffset, range.getLength()));
                    }
                } else if (range != null && outsidePath != null) {
                    jspOffset = range.getOffset();
                    if (jspOffset >= 0) {
                        link = new ExternalFileHyperlink(region, outsidePath.toFile());
                    }
                }
            } catch (JavaScriptModelException jme) {
                Logger.log(Logger.WARNING_DEBUG, jme.getMessage(), jme);
            }
        }
        if (link == null) {
            link = new JSDTHyperlink(region, element);
        }
    }
    return link;
}
Also used : IFile(org.eclipse.core.resources.IFile) IClassFile(org.eclipse.wst.jsdt.core.IClassFile) IPath(org.eclipse.core.runtime.IPath) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) IFunction(org.eclipse.wst.jsdt.core.IFunction) ILocalVariable(org.eclipse.wst.jsdt.core.ILocalVariable) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJavaScriptUnit(org.eclipse.wst.jsdt.core.IJavaScriptUnit) ISourceReference(org.eclipse.wst.jsdt.core.ISourceReference) ISourceRange(org.eclipse.wst.jsdt.core.ISourceRange)

Example 3 with IJavaScriptElement

use of org.eclipse.wst.jsdt.core.IJavaScriptElement in project webtools.sourceediting by eclipse.

the class JSPJavaSelectionProvider method getSelection.

static IJavaScriptElement[] getSelection(ITextEditor textEditor) {
    IJavaScriptElement[] 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 = null;
        try {
            model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
            if (model instanceof IDOMModel) {
                IDOMModel xmlModel = (IDOMModel) model;
                IDOMDocument xmlDoc = xmlModel.getDocument();
                JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
                if (adapter != null) {
                    IJsTranslation translation = adapter.getJsTranslation(true);
                    elements = translation.getElementsFromJsRange(translation.getJavaScriptOffset(textSelection.getOffset()), translation.getJavaScriptOffset(textSelection.getOffset() + textSelection.getLength()));
                }
            }
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    if (elements == null) {
        elements = new IJavaScriptElement[0];
    }
    return elements;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) 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) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 4 with IJavaScriptElement

use of org.eclipse.wst.jsdt.core.IJavaScriptElement in project webtools.sourceediting by eclipse.

the class ShowHistoryAction method selectionChanged.

public void selectionChanged(IAction action, ISelection selection) {
    setSelection(selection);
    IJavaScriptElement[] elements = JsElementActionProxy.getJsElementsFromSelection(getCurrentSelection());
    for (int i = 0; i < elements.length; i++) {
        if (elements[i].isVirtual()) {
            IResource resource = getHostResource(elements[i]);
            if (resource == null || !resource.exists()) {
                action.setEnabled(false);
            }
        }
    }
}
Also used : IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) IResource(org.eclipse.core.resources.IResource)

Example 5 with IJavaScriptElement

use of org.eclipse.wst.jsdt.core.IJavaScriptElement in project webtools.sourceediting by eclipse.

the class ShowInScriptExplorerAction method selectionChanged.

public void selectionChanged(IAction action, ISelection selection) {
    setSelection(selection);
    IJavaScriptElement[] elements = JsElementActionProxy.getJsElementsFromSelection(getCurrentSelection());
    for (int i = 0; i < elements.length; i++) {
        if (elements[i].isVirtual()) {
            IResource resource = getHostResource(elements[i]);
            if (resource == null || !resource.exists()) {
                action.setEnabled(false);
            }
        }
    }
}
Also used : IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) IResource(org.eclipse.core.resources.IResource)

Aggregations

IJavaScriptElement (org.eclipse.wst.jsdt.core.IJavaScriptElement)22 IResource (org.eclipse.core.resources.IResource)6 JavaScriptModelException (org.eclipse.wst.jsdt.core.JavaScriptModelException)6 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)6 ISelection (org.eclipse.jface.viewers.ISelection)4 IMember (org.eclipse.wst.jsdt.core.IMember)4 ISourceRange (org.eclipse.wst.jsdt.core.ISourceRange)4 ArrayList (java.util.ArrayList)3 ITypedRegion (org.eclipse.jface.text.ITypedRegion)3 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)3 ISetSelectionTarget (org.eclipse.ui.part.ISetSelectionTarget)3 IJavaScriptUnit (org.eclipse.wst.jsdt.core.IJavaScriptUnit)3 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)3 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)3 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)3 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)3 List (java.util.List)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IDocument (org.eclipse.jface.text.IDocument)2 IRegion (org.eclipse.jface.text.IRegion)2