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