Skip to content

Commit 4a56d2a

Browse files
committed
Support initial checked state
- In MenuView initial check state can be set. - Relates #832
1 parent e149dfa commit 4a56d2a

File tree

2 files changed

+77
-1
lines changed
  • spring-shell-core/src

2 files changed

+77
-1
lines changed

spring-shell-core/src/main/java/org/springframework/shell/component/view/control/MenuView.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ public void setItems(@Nullable List<MenuItem> items) {
9494
if (!items.isEmpty()) {
9595
activeItemIndex = 0;
9696
}
97+
items.forEach(i -> {
98+
if (i.initialCheckState && i.getCheckStyle() == MenuItemCheckStyle.CHECKED) {
99+
checkedActive.add(i);
100+
}
101+
else if (i.initialCheckState && i.getCheckStyle() == MenuItemCheckStyle.RADIO) {
102+
radioActive = i;
103+
}
104+
});
97105
}
98106
}
99107

@@ -321,6 +329,7 @@ public static class MenuItem {
321329
private final MenuItemCheckStyle checkStyle;
322330
private final List<MenuItem> items;
323331
private Runnable action;
332+
private boolean initialCheckState = false;
324333

325334
/**
326335
* Construct menu item with a title.
@@ -342,19 +351,33 @@ public MenuItem(String title, MenuItemCheckStyle checkStyle) {
342351
}
343352

344353
/**
345-
* Construct menu item with a title and a check style.
354+
* Construct menu item with a title, a check style and a runnable.
346355
*
347356
* @param title the title
348357
* @param checkStyle the check style
349358
* @param action the action to run when item is chosen
350359
*/
351360
public MenuItem(String title, MenuItemCheckStyle checkStyle, Runnable action) {
361+
this(title, checkStyle, action, false);
362+
}
363+
364+
/**
365+
* Construct menu item with a title, a check style, a runnable and initial
366+
* checked state.
367+
*
368+
* @param title the title
369+
* @param checkStyle the check style
370+
* @param action the action to run when item is chosen
371+
* @param initialCheckState initial checked state
372+
*/
373+
public MenuItem(String title, MenuItemCheckStyle checkStyle, Runnable action, boolean initialCheckState) {
352374
Assert.state(StringUtils.hasText(title), "title must have text");
353375
Assert.notNull(checkStyle, "check style cannot be null");
354376
this.title = title;
355377
this.checkStyle = checkStyle;
356378
this.action = action;
357379
this.items = null;
380+
this.initialCheckState = initialCheckState;
358381
}
359382

360383
protected MenuItem(String title, MenuItem[] items) {
@@ -395,6 +418,10 @@ public static MenuItem of(String title, MenuItemCheckStyle checkStyle, Runnable
395418
return new MenuItem(title, checkStyle, action);
396419
}
397420

421+
public static MenuItem of(String title, MenuItemCheckStyle checkStyle, Runnable action, boolean initialCheckState) {
422+
return new MenuItem(title, checkStyle, action, initialCheckState);
423+
}
424+
398425
public Runnable getAction() {
399426
return action;
400427
}
@@ -403,6 +430,15 @@ public void setAction(Runnable action) {
403430
this.action = action;
404431
}
405432

433+
/**
434+
* Gets initial check state.
435+
*
436+
* @return initial check state
437+
*/
438+
public boolean isInitialCheckState() {
439+
return initialCheckState;
440+
}
441+
406442
/**
407443
* Get a {@code title}. Never null, empty or just having white spaces.
408444
*

spring-shell-core/src/test/java/org/springframework/shell/component/view/control/MenuViewTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.time.Duration;
1919
import java.util.Arrays;
20+
import java.util.Set;
2021

2122
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Nested;
@@ -38,10 +39,13 @@
3839
class MenuViewTests extends AbstractViewTests {
3940

4041
private static final String SELECTED_FIELD = "activeItemIndex";
42+
private static final String RADIO_ACTIVE_FIELD = "radioActive";
43+
private static final String CHECKED_ACTIVE_FIELD = "checkedActive";
4144

4245
@Nested
4346
class Construction {
4447

48+
@SuppressWarnings("unchecked")
4549
@Test
4650
void constructView() {
4751
MenuView view;
@@ -69,6 +73,42 @@ void constructView() {
6973
new MenuItem("sub2", MenuItemCheckStyle.RADIO)
7074
});
7175
assertThat(view.getItems()).hasSize(2);
76+
77+
view = new MenuView(new MenuItem[] {
78+
new MenuItem("sub1", MenuItemCheckStyle.RADIO, null, true),
79+
new MenuItem("sub2", MenuItemCheckStyle.RADIO, null, false)
80+
});
81+
assertThat(view.getItems()).hasSize(2);
82+
MenuItem radioActive = (MenuItem) ReflectionTestUtils.getField(view, RADIO_ACTIVE_FIELD);
83+
assertThat(radioActive).isNotNull();
84+
assertThat(radioActive.getTitle()).isEqualTo("sub1");
85+
86+
view = new MenuView(new MenuItem[] {
87+
new MenuItem("sub1", MenuItemCheckStyle.CHECKED, null, true),
88+
new MenuItem("sub2", MenuItemCheckStyle.CHECKED, null, true)
89+
});
90+
assertThat(view.getItems()).hasSize(2);
91+
Set<MenuItem> checkedActive = (Set<MenuItem>) ReflectionTestUtils.getField(view, CHECKED_ACTIVE_FIELD);
92+
assertThat(checkedActive).isNotNull();
93+
assertThat(checkedActive).hasSize(2);
94+
}
95+
96+
@Test
97+
void constructItem() {
98+
MenuItem item;
99+
Runnable runnable = () -> {};
100+
101+
item = new MenuItem("title", MenuItemCheckStyle.RADIO, runnable, true);
102+
assertThat(item.getTitle()).isEqualTo("title");
103+
assertThat(item.getCheckStyle()).isEqualTo(MenuItemCheckStyle.RADIO);
104+
assertThat(item.getAction()).isSameAs(runnable);
105+
assertThat(item.isInitialCheckState()).isTrue();
106+
107+
item = MenuItem.of("title", MenuItemCheckStyle.RADIO, runnable, true);
108+
assertThat(item.getTitle()).isEqualTo("title");
109+
assertThat(item.getCheckStyle()).isEqualTo(MenuItemCheckStyle.RADIO);
110+
assertThat(item.getAction()).isSameAs(runnable);
111+
assertThat(item.isInitialCheckState()).isTrue();
72112
}
73113

74114
@Test

0 commit comments

Comments
 (0)