Search in sources :

Example 21 with Entity

use of com.codename1.rad.models.Entity 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 22 with Entity

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

the class Controller method withLookupEntity.

/**
 * Looks up an entity of a given class, and executes the provided callback with it if the
 * entity is found.
 * @param type A type of entity.  This may be a Class or an Interface.
 * @param callback  Call back that is only called if the lookup finds a match.
 * @return Boolean returns true if the lookup was found.
 * @since 2.0
 */
public boolean withLookupEntity(Class type, SuccessCallback<Entity> callback) {
    Entity o = lookupEntity(type);
    if (o == null)
        return false;
    callback.onSucess(o);
    return true;
}
Also used : Entity(com.codename1.rad.models.Entity)

Example 23 with Entity

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

the class Controller method withParentLookupEntity.

/**
 * Calls {@link #lookupEntity(Class)} on the Parent controller.
 * @param type The typeof entity to look for.  This may also be an Interface.
 * @param callback Callback executed only if a matching entity is found.
 * @return True if entity is found.
 * @since 2.0
 */
public boolean withParentLookupEntity(Class type, SuccessCallback<Entity> callback) {
    Entity o = parentLookupEntity(type);
    if (o == null)
        return false;
    callback.onSucess(o);
    return true;
}
Also used : Entity(com.codename1.rad.models.Entity)

Example 24 with Entity

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

the class ActionNode method fireEvent.

/**
 * Fires an event with the given entity, source, and extra data as the context.
 * @param entity
 * @param source
 * @param extraData
 * @return
 */
public ActionEvent fireEvent(Entity entity, Component source, Map extraData) {
    EventFactoryNode eventFactory = this.getEventFactory();
    if (eventFactory == null) {
        eventFactory = new EventFactoryNode(UI.getDefaultEventFactory());
    }
    EventContext eventContext = new EventContext();
    eventContext.setEntity(entity);
    eventContext.setAction(this);
    eventContext.setEventSource(source);
    if (extraData != null) {
        for (Object k : extraData.keySet()) {
            eventContext.putExtra(k, extraData.get(k));
        }
    }
    ActionEvent actionEvent = eventFactory.getValue().createEvent(eventContext);
    fireActionListeners(actionEvent);
    if (actionEvent.isConsumed()) {
        fireAfterActionCallback(actionEvent);
        return actionEvent;
    }
    ActionSupport.dispatchEvent(actionEvent);
    fireAfterActionCallback(actionEvent);
    return actionEvent;
}
Also used : EventContext(com.codename1.rad.events.EventContext) ActionEvent(com.codename1.ui.events.ActionEvent)

Example 25 with Entity

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

the class PropertySelector method addPropertyChangeListener.

/**
 * Adds a change listener on property.
 * @param l The listener to add.
 */
public void addPropertyChangeListener(ActionListener<PropertyChangeEvent> l) {
    if (root != null) {
        Property prop = property;
        if (prop == null && tags != null) {
            prop = root.getEntity().findProperty(tags);
        }
        if (prop != null) {
            root.getEntity().addPropertyChangeListener(prop, pcl());
        }
    } else {
        Entity leafEntity = getLeafEntity();
        Property leafProperty = getLeafProperty();
        if (leafEntity != null && leafProperty != null) {
            leafEntity.getEntity().addPropertyChangeListener(leafProperty, pcl());
        }
        parent.addVetoablePropertyChangeListener(vpcl());
        parent.addListChangeListener(listChangeListener());
    }
    if (listeners == null) {
        listeners = new EventDispatcher();
    }
    listeners.addListener(l);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Aggregations

Entity (com.codename1.rad.models.Entity)38 Property (com.codename1.rad.models.Property)15 EntityList (com.codename1.rad.models.EntityList)13 Container (com.codename1.ui.Container)12 BorderLayout (com.codename1.ui.layouts.BorderLayout)11 ActionNode (com.codename1.rad.nodes.ActionNode)10 FieldNode (com.codename1.rad.nodes.FieldNode)10 Form (com.codename1.ui.Form)8 ViewNode (com.codename1.rad.nodes.ViewNode)7 GridLayout (com.codename1.ui.layouts.GridLayout)7 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)6 ResultParser (com.codename1.rad.io.ResultParser)6 Component (com.codename1.ui.Component)6 Element (com.codename1.xml.Element)6 BadgeUIID (com.codename1.rad.attributes.BadgeUIID)5 Thing (com.codename1.rad.schemas.Thing)5 Button (com.codename1.ui.Button)5 BoxLayout (com.codename1.ui.layouts.BoxLayout)5 Map (java.util.Map)5 Log (com.codename1.io.Log)4