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

Multi Select

The multi select component asks a user to select multiple items from a list. The following listing shows an example:

public class ComponentCommands {

	@Command(name = "component multi", description = "Multi selector", group = "Components")
	public String multiSelector() {
		List<SelectorItem<String>> items = new ArrayList<>();
		items.add(SelectorItem.of("key1", "value1"));
		items.add(SelectorItem.of("key2", "value2", false, true));
		items.add(SelectorItem.of("key3", "value3"));
		MultiItemSelector<String, SelectorItem<String>> component = new MultiItemSelector<>(getTerminal(),
				items, "testSimple", null);
		ResourceLoader resourceLoader = null; // getResourceLoader();
		TemplateExecutor templateExecutor = null; // getTemplateExecutor();
		component.setResourceLoader(resourceLoader);
		component.setTemplateExecutor(templateExecutor);
		MultiItemSelectorContext<String, SelectorItem<String>> context = component
			.run(MultiItemSelectorContext.empty());
		String result = context.getResultItems()
			.stream()
			.map(si -> si.getItem())
			.collect(Collectors.joining(","));
		return "Got value " + result;
	}

}

The following screencast shows a typical multi-select component:

The context object is MultiItemSelectorContext. The following table describes its context variables:

Table 1. MultiItemSelectorContext Template Variables
Key Description

values

The values returned when the component exists.

rows

The visible items, where rows contain maps of name, selected, on-row, and enabled items.

model

The parent context variables (see SelectorComponentContext Template Variables).