Example usage for com.google.gwt.core.ext SelectionProperty getPossibleValues

List of usage examples for com.google.gwt.core.ext SelectionProperty getPossibleValues

Introduction

In this page you can find the example usage for com.google.gwt.core.ext SelectionProperty getPossibleValues.

Prototype

SortedSet<String> getPossibleValues();

Source Link

Document

Returns the possible values for the property in sorted order.

Usage

From source file:com.googlecode.mgwt.useragent.rebind.UserAgentGenerator.java

License:Apache License

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle typeOracle = context.getTypeOracle();

    JClassType userType;// www.  j  a va  2s  .  c o  m
    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();
    }

    PropertyOracle propertyOracle = context.getPropertyOracle();

    String userAgentValue;
    SelectionProperty selectionProperty;
    try {
        selectionProperty = propertyOracle.getSelectionProperty(logger, PROPERTY_USER_AGENT);
        userAgentValue = selectionProperty.getCurrentValue();
    } catch (BadPropertyValueException e) {
        logger.log(TreeLogger.ERROR, "Unable to find value for '" + PROPERTY_USER_AGENT + "'", e);
        throw new UnableToCompleteException();
    }

    String userAgentValueInitialCap = userAgentValue.substring(0, 1).toUpperCase(Locale.ENGLISH)
            + userAgentValue.substring(1);
    className = className + "Impl" + userAgentValueInitialCap;

    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className);
    composerFactory.addImplementedInterface(userType.getQualifiedSourceName());

    PrintWriter pw = context.tryCreate(logger, packageName, className);
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);

        sw.println();
        sw.println("public native String getRuntimeValue() /*-{");
        sw.indent();
        UserAgentPropertyGenerator.writeUserAgentPropertyJavaScript(sw, selectionProperty.getPossibleValues(),
                null);
        sw.outdent();
        sw.println("}-*/;");
        sw.println();

        sw.println();
        sw.println("public String getCompileTimeValue() {");
        sw.indent();
        sw.println("return \"" + userAgentValue.trim() + "\";");
        sw.outdent();
        sw.println("}");

        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}

From source file:com.sencha.gxt.core.rebind.BindingPropertyGenerator.java

License:sencha.com license

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    TypeOracle oracle = context.getTypeOracle();

    JClassType toGenerate = oracle.findType(typeName).isInterface();
    if (toGenerate == null) {
        logger.log(Type.ERROR, typeName + " is not an interface");
        throw new UnableToCompleteException();
    }//from w  w w. j  a  v a2  s  .c om

    PropertyName annotation = toGenerate.getAnnotation(PropertyName.class);
    if (annotation == null) {
        logger.log(Type.ERROR, "Cannot generate with a @PropertyName anntation on the type");
        throw new UnableToCompleteException();
    }

    String propertyName = annotation.value();
    SelectionProperty property;
    String value;
    try {
        property = context.getPropertyOracle().getSelectionProperty(logger, propertyName);
        value = property.getCurrentValue();
    } catch (BadPropertyValueException e) {
        logger.log(Type.ERROR, "Error occured loading property: ", e);
        throw new UnableToCompleteException();
    }
    String packageName = toGenerate.getPackage().getName();
    String simpleSourceName = toGenerate.getName().replace('.', '_') + "_" + value;
    PrintWriter pw = context.tryCreate(logger, packageName, simpleSourceName);
    if (pw == null) {
        return packageName + "." + simpleSourceName;
    }

    ClassSourceFileComposerFactory factory = new ClassSourceFileComposerFactory(packageName, simpleSourceName);
    factory.addImplementedInterface(typeName);
    SourceWriter sw = factory.createSourceWriter(context, pw);

    for (JMethod method : toGenerate.getMethods()) {
        if (method.getReturnType().isPrimitive() != JPrimitiveType.BOOLEAN && !method.getReturnType().isClass()
                .getQualifiedSourceName().equals(Name.getSourceNameForClass(Boolean.class))) {
            logger.log(Type.ERROR, "Methods must return boolean or Boolean");
            throw new UnableToCompleteException();
        }
        sw.println("%1$s {", method.getReadableDeclaration(false, true, true, true, true));

        PropertyValue val = method.getAnnotation(PropertyValue.class);
        if (val == null) {
            logger.log(Type.ERROR, "Method must have a @PropertyValue annotation");
            throw new UnableToCompleteException();
        }

        if (!property.getPossibleValues().contains(val.value()) && val.warn()) {
            logger.log(Type.WARN,
                    "Value '" + val
                            + "' is not present in the current set of possible values for selection property "
                            + propertyName);
        }
        sw.indentln("return %1$b;", val.value().equals(value));

        sw.println("}");
    }

    sw.commit(logger);

    return factory.getCreatedClassName();
}