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

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

Introduction

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

Prototype

void outdent();

Source Link

Usage

From source file:org.metawidget.gwt.generator.widgetprocessor.binding.simple.SimpleBindingProcessorAdapterGenerator.java

License:LGPL

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

    // Lookup the type

    mTypeOracle = context.getTypeOracle();
    JClassType classType;//  www . j  av a 2  s .  c  om

    try {
        classType = mTypeOracle.getType(typeName);
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }

    String packageName = classType.getPackage().getName();
    String sourceClassName = classType.getSimpleSourceName();
    String bindingClassName = sourceClassName + "BindingAdapter";
    PrintWriter printWriter = context.tryCreate(logger, packageName, bindingClassName);

    // Already generated?

    String qualifiedBindingClassName = packageName + StringUtils.SEPARATOR_DOT_CHAR + bindingClassName;

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

    // Start the BindingAdapter subclass

    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, bindingClassName);
    composer.addImplementedInterface(
            SimpleBindingProcessorAdapter.class.getName() + "<" + classType.getQualifiedSourceName() + ">");
    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);

    if (sourceWriter != null) {
        String variableName = VARIABLE_NAME_PREFIX + sourceClassName;
        sourceWriter.println();
        sourceWriter.println("// Public methods");

        // getProperty method

        sourceWriter.println();
        sourceWriter.println("public Object getProperty( " + classType.getQualifiedSourceName() + " "
                + variableName + ", String... names ) {");
        sourceWriter.indent();
        writeMethod(sourceWriter, classType, variableName, WRITE_GETTER);
        sourceWriter.outdent();
        sourceWriter.println("}");

        // getPropertyType method

        sourceWriter.println();
        sourceWriter.println("public Class<?> getPropertyType( " + classType.getQualifiedSourceName() + " "
                + variableName + ", String... names ) {");
        sourceWriter.indent();
        writeMethod(sourceWriter, classType, variableName, WRITE_TYPE_GETTER);
        sourceWriter.outdent();
        sourceWriter.println("}");

        // setProperty method

        sourceWriter.println();
        sourceWriter.println("public void setProperty( " + classType.getQualifiedSourceName() + " "
                + variableName + ", Object value, String... names ) {");
        sourceWriter.indent();
        writeMethod(sourceWriter, classType, variableName, WRITE_SETTER);
        sourceWriter.outdent();
        sourceWriter.println("}");

        // invokeAction method

        sourceWriter.println();
        sourceWriter.println("public void invokeAction( " + classType.getQualifiedSourceName() + " "
                + variableName + ", String... names ) {");
        sourceWriter.indent();
        writeMethod(sourceWriter, classType, variableName, WRITE_ACTION);
        sourceWriter.outdent();
        sourceWriter.println("}");

        // End the BindingAdapter subclass

        sourceWriter.commit(logger);
    }

    return qualifiedBindingClassName;
}

From source file:org.metawidget.gwt.generator.widgetprocessor.binding.simple.SimpleBindingProcessorAdapterGenerator.java

License:LGPL

private void writeProperties(SourceWriter sourceWriter, JClassType classType, String variableName,
        int nameIndex, boolean writeInstanceOf, JClassType parentType, int writeType, int depth) {

    String currentVariableName = variableName;
    boolean writtenAProperty = false;

    for (JMethod method : classType.getMethods()) {
        // ...if the method is public...

        if (!method.isPublic()) {
            continue;
        }//from ww w .  j a va  2  s  .  c  om

        String methodName = method.getName();
        JType returnType = method.getReturnType();

        // ...and follows the action convention...

        if (JPrimitiveType.VOID.equals(returnType)) {
            if (writeType == WRITE_ACTION) {
                if (method.getParameters().length == 0) {
                    sourceWriter.println("if ( \"" + methodName + "\".equals( names[" + nameIndex + "] )) { "
                            + currentVariableName + StringUtils.SEPARATOR_DOT_CHAR + methodName
                            + "(); return; }");
                }
            }

            continue;
        }

        // ...or follows the JavaBean convention...

        String propertyName;

        if (methodName.startsWith(ClassUtils.JAVABEAN_GET_PREFIX)) {
            propertyName = methodName.substring(ClassUtils.JAVABEAN_GET_PREFIX.length());
        } else if (methodName.startsWith(ClassUtils.JAVABEAN_IS_PREFIX)
                && "boolean".equals(method.getReturnType().getQualifiedSourceName())) {

            // As per section 8.3.2 (Boolean properties) of The JavaBeans API specification, 'is'
            // only applies to boolean (little 'b')

            propertyName = methodName.substring(ClassUtils.JAVABEAN_IS_PREFIX.length());
        } else {
            continue;
        }

        String decapitalizedPropertyName = StringUtils.decapitalize(propertyName);

        // Open the block

        if (!writtenAProperty) {
            sourceWriter.println();
            sourceWriter.println("// " + classType.getSimpleSourceName() + " properties");
            sourceWriter.println();

            if (writeInstanceOf) {
                sourceWriter
                        .println("if ( " + currentVariableName + " instanceof " + classType.getName() + " ) {");
                sourceWriter.indent();
                String superclassVariableName = currentVariableName;
                currentVariableName = VARIABLE_NAME_PREFIX + classType.getSimpleSourceName();
                sourceWriter.println(classType.getParameterizedQualifiedSourceName() + " " + currentVariableName
                        + " = (" + classType.getParameterizedQualifiedSourceName() + ") "
                        + superclassVariableName + ";");
            }

            writtenAProperty = true;
        }

        // ...call our adaptee...

        sourceWriter
                .println("if ( \"" + decapitalizedPropertyName + "\".equals( names[" + nameIndex + "] )) {");
        sourceWriter.indent();

        int nextNameIndex = nameIndex + 1;

        // ...recursively if the return type is within our own package...

        JClassType nestedClassType = returnType.isClass();

        if (nestedClassType != null
                && nestedClassType.getPackage().getName().startsWith(parentType.getPackage().getName())) {
            String nestedVariableName = VARIABLE_NAME_PREFIX + propertyName;

            if (depth > 0) {
                nestedVariableName += (depth + 1);
            }

            sourceWriter.println(
                    nestedClassType.getParameterizedQualifiedSourceName() + " " + nestedVariableName + " = "
                            + currentVariableName + StringUtils.SEPARATOR_DOT_CHAR + methodName + "();");

            switch (writeType) {
            case WRITE_GETTER:
                sourceWriter.println(
                        "if ( names.length == " + nextNameIndex + " ) return " + nestedVariableName + ";");
                break;

            case WRITE_TYPE_GETTER:
                sourceWriter.println("if ( names.length == " + nextNameIndex + " ) return "
                        + getWrapperType(returnType).getQualifiedSourceName() + ".class;");
                break;

            case WRITE_SETTER:
                try {
                    String setterMethodName = "set" + propertyName;
                    classType.getMethod(setterMethodName, new JType[] { returnType });
                    sourceWriter.println("if ( names.length == " + nextNameIndex + " ) { " + currentVariableName
                            + StringUtils.SEPARATOR_DOT_CHAR + setterMethodName + "( ("
                            + getWrapperType(returnType).getParameterizedQualifiedSourceName()
                            + ") value ); return; }");
                } catch (NotFoundException e) {
                    sourceWriter.println("if ( names.length == " + nextNameIndex
                            + " ) throw new RuntimeException( \"No setter for name '"
                            + decapitalizedPropertyName + "'\" );");
                }
                break;

            case WRITE_ACTION:
                break;

            default:
                throw new UnsupportedOperationException(String.valueOf(writeType));
            }

            writeSubtypes(sourceWriter, nestedClassType, nestedVariableName, nextNameIndex, writeType,
                    depth + 1);
            sourceWriter.outdent();
            sourceWriter.println("}");
            continue;
        }

        // ...or non-recursively for other types (eg. boolean, Date, Class)

        sourceWriter.println("if ( names.length > " + nextNameIndex
                + " ) throw new RuntimeException( \"Cannot traverse into property '" + decapitalizedPropertyName
                + ".\" + names[" + nextNameIndex + "] + \"'\" );");

        switch (writeType) {
        case WRITE_GETTER:
            sourceWriter.println(
                    "return " + currentVariableName + StringUtils.SEPARATOR_DOT_CHAR + methodName + "();");
            break;

        case WRITE_TYPE_GETTER:
            sourceWriter.println("return " + getWrapperType(returnType).getQualifiedSourceName() + ".class;");
            break;

        case WRITE_SETTER:
            try {
                String setterMethodName = "set" + propertyName;
                classType.getMethod(setterMethodName, new JType[] { returnType });
                sourceWriter.println(currentVariableName + StringUtils.SEPARATOR_DOT_CHAR + setterMethodName
                        + "( (" + getWrapperType(returnType).getParameterizedQualifiedSourceName()
                        + ") value );");
                sourceWriter.println("return;");
            } catch (NotFoundException e) {
                sourceWriter.println("throw new RuntimeException( \"No setter for property '"
                        + decapitalizedPropertyName + "'\" );");
            }
            break;

        case WRITE_ACTION:
            sourceWriter.println("if ( names.length == " + nextNameIndex
                    + " ) throw new RuntimeException( \"Cannot execute '" + decapitalizedPropertyName
                    + "' - is a property, not an action\" );");
            break;

        default:
            throw new UnsupportedOperationException(String.valueOf(writeType));
        }

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

    // Close the block

    if (writtenAProperty && writeInstanceOf) {
        sourceWriter.outdent();
        sourceWriter.println("}");
    }
}

From source file:org.opencms.gwt.rebind.CmsClassInitGenerator.java

License:Open Source License

/**
 * This method generates the source code for the class initializer class.<p>
 * /*from   w w w.ja  va 2s . com*/
 * @param logger the logger to be used 
 * @param context the generator context
 * @param subclasses the classes for which the generated code should the initClass() method 
 */
public void generateClass(TreeLogger logger, GeneratorContext context, List<JClassType> subclasses) {

    PrintWriter printWriter = context.tryCreate(logger, PACKAGE_NAME, CLASS_NAME);
    if (printWriter == null) {
        return;
    }
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(PACKAGE_NAME, CLASS_NAME);
    composer.addImplementedInterface(INIT_INTERFACE_NAME);
    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
    sourceWriter.println("public void initClasses() {");
    sourceWriter.indent();
    for (JClassType type : subclasses) {
        sourceWriter.println(type.getQualifiedSourceName() + ".initClass();");
    }
    sourceWriter.outdent();
    sourceWriter.println("}");
    sourceWriter.outdent();
    sourceWriter.println("}");
    context.commit(logger, printWriter);
}

From source file:org.opencms.gwt.rebind.CmsCommandInitGenerator.java

License:Open Source License

/**
 * This method generates the source code for the class initializer class.<p>
 * /*from w  ww  .j  a  v  a2s . c om*/
 * @param logger the logger to be used 
 * @param context the generator context
 * @param subclasses the classes for which the generated code should the initClass() method 
 */
public void generateClass(TreeLogger logger, GeneratorContext context, List<JClassType> subclasses) {

    PrintWriter printWriter = context.tryCreate(logger, PACKAGE_NAME, CLASS_NAME);
    if (printWriter == null) {
        return;
    }
    ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(PACKAGE_NAME, CLASS_NAME);
    composer.addImplementedInterface(INIT_INTERFACE_NAME);
    SourceWriter sourceWriter = composer.createSourceWriter(context, printWriter);
    sourceWriter.println("public java.util.Map<String, " + COMMAND_INTERFACE + "> initCommands() {");
    sourceWriter.indent();
    sourceWriter.println("java.util.Map<String, " + COMMAND_INTERFACE
            + "> result=new java.util.HashMap<String, " + COMMAND_INTERFACE + ">();");
    for (JClassType type : subclasses) {
        sourceWriter.println("result.put(\"" + type.getQualifiedSourceName() + "\","
                + type.getQualifiedSourceName() + "." + GET_COMMAND_METHOD + "());");
    }
    sourceWriter.println("return result;");
    sourceWriter.outdent();
    sourceWriter.println("}");
    sourceWriter.outdent();
    sourceWriter.println("}");
    context.commit(logger, printWriter);
}

From source file:org.pentaho.mantle.rebind.CommandExecGenerator.java

License:Open Source License

private void generateClass(TreeLogger logger, GeneratorContext context) {

    // get print writer that receives the source code
    PrintWriter printWriter = null;
    printWriter = context.tryCreate(logger, packageName, className);
    // print writer if null, source code has ALREADY been generated, return
    if (printWriter == null) {
        return;/*from w  ww . ja  v a 2 s . c  o m*/
    }

    // init composer, set class properties, create source writer
    ClassSourceFileComposerFactory composer = null;
    composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImplementedInterface(CommandExec.class.getName());
    composer.addImport(SolutionBrowserPanel.class.getName());
    composer.addImport(JavaScriptObject.class.getName());
    composer.addImport(Command.class.getName());

    SourceWriter sourceWriter = null;
    sourceWriter = composer.createSourceWriter(context, printWriter);

    sourceWriter.indent();

    // generator constructor source code
    generateConstructor(sourceWriter);
    generateMethods(sourceWriter);

    // close generated class
    sourceWriter.outdent();
    sourceWriter.println("}");

    // commit generated class
    context.commit(logger, printWriter);
}

From source file:org.pentaho.mantle.rebind.CommandExecGenerator.java

License:Open Source License

private void generateMethods(SourceWriter sourceWriter) {
    sourceWriter.println();/*from www .ja  va2s  .c  om*/
    sourceWriter.println(
            "public native String getParameterString(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println(
            "public native Integer getParameterInteger(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println(
            "public native Boolean getParameterBoolean(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println("public native Float getParameterFloat(final String paramName,"
            + "final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println("public native Double getParameterDouble(final String paramName, "
            + "final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println(
            "public native Long getParameterLong(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println("public native Short getParameterShort(final String paramName,"
            + "final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter
            .println("public void execute(final String commandName, final JavaScriptObject parameterMap) { ");
    sourceWriter.indent();

    try {
        // find Command implementors
        ArrayList<JClassType> implementingTypes = new ArrayList<JClassType>();
        JPackage pack = typeOracle.getPackage(CommandExec.class.getPackage().getName());
        JClassType commandSourceType = typeOracle.getType(Command.class.getName());

        for (JClassType type : pack.getTypes()) {
            if (type.isAssignableTo(commandSourceType)) {
                implementingTypes.add(type);
            }
        }

        sourceWriter.println("if(false){}"); // placeholder
        for (JClassType implementingType : implementingTypes) {
            if (implementingType.isAbstract()) {
                continue;
            }
            sourceWriter.println(
                    "else if(commandName.equals(\"" + implementingType.getSimpleSourceName() + "\")){");
            sourceWriter.indent();
            sourceWriter.println(
                    implementingType.getName() + " command = new " + implementingType.getName() + "();");
            for (JMethod eventMethod : implementingType.getMethods()) {
                if (eventMethod.isPublic() && !eventMethod.isStatic() && eventMethod.isConstructor() == null
                        && eventMethod.getName().startsWith("set")) {
                    String propertyName = eventMethod.getName().substring(3);
                    propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
                    String simpleType = implementingType.getField(propertyName).getType().getSimpleSourceName();
                    if ("string".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("command." + eventMethod.getName() + "(getParameterString(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("integer".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("command." + eventMethod.getName() + "(getParameterInteger(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("float".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("command." + eventMethod.getName() + "(getParameterFloat(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("double".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("command." + eventMethod.getName() + "(getParameterDouble(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("long".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("command." + eventMethod.getName() + "(getParameterLong(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("short".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("command." + eventMethod.getName() + "(getParameterShort(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("boolean".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("command." + eventMethod.getName() + "(getParameterBoolean(\""
                                + propertyName + "\", parameterMap));");
                    }
                }
            }

            sourceWriter.println("command.execute();");
            sourceWriter.outdent();
            sourceWriter.println("}");
        }

    } catch (Exception e) {
        // record to logger that Map generation threw an exception
        logger.log(TreeLogger.ERROR, "Error generating BindingContext!!!", e);
    }
    sourceWriter.outdent();
    sourceWriter.println("}");
}

From source file:org.pentaho.mantle.rebind.CommandExecGenerator.java

License:Open Source License

private void generateConstructor(SourceWriter sourceWriter) {
    // start constructor source generation
    sourceWriter.println("public " + className + "() { ");
    sourceWriter.indent();//from w w w .ja va  2  s.  c o  m
    sourceWriter.println("super();");

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

From source file:org.pentaho.mantle.rebind.EventBusUtilGenerator.java

License:Open Source License

private void generateClass(TreeLogger logger, GeneratorContext context) {

    // get print writer that receives the source code
    PrintWriter printWriter = null;
    printWriter = context.tryCreate(logger, packageName, className);
    // print writer if null, source code has ALREADY been generated, return
    if (printWriter == null) {
        return;/*from  www.  j a v a2s.co  m*/
    }

    // init composer, set class properties, create source writer
    ClassSourceFileComposerFactory composer = null;
    composer = new ClassSourceFileComposerFactory(packageName, className);
    composer.addImplementedInterface(EventBusUtil.class.getName());
    composer.addImport(GwtEvent.class.getName());
    composer.addImport(JavaScriptObject.class.getName());
    composer.addImport(EventBus.class.getName());
    composer.addImport(HandlerRegistration.class.getName());

    SourceWriter sourceWriter = null;
    sourceWriter = composer.createSourceWriter(context, printWriter);

    sourceWriter.indent();

    // generator constructor source code
    generateConstructor(sourceWriter);
    generateMethods(sourceWriter);

    // close generated class
    sourceWriter.outdent();
    sourceWriter.println("}");

    // commit generated class
    context.commit(logger, printWriter);
}

From source file:org.pentaho.mantle.rebind.EventBusUtilGenerator.java

License:Open Source License

private void generateMethods(SourceWriter sourceWriter) {

    sourceWriter.println(/* w w  w.  ja v a 2  s.co  m*/
            "public native void invokeEventBusJSO(final JavaScriptObject jso, final String parameterJSON)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    // unfortunately, we can obtain pairs like {"a":"undefined"} or even {"a":undefined}
    // the latter will crash the procedure, so let's try to clean up the string
    // by removing pairs with 'undefined'
    sourceWriter.println("var tmp = parameterJSON"
            // replace ("a":undefined) and ("a":"undefined") with empty space
            + ".replace(/\\\"[\\w]+\\\"\\:[ ]*(\\\")?undefined(\\\")?(,)?/g, '')"
            // remove doubled commas
            + ".replace(/,[ ]*,/, ',')"
            // remove leading and trailing commas
            + ".replace(/\\{[ ]*,/, '{')" + ".replace(/,[ ]*\\}/, '}');");
    sourceWriter.println("var p = JSON.parse(tmp);");
    sourceWriter.println("jso.call(this, p)");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println(
            "public native String getParameterString(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println(
            "public native Integer getParameterInteger(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println(
            "public native Boolean getParameterBoolean(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println("public native Float getParameterFloat(final String paramName, "
            + "final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println(
            "public native Double getParameterDouble(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println(
            "public native Long getParameterLong(final String paramName, final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    sourceWriter.println();
    sourceWriter.println("public native Short getParameterShort(final String paramName, "
            + "final JavaScriptObject parameterMap)");
    sourceWriter.println("/*-{");
    sourceWriter.indent();
    sourceWriter.println("return parameterMap[paramName];");
    sourceWriter.outdent();
    sourceWriter.println("}-*/;");

    // *********************
    // FIRE EVENT
    // *********************
    sourceWriter.println();
    sourceWriter
            .println("public void fireEvent(final String eventType, final JavaScriptObject parameterMap) { ");
    sourceWriter.indent();
    try {
        // find Command implementors
        ArrayList<JClassType> implementingTypes = new ArrayList<JClassType>();
        JPackage pack = typeOracle.getPackage(EventBusUtil.class.getPackage().getName());
        JClassType eventSourceType = typeOracle.getType(GwtEvent.class.getName());

        for (JClassType type : pack.getTypes()) {
            if (type.isAssignableTo(eventSourceType)) {
                implementingTypes.add(type);
            }
        }

        sourceWriter.println("if(false){}"); // placeholder
        for (JClassType implementingType : implementingTypes) {
            sourceWriter
                    .println("else if(eventType.equals(\"" + implementingType.getSimpleSourceName() + "\")){");
            sourceWriter.indent();
            sourceWriter
                    .println(implementingType.getName() + " event = new " + implementingType.getName() + "();");
            for (JMethod eventMethod : implementingType.getMethods()) {
                if (eventMethod.isPublic() && !eventMethod.isStatic() && eventMethod.isConstructor() == null
                        && eventMethod.getName().startsWith("set")) {
                    String propertyName = eventMethod.getName().substring(3);
                    propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
                    String simpleType = implementingType.getField(propertyName).getType().getSimpleSourceName();
                    if ("string".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("event." + eventMethod.getName() + "(getParameterString(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("integer".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("event." + eventMethod.getName() + "(getParameterInteger(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("float".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("event." + eventMethod.getName() + "(getParameterFloat(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("double".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("event." + eventMethod.getName() + "(getParameterDouble(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("long".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("event." + eventMethod.getName() + "(getParameterLong(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("short".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("event." + eventMethod.getName() + "(getParameterShort(\""
                                + propertyName + "\", parameterMap));");
                    } else if ("boolean".equalsIgnoreCase(simpleType)) {
                        sourceWriter.println("event." + eventMethod.getName() + "(getParameterBoolean(\""
                                + propertyName + "\", parameterMap));");
                    }
                }
            }

            sourceWriter.println("EVENT_BUS.fireEvent(event);");
            sourceWriter.outdent();
            sourceWriter.println("}");
        }

    } catch (Exception e) {
        // record to logger that Map generation threw an exception
        logger.log(TreeLogger.ERROR, "Error generating BindingContext!!!", e);
    }
    sourceWriter.outdent();
    sourceWriter.println("}");

    // *********************
    // ADD HANDLER
    // *********************
    sourceWriter.println();
    sourceWriter.println(
            "public HandlerRegistration addHandler(final String eventType, final JavaScriptObject handler) { ");
    sourceWriter.indent();
    try {
        // find Command implementors
        JPackage pack = typeOracle.getPackage(EventBusUtil.class.getPackage().getName());
        JClassType eventSourceType = typeOracle.getType(GwtEvent.class.getName());

        sourceWriter.println("if(false){return null;}"); // placeholder
        for (JClassType type : pack.getTypes()) {
            if (type.isAssignableTo(eventSourceType)) {
                addHandlerElseCondition(sourceWriter, type);
            }
        }
    } catch (Exception e) {
        // record to logger that Map generation threw an exception
        logger.log(TreeLogger.ERROR, "Error generating BindingContext!!!", e);
    }

    sourceWriter.indent();
    sourceWriter.println("return null;");

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

From source file:org.pentaho.mantle.rebind.EventBusUtilGenerator.java

License:Open Source License

private void addHandlerElseCondition(SourceWriter writer, JClassType type) throws NotFoundException {
    writer.println("else if(eventType.equals(\"" + type.getSimpleSourceName() + "\")){");
    writer.indent();//www .  j ava2s . c  o m

    JClassType handlerType = typeOracle
            .getType(EventBusUtil.class.getPackage().getName() + "." + type.getName() + "Handler");
    writer.println("HandlerRegistration handlerRegistration = EVENT_BUS.addHandler(" + type.getName()
            + ".TYPE, new " + type.getName() + "Handler() {");
    writer.indent();
    for (JMethod handlerMethod : handlerType.getMethods()) {

        String parameterJSON = addHandlerParamJson(type);

        writer.println("public void " + handlerMethod.getName() + "(" + type.getName() + " event) {");
        writer.indent();
        writer.println("invokeEventBusJSO(handler," + parameterJSON + ");");
        writer.outdent();
        writer.println("}");
    }
    writer.outdent();
    writer.println("});");

    writer.indent();
    writer.println("return handlerRegistration;");

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