package org.gcube.portlets.widgets.ckandatapublisherwidget.client.ui.timeandreanges; import java.util.Date; import com.github.gwtbootstrap.datepicker.client.ui.DateBox; import com.github.gwtbootstrap.datetimepicker.client.ui.DateTimeBox; import com.google.gwt.core.client.GWT; import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.Widget; public class DataTimeBox extends Composite{ private static DataTimeBoxUiBinder uiBinder = GWT .create(DataTimeBoxUiBinder.class); interface DataTimeBoxUiBinder extends UiBinder { } public static final String RANGE_SEPARATOR_START_END = "/"; public static final String MISSING_RANGE_VALUE = "MISSING_RANGE_PART"; private static final String MINOR_HYPHEN = "-"; private static final String COLON = ":"; private static final String INSERT_DATE_INSTANT_LABEL = "Insert a date"; private static final String INSERT_TIME_INSTANT_LABEL = "Hour and minutes"; private static final String INSERT_DATE_START_LABEL = "Insert a start date"; private static final String INSERT_DATE_END_LABEL = "Insert an end date"; private static final DateTimeFormat formatDate = DateTimeFormat.getFormat("yyyy/MM/dd"); private static final DateTimeFormat formatTime = DateTimeFormat.getFormat("HH:mm"); private boolean isRange; @UiField DateBox startRangeDate; @UiField DateTimeBox startRangeTime; @UiField DateBox endRangeDate; @UiField DateTimeBox endRangeTime; @UiField FlowPanel singleDataEnd; @UiField FlowPanel singleDataStart; public DataTimeBox(boolean isRange) { initWidget(uiBinder.createAndBindUi(this)); this.isRange = isRange; startRangeDate.setPlaceholder(INSERT_DATE_INSTANT_LABEL); startRangeTime.setPlaceholder(INSERT_TIME_INSTANT_LABEL); if(isRange){ singleDataEnd.setVisible(true); singleDataEnd.setWidth("50%"); singleDataStart.setWidth("50%"); startRangeDate.setPlaceholder(INSERT_DATE_START_LABEL); endRangeDate.setPlaceholder(INSERT_DATE_END_LABEL); endRangeTime.setPlaceholder(INSERT_TIME_INSTANT_LABEL); } startRangeDate.setValue(null); startRangeTime.setValue(null); endRangeDate.setValue(null); endRangeTime.setValue(null); } public boolean getIsRange(){ return isRange; } public void setStartDate(String date, String time){ GWT.log("Date is " + date + " and time is " + time); startRangeDate.setValue(new Date(date.replaceAll(MINOR_HYPHEN, RANGE_SEPARATOR_START_END))); if(time != null && !time.isEmpty()){ Date completeDate = new Date(); completeDate.setHours(Integer.parseInt(time.split(COLON)[0])); completeDate.setMinutes(Integer.parseInt(time.split(COLON)[1])); startRangeTime.setValue(completeDate); } } public void setEndDate(String date, String time){ GWT.log("Date is " + date + " and time is " + time); endRangeDate.setValue(new Date(date.replaceAll(MINOR_HYPHEN, RANGE_SEPARATOR_START_END))); if(time != null && !time.isEmpty()){ Date completeDate = new Date(); completeDate.setHours(Integer.parseInt(time.split(COLON)[0])); completeDate.setMinutes(Integer.parseInt(time.split(COLON)[1])); endRangeTime.setValue(completeDate); } } /** * Return the current value, with MISSING_RANGE_VALUE in case of missing entry * @return */ public String getCurrentValue(){ String firstRange = MISSING_RANGE_VALUE; String secondRange = MISSING_RANGE_VALUE; if(startRangeDate.getValue() != null){ firstRange = formatDate.format(startRangeDate.getValue()) + " " + (startRangeTime.getValue() != null ? formatTime.format(startRangeTime.getValue()) : ""); firstRange = firstRange.replaceAll(RANGE_SEPARATOR_START_END, MINOR_HYPHEN); } if(isRange && endRangeDate.getValue() != null){ secondRange = formatDate.format(endRangeDate.getValue()) + " " + (endRangeTime.getValue() != null ? formatTime.format(endRangeTime.getValue()) : ""); secondRange = secondRange.replaceAll(RANGE_SEPARATOR_START_END, MINOR_HYPHEN); } if(isRange){ GWT.log("Returning " + firstRange + RANGE_SEPARATOR_START_END + secondRange); return firstRange + RANGE_SEPARATOR_START_END + secondRange; }else { GWT.log("Returning " + firstRange); return firstRange; } } /** * Freeze the inputs */ public void freeze(){ startRangeDate.setEnabled(false); startRangeTime.setEnabled(false); endRangeDate.setEnabled(false); endRangeTime.setEnabled(false); } }