Search in sources :

Example 6 with EntityList

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

the class ProfileAvatarsTitleComponentBuilder method build.

@Override
public ProfileAvatarsTitleComponent build() {
    ViewNode node = new ViewNode();
    node.setParent(getContext().getNode());
    EntityList l = (entity instanceof EntityList) ? (EntityList) entity : new EntityList();
    if (entity != l) {
        l.add(entity);
    }
    if (avatarSize <= 0) {
        avatarSize = CN.convertToPixels(1f, Style.UNIT_TYPE_REM);
    }
    return new ProfileAvatarsTitleComponent(l, node, avatarSize / (float) CN.convertToPixels(1f));
}
Also used : EntityList(com.codename1.rad.models.EntityList) ViewNode(com.codename1.rad.nodes.ViewNode) ProfileAvatarsTitleComponent(com.codename1.rad.ui.entityviews.ProfileAvatarsTitleComponent)

Example 7 with EntityList

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

the class EntityListTest method testTransactions.

private void testTransactions() throws Exception {
    EntityList el = new EntityList();
    Entity bob = new BaseEntity();
    Entity gary = new BaseEntity();
    Entity june = new BaseEntity();
    class Stats {

        private Object data;

        private int count;

        private void reset() {
            data = null;
            count = 0;
        }
    }
    // Warm it up.. make sure no NPEs or anything
    el.add(bob);
    el.add(gary);
    el.remove(bob);
    el.remove(gary);
    Stats stats = new Stats();
    ActionListener<EntityListEvent> l1 = evt -> {
        if (evt instanceof EntityAddedEvent || evt instanceof EntityRemovedEvent) {
            stats.count++;
            stats.data = evt;
        }
    };
    el.addActionListener(l1);
    el.add(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    assertEqual(1, el.size());
    EntityAddedEvent eae = (EntityAddedEvent) stats.data;
    assertEqual(bob, eae.getEntity());
    assertNull(eae.getTransaction(), "EntityAddedEvent not in transaction should return null for getTransaction()");
    stats.reset();
    el.startTransaction();
    assertEqual(0, stats.count);
    assertNull(stats.data);
    el.remove(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityRemovedEvent);
    assertEqual(0, el.size());
    EntityRemovedEvent ere = (EntityRemovedEvent) stats.data;
    assertEqual(bob, ere.getEntity());
    assertNotNull(ere.getTransaction(), "EntityRemovedEvent inside transaction should return non-null for getTransaction()");
    Throwable t = null;
    try {
        el.startTransaction();
    } catch (IllegalStateException ex) {
        t = ex;
    }
    assertTrue(t instanceof IllegalStateException, "Starting a transaction while another one is active should raise an IllegalStateException");
    el.commitTransaction();
    el.removeActionListener(l1);
    l1 = evt -> {
        if (evt instanceof EntityAddedEvent || evt instanceof EntityRemovedEvent || evt instanceof TransactionEvent) {
            stats.count++;
            stats.data = evt;
        }
    };
    el.addActionListener(l1);
    stats.reset();
    el.startTransaction();
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof TransactionEvent);
    TransactionEvent te = (TransactionEvent) stats.data;
    assertTrue(te.isEmpty());
    assertTrue(!te.isComplete());
    stats.reset();
    el.add(bob);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    eae = (EntityAddedEvent) stats.data;
    assertEqual(bob, eae.getEntity());
    assertEqual(eae.getTransaction(), te, "EntityAddedEvent.getTransaction() should return same TransactionEvent from startTransaction");
    stats.reset();
    el.add(gary);
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof EntityAddedEvent);
    eae = (EntityAddedEvent) stats.data;
    assertEqual(gary, eae.getEntity());
    stats.reset();
    el.commitTransaction();
    assertEqual(1, stats.count);
    assertTrue(stats.data instanceof TransactionEvent);
    te = (TransactionEvent) stats.data;
    assertEqual(2, te.size());
    assertTrue(te.isComplete());
    assertEqual(bob, te.get(0).getEntity());
    assertEqual(gary, te.get(1).getEntity());
    t = null;
    try {
        el.commitTransaction();
    } catch (IllegalStateException ex) {
        t = ex;
    }
    assertTrue(t instanceof IllegalStateException, "commitTransaction() on list with no current transaction should raise an IllegalStateException");
    el.startTransaction();
    stats.reset();
    el.clear();
    assertEqual(0, el.size(), "EntityList.add() and EntityList.remove() should immediately change the list even if transaction is in progress");
    assertEqual(2, stats.count, "EntityList.clear() should trigger remove events for each item in list");
    el.commitTransaction();
    assertEqual(0, el.size());
    assertEqual(3, stats.count);
    assertTrue(stats.data instanceof TransactionEvent, "commitTransaction() didn't fire a transaction event");
    te = (TransactionEvent) stats.data;
    assertEqual(2, te.size(), "TransactionEvent size() should reflect the number of add or remove operations in transaction.");
    assertEqual(bob, te.get(0).getEntity());
    assertEqual(gary, te.get(1).getEntity());
}
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) EntityList(com.codename1.rad.models.EntityList) BaseEntity(com.codename1.rad.models.BaseEntity) EntityListEvent(com.codename1.rad.models.EntityList.EntityListEvent) TransactionEvent(com.codename1.rad.models.EntityList.TransactionEvent) EntityRemovedEvent(com.codename1.rad.models.EntityList.EntityRemovedEvent) EntityAddedEvent(com.codename1.rad.models.EntityList.EntityAddedEvent)

Example 8 with EntityList

use of com.codename1.rad.models.EntityList in project CodenameOne by codenameone.

the class RADActionBoundUIIDSample method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    Form hi = new Form("Toggled Actions Sample", new BorderLayout());
    // Create a tag fo the online status property.
    Tag TAG_ONLINE = new Tag("online");
    // Create an action that will indicte the online/offline status
    ActionNode status = UI.action(// on state of TAG_ONLINE property.
    UI.label(person -> {
        if (person.isFalsey(TAG_ONLINE)) {
            return "Offline";
        } else {
            return "Online";
        }
    }), // Depending on state of TAG_ONLINE property
    UI.uiid(person -> {
        if (person.isFalsey(TAG_ONLINE)) {
            return "LoggedOutStatusButton";
        } else {
            return "LoggedInStatusButton";
        }
    }), // Icon for the action
    UI.icon(FontImage.MATERIAL_PERSON), // define it to always return true so action is always visible.
    UI.condition(person -> {
        return true;
    }));
    // A User entity we use for the models.
    class User extends Entity {
    }
    entityTypeBuilder(User.class).Boolean(TAG_ONLINE).string(Thing.name).factory(cls -> {
        return new User();
    }).build();
    // Create an entity list that will hold several users.
    EntityList el = new EntityList();
    for (int i = 0; i < 200; i++) {
        User u = new User();
        u.set(Thing.name, "User " + i);
        u.set(TAG_ONLINE, i % 2 == 0);
        el.add(u);
    }
    // The ListNode is a wrapper that will be passed to our View so that
    // they can access our action.
    ListNode node = new ListNode(// for each row.
    UI.actions(ProfileListView.ACCOUNT_LIST_ROW_ACTIONS, status));
    // Use a ProfileListView to display all of the users
    // https://shannah.github.io/CodeRAD/javadoc/com/codename1/rad/ui/entityviews/ProfileListView.html
    ProfileListView plv = new ProfileListView(el, node, 8);
    plv.setScrollableY(true);
    // In order to respond to events raised by the action, our view needs to be wrapped
    // in a controller.  Normally our form would have a FormViewController so we could
    // just use FormController, but this sample is compressed to be inside
    // a single method here so we'll create a dedicated ViewController for the list
    ViewController ctrl = new ViewController(null);
    ctrl.setView(plv);
    ctrl.addActionListener(status, evt -> {
        // The action was pressed by the user
        // Update the model's online status
        User u = (User) evt.getEntity();
        u.set(TAG_ONLINE, u.isFalsey(TAG_ONLINE));
    // This will trigger a property change in the model which will update the view.
    });
    hi.add(CENTER, plv);
    hi.show();
}
Also used : ViewNode(com.codename1.rad.nodes.ViewNode) Toolbar(com.codename1.ui.Toolbar) BoxLayout(com.codename1.ui.layouts.BoxLayout) EntityTypeBuilder.entityTypeBuilder(com.codename1.rad.models.EntityTypeBuilder.entityTypeBuilder) Form(com.codename1.ui.Form) NetworkEvent(com.codename1.io.NetworkEvent) ListNode(com.codename1.rad.nodes.ListNode) Display(com.codename1.ui.Display) FontImage(com.codename1.ui.FontImage) Label(com.codename1.ui.Label) CN(com.codename1.ui.CN) ViewController(com.codename1.rad.controllers.ViewController) UI(com.codename1.rad.ui.UI) Entity(com.codename1.rad.models.Entity) Tag(com.codename1.rad.models.Tag) Resources(com.codename1.ui.util.Resources) EntityList(com.codename1.rad.models.EntityList) IOException(java.io.IOException) ActionNode(com.codename1.rad.nodes.ActionNode) Log(com.codename1.io.Log) BorderLayout(com.codename1.ui.layouts.BorderLayout) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView) UIManager(com.codename1.ui.plaf.UIManager) Dialog(com.codename1.ui.Dialog) Thing(com.codename1.rad.schemas.Thing) EntityListView(com.codename1.rad.ui.entityviews.EntityListView) Entity(com.codename1.rad.models.Entity) BorderLayout(com.codename1.ui.layouts.BorderLayout) Form(com.codename1.ui.Form) ViewController(com.codename1.rad.controllers.ViewController) ActionNode(com.codename1.rad.nodes.ActionNode) EntityList(com.codename1.rad.models.EntityList) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView) Tag(com.codename1.rad.models.Tag) ListNode(com.codename1.rad.nodes.ListNode)

Example 9 with EntityList

use of com.codename1.rad.models.EntityList in project CodenameOne by codenameone.

the class RADEntityListAddRemoveInvalidateSample method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    // An entity for the rows our our list view.
    class Person extends Entity {
    }
    entityTypeBuilder(Person.class).string(Thing.name).factory(cls -> {
        return new Person();
    }).build();
    // A remove row action that will be added to each row
    ActionNode removeRow = UI.action(UI.icon(FontImage.MATERIAL_DELETE));
    // An internal list we will use to store the rows of the entitylist
    ArrayList internalList = new ArrayList();
    EntityList profileList = new EntityList() {

        /**
         * Override createInternalList() so that we can use our own data structure
         * for storing this list.  This is contrived to allow us to test the
         * invalidate() method.
         */
        @Override
        protected List createInternalList() {
            return internalList;
        }
    };
    // A list node to wrap our action and pass it to our view
    ListNode node = new ListNode(// of the list.
    UI.actions(ProfileListView.ACCOUNT_LIST_ROW_ACTIONS, removeRow));
    // A ProfileListView to render the list.
    // See https://shannah.github.io/CodeRAD/javadoc/com/codename1/rad/ui/entityviews/ProfileListView.html
    ProfileListView listView = new ProfileListView(profileList, node, 10);
    listView.setScrollableY(true);
    // Create a controller for the ProfileListView so that we can handle actions fired by the view.
    // Normally we'd do this in the FormController but since this sample doesn't have one
    // we create a ViewController for the ProfileListView directly.
    ViewController controller = new ViewController(null);
    controller.setView(listView);
    // Button to add rows to the list
    Button addRow = new Button(FontImage.MATERIAL_ADD);
    // Button to clear the list
    Button clear = new Button("Clear");
    addRow.addActionListener(evt -> {
        // "Add" button clicked.
        // Create new person and add to the list
        Person p = new Person();
        p.set(Thing.name, "Row " + profileList.size());
        profileList.add(p);
    // This will trigger an EntityAddedEvent which will allow
    // the ProfileListView to synchronize
    });
    clear.addActionListener(evt -> {
        // "Clear" button clicked
        // We could have called profileList.clear()
        // but this would send EntityRemoved events for each row removed
        // which is inefficient.  Instead we'll clear the elements in
        // the internal list directly, and then call invalidate()
        // so that the ProfileListView knows to resynchronize its state.
        internalList.clear();
        profileList.invalidate();
    });
    controller.addActionListener(removeRow, evt -> {
        // The "Remove" button was clicked on a row.
        profileList.remove(evt.getEntity());
    });
    Form hi = new Form("Hi World", new BorderLayout());
    hi.add(NORTH, GridLayout.encloseIn(2, clear, addRow));
    hi.add(CENTER, listView);
    hi.show();
}
Also used : Toolbar(com.codename1.ui.Toolbar) BoxLayout(com.codename1.ui.layouts.BoxLayout) EntityTypeBuilder.entityTypeBuilder(com.codename1.rad.models.EntityTypeBuilder.entityTypeBuilder) Form(com.codename1.ui.Form) NetworkEvent(com.codename1.io.NetworkEvent) ArrayList(java.util.ArrayList) ListNode(com.codename1.rad.nodes.ListNode) GridLayout(com.codename1.ui.layouts.GridLayout) Display(com.codename1.ui.Display) FontImage(com.codename1.ui.FontImage) Label(com.codename1.ui.Label) Button(com.codename1.ui.Button) CN(com.codename1.ui.CN) ViewController(com.codename1.rad.controllers.ViewController) UI(com.codename1.rad.ui.UI) Entity(com.codename1.rad.models.Entity) Resources(com.codename1.ui.util.Resources) EntityList(com.codename1.rad.models.EntityList) IOException(java.io.IOException) ActionNode(com.codename1.rad.nodes.ActionNode) Log(com.codename1.io.Log) BorderLayout(com.codename1.ui.layouts.BorderLayout) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView) UIManager(com.codename1.ui.plaf.UIManager) List(java.util.List) Dialog(com.codename1.ui.Dialog) Thing(com.codename1.rad.schemas.Thing) Entity(com.codename1.rad.models.Entity) BorderLayout(com.codename1.ui.layouts.BorderLayout) ViewController(com.codename1.rad.controllers.ViewController) Button(com.codename1.ui.Button) Form(com.codename1.ui.Form) ActionNode(com.codename1.rad.nodes.ActionNode) ArrayList(java.util.ArrayList) EntityList(com.codename1.rad.models.EntityList) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView) ListNode(com.codename1.rad.nodes.ListNode)

Example 10 with EntityList

use of com.codename1.rad.models.EntityList in project CodenameOne by codenameone.

the class EntityListTransactionsSample method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    entityList = new EntityList();
    entityListView = new ProfileListView(entityList);
    entityListView.setScrollableY(true);
    Button addTen = new Button("Add 10 rows");
    addTen.addActionListener(evt -> {
        addRows(10);
    });
    Button addHundred = new Button("Add 100 rows");
    addHundred.addActionListener(evt -> {
        addRows(100);
    });
    Button removeTen = new Button("Remove 10 rows");
    removeTen.addActionListener(evt -> {
        removeRows(10);
    });
    Form hi = new Form("Hi World", new BorderLayout());
    hi.add(NORTH, GridLayout.encloseIn(3, addTen, addHundred, removeTen));
    hi.add(CENTER, entityListView);
    hi.show();
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) Form(com.codename1.ui.Form) EntityList(com.codename1.rad.models.EntityList) ProfileListView(com.codename1.rad.ui.entityviews.ProfileListView)

Aggregations

EntityList (com.codename1.rad.models.EntityList)18 Entity (com.codename1.rad.models.Entity)14 Map (java.util.Map)6 Thing (com.codename1.rad.schemas.Thing)5 AbstractTest (com.codename1.testing.AbstractTest)5 Form (com.codename1.ui.Form)5 List (java.util.List)5 Log (com.codename1.io.Log)4 ResultParser (com.codename1.rad.io.ResultParser)4 ActionNode (com.codename1.rad.nodes.ActionNode)4 ProfileListView (com.codename1.rad.ui.entityviews.ProfileListView)4 ArrayList (java.util.ArrayList)4 BaseEntity (com.codename1.rad.models.BaseEntity)3 BaseEntity.entityTypeBuilder (com.codename1.rad.models.BaseEntity.entityTypeBuilder)3 Result (com.codename1.rad.processing.Result)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 ButtonList (com.codename1.components.ButtonList)2 NetworkEvent (com.codename1.io.NetworkEvent)2 ParseException (com.codename1.l10n.ParseException)2 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)2