Skip to content

Commit

Permalink
Add showToday property with CSS support
Browse files Browse the repository at this point in the history
Enhance CalendarView by adding a `showToday` property, allowing it to be styled via CSS. Additionally, update DateRangeView to include specific CSS classes for start and end calendar views.
  • Loading branch information
leewyatt committed Sep 2, 2024
1 parent 674703a commit ed96799
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
43 changes: 39 additions & 4 deletions gemsfx/src/main/java/com/dlsc/gemsfx/CalendarView.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import javafx.collections.ObservableSet;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.css.StyleableBooleanProperty;
import javafx.css.StyleableObjectProperty;
import javafx.css.StyleableProperty;
import javafx.css.converter.BooleanConverter;
import javafx.css.converter.EnumConverter;
import javafx.scene.control.Cell;
import javafx.scene.control.Control;
Expand Down Expand Up @@ -82,6 +84,7 @@ public class CalendarView extends Control {

private static final YearDisplayMode DEFAULT_YEAR_DISPLAY_MODE = YearDisplayMode.TEXT_ONLY;
private static final MonthDisplayMode DEFAULT_MONTH_DISPLAY_MODE = MonthDisplayMode.TEXT_ONLY;
private static final boolean DEFAULT_SHOW_TODAY = true;
private YearMonthView yearMonthView;

private YearView yearView;
Expand Down Expand Up @@ -241,8 +244,6 @@ public final void setShowDaysOfPreviousOrNextMonth(boolean showDaysOfPreviousOrN
this.showDaysOfPreviousOrNextMonth.set(showDaysOfPreviousOrNextMonth);
}

private final BooleanProperty showToday = new SimpleBooleanProperty(this, "showToday", true);

private final ObjectProperty<LocalDate> today = new SimpleObjectProperty<>(this, "today", LocalDate.now());

/**
Expand Down Expand Up @@ -274,6 +275,8 @@ public final LocalDate getToday() {
return todayProperty().get();
}

private BooleanProperty showToday;

/**
* A flag used to indicate that the view will mark the area that represents
* the value of {@link #todayProperty()}. By default, this area will be
Expand All @@ -283,6 +286,24 @@ public final LocalDate getToday() {
* @return true if today will be shown differently
*/
public final BooleanProperty showTodayProperty() {
if (showToday == null) {
showToday = new StyleableBooleanProperty(DEFAULT_SHOW_TODAY) {
@Override
public Object getBean() {
return this;
}

@Override
public String getName() {
return "showToday";
}

@Override
public CssMetaData<? extends Styleable, Boolean> getCssMetaData() {
return StyleableProperties.SHOW_TODAY;
}
};
}
return showToday;
}

Expand All @@ -292,7 +313,7 @@ public final BooleanProperty showTodayProperty() {
* @return true if today will be highlighted visually
*/
public final boolean isShowToday() {
return showTodayProperty().get();
return showToday == null ? DEFAULT_SHOW_TODAY : showToday.get();
}

/**
Expand Down Expand Up @@ -1078,11 +1099,25 @@ public StyleableProperty<MonthDisplayMode> getStyleableProperty(CalendarView con
}
};

private static final CssMetaData<CalendarView, Boolean> SHOW_TODAY = new CssMetaData<>(
"-fx-show-today", BooleanConverter.getInstance(), DEFAULT_SHOW_TODAY) {

@Override
public boolean isSettable(CalendarView control) {
return control.showToday == null || !control.showToday.isBound();
}

@Override
public StyleableProperty<Boolean> getStyleableProperty(CalendarView control) {
return (StyleableProperty<Boolean>) control.showTodayProperty();
}
};

private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;

static {
final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<>(Control.getClassCssMetaData());
Collections.addAll(styleables, YEAR_DISPLAY_MODE, MONTH_DISPLAY_MODE);
Collections.addAll(styleables, YEAR_DISPLAY_MODE, MONTH_DISPLAY_MODE, SHOW_TODAY);
STYLEABLES = Collections.unmodifiableList(styleables);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public CalendarView getStartCalendarView() {
if (startCalendarView == null) {
startCalendarView = createCalendar();
startCalendarView.setYearMonth(YearMonth.now());
startCalendarView.getStyleClass().add("start-calendar");
}
return startCalendarView;
}
Expand All @@ -142,6 +143,7 @@ public CalendarView getEndCalendarView() {
if (endCalendarView == null) {
endCalendarView = createCalendar();
endCalendarView.setYearMonth(YearMonth.now().plusMonths(1));
endCalendarView.getStyleClass().add("end-calendar");
}
return endCalendarView;
}
Expand Down

0 comments on commit ed96799

Please sign in to comment.