This commit is contained in:
Johanna Stuber 2021-02-03 11:08:10 +01:00
commit c44082693d
4 changed files with 66 additions and 34 deletions

View File

@ -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<String> 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();
}
}

View File

@ -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<NotFoundException> {
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<NotFoundException> parameter) {
if (event.getLocation().getPath().matches(ROUTE + SLASH + ".*")) {
List<String> 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 "";
}
}
}

View File

@ -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<NotFoundException> {
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<NotFoundException> 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);
}
}

View File

@ -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);
}
}