Search in sources :

Example 16 with IJavaScriptElement

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

the class ShowInNavigatorAction 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 17 with IJavaScriptElement

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

the class JSDTHoverProcessor method getHoverInfo.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer,
	 *      org.eclipse.jface.text.IRegion)
	 */
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
    // get JSP translation object for this viewer's document
    IDOMModel xmlModel = null;
    try {
        xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getExistingModelForRead(textViewer.getDocument());
        if (xmlModel != null) {
            IDOMDocument xmlDoc = xmlModel.getDocument();
            JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
            if (adapter != null) {
                try {
                    boolean proceed = false;
                    ITypedRegion[] partitions = xmlDoc.getStructuredDocument().computePartitioning(hoverRegion.getOffset(), hoverRegion.getLength());
                    for (int i = 0; i < partitions.length; i++) {
                        for (int j = 0; j < PARTITION_TYPES.length; j++) {
                            if (PARTITION_TYPES[j].equals(partitions[i].getType())) {
                                proceed = true;
                                break;
                            }
                        }
                    }
                    if (proceed) {
                        IJsTranslation translation = adapter.getJsTranslation(true);
                        IJavaScriptElement[] result = translation.getElementsFromJsRange(translation.getJavaScriptOffset(hoverRegion.getOffset()), translation.getJavaScriptOffset(hoverRegion.getOffset() + hoverRegion.getLength()));
                        return translation.fixupMangledName(getHoverInfo(result));
                    }
                } catch (BadLocationException e) {
                // do nothing
                }
            }
        }
    } finally {
        if (xmlModel != null) {
            xmlModel.releaseFromRead();
        }
    }
    return null;
}
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) ITypedRegion(org.eclipse.jface.text.ITypedRegion) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) JsTranslationAdapter(org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 18 with IJavaScriptElement

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

the class JFaceNodeAdapterForJs method getJSElementsFromNode.

private synchronized Object[] getJSElementsFromNode(Node node) {
    if (node == null) {
        return new Object[0];
    }
    int startOffset = 0;
    int endOffset = 0;
    IJavaScriptElement[] result = new IJavaScriptElement[0];
    IJsTranslation translation = null;
    if (node.getNodeType() == Node.TEXT_NODE && (node instanceof NodeImpl)) {
        translation = getTranslation(node);
        startOffset = translation.getJavaScriptOffset(((NodeImpl) node).getStartOffset());
        endOffset = translation.getJavaScriptOffset(((NodeImpl) node).getEndOffset() - 1);
        if (startOffset >= 0 && endOffset >= 0)
            result = translation.getAllElementsInJsRange(startOffset, endOffset);
    }
    return result;
// 
// if (result == null) return null;
// Object[] newResults = new Object[result.length];
// for (int i = 0; i < result.length; i++) {
// int htmllength = 0;
// int htmloffset = 0;
// Position position = null;
// try {
// htmllength = ((SourceRefElement) (result[i])).getSourceRange().getLength();
// htmloffset = translation.getJspOffset(((SourceRefElement)
// (result[i])).getSourceRange().getOffset());
// position = new Position(htmloffset, htmllength);
// } catch (JavaScriptModelException e) {
// e.printStackTrace();
// }
// newResults[i] = getJsNode(node.getParentNode(), (IJavaScriptElement) result[i],
// position);
// }
// return newResults;
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) NodeImpl(org.eclipse.wst.xml.core.internal.document.NodeImpl) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement)

Example 19 with IJavaScriptElement

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

the class JSDTHyperlinkDetector method detectHyperlinks.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer,
	 *      org.eclipse.jface.text.IRegion, boolean)
	 */
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    List hyperlinks = new ArrayList(0);
    if (region != null && textViewer != null) {
        IDocument document = textViewer.getDocument();
        try {
            boolean proceed = false;
            ITypedRegion[] partitions = document.computePartitioning(region.getOffset(), region.getLength());
            for (int i = 0; i < partitions.length; i++) {
                for (int j = 0; j < PARTITION_TYPES.length; j++) {
                    if (PARTITION_TYPES[j].equals(partitions[i].getType())) {
                        proceed = true;
                    }
                }
            }
            if (proceed) {
                IJsTranslation jsTranslation = getJsTranslation(document);
                if (jsTranslation != null) {
                    // find hyperlink range for JavaScript elements
                    IRegion hyperlinkRegion = selectWord(document, region.getOffset());
                    IJavaScriptElement[] elements = jsTranslation.getElementsFromJsRange(jsTranslation.getJavaScriptOffset(region.getOffset()), jsTranslation.getJavaScriptOffset(region.getOffset() + region.getLength()));
                    if (elements != null && elements.length > 0) {
                        // create a hyperlink for each JavaScript element
                        for (int i = 0; i < elements.length; ++i) {
                            IJavaScriptElement element = elements[i];
                            IHyperlink link = createHyperlink(jsTranslation, element, hyperlinkRegion, document);
                            if (link != null) {
                                hyperlinks.add(link);
                            }
                        }
                    }
                }
            }
        } catch (BadLocationException e) {
        }
    }
    if (hyperlinks.size() == 0) {
        return null;
    }
    return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[0]);
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) ArrayList(java.util.ArrayList) IRegion(org.eclipse.jface.text.IRegion) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) ITypedRegion(org.eclipse.jface.text.ITypedRegion) ArrayList(java.util.ArrayList) List(java.util.List) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 20 with IJavaScriptElement

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

the class JFaceNodeAdapterForJs method getChildren.

public Object[] getChildren(Object object) {
    if (object instanceof IJavaScriptElement) {
        return getJavaElementProvider().getChildren(object);
    }
    if (object instanceof IJavaWebNode) {
        JavaElement enclosedElement = (JavaElement) ((IJavaWebNode) object).getJavaElement();
        if (enclosedElement != null) {
            try {
                IJavaScriptElement[] children = enclosedElement.getChildren();
                if (children == null) {
                    return new IJavaScriptElement[0];
                }
                Object[] nodes = new Object[children.length];
                Node parent = ((IJavaWebNode) object).getParentNode();
                for (int i = 0; i < children.length; i++) {
                    // int htmllength = ((SourceRefElement) (children[i])).getSourceRange().getLength();
                    // int htmloffset = ((SourceRefElement) (children[i])).getSourceRange().getOffset();
                    IJavaScriptElement javaElement = children[i];
                    ISourceRange range = null;
                    if (javaElement instanceof Member) {
                        range = ((IMember) javaElement).getNameRange();
                    } else {
                        range = ((ISourceReference) javaElement).getSourceRange();
                    }
                    int htmllength = range.getLength();
                    int htmloffset = range.getOffset();
                    Position position = new Position(htmloffset, htmllength);
                    nodes[i] = getJsNode(parent, javaElement, position);
                }
                return nodes;
            } catch (JavaScriptModelException ex) {
            }
        }
    }
    Node node = (Node) object;
    if (isJSElementParent(node)) {
        Object[] results = getJSElementsFromNode(node.getFirstChild(), true);
        return filter(results);
    }
    return super.getChildren(object);
}
Also used : JavaElement(org.eclipse.wst.jsdt.internal.core.JavaElement) Position(org.eclipse.jface.text.Position) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) Node(org.w3c.dom.Node) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) IMember(org.eclipse.wst.jsdt.core.IMember) Member(org.eclipse.wst.jsdt.internal.core.Member) ISourceRange(org.eclipse.wst.jsdt.core.ISourceRange)

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