Merge branch 'feature/19-create-component-for-selecting-year-and-month' into 'develop/0.2.0'
Resolve "Create component for selecting year and month" Closes #19 See merge request tpeetz/kontor!19
This commit was merged in pull request #63.
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package de.thpeetz.kontor.comics.views;
|
||||
|
||||
import java.time.*;
|
||||
import java.time.format.*;
|
||||
import java.util.List;
|
||||
|
||||
import com.vaadin.flow.component.ComponentEvent;
|
||||
@@ -11,14 +9,16 @@ import com.vaadin.flow.component.button.Button;
|
||||
import com.vaadin.flow.component.button.ButtonVariant;
|
||||
import com.vaadin.flow.component.checkbox.Checkbox;
|
||||
import com.vaadin.flow.component.combobox.ComboBox;
|
||||
import com.vaadin.flow.component.datepicker.*;
|
||||
import com.vaadin.flow.component.formlayout.FormLayout;
|
||||
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
|
||||
import com.vaadin.flow.component.textfield.TextField;
|
||||
import com.vaadin.flow.data.binder.*;
|
||||
|
||||
import com.vaadin.flow.data.converter.*;
|
||||
import de.thpeetz.kontor.comics.data.*;
|
||||
import com.vaadin.flow.data.binder.BeanValidationBinder;
|
||||
import com.vaadin.flow.data.binder.Binder;
|
||||
import de.thpeetz.kontor.comics.data.Comic;
|
||||
import de.thpeetz.kontor.comics.data.Issue;
|
||||
import de.thpeetz.kontor.comics.data.Volume;
|
||||
import de.thpeetz.kontor.common.views.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@@ -28,7 +28,7 @@ public class IssueForm extends FormLayout {
|
||||
ComboBox<Volume> volume = new ComboBox<>("Volume");
|
||||
TextField issueNumber = new TextField("Issue number");
|
||||
TextField title = new TextField("Full Title");
|
||||
TextField publishedOn = new TextField("Published");
|
||||
YearMonthField publishedOn = new YearMonthField();
|
||||
Checkbox isRead = new Checkbox("Read");
|
||||
Checkbox inStock = new Checkbox("In stock");
|
||||
|
||||
@@ -40,23 +40,6 @@ public class IssueForm extends FormLayout {
|
||||
|
||||
public IssueForm(List<Comic> comics) {
|
||||
addClassName("issue-form");
|
||||
binder.forField(publishedOn).withConverter(new Converter<String, YearMonth>() {
|
||||
@Override
|
||||
public Result<YearMonth> convertToModel(String value, ValueContext context) {
|
||||
try {
|
||||
YearMonth result = YearMonth.parse(value);
|
||||
return Result.ok(result);
|
||||
} catch (DateTimeParseException e) {
|
||||
return Result.error("invalid year-month format");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToPresentation(YearMonth value, ValueContext context) {
|
||||
if (value == null) return "";
|
||||
return value.format(DateTimeFormatter.ofPattern("yyyy-MM"));
|
||||
}
|
||||
}).bind(Issue::getPublishedOn, Issue::setPublishedOn);
|
||||
binder.bindInstanceFields(this);
|
||||
|
||||
comic.setItems(comics);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package de.thpeetz.kontor.common.views;
|
||||
|
||||
import com.vaadin.flow.component.Unit;
|
||||
import com.vaadin.flow.component.customfield.CustomField;
|
||||
import com.vaadin.flow.component.select.Select;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.time.YearMonth;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.TextStyle;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@Slf4j
|
||||
public class YearMonthField extends CustomField<YearMonth> {
|
||||
|
||||
private final Select<Integer> year = new Select<>();;
|
||||
private final Select<Month> month = new Select<>();
|
||||
|
||||
public YearMonthField() {
|
||||
LocalDate now = LocalDate.now(ZoneId.systemDefault());
|
||||
List<Integer> selectableYears = IntStream.range(1970, now.getYear()).boxed().toList();
|
||||
year.setItems(selectableYears);
|
||||
year.setWidth(6, Unit.EM);
|
||||
month.setItems(Month.values());
|
||||
month.setItemLabelGenerator(m -> m.getDisplayName(TextStyle.FULL, Locale.getDefault()));
|
||||
month.setWidth(9, Unit.EM);
|
||||
add(year, month);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected YearMonth generateModelValue() {
|
||||
if (year.getValue() != null && month.getValue() != null) {
|
||||
int yearValue = year.getValue();
|
||||
int monthValue = month.getValue().getValue();
|
||||
YearMonth result = YearMonth.of(yearValue, monthValue);
|
||||
log.debug("YearMonthField.generateModelValue() = {}", result);
|
||||
return result;
|
||||
} else {
|
||||
log.debug("YearMonthField.generateModelValue() = null");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setPresentationValue(YearMonth yearMonth) {
|
||||
if (yearMonth == null) return;
|
||||
year.setValue(yearMonth.getYear());
|
||||
month.setValue(yearMonth.getMonth());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user