use of com.codename1.rad.ui.entityviews.ProfileListView in project CodenameOne by codenameone.
the class RADActionBoundUIIDSample method start.
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Toggled Actions Sample", new BorderLayout());
// Create a tag fo the online status property.
Tag TAG_ONLINE = new Tag("online");
// Create an action that will indicte the online/offline status
ActionNode status = UI.action(// on state of TAG_ONLINE property.
UI.label(person -> {
if (person.isFalsey(TAG_ONLINE)) {
return "Offline";
} else {
return "Online";
}
}), // Depending on state of TAG_ONLINE property
UI.uiid(person -> {
if (person.isFalsey(TAG_ONLINE)) {
return "LoggedOutStatusButton";
} else {
return "LoggedInStatusButton";
}
}), // Icon for the action
UI.icon(FontImage.MATERIAL_PERSON), // define it to always return true so action is always visible.
UI.condition(person -> {
return true;
}));
// A User entity we use for the models.
class User extends Entity {
}
entityTypeBuilder(User.class).Boolean(TAG_ONLINE).string(Thing.name).factory(cls -> {
return new User();
}).build();
// Create an entity list that will hold several users.
EntityList el = new EntityList();
for (int i = 0; i < 200; i++) {
User u = new User();
u.set(Thing.name, "User " + i);
u.set(TAG_ONLINE, i % 2 == 0);
el.add(u);
}
// The ListNode is a wrapper that will be passed to our View so that
// they can access our action.
ListNode node = new ListNode(// for each row.
UI.actions(ProfileListView.ACCOUNT_LIST_ROW_ACTIONS, status));
// Use a ProfileListView to display all of the users
// https://shannah.github.io/CodeRAD/javadoc/com/codename1/rad/ui/entityviews/ProfileListView.html
ProfileListView plv = new ProfileListView(el, node, 8);
plv.setScrollableY(true);
// In order to respond to events raised by the action, our view needs to be wrapped
// in a controller. Normally our form would have a FormViewController so we could
// just use FormController, but this sample is compressed to be inside
// a single method here so we'll create a dedicated ViewController for the list
ViewController ctrl = new ViewController(null);
ctrl.setView(plv);
ctrl.addActionListener(status, evt -> {
// The action was pressed by the user
// Update the model's online status
User u = (User) evt.getEntity();
u.set(TAG_ONLINE, u.isFalsey(TAG_ONLINE));
// This will trigger a property change in the model which will update the view.
});
hi.add(CENTER, plv);
hi.show();
}
use of com.codename1.rad.ui.entityviews.ProfileListView 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.ui.entityviews.ProfileListView in project CodenameOne by codenameone.
the class EntityListTransactionsSample method start.
public void start() {
if (current != null) {
current.show();
return;
}
entityList = new EntityList();
entityListView = new ProfileListView(entityList);
entityListView.setScrollableY(true);
Button addTen = new Button("Add 10 rows");
addTen.addActionListener(evt -> {
addRows(10);
});
Button addHundred = new Button("Add 100 rows");
addHundred.addActionListener(evt -> {
addRows(100);
});
Button removeTen = new Button("Remove 10 rows");
removeTen.addActionListener(evt -> {
removeRows(10);
});
Form hi = new Form("Hi World", new BorderLayout());
hi.add(NORTH, GridLayout.encloseIn(3, addTen, addHundred, removeTen));
hi.add(CENTER, entityListView);
hi.show();
}
use of com.codename1.rad.ui.entityviews.ProfileListView in project CodeRAD by shannah.
the class EntityListViewTest method testSortedListTransactions.
private void testSortedListTransactions() {
SortedList<Entity> internal = new SortedList<Entity>(new Comparator<Entity>() {
@Override
public int compare(Entity a, Entity b) {
String name1 = a.getText(Thing.name);
if (name1 == null)
name1 = "";
String name2 = b.getText(Thing.name);
if (name2 == null)
name2 = "";
return name1.compareTo(name2);
}
});
EntityList el = new EntityList() {
@Override
protected List createInternalList() {
return internal;
}
};
class Person extends BaseEntity {
}
entityTypeBuilder(Person.class).string(Thing.name).factory(cls -> {
return new Person();
}).build();
Person a = new Person();
a.set(Thing.name, "A");
Person b = new Person();
b.set(Thing.name, "B");
Person c = new Person();
c.set(Thing.name, "C");
Person d = new Person();
d.set(Thing.name, "D");
ProfileListView view = new ProfileListView(el);
view.bind();
assertEqual(0, view.getScrollWrapper().getComponentCount());
el.add(b);
assertEqual(1, view.getScrollWrapper().getComponentCount());
assertNotNull(view.getRowViewForEntity(b));
assertNull(view.getRowViewForEntity(a));
el.add(a);
assertEqual(0, el.indexOf(a));
assertEqual(1, el.indexOf(b));
assertEqual(2, view.getScrollWrapper().getComponentCount());
assertEqual(0, view.getScrollWrapper().getComponentIndex((Component) view.getRowViewForEntity(a)));
assertEqual(1, view.getScrollWrapper().getComponentIndex((Component) view.getRowViewForEntity(b)));
view.unbind();
}
Aggregations