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:org.cruxframework.crux.plugin.bootstrap.rebind.font.FontResourceGenerator.java

License:Apache License

private void writeGetFontName(JMethod method, SourceWriter sw) {
    sw.println("public String getFontName() {");
    sw.indent();//from ww  w  .jav  a2  s.  c o m

    sw.print("return \"");
    sw.print(getFontName(method));
    sw.println("\";");
    sw.outdent();
    sw.println("}");
}

From source file:org.cruxframework.crux.plugin.bootstrap.rebind.font.FontResourceGenerator.java

License:Apache License

private void writeGetText(TreeLogger logger, ResourceContext context, JMethod method, SourceWriter sw)
        throws UnableToCompleteException {
    sw.println("public String getText() {");
    sw.indent();/* w w w .j a va2 s  .  c o  m*/

    URL[] urls = ResourceGeneratorUtil.findResources(logger, context, method);

    sw.print("return \"");
    sw.print("@font-face{");

    sw.print("font-family:'");
    sw.print(getFontName(method));
    sw.print("';");

    sw.print("font-style:normal;");
    sw.print("font-weight:400;");

    try {
        String agent = context.getGeneratorContext().getPropertyOracle()
                .getSelectionProperty(logger, "user.agent").getCurrentValue();
        if (agent.equals("ie6") || agent.equals("ie8"))
            writeSrcIE(logger, context, method, sw, urls);
        else
            writeSrc(logger, context, method, sw, urls);
    } catch (BadPropertyValueException e) {
        throw new UnableToCompleteException();
    }
    sw.print("}");

    sw.println("\";");

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

}

From source file:org.cruxframework.crux.plugin.bootstrap.rebind.font.FontResourceGenerator.java

License:Apache License

private void writeSrc(TreeLogger logger, ResourceContext context, JMethod method, SourceWriter sw, URL[] urls)
        throws UnableToCompleteException {
    for (URL url : urls) {
        String lower = url.getPath().toLowerCase();
        if (lower.endsWith(".ttf") || lower.endsWith(".otf")) {
            String outputUrlExpression = context.deploy(url, "application/x-font-ttf", false);

            sw.print("src:url('\" + ");
            sw.print(outputUrlExpression);
            sw.print(" + \"') format('truetype');");
        }/*from www.j  a va  2  s.  c o m*/
    }
}

From source file:org.cruxframework.crux.plugin.bootstrap.rebind.font.FontResourceGenerator.java

License:Apache License

private void writeSrcIE(TreeLogger logger, ResourceContext context, JMethod method, SourceWriter sw, URL[] urls)
        throws UnableToCompleteException {
    for (URL url : urls) {
        if (url.getPath().toLowerCase().endsWith(".eot")) {
            String outputUrlExpression = context.deploy(url, "application/vnd.ms-fontobject", false);

            sw.print("src:url('\" + ");
            sw.print(outputUrlExpression);
            sw.print(" + \"');");
        }// w w w  .  j av  a  2 s  .c  o m
    }
}

From source file:org.cruxframework.crux.plugin.gadget.rebind.gwt.UserPreferencesGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {

    // The TypeOracle knows about all types in the type system
    TypeOracle typeOracle = context.getTypeOracle();

    // Get a reference to the type that the generator should implement
    JClassType sourceType = typeOracle.findType(typeName);

    // Ensure that the requested type exists
    if (sourceType == null) {
        logger.log(TreeLogger.ERROR, "Could not find requested typeName", null);
        throw new UnableToCompleteException();
    }/*from   www. j  av  a2 s.  c  o m*/

    // Make sure the UserPreferences type is correctly defined
    validateType(logger, sourceType);

    // Pick a name for the generated class to not conflict.
    String generatedSimpleSourceName = sourceType.getSimpleSourceName() + "UserPreferencesImpl";

    // Begin writing the generated source.
    ClassSourceFileComposerFactory f = new ClassSourceFileComposerFactory(sourceType.getPackage().getName(),
            generatedSimpleSourceName);
    f.addImport(GWT.class.getName());
    f.addImport(PreferencesUtil.class.getName());
    f.addImplementedInterface(typeName);

    // All source gets written through this Writer
    PrintWriter out = context.tryCreate(logger, sourceType.getPackage().getName(), generatedSimpleSourceName);

    // If an implementation already exists, we don't need to do any work
    if (out != null) {
        JClassType preferenceType = typeOracle.findType(Preference.class.getName().replace('$', '.'));
        assert preferenceType != null;

        // We really use a SourceWriter since it's convenient
        SourceWriter sw = f.createSourceWriter(context, out);

        for (JMethod m : sourceType.getOverridableMethods()) {
            JClassType extendsPreferenceType = m.getReturnType().isClassOrInterface();
            if (extendsPreferenceType == null) {
                logger.log(TreeLogger.ERROR,
                        "Return type of " + m.getName() + " is not a class or interface (must be assignable to "
                                + preferenceType.getName() + ").",
                        null);
                throw new UnableToCompleteException();
            }

            if (!preferenceType.isAssignableFrom(extendsPreferenceType)) {
                logger.log(TreeLogger.ERROR, "Cannot assign " + extendsPreferenceType.getQualifiedSourceName()
                        + " to " + preferenceType.getQualifiedSourceName(), null);
                throw new UnableToCompleteException();
            }

            // private final FooProperty __propName = new FooProperty()
            // {...}
            sw.print("private final " + extendsPreferenceType.getParameterizedQualifiedSourceName() + " __"
                    + m.getName() + " = ");
            writeInstantiation(logger, sw, extendsPreferenceType, m);
            sw.println(";");

            // public FooProperty property() { return __property; }
            sw.print("public ");
            sw.print(m.getReadableDeclaration(true, true, true, true, true));
            sw.println("{");
            sw.indent();
            sw.println("return __" + m.getName() + ";");
            sw.outdent();
            sw.println("}");
        }

        sw.commit(logger);
    }

    return f.getCreatedClassName();
}

From source file:org.drools.guvnor.gwtutil.AssetEditorFactoryGenerator.java

License:Apache License

private void generateGetRegisteredAssetEditorFormatsMethod(SourceWriter sourceWriter,
        List<AssetEditorConfiguration> registeredEditors) {
    sourceWriter.println("public String[] getRegisteredAssetEditorFormats() {");
    sourceWriter.indent();/*from  w  ww.  j  av  a2s  . c o  m*/
    sourceWriter.println("String[] formats = new String[] {");
    int i = 0;
    for (AssetEditorConfiguration a : registeredEditors) {
        String format = a.getFormat();
        sourceWriter.print("\"" + format + "\"");
        if (i < registeredEditors.size() - 1) {
            sourceWriter.print(", ");
        }
        i++;
    }

    sourceWriter.println("};");
    sourceWriter.println("return formats;");
    sourceWriter.outdent();
    sourceWriter.println("}");
}

From source file:org.drools.guvnor.gwtutil.PerspectiveFactoryGenerator.java

License:Apache License

private void generateGetRegisteredModuleEditorFormatsMethod(SourceWriter sourceWriter,
        Map<String, List<ModuleEditorConfiguration>> registeredEditors) {
    sourceWriter.println("public String[] getRegisteredModuleEditorFormats(String perspectiveType) {");
    sourceWriter.indent();/*from  www  . j  a va  2s  .com*/

    for (String perspectiveType : registeredEditors.keySet()) {
        List<ModuleEditorConfiguration> moduleEditors = registeredEditors.get(perspectiveType);
        sourceWriter.println("if(\"" + perspectiveType + "\".equals(perspectiveType)) {");
        sourceWriter.indent();
        sourceWriter.println("String[] formats = new String[] {");
        int i = 0;
        for (ModuleEditorConfiguration a : moduleEditors) {
            String format = a.getFormat();
            sourceWriter.print("\"" + format + "\"");
            if (i < moduleEditors.size() - 1) {
                sourceWriter.print(", ");
            }
            i++;
        }

        sourceWriter.println("};");
        sourceWriter.println("return formats;");
        sourceWriter.outdent();
        sourceWriter.println("}");
    }

    sourceWriter.println("return null;");
    sourceWriter.outdent();
    sourceWriter.println("}");
}

From source file:org.gwt.beansbinding.core.rebind.BeanPropertyDescriptorGenerator.java

License:Apache License

private void write(SourceWriter w, JClassType _type, JClassType[] types) {
    w.println("public static void setupBeanInfo() throws " + IntrospectionException.class.getName());
    w.println("{");
    w.indent();//from   w  ww . j a va  2s. c  o  m
    w.println("GwtBeanInfo beanInfo;");
    for (JClassType type : types) {
        Collection<Property> properties = lookupJavaBeanPropertyAccessors(type);
        w.println("\n// " + type.getName());
        w.println("beanInfo = new GwtBeanInfo();");
        for (Property property : properties) {
            w.print("beanInfo.addPropertyDescriptor( ");
            writePropertyDescriptor(w, type, property.name, property.propertyType, property.getter,
                    property.setter);
            w.println(" );");
        }
        w.println("GwtIntrospector.setBeanInfo( " + type.getQualifiedSourceName() + ".class, beanInfo );");
    }
    w.outdent();
    w.println("}");

    // automatically invoke setupBeanInfo()
    w.println("static {");
    w.indent();
    w.println(
            "try{setupBeanInfo();}catch(Exception ex){com.google.gwt.user.client.Window.alert(ex.getMessage());}");
    w.outdent();
    w.println("}");
}

From source file:org.gwt.beansbinding.core.rebind.BeanPropertyDescriptorGenerator.java

License:Apache License

private void writePropertyDescriptor(SourceWriter sw, JClassType type, String propertyName, String propertyType,
        JMethod getter, JMethod setter) {
    sw.print("new PropertyDescriptor( \"" + propertyName + "\", " + propertyType + ".class, ");
    if (getter != null) {
        sw.println("new Method() ");
        sw.println("{");
        sw.indent();/* w w w .  j  a  v a2  s. co  m*/
        sw.println("public Object invoke( Object bean, Object... args )");
        sw.println("{");
        sw.indent();
        sw.println("return ( (" + type.getQualifiedSourceName() + ") bean)." + getter.getName() + "();");
        sw.outdent();
        sw.println("}");
        sw.outdent();
        sw.print("}, ");
    } else {
        sw.print("null, ");
    }
    if (setter != null) {
        sw.println("new Method() ");
        sw.println("{");
        sw.indent();
        sw.println("public Object invoke( Object bean, Object... args )");
        sw.println("{");
        sw.indent();
        JType argType = setter.getParameters()[0].getType().getErasedType();
        String argTypeName;
        if (argType.isPrimitive() != null) {
            argTypeName = argType.isPrimitive().getQualifiedBoxedSourceName();
        } else {
            argTypeName = argType.getQualifiedSourceName();
        }
        sw.println("( (" + type.getQualifiedSourceName() + ") bean)." + setter.getName() + "( (" + argTypeName
                + ") args[0] );");
        sw.println("return null;");
        sw.outdent();
        sw.println("}");
        sw.outdent();
        sw.print("} )");
    } else {
        sw.print("null )");
    }
}

From source file:org.gwt.json.serialization.InternalArraySerializerGenerator.java

License:Apache License

public String generateSerializers(SourceWriter writer) throws NotFoundException {
    String serializerName = jArrayType.getQualifiedSourceName().replaceAll("\\.", "\\$").replaceAll("\\[\\]",
            "_1") + "_SerializableImpl";
    writer.println("public class " + serializerName + " extends JsonSimpleTypeSerializer<"
            + jArrayType.getQualifiedSourceName() + "> { ");
    writer.indent();/*from   ww  w.  jav a 2s  .co m*/
    writer.println("public " + serializerName + "(){}");

    writer.println("public JSONValue serialize(" + jArrayType.getQualifiedSourceName() + " obj) {");
    writer.indent();
    writer.println("if (obj == null) return JSONNull.getInstance();");
    writer.println("JSONArray jsonArray = new JSONArray();");
    writer.println("for (int i=0; i< obj.length; i++) {");
    writer.indent();
    JType componentType = jArrayType.getComponentType();
    String fieldType = GenericGenerationUtils.resolveType(componentType);
    if (componentType.isArray() != null) {
        writer.println(
                "jsonArray.set(i, getSerializerForType(\"" + fieldType + "\").serialize(obj[i], null));");
        registerArraySerializer(componentType.isArray());
    } else {
        JParameterizedType parametrizedType = componentType.isParameterized();
        writer.println("jsonArray.set(i, getSerializerForType(\"" + fieldType + "\").serialize(obj[i], "
                + GenericGenerationUtils.getGenericTypesParam(componentType,
                        parametrizedType != null ? parametrizedType.getTypeArgs() : null)
                + "));");
    }
    writer.outdent();
    writer.println("}");

    writer.println("return jsonArray;");
    writer.outdent();
    writer.println("} ");
    writer.println();

    writer.println("public " + jArrayType.getQualifiedSourceName()
            + " deSerialize(JSONValue jsonObj) throws JSONException {");
    writer.indent();

    writer.println("if (jsonObj == null) return null;");

    writer.println("JSONArray arr = jsonObj.isArray();");
    writer.println(jArrayType.getQualifiedSourceName() + " response = new "
            + insertAsFirstArray(jArrayType.getComponentType().getQualifiedSourceName(), "[arr.size()]") + ";");
    writer.println("JsonGenericTypeSerializer<" + fieldType + "> serializer = getSerializerForType(\""
            + fieldType + "\");");
    writer.println("for (int i=0; i<arr.size(); i++) {");
    writer.indent();
    if (componentType.isArray() != null) {
        writer.print("response[i] = serializer.deSerialize(arr.get(i), null);");
        registerArraySerializer(componentType.isArray());
    } else {
        JParameterizedType parametrizedType = componentType.isParameterized();
        writer.print("response[i] = serializer.deSerialize(arr.get(i), "
                + GenericGenerationUtils.getGenericTypesParam(componentType,
                        parametrizedType != null ? parametrizedType.getTypeArgs() : null)
                + ");");
    }
    writer.outdent();
    writer.println("}");
    writer.println("return response;");
    writer.outdent();
    writer.println("}");
    writer.outdent();
    writer.println("}");
    return serializerName;
}