Skip to content

Commit c532dc2

Browse files
committed
Flow system for UI components
- Implement basic flow system with builder patterns to easily use components and hook those together as a flow. - Fixes #364
1 parent e5151bb commit c532dc2

30 files changed

+3017
-6
lines changed

spring-shell-core/src/main/java/org/springframework/shell/component/ConfirmationInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ConfirmationInput(Terminal terminal, String name, boolean defaultValue,
6363
}
6464

6565
@Override
66-
protected ConfirmationInputContext getThisContext(ComponentContext<?> context) {
66+
public ConfirmationInputContext getThisContext(ComponentContext<?> context) {
6767
if (context != null && currentContext == context) {
6868
return currentContext;
6969
}

spring-shell-core/src/main/java/org/springframework/shell/component/MultiItemSelector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public MultiItemSelector(Terminal terminal, List<I> items, String name, Comparat
5252
}
5353

5454
@Override
55-
protected MultiItemSelectorContext<T, I> getThisContext(ComponentContext<?> context) {
55+
public MultiItemSelectorContext<T, I> getThisContext(ComponentContext<?> context) {
5656
if (context != null && currentContext == context) {
5757
return currentContext;
5858
}

spring-shell-core/src/main/java/org/springframework/shell/component/PathInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public PathInput(Terminal terminal, String name, Function<PathInputContext, List
6060
}
6161

6262
@Override
63-
protected PathInputContext getThisContext(ComponentContext<?> context) {
63+
public PathInputContext getThisContext(ComponentContext<?> context) {
6464
if (context != null && currentContext == context) {
6565
return currentContext;
6666
}

spring-shell-core/src/main/java/org/springframework/shell/component/SingleItemSelector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public SingleItemSelector(Terminal terminal, List<I> items, String name, Compara
5252
}
5353

5454
@Override
55-
protected SingleItemSelectorContext<T, I> getThisContext(ComponentContext<?> context) {
55+
public SingleItemSelectorContext<T, I> getThisContext(ComponentContext<?> context) {
5656
if (context != null && currentContext == context) {
5757
return currentContext;
5858
}

spring-shell-core/src/main/java/org/springframework/shell/component/StringInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void setMaskCharater(Character maskCharacter) {
6868
}
6969

7070
@Override
71-
protected StringInputContext getThisContext(ComponentContext<?> context) {
71+
public StringInputContext getThisContext(ComponentContext<?> context) {
7272
if (context != null && currentContext == context) {
7373
return currentContext;
7474
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.component.flow;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.function.Consumer;
21+
import java.util.function.Function;
22+
23+
import org.jline.utils.AttributedString;
24+
25+
import org.springframework.shell.component.ConfirmationInput.ConfirmationInputContext;
26+
import org.springframework.shell.component.flow.ComponentFlow.BaseBuilder;
27+
import org.springframework.shell.component.flow.ComponentFlow.Builder;
28+
29+
/**
30+
* Base impl for {@link ConfirmationInputSpec}.
31+
*
32+
* @author Janne Valkealahti
33+
*/
34+
public abstract class BaseConfirmationInput extends BaseInput<ConfirmationInputSpec> implements ConfirmationInputSpec {
35+
36+
private String name;
37+
private Boolean defaultValue;
38+
private Function<ConfirmationInputContext, List<AttributedString>> renderer;
39+
private List<Consumer<ConfirmationInputContext>> preHandlers = new ArrayList<>();
40+
private List<Consumer<ConfirmationInputContext>> postHandlers = new ArrayList<>();
41+
private boolean storeResult = true;
42+
private String templateLocation;
43+
private Function<ConfirmationInputContext, String> next;
44+
45+
public BaseConfirmationInput(BaseBuilder builder, String id) {
46+
super(builder, id);
47+
}
48+
49+
@Override
50+
public ConfirmationInputSpec name(String name) {
51+
this.name = name;
52+
return this;
53+
}
54+
55+
@Override
56+
public ConfirmationInputSpec defaultValue(Boolean defaultValue) {
57+
this.defaultValue = defaultValue;
58+
return this;
59+
}
60+
61+
@Override
62+
public ConfirmationInputSpec renderer(Function<ConfirmationInputContext, List<AttributedString>> renderer) {
63+
this.renderer = renderer;
64+
return this;
65+
}
66+
67+
@Override
68+
public ConfirmationInputSpec template(String location) {
69+
this.templateLocation = location;
70+
return this;
71+
}
72+
73+
@Override
74+
public ConfirmationInputSpec preHandler(Consumer<ConfirmationInputContext> handler) {
75+
this.preHandlers.add(handler);
76+
return this;
77+
}
78+
79+
@Override
80+
public ConfirmationInputSpec postHandler(Consumer<ConfirmationInputContext> handler) {
81+
this.postHandlers.add(handler);
82+
return this;
83+
}
84+
85+
@Override
86+
public ConfirmationInputSpec storeResult(boolean store) {
87+
this.storeResult = store;
88+
return this;
89+
}
90+
91+
@Override
92+
public ConfirmationInputSpec next(Function<ConfirmationInputContext, String> next) {
93+
this.next = next;
94+
return this;
95+
}
96+
97+
@Override
98+
public Builder and() {
99+
getBuilder().addConfirmationInput(this);
100+
return getBuilder();
101+
}
102+
103+
@Override
104+
public ConfirmationInputSpec getThis() {
105+
return this;
106+
}
107+
108+
public String getName() {
109+
return name;
110+
}
111+
112+
public boolean getDefaultValue() {
113+
return defaultValue != null ? defaultValue : true;
114+
}
115+
116+
public Function<ConfirmationInputContext, List<AttributedString>> getRenderer() {
117+
return renderer;
118+
}
119+
120+
public String getTemplateLocation() {
121+
return templateLocation;
122+
}
123+
124+
public List<Consumer<ConfirmationInputContext>> getPreHandlers() {
125+
return preHandlers;
126+
}
127+
128+
public List<Consumer<ConfirmationInputContext>> getPostHandlers() {
129+
return postHandlers;
130+
}
131+
132+
public boolean isStoreResult() {
133+
return storeResult;
134+
}
135+
136+
public Function<ConfirmationInputContext, String> getNext() {
137+
return next;
138+
}
139+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.component.flow;
17+
18+
import org.springframework.core.Ordered;
19+
import org.springframework.shell.component.flow.ComponentFlow.BaseBuilder;
20+
21+
/**
22+
* Base impl for specs.
23+
*
24+
* @author Janne Valkealahti
25+
*/
26+
public abstract class BaseInput<T extends BaseInputSpec<T>> implements Ordered, BaseInputSpec<T> {
27+
28+
private final BaseBuilder builder;
29+
private final String id;
30+
private int order;
31+
32+
BaseInput(BaseBuilder builder, String id) {
33+
this.builder = builder;
34+
this.id = id;
35+
}
36+
37+
@Override
38+
public int getOrder() {
39+
return order;
40+
}
41+
42+
public void setOrder(int order) {
43+
this.order = order;
44+
}
45+
46+
@Override
47+
public T order(int order) {
48+
this.order = order;
49+
return getThis();
50+
}
51+
52+
public BaseBuilder getBuilder() {
53+
return builder;
54+
}
55+
56+
public String getId() {
57+
return id;
58+
}
59+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.component.flow;
17+
18+
/**
19+
* Base spec for other specs.
20+
*
21+
* @author Janne Valkealahti
22+
*/
23+
public interface BaseInputSpec<T extends BaseInputSpec<T>> {
24+
25+
/**
26+
* Sets order of this component.
27+
*
28+
* @param order the order
29+
* @return a builder
30+
*/
31+
T order(int order);
32+
33+
/**
34+
* Usual this trick to get typed child.
35+
*
36+
* @return a builder
37+
*/
38+
T getThis();
39+
}

0 commit comments

Comments
 (0)