Example usage for com.google.gwt.user.rebind SourceWriter print

List of usage examples for com.google.gwt.user.rebind SourceWriter print

Introduction

In this page you can find the example usage for com.google.gwt.user.rebind SourceWriter print.

Prototype

void print(String s);

Source Link

Usage

From source file:cc.alcina.framework.entity.gen.SimpleCssResourceGenerator.java

License:Apache License

/**
 * A single constant that is too long will crash the compiler with an out of
 * memory error. Break up the constant and generate code that appends using
 * a buffer./*from  w ww .  ja v a 2s.c  o  m*/
 */
private void writeLongString(SourceWriter sw, String toWrite) {
    sw.println("StringBuilder builder = new StringBuilder();");
    int offset = 0;
    int length = toWrite.length();
    while (offset < length - 1) {
        int subLength = Math.min(MAX_STRING_CHUNK, length - offset);
        sw.print("builder.append(\"");
        sw.print(Generator.escape(toWrite.substring(offset, offset + subLength)));
        sw.println("\");");
        offset += subLength;
    }
    sw.println("return builder.toString();");
}

From source file:com.ait.ext4j.rebind.BeanModelGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    oracle = context.getTypeOracle();/*from  w w w  .  j  a  v a2s  .  com*/
    beanModelMarkerType = oracle.findType(BeanModelMarker.class.getName());
    beanModelTagType = oracle.findType(BeanModelTag.class.getName());

    try {
        // final all beans and bean markers
        beans = new ArrayList<JClassType>();
        JClassType[] types = oracle.getTypes();
        for (JClassType type : types) {
            if (isBeanMarker(type)) {
                beans.add(getMarkerBean(type));
            } else if (isBean(type)) {
                beans.add(type);
            }
        }

        final String genPackageName = BeanModelLookup.class.getPackage().getName();
        final String genClassName = "BeanModelLookupImpl";

        ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName,
                genClassName);
        composer.setSuperclass(BeanModelLookup.class.getCanonicalName());
        composer.addImport(BeanModelFactory.class.getName());
        composer.addImport(Map.class.getName());
        composer.addImport(FastMap.class.getName());

        PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName);

        if (pw != null) {
            SourceWriter sw = composer.createSourceWriter(context, pw);

            sw.println("private Map<String, BeanModelFactory> m;");

            sw.println("public BeanModelFactory getFactory(Class b) {");
            sw.indent();
            sw.println("String n = b.getName();");
            sw.println("if (m == null) {");
            sw.indentln("m = new FastMap<BeanModelFactory>();");
            sw.println("}");
            sw.println("if (m.get(n) == null) {");
            sw.indent();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < beans.size(); i++) {
                JClassType bean = beans.get(i);
                String name = createBean(bean, logger, context);
                String factory = createFactory(bean, name, logger, context);

                if (i > 0) {
                    sw.print(" else ");
                }
                sw.println("if (" + bean.getQualifiedSourceName() + ".class.getName().equals(n)) {");
                sw.indentln("m" + i + "();");

                sb.append("private void m" + i + "() {\n");
                sb.append("  m.put(" + bean.getQualifiedSourceName() + ".class.getName(), new " + factory
                        + "());\n");
                sb.append("}\n");

                sw.print("}");
            }
            sw.outdent();
            sw.println("}");
            sw.println("return m.get(n);");
            sw.outdent();
            sw.println("}");

            sw.println(sb.toString());
            sw.commit(logger);
        }

        return composer.getCreatedClassName();

    } catch (Exception e) {
        logger.log(TreeLogger.ERROR, "Class " + typeName + " not found.", e);
        throw new UnableToCompleteException();
    }

}

From source file:com.ait.toolkit.rebind.BeanModelGenerator.java

License:Open Source License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    oracle = context.getTypeOracle();//from w w w  .j  ava  2 s .  c  om
    beanModelMarkerType = oracle.findType(BeanMarker.class.getName());
    beanModelTagType = oracle.findType(BeanTag.class.getName());

    try {
        // final all beans and bean markers
        beans = new ArrayList<JClassType>();
        JClassType[] types = oracle.getTypes();
        for (JClassType type : types) {
            if (isBeanMarker(type)) {
                beans.add(getMarkerBean(type));
            } else if (isBean(type)) {
                beans.add(type);
            }
        }

        final String genPackageName = BeanLookup.class.getPackage().getName();
        final String genClassName = "BeanLookupImpl";

        ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(genPackageName,
                genClassName);
        composer.setSuperclass(BeanLookup.class.getCanonicalName());
        composer.addImport(BeanFactory.class.getName());
        composer.addImport(Map.class.getName());
        composer.addImport(FastMap.class.getName());

        PrintWriter pw = context.tryCreate(logger, genPackageName, genClassName);

        if (pw != null) {
            SourceWriter sw = composer.createSourceWriter(context, pw);

            sw.println("private Map<String, BeanFactory> m;");

            sw.println("public BeanFactory getFactory(Class b) {");
            sw.indent();
            sw.println("String n = b.getName();");
            sw.println("if (m == null) {");
            sw.indentln("m = new FastMap<BeanFactory>();");
            sw.println("}");
            sw.println("if (m.get(n) == null) {");
            sw.indent();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < beans.size(); i++) {
                JClassType bean = beans.get(i);
                String name = createBean(bean, logger, context);
                String factory = createFactory(bean, name, logger, context);

                if (i > 0) {
                    sw.print(" else ");
                }
                sw.println("if (" + bean.getQualifiedSourceName() + ".class.getName().equals(n)) {");
                sw.indentln("m" + i + "();");

                sb.append("private void m" + i + "() {\n");
                sb.append("  m.put(" + bean.getQualifiedSourceName() + ".class.getName(), new " + factory
                        + "());\n");
                sb.append("}\n");

                sw.print("}");
            }
            sw.outdent();
            sw.println("}");
            sw.println("return m.get(n);");
            sw.outdent();
            sw.println("}");

            sw.println(sb.toString());
            sw.commit(logger);
        }

        return composer.getCreatedClassName();

    } catch (Exception e) {
        logger.log(TreeLogger.ERROR, "Class " + typeName + " not found.", e);
        throw new UnableToCompleteException();
    }

}

From source file:com.cgxlib.xq.rebind.JsonBuilderGenerator.java

License:Apache License

public void generateMethod(SourceWriter sw, JMethod method, String name, TreeLogger logger)
        throws UnableToCompleteException {
    String ifaceName = method.getEnclosingType().getQualifiedSourceName();

    String retType = method.getReturnType().getParameterizedQualifiedSourceName();
    sw.print("public final " + retType + " " + method.getName());
    JParameter[] params = method.getParameters();
    if (params.length == 0) {
        JArrayType arr = method.getReturnType().isArray();
        JParameterizedType list = method.getReturnType().isParameterized();

        sw.println("() {");
        sw.indent();/*from  w ww . j a  v  a  2  s.co  m*/
        if (retType.matches("(java.lang.Boolean|boolean)")) {
            sw.println("return p.getBoolean(\"" + name + "\");");
        } else if (retType.matches("java.util.Date")) {
            sw.println("return new Date(java.lang.Long.parseLong(p.getStr(\"" + name + "\")));");
        } else if (method.getReturnType().isPrimitive() != null) {
            sw.println("return (" + retType + ")p.getFloat(\"" + name + "\");");
        } else if (retType.equals("java.lang.Character")) {
            sw.println("return (char) p.getFloat(\"" + name + "\");");
        } else if (retType.equals("java.lang.Byte")) {
            sw.println("return (byte) p.getFloat(\"" + name + "\");");
        } else if (retType.equals("java.lang.Integer")) {
            sw.println("return (int) p.getFloat(\"" + name + "\");");
        } else if (retType.equals("java.lang.Float")) {
            sw.println("return p.getFloat(\"" + name + "\");");
        } else if (retType.equals("java.lang.Double")) {
            sw.println("return (double) p.getFloat(\"" + name + "\");");
        } else if (retType.equals("java.lang.Long")) {
            sw.println("return (long) p.getFloat(\"" + name + "\");");
        } else if (retType.equals("java.lang.Byte")) {
            sw.println("return (byte) p.getFloat(\"" + name + "\");");
        } else if (isTypeAssignableTo(method.getReturnType(), stringType)) {
            sw.println("return p.getStr(\"" + name + "\");");
        } else if (isTypeAssignableTo(method.getReturnType(), jsonBuilderType)) {
            String q = method.getReturnType().getQualifiedSourceName();
            sw.println("return " + "((" + q + ")GWT.create(" + q + ".class))" + ".load(getPropertiesBase(\""
                    + name + "\"));");
        } else if (isTypeAssignableTo(method.getReturnType(), settingsType)) {
            String q = method.getReturnType().getQualifiedSourceName();
            sw.println("return " + "((" + q + ")getPropertiesBase(\"" + name + "\"));");
        } else if (retType.equals(Properties.class.getName())) {
            sw.println("return getPropertiesBase(\"" + name + "\");");
        } else if (isTypeAssignableTo(method.getReturnType(), jsType)) {
            sw.println("return p.getJavaScriptObject(\"" + name + "\");");
        } else if (isTypeAssignableTo(method.getReturnType(), functionType)) {
            sw.println("return p.getFunction(\"" + name + "\");");
        } else if (arr != null || list != null) {
            JType type = arr != null ? arr.getComponentType() : list.getTypeArgs()[0];
            boolean buildType = isTypeAssignableTo(type, jsonBuilderType);
            String t = type.getQualifiedSourceName();
            sw.println("JsArrayMixed a = p.getArray(\"" + name + "\");");
            sw.println("int l = a == null ? 0 : a.length();");
            String ret;
            if (buildType) {
                sw.println(t + "[] r = new " + t + "[l];");
                sw.println("JsObjectArray<?> a1 = p.getArray(\"" + name + "\").cast();");
                sw.println("int l1 = r.length;");
                sw.println("for (int i = 0 ; i < l1 ; i++) {");
                sw.println("  Object w = a1.get(i);");
                sw.println("  " + t + " instance = GWT.create(" + t + ".class);");
                sw.println("  r[i] = instance.load(w);");
                sw.println("}");
                ret = "r";
            } else {
                ret = "getArrayBase(\"" + name + "\", new " + t + "[l], " + t + ".class)";
            }
            if (arr != null) {
                sw.println("return " + ret + ";");
            } else {
                sw.println("return Arrays.asList(" + ret + ");");
            }
        } else if (method.getReturnType().isEnum() != null) {
            sw.println("return " + method.getReturnType().getQualifiedSourceName() + ".valueOf(p.getStr(\""
                    + name + "\"));");
        } else {
            sw.println("System.err.println(\"JsonBuilderGenerator WARN: unknown return type " + retType + " "
                    + ifaceName + "." + name + "()\"); ");
            // We return the object because probably the user knows how to handle it
            sw.println("return p.get(\"" + name + "\");");
        }
        sw.outdent();
        sw.println("}");
    } else if (params.length == 1) {
        JType type = params[0].getType();
        JArrayType arr = type.isArray();
        JParameterizedType list = type.isParameterized();

        sw.print("(" + type.getParameterizedQualifiedSourceName() + " a)");
        sw.println("{");
        sw.indent();
        if (arr != null || list != null) {
            String a = "a";
            if (list != null) {
                a = "a.toArray(new " + list.getTypeArgs()[0].getQualifiedSourceName() + "[0])";
            }
            sw.println("setArrayBase(\"" + name + "\", " + a + ");");
        } else if (type.getParameterizedQualifiedSourceName().matches("java.util.Date")) {
            sw.println("p.setNumber(\"" + name + "\", a.getTime());");
        } else if (type.getParameterizedQualifiedSourceName().matches(
                "(java.lang.(Character|Long|Double|Integer|Float|Byte)|(char|long|double|int|float|byte))")) {
            sw.println("p.setNumber(\"" + name + "\", a);");
        } else if (type.getParameterizedQualifiedSourceName().matches("(java.lang.Boolean|boolean)")) {
            sw.println("p.setBoolean(\"" + name + "\", a);");
        } else if (type.getParameterizedQualifiedSourceName().matches("com.cgxlib.xq.client.Function")) {
            sw.println("p.setFunction(\"" + name + "\", a);");
        } else if (type.isEnum() != null) {
            sw.println("p.set(\"" + name + "\", a.name());");
        } else {
            sw.println("set(\"" + name + "\", a);");
        }
        if (!"void".equals(retType)) {
            if (isTypeAssignableTo(method.getReturnType(), method.getEnclosingType())) {
                sw.println("return this;");
            } else {
                sw.println("return null;");
            }
        }
        sw.outdent();
        sw.println("}");
    }
}

From source file:com.cgxlib.xq.rebind.LazyGenerator.java

License:Apache License

public void generateMethod(SourceWriter sw, JMethod method, String nonLazyClass, String genClass,
        TreeLogger logger) throws UnableToCompleteException {

    JParameter[] params = method.getParameters();
    JTypeParameter gType = method.getReturnType().isTypeParameter();

    String retType = method.getReturnType().getParameterizedQualifiedSourceName();
    if (gType != null) {
        retType = "<" + gType.getParameterizedQualifiedSourceName() + " extends "
                + gType.getFirstBound().getQualifiedSourceName() + "> " + retType;
    }//from www  .  j a va2 s. c  om
    sw.print("public final native " + retType + " " + method.getName());
    sw.print("(");
    int argNum = 0;
    for (JParameter param : params) {
        sw.print((argNum == 0 ? "" : ", ") + param.getType().getParameterizedQualifiedSourceName() + " arg"
                + argNum);
        argNum++;
    }
    sw.println(") /*-{");

    sw.indent();
    sw.println("var self=this;");
    sw.println("this.@" + genClass + "::closures.push(");
    sw.indent();
    sw.println("function() {");
    sw.indent();
    sw.print("self.@" + genClass + "::ctx=self.@" + genClass + "::ctx.@" + nonLazyClass + "::"
            + method.getName());
    sw.print(getJSNIParams(method));
    sw.print("(");
    for (int i = 0; i < argNum; i++) {
        sw.print("arg" + i + (i < argNum - 1 ? "," : ""));
    }

    sw.print(")");
    // special case, as() needs to invoke createLazy()
    if ("as".equals(method.getName())) {
        sw.print(".createLazy()");
    }
    sw.println(";");
    sw.outdent();
    sw.println("}");
    sw.outdent();
    sw.println(");");
    sw.println("return this;");
    sw.outdent();
    sw.println("}-*/;");
}

From source file:com.cgxlib.xq.rebind.SelectorGeneratorBase.java

License:Apache License

public void generateMethod(SourceWriter sw, JMethod method, TreeLogger logger)
        throws UnableToCompleteException {
    Selector selectorAnnotation = method.getAnnotation(Selector.class);
    if (selectorAnnotation == null) {
        return;//from w  w w .  j av a  2 s. c o  m
    }

    JParameter[] params = method.getParameters();

    String retType = method.getReturnType().getParameterizedQualifiedSourceName();
    sw.print("public final " + retType + " " + method.getName());
    boolean hasContext = false;
    if (params.length == 0) {
        sw.print("()");
    } else if (params.length == 1) {
        JClassType type = params[0].getType().isClassOrInterface();
        if (type != null && type.isAssignableTo(nodeType)) {
            sw.print("(Node root)");
            hasContext = true;
        }
    }
    sw.println(" {");
    sw.indent();
    Selector sel = method.getAnnotation(Selector.class);

    if (sel != null && sel.value().matches("^#\\w+$")) {
        // short circuit #foo
        sw.println("return " + wrap(method,
                "JsNodeArray.create(((Document)root).getElementById(\"" + sel.value().substring(1) + "\"))")
                + ";");
    } else if (sel != null && sel.value().matches("^\\w+$")) {
        // short circuit FOO
        sw.println("return "
                + wrap(method,
                        "JsNodeArray.create(((Element)root).getElementsByTagName(\"" + sel.value() + "\"))")
                + ";");
    } else if (sel != null && sel.value().matches("^\\.\\w+$") && hasGetElementsByClassName()) {
        // short circuit .foo for browsers with native getElementsByClassName
        sw.println("return " + wrap(method,
                "JsNodeArray.create(getElementsByClassName(\"" + sel.value().substring(1) + "\", root))")
                + ";");
    } else {
        generateMethodBody(sw, method, logger, hasContext);
    }
    sw.outdent();
    sw.println("}");
}

From source file:com.cgxlib.xq.rebind.XmlBuilderGenerator.java

License:Apache License

public void generateMethod(SourceWriter sw, JMethod method, TreeLogger logger)
        throws UnableToCompleteException {
    Name nameAnnotation = method.getAnnotation(Name.class);
    String name = nameAnnotation != null ? nameAnnotation.value()
            : method.getName().replaceFirst("^(get|set)", "");

    if (nameAnnotation == null) {
        name = name.substring(0, 1).toLowerCase() + name.substring(1);
    }/*from   www  . j  a  v a2  s  . c om*/

    String retType = method.getReturnType().getParameterizedQualifiedSourceName();

    sw.print("public final " + retType + " " + method.getName());
    JParameter[] params = method.getParameters();
    if (params.length == 0) {
        JArrayType arr = method.getReturnType().isArray();
        sw.println("() {");
        sw.indent();
        if (retType.matches("(java.lang.Boolean|boolean)")) {
            sw.println("return getBooleanBase(\"" + name + "\");");
        } else if (method.getReturnType().isPrimitive() != null) {
            sw.println("return (" + retType + ")getFloatBase(\"" + name + "\");");
        } else if (isTypeAssignableTo(method.getReturnType(), stringType)) {
            sw.println("return getStrBase(\"" + name + "\");");
        } else if (isTypeAssignableTo(method.getReturnType(), xmlBuilderType)) {
            String q = method.getReturnType().getQualifiedSourceName();
            sw.println("Element e = getElementBase(\"" + name + "\");");
            sw.println(
                    "return e == null ? null : (" + q + ")((" + q + ")GWT.create(" + q + ".class)).load(e);");
        } else if (retType.equals(Properties.class.getName())) {
            sw.println("return getPropertiesBase(\"" + name + "\");");
        } else if (arr != null) {
            String q = arr.getComponentType().getQualifiedSourceName();
            sw.println("ArrayList<" + q + "> l = new ArrayList<" + q + ">();");
            sw.println("for (Element e: getElementsBase(\"" + name + "\")) {");
            sw.println("  " + q + " c = GWT.create(" + q + ".class);");
            sw.println("  c.load(e);");
            sw.println("  l.add(c);");
            sw.println("}");
            sw.println("return l.toArray(new " + q + "[0]);");
        } else {
            sw.println("return null; // Unsupported return type: " + retType);
        }
        sw.outdent();
        sw.println("}");
    } else if (params.length == 1) {
        JType type = params[0].getType();
        JArrayType arr = type.isArray();
        String qname = type.getParameterizedQualifiedSourceName();
        sw.print("(" + qname + " a)");
        sw.println("{");
        sw.indent();
        if (arr != null) {
            sw.println("setArrayBase(\"" + name + "\", a);");
        } else {
            sw.println("setBase(\"" + name + "\", a);");
        }
        if (!"void".equals(retType)) {
            if (isTypeAssignableTo(method.getReturnType(), method.getEnclosingType())) {
                sw.println("return this;");
            } else {
                sw.println("return null;");
            }
        }
        sw.outdent();
        sw.println("}");
    }
}

From source file:com.colinalworth.gwt.websockets.rebind.ServerCreator.java

License:Apache License

public void create(TreeLogger logger, GeneratorContext context) throws UnableToCompleteException {
    String typeName = this.serverType.getQualifiedSourceName();

    String packageName = getPackageName();
    String simpleName = getSimpleName();

    TypeOracle oracle = context.getTypeOracle();

    PrintWriter pw = context.tryCreate(logger, packageName, simpleName);
    if (pw == null) {
        return;//from  w  w w  .  ja  va  2s.  c  o  m
    }
    JClassType serverType = oracle.findType(Name.getSourceNameForClass(Server.class));
    JClassType clientType = ModelUtils.findParameterizationOf(serverType, this.serverType)[1];

    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleName);
    factory.setSuperclass(Name.getSourceNameForClass(ServerImpl.class) + "<" + typeName + ","
            + clientType.getQualifiedSourceName() + ">");
    factory.addImplementedInterface(typeName);

    SourceWriter sw = factory.createSourceWriter(context, pw);

    //TODO move this check before the printwriter creation can fail, and allow the warn to be optional
    sw.println("public %1$s(%2$s errorHandler) {", simpleName,
            Name.getSourceNameForClass(ServerBuilder.ConnectionErrorHandler.class));
    RemoteServiceRelativePath path = this.serverType.getAnnotation(RemoteServiceRelativePath.class);
    if (path == null) {
        //         logger.log(Type.WARN, "@RemoteServiceRelativePath required on " + typeName + " to make a connection to the server without a ServerBuilder");
        //         throw new UnableToCompleteException();
        sw.indentln("super(null);");
        sw.indentln(
                "throw new RuntimeException(\"@RemoteServiceRelativePath annotation required on %1$s to make a connection without a path defined in ServerBuilder\");");
    } else {
        sw.indentln("super(errorHandler, "
                + "com.google.gwt.user.client.Window.Location.getProtocol().toLowerCase().startsWith(\"https\") ? \"wss://\": \"ws://\", "
                + "com.google.gwt.user.client.Window.Location.getHost(), \"%1$s\");", path.value());
    }
    sw.println("}");

    sw.println("public %1$s(%2$s errorHandler, String url) {", simpleName,
            Name.getSourceNameForClass(ServerBuilder.ConnectionErrorHandler.class));
    sw.indentln("super(errorHandler, url);");
    sw.println("}");

    //Find all types that may go over the wire
    // Collect the types the server will send to the client using the Client interface
    SerializableTypeOracleBuilder serverSerializerBuilder = new SerializableTypeOracleBuilder(logger, context);
    appendMethodParameters(logger, clientType, Client.class, serverSerializerBuilder);
    // Also add the wrapper object ClientInvocation
    serverSerializerBuilder.addRootType(logger, oracle.findType(ClientInvocation.class.getName()));
    serverSerializerBuilder.addRootType(logger, oracle.findType(ClientCallbackInvocation.class.getName()));

    // Collect the types the client will send to the server using the Server interface
    SerializableTypeOracleBuilder clientSerializerBuilder = new SerializableTypeOracleBuilder(logger, context);
    appendMethodParameters(logger, this.serverType, Server.class, clientSerializerBuilder);
    // Also add the ServerInvocation wrapper
    clientSerializerBuilder.addRootType(logger, oracle.findType(ServerInvocation.class.getName()));
    clientSerializerBuilder.addRootType(logger, oracle.findType(ServerCallbackInvocation.class.getName()));

    String tsName = simpleName + "_TypeSerializer";
    TypeSerializerCreator serializerCreator = new TypeSerializerCreator(logger,
            clientSerializerBuilder.build(logger), serverSerializerBuilder.build(logger), context,
            packageName + "." + tsName, tsName);
    serializerCreator.realize(logger);

    // Make the newly created Serializer available at runtime
    sw.println("protected %1$s __getSerializer() {", Serializer.class.getName());
    sw.indentln("return %2$s.<%1$s>create(%1$s.class);", tsName, GWT.class.getName());
    sw.println("}");

    // Build methods that call from the client to the server
    for (JMethod m : this.serverType.getInheritableMethods()) {
        if (isRemoteMethod(m, Server.class)) {
            printServerMethodBody(logger, context, sw, m);
        }
    }

    // Read incoming calls and dispatch them to the correct client method
    sw.println("protected void __invoke(String method, Object[] params) {");
    for (JMethod m : clientType.getInheritableMethods()) {
        if (isRemoteMethod(m, Client.class)) {
            JParameter[] params = m.getParameters();
            sw.println("if (method.equals(\"%1$s\") && params.length == %2$d) {", m.getName(), params.length);
            sw.indent();
            sw.println("getClient().%1$s(", m.getName());
            sw.indent();
            for (int i = 0; i < params.length; i++) {
                if (i != 0) {
                    sw.print(",");
                }
                sw.println("(%1$s)params[%2$d]", params[i].getType().getQualifiedSourceName(), i);
            }
            sw.outdent();
            sw.println(");");
            sw.outdent();
            sw.println("}");
        }
    }
    sw.println("}");

    sw.println("protected void __onError(Exception error) {");
    sw.println("}");

    sw.commit(logger);
}

From source file:com.eleven.rebind.SkinBundleGenerator.java

License:Apache License

private void generateImageMethod(final SkinBundleBuilder compositeImage, final SourceWriter sw,
        final JMethod method, final String imgResName) {

    String decl = method.getReadableDeclaration(false, true, true, true, true);

    {/*  ww w .jav  a 2 s  . c  om*/
        sw.indent();

        // Create a singleton that this method can return. There is no need
        // to
        // create a new instance every time this method is called, since
        // ClippedImagePrototype is immutable

        SkinBundleBuilder.ImageRect imageRect = compositeImage.getMapping(imgResName);
        String singletonName = method.getName() + "_SINGLETON";

        sw.print("private static final ClippedImagePrototype ");
        sw.print(singletonName);
        sw.print(" = new ClippedImagePrototype(IMAGE_BUNDLE_URL, ");
        sw.print(Integer.toString(imageRect.getLeft()));
        sw.print(", ");
        sw.print(Integer.toString(imageRect.getTop()));
        sw.print(", ");
        sw.print(Integer.toString(imageRect.getWidth()));
        sw.print(", ");
        sw.print(Integer.toString(imageRect.getHeight()));
        sw.println(");");

        sw.print(decl);
        sw.println(" {");
        sw.indent();
        sw.print("return ");
        sw.print(singletonName);
        sw.println(";");
        sw.outdent();

        sw.println("}");
        sw.outdent();
    }
}

From source file:com.eleven.rebind.SkinBundleGenerator.java

License:Apache License

private void generateImageDef(final SkinBundleBuilder compositeImage, final SourceWriter sw,
        final List<String> imgResNames) {
    sw.indent();//from  w  w  w.java 2 s  . com

    sw.println("private static final HashMap<String, ClippedImagePrototype> imageMap"
            + "= new HashMap<String, ClippedImagePrototype>() {");

    sw.indent();
    sw.println("{");

    sw.indent();

    for (String imgResName : imgResNames) {
        SkinBundleBuilder.ImageRect imageRect = compositeImage.getMapping(imgResName);

        sw.print("put (\"" + imgResName.replace(File.separatorChar, '/') + "\", ");
        sw.print("new ClippedImagePrototype(IMAGE_BUNDLE_URL, ");
        sw.print(Integer.toString(imageRect.getLeft()));
        sw.print(", ");
        sw.print(Integer.toString(imageRect.getTop()));
        sw.print(", ");
        sw.print(Integer.toString(imageRect.getWidth()));
        sw.print(", ");
        sw.print(Integer.toString(imageRect.getHeight()));
        sw.println("));");
    }

    sw.outdent();
    sw.println("}");

    sw.outdent();
    sw.println("};");

    sw.println("@Override");
    sw.println("public void prefetchAll() {");
    sw.indent();
    for (String imgResName : imgResNames)
        sw.println("Image.prefetch(\"" + imgResName.replace(File.separatorChar, '/') + "\");");

    sw.println("}");
    sw.outdent();

    sw.println("@Override");
    sw.println("public void prefetchBundle() {");
    sw.indent();
    sw.println("Image.prefetch(IMAGE_BUNDLE_URL);");
    sw.println("}");
    sw.outdent();
}