Search in sources :

Example 31 with Tag

use of com.codename1.rad.models.Tag in project CodeRAD by shannah.

the class ResultParserTest method manualJSONTest.

private void manualJSONTest() throws Exception {
    String jsonData = "{\n" + "  \"colors\": [\n" + "    {\n" + "      \"color\": \"black\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,255,255,1],\n" + "        \"hex\": \"#000\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"white\",\n" + "      \"category\": \"value\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,0,0,1],\n" + "        \"hex\": \"#FFF\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"red\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,0,0,1],\n" + "        \"hex\": \"#FF0\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"blue\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,0,255,1],\n" + "        \"hex\": \"#00F\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"yellow\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,255,0,1],\n" + "        \"hex\": \"#FF0\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"green\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"secondary\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,255,0,1],\n" + "        \"hex\": \"#0F0\"\n" + "      }\n" + "    },\n" + "  ]\n" + "}";
    class Color extends BaseEntity {
    }
    Tag type = new Tag("type");
    Tag red = new Tag("red"), green = new Tag("green"), blue = new Tag("blue"), alpha = new Tag("alpha");
    EntityType colorType = entityTypeBuilder(Color.class).string(Thing.name).string(Product.category).string(type).Integer(red).Integer(green).Integer(blue).Integer(alpha).factory(cls -> {
        return new Color();
    }).build();
    class Colors extends EntityList<Color> {
    }
    EntityType.registerList(Colors.class, Color.class, cls -> {
        return new Colors();
    });
    Tag colors = new Tag("colors");
    class ColorSet extends BaseEntity {
    }
    EntityType colorsetType = entityTypeBuilder(ColorSet.class).list(Colors.class, colors).factory(cls -> {
        return new ColorSet();
    }).build();
    ResultParser parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).Integer("code/rgba[0]", red).Integer("code/rgba[1]", green).Integer("code/rgba[2]", blue).Integer("code/rgba[3]", alpha);
    ColorSet colorSet = (ColorSet) parser.parseJSON(jsonData, new ColorSet());
    Colors theColors = (Colors) colorSet.get(colors);
    assertEqual(6, theColors.size());
    assertEqual(6, colorSet.getEntityList(colors).size());
    assertEqual("black", theColors.get(0).getText(Thing.name));
    assertEqual("hue", theColors.get(0).getText(Product.category));
    assertEqual("primary", theColors.get(0).getText(type));
    assertEqual(255, (int) theColors.get(0).getInt(red));
    assertEqual(255, (int) theColors.get(0).getInt(green));
    assertEqual(255, (int) theColors.get(0).getInt(blue));
    assertEqual(1, (int) theColors.get(0).getInt(alpha));
    assertEqual("green", theColors.get(5).getText(Thing.name));
    assertEqual(0, (int) theColors.get(5).getInt(red));
    assertEqual(255, (int) theColors.get(5).getInt(green));
    assertEqual(0, (int) theColors.get(5).getInt(blue));
    assertEqual(1, (int) theColors.get(5).getInt(alpha));
    class CodeParser implements PropertyParserCallback {

        private int index;

        CodeParser(int index) {
            this.index = index;
        }

        @Override
        public Object parse(Object codeMap) {
            if (!(codeMap instanceof Map)) {
                return 0;
            }
            Map m = (Map) codeMap;
            List rgba = (List) m.get("rgba");
            if (rgba == null) {
                return 0;
            }
            if (index < 0 || index >= rgba.size()) {
                return 0;
            }
            return rgba.get(index);
        }
    }
    parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).property("code", red, new CodeParser(0)).property("code", green, new CodeParser(1)).property("code", blue, new CodeParser(2)).property("code", alpha, new CodeParser(3));
    colorSet = (ColorSet) parser.parseJSON(jsonData, new ColorSet());
    theColors = (Colors) colorSet.get(colors);
    assertEqual(6, theColors.size());
    assertEqual(6, colorSet.getEntityList(colors).size());
    assertEqual("black", theColors.get(0).getText(Thing.name));
    assertEqual("hue", theColors.get(0).getText(Product.category));
    assertEqual("primary", theColors.get(0).getText(type));
    assertEqual(255, (int) theColors.get(0).getInt(red));
    assertEqual(255, (int) theColors.get(0).getInt(green));
    assertEqual(255, (int) theColors.get(0).getInt(blue));
    assertEqual(1, (int) theColors.get(0).getInt(alpha));
    assertEqual("green", theColors.get(5).getText(Thing.name));
    assertEqual(0, (int) theColors.get(5).getInt(red));
    assertEqual(255, (int) theColors.get(5).getInt(green));
    assertEqual(0, (int) theColors.get(5).getInt(blue));
    assertEqual(1, (int) theColors.get(5).getInt(alpha));
}
Also used : AbstractTest(com.codename1.testing.AbstractTest) Person(com.codename1.rad.schemas.Person) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) BaseEntity.entityTypeBuilder(com.codename1.rad.models.BaseEntity.entityTypeBuilder) Element(com.codename1.xml.Element) Log(com.codename1.io.Log) ParseException(com.codename1.l10n.ParseException) com.codename1.rad.models(com.codename1.rad.models) PropertyParserCallback(com.codename1.rad.io.ResultParser.PropertyParserCallback) XMLParser(com.codename1.xml.XMLParser) List(java.util.List) ResultParser.resultParser(com.codename1.rad.io.ResultParser.resultParser) StringReader(java.io.StringReader) ParsingService(com.codename1.rad.io.ParsingService) Product(com.codename1.rad.schemas.Product) Map(java.util.Map) Thing(com.codename1.rad.schemas.Thing) Result(com.codename1.rad.processing.Result) ResultParser(com.codename1.rad.io.ResultParser) Entity(com.codename1.rad.models.Entity) PropertyParserCallback(com.codename1.rad.io.ResultParser.PropertyParserCallback) List(java.util.List) ResultParser(com.codename1.rad.io.ResultParser) Map(java.util.Map)

Example 32 with Tag

use of com.codename1.rad.models.Tag in project CodeRAD by shannah.

the class ResultParserTest method parserServiceTest.

private void parserServiceTest() throws Exception {
    String jsonData = "{\n" + "  \"colors\": [\n" + "    {\n" + "      \"color\": \"black\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,255,255,1],\n" + "        \"hex\": \"#000\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"white\",\n" + "      \"category\": \"value\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,0,0,1],\n" + "        \"hex\": \"#FFF\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"red\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,0,0,1],\n" + "        \"hex\": \"#FF0\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"blue\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,0,255,1],\n" + "        \"hex\": \"#00F\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"yellow\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"primary\",\n" + "      \"code\": {\n" + "        \"rgba\": [255,255,0,1],\n" + "        \"hex\": \"#FF0\"\n" + "      }\n" + "    },\n" + "    {\n" + "      \"color\": \"green\",\n" + "      \"category\": \"hue\",\n" + "      \"type\": \"secondary\",\n" + "      \"code\": {\n" + "        \"rgba\": [0,255,0,1],\n" + "        \"hex\": \"#0F0\"\n" + "      }\n" + "    },\n" + "  ]\n" + "}";
    class Color extends BaseEntity {
    }
    Tag type = new Tag("type");
    Tag red = new Tag("red"), green = new Tag("green"), blue = new Tag("blue"), alpha = new Tag("alpha");
    EntityType colorType = entityTypeBuilder(Color.class).string(Thing.name).string(Product.category).string(type).Integer(red).Integer(green).Integer(blue).Integer(alpha).factory(cls -> {
        return new Color();
    }).build();
    class Colors extends EntityList<Color> {
    }
    EntityType.registerList(Colors.class, Color.class, cls -> {
        return new Colors();
    });
    Tag colors = new Tag("colors");
    class ColorSet extends BaseEntity {
    }
    EntityType colorsetType = entityTypeBuilder(ColorSet.class).list(Colors.class, colors).factory(cls -> {
        return new ColorSet();
    }).build();
    ResultParser parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).Integer("code/rgba[0]", red).Integer("code/rgba[1]", green).Integer("code/rgba[2]", blue).Integer("code/rgba[3]", alpha);
    ParsingService parserService = new ParsingService();
    Throwable[] errors = new Throwable[1];
    parserService.parseJSON(jsonData, parser, new ColorSet()).ready(colorSet -> {
        Colors theColors = (Colors) colorSet.get(colors);
        assertEqual(6, theColors.size());
        assertEqual(6, colorSet.getEntityList(colors).size());
        assertEqual("black", theColors.get(0).getText(Thing.name));
        assertEqual("hue", theColors.get(0).getText(Product.category));
        assertEqual("primary", theColors.get(0).getText(type));
        assertEqual(255, (int) theColors.get(0).getInt(red));
        assertEqual(255, (int) theColors.get(0).getInt(green));
        assertEqual(255, (int) theColors.get(0).getInt(blue));
        assertEqual(1, (int) theColors.get(0).getInt(alpha));
        assertEqual("green", theColors.get(5).getText(Thing.name));
        assertEqual(0, (int) theColors.get(5).getInt(red));
        assertEqual(255, (int) theColors.get(5).getInt(green));
        assertEqual(0, (int) theColors.get(5).getInt(blue));
        assertEqual(1, (int) theColors.get(5).getInt(alpha));
        System.out.println("Finished ready callback");
    }).except(err -> {
        errors[0] = err;
    }).await();
    System.out.println("Finished await");
    if (errors[0] != null) {
        if (errors[0] instanceof RuntimeException) {
            throw (RuntimeException) errors[0];
        } else {
            throw (Exception) errors[0];
        }
    }
    class CodeParser implements PropertyParserCallback {

        private int index;

        CodeParser(int index) {
            this.index = index;
        }

        @Override
        public Object parse(Object codeMap) {
            if (!(codeMap instanceof Map)) {
                return 0;
            }
            Map m = (Map) codeMap;
            List rgba = (List) m.get("rgba");
            if (rgba == null) {
                return 0;
            }
            if (index < 0 || index >= rgba.size()) {
                return 0;
            }
            return rgba.get(index);
        }
    }
    parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).property("code", red, new CodeParser(0)).property("code", green, new CodeParser(1)).property("code", blue, new CodeParser(2)).property("code", alpha, new CodeParser(3));
    parserService.parseJSON(jsonData, parser, new ColorSet()).ready(colorSet -> {
        Colors theColors = (Colors) colorSet.get(colors);
        assertEqual(6, theColors.size());
        assertEqual(6, colorSet.getEntityList(colors).size());
        assertEqual("black", theColors.get(0).getText(Thing.name));
        assertEqual("hue", theColors.get(0).getText(Product.category));
        assertEqual("primary", theColors.get(0).getText(type));
        assertEqual(255, (int) theColors.get(0).getInt(red));
        assertEqual(255, (int) theColors.get(0).getInt(green));
        assertEqual(255, (int) theColors.get(0).getInt(blue));
        assertEqual(1, (int) theColors.get(0).getInt(alpha));
        assertEqual("green", theColors.get(5).getText(Thing.name));
        assertEqual(0, (int) theColors.get(5).getInt(red));
        assertEqual(255, (int) theColors.get(5).getInt(green));
        assertEqual(0, (int) theColors.get(5).getInt(blue));
        assertEqual(1, (int) theColors.get(5).getInt(alpha));
        System.out.println("Finished ready callback");
    }).except(err -> {
        errors[0] = err;
    }).await();
    System.out.println("Finished await");
    if (errors[0] != null) {
        if (errors[0] instanceof RuntimeException) {
            throw (RuntimeException) errors[0];
        } else {
            throw (Exception) errors[0];
        }
    }
    parserService.stop();
}
Also used : AbstractTest(com.codename1.testing.AbstractTest) Person(com.codename1.rad.schemas.Person) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) BaseEntity.entityTypeBuilder(com.codename1.rad.models.BaseEntity.entityTypeBuilder) Element(com.codename1.xml.Element) Log(com.codename1.io.Log) ParseException(com.codename1.l10n.ParseException) com.codename1.rad.models(com.codename1.rad.models) PropertyParserCallback(com.codename1.rad.io.ResultParser.PropertyParserCallback) XMLParser(com.codename1.xml.XMLParser) List(java.util.List) ResultParser.resultParser(com.codename1.rad.io.ResultParser.resultParser) StringReader(java.io.StringReader) ParsingService(com.codename1.rad.io.ParsingService) Product(com.codename1.rad.schemas.Product) Map(java.util.Map) Thing(com.codename1.rad.schemas.Thing) Result(com.codename1.rad.processing.Result) ResultParser(com.codename1.rad.io.ResultParser) Entity(com.codename1.rad.models.Entity) PropertyParserCallback(com.codename1.rad.io.ResultParser.PropertyParserCallback) ParsingService(com.codename1.rad.io.ParsingService) ParseException(com.codename1.l10n.ParseException) List(java.util.List) ResultParser(com.codename1.rad.io.ResultParser) Map(java.util.Map)

Example 33 with Tag

use of com.codename1.rad.models.Tag in project CodeRAD by shannah.

the class PickerPropertyViewBuilder method build.

@Override
public PickerPropertyView build() {
    if (fieldNode == null) {
        throw new IllegalStateException("PickerPropertyView requires tag to be set");
    }
    Picker picker = this.picker == null ? new Picker() : this.picker;
    String t = getTagName();
    if ("raddatePicker".equalsIgnoreCase(t)) {
        picker.setType(Display.PICKER_TYPE_DATE);
    } else if ("raddateTimePicker".equalsIgnoreCase(t)) {
        picker.setType(Display.PICKER_TYPE_DATE_AND_TIME);
    } else if ("radtimePicker".equalsIgnoreCase(t)) {
        picker.setType(Display.PICKER_TYPE_TIME);
    } else if ("radcalendarPicker".equalsIgnoreCase(t)) {
        picker.setType(Display.PICKER_TYPE_CALENDAR);
    } else if ("raddurationPicker".equalsIgnoreCase(t)) {
        picker.setType(Display.PICKER_TYPE_DURATION);
    } else if ("raddurationHoursPicker".equalsIgnoreCase(t)) {
        picker.setType(Display.PICKER_TYPE_DURATION_HOURS);
    } else if ("raddurationMinutesPicker".equalsIgnoreCase(t)) {
        picker.setType(Display.PICKER_TYPE_DURATION_MINUTES);
    }
    return new PickerPropertyView(picker, getEntity(), fieldNode);
}
Also used : Picker(com.codename1.ui.spinner.Picker) PickerPropertyView(com.codename1.rad.propertyviews.PickerPropertyView)

Aggregations

FieldNode (com.codename1.rad.nodes.FieldNode)6 Label (com.codename1.ui.Label)6 Container (com.codename1.ui.Container)5 BorderLayout (com.codename1.ui.layouts.BorderLayout)5 BoxLayout (com.codename1.ui.layouts.BoxLayout)5 Log (com.codename1.io.Log)4 ResultParser (com.codename1.rad.io.ResultParser)4 Entity (com.codename1.rad.models.Entity)4 Thing (com.codename1.rad.schemas.Thing)4 Hashtable (java.util.Hashtable)4 RAD (com.codename1.rad.annotations.RAD)3 Component (com.codename1.ui.Component)3 Dialog (com.codename1.ui.Dialog)3 TextArea (com.codename1.ui.TextArea)3 Element (com.codename1.xml.Element)3 XMLParser (com.codename1.xml.XMLParser)3 IOException (java.io.IOException)3 NetworkEvent (com.codename1.io.NetworkEvent)2 ParseException (com.codename1.l10n.ParseException)2 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)2