Skip to content

Commit

Permalink
Made formatter exchangeable for weeday header cells.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Sep 20, 2022
1 parent 8176da4 commit e73ab40
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,23 @@ public WeekDayHeaderView() {
startDate.set(getDate());
endDate.set(getDate().plusDays(getNumberOfDays() - 1));

setCellFactory(date -> new WeekDayCell(this));
setCellFactory(date -> new WeekDayHeaderCell(this));
}

@Override
protected Skin<?> createDefaultSkin() {
return new WeekDayHeaderViewSkin(this);
}

private final ObjectProperty<Callback<WeekDayHeaderView, WeekDayCell>> cellFactory = new SimpleObjectProperty<>(this, "cellFactory");
private final ObjectProperty<Callback<WeekDayHeaderView, WeekDayHeaderCell>> cellFactory = new SimpleObjectProperty<>(this, "cellFactory");

/**
* A cell factory used for creating instances of {@link WeekDayCell} that will
* A cell factory used for creating instances of {@link WeekDayHeaderCell} that will
* be used to display the weekend day names.
*
* @return the cell factory
*/
public final ObjectProperty<Callback<WeekDayHeaderView, WeekDayCell>> cellFactoryProperty() {
public final ObjectProperty<Callback<WeekDayHeaderView, WeekDayHeaderCell>> cellFactoryProperty() {
return cellFactory;
}

Expand All @@ -97,7 +97,7 @@ public final ObjectProperty<Callback<WeekDayHeaderView, WeekDayCell>> cellFactor
*
* @return the cell factory
*/
public final Callback<WeekDayHeaderView, WeekDayCell> getCellFactory() {
public final Callback<WeekDayHeaderView, WeekDayHeaderCell> getCellFactory() {
return cellFactoryProperty().get();
}

Expand All @@ -107,7 +107,7 @@ public final Callback<WeekDayHeaderView, WeekDayCell> getCellFactory() {
* @param factory
* the cell factory
*/
public final void setCellFactory(Callback<WeekDayHeaderView, WeekDayCell> factory) {
public final void setCellFactory(Callback<WeekDayHeaderView, WeekDayHeaderCell> factory) {
requireNonNull(factory);
cellFactoryProperty().set(factory);
}
Expand Down Expand Up @@ -246,20 +246,18 @@ public final ReadOnlyObjectProperty<LocalDate> endDateProperty() {
*
* @see WeekDayHeaderView#cellFactoryProperty()
*/
public static class WeekDayCell extends Label {

private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Messages.getString("WeekDayHeaderView.CELL_DATE_FORMAT"));
public static class WeekDayHeaderCell extends Label {

/**
* Constructs a new date cell.
*
* @param view the weekday header view
*/
public WeekDayCell(WeekDayHeaderView view) {
public WeekDayHeaderCell(WeekDayHeaderView view) {
Objects.requireNonNull(view);
getStyleClass().add("cell");
setMaxWidth(Double.MAX_VALUE);
dateProperty().addListener(it -> setText(formatter.format(getDate())));
dateProperty().addListener(it -> setText(getFormatter().format(getDate())));
if (view.isEnableHyperlinks()) {
getStyleClass().add("date-hyperlink");
setOnMouseClicked(evt -> {
Expand All @@ -270,6 +268,25 @@ public WeekDayCell(WeekDayHeaderView view) {
}
}

private final ObjectProperty<DateTimeFormatter> formatter = new SimpleObjectProperty<>(this, "", DateTimeFormatter.ofPattern(Messages.getString("WeekDayHeaderView.CELL_DATE_FORMAT")));

public final DateTimeFormatter getFormatter() {
return formatter.get();
}

/**
* The formatter to be used for the date.
*
* @return the date formatter
*/
public final ObjectProperty<DateTimeFormatter> formatterProperty() {
return formatter;
}

public final void setFormatter(DateTimeFormatter formatter) {
this.formatter.set(formatter);
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package impl.com.calendarfx.view;

import com.calendarfx.view.WeekDayHeaderView;
import com.calendarfx.view.WeekDayHeaderView.WeekDayCell;
import com.calendarfx.view.WeekDayHeaderView.WeekDayHeaderCell;
import javafx.beans.InvalidationListener;
import javafx.scene.control.SkinBase;
import javafx.scene.layout.HBox;
Expand Down Expand Up @@ -67,10 +67,10 @@ private void updateControl() {
final int numberOfDays = view.getNumberOfDays();

Callback<WeekDayHeaderView, Region> separatorFactory = view.getSeparatorFactory();
Callback<WeekDayHeaderView, WeekDayCell> cellFactory = view.getCellFactory();
Callback<WeekDayHeaderView, WeekDayHeaderCell> cellFactory = view.getCellFactory();

for (int i = 0; i < numberOfDays; i++) {
WeekDayCell cell = cellFactory.call(view);
WeekDayHeaderCell cell = cellFactory.call(view);
cell.setPrefWidth(1); // equal width distribution

final int dayCount = i;
Expand All @@ -94,7 +94,7 @@ private void updateControl() {
}
}

private void updateCell(WeekDayCell cell, int dayCount) {
private void updateCell(WeekDayHeaderCell cell, int dayCount) {
LocalDate startDate = getSkinnable().getDate();
LocalDate date = getDate(startDate, dayCount);
cell.setDate(date);
Expand Down

0 comments on commit e73ab40

Please sign in to comment.