use of com.codename1.rad.controllers.ControllerEvent in project CodeRAD by shannah.
the class ActionSupport method dispatchEvent.
/**
* Dispatches an event, first using the source's action support,
* if it implements EventProducer interface. If event not consumed,
* it will find the nearest ViewController and dispatch the event
* up the controller chain. Latter also requires that event is ControllerEvent
* @param evt
*/
public static void dispatchEvent(ActionEvent evt) {
if (evt.isConsumed()) {
return;
}
Object source = evt.getSource();
if (source instanceof EventProducer) {
EventProducer ep = (EventProducer) source;
ep.getActionSupport().fireActionEvent(evt);
}
if (evt.isConsumed()) {
return;
}
if (evt instanceof ControllerEvent) {
if (source instanceof Component) {
Component cmp = (Component) source;
ViewController controller = ViewController.getViewController(cmp);
if (controller != null) {
controller.dispatchEvent((ControllerEvent) evt);
}
} else if (source instanceof Controller) {
Controller ctrl = (Controller) source;
ctrl.dispatchEvent((ControllerEvent) evt);
}
}
}
use of com.codename1.rad.controllers.ControllerEvent in project CodeRAD by shannah.
the class ControllerTest method basicControllerTest.
private void basicControllerTest() throws Exception {
Controller parent = new Controller(null);
Controller child = new Controller(parent);
assertNull(parent.getParent());
assertEqual(parent, child.getParent());
class SharedObject {
String name;
}
SharedObject shared = new SharedObject();
shared.name = "Hello";
parent.addLookup(shared);
assertEqual(shared, child.lookup(SharedObject.class));
assertEqual(shared, parent.lookup(SharedObject.class));
child.setParent(null);
assertNull(child.lookup(SharedObject.class));
assertEqual(shared, parent.lookup(SharedObject.class));
child.setParent(parent);
class Stats {
ControllerEvent lastEvent;
}
Stats stats = new Stats();
parent.addEventListener(evt -> {
;
stats.lastEvent = evt;
});
ControllerEvent e = new ControllerEvent(child);
ActionSupport.dispatchEvent(e);
assertNotNull(stats.lastEvent);
assertEqual(stats.lastEvent, e);
ControllerEvent e2 = new ControllerEvent(child);
child.addEventListener(evt -> {
if (evt == e2) {
evt.consume();
}
});
ActionSupport.dispatchEvent(e2);
assertEqual(stats.lastEvent, e);
assertTrue(e2.isConsumed());
}
use of com.codename1.rad.controllers.ControllerEvent in project CodeRAD by shannah.
the class EntityListView method loadMore.
public void loadMore() {
Entity requestData = null;
ActionNode refreshAction = getViewNode().getAction(ActionCategories.LIST_REFRESH_ACTION);
ActionNode loadMoreAction = getViewNode().getAction(ActionCategories.LIST_LOAD_MORE_ACTION);
if (loadMoreAction != null) {
Map extraData = new HashMap();
if (nextProviderRequest != null) {
EventContext.addExtra(extraData, nextProviderRequest);
}
EventContext.addExtra(extraData, EntityListProvider.RequestType.LOAD_MORE);
ControllerEvent evt = ActionSupport.as(loadMoreAction.fireEvent(getEntity(), this, extraData), ControllerEvent.class);
if (evt != null && evt.isConsumed()) {
EntityListProvider.Request req = evt.getAsyncResource(EntityListProvider.Request.class);
nextProviderRequest = null;
if (req != null) {
req.onResult((res, err) -> {
if (err != null) {
// We just swallow errors.
// The provider can return an error to indicate that it's done but no data.
// But it is up to the provider to propagate errors up the UI or log if necessary.
InfiniteScrollAdapter.addMoreComponents(wrapper, new Component[0], req.hasMore());
return;
}
nextProviderRequest = req.getNextRequest();
EntityList modelSet = getEntity();
if (res.size() > 0) {
modelSet.startTransaction();
for (Entity en : (Iterable<Entity>) res) {
modelSet.add(en);
}
boolean localAnimateInsertions = animateInsertions;
animateInsertions = false;
boolean localAnimateRemovals = animateRemovals;
animateRemovals = false;
modelSet.commitTransaction();
animateInsertions = localAnimateInsertions;
animateRemovals = localAnimateRemovals;
InfiniteScrollAdapter.addMoreComponents(wrapper, new Component[0], req.hasMore());
Form f = getComponentForm();
if (f != null) {
getComponentForm().revalidateWithAnimationSafety();
}
} else {
InfiniteScrollAdapter.addMoreComponents(wrapper, new Component[0], req.hasMore());
}
});
}
}
}
}
use of com.codename1.rad.controllers.ControllerEvent in project CodeRAD by shannah.
the class EntityListView method refresh.
public void refresh() {
ActionNode refreshAction = getViewNode().getAction(ActionCategories.LIST_REFRESH_ACTION);
ActionNode loadMoreAction = getViewNode().getAction(ActionCategories.LIST_LOAD_MORE_ACTION);
Entity requestData = null;
if (refreshAction != null) {
Map extraData = new HashMap();
EventContext.addExtra(extraData, EntityListProvider.RequestType.REFRESH);
ControllerEvent evt = ActionSupport.as(refreshAction.fireEvent(getEntity(), this, extraData), ControllerEvent.class);
if (evt != null && evt.isConsumed()) {
EntityListProvider.Request request = evt.getAsyncResource(EntityListProvider.Request.class);
if (request != null) {
// The provider is fulfilling the request asynchronously.
request.onResult((res, err) -> {
if (err != null) {
return;
}
nextProviderRequest = request.getNextRequest();
EntityList modelList = getEntity();
modelList.startTransaction();
modelList.clear();
for (Object o : res) {
modelList.add((Entity) o);
}
boolean localAnimateInsertions = animateInsertions;
animateInsertions = false;
boolean localAnimateRemovals = animateRemovals;
animateRemovals = false;
modelList.commitTransaction();
animateInsertions = localAnimateInsertions;
animateRemovals = localAnimateRemovals;
if (loadMoreAction != null) {
InfiniteScrollAdapter.addMoreComponents(wrapper, new Component[0], request.hasMore());
}
Form f = getComponentForm();
if (f != null) {
getComponentForm().revalidateWithAnimationSafety();
}
});
}
}
}
}
Aggregations