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;
}
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;
}
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);
}
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;
}
Aggregations