//todo: replace with different strategies depending on file type, e.g. XmlBackedJSClassImpl for MXML
if (targetFile instanceof JSFile)
{
//We need the psiFile to be able to access psi elements (constructor, methods, etc) and add/edit/update them
JSFile jsFile = (JSFile) targetFile;
final JSClass jsClass = JSPsiImplUtils.findClass(jsFile);
String relativePathFromRoot = getRelativePathFromSourceRoot(targetFile, psiFile, project);
//Because an image can start with a number (and a var can't), prepend the image name with "image_"
String statement = "[Embed(source=\"" + relativePathFromRoot + "\")]\npublic var image_" + psiFile.getName().replace(".jpg", "") + ":Class;";
ASTNode jsTreeFromText = JSChangeUtil.createJSTreeFromText(project, statement, JavaScriptSupportLoader.ECMA_SCRIPT_L4);
if (jsClass.getConstructor() != null)
{
jsClass.addBefore(jsTreeFromText.getPsi(), jsClass.getConstructor());
}
else
{
jsClass.add(jsTreeFromText.getPsi());
}
}
}
}