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());
}
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;
}
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;
}
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;
}
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);
}
Aggregations