From 2d6b33190c35d48a2842f7062bde11117f19fdf7 Mon Sep 17 00:00:00 2001 From: Moritz Dieing <63721811+moritzdieing@users.noreply.github.com> Date: Wed, 10 Mar 2021 20:41:40 +0100 Subject: [PATCH] Handle empty type assumptions --- .../view/main/TypeAssumptionField.java | 28 ++++++++++++++----- .../view/main/TypeAssumptionsArea.java | 3 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/edu/kit/typicalc/view/main/TypeAssumptionField.java b/src/main/java/edu/kit/typicalc/view/main/TypeAssumptionField.java index 1391656..8033a43 100644 --- a/src/main/java/edu/kit/typicalc/view/main/TypeAssumptionField.java +++ b/src/main/java/edu/kit/typicalc/view/main/TypeAssumptionField.java @@ -9,6 +9,7 @@ import com.vaadin.flow.component.icon.VaadinIcon; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.data.binder.Binder; +import com.vaadin.flow.data.value.ValueChangeMode; import com.vaadin.flow.i18n.LocaleChangeEvent; import com.vaadin.flow.i18n.LocaleChangeObserver; import edu.kit.typicalc.model.parser.TypeAssumptionParser; @@ -48,7 +49,10 @@ public class TypeAssumptionField extends HorizontalLayout implements LocaleChang private final transient TypeAssumptionParser parser = new TypeAssumptionParser(); private final TextField variableInputField; private final TextField typeInputField; - + + private final Binder varBinder = new Binder<>(); + private final Binder typeBinder = new Binder<>(); + /** * Creates a new TypeAssumptionField with initial values and a callback to remove this * type assumption from the {@link TypeAssumptionsArea}. @@ -81,6 +85,9 @@ public class TypeAssumptionField extends HorizontalLayout implements LocaleChang Button deleteButton = new Button(minusIcon, event -> deleteSelf.accept(this)); deleteButton.setId(ASS_DELETE_BUTTON_ID); deleteButton.setTabIndex(-1); + variableInputField.addBlurListener(event -> typeBinder.validate()); + typeInputField.addBlurListener(event -> varBinder.validate()); + addValidatior(); add(variableInputField, typeInputField, deleteButton); setId(ASSUMPTIONS_FIELD_ID); @@ -93,7 +100,7 @@ public class TypeAssumptionField extends HorizontalLayout implements LocaleChang * @return true if the variable matches the syntax, false if not */ protected boolean hasCorrectVariable(String variable) { - return variable.isEmpty() || TypeAssumptionParser.TYPE_NAME_PATTERN.matcher(variable).matches(); + return TypeAssumptionParser.TYPE_NAME_PATTERN.matcher(variable).matches(); } /** @@ -103,18 +110,16 @@ public class TypeAssumptionField extends HorizontalLayout implements LocaleChang * @return true if the type matches the syntax, false if not */ protected boolean hasCorrectType(String type) { - return type.isEmpty() || parser.parseType(parseBackType(type)).isOk(); + return parser.parseType(parseBackType(type)).isOk(); } private void addValidatior() { - Binder varBinder = new Binder<>(); varBinder.forField(variableInputField) - .withValidator(this::hasCorrectVariable, StringUtils.EMPTY) + .withValidator(var -> (hasCorrectVariable(var) || isEmpty()), StringUtils.EMPTY) .bind(o -> variableInputField.getEmptyValue(), null); variableInputField.setReadOnly(false); - Binder typeBinder = new Binder<>(); typeBinder.forField(typeInputField) - .withValidator(this::hasCorrectType, StringUtils.EMPTY) + .withValidator(type -> (hasCorrectVariable(type) || isEmpty()), StringUtils.EMPTY) .bind(o -> typeInputField.getEmptyValue(), null); typeInputField.setReadOnly(false); } @@ -129,6 +134,15 @@ public class TypeAssumptionField extends HorizontalLayout implements LocaleChang } return new String(rawTypeArray); } + + /** + * Checks if both text fields of this type assumption are empty. + * + * @return true if both text fields are empty, false if not + */ + protected boolean isEmpty() { + return getVariable().isEmpty() && getType().isEmpty(); + } /** * This method is called when the dialog containing this field is reopened. Since Vaadin somehow detaches the diff --git a/src/main/java/edu/kit/typicalc/view/main/TypeAssumptionsArea.java b/src/main/java/edu/kit/typicalc/view/main/TypeAssumptionsArea.java index 6eaef76..1910136 100644 --- a/src/main/java/edu/kit/typicalc/view/main/TypeAssumptionsArea.java +++ b/src/main/java/edu/kit/typicalc/view/main/TypeAssumptionsArea.java @@ -96,7 +96,8 @@ public class TypeAssumptionsArea extends Dialog implements LocaleChangeObserver private void closeAction() { for (TypeAssumptionField field : fields) { - if (!(field.hasCorrectType(field.getType()) && field.hasCorrectVariable(field.getVariable()))) { + if (!(field.hasCorrectType(field.getType()) && field.hasCorrectVariable(field.getVariable())) + && !field.isEmpty()) { invalidInputNotification.open(); return; }