Support initial checked state
- In MenuView initial check state can be set. - Relates #832
This commit is contained in:
@@ -94,6 +94,14 @@ public class MenuView extends BoxView {
|
||||
if (!items.isEmpty()) {
|
||||
activeItemIndex = 0;
|
||||
}
|
||||
items.forEach(i -> {
|
||||
if (i.initialCheckState && i.getCheckStyle() == MenuItemCheckStyle.CHECKED) {
|
||||
checkedActive.add(i);
|
||||
}
|
||||
else if (i.initialCheckState && i.getCheckStyle() == MenuItemCheckStyle.RADIO) {
|
||||
radioActive = i;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,6 +329,7 @@ public class MenuView extends BoxView {
|
||||
private final MenuItemCheckStyle checkStyle;
|
||||
private final List<MenuItem> items;
|
||||
private Runnable action;
|
||||
private boolean initialCheckState = false;
|
||||
|
||||
/**
|
||||
* Construct menu item with a title.
|
||||
@@ -342,19 +351,33 @@ public class MenuView extends BoxView {
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct menu item with a title and a check style.
|
||||
* Construct menu item with a title, a check style and a runnable.
|
||||
*
|
||||
* @param title the title
|
||||
* @param checkStyle the check style
|
||||
* @param action the action to run when item is chosen
|
||||
*/
|
||||
public MenuItem(String title, MenuItemCheckStyle checkStyle, Runnable action) {
|
||||
this(title, checkStyle, action, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct menu item with a title, a check style, a runnable and initial
|
||||
* checked state.
|
||||
*
|
||||
* @param title the title
|
||||
* @param checkStyle the check style
|
||||
* @param action the action to run when item is chosen
|
||||
* @param initialCheckState initial checked state
|
||||
*/
|
||||
public MenuItem(String title, MenuItemCheckStyle checkStyle, Runnable action, boolean initialCheckState) {
|
||||
Assert.state(StringUtils.hasText(title), "title must have text");
|
||||
Assert.notNull(checkStyle, "check style cannot be null");
|
||||
this.title = title;
|
||||
this.checkStyle = checkStyle;
|
||||
this.action = action;
|
||||
this.items = null;
|
||||
this.initialCheckState = initialCheckState;
|
||||
}
|
||||
|
||||
protected MenuItem(String title, MenuItem[] items) {
|
||||
@@ -395,6 +418,10 @@ public class MenuView extends BoxView {
|
||||
return new MenuItem(title, checkStyle, action);
|
||||
}
|
||||
|
||||
public static MenuItem of(String title, MenuItemCheckStyle checkStyle, Runnable action, boolean initialCheckState) {
|
||||
return new MenuItem(title, checkStyle, action, initialCheckState);
|
||||
}
|
||||
|
||||
public Runnable getAction() {
|
||||
return action;
|
||||
}
|
||||
@@ -403,6 +430,15 @@ public class MenuView extends BoxView {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets initial check state.
|
||||
*
|
||||
* @return initial check state
|
||||
*/
|
||||
public boolean isInitialCheckState() {
|
||||
return initialCheckState;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@code title}. Never null, empty or just having white spaces.
|
||||
*
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.shell.component.view.control;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
@@ -38,10 +39,13 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
class MenuViewTests extends AbstractViewTests {
|
||||
|
||||
private static final String SELECTED_FIELD = "activeItemIndex";
|
||||
private static final String RADIO_ACTIVE_FIELD = "radioActive";
|
||||
private static final String CHECKED_ACTIVE_FIELD = "checkedActive";
|
||||
|
||||
@Nested
|
||||
class Construction {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
void constructView() {
|
||||
MenuView view;
|
||||
@@ -69,6 +73,42 @@ class MenuViewTests extends AbstractViewTests {
|
||||
new MenuItem("sub2", MenuItemCheckStyle.RADIO)
|
||||
});
|
||||
assertThat(view.getItems()).hasSize(2);
|
||||
|
||||
view = new MenuView(new MenuItem[] {
|
||||
new MenuItem("sub1", MenuItemCheckStyle.RADIO, null, true),
|
||||
new MenuItem("sub2", MenuItemCheckStyle.RADIO, null, false)
|
||||
});
|
||||
assertThat(view.getItems()).hasSize(2);
|
||||
MenuItem radioActive = (MenuItem) ReflectionTestUtils.getField(view, RADIO_ACTIVE_FIELD);
|
||||
assertThat(radioActive).isNotNull();
|
||||
assertThat(radioActive.getTitle()).isEqualTo("sub1");
|
||||
|
||||
view = new MenuView(new MenuItem[] {
|
||||
new MenuItem("sub1", MenuItemCheckStyle.CHECKED, null, true),
|
||||
new MenuItem("sub2", MenuItemCheckStyle.CHECKED, null, true)
|
||||
});
|
||||
assertThat(view.getItems()).hasSize(2);
|
||||
Set<MenuItem> checkedActive = (Set<MenuItem>) ReflectionTestUtils.getField(view, CHECKED_ACTIVE_FIELD);
|
||||
assertThat(checkedActive).isNotNull();
|
||||
assertThat(checkedActive).hasSize(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructItem() {
|
||||
MenuItem item;
|
||||
Runnable runnable = () -> {};
|
||||
|
||||
item = new MenuItem("title", MenuItemCheckStyle.RADIO, runnable, true);
|
||||
assertThat(item.getTitle()).isEqualTo("title");
|
||||
assertThat(item.getCheckStyle()).isEqualTo(MenuItemCheckStyle.RADIO);
|
||||
assertThat(item.getAction()).isSameAs(runnable);
|
||||
assertThat(item.isInitialCheckState()).isTrue();
|
||||
|
||||
item = MenuItem.of("title", MenuItemCheckStyle.RADIO, runnable, true);
|
||||
assertThat(item.getTitle()).isEqualTo("title");
|
||||
assertThat(item.getCheckStyle()).isEqualTo(MenuItemCheckStyle.RADIO);
|
||||
assertThat(item.getAction()).isSameAs(runnable);
|
||||
assertThat(item.isInitialCheckState()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user