Package org.gwtoolbox.bean.rebind.validation.config

Source Code of org.gwtoolbox.bean.rebind.validation.config.ValidationMessagesHolderGenerator

package org.gwtoolbox.bean.rebind.validation.config;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.ext.GeneratorContext;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
import org.gwtoolbox.bean.client.validation.messages.ValidationMessages;
import org.gwtoolbox.commons.generator.rebind.EasyTreeLogger;

import java.io.PrintWriter;

/**
* @author Uri Boness
*/
public class ValidationMessagesHolderGenerator {

    public static String generate(EasyTreeLogger logger, GeneratorContext context, Class<? extends ValidationMessages> messagesClass) {

        String packageName = ValidationMessages.class.getPackage().getName();

        String className = "gtx__ValidationMessagesHolder";
        String qualifiedValidatorClassName = packageName + "." + className;
        SourceWriter sourceWriter = getSourceWriter(logger, context, packageName, className);
        if (sourceWriter == null) {
            return qualifiedValidatorClassName;
        }

        write(logger, context, sourceWriter, messagesClass);

        sourceWriter.commit(logger);

        return qualifiedValidatorClassName;
    }


    private static SourceWriter getSourceWriter(EasyTreeLogger logger, GeneratorContext context, String packageName, String className) {

        PrintWriter printWriter = context.tryCreate(logger, packageName, className);

        if (printWriter == null) {
            return null;
        }

        ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);

        //TODO add imports here
        composerFactory.addImport(GWT.class.getName());

        return composerFactory.createSourceWriter(context, printWriter);
    }

    private static void write(
            EasyTreeLogger logger,
            GeneratorContext context,
            SourceWriter writer,
            Class<? extends ValidationMessages> messagesClass) {

        writer.println("public static " + messagesClass.getName() + " instance;");
        writer.println();

        writer.println("public static " + messagesClass.getName() + " get() {");
        writer.println("    if (instance == null) {");
        writer.println("        instance = GWT.create(" + messagesClass.getName() + ".class);");
        writer.println("    }");
        writer.println("    return instance;");
        writer.println("}");
    }

}
TOP

Related Classes of org.gwtoolbox.bean.rebind.validation.config.ValidationMessagesHolderGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.