Extract WebDateField.createDateValueChangeListener method #PL-6045

This commit is contained in:
Yuriy Artamonov 2015-09-25 10:56:46 +00:00
parent d162f7727c
commit 0658dc0ddb

View File

@ -22,6 +22,7 @@ import com.haulmont.cuba.web.AppUI;
import com.haulmont.cuba.web.toolkit.ui.CubaDateField;
import com.haulmont.cuba.web.toolkit.ui.CubaDateFieldWrapper;
import com.haulmont.cuba.web.toolkit.ui.CubaMaskedTextField;
import com.vaadin.data.Property;
import com.vaadin.ui.HorizontalLayout;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -80,19 +81,26 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
vTimeField.setInvalidAllowed(false);
vTimeField.setInvalidCommitted(true);
dateField.addValueChangeListener(event -> {
dateField.addValueChangeListener(createDateValueChangeListener());
timeField.addValueChangeListener(createTimeValueChangeListener());
setResolution(Resolution.MIN);
component = new CubaDateFieldWrapper(this, innerLayout);
}
protected Property.ValueChangeListener createDateValueChangeListener() {
return e -> {
updateInstance();
if (component != null) {
// Repaint error state
component.markAsDirty();
}
});
};
}
timeField.addValueChangeListener(e -> updateInstance());
setResolution(Resolution.MIN);
component = new CubaDateFieldWrapper(this, innerLayout);
protected Component.ValueChangeListener createTimeValueChangeListener() {
return event -> updateInstance();
}
public CubaDateField getDateField() {
@ -120,6 +128,26 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
return dateTimeFormat;
}
@Override
public void setDateFormat(String dateFormat) {
dateTimeFormat = dateFormat;
StringBuilder date = new StringBuilder(dateFormat);
StringBuilder time = new StringBuilder(dateFormat);
int timeStartPos = findTimeStartPos(dateFormat);
if (timeStartPos >= 0) {
time.delete(0, timeStartPos);
date.delete(timeStartPos, dateFormat.length());
timeFormat = StringUtils.trimToEmpty(time.toString());
timeField.setFormat(timeFormat);
setResolution(resolution);
} else if (resolution.ordinal() < Resolution.DAY.ordinal()) {
setResolution(Resolution.DAY);
}
this.dateFormat = StringUtils.trimToEmpty(date.toString());
dateField.setDateFormat(this.dateFormat);
}
@Override
public TimeZone getTimeZone() {
return timeZone;
@ -154,26 +182,6 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
// do nothing
}
@Override
public void setDateFormat(String dateFormat) {
dateTimeFormat = dateFormat;
StringBuilder date = new StringBuilder(dateFormat);
StringBuilder time = new StringBuilder(dateFormat);
int timeStartPos = findTimeStartPos(dateFormat);
if (timeStartPos >= 0) {
time.delete(0, timeStartPos);
date.delete(timeStartPos, dateFormat.length());
timeFormat = StringUtils.trimToEmpty(time.toString());
timeField.setFormat(timeFormat);
setResolution(resolution);
} else if (resolution.ordinal() < Resolution.DAY.ordinal()) {
setResolution(Resolution.DAY);
}
this.dateFormat = StringUtils.trimToEmpty(date.toString());
dateField.setDateFormat(this.dateFormat);
}
protected int findTimeStartPos(String dateTimeFormat) {
List<Integer> positions = new ArrayList<>();
@ -276,6 +284,11 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
fireValueChanged(newValue);
}
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public void setEnabled(boolean enabled) {
this.enabled = enabled;
@ -283,11 +296,6 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
timeField.setEnabled(enabled);
}
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public void setDatasource(Datasource datasource, String property) {
this.datasource = datasource;
@ -314,6 +322,7 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
fireValueChanged(value);
});
//noinspection unchecked
datasource.addItemPropertyChangeListener(e -> {
if (updatingInstance) {
return;
@ -423,8 +432,9 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
@Override
public void validate() throws ValidationException {
if (!isVisible() || !isEditable() || !isEnabled())
if (!isVisible() || !isEditable() || !isEnabled()) {
return;
}
if (isRequired() && dateField.getValue() == null) {
throw new RequiredValueMissingException(component.getRequiredError(), this);