String templatePathString = templatePath.toPortableString();
// package
IPackageFragment uiPackage;
{
if (!(m_file.getParent() instanceof IFolder)) {
throw new DesignerException(IExceptionConstants.NO_FORM_PACKAGE, templatePathString);
}
// prepare package
IFolder uiFolder = (IFolder) m_file.getParent();
IJavaElement uiElement = JavaCore.create(uiFolder);
if (!(uiElement instanceof IPackageFragment)) {
throw new DesignerException(IExceptionConstants.NO_FORM_PACKAGE, templatePathString);
}
uiPackage = (IPackageFragment) uiElement;
// has client package
if (!Utils.isModuleSourcePackage(uiPackage)) {
throw new DesignerException(IExceptionConstants.NOT_CLIENT_PACKAGE, templatePathString);
}
}
// binder resource
m_binderResourceName = uiPackage.getElementName().replace('.', '/') + "/" + m_file.getName();
// try current package
{
String formName = StringUtils.removeEnd(m_file.getName(), ".ui.xml");
m_formClassName = uiPackage.getElementName() + "." + formName;
m_formType = m_javaProject.findType(m_formClassName);
if (m_formType != null) {
m_formFile = (IFile) m_formType.getCompilationUnit().getUnderlyingResource();
prepareBinderClass();
if (m_binderClassName != null) {
return;
}
}
}
// try @UiTemplate
IType uiTemplateType = m_javaProject.findType("com.google.gwt.uibinder.client.UiTemplate");
List<IJavaElement> references = CodeUtils.searchReferences(uiTemplateType);
for (IJavaElement reference : references) {
if (reference instanceof IAnnotation) {
IAnnotation annotation = (IAnnotation) reference;
IMemberValuePair[] valuePairs = annotation.getMemberValuePairs();
if (valuePairs.length == 1 && valuePairs[0].getValue() instanceof String) {
String templateName = (String) valuePairs[0].getValue();
// prepare ICompilationUnit
ICompilationUnit compilationUnit =
(ICompilationUnit) annotation.getAncestor(IJavaElement.COMPILATION_UNIT);
// prepare qualified template name
templateName = StringUtils.removeEnd(templateName, ".ui.xml");
if (templateName.contains(".")) {
templateName = templateName.replace('.', '/');
} else {
String packageName = compilationUnit.getPackageDeclarations()[0].getElementName();
templateName = packageName.replace('.', '/') + "/" + templateName;
}
templateName += ".ui.xml";
// if found, initialize "form" element
if (m_binderResourceName.equals(templateName)) {
m_formType = (IType) annotation.getParent().getParent();
m_formClassName = m_formType.getFullyQualifiedName();
m_formFile = (IFile) m_formType.getCompilationUnit().getUnderlyingResource();
prepareBinderClass();
if (m_binderClassName != null) {
return;
}
}
}
}
}
// no Java form
throw new DesignerException(IExceptionConstants.NO_FORM_TYPE, m_binderResourceName);
}