|
| 1 | +package ifml3.app.view.ui.internal; |
| 2 | + |
| 3 | +import ifml3.api.ComponentConnector; |
| 4 | +import ifml3.api.internal.BaseComponentConnector; |
| 5 | +import ifml3.api.internal.BaseSubscriber; |
| 6 | +import ifml3.app.view.ui.UIComponent; |
| 7 | +import ifml3.ui.TextSanitizer; |
| 8 | +import ifml3.ui.UserCommand; |
| 9 | +import ifml3.ui.UserInterface; |
| 10 | +import ifml3.ui.UserMessage; |
| 11 | +import ifml3.ui.internal.UserTextSanitizer; |
| 12 | +import java.util.concurrent.Flow.Subscriber; |
| 13 | +import java.util.concurrent.SubmissionPublisher; |
| 14 | +import javafx.event.ActionEvent; |
| 15 | +import javafx.scene.Parent; |
| 16 | +import javafx.scene.control.TextArea; |
| 17 | +import javafx.scene.control.TextField; |
| 18 | +import javafx.scene.layout.VBox; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +public class PlayerComponent implements UIComponent, UserInterface { |
| 23 | + |
| 24 | + private static final Logger logger = LoggerFactory.getLogger(PlayerComponent.class); |
| 25 | + |
| 26 | + private final SubmissionPublisher<UserCommand> publisher; |
| 27 | + private final Subscriber<UserMessage> subscriber; |
| 28 | + private final TextSanitizer sanitizer; |
| 29 | + private final ComponentConnector<UserMessage, UserCommand> connector; |
| 30 | + private final VBox vBox; |
| 31 | + private final TextArea textArea; |
| 32 | + private final TextField textField; |
| 33 | + |
| 34 | + public PlayerComponent() { |
| 35 | + // @todo at least sanitizer should be as dependency |
| 36 | + this.sanitizer = new UserTextSanitizer(); |
| 37 | + this.publisher = new SubmissionPublisher<>(); |
| 38 | + this.textArea = new TextArea(); |
| 39 | + this.textArea.setEditable(false); |
| 40 | + this.textArea.setPrefRowCount(1_000); |
| 41 | + this.textField = new TextField(); |
| 42 | + this.textField.setOnAction(this::processCommand); |
| 43 | + this.vBox = new VBox(textArea, textField); |
| 44 | + this.subscriber = new BaseSubscriber<>(this::processMessage); |
| 45 | + this.connector = new BaseComponentConnector<>(subscriber, publisher); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Parent get() { |
| 50 | + return vBox; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public ComponentConnector<UserMessage, UserCommand> uiConnector() { |
| 55 | + return connector; |
| 56 | + } |
| 57 | + |
| 58 | + private void processCommand(final ActionEvent event) { |
| 59 | + final var text = textField.getText(); |
| 60 | + logger.info("Input from user: '{}'", text); |
| 61 | + textField.setText(""); |
| 62 | + textArea.appendText("> " + text + "\n"); |
| 63 | + final var input = sanitizer.sanitize(text); |
| 64 | + // @todo preprocessing needed |
| 65 | + publisher.submit(() -> input); |
| 66 | + } |
| 67 | + |
| 68 | + private void processMessage(final UserMessage message) { |
| 69 | + logger.info("Get message for user: {}", message); |
| 70 | + textArea.appendText("\n" + message.message() + "\n\n"); |
| 71 | + } |
| 72 | +} |
0 commit comments