Skip to content

Commit

Permalink
Refactor event handling and improve navigation in CalendarPicker
Browse files Browse the repository at this point in the history
Reorganized mouse and touch events to occur in the CalendarPicker instead of CalendarPickerSkin. Altered key events to work with individual days instead of months for more precise navigation. Introduced a String Converter for parsing LocalDate types. The changes provide a more intuitive interaction with the calendar and streamline its functionality.
  • Loading branch information
dlemmermann committed Feb 22, 2024
1 parent c9af1d7 commit 4c11bda
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
53 changes: 30 additions & 23 deletions gemsfx/src/main/java/com/dlsc/gemsfx/CalendarPicker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dlsc.gemsfx;

import com.dlsc.gemsfx.skins.CalendarPickerSkin;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.css.PseudoClass;
Expand All @@ -11,6 +12,7 @@
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Region;
import javafx.util.StringConverter;
import javafx.util.converter.LocalDateStringConverter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
Expand Down Expand Up @@ -39,6 +41,11 @@ public CalendarPicker() {

getStyleClass().setAll("calendar-picker", "text-input");

setEditable(true);

setOnMouseClicked(evt -> commitValueAndShow());
setOnTouchPressed(evt -> commitValueAndShow());

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

Expand All @@ -56,11 +63,19 @@ public CalendarPicker() {
pseudoClassStateChanged(PseudoClass.getPseudoClass("focused"), editor.isFocused());
});

editor.addEventHandler(KeyEvent.ANY, evt -> {
if (evt.getCode().equals(KeyCode.DOWN)) {
setValue(getValue().plusMonths(1));
} else if (evt.getCode().equals(KeyCode.UP)) {
setValue(getValue().minusMonths(1));
editor.addEventHandler(KeyEvent.KEY_PRESSED, evt -> {
if (evt.getCode().equals(KeyCode.UP)) {
setValue(getValue().minusDays(1));
placeCaretAtEnd();
} else if (evt.getCode().equals(KeyCode.DOWN)) {
setValue(getValue().plusDays(1));
placeCaretAtEnd();
} else if (evt.getCode().equals(KeyCode.LEFT) && !isEditable()) {
setValue(getValue().minusDays(1));
placeCaretAtEnd();
} else if (evt.getCode().equals(KeyCode.RIGHT) && !isEditable()) {
setValue(getValue().plusDays(1));
placeCaretAtEnd();
}
});

Expand All @@ -74,6 +89,15 @@ public CalendarPicker() {
updateText();
}

private void placeCaretAtEnd() {
Platform.runLater(() -> getEditor().positionCaret(getEditor().textProperty().getValueSafe().length()));
}

private void commitValueAndShow() {
commitValue();
show();
}

@Override
protected Skin<?> createDefaultSkin() {
return new CalendarPickerSkin(this);
Expand Down Expand Up @@ -133,24 +157,7 @@ public final TextField getEditor() {
return editor;
}

private final ObjectProperty<StringConverter<LocalDate>> converter = new SimpleObjectProperty<>(this, "value", new StringConverter<>() {
@Override
public String toString(LocalDate object) {
if (object != null) {
return DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(object);
}
return null;
}

@Override
public LocalDate fromString(String string) {
try {
return DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).parse(string, LocalDate::from);
} catch (DateTimeParseException ex) {
return null;
}
}
});
private final ObjectProperty<StringConverter<LocalDate>> converter = new SimpleObjectProperty<>(this, "value", new LocalDateStringConverter());

public final StringConverter<LocalDate> getConverter() {
return converter.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class CalendarPickerSkin extends CustomComboBoxSkinBase<CalendarPicker> {
public CalendarPickerSkin(CalendarPicker picker) {
super(picker);

picker.setOnMouseClicked(evt -> picker.show());
picker.setOnTouchPressed(evt -> picker.show());
picker.valueProperty().addListener(it -> {
if (view != null) {
view.setYearMonth(YearMonth.from(picker.getValue()));
Expand All @@ -49,7 +47,10 @@ public CalendarPickerSkin(CalendarPicker picker) {

protected Node getPopupContent() {
if (view == null) {
view = getSkinnable().getCalendarView();
CalendarPicker picker = getSkinnable();
view = picker.getCalendarView();
view.setYearMonth(YearMonth.from(picker.getValue()));
view.getSelectionModel().select(picker.getValue());
view.setFocusTraversable(false); // keep the picker focused / blue border
view.selectionModelProperty().addListener((obs, oldModel, newModel) -> bindSelectionModel(oldModel, newModel));
bindSelectionModel(null, view.getSelectionModel());
Expand Down

0 comments on commit 4c11bda

Please sign in to comment.