This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Shell 3.4.0!

ListView

ListView is a base implementation providing functionality to draw a list of items. Inherits BoxView.

ListView<T> is typed as its item and can take any object. Further item processing happens in a CellFactory. For conveniance there is a support for generic higher level list feature showing checked states as normal check and radio types. Essentially what you can have is a list of items which are shown as is, shown where any items can have a checked state or only one item can have a checked state.

ListView<String> view = new ListView<>();
view.setItems(List.of("item1", "item2"));

Default item style is nocheck but can be changed.

Supports NOCHECK, `CHECK and `RADIO`

ListView<String> view = new ListView<>(ItemStyle.CHECKED);

Customisation

How individual cells are shown depends on a CellFactory. Default implementation simply shows item using its toString() method.

It can be customised by modifying a used CellFactory.

record ExampleData(String name) {
};

static class ExampleListCell extends AbstractListCell<ExampleData> {

	public ExampleListCell(ExampleData item) {
		super(item);
	}

	@Override
	public void draw(Screen screen) {
		Rectangle rect = getRect();
		Writer writer = screen.writerBuilder().style(getStyle()).build();
		writer.text(getItem().name(), rect.x(), rect.y());
		writer.background(rect, getBackgroundColor());
	}
}

And set it as a factory:

ListView<ExampleData> view = new ListView<>();
view.setCellFactory((list, item) -> new ExampleListCell(item));

Default Bindings

Default view commands are:

Table 1. ViewCommands
Command Description

LINE_UP

Selection moves up.

LINE_DOWN

Selection moves down.

Default key bindigs are:

Table 2. Key
Command Description

CursorUp

Bound ViewCommand LINE_UP

CursorDown

Bound ViewCommand LINE_DOWN

Enter

Choose active item.

Space

Change active item selection state.

Default mouse bindigs are:

Table 3. Mouse
Command Description

Wheel | WheelUp

Bound ViewCommand LINE_UP

Wheel | WheelDown

Bound ViewCommand LINE_DOWN

Released | Button1

Choose item

Events

Events are sent depending on a used list type.

Table 4. ListView Events
Event Description

ListViewOpenSelectedItemEvent

Active item is requested to get opened.

ListViewSelectedItemChangedEvent

Active item is changed.