Search in sources :

Example 1 with ControllerEvent

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);
        }
    }
}
Also used : Component(com.codename1.ui.Component)

Example 2 with ControllerEvent

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());
}
Also used : Controller(com.codename1.rad.controllers.Controller) ControllerEvent(com.codename1.rad.controllers.ControllerEvent)

Example 3 with ControllerEvent

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());
                    }
                });
            }
        }
    }
}
Also used : Entity(com.codename1.rad.models.Entity) HashMap(java.util.HashMap) Form(com.codename1.ui.Form) ActionNode(com.codename1.rad.nodes.ActionNode) EntityList(com.codename1.rad.models.EntityList) HashMap(java.util.HashMap) Map(java.util.Map) EntityListProvider(com.codename1.rad.models.EntityListProvider) ControllerEvent(com.codename1.rad.controllers.ControllerEvent)

Example 4 with ControllerEvent

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();
                    }
                });
            }
        }
    }
}
Also used : Entity(com.codename1.rad.models.Entity) HashMap(java.util.HashMap) Form(com.codename1.ui.Form) ActionNode(com.codename1.rad.nodes.ActionNode) EntityList(com.codename1.rad.models.EntityList) HashMap(java.util.HashMap) Map(java.util.Map) EntityListProvider(com.codename1.rad.models.EntityListProvider) ControllerEvent(com.codename1.rad.controllers.ControllerEvent)

Aggregations

ControllerEvent (com.codename1.rad.controllers.ControllerEvent)3 Entity (com.codename1.rad.models.Entity)2 EntityList (com.codename1.rad.models.EntityList)2 EntityListProvider (com.codename1.rad.models.EntityListProvider)2 ActionNode (com.codename1.rad.nodes.ActionNode)2 Form (com.codename1.ui.Form)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Controller (com.codename1.rad.controllers.Controller)1 Component (com.codename1.ui.Component)1