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 f26d63e..dc48e48 100644 --- a/src/main/java/edu/kit/typicalc/view/main/InputBar.java +++ b/src/main/java/edu/kit/typicalc/view/main/InputBar.java @@ -38,6 +38,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { private static final short MAX_INPUT_LENGTH = 1000; + private final transient Consumer>> callback; private final TextField inputField; private TypeAssumptionsArea typeAssumptionsArea; private final Button inferTypeButton; @@ -50,6 +51,8 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { * @param callback Consumer to call the inferType()-method in UpperBar */ protected InputBar(Consumer>> callback) { + this.callback = callback; + Button infoIcon = new Button(new Icon(VaadinIcon.INFO_CIRCLE)); infoIcon.addClickListener(event -> onInfoIconClick()); @@ -78,7 +81,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { typeAssumptionsArea = new TypeAssumptionsArea(); Button exampleButton = new Button(VaadinIcon.PAPERCLIP.create(), event -> onExampleButtonClick()); exampleButton.setId(EXAMPLE_BUTTON_ID); - inferTypeButton = new Button("", event -> onTypeInferButtonClick(callback)); + inferTypeButton = new Button("", event -> onTypeInferButtonClick()); inferTypeButton.addClickShortcut(Key.ENTER).listenOn(this); inferTypeButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY); inferTypeButton.setId(INFER_BUTTON_ID); @@ -88,13 +91,12 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { } /** - * Sets the provided string as the value of the inputField and starts the type inference algorithm. + * Sets the provided string as the value of the inputField. * * @param term the provided string */ - protected void inferTerm(String term) { + protected void setTerm(String term) { inputField.setValue(term); - UI.getCurrent().getPage().executeJs("document.getElementById($0).click()", INFER_BUTTON_ID); } /** @@ -106,15 +108,10 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { typeAssumptionsArea = new TypeAssumptionsArea(typeAssumptions); } - private void onTypeInferButtonClick(Consumer>> callback) { + private void onTypeInferButtonClick() { String currentInput = inputField.getOptionalValue().orElse(StringUtils.EMPTY); inputField.blur(); - if ("".equals(currentInput)) { - UI.getCurrent().getPage().setTitle(getTranslation("root.typicalc")); - } else { - UI.getCurrent().getPage().setTitle(getTranslation("root.typicalc") + " - " + currentInput); - } callback.accept(Pair.of(currentInput, typeAssumptionsArea.getTypeAssumptions())); } @@ -123,7 +120,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { } private void onExampleButtonClick() { - Dialog exampleDialog = new ExampleDialog(this::inferTerm); + Dialog exampleDialog = new ExampleDialog(this::setTerm); exampleDialog.open(); } diff --git a/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java b/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java index 233b3b5..c86500f 100644 --- a/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java +++ b/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java @@ -20,6 +20,7 @@ import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; /** @@ -41,6 +42,7 @@ public class MainViewImpl extends AppLayout public static final String PAGE_TITLE = "Typicalc"; private final UpperBar upperBar; + private transient Optional tiv = Optional.empty(); /** * Creates a new MainViewImpl. @@ -55,8 +57,8 @@ public class MainViewImpl extends AppLayout @Override public void setTypeInferenceView(TypeInfererInterface typeInferer) { - TypeInferenceView tiv = new TypeInferenceView(typeInferer); - setContent(tiv); + tiv = Optional.of(new TypeInferenceView(typeInferer)); + setContent(tiv.get()); } @Override @@ -67,6 +69,7 @@ public class MainViewImpl extends AppLayout @Override public void beforeEnter(BeforeEnterEvent event) { + tiv = Optional.empty(); if (event.getLocation().getPath().matches(TypeInferenceView.ROUTE + "/.*")) { Location url = event.getLocation(); List segments = url.getSegments(); @@ -83,6 +86,11 @@ public class MainViewImpl extends AppLayout } } + @Override + protected void afterNavigation() { + // this method ensures that the content is visible after navigation + tiv.ifPresent(this::setContent); + } private void setTermInURL(Pair> lambdaTermAndAssumptions) { String lambdaTerm = lambdaTermAndAssumptions.getLeft(); @@ -99,7 +107,7 @@ public class MainViewImpl extends AppLayout if (types.length() > 0) { typeAssumptions = "?" + types.toString(); } - UI.getCurrent().getPage().getHistory().replaceState(null, + UI.getCurrent().getPage().getHistory().pushState(null, new Location(TypeInferenceView.ROUTE + "/" + lambdaTerm + typeAssumptions)); } diff --git a/src/main/java/edu/kit/typicalc/view/main/UpperBar.java b/src/main/java/edu/kit/typicalc/view/main/UpperBar.java index 5d2ff9a..71a1bf6 100644 --- a/src/main/java/edu/kit/typicalc/view/main/UpperBar.java +++ b/src/main/java/edu/kit/typicalc/view/main/UpperBar.java @@ -62,25 +62,30 @@ public class UpperBar extends HorizontalLayout { } /** - * Starts the type inference algorithm by passing the required arguments to the MainViewListener. + * Starts the type inference algorithm by passing the required arguments to the MainViewListener + * and updating the URL. * * @param termAndAssumptions the lambda term to be type-inferred and the type assumptions to use */ protected void typeInfer(Pair> termAndAssumptions) { setTermInURL.accept(termAndAssumptions); - presenter.typeInferLambdaString(termAndAssumptions.getLeft(), termAndAssumptions.getRight()); + startInfer(termAndAssumptions.getLeft(), termAndAssumptions.getRight()); + } + + private void startInfer(String term, Map typeAssumptions) { + presenter.typeInferLambdaString(term, typeAssumptions); } /** - * Calls the inferTerm method in {@link InputBar} with the provided - * string as the argument. + * Sets the lambda term and type assumptions in the InputBar and starts the inference. * * @param term the provided string * @param typeAssumptions type assumptions to use */ protected void inferTerm(String term, Map typeAssumptions) { inputBar.setTypeAssumptions(typeAssumptions); - inputBar.inferTerm(term); + inputBar.setTerm(term); + startInfer(term, typeAssumptions); } private void onHelpIconClick() {