io.devbench.uibuilder.components.form.UIBuilderFormResettableField Maven / Gradle / Ivy
/*
*
* Copyright © 2018 Webvalto Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.devbench.uibuilder.components.form;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.data.binder.HasItems;
import io.devbench.uibuilder.components.form.exception.FormException;
import io.devbench.uibuilder.components.form.exception.FormResettableFieldException;
import io.devbench.uibuilder.core.utils.reflection.PropertyMetadata;
import org.apache.commons.lang3.SerializationUtils;
import java.io.Serializable;
import java.util.Collection;
public class UIBuilderFormResettableField {
private UIBuilderForm form;
private Component component;
private PropertyMetadata> propertyMetadata;
private Serializable original;
private boolean forceValue = false;
public UIBuilderFormResettableField(UIBuilderForm form, Component component, PropertyMetadata> propertyMetadata, boolean forceValue) {
this(form, component, propertyMetadata, propertyMetadata.getValue());
this.forceValue = forceValue;
}
public UIBuilderFormResettableField(UIBuilderForm form, Component component, PropertyMetadata> propertyMetadata, Serializable original) {
this.form = form;
this.component = component;
this.propertyMetadata = propertyMetadata;
this.original = SerializationUtils.clone(original);
}
public boolean isForceValue() {
return forceValue;
}
public void setForceValue(boolean forceValue) {
this.forceValue = forceValue;
}
protected Serializable getOriginal() {
return original;
}
public void mark() {
this.original = SerializationUtils.clone(getCurrentValue());
}
public void reset() {
Serializable currentValue = getCurrentValue();
if (original != null) {
if (!original.equals(currentValue)) {
setCurrentValue(original);
original = SerializationUtils.clone(original);
}
} else {
if (currentValue != null) {
setCurrentValue(null);
}
}
}
private PropertyMetadata ensurePropertyMetadata() {
return form.getFormItemClassMetadata().getProperties().stream()
.filter(pm -> pm.equals(propertyMetadata)).findFirst()
.orElseThrow(() -> new FormException("Unknown form property metadata: " + propertyMetadata.getName()));
}
private Serializable getCurrentValue() {
return ensurePropertyMetadata().getValue();
}
private void setCurrentValue(Serializable value) {
ensurePropertyMetadata().setValue(value);
setComponentValue(value);
}
@SuppressWarnings("unchecked")
private void setComponentValue(Serializable value) {
if (!forceValue && component instanceof HasItems) {
((HasItems) component).setItems((Collection) value);
} else if (component instanceof HasValue) {
((HasValue) component).setValue(value);
} else {
throw new FormResettableFieldException("Cannot set value (" + value + ") on component: " + component.getClass().getName());
}
}
}