mirror of
https://gitlab.kit.edu/uskyk/typicalc.git
synced 2024-11-08 10:20:41 +00:00
Handle empty type assumptions
This commit is contained in:
parent
3a0ee669d4
commit
2d6b33190c
@ -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<String> varBinder = new Binder<>();
|
||||
private final Binder<String> 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<String> 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<String> 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
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user