Search in sources :

Example 6 with BaseEntity

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

the class EntityListTest method testGenericList.

private void testGenericList() throws Exception {
    EntityList list = new EntityList();
    assertEqual(0, list.size());
    list.add(new BaseEntity());
    list.add(new BaseEntity());
    list.add(new BaseEntity());
    assertEqual(3, list.size());
    Entity firstEntity = list.get(0);
    assertNotNull(firstEntity);
    list.remove(firstEntity);
    assertEqual(2, list.size());
    class Stats {

        int counter = 0;

        Entity lastAdd;

        Entity lastRemove;

        int added;

        int removed;
    }
    Stats stats = new Stats();
    Observer observer = (o, arg) -> {
        stats.counter++;
    };
    list.addObserver(observer);
    list.notifyObservers();
    assertEqual(stats.counter, 1);
    list.notifyObservers();
    assertEqual(1, stats.counter);
    list.add(new BaseEntity());
    assertEqual(1, stats.counter);
    list.notifyObservers();
    assertEqual(2, stats.counter);
    list.addActionListener(evt -> {
        if (evt instanceof EntityAddedEvent) {
            EntityAddedEvent eae = (EntityAddedEvent) evt;
            stats.added++;
            stats.lastAdd = eae.getEntity();
        } else if (evt instanceof EntityRemovedEvent) {
            EntityRemovedEvent ere = (EntityRemovedEvent) evt;
            stats.removed++;
            stats.lastRemove = ere.getEntity();
        }
    });
    list.add(firstEntity);
    assertEqual(1, stats.added);
    assertEqual(firstEntity, stats.lastAdd);
    list.remove(firstEntity);
    assertEqual(1, stats.removed);
    assertEqual(firstEntity, stats.lastRemove);
}
Also used : AbstractTest(com.codename1.testing.AbstractTest) Observer(java.util.Observer) TransactionEvent(com.codename1.rad.models.EntityList.TransactionEvent) EntityAddedEvent(com.codename1.rad.models.EntityList.EntityAddedEvent) EntityRemovedEvent(com.codename1.rad.models.EntityList.EntityRemovedEvent) EntityList(com.codename1.rad.models.EntityList) BaseEntity(com.codename1.rad.models.BaseEntity) EntityListEvent(com.codename1.rad.models.EntityList.EntityListEvent) ActionListener(com.codename1.ui.events.ActionListener) Entity(com.codename1.rad.models.Entity) BaseEntity(com.codename1.rad.models.BaseEntity) Entity(com.codename1.rad.models.Entity) EntityRemovedEvent(com.codename1.rad.models.EntityList.EntityRemovedEvent) Observer(java.util.Observer) EntityList(com.codename1.rad.models.EntityList) BaseEntity(com.codename1.rad.models.BaseEntity) EntityAddedEvent(com.codename1.rad.models.EntityList.EntityAddedEvent)

Example 7 with BaseEntity

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

the class EntityListViewTest method testSortedListTransactions.

private void testSortedListTransactions() {
    SortedList<Entity> internal = new SortedList<Entity>(new Comparator<Entity>() {

        @Override
        public int compare(Entity a, Entity b) {
            String name1 = a.getText(Thing.name);
            if (name1 == null)
                name1 = "";
            String name2 = b.getText(Thing.name);
            if (name2 == null)
                name2 = "";
            return name1.compareTo(name2);
        }
    });
    EntityList el = new EntityList() {

        @Override
        protected List createInternalList() {
            return internal;
        }
    };
    class Person extends BaseEntity {
    }
    entityTypeBuilder(Person.class).string(Thing.name).factory(cls -> {
        return new Person();
    }).build();
    Person a = new Person();
    a.set(Thing.name, "A");
    Person b = new Person();
    b.set(Thing.name, "B");
    Person c = new Person();
    c.set(Thing.name, "C");
    Person d = new Person();
    d.set(Thing.name, "D");
    ProfileListView view = new ProfileListView(el);
    view.bind();
    assertEqual(0, view.getScrollWrapper().getComponentCount());
    el.add(b);
    assertEqual(1, view.getScrollWrapper().getComponentCount());
    assertNotNull(view.getRowViewForEntity(b));
    assertNull(view.getRowViewForEntity(a));
    el.add(a);
    assertEqual(0, el.indexOf(a));
    assertEqual(1, el.indexOf(b));
    assertEqual(2, view.getScrollWrapper().getComponentCount());
    assertEqual(0, view.getScrollWrapper().getComponentIndex((Component) view.getRowViewForEntity(a)));
    assertEqual(1, view.getScrollWrapper().getComponentIndex((Component) view.getRowViewForEntity(b)));
    view.unbind();
}
Also used : AbstractTest(com.codename1.testing.AbstractTest) EntityType(com.codename1.rad.models.EntityType) BaseEntity.entityTypeBuilder(com.codename1.rad.models.BaseEntity.entityTypeBuilder) EntityList(com.codename1.rad.models.EntityList) AbstractList(java.util.AbstractList) BaseEntity(com.codename1.rad.models.BaseEntity) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) Component(com.codename1.ui.Component) Thing(com.codename1.rad.schemas.Thing) Comparator(java.util.Comparator) Entity(com.codename1.rad.models.Entity) Collections(java.util.Collections) BaseEntity(com.codename1.rad.models.BaseEntity) Entity(com.codename1.rad.models.Entity) EntityList(com.codename1.rad.models.EntityList) BaseEntity(com.codename1.rad.models.BaseEntity) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView) Component(com.codename1.ui.Component)

Aggregations

Entity (com.codename1.rad.models.Entity)5 AbstractTest (com.codename1.testing.AbstractTest)5 ResultParser (com.codename1.rad.io.ResultParser)4 BaseEntity (com.codename1.rad.models.BaseEntity)3 BaseEntity.entityTypeBuilder (com.codename1.rad.models.BaseEntity.entityTypeBuilder)3 EntityList (com.codename1.rad.models.EntityList)3 Thing (com.codename1.rad.schemas.Thing)3 List (java.util.List)3 Log (com.codename1.io.Log)2 ParseException (com.codename1.l10n.ParseException)2 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)2 ParsingService (com.codename1.rad.io.ParsingService)2 PropertyParserCallback (com.codename1.rad.io.ResultParser.PropertyParserCallback)2 ResultParser.resultParser (com.codename1.rad.io.ResultParser.resultParser)2 com.codename1.rad.models (com.codename1.rad.models)2 EntityAddedEvent (com.codename1.rad.models.EntityList.EntityAddedEvent)2 EntityListEvent (com.codename1.rad.models.EntityList.EntityListEvent)2 EntityRemovedEvent (com.codename1.rad.models.EntityList.EntityRemovedEvent)2 TransactionEvent (com.codename1.rad.models.EntityList.TransactionEvent)2 Result (com.codename1.rad.processing.Result)2