Skip to content

Commit

Permalink
Add dateFilter for CalendarPicker
Browse files Browse the repository at this point in the history
  • Loading branch information
leewyatt committed Mar 13, 2024
1 parent 12dd5c6 commit 36d0cc2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ public void start(Stage stage) {
CheckBox disable = new CheckBox("Disable");
disable.selectedProperty().bindBidirectional(calendarPicker.disableProperty());

CheckBox disabledWeekendBox = new CheckBox("Filter: Disable Weekend");
calendarPicker.dateFilterProperty().bind(Bindings.createObjectBinding(() -> {
if (disabledWeekendBox.isSelected()) {
return date -> date.getDayOfWeek().getValue() < 6;
} else {
return null; // return date -> true;
}
}, disabledWeekendBox.selectedProperty()));

Button showPopupButton = new Button("Show Popup");
showPopupButton.setOnAction(evt -> calendarPicker.show());

Expand All @@ -40,7 +49,7 @@ public void start(Stage stage) {

HBox popupButtons = new HBox(10, showPopupButton, hidePopupButton);

VBox vBox = new VBox(10, popupButtons, calendarPicker, valueLabel, editable, disable);
VBox vBox = new VBox(10, popupButtons, calendarPicker, valueLabel, editable, disable, disabledWeekendBox);
vBox.setAlignment(Pos.TOP_LEFT);
vBox.setPadding(new Insets(20));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void start(Stage stage) {
VBox.setMargin(headerLayoutLabel, new Insets(10, 0, 0, 0));
options1.getChildren().addAll(headerLayoutLabel, headerLayoutComboBox);

CheckBox disabledWeekendBox = new CheckBox("Disable Weekend");
CheckBox disabledWeekendBox = new CheckBox("Filter: Disable Weekend");
calendarView.dateFilterProperty().bind(Bindings.createObjectBinding(() -> {
if (disabledWeekendBox.isSelected()) {
return date -> date.getDayOfWeek().getValue() < 6;
Expand Down
34 changes: 29 additions & 5 deletions gemsfx/src/main/java/com/dlsc/gemsfx/CalendarPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Region;
import javafx.util.Callback;
import javafx.util.StringConverter;
import javafx.util.converter.LocalDateStringConverter;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -45,6 +46,7 @@ public CalendarPicker() {

calendarView.setShowToday(true);
calendarView.setShowTodayButton(true);
calendarView.dateFilterProperty().bind(dateFilterProperty());

setFocusTraversable(false);

Expand Down Expand Up @@ -123,11 +125,7 @@ private void commitValue() {
StringConverter<LocalDate> converter = getConverter();
if (converter != null) {
LocalDate value = converter.fromString(text);
if (value != null) {
setValue(value);
} else {
setValue(null);
}
setValue(value);
}
}
}
Expand Down Expand Up @@ -179,4 +177,30 @@ public final ObjectProperty<StringConverter<LocalDate>> converterProperty() {
public final void setConverter(StringConverter<LocalDate> converter) {
this.converter.set(converter);
}

/**
* A property to define a filter for determining which dates in the calendar can be selected.
* This filter is applied to each date displayed in the calendar. If the filter returns true for
* a given date, that date will be selectable (i.e., it passes the filter). If the filter returns
* false, the date will be disabled and cannot be selected. This property is particularly useful
* for scenarios where only specific dates should be available for selection based on custom
* logic, such as business rules, holidays, or availability.
* When SelectionMode is DATE_RANGE, disabled dates can be included within the selected range.
* However, disabled dates cannot be used as either the starting or ending point of the range.
*
* @return A callback that determines the selectability of each date based on custom criteria.
*/
private final ObjectProperty<Callback<LocalDate,Boolean>> dateFilter = new SimpleObjectProperty<>(this, "dateFilter");

public Callback<LocalDate, Boolean> getDateFilter() {
return dateFilter.get();
}

public ObjectProperty<Callback<LocalDate, Boolean>> dateFilterProperty() {
return dateFilter;
}

public void setDateFilter(Callback<LocalDate, Boolean> dateFilter) {
this.dateFilter.set(dateFilter);
}
}

0 comments on commit 36d0cc2

Please sign in to comment.