package com.vaadin.client.ui;
import java.util.Date;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
+import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
onSubmit();
};
+ private Map<String, String> dateStyles = new HashMap<String, String>();
+ private DateTimeFormat df = DateTimeFormat.getFormat("yyyy-MM-dd");
+
public VAbstractCalendarPanel() {
getElement().setId(DOM.createUniqueId());
setStyleName(VDateField.CLASSNAME + "-calendarpanel");
}
}
+ public void setDateStyles(Map<String, String> dateStyles) {
+ this.dateStyles.clear();
+ if (dateStyles != null) {
+ this.dateStyles.putAll(dateStyles);
+ }
+ }
+
private void clearCalendarBody(boolean remove) {
if (!remove) {
// Leave the cells in place but clear their contents
if (curr.getMonth() != displayedMonth.getMonth()) {
day.addStyleDependentName(CN_OFFMONTH);
}
+ String dayDateString = df.format(dayDate);
+ if (dateStyles.containsKey(dayDateString)) {
+ day.addStyleName(dateStyles.get(dayDateString));
+ }
days.setWidget(weekOfMonth, firstWeekdayColumn + dayOfWeek,
day);
* {@link #updateFromUIDL(UIDL, ApplicationConnection)} method as is and
* customizing only listeners logic.
*/
+ @SuppressWarnings("deprecation")
protected void updateListeners() {
VAbstractDateFieldCalendar<PANEL, R> widget = getWidget();
if (isResolutionMonthOrHigher()) {
} else {
widget.calendarPanel.setDate(null);
}
+ widget.calendarPanel.setDateStyles(getState().dateStyles);
updateListeners();
}
@Override
+ @SuppressWarnings("unchecked")
public VAbstractDateFieldCalendar<PANEL, R> getWidget() {
return (VAbstractDateFieldCalendar<PANEL, R>) super.getWidget();
}
import com.google.gwt.user.client.ui.PopupPanel;
import com.vaadin.client.ApplicationConnection;
import com.vaadin.client.UIDL;
+import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.VAbstractCalendarPanel;
import com.vaadin.client.ui.VAbstractCalendarPanel.FocusChangeListener;
getWidget().setRangeStart(nullSafeDateClone(getState().rangeStart));
getWidget().setRangeEnd(nullSafeDateClone(getState().rangeEnd));
+ getWidget().calendar.setDateStyles(getState().dateStyles);
getWidget().calendar
.setDateTimeService(getWidget().getDateTimeService());
getWidget().calendar
getWidget().getStylePrimaryName() + "-popup" + styleName, add);
}
+ @OnStateChange("dateStyles")
+ void dateStylesUpdated() {
+ VAbstractPopupCalendar<PANEL, R> widget = getWidget();
+ widget.calendar.setDateStyles(getState().dateStyles);
+ // Update text field if locale already set
+ if (widget.getCurrentLocale() != null) {
+ widget.buildDate();
+ }
+ }
+
}
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
}
};
}
+
+ /**
+ * <p>
+ * Sets a custom style name for the given date's calendar cell. Setting the
+ * style name will override any previous style names that have been set for
+ * that date, but can contain several actual style names separated by space.
+ * Setting the custom style name {@code null} will only remove the previous
+ * custom style name.
+ * </p>
+ * <p>
+ * This logic is entirely separate from {@link #setStyleName(String)}
+ * </p>
+ * <p>
+ * Usage examples: <br>
+ * {@code setDateStyle(LocalDate.now(), "teststyle");} <br>
+ * {@code setDateStyle(LocalDate.now(), "teststyle1 teststyle2");}
+ * </p>
+ *
+ * @param date
+ * which date cell to modify
+ * @param styleName
+ * the custom style name(s) for given date, {@code null} to clear
+ * custom style name(s)
+ */
+ public void setDateStyle(LocalDate date, String styleName) {
+ if (date != null) {
+ if (styleName != null) {
+ getState().dateStyles.put(date.toString(), styleName);
+ } else {
+ getState().dateStyles.remove(date.toString());
+ }
+ }
+ }
+
+ /**
+ * Returns the custom style name that corresponds with the given date's
+ * calendar cell.
+ *
+ * @param date
+ * which date cell's custom style name(s) to return
+ * @return the corresponding style name(s), if any, {@code null} otherwise
+ *
+ * @see {@link #setDateStyle(LocalDate, String)}
+ */
+ public String getDateStyle(LocalDate date) {
+ if (date == null) {
+ return null;
+ }
+ return getState(false).dateStyles.get(date.toString());
+ }
+
+ /**
+ * Returns a map from dates to custom style names in each date's calendar
+ * cell.
+ *
+ * @return map from dates to custom style names in each date's calendar cell
+ *
+ * @see {@link #setDateStyle(LocalDate, String)}
+ */
+ public Map<LocalDate, String> getDateStyles() {
+ HashMap<LocalDate, String> hashMap = new HashMap<>();
+ for (Entry<String, String> entry : getState(false).dateStyles
+ .entrySet()) {
+ hashMap.put(LocalDate.parse(entry.getKey()), entry.getValue());
+ }
+ return hashMap;
+ }
}
*/
public boolean parsable = true;
+ /**
+ * Map of custom style names that correspond with given dates. Each date
+ * must be set to midnight for the handling logic to work correctly.
+ */
+ public Map<String, String> dateStyles = new HashMap<String, String>();
+
}
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUIWithLog;
+import com.vaadin.ui.Button;
import com.vaadin.ui.DateField;
import com.vaadin.ui.InlineDateField;
@Override
protected void setup(VaadinRequest request) {
+ getPage().getStyles()
+ .add(".v-inline-datefield .teststyle { background: yellow; }");
+ getPage().getStyles()
+ .add(".v-datefield-popup .teststyle { background: yellow; }");
+
log.setNumberLogRows(false);
DateField df = new DateField();
df.addValueChangeListener(event -> log(
usDatefield.addValueChangeListener(
event -> log("US date field value set to " + event.getValue()));
addComponent(usDatefield);
+
+ addComponent(new Button("Add date styles", e -> {
+ inlineDateField.setDateStyle(LocalDate.now(), "teststyle");
+ finnishDatefield.setDateStyle(LocalDate.of(2017, 12, 1),
+ "teststyle");
+ usDatefield.setDateStyle(LocalDate.of(2017, 12, 1), "teststyle");
+ }));
}
@Override
package com.vaadin.tests.components.datefield;
+import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Locale;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractReindeerTestUIWithLog;
+import com.vaadin.ui.Button;
import com.vaadin.ui.DateTimeField;
import com.vaadin.ui.InlineDateTimeField;
@Override
protected void setup(VaadinRequest request) {
+ getPage().getStyles()
+ .add(".v-inline-datefield .teststyle { background: yellow; }");
+ getPage().getStyles()
+ .add(".v-datefield-popup .teststyle { background: yellow; }");
+
log.setNumberLogRows(false);
DateTimeField df = new DateTimeField();
df.addValueChangeListener(event -> log(
usDateTimeField.addValueChangeListener(
event -> log("US date field value set to " + event.getValue()));
addComponent(usDateTimeField);
+
+ addComponent(new Button("Add date styles", e -> {
+ inlineDateTimeField.setDateStyle(LocalDate.now(), "teststyle");
+ finnishDateTimeField.setDateStyle(LocalDate.of(2017, 12, 1),
+ "teststyle");
+ usDateTimeField.setDateStyle(LocalDate.of(2017, 12, 1),
+ "teststyle");
+ }));
}
@Override
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.time.LocalDate;
import org.junit.Test;
+import org.openqa.selenium.WebElement;
+import com.vaadin.testbench.By;
+import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.DateFieldElement;
import com.vaadin.testbench.elements.InlineDateFieldElement;
import com.vaadin.tests.tb3.SingleBrowserTest;
DateFieldElementUI.ANOTHER_TEST_DATE_TIME);
}
+ @Test
+ public void testDateStyles() {
+ openTestURL();
+ assertTrue(findElements(By.className("teststyle")).isEmpty());
+
+ $(ButtonElement.class).first().click();
+
+ WebElement styledDateCell = $(InlineDateFieldElement.class).first()
+ .findElement(By.className("teststyle"));
+ assertEquals(String.valueOf(LocalDate.now().getDayOfMonth()),
+ styledDateCell.getText());
+
+ DateFieldElement fi = $(DateFieldElement.class).id("fi");
+ fi.openPopup();
+ waitForElementPresent(By.className("v-datefield-popup"));
+ WebElement popup = findElement(
+ com.vaadin.testbench.By.className("v-datefield-popup"));
+ styledDateCell = popup.findElement(By.className("teststyle"));
+ assertEquals("1", styledDateCell.getText());
+
+ styledDateCell.click(); // close popup
+ waitForElementNotPresent(By.className("v-datefield-popup"));
+
+ DateFieldElement us = $(DateFieldElement.class).id("us");
+ us.openPopup();
+ waitForElementPresent(By.className("v-datefield-popup"));
+ popup = findElement(
+ com.vaadin.testbench.By.className("v-datefield-popup"));
+ styledDateCell = popup.findElement(By.className("teststyle"));
+ assertEquals("1", styledDateCell.getText());
+ }
+
private void assertServerValue(String id, LocalDate testDateTime) {
assertEquals(id + " value set to " + testDateTime, getLogRow(0));
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.time.LocalDateTime;
import org.junit.Test;
+import org.openqa.selenium.WebElement;
+import com.vaadin.testbench.By;
+import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.DateTimeFieldElement;
import com.vaadin.testbench.elements.InlineDateTimeFieldElement;
import com.vaadin.tests.tb3.SingleBrowserTest;
DateTimeFieldElementUI.ANOTHER_TEST_DATE_TIME);
}
+ @Test
+ public void testDateStyles() {
+ openTestURL();
+ assertTrue(findElements(By.className("teststyle")).isEmpty());
+
+ // add styles
+ $(ButtonElement.class).first().click();
+
+ WebElement styledDateCell = $(InlineDateTimeFieldElement.class).first()
+ .findElement(By.className("teststyle"));
+ assertEquals(String.valueOf(LocalDateTime.now().getDayOfMonth()),
+ styledDateCell.getText());
+
+ DateTimeFieldElement fi = $(DateTimeFieldElement.class).id("fi");
+ fi.openPopup();
+ waitForElementPresent(By.className("v-datefield-popup"));
+ WebElement popup = findElement(
+ com.vaadin.testbench.By.className("v-datefield-popup"));
+ styledDateCell = popup.findElement(By.className("teststyle"));
+ assertEquals("1", styledDateCell.getText());
+
+ styledDateCell.click(); // close popup
+ waitForElementNotPresent(By.className("v-datefield-popup"));
+
+ DateTimeFieldElement us = $(DateTimeFieldElement.class).id("us");
+ us.openPopup();
+ waitForElementPresent(By.className("v-datefield-popup"));
+ popup = findElement(
+ com.vaadin.testbench.By.className("v-datefield-popup"));
+ styledDateCell = popup.findElement(By.className("teststyle"));
+ assertEquals("1", styledDateCell.getText());
+ }
+
private void assertServerValue(String id, LocalDateTime testDateTime) {
assertEquals(id + " value set to " + testDateTime, getLogRow(0));