Search in sources :

Example 1 with ISourceRange

use of org.eclipse.wst.jsdt.core.ISourceRange 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 2 with ISourceRange

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

the class JsTranslation method getAllElementsInJsRange.

/* (non-Javadoc)
	 * @see org.eclipse.wst.jsdt.web.core.internal.java.IJsTranslation#getAllElementsInJsRange(int, int)
	 */
public IJavaScriptElement[] getAllElementsInJsRange(int javaPositionStart, int javaPositionEnd) {
    IJavaScriptElement[] EMTPY_RESULT_SET = new IJavaScriptElement[0];
    IJavaScriptElement[] result = EMTPY_RESULT_SET;
    IJavaScriptElement[] allChildren = null;
    try {
        allChildren = getCompilationUnit().getChildren();
    } catch (JavaScriptModelException e) {
    }
    Vector validChildren = new Vector();
    for (int i = 0; i < allChildren.length; i++) {
        ISourceRange range = getJSSourceRangeOf(allChildren[i]);
        if (javaPositionStart <= range.getOffset() && range.getLength() + range.getOffset() <= (javaPositionEnd)) {
            validChildren.add(allChildren[i]);
        } else if (allChildren[i].getElementType() == IJavaScriptElement.TYPE) {
            validChildren.add(allChildren[i]);
        }
    }
    if (validChildren.size() > 0) {
        result = (IJavaScriptElement[]) validChildren.toArray(new IJavaScriptElement[] {});
    }
    if (result == null || result.length == 0) {
        return EMTPY_RESULT_SET;
    }
    return result;
}
Also used : JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) Vector(java.util.Vector) ISourceRange(org.eclipse.wst.jsdt.core.ISourceRange)

Example 3 with ISourceRange

use of org.eclipse.wst.jsdt.core.ISourceRange 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)

Example 4 with ISourceRange

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

the class JFaceNodeAdapterForJs method filterChildrenForRange.

private Object[] filterChildrenForRange(IJavaScriptElement[] allChildren, Node node) {
    int javaPositionEnd = ((NodeImpl) node).getEndOffset();
    int javaPositionStart = ((NodeImpl) node).getStartOffset();
    javaPositionStart = getTranslation(node).getJavaScriptOffset(((NodeImpl) node).getStartOffset());
    if (javaPositionStart < 0)
        javaPositionStart = ((NodeImpl) node).getStartOffset();
    javaPositionEnd = getTranslation(node).getJavaScriptOffset(((NodeImpl) node).getEndOffset() - 1);
    if (javaPositionEnd < 0)
        javaPositionEnd = ((NodeImpl) node).getEndOffset();
    List validChildren = new ArrayList();
    for (int i = 0; i < allChildren.length; i++) {
        ISourceRange range = null;
        if (allChildren[i] instanceof Member) {
            try {
                range = ((Member) allChildren[i]).getNameRange();
            } catch (JavaScriptModelException e) {
                Logger.logException("Error getting range of Member child.", e);
            }
        } else if (allChildren[i] instanceof SourceRefElement) {
            try {
                range = ((SourceRefElement) allChildren[i]).getSourceRange();
            } catch (JavaScriptModelException e) {
                Logger.logException("Error getting range of SourceRefElement child.", e);
            }
        }
        if (range != null && javaPositionStart <= range.getOffset() && (range.getLength() + range.getOffset()) <= (javaPositionEnd)) {
            int htmlLength = range.getLength();
            int htmlOffset = range.getOffset();
            if (htmlLength >= 0 && htmlOffset >= 0) {
                Position position = new Position(htmlOffset, htmlLength);
                validChildren.add(getJsNode(node.getParentNode(), allChildren[i], position));
            }
        }
    }
    Object[] result = new Object[0];
    if (validChildren.size() > 0) {
        result = validChildren.toArray();
    }
    if (result == null || result.length == 0) {
        return new IJavaScriptElement[0];
    }
    return result;
}
Also used : NodeImpl(org.eclipse.wst.xml.core.internal.document.NodeImpl) Position(org.eclipse.jface.text.Position) JavaScriptModelException(org.eclipse.wst.jsdt.core.JavaScriptModelException) ArrayList(java.util.ArrayList) IJavaScriptElement(org.eclipse.wst.jsdt.core.IJavaScriptElement) ArrayList(java.util.ArrayList) List(java.util.List) IMember(org.eclipse.wst.jsdt.core.IMember) Member(org.eclipse.wst.jsdt.internal.core.Member) ISourceRange(org.eclipse.wst.jsdt.core.ISourceRange) SourceRefElement(org.eclipse.wst.jsdt.internal.core.SourceRefElement)

Aggregations

IJavaScriptElement (org.eclipse.wst.jsdt.core.IJavaScriptElement)4 ISourceRange (org.eclipse.wst.jsdt.core.ISourceRange)4 JavaScriptModelException (org.eclipse.wst.jsdt.core.JavaScriptModelException)4 Position (org.eclipse.jface.text.Position)2 IMember (org.eclipse.wst.jsdt.core.IMember)2 Member (org.eclipse.wst.jsdt.internal.core.Member)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Vector (java.util.Vector)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 IRegion (org.eclipse.jface.text.IRegion)1 ITypedRegion (org.eclipse.jface.text.ITypedRegion)1 Region (org.eclipse.jface.text.Region)1 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)1 IClassFile (org.eclipse.wst.jsdt.core.IClassFile)1 IFunction (org.eclipse.wst.jsdt.core.IFunction)1 IJavaScriptUnit (org.eclipse.wst.jsdt.core.IJavaScriptUnit)1 ILocalVariable (org.eclipse.wst.jsdt.core.ILocalVariable)1 ISourceReference (org.eclipse.wst.jsdt.core.ISourceReference)1