1 package autotest.common.table; 2 3 import autotest.common.ui.DateTimeBox; 4 5 import com.google.gwt.event.logical.shared.ValueChangeEvent; 6 import com.google.gwt.event.logical.shared.ValueChangeHandler; 7 import com.google.gwt.i18n.client.DateTimeFormat; 8 import com.google.gwt.user.client.ui.HorizontalPanel; 9 import com.google.gwt.user.client.ui.Label; 10 import com.google.gwt.user.client.ui.Panel; 11 import com.google.gwt.user.client.ui.Widget; 12 import com.google.gwt.user.datepicker.client.CalendarUtil; 13 14 import java.util.Date; 15 16 public class DatetimeSegmentFilter extends SimpleFilter { 17 protected DateTimeBox startDatetimeBox; 18 protected DateTimeBox endDatetimeBox; 19 protected Panel panel; 20 protected Label fromLabel; 21 protected Label toLabel; 22 private String placeHolderStartDatetime; 23 private String placeHolderEndDatetime; 24 25 // only allow queries of at most 2 weeks of data to reduce the load on the 26 // database. 27 private static final long MAXIMUM_TIME_RANGE_MS = 14 * 1000 * 60 * 60 * 24; 28 private static final DateTimeFormat dateTimeFormat = 29 DateTimeFormat.getFormat("yyyy-MM-dd"); 30 private static final DateTimeFormat parseFormat = 31 DateTimeFormat.getFormat("yyyy-MM-ddTHH:mm"); 32 DatetimeSegmentFilter()33 public DatetimeSegmentFilter() { 34 startDatetimeBox = new DateTimeBox(); 35 endDatetimeBox = new DateTimeBox(); 36 fromLabel = new Label("From"); 37 toLabel = new Label("to"); 38 39 panel = new HorizontalPanel(); 40 panel.add(fromLabel); 41 panel.add(startDatetimeBox); 42 panel.add(toLabel); 43 panel.add(endDatetimeBox); 44 45 // The order here matters. It is not possible to set startTime that is 46 // not close to endTime (see updateStartDateConstraint). So, we must 47 // first set endTime to really get the startTime we want. 48 // We want all entries from today, so advance end date to tomorrow. 49 placeHolderEndDatetime = getDateTimeStringOffsetFromToday(1); 50 setEndTimeToPlaceHolderValue(); 51 placeHolderStartDatetime = getDateTimeStringOffsetFromToday(-6); 52 setStartTimeToPlaceHolderValue(); 53 54 addValueChangeHandler( 55 new ValueChangeHandler() { 56 public void onValueChange(ValueChangeEvent event) { 57 notifyListeners(); 58 } 59 }, 60 new ValueChangeHandler<String>() { 61 public void onValueChange(ValueChangeEvent<String> event) { 62 updateStartDateConstraint(event.getValue()); 63 notifyListeners(); 64 } 65 } 66 ); 67 } 68 69 /* 70 * Put a 2-week constraint on the width of the date interval; whenever the 71 * endDate changes, update the start date if needed, and update its minimum 72 * Date to be two weeks earlier than the new endDate value. 73 */ updateStartDateConstraint(String newEndDateValue)74 public void updateStartDateConstraint(String newEndDateValue) { 75 Date newEndDate = parse(newEndDateValue); 76 String currentStartDateStr = startDatetimeBox.getValue(); 77 Date startDateConstraint = minimumStartDate(newEndDate); 78 Date newStartDate = startDateConstraint; 79 // Only compare to the existing start date if it has been set. 80 if (!currentStartDateStr.equals("")) { 81 Date currentStartDate = parse(currentStartDateStr); 82 if (currentStartDate.compareTo(startDateConstraint) < 0) { 83 newStartDate = startDateConstraint; 84 } 85 } 86 startDatetimeBox.setValue(format(newStartDate)); 87 startDatetimeBox.setMin(format(startDateConstraint)); 88 } 89 format(Date date)90 public static String format(Date date) { 91 return dateTimeFormat.format(date) + "T00:00"; 92 } 93 parse(String date)94 public static Date parse(String date) { 95 return parseFormat.parse(date); 96 } 97 minimumStartDate(Date endDate)98 public static Date minimumStartDate(Date endDate) { 99 long sinceEpoch = endDate.getTime(); 100 return new Date(sinceEpoch - MAXIMUM_TIME_RANGE_MS); 101 } 102 103 @Override getWidget()104 public Widget getWidget() { 105 return panel; 106 } 107 setStartTimeToPlaceHolderValue()108 public void setStartTimeToPlaceHolderValue() { 109 startDatetimeBox.setValue(placeHolderStartDatetime); 110 } 111 setEndTimeToPlaceHolderValue()112 public void setEndTimeToPlaceHolderValue() { 113 endDatetimeBox.setValue(placeHolderEndDatetime); 114 updateStartDateConstraint(placeHolderEndDatetime); 115 } 116 addValueChangeHandler(ValueChangeHandler<String> startTimeHandler, ValueChangeHandler<String> endTimeHandler)117 public void addValueChangeHandler(ValueChangeHandler<String> startTimeHandler, 118 ValueChangeHandler<String> endTimeHandler) { 119 startDatetimeBox.addValueChangeHandler(startTimeHandler); 120 endDatetimeBox.addValueChangeHandler(endTimeHandler); 121 } 122 getDateTimeStringOffsetFromToday(int offsetDays)123 private static String getDateTimeStringOffsetFromToday(int offsetDays) { 124 Date placeHolderDate = new Date(); 125 CalendarUtil.addDaysToDate(placeHolderDate, offsetDays); 126 return format(placeHolderDate); 127 } 128 } 129