Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CalendarView properties #144

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/CalendarViewApp.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.dlsc.gemsfx.demo;

import com.dlsc.gemsfx.CalendarView;
import com.dlsc.gemsfx.CalendarView.HeaderLayout;
import com.dlsc.gemsfx.CalendarView.SelectionModel.SelectionMode;
import com.dlsc.gemsfx.CalendarView.YearDisplayMode;
import com.dlsc.gemsfx.CalendarView.MonthDisplayMode;
import fr.brouillard.oss.cssfx.CSSFX;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
Expand Down Expand Up @@ -50,30 +52,24 @@ public void start(Stage stage) {
options1.getChildren().add(createOption("Show today button", calendarView.showTodayButtonProperty()));
options1.getChildren().add(createOption("Show month", calendarView.showMonthProperty()));
options1.getChildren().add(createOption("Show year", calendarView.showYearProperty()));
options1.getChildren().add(createOption("Show year spinner", calendarView.showYearSpinnerProperty()));
options1.getChildren().add(createOption("Show year dropdown", calendarView.showYearDropdownProperty()));
options1.getChildren().add(createOption("Show month arrows", calendarView.showMonthArrowsProperty()));
options1.getChildren().add(createOption("Show month dropdown", calendarView.showMonthDropdownProperty()));
options1.getChildren().add(createOption("Show week numbers", calendarView.showWeekNumbersProperty()));
options1.getChildren().add(createOption("Show days of other months", calendarView.showDaysOfPreviousOrNextMonthProperty()));
options1.getChildren().add(createOption("Mark days of other months selected", calendarView.markSelectedDaysOfPreviousOrNextMonthProperty()));
options1.getChildren().add(createOption("Show grid lines", showGridLines));
options1.getChildren().add(new Separator());

ComboBox<SelectionMode> selectionModeComboBox = new ComboBox<>();
selectionModeComboBox.setMaxWidth(Double.MAX_VALUE);
selectionModeComboBox.getItems().setAll(SelectionMode.values());
selectionModeComboBox.valueProperty().bindBidirectional(calendarView.getSelectionModel().selectionModeProperty());
Label selectionModeLabel = new Label("Selection mode");
VBox.setMargin(selectionModeLabel, new Insets(10, 0, 0, 0));
options1.getChildren().addAll(selectionModeLabel, selectionModeComboBox);

ComboBox<HeaderLayout> headerLayoutComboBox = new ComboBox<>();
headerLayoutComboBox.setMaxWidth(Double.MAX_VALUE);
headerLayoutComboBox.getItems().setAll(HeaderLayout.values());
headerLayoutComboBox.valueProperty().bindBidirectional(calendarView.headerLayoutProperty());
Label headerLayoutLabel = new Label("Header layout");
VBox.setMargin(headerLayoutLabel, new Insets(10, 0, 0, 0));
options1.getChildren().addAll(headerLayoutLabel, headerLayoutComboBox);
VBox yearDisplayMode = createComboBoxOption("Year Display Mode", YearDisplayMode.TEXT_ONLY, calendarView.yearDisplayModeProperty());
options1.getChildren().add(yearDisplayMode);

VBox monthDisplayMode = createComboBoxOption("Month Display Mode", MonthDisplayMode.TEXT_ONLY, calendarView.monthDisplayModeProperty());
options1.getChildren().add(monthDisplayMode);

VBox selectionModeBox = createComboBoxOption("Selection mode", SelectionMode.DATE_RANGE, calendarView.getSelectionModel().selectionModeProperty());
options1.getChildren().add(selectionModeBox);

VBox headerLayoutBox = createComboBoxOption("Header layout", CalendarView.HeaderLayout.CENTER, calendarView.headerLayoutProperty());
options1.getChildren().add(headerLayoutBox);

CheckBox disabledWeekendBox = new CheckBox("Filter: Disable Weekend");
calendarView.dateFilterProperty().bind(Bindings.createObjectBinding(() -> {
Expand Down Expand Up @@ -126,6 +122,18 @@ private Node createOption(String name, BooleanProperty property) {
return box;
}

private <E extends Enum<E>> VBox createComboBoxOption(String title, E defaultEnum, ObjectProperty<E> property) {
ComboBox<E> comboBox = new ComboBox<>();
comboBox.setMaxWidth(Double.MAX_VALUE);
comboBox.getItems().addAll(defaultEnum.getDeclaringClass().getEnumConstants());
comboBox.setValue(defaultEnum);
comboBox.valueProperty().bindBidirectional(property);
VBox.setMargin(comboBox, new Insets(0, 0, 5, 0));

Label selectionModeLabel = new Label(title);
return new VBox(2, selectionModeLabel, comboBox);
}

public static void main(String[] args) {
launch();
}
Expand Down
250 changes: 180 additions & 70 deletions gemsfx/src/main/java/com/dlsc/gemsfx/CalendarView.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,24 @@

import com.dlsc.gemsfx.skins.CalendarViewSkin;
import com.dlsc.gemsfx.skins.DateCellSkin;
import javafx.beans.property.*;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.css.StyleableObjectProperty;
import javafx.css.StyleableProperty;
import javafx.css.converter.EnumConverter;
import javafx.scene.control.Cell;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
Expand All @@ -33,6 +47,9 @@
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static java.lang.Double.MAX_VALUE;
Expand Down Expand Up @@ -63,6 +80,8 @@
*/
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 YearMonthView yearMonthView;

private YearView yearView;
Expand Down Expand Up @@ -579,75 +598,6 @@ public final void setShowYear(boolean showYear) {
this.showYear.set(showYear);
}

private final BooleanProperty showMonthDropdown = new SimpleBooleanProperty(this, "showMonthDropdown", false);

public final boolean isShowMonthDropdown() {
return showMonthDropdown.get();
}

/**
* Show or hide a dropdown icon next to the label that displays
* the currently selected month.
*
* @return show / hide month dropdown
*/
public final BooleanProperty showMonthDropdownProperty() {
return showMonthDropdown;
}

public final void setShowMonthDropdown(boolean showMonthDropdown) {
this.showMonthDropdown.set(showMonthDropdown);
}

private final BooleanProperty showYearDropdown = new SimpleBooleanProperty(this, "showYearDropdown", false);

public final boolean isShowYearDropdown() {
return showYearDropdown.get();
}

/**
* Show or hide a dropdown icon next to the label that displays
* the currently selected year.
*
* @return show / hide year dropdown
*/
public final BooleanProperty showYearDropdownProperty() {
return showYearDropdown;
}

public final void setShowYearDropdown(boolean showYearDropdown) {
this.showYearDropdown.set(showYearDropdown);
}

private final BooleanProperty showYearSpinner = new SimpleBooleanProperty(this, "showYearSpinner", false);

/**
* Show or hide the year / month spinner.
*
* @return true if the year will be shown
*/
public final BooleanProperty showYearSpinnerProperty() {
return showYearSpinner;
}

/**
* Sets the value of {@link #showYearSpinnerProperty()}.
*
* @param show if true the year / month spinner at the top will be shown
*/
public final void setShowYearSpinner(boolean show) {
showYearSpinnerProperty().set(show);
}

/**
* Returns the value of {@link #showYearSpinnerProperty()}.
*
* @return true if the year / month spinner will be shown
*/
public final boolean isShowYearSpinner() {
return showYearSpinnerProperty().get();
}

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

/**
Expand Down Expand Up @@ -839,6 +789,117 @@ public final void setLatestDate(LocalDate latestDate) {
this.latestDate.set(latestDate);
}

/**
* Enumerates the display modes for the year label at the top of the calendar view.
*/
public enum YearDisplayMode {
/**
* Displays only the year text. This is the default mode.
*/
TEXT_ONLY,

/**
* Displays the year text with a spinner for adjustment.
*/
TEXT_AND_SPINNER,

/**
* Displays the year text with a dropdown button.
*/
TEXT_AND_DROPDOWN
}

private ObjectProperty<YearDisplayMode> yearDisplayMode;

/**
* The display mode for the year label at the top of the calendar view.
* {@link YearDisplayMode#TEXT_ONLY} is the default mode.
*
* @return the year display mode property.
*/
public final ObjectProperty<YearDisplayMode> yearDisplayModeProperty() {
if (yearDisplayMode == null) {
yearDisplayMode = new StyleableObjectProperty<>(DEFAULT_YEAR_DISPLAY_MODE) {
@Override
public Object getBean() {
return CalendarView.this;
}

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

@Override
public CssMetaData<? extends Styleable, YearDisplayMode> getCssMetaData() {
return StyleableProperties.YEAR_DISPLAY_MODE;
}
};
}
return yearDisplayMode;
}

public final YearDisplayMode getYearDisplayMode() {
return yearDisplayMode == null ? DEFAULT_YEAR_DISPLAY_MODE : yearDisplayMode.get();
}

public final void setYearDisplayMode(YearDisplayMode yearDisplayMode) {
yearDisplayModeProperty().set(yearDisplayMode);
}

/**
* Enumerates the display modes for the month label at the top of the calendar view.
*/
public enum MonthDisplayMode {
/**
* Displays only the month text. This is the default mode.
*/
TEXT_ONLY,

/**
* Displays the month text with a dropdown button.
*/
TEXT_AND_DROPDOWN
}

private ObjectProperty<MonthDisplayMode> monthDisplayMode;

/**
* The display mode for the month label at the top of the calendar view.
* {@link MonthDisplayMode#TEXT_ONLY} is the default mode.
*
* @return the month display mode property.
*/
public final ObjectProperty<MonthDisplayMode> monthDisplayModeProperty() {
if (monthDisplayMode == null) {
monthDisplayMode = new StyleableObjectProperty<>(DEFAULT_MONTH_DISPLAY_MODE) {
@Override
public Object getBean() {
return CalendarView.this;
}

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

@Override
public CssMetaData<? extends Styleable, MonthDisplayMode> getCssMetaData() {
return StyleableProperties.MONTH_DISPLAY_MODE;
}
};
}
return monthDisplayMode;
}

public final MonthDisplayMode getMonthDisplayMode() {
return monthDisplayMode == null ? DEFAULT_MONTH_DISPLAY_MODE : monthDisplayMode.get();
}

public final void setMonthDisplayMode(MonthDisplayMode monthDisplayMode) {
monthDisplayModeProperty().set(monthDisplayMode);
}

public static class SelectionModel {

public enum SelectionMode {
Expand Down Expand Up @@ -986,4 +1047,53 @@ public final void setSelectedDates(ObservableList<LocalDate> selectedDates) {
this.selectedDates.set(selectedDates);
}
}

private static class StyleableProperties {

private static final CssMetaData<CalendarView, YearDisplayMode> YEAR_DISPLAY_MODE = new CssMetaData<>(
"-fx-year-display-mode", new EnumConverter<>(YearDisplayMode.class), DEFAULT_YEAR_DISPLAY_MODE) {

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

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

private static final CssMetaData<CalendarView, MonthDisplayMode> MONTH_DISPLAY_MODE = new CssMetaData<>(
"-fx-month-display-mode", new EnumConverter<>(MonthDisplayMode.class), DEFAULT_MONTH_DISPLAY_MODE) {

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

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

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);
STYLEABLES = Collections.unmodifiableList(styleables);
}
}

@Override
public List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() {
return getClassCssMetaData();
}

public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
return CalendarView.StyleableProperties.STYLEABLES;
}

}
Loading
Loading