From b0288a1e3e5e641244b628b4632f58cb8a2791e0 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 12 Jul 2021 10:09:20 +0200 Subject: [PATCH] Focus correct input field on syntax error fixes #11 --- .../java/edu/kit/typicalc/presenter/Presenter.java | 12 +++++++++--- .../java/edu/kit/typicalc/view/main/InputBar.java | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/edu/kit/typicalc/presenter/Presenter.java b/src/main/java/edu/kit/typicalc/presenter/Presenter.java index 45bc066..e74e533 100644 --- a/src/main/java/edu/kit/typicalc/presenter/Presenter.java +++ b/src/main/java/edu/kit/typicalc/presenter/Presenter.java @@ -8,6 +8,9 @@ import edu.kit.typicalc.util.Result; import edu.kit.typicalc.view.main.MainView; import edu.kit.typicalc.view.main.MainView.MainViewListener; +import java.util.Optional; + +import static edu.kit.typicalc.view.main.InputBar.ASS_INPUT_FIELD_ID; import static edu.kit.typicalc.view.main.InputBar.TERM_INPUT_FIELD_ID; /** @@ -33,10 +36,13 @@ public class Presenter implements MainViewListener { public void typeInferLambdaString(String lambdaTerm, String typeAssumptions) { Result result = model.getTypeInferer(lambdaTerm, typeAssumptions); if (result.isError()) { - view.displayError(result.unwrapError()); - // focus the input bar again + ParseError e = result.unwrapError(); + view.displayError(e); + // focus the corresponding input field + String inputFieldId = e.getErrorType().equals(Optional.of(ParseError.ErrorType.TERM_ERROR)) + ? TERM_INPUT_FIELD_ID : ASS_INPUT_FIELD_ID; UI.getCurrent().getPage().executeJs( - "setTimeout(() => document.getElementById('" + TERM_INPUT_FIELD_ID + "').focus(), 0)"); + "setTimeout(() => document.getElementById('" + inputFieldId + "').focus(), 0)"); } else { view.setTypeInferenceView(result.unwrap()); } diff --git a/src/main/java/edu/kit/typicalc/view/main/InputBar.java b/src/main/java/edu/kit/typicalc/view/main/InputBar.java index c5cbc7a..014bcff 100644 --- a/src/main/java/edu/kit/typicalc/view/main/InputBar.java +++ b/src/main/java/edu/kit/typicalc/view/main/InputBar.java @@ -35,7 +35,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { private static final String EXAMPLE_BUTTON_ID = "exampleButton"; private static final String LAMBDA_BUTTON_ID = "lambdaButton"; private static final String QUANTIFIER_BUTTON_ID = "quantifier-button"; - private static final String ASS_INPUT_FIELD_ID = "ass-input-field"; + public static final String ASS_INPUT_FIELD_ID = "ass-input-field"; private static final short MAX_INPUT_LENGTH = 1000;