PL-7471 Implement rangeStart / rangeEnd for DateField

This commit is contained in:
Nikita Petunin 2016-07-14 13:48:41 +04:00
parent 60c2b42303
commit 8424cb7135
7 changed files with 189 additions and 14 deletions

View File

@ -25,10 +25,7 @@ import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.chile.core.model.utils.InstanceUtils;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.entity.annotation.IgnoreUserTimeZone;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.MessageTools;
import com.haulmont.cuba.core.global.TimeZones;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.desktop.App;
import com.haulmont.cuba.desktop.gui.executors.impl.DesktopBackgroundWorker;
import com.haulmont.cuba.desktop.sys.DesktopToolTipManager;
@ -38,6 +35,7 @@ import com.haulmont.cuba.desktop.sys.vcl.DatePicker.DatePicker;
import com.haulmont.cuba.desktop.sys.vcl.Flushable;
import com.haulmont.cuba.desktop.sys.vcl.FocusableComponent;
import com.haulmont.cuba.gui.components.DateField;
import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.components.RequiredValueMissingException;
import com.haulmont.cuba.gui.components.ValidationException;
import com.haulmont.cuba.gui.data.Datasource;
@ -55,6 +53,7 @@ import java.util.*;
import java.util.List;
public class DesktopDateField extends DesktopAbstractField<JPanel> implements DateField {
protected Messages messages;
protected Resolution resolution;
protected Datasource datasource;
protected String dateTimeFormat;
@ -77,10 +76,13 @@ public class DesktopDateField extends DesktopAbstractField<JPanel> implements Da
protected Datasource.ItemChangeListener itemChangeListener;
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
protected Date startDate;
protected Date endDate;
public DesktopDateField() {
impl = new FocusableComposition();
messages = AppBeans.get(Messages.NAME);
initComponentParts();
setResolution(Resolution.MIN);
@ -106,11 +108,19 @@ public class DesktopDateField extends DesktopAbstractField<JPanel> implements Da
timeField = new DesktopTimeField();
timeField.addValueChangeListener(e -> {
if (!checkRange(constructDate())) {
return;
}
updateInstance();
});
datePicker.addPropertyChangeListener(evt -> {
if ("date".equals(evt.getPropertyName())) {
if (!checkRange(constructDate())) {
return;
}
updateInstance();
updateMissingValueState();
}
@ -210,6 +220,54 @@ public class DesktopDateField extends DesktopAbstractField<JPanel> implements Da
}
}
@Override
public void setRangeStart(Date value) {
startDate = value;
}
@Override
public Date getRangeStart() {
return startDate;
}
@Override
public void setRangeEnd(Date value) {
endDate = value;
}
@Override
public Date getRangeEnd() {
return endDate;
}
private boolean checkRange(Date value) {
if (value != null) {
if (startDate != null && value.before(startDate)) {
if (getFrame() != null) {
getFrame().showNotification(messages.getMainMessage("dateField.dateOutOfRangeMessage"),
Frame.NotificationType.WARNING);
}
datePicker.setDate((Date) prevValue);
timeField.setValue((Date) prevValue);
return false;
}
if (endDate != null && value.after(endDate)) {
if (getFrame() != null) {
getFrame().showNotification(messages.getMainMessage("dateField.dateOutOfRangeMessage"),
Frame.NotificationType.WARNING);
}
datePicker.setDate((Date) prevValue);
timeField.setValue((Date) prevValue);
return false;
}
}
return true;
}
@Override
public void requestFocus() {
SwingUtilities.invokeLater(() -> {
@ -426,12 +484,10 @@ public class DesktopDateField extends DesktopAbstractField<JPanel> implements Da
try {
if (datasource != null && metaPropertyPath != null) {
Date value = constructDate();
if (ObjectUtils.equals(prevValue, value)) {
valid = true;
return;
}
setValueToDs(value);
}
valid = true;

View File

@ -68,4 +68,6 @@ notification.title.TRAY_HTML =
notification.title.HUMANIZED = Information
notification.title.HUMANIZED_HTML = Information
sessionMessageDialog.caption = Message from administrator
sessionMessageDialog.caption = Message from administrator
dateField.dateOutOfRangeMessage = Date out of range

View File

@ -67,4 +67,5 @@ notification.title.TRAY_HTML =
notification.title.HUMANIZED = Информация
notification.title.HUMANIZED_HTML = Информация
sessionMessageDialog.caption = Сообщение от администратора
sessionMessageDialog.caption = Сообщение от администратора
dateField.dateOutOfRangeMessage = Выбранная дата находится за пределами допустимых значений

View File

@ -42,6 +42,12 @@ public interface DateField extends Field {
TimeZone getTimeZone();
void setTimeZone(TimeZone timeZone);
void setRangeStart(Date value);
Date getRangeStart();
void setRangeEnd(Date value);
Date getRangeEnd();
@SuppressWarnings("unchecked")
@Override
Date getValue();

View File

@ -776,6 +776,10 @@
<xs:sequence>
<xs:element name="validator" minOccurs="0" maxOccurs="1" type="validatorType"/>
</xs:sequence>
<xs:attribute name="rangeStart" type="xs:string"/>
<xs:attribute name="rangeEnd" type="xs:string"/>
<xs:attribute name="resolution" type="dateFieldResolution"/>
<xs:attribute name="dateFormat" type="xs:string"/>
</xs:extension>

View File

@ -18,12 +18,19 @@ package com.haulmont.cuba.gui.xml.layout.loaders;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.datatypes.impl.DateDatatype;
import com.haulmont.cuba.gui.GuiDevelopmentException;
import com.haulmont.cuba.gui.components.DateField;
import org.apache.commons.lang.StringUtils;
import javax.persistence.TemporalType;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFieldLoader extends AbstractFieldLoader<DateField> {
protected static final String DATE_PATTERN_DAY = "yyyy-MM-dd";
protected static final String DATE_PATTERN_MIN = "yyyy-MM-dd hh:mm";
@Override
public void createComponent() {
resultComponent = (DateField) factory.createComponent(DateField.NAME);
@ -80,5 +87,44 @@ public class DateFieldLoader extends AbstractFieldLoader<DateField> {
}
}
resultComponent.setDateFormat(formatStr);
loadRangeStart();
loadRangeEnd();
}
protected void loadRangeStart() {
String rangeStart = element.attributeValue("rangeStart");
if (StringUtils.isNotEmpty(rangeStart)) {
try {
resultComponent.setRangeStart(parseDateOrDateTime(rangeStart));
} catch (ParseException e) {
throw new GuiDevelopmentException(
"'rangeStart' parsing error for date picker: " +
rangeStart, context.getFullFrameId(), "DatePicker ID", resultComponent.getId());
}
}
}
protected void loadRangeEnd() {
String rangeEnd = element.attributeValue("rangeEnd");
if (StringUtils.isNotEmpty(rangeEnd)) {
try {
resultComponent.setRangeEnd(parseDateOrDateTime(rangeEnd));
} catch (ParseException e) {
throw new GuiDevelopmentException(
"'rangeEnd' parsing error for date picker: " +
rangeEnd, context.getFullFrameId(), "DatePicker ID", resultComponent.getId());
}
}
}
protected Date parseDateOrDateTime(String value) throws ParseException {
SimpleDateFormat rangeDF;
if (value.length() == 10) {
rangeDF = new SimpleDateFormat(DATE_PATTERN_DAY);
} else {
rangeDF = new SimpleDateFormat(DATE_PATTERN_MIN);
}
return rangeDF.parse(value);
}
}

View File

@ -22,10 +22,7 @@ import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.utils.InstanceUtils;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.entity.annotation.IgnoreUserTimeZone;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.MessageTools;
import com.haulmont.cuba.core.global.TimeZones;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.TestIdManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.data.Datasource;
@ -70,6 +67,8 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
protected Datasource.ItemPropertyChangeListener itemPropertyChangeListener;
protected Datasource.ItemChangeListener itemChangeListener;
protected Messages messages;
public WebDateField() {
innerLayout = new HorizontalLayout();
innerLayout.setSpacing(true);
@ -97,10 +96,16 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
setResolution(Resolution.MIN);
component = new CubaDateFieldWrapper(this, innerLayout);
messages = AppBeans.get(Messages.NAME);
}
protected Property.ValueChangeListener createDateValueChangeListener() {
return e -> {
if (!checkRange(constructDate())) {
return;
}
updateInstance();
if (component != null) {
@ -111,7 +116,13 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
}
protected Component.ValueChangeListener createTimeValueChangeListener() {
return event -> updateInstance();
return event -> {
if (!checkRange(constructDate())) {
return;
}
updateInstance();
};
}
public CubaDateField getDateField() {
@ -134,6 +145,54 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
updateLayout();
}
@Override
public void setRangeStart(Date value) {
dateField.setRangeStart(value);
}
@Override
public Date getRangeStart() {
return dateField.getRangeStart();
}
@Override
public void setRangeEnd(Date value) {
dateField.setRangeEnd(value);
}
@Override
public Date getRangeEnd() {
return dateField.getRangeEnd();
}
private boolean checkRange(Date value) {
if (value != null) {
if (dateField.getRangeStart() != null && value.before(dateField.getRangeStart())) {
if (getFrame() != null) {
getFrame().showNotification(messages.getMainMessage("datePicker.dateOutOfRangeMessage"),
Frame.NotificationType.WARNING);
}
dateField.setValue((Date) prevValue);
timeField.setValue((Date) prevValue);
return false;
}
if (dateField.getRangeEnd() != null && value.after(dateField.getRangeEnd())) {
if (getFrame() != null) {
getFrame().showNotification(messages.getMainMessage("datePicker.dateOutOfRangeMessage"),
Frame.NotificationType.WARNING);
}
dateField.setValue((Date) prevValue);
timeField.setValue((Date) prevValue);
return false;
}
}
return true;
}
@Override
public String getDateFormat() {
return dateTimeFormat;
@ -281,8 +340,9 @@ public class WebDateField extends WebAbstractField<CubaDateFieldWrapper> impleme
updatingInstance = true;
try {
Date value = constructDate();
if (datasource != null && metaPropertyPath != null) {
Date value = constructDate();
if (datasource.getItem() != null) {
InstanceUtils.setValueEx(datasource.getItem(), metaPropertyPath.getPath(), value);
}