diff --git a/src/test/java/com/flowingcode/addons/ycalendar/LocaleSelector.java b/src/test/java/com/flowingcode/addons/ycalendar/LocaleSelector.java new file mode 100644 index 0000000..f9d00cc --- /dev/null +++ b/src/test/java/com/flowingcode/addons/ycalendar/LocaleSelector.java @@ -0,0 +1,60 @@ +/*- + * #%L + * Year Month Calendar Add-on + * %% + * Copyright (C) 2021 - 2024 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.addons.ycalendar; + +import com.vaadin.flow.component.Composite; +import com.vaadin.flow.component.datepicker.DatePicker; +import com.vaadin.flow.component.datepicker.DatePicker.DatePickerI18n; +import com.vaadin.flow.component.radiobutton.RadioButtonGroup; +import com.vaadin.flow.function.SerializableConsumer; +import java.text.DateFormatSymbols; +import java.time.DayOfWeek; +import java.time.temporal.WeekFields; +import java.util.List; +import java.util.Locale; + +@SuppressWarnings("serial") +public class LocaleSelector extends Composite> { + + private final SerializableConsumer consumer; + + public LocaleSelector(SerializableConsumer consumer) { + this.consumer = consumer; + getContent().addValueChangeListener(ev -> updateLocale(ev.getValue())); + getContent().setItems(Locale.ENGLISH, Locale.GERMAN, new Locale("es")); + getContent().setItemLabelGenerator(locale -> locale.getDisplayLanguage(Locale.ENGLISH)); + getContent().setLabel("Choose Language"); + getContent().setValue(Locale.ENGLISH); + } + + private void updateLocale(Locale locale) { + DatePickerI18n i18n = new DatePickerI18n(); + DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale); + i18n.setMonthNames(List.of(dfs.getMonths())); + i18n.setWeekdays(List.of(dfs.getWeekdays()).stream().skip(1).toList()); + i18n.setWeekdaysShort(List.of(dfs.getShortWeekdays()).stream().skip(1).toList()); + + DayOfWeek firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek(); + i18n.setFirstDayOfWeek(firstDayOfWeek.getValue() % 7); + + consumer.accept(i18n); + } + +} diff --git a/src/test/java/com/flowingcode/addons/ycalendar/MonthDemo.java b/src/test/java/com/flowingcode/addons/ycalendar/MonthDemo.java index ba0e03d..e116c57 100644 --- a/src/test/java/com/flowingcode/addons/ycalendar/MonthDemo.java +++ b/src/test/java/com/flowingcode/addons/ycalendar/MonthDemo.java @@ -2,7 +2,7 @@ * #%L * Year Month Calendar Add-on * %% - * Copyright (C) 2021 - 2023 Flowing Code + * Copyright (C) 2021 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,8 +54,10 @@ public MonthDemo() { selectedDate.setText("Selected date: " + ev.getDate()); }); - Span instructions = new Span("Use arrow keys to move."); - add(new HorizontalLayout(instructions, selectedDate), calendar); + Span instructions = new Span("Use arrow keys to move."); // hide-source + add(new HorizontalLayout(instructions, selectedDate)); // hide-source + add(new LocaleSelector(calendar::setI18n)); // hide-source + add(calendar); } } diff --git a/src/test/java/com/flowingcode/addons/ycalendar/YearDemo.java b/src/test/java/com/flowingcode/addons/ycalendar/YearDemo.java index 3a8465d..5cee6cb 100644 --- a/src/test/java/com/flowingcode/addons/ycalendar/YearDemo.java +++ b/src/test/java/com/flowingcode/addons/ycalendar/YearDemo.java @@ -2,7 +2,7 @@ * #%L * Year Month Calendar Add-on * %% - * Copyright (C) 2021 - 2023 Flowing Code + * Copyright (C) 2021 - 2024 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import com.vaadin.flow.component.html.Div; import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.textfield.IntegerField; import com.vaadin.flow.router.PageTitle; @@ -45,6 +46,7 @@ public class YearDemo extends Div { public YearDemo() { addClassName("year-demo"); YearCalendar calendar = new YearCalendar(); + calendar.setClassNameGenerator(date -> { if (TestUtils.isPublicHoliday(date)) { return "holiday"; @@ -62,6 +64,7 @@ public YearDemo() { selectedDate.setText("Selected date: " + ev.getDate()); }); + // #if vaadin eq 0 Span instructions = new Span("Use arrow keys or Ctrl+arrow keys to move."); IntegerField yearField = new IntegerField(); @@ -78,9 +81,17 @@ public YearDemo() { } yearField.setValue(calendar.getYear()); + yearField.setLabel("Change year"); yearField.addValueChangeListener(e -> calendar.setYear(e.getValue())); - add(new HorizontalLayout(instructions, selectedDate), yearField, calendar); + LocaleSelector localeSelector = new LocaleSelector(calendar::setI18n); + HorizontalLayout controls = new HorizontalLayout(yearField, localeSelector); + controls.setSpacing(true); + controls.setAlignItems(Alignment.BASELINE); + + add(new HorizontalLayout(instructions, selectedDate), controls); + // #endif + add(calendar); } }