Search in sources :

Example 11 with ClassSourceFileComposerFactory

use of com.google.gwt.user.rebind.ClassSourceFileComposerFactory in project rstudio by rstudio.

the class ImageResourceInfo method generate.

/**
    * Generates the impl class and returns its name.
    */
public String generate() throws Exception {
    ImageResourceInfo images = generateImageBundle();
    simpleName_ = bundleType_.getName().replace('.', '_') + "__Impl";
    PrintWriter printWriter = context_.tryCreate(logger_, packageName_, simpleName_);
    if (printWriter != null) {
        // always null only when printWriter is also null.
        assert images != null;
        ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName_, simpleName_);
        factory.setSuperclass(bundleType_.getName());
        factory.addImport("org.rstudio.core.client.command.AppCommand");
        factory.addImport("org.rstudio.core.client.command.MenuCallback");
        factory.addImport("org.rstudio.core.client.command.ShortcutManager");
        factory.addImport("org.rstudio.core.client.resources.ImageResource2x");
        SourceWriter writer = factory.createSourceWriter(context_, printWriter);
        emitConstructor(writer, images);
        emitCommandFields(writer);
        emitMenus(writer);
        emitShortcuts(writer);
        emitCommandAccessors(writer);
        // Close the class and commit it
        writer.outdent();
        writer.println("}");
        context_.commit(logger_, printWriter);
    }
    return packageName_ + "." + simpleName_;
}
Also used : ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) SourceWriter(com.google.gwt.user.rebind.SourceWriter) PrintWriter(java.io.PrintWriter)

Example 12 with ClassSourceFileComposerFactory

use of com.google.gwt.user.rebind.ClassSourceFileComposerFactory in project rstudio by rstudio.

the class ImageResourceInfo method generateImageBundle.

private ImageResourceInfo generateImageBundle() {
    String className = bundleType_.getSimpleSourceName() + "__AutoGenResources";
    String pathToInstance = packageName_ + "." + className + ".INSTANCE";
    ImageResourceInfo iri = new ImageResourceInfo(pathToInstance);
    PrintWriter printWriter = context_.tryCreate(logger_, packageName_, className);
    if (printWriter == null)
        return null;
    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName_, className);
    factory.addImport("com.google.gwt.core.client.GWT");
    factory.addImport("com.google.gwt.resources.client.*");
    factory.makeInterface();
    factory.addImplementedInterface("ClientBundle");
    SourceWriter writer = factory.createSourceWriter(context_, printWriter);
    Set<String> resourceNames = context_.getResourcesOracle().getPathNames();
    for (JMethod method : commandMethods_) {
        String commandId = method.getName();
        String key = packageName_.replace('.', '/') + "/" + commandId;
        if (resourceNames.contains(key + ".png")) {
            writer.println("ImageResource " + commandId + "();");
            iri.addImage(commandId);
        }
        if (resourceNames.contains(key + "_2x.png")) {
            writer.println("@Source(\"" + commandId + "_2x.png\")");
            writer.println("ImageResource " + commandId + "2x();");
            iri.addImage(commandId + "2x");
        }
    }
    writer.println("public static final " + className + " INSTANCE = " + "(" + className + ")GWT.create(" + className + ".class);");
    writer.outdent();
    writer.println("}");
    context_.commit(logger_, printWriter);
    return iri;
}
Also used : ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) JMethod(com.google.gwt.core.ext.typeinfo.JMethod) SourceWriter(com.google.gwt.user.rebind.SourceWriter) PrintWriter(java.io.PrintWriter)

Example 13 with ClassSourceFileComposerFactory

use of com.google.gwt.user.rebind.ClassSourceFileComposerFactory in project playn by threerings.

the class AutoClientBundleGenerator method generate.

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
    TypeOracle typeOracle = context.getTypeOracle();
    JClassType userType;
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type: " + typeName, e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();
    className = className.replace('.', '_');
    if (userType.isInterface() == null) {
        logger.log(TreeLogger.ERROR, userType.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className + "Impl");
    composerFactory.addImplementedInterface(userType.getQualifiedSourceName());
    composerFactory.addImport(ClientBundleWithLookup.class.getName());
    composerFactory.addImport(DataResource.class.getName());
    composerFactory.addImport(GWT.class.getName());
    composerFactory.addImport(ImageResource.class.getName());
    composerFactory.addImport(ResourcePrototype.class.getName());
    composerFactory.addImport(TextResource.class.getName());
    File warDirectory = getWarDirectory(logger);
    assert warDirectory.isDirectory();
    File classesDirectory = new File(warDirectory, WEB_INF_CLASSES);
    assert classesDirectory.isDirectory();
    File resourcesDirectory = new File(classesDirectory, packageName.replace('.', '/'));
    assert resourcesDirectory.isDirectory();
    String baseClassesPath = classesDirectory.getPath();
    logger.log(TreeLogger.DEBUG, "baseClassesPath: " + baseClassesPath);
    Set<Resource> resources = preferMp3(getResources(context, userType, fileFilter));
    Set<String> methodNames = new HashSet<String>();
    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);
        // write out jump methods
        sw.println("public ResourcePrototype[] getResources() {");
        sw.indent();
        sw.println("return MyBundle.INSTANCE.getResources();");
        sw.outdent();
        sw.println("}");
        sw.println("public ResourcePrototype getResource(String name) {");
        sw.indent();
        sw.println("return MyBundle.INSTANCE.getResource(name);");
        sw.outdent();
        sw.println("}");
        // write out static ClientBundle interface
        sw.println("static interface MyBundle extends ClientBundleWithLookup {");
        sw.indent();
        sw.println("MyBundle INSTANCE = GWT.create(MyBundle.class);");
        for (Resource resource : resources) {
            String relativePath = resource.getPath();
            String filename = resource.getPath().substring(resource.getPath().lastIndexOf('/') + 1);
            String contentType = getContentType(logger, resource);
            String methodName = stripExtension(filename);
            if (!isValidMethodName(methodName)) {
                logger.log(TreeLogger.WARN, "Skipping invalid method name (" + methodName + ") due to: " + relativePath);
                continue;
            }
            if (!methodNames.add(methodName)) {
                logger.log(TreeLogger.WARN, "Skipping duplicate method name due to: " + relativePath);
                continue;
            }
            logger.log(TreeLogger.DEBUG, "Generating method for: " + relativePath);
            Class<? extends ResourcePrototype> returnType = getResourcePrototype(contentType);
            // generate method
            sw.println();
            if (returnType == DataResource.class) {
                if (contentType.startsWith("audio/")) {
                    // Prevent the use of data URLs, which Flash won't play
                    sw.println("@DataResource.DoNotEmbed");
                } else {
                    // Specify an explicit MIME type, for use in the data URL
                    sw.println("@DataResource.MimeType(\"" + contentType + "\")");
                }
            }
            sw.println("@Source(\"" + relativePath + "\")");
            sw.println(returnType.getName() + " " + methodName + "();");
        }
        sw.outdent();
        sw.println("}");
        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}
Also used : TextResource(com.google.gwt.resources.client.TextResource) ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) GWT(com.google.gwt.core.client.GWT) ClientBundleWithLookup(com.google.gwt.resources.client.ClientBundleWithLookup) ImageResource(com.google.gwt.resources.client.ImageResource) Resource(com.google.gwt.dev.resource.Resource) TextResource(com.google.gwt.resources.client.TextResource) DataResource(com.google.gwt.resources.client.DataResource) NotFoundException(com.google.gwt.core.ext.typeinfo.NotFoundException) DataResource(com.google.gwt.resources.client.DataResource) SourceWriter(com.google.gwt.user.rebind.SourceWriter) ResourcePrototype(com.google.gwt.resources.client.ResourcePrototype) JClassType(com.google.gwt.core.ext.typeinfo.JClassType) ImageResource(com.google.gwt.resources.client.ImageResource) TypeOracle(com.google.gwt.core.ext.typeinfo.TypeOracle) UnableToCompleteException(com.google.gwt.core.ext.UnableToCompleteException) File(java.io.File) HashSet(java.util.HashSet) PrintWriter(java.io.PrintWriter)

Aggregations

ClassSourceFileComposerFactory (com.google.gwt.user.rebind.ClassSourceFileComposerFactory)13 PrintWriter (java.io.PrintWriter)13 SourceWriter (com.google.gwt.user.rebind.SourceWriter)11 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)4 GWT (com.google.gwt.core.client.GWT)3 UnableToCompleteException (com.google.gwt.core.ext.UnableToCompleteException)3 TypeOracle (com.google.gwt.core.ext.typeinfo.TypeOracle)3 NotFoundException (com.google.gwt.core.ext.typeinfo.NotFoundException)2 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 BadPropertyValueException (com.google.gwt.core.ext.BadPropertyValueException)1 ConfigurationProperty (com.google.gwt.core.ext.ConfigurationProperty)1 PropertyOracle (com.google.gwt.core.ext.PropertyOracle)1 JMethod (com.google.gwt.core.ext.typeinfo.JMethod)1 Resource (com.google.gwt.dev.resource.Resource)1 ClientBundleWithLookup (com.google.gwt.resources.client.ClientBundleWithLookup)1 DataResource (com.google.gwt.resources.client.DataResource)1 ImageResource (com.google.gwt.resources.client.ImageResource)1 ResourcePrototype (com.google.gwt.resources.client.ResourcePrototype)1 TextResource (com.google.gwt.resources.client.TextResource)1 File (java.io.File)1