Skip to content

Commit 323412d

Browse files
committed
Use lambdas & cleanup
1 parent 9f80373 commit 323412d

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

ui/src/main/java/edu/wpi/grip/ui/pipeline/StepController.java

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,12 @@
1717
import com.google.inject.assistedinject.Assisted;
1818

1919
import java.io.InputStream;
20-
import java.util.ArrayList;
2120
import java.util.Collection;
22-
import java.util.List;
23-
21+
import java.util.function.Predicate;
2422
import javafx.animation.KeyFrame;
2523
import javafx.animation.KeyValue;
2624
import javafx.animation.Timeline;
27-
import javafx.animation.TimelineBuilder;
2825
import javafx.beans.property.DoubleProperty;
29-
import javafx.event.ActionEvent;
30-
import javafx.event.EventHandler;
3126
import javafx.fxml.FXML;
3227
import javafx.scene.Node;
3328
import javafx.scene.control.Button;
@@ -52,7 +47,6 @@ public class StepController implements Controller {
5247
private final ExceptionWitnessResponderButton.Factory exceptionWitnessResponderButtonFactory;
5348
private final StepDragService stepDragService;
5449
private final Step step;
55-
private final List<InputSocketController> inputSockets;
5650
private boolean expanded = true;
5751
@FXML
5852
private VBox root;
@@ -75,6 +69,8 @@ public class StepController implements Controller {
7569

7670
private static final Image UP_ARROW = new Image("/edu/wpi/grip/ui/icons/up.png");
7771
private static final Image DOWN_ARROW = new Image("/edu/wpi/grip/ui/icons/down.png");
72+
private static final Predicate<InputSocketController> interactiveInputSocketFilter
73+
= i -> !i.getSocket().getSocketHint().getView().equals(SocketHint.View.NONE);
7874

7975
@Inject
8076
StepController(Pipeline pipeline,
@@ -89,7 +85,6 @@ public class StepController implements Controller {
8985
this.exceptionWitnessResponderButtonFactory = exceptionWitnessResponderButtonFactory;
9086
this.stepDragService = stepDragService;
9187
this.step = step;
92-
inputSockets = new ArrayList<>();
9388
}
9489

9590
@FXML
@@ -113,11 +108,7 @@ private void initialize() {
113108

114109
// Add a SocketControlView for each input socket and output socket
115110
for (InputSocket<?> inputSocket : step.getInputSockets()) {
116-
InputSocketController tempSocket = inputSocketControllerFactory.create(inputSocket);
117-
inputSocketMapManager.add(tempSocket);
118-
if (!inputSocket.getSocketHint().getView().equals(SocketHint.View.NONE)) {
119-
inputSockets.add(tempSocket);
120-
}
111+
inputSocketMapManager.add(inputSocketControllerFactory.create(inputSocket));
121112
}
122113

123114
for (OutputSocket<?> outputSocket : step.getOutputSockets()) {
@@ -178,20 +169,17 @@ private void moveStepRight() {
178169
@FXML
179170
private void expand() {
180171
if (expanded) {
181-
for (InputSocketController input : inputSockets) {
182-
inputs.setMaxHeight(inputs.getHeight());
183-
inputs.setPrefHeight(inputs.getHeight());
184-
if (input.getSocket().getConnections().isEmpty()) {
185-
fadeOut(input);
186-
}
187-
}
172+
inputSocketMapManager.keySet().stream()
173+
.filter(interactiveInputSocketFilter)
174+
.filter(i -> i.getSocket().getConnections().isEmpty())
175+
.forEach(this::fadeOut);
188176
closeUp();
189177
expandIcon.setImage(DOWN_ARROW);
190178
expanded = false;
191179
} else {
192-
for (InputSocketController input : inputSockets) {
193-
fadeIn(input);
194-
}
180+
inputSocketMapManager.keySet().stream()
181+
.filter(interactiveInputSocketFilter)
182+
.forEach(this::fadeIn);
195183
reopen();
196184
expandIcon.setImage(UP_ARROW);
197185
expanded = true;
@@ -209,12 +197,9 @@ private void fadeOut(InputSocketController input) {
209197
Timeline fadeOut = new Timeline(
210198
new KeyFrame(Duration.ZERO, new KeyValue(opacity, 1.0)),
211199
new KeyFrame(new Duration(100), new KeyValue(opacity, 0.0)));
212-
fadeOut.setOnFinished(new EventHandler<ActionEvent>() {
213-
@Override
214-
public void handle(ActionEvent event) {
215-
input.getRoot().setVisible(false);
216-
input.getRoot().setManaged(false);
217-
}
200+
fadeOut.setOnFinished(event -> {
201+
input.getRoot().setVisible(false);
202+
input.getRoot().setManaged(false);
218203
});
219204
fadeOut.play();
220205
}
@@ -229,34 +214,28 @@ private void fadeIn(InputSocketController input) {
229214
DoubleProperty opacity = input.getRoot().opacityProperty();
230215
Timeline fadeIn = new Timeline(
231216
new KeyFrame(new Duration(100), new KeyValue(opacity, 1.0)));
232-
fadeIn.setOnFinished(new EventHandler<ActionEvent>() {
233-
@Override
234-
public void handle(ActionEvent event) {
235-
for (InputSocketController input : inputSockets) {
236-
input.getRoot().setManaged(true);
237-
}
238-
}
239-
});
217+
fadeIn.setOnFinished(
218+
event -> inputSocketMapManager.keySet().forEach(i -> input.getRoot().setManaged(true)));
240219
fadeIn.play();
241220
}
242221

243222
/**
244223
* Makes an animation to make the input vbox slide closed over .25 seconds
245224
*/
246225
private void closeUp() {
247-
Timeline animation = TimelineBuilder.create().cycleCount(1).keyFrames(
226+
Timeline animation = new Timeline(
248227
new KeyFrame(Duration.seconds(0.25),
249-
new KeyValue(inputs.prefHeightProperty(), 0))).build();
228+
new KeyValue(inputs.prefHeightProperty(), 0)));
250229
animation.play();
251230
}
252231

253232
/**
254233
* Makes an animation to make the input vbox slide open over .1 seconds
255234
*/
256235
private void reopen() {
257-
Timeline animation = TimelineBuilder.create().cycleCount(1).keyFrames(
236+
Timeline animation = new Timeline(
258237
new KeyFrame(Duration.seconds(0.1),
259-
new KeyValue(inputs.prefHeightProperty(), inputs.getMaxHeight()))).build();
238+
new KeyValue(inputs.prefHeightProperty(), inputs.getMaxHeight())));
260239
animation.play();
261240
}
262241

0 commit comments

Comments
 (0)