Skip to content

Commit

Permalink
Fixes #178 Provide adjustFirstDayOfWeek in ResourceCalendarView
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Sep 16, 2022
1 parent 54c7da1 commit af9f9eb
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.calendarfx.view.resources.Resource;
import com.calendarfx.view.resources.ResourcesView;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
Expand Down Expand Up @@ -73,20 +74,23 @@ public Node getControlPanel() {
behaviourBox.getItems().setAll(AvailabilityEditingEntryBehaviour.values());
behaviourBox.valueProperty().bindBidirectional(resourcesView.entryViewAvailabilityEditingBehaviourProperty());

CheckBox adjustBox = new CheckBox("Adjust first day of week");
adjustBox.selectedProperty().bindBidirectional(resourcesView.adjustToFirstDayOfWeekProperty());

Slider slider = new Slider();
slider.setMin(0);
slider.setMax(1);
slider.valueProperty().bindBidirectional(resourcesView.entryViewAvailabilityEditingOpacityProperty());

return new VBox(10, availabilityButton, datePicker, daysBox, new Label("Availability Behaviour"), behaviourBox, new Label("Availability Opacity"), slider);
return new VBox(10, availabilityButton, datePicker, adjustBox, daysBox, new Label("Availability Behaviour"), behaviourBox, new Label("Availability Opacity"), slider);
}

@Override
protected DateControl createControl() {
resourcesView = new ResourcesView();
resourcesView.setNumberOfDays(5);
resourcesView.setEarlyLateHoursStrategy(EarlyLateHoursStrategy.HIDE);
resourcesView.getResources().addAll(create("Dirk", Style.STYLE1), create("Katja", Style.STYLE2), create("Philip", Style.STYLE3), create("Jule", Style.STYLE4), create("Armin", Style.STYLE5));
resourcesView.getResources().addAll(create("Dirk", Style.STYLE1), create("Katja", Style.STYLE2), create("Philip", Style.STYLE3)); //, create("Jule", Style.STYLE4), create("Armin", Style.STYLE5));
return resourcesView;
}

Expand Down
25 changes: 20 additions & 5 deletions CalendarFXView/src/main/java/com/calendarfx/view/DateControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -650,25 +650,40 @@ public Optional<Calendar> getCalendarAt(double x, double y) {
*/
public final void showEntry(Entry<?> entry) {
requireNonNull(entry);
doShowEntry(entry, false);
doShowEntry(entry, false, true);
}

/**
* Adjusts the current view / page in such a way that the given entry
* becomes visible and brings up the details editor / UI for the entry
* becomes visible and brings up the detail editor / UI for the entry
* (default is a popover).
*
* @param entry the entry to show
*/
public final void editEntry(Entry<?> entry) {
requireNonNull(entry);
doShowEntry(entry, true);
doShowEntry(entry, true, true);
}

private void doShowEntry(Entry<?> entry, boolean startEditing) {
/**
* Adjusts the current view / page in such a way that the given entry
* becomes visible and brings up the detail editor / UI for the entry
* (default is a popover).
*
* @param entry the entry to show
* @param changeDate change the date of the control to the entry's start date
*/
public final void editEntry(Entry<?> entry, boolean changeDate) {
requireNonNull(entry);
doShowEntry(entry, true, changeDate);
}

private void doShowEntry(Entry<?> entry, boolean startEditing, boolean changeDate) {
layout(); // important so that entry view bounds can be found

setDate(entry.getStartDate());
if (changeDate) {
setDate(entry.getStartDate());
}

Platform.runLater(() -> {
// do not scroll time when a location is already given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public ResourcesView() {
getStyleClass().add(DEFAULT_STYLE);
setShowToday(false);

addEventHandler(REQUEST_ENTRY, evt -> maybeRunAndConsume(evt, e -> editEntry(evt.getEntry())));
// calling "editEntry" with "false" flag because we do not want to change the start date of the view
addEventHandler(REQUEST_ENTRY, evt -> maybeRunAndConsume(evt, e -> editEntry(evt.getEntry(), false)));
}

private void maybeRunAndConsume(RequestEvent evt, Consumer<RequestEvent> consumer) {
Expand All @@ -84,6 +85,37 @@ protected Skin<?> createDefaultSkin() {
return new ResourcesViewSkin(this);
}

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

/**
* A flag used to indicate that the view should always show the first day of
* the week (e.g. "Monday") at its beginning even if the
* {@link #dateProperty()} is set to another day (e.g. "Thursday").
*
* @return true if the view always shows the first day of the week
*/
public final BooleanProperty adjustToFirstDayOfWeekProperty() {
return adjustToFirstDayOfWeek;
}

/**
* Returns the value of {@link #adjustToFirstDayOfWeekProperty()}.
*
* @return true if the view always shows the first day of the week
*/
public final boolean isAdjustToFirstDayOfWeek() {
return adjustToFirstDayOfWeekProperty().get();
}

/**
* Sets the value of {@link #adjustToFirstDayOfWeekProperty()}.
*
* @param adjust if true the view will always show the first day of the week
*/
public final void setAdjustToFirstDayOfWeek(boolean adjust) {
adjustToFirstDayOfWeekProperty().set(adjust);
}

private final ObjectProperty<Callback<T, AllDayView>> allDayViewFactory = new SimpleObjectProperty<>(this, "allDayViewFactory", it-> new AllDayView());

public final Callback<T, AllDayView> getAllDayViewFactory() {
Expand Down Expand Up @@ -287,11 +319,7 @@ public final boolean isShowScrollBar() {
return showScrollBar.get();
}

private final ObjectProperty<Callback<T, WeekView>> weekViewFactory = new SimpleObjectProperty<>(this, "weekViewFactory", resource -> {
WeekView view = new WeekView();
view.setAdjustToFirstDayOfWeek(false);
return view;
});
private final ObjectProperty<Callback<T, WeekView>> weekViewFactory = new SimpleObjectProperty<>(this, "weekViewFactory", resource -> new WeekView());

public final Callback<T, WeekView> getWeekViewFactory() {
return weekViewFactory.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import com.calendarfx.view.WeekView;
import com.calendarfx.view.resources.Resource;
import com.calendarfx.view.resources.ResourcesView;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
Expand All @@ -28,13 +30,45 @@ public ResourcesContainer(ResourcesView<T> view) {
resourcesProperty().bind(view.resourcesProperty());
numberOfDaysProperty().bind(view.numberOfDaysProperty());
weekViewFactoryProperty().bind(view.weekViewFactoryProperty());
adjustToFirstDayOfWeekProperty().bind(view.adjustToFirstDayOfWeekProperty());
}

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

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

/**
* A flag used to indicate that the view should always show the first day of
* the week (e.g. "Monday") at its beginning even if the
* {@link #dateProperty()} is set to another day (e.g. "Thursday").
*
* @return true if the view always shows the first day of the week
*/
public final BooleanProperty adjustToFirstDayOfWeekProperty() {
return adjustToFirstDayOfWeek;
}

/**
* Returns the value of {@link #adjustToFirstDayOfWeekProperty()}.
*
* @return true if the view always shows the first day of the week
*/
public final boolean isAdjustToFirstDayOfWeek() {
return adjustToFirstDayOfWeekProperty().get();
}

/**
* Sets the value of {@link #adjustToFirstDayOfWeekProperty()}.
*
* @param adjust if true the view will always show the first day of the week
*/
public final void setAdjustToFirstDayOfWeek(boolean adjust) {
adjustToFirstDayOfWeekProperty().set(adjust);
}

private final ListProperty<T> resources = new SimpleListProperty<>(this, "resources", FXCollections.observableArrayList());

public final ObservableList<T> getResources() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,54 @@ public ResourcesContainerSkin(ResourcesContainer<T> view) {
private void updateView() {
container.getChildren().clear();

ResourcesContainer<T> resourcesViewContainer = getSkinnable();
ObservableList<T> resources = resourcesViewContainer.getResources();
ResourcesContainer<T> container = getSkinnable();
ObservableList<T> resources = container.getResources();
for (int i = 0; i < resources.size(); i++) {
T resource = resources.get(i);

WeekView weekView = resourcesViewContainer.getWeekViewFactory().call(resource);
WeekView weekView = container.getWeekViewFactory().call(resource);

weekView.setPrefWidth(0); // so they all end up with the same percentage width

// bind day view to container but remove bindings that interfere
resourcesViewContainer.bind(weekView, true);
Bindings.unbindBidirectional(resourcesViewContainer.defaultCalendarProviderProperty(), weekView.defaultCalendarProviderProperty());
Bindings.unbindBidirectional(resourcesViewContainer.draggedEntryProperty(), weekView.draggedEntryProperty());
Bindings.unbindBidirectional(resourcesViewContainer.enableCurrentTimeMarkerProperty(), weekView.enableCurrentTimeMarkerProperty());
Bindings.unbindBidirectional(resourcesViewContainer.enableCurrentTimeCircleProperty(), weekView.enableCurrentTimeCircleProperty());
Bindings.unbindBidirectional(resourcesViewContainer.availabilityCalendarProperty(), weekView.availabilityCalendarProperty());
Bindings.unbindBidirectional(resourcesViewContainer.lassoStartProperty(), weekView.lassoStartProperty());
Bindings.unbindBidirectional(resourcesViewContainer.lassoEndProperty(), weekView.lassoEndProperty());
Bindings.unbindBidirectional(resourcesViewContainer.onLassoFinishedProperty(), weekView.onLassoFinishedProperty());
Bindings.unbindContentBidirectional(resourcesViewContainer.getCalendarSources(), weekView.getCalendarSources());
container.bind(weekView, true);

// rebind "adjust"
weekView.adjustToFirstDayOfWeekProperty().bind(container.adjustToFirstDayOfWeekProperty());

// unbind what is not needed
Bindings.unbindBidirectional(container.defaultCalendarProviderProperty(), weekView.defaultCalendarProviderProperty());
Bindings.unbindBidirectional(container.draggedEntryProperty(), weekView.draggedEntryProperty());
Bindings.unbindBidirectional(container.enableCurrentTimeMarkerProperty(), weekView.enableCurrentTimeMarkerProperty());
Bindings.unbindBidirectional(container.enableCurrentTimeCircleProperty(), weekView.enableCurrentTimeCircleProperty());
Bindings.unbindBidirectional(container.availabilityCalendarProperty(), weekView.availabilityCalendarProperty());
Bindings.unbindBidirectional(container.lassoStartProperty(), weekView.lassoStartProperty());
Bindings.unbindBidirectional(container.lassoEndProperty(), weekView.lassoEndProperty());
Bindings.unbindBidirectional(container.onLassoFinishedProperty(), weekView.onLassoFinishedProperty());
Bindings.unbindContentBidirectional(container.getCalendarSources(), weekView.getCalendarSources());

weekView.setEnableCurrentTimeCircle(i == 0);
weekView.setEnableCurrentTimeMarker(true);

weekView.setAvailabilityCalendar(resource.getAvailabilityCalendar());
weekView.installDefaultLassoFinishedBehaviour();
weekView.numberOfDaysProperty().bind(resourcesViewContainer.numberOfDaysProperty());
weekView.numberOfDaysProperty().bind(container.numberOfDaysProperty());

resourcesViewContainer.numberOfDaysProperty().addListener(it -> System.out.println("number of days (multi resources): " + weekView.getNumberOfDays()));
container.numberOfDaysProperty().addListener(it -> System.out.println("number of days (multi resources): " + weekView.getNumberOfDays()));
weekView.numberOfDaysProperty().addListener(it -> System.out.println("number of days: " + weekView.getNumberOfDays()));

CalendarSource calendarSource = createCalendarSource(resource);
weekView.getCalendarSources().setAll(calendarSource);
weekView.setDefaultCalendarProvider(control -> calendarSource.getCalendars().get(0));

container.getChildren().add(weekView);
this.container.getChildren().add(weekView);

if (i < resources.size() - 1) {
Callback<T, Region> separatorFactory = resourcesViewContainer.getSeparatorFactory();
Callback<T, Region> separatorFactory = container.getSeparatorFactory();
if (separatorFactory != null) {
Region separator = separatorFactory.call(resource);
if (separator != null) {
container.getChildren().add(separator);
this.container.getChildren().add(separator);
HBox.setHgrow(separator, Priority.NEVER);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ private void updateView() {
view.bind(allDayView, true);
allDayView.numberOfDaysProperty().bind(view.numberOfDaysProperty());

// rebind
allDayView.adjustToFirstDayOfWeekProperty().bind(view.adjustToFirstDayOfWeekProperty());

// some unbindings for AllDayView
Bindings.unbindBidirectional(view.defaultCalendarProviderProperty(), allDayView.defaultCalendarProviderProperty());
Bindings.unbindBidirectional(view.draggedEntryProperty(), allDayView.draggedEntryProperty());
Expand All @@ -158,7 +161,7 @@ private void updateView() {
HBox.setHgrow(resourceHeader, Priority.ALWAYS);

WeekDayHeaderView weekDayHeaderView = view.getWeekDayHeaderViewFactory().call(resource);
weekDayHeaderView.setAdjustToFirstDayOfWeek(false);
weekDayHeaderView.adjustToFirstDayOfWeekProperty().bind(view.adjustToFirstDayOfWeekProperty());
weekDayHeaderView.numberOfDaysProperty().bind(view.numberOfDaysProperty());
view.bind(weekDayHeaderView, true);

Expand Down

0 comments on commit af9f9eb

Please sign in to comment.