From 70c4f5ce430d0534bafb911860b373d8b53a9726 Mon Sep 17 00:00:00 2001 From: ucrhh Date: Wed, 3 Feb 2021 10:40:23 +0100 Subject: [PATCH] read lambda term from url --- .../edu/kit/typicalc/view/main/InputBar.java | 22 ++++++---- .../kit/typicalc/view/main/MainViewImpl.java | 43 +++++++++++++++++-- .../kit/typicalc/view/main/NotFoundView.java | 26 ++++------- .../edu/kit/typicalc/view/main/UpperBar.java | 9 ++-- 4 files changed, 66 insertions(+), 34 deletions(-) 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 a3a7ec9..1808124 100644 --- a/src/main/java/edu/kit/typicalc/view/main/InputBar.java +++ b/src/main/java/edu/kit/typicalc/view/main/InputBar.java @@ -1,12 +1,5 @@ package edu.kit.typicalc.view.main; -import java.util.Optional; -import java.util.function.Consumer; - -import com.vaadin.flow.component.textfield.TextField; - -import org.apache.commons.lang3.StringUtils; - import com.vaadin.flow.component.Key; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.ButtonVariant; @@ -16,8 +9,13 @@ import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.icon.VaadinIcon; import com.vaadin.flow.component.notification.Notification; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.i18n.LocaleChangeEvent; import com.vaadin.flow.i18n.LocaleChangeObserver; +import org.apache.commons.lang3.StringUtils; + +import java.util.Optional; +import java.util.function.Consumer; /** * Contains components which allow the user to enter a lambda term and start the type inference algorithm. @@ -56,7 +54,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { inputField.setId(INPUT_FIELD_ID); inputField.setClearButtonVisible(true); inputField.addValueChangeListener(event -> onInputFieldValueChange()); - lambdaButton = new Button(getTranslation("root.lambda"), event -> onlambdaButtonClick()); + lambdaButton = new Button(getTranslation("root.lambda"), event -> onLambdaButtonClick()); exampleButton = new Button(getTranslation("root.examplebutton"), event -> onExampleButtonClick()); exampleButton.setId(EXAMPLE_BUTTON_ID); inferTypeButton = new Button(getTranslation("root.typeInfer"), event -> onTypeInferButtonClick(callback)); @@ -88,7 +86,7 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { } } - private void onlambdaButtonClick() { + private void onLambdaButtonClick() { final StringBuilder inputBuilder = new StringBuilder(); final Optional currentInput = inputField.getOptionalValue(); currentInput.ifPresent(inputBuilder::append); @@ -111,4 +109,10 @@ public class InputBar extends HorizontalLayout implements LocaleChangeObserver { public void localeChange(LocaleChangeEvent event) { inferTypeButton.setText(getTranslation("root.typeInfer")); } + + //todo documentation + protected void inferTerm(String term) { + inputField.setValue(term); + inferTypeButton.click(); + } } 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 7816245..91e98a9 100644 --- a/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java +++ b/src/main/java/edu/kit/typicalc/view/main/MainViewImpl.java @@ -6,13 +6,18 @@ import com.vaadin.flow.component.dependency.CssImport; import com.vaadin.flow.component.dependency.JavaScript; import com.vaadin.flow.component.dependency.JsModule; import com.vaadin.flow.component.notification.Notification; -import com.vaadin.flow.router.Location; +import com.vaadin.flow.router.*; import edu.kit.typicalc.model.ModelImpl; import edu.kit.typicalc.model.TypeInfererInterface; import edu.kit.typicalc.model.parser.ParseError; import edu.kit.typicalc.presenter.Presenter; +import edu.kit.typicalc.view.content.infocontent.StartPageView; import edu.kit.typicalc.view.content.typeinferencecontent.TypeInferenceView; +import javax.servlet.http.HttpServletResponse; +import java.nio.charset.StandardCharsets; +import java.util.List; + /** * Contains all the displayed components and builds the applications user interface (UI). * Vaadin's app layout provides the rough structure of the UI. Using this structure the UI always @@ -23,19 +28,22 @@ import edu.kit.typicalc.view.content.typeinferencecontent.TypeInferenceView; @JsModule("./styles/shared-styles.js") @JavaScript("./src/svg-pan-zoom.min.js") @JavaScript("./src/tex-svg-full.js") -public class MainViewImpl extends AppLayout implements MainView { +public class MainViewImpl extends AppLayout implements MainView, HasErrorParameter { private static final long serialVersionUID = -2411433187835906976L; private static final String ROUTE = "infer"; private static final String SLASH = "/"; - private String termToType = "lambda x.x"; //todo replace with real value + private final UpperBar upperBar; + + private String termToType = "x"; //todo replace with real value /** * Creates a new MainViewImpl. */ public MainViewImpl() { setDrawerOpened(false); MainViewListener presenter = new Presenter(new ModelImpl(), this); - addToNavbar(true, new UpperBar(presenter, this::setContent)); + upperBar = new UpperBar(presenter, this::setContent); + addToNavbar(upperBar); addToDrawer(new DrawerContent()); } @@ -52,4 +60,31 @@ public class MainViewImpl extends AppLayout implements MainView { final Notification errorNotification = new ErrorNotification(getTranslation("root." + error.toString())); errorNotification.open(); } + + @Override + public int setErrorParameter( + BeforeEnterEvent event, + ErrorParameter parameter) { + + if (event.getLocation().getPath().matches(ROUTE + SLASH + ".*")) { + List segments = event.getLocation().getSegments(); + String term = segments.get(segments.size() - 1); + upperBar.inferTerm(decodeURL(term)); + return HttpServletResponse.SC_OK; + } else if (event.getLocation().getPath().equals("")) { + setContent(new StartPageView()); + return HttpServletResponse.SC_OK; + } else { + setContent(new NotFoundView(event)); + return HttpServletResponse.SC_NOT_FOUND; + } + } + + private String decodeURL(String encodedUrl) { + try { + return java.net.URLDecoder.decode(encodedUrl, StandardCharsets.UTF_8); + } catch (IllegalArgumentException e) { + return ""; + } + } } diff --git a/src/main/java/edu/kit/typicalc/view/main/NotFoundView.java b/src/main/java/edu/kit/typicalc/view/main/NotFoundView.java index 72f0fda..166b02c 100644 --- a/src/main/java/edu/kit/typicalc/view/main/NotFoundView.java +++ b/src/main/java/edu/kit/typicalc/view/main/NotFoundView.java @@ -1,27 +1,17 @@ package edu.kit.typicalc.view.main; -import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.Label; -import com.vaadin.flow.router.*; - -import javax.servlet.http.HttpServletResponse; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.router.BeforeEnterEvent; +import com.vaadin.flow.router.ParentLayout; @ParentLayout(MainViewImpl.class) -public class NotFoundView extends Div - implements HasErrorParameter { +public class NotFoundView extends VerticalLayout { - private final Label error = new Label(); - public NotFoundView() { - add(error); // todo make more beautiful - } - - @Override - public int setErrorParameter( - BeforeEnterEvent event, - ErrorParameter parameter) { - - error.setText("Cannot find URL: " + event.getLocation().getPath()); - return HttpServletResponse.SC_NOT_FOUND; + public NotFoundView(BeforeEnterEvent event) { + Label error = new Label(event.getLocation().getPath()); + add(error); + setAlignItems(Alignment.CENTER); } } \ No newline at end of file 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 341e7b8..3c82655 100644 --- a/src/main/java/edu/kit/typicalc/view/main/UpperBar.java +++ b/src/main/java/edu/kit/typicalc/view/main/UpperBar.java @@ -10,15 +10,13 @@ import com.vaadin.flow.component.html.H1; import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.icon.VaadinIcon; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; - import edu.kit.typicalc.view.content.infocontent.StartPageView; import edu.kit.typicalc.view.main.MainView.MainViewListener; +import org.apache.commons.lang3.StringUtils; import java.util.HashMap; import java.util.function.Consumer; -import org.apache.commons.lang3.StringUtils; - /** * Contains all the components constantly shown in the upper part of the webage. @@ -93,4 +91,9 @@ public class UpperBar extends HorizontalLayout { Dialog helpDialog = new HelpDialog(); helpDialog.open(); } + + //TODO documentation + protected void inferTerm(String term) { + inputBar.inferTerm(term); + } }