com.gwtplatform.idhandler.rebind.ElementIdHandlerGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cedar-common-gwt Show documentation
Show all versions of cedar-common-gwt Show documentation
Utility code for use with GWT and Mvp4g.
/**
* Copyright 2011 ArcBees Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.gwtplatform.idhandler.rebind;
import java.io.PrintWriter;
import com.google.gwt.core.ext.Generator;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
import com.gwtplatform.idhandler.client.BaseElementIdHandler;
/**
* GWT deferred binding generator that provides {@link com.gwtplatform.idhandler.client.ElementIdHandler
* ElementIdHandler} implementations.
*
* Code Source
*
*
* This is external code that was copied into the CedarCommon codebase under
* the terms of its license.
*
*
*
*
*
*
* Source:
* GWTP Issue #389
*
*
* Date:
* March, 2011
*
*
*
*
*
* @see com.gwtplatform.idhandler.client.ElementIdHandler ElementIdHandler
*/
public class ElementIdHandlerGenerator extends Generator {
static final String ElementIdHandler_generateAndSetIds_owner = "owner";
@Override
public String generate(TreeLogger logger, GeneratorContext context,
String typeName) throws UnableToCompleteException {
TypeOracle oracle = context.getTypeOracle();
JClassType toGenerate = oracle.findType(typeName).isInterface();
if (toGenerate == null) {
logger.log(TreeLogger.ERROR, typeName + " is not an interface type");
throw new UnableToCompleteException();
}
ElementIdTypeParser parser = new ElementIdTypeParser(logger, toGenerate);
ElementIdStatement[] statements = parser.parseStatements();
String packageName = toGenerate.getPackage().getName();
String simpleSourceName = toGenerate.getName().replace('.', '_')
+ "Impl";
PrintWriter pw = context.tryCreate(logger, packageName,
simpleSourceName);
if (pw == null) {
return packageName + "." + simpleSourceName;
}
JClassType superclass = oracle.findType(
BaseElementIdHandler.class.getName()).isClass();
assert superclass != null : "No BaseElementIdHandler type";
ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(
packageName, simpleSourceName);
factory.setSuperclass(superclass.getQualifiedSourceName() + "<"
+ parser.getOwnerType().getParameterizedQualifiedSourceName()
+ ">");
factory.addImplementedInterface(typeName);
SourceWriter sw = factory.createSourceWriter(context, pw);
writeGenerateAndSetIds(sw, parser.getOwnerType(), statements);
sw.commit(logger);
return factory.getCreatedClassName();
}
void writeGenerateAndSetIds(SourceWriter sw, JClassType ownerType,
ElementIdStatement[] statements) {
sw.println("@Override public void generateAndSetIds(%s %s) {",
ownerType.getQualifiedSourceName(),
ElementIdHandler_generateAndSetIds_owner);
sw.indent();
for (ElementIdStatement st : statements) {
sw.println(String.format("if (%s) %s;", st.buildGuardCondition(),
st.buildIdSetterStatement()));
}
sw.outdent();
sw.println("}");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy