use of org.eclipse.wst.jsdt.core.IClassFile 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;
}
Aggregations