Search in sources :

Example 11 with Controller

use of com.codename1.rad.controllers.Controller 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 12 with Controller

use of com.codename1.rad.controllers.Controller in project CodeRAD by shannah.

the class Controller method getViewNode.

/**
 * Gets the {@link ViewNode} that should be used as the view model for views of this controller. Subclasses should override {@link #createViewNode() }
 * to define the view node for the controller.  This method will defer to that for the initial view node creation, and then just return
 * that view node on subsequeuent calls.
 *
 * NOTE: This will automatically set the parent node of the view node to the view node of the parent controller.
 * @return
 */
public ViewNode getViewNode() {
    startControllerInternal();
    if (node == null) {
        node = createViewNode();
        ViewNode parentNode = null;
        if (parent != null) {
            parentNode = parent.getViewNode();
        }
        node.setParent(parentNode);
    }
    return node;
}
Also used : ViewNode(com.codename1.rad.nodes.ViewNode)

Example 13 with Controller

use of com.codename1.rad.controllers.Controller in project CodeRAD by shannah.

the class Controller method extendAction.

/**
 * Extends an existing action from one of the parent controllers, and registers it as an action
 * on this controller.
 * @param category The category to register the action to.
 * @param overwriteAttributes Whether to overwrite existing attributes of the action.  If false, attributes
 *                            provided will be ignored when extending actions that already have those attributes
 *                            defined.
 * @param attributes Attributes to add to the action.
 * @return The action that was added.
 * @since 2.0
 */
public ActionNode extendAction(ActionNode.Category category, boolean overwriteAttributes, Attribute... attributes) {
    ActionNode action = getInheritedAction(category);
    if (action == null) {
        action = UI.action(attributes);
    } else {
        action = (ActionNode) action.createProxy(action.getParent());
        action.setAttributes(overwriteAttributes, attributes);
    }
    addActions(category, action);
    return action;
}
Also used : ActionNode(com.codename1.rad.nodes.ActionNode)

Example 14 with Controller

use of com.codename1.rad.controllers.Controller in project CodeRAD by shannah.

the class ControllerEvent method getViewController.

public ViewController getViewController() {
    Object source = getSource();
    if (source instanceof Controller) {
        return ((Controller) source).getViewController();
    }
    Component cmp = getComponent();
    if (cmp == null) {
        return null;
    }
    return ViewController.getViewController(cmp);
}
Also used : Component(com.codename1.ui.Component)

Example 15 with Controller

use of com.codename1.rad.controllers.Controller in project CodeRAD by shannah.

the class FormController method showBack.

/**
 * Shows this controller's form using the "back" animation.  If the current controller is an instance of {@link AutoCloseable},
 * then this will first attempt to call {@link AutoCloseable#close() } on the current form first.  If it throws an exception,
 * then that exception will be swallowed, but the showBack() action will be cancelled.
 */
public void showBack() {
    Form currForm = CN.getCurrentForm();
    if (currForm != null) {
        ViewController currentController = getViewController(currForm);
        if (currentController instanceof AutoCloseable) {
            AutoCloseable ac = (AutoCloseable) currentController;
            try {
                ac.close();
            } catch (Exception ex) {
                return;
            }
        }
    }
    getView().showBack();
}
Also used : Form(com.codename1.ui.Form)

Aggregations

Form (com.codename1.ui.Form)6 ActionNode (com.codename1.rad.nodes.ActionNode)5 Entity (com.codename1.rad.models.Entity)4 ViewNode (com.codename1.rad.nodes.ViewNode)3 Component (com.codename1.ui.Component)3 Toolbar (com.codename1.ui.Toolbar)3 IOException (java.io.IOException)3 Log (com.codename1.io.Log)2 NetworkEvent (com.codename1.io.NetworkEvent)2 ViewController (com.codename1.rad.controllers.ViewController)2 EntityList (com.codename1.rad.models.EntityList)2 EntityTypeBuilder.entityTypeBuilder (com.codename1.rad.models.EntityTypeBuilder.entityTypeBuilder)2 ListNode (com.codename1.rad.nodes.ListNode)2 Thing (com.codename1.rad.schemas.Thing)2 UI (com.codename1.rad.ui.UI)2 ProfileListView (com.codename1.rad.ui.entityviews.ProfileListView)2 CN (com.codename1.ui.CN)2 Dialog (com.codename1.ui.Dialog)2 Display (com.codename1.ui.Display)2 FontImage (com.codename1.ui.FontImage)2