From 91ee07aad536b21d7ec9bccf4c478b268e77dd8f Mon Sep 17 00:00:00 2001 From: Dirk Lemmermann Date: Tue, 27 Sep 2022 12:39:45 +0200 Subject: [PATCH] Fixes #187 Changing date will leave all events visible from previous date range --- .../view/resources/ResourcesContainer.java | 224 ------------------ .../resources/ResourcesViewContainer.java | 27 +++ ...n.java => ResourcesViewContainerSkin.java} | 69 +++--- .../view/resources/ResourcesViewSkin.java | 7 +- 4 files changed, 64 insertions(+), 263 deletions(-) delete mode 100644 CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesContainer.java create mode 100644 CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewContainer.java rename CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/{ResourcesContainerSkin.java => ResourcesViewContainerSkin.java} (78%) diff --git a/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesContainer.java b/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesContainer.java deleted file mode 100644 index 6377417d..00000000 --- a/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesContainer.java +++ /dev/null @@ -1,224 +0,0 @@ -package impl.com.calendarfx.view.resources; - -import com.calendarfx.view.DayView; -import com.calendarfx.view.DayViewBase; -import com.calendarfx.view.WeekView; -import com.calendarfx.view.resources.Resource; -import com.calendarfx.view.resources.ResourcesView; -import com.calendarfx.view.resources.ResourcesView.Type; -import javafx.beans.binding.Bindings; -import javafx.beans.property.BooleanProperty; -import javafx.beans.property.IntegerProperty; -import javafx.beans.property.ObjectProperty; -import javafx.beans.property.SimpleBooleanProperty; -import javafx.beans.property.SimpleIntegerProperty; -import javafx.beans.property.SimpleObjectProperty; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; -import javafx.scene.control.Skin; -import javafx.scene.layout.Region; -import javafx.util.Callback; - -public class ResourcesContainer> extends DayViewBase { - - private final ResourcesView resourcesView; - - public ResourcesContainer(ResourcesView view) { - this.resourcesView = view; - - getStyleClass().add("resources-view-container"); - setShowToday(false); - - Bindings.bindContentBidirectional(getResources(), view.getResources()); - - numberOfDaysProperty().bind(view.numberOfDaysProperty()); - smallSeparatorFactoryProperty().bind(view.smallSeparatorFactoryProperty()); - largeSeparatorFactoryProperty().bind(view.largeSeparatorFactoryProperty()); - numberOfDaysProperty().bind(view.numberOfDaysProperty()); - weekViewFactoryProperty().bind(view.weekViewFactoryProperty()); - dayViewFactoryProperty().bind(view.dayViewFactoryProperty()); - adjustToFirstDayOfWeekProperty().bind(view.adjustToFirstDayOfWeekProperty()); - typeProperty().bind(view.typeProperty()); - } - - @Override - protected Skin createDefaultSkin() { - return new ResourcesContainerSkin<>(this); - } - - public final ResourcesView getResourcesView() { - return resourcesView; - } - - private final ObjectProperty type = new SimpleObjectProperty<>(this, "type", Type.RESOURCES_OVER_DATE); - - public final Type getType() { - return type.get(); - } - - /** - * Determines the visualization type: resoruces over dates or dates over resources. - * - * @return the visualization type - */ - public final ObjectProperty typeProperty() { - return type; - } - - public final void setType(Type type) { - this.type.set(type); - } - - 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 ObservableList resources = FXCollections.observableArrayList(); - - /** - * The resources to be shown in this view. - * - * @return the list of resources - */ - public final ObservableList getResources() { - return resources; - } - - private final IntegerProperty numberOfDays = new SimpleIntegerProperty(this, "numberOfDays", 7); - - /** - * Stores the number of days that will be shown by this view. - * - * @return the number of days shown by the view - */ - public final IntegerProperty numberOfDaysProperty() { - return numberOfDays; - } - - /** - * Returns the value of {@link #numberOfDaysProperty()}. - * - * @return the number of days shown by the view - */ - public final int getNumberOfDays() { - return numberOfDaysProperty().get(); - } - - /** - * Sets the value of {@link #numberOfDaysProperty()}. - * - * @param number the new number of days shown by the view - */ - public final void setNumberOfDays(int number) { - if (number < 1) { - throw new IllegalArgumentException("invalid number of days, must be larger than 0 but was " + number); - } - - numberOfDaysProperty().set(number); - } - - private final ObjectProperty> weekViewFactory = new SimpleObjectProperty<>(this, "weekViewFactory"); - - public final Callback getWeekViewFactory() { - return weekViewFactory.get(); - } - - /** - * A factory used for creating a new {@link WeekView} instance for each resource - * shown in the view. - * - * @return a factory for resource week views - */ - public final ObjectProperty> weekViewFactoryProperty() { - return weekViewFactory; - } - - public void setWeekViewFactory(Callback weekViewFactory) { - this.weekViewFactory.set(weekViewFactory); - } - - private final ObjectProperty> dayViewFactory = new SimpleObjectProperty<>(this, "dayViewFactory"); - - public final Callback getDayViewFactory() { - return dayViewFactory.get(); - } - - /** - * A factory used for creating a new {@link DayView} instance for a resource day - * shown in the view. - * - * @return a factory for resource day views - */ - public final ObjectProperty> dayViewFactoryProperty() { - return dayViewFactory; - } - - public void setDayViewFactory(Callback dayViewFactory) { - this.dayViewFactory.set(dayViewFactory); - } - - private final ObjectProperty, Region>> smallSeparatorFactory = new SimpleObjectProperty<>(this, "smallSeparatorFactory", it-> { - Region region = new Region(); - region.getStyleClass().add("small-separator"); - return region; - }); - - public final Callback, Region> getSmallSeparatorFactory() { - return smallSeparatorFactory.get(); - } - - public final ObjectProperty, Region>> smallSeparatorFactoryProperty() { - return smallSeparatorFactory; - } - - public final void setSmallSeparatorFactory(Callback, Region> smallSeparatorFactory) { - this.smallSeparatorFactory.set(smallSeparatorFactory); - } - - private final ObjectProperty, Region>> largeSeparatorFactory = new SimpleObjectProperty<>(this, "largeSeparatorFactory"); - - public final Callback, Region> getLargeSeparatorFactory() { - return largeSeparatorFactory.get(); - } - - /** - * A factory used for creating the vertical separators between the resources. - * - * @return the resource separator factory - */ - public final ObjectProperty, Region>> largeSeparatorFactoryProperty() { - return largeSeparatorFactory; - } - - public final void setLargeSeparatorFactory(Callback, Region> largeSeparatorFactory) { - this.largeSeparatorFactory.set(largeSeparatorFactory); - } -} - diff --git a/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewContainer.java b/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewContainer.java new file mode 100644 index 00000000..ad8d0024 --- /dev/null +++ b/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewContainer.java @@ -0,0 +1,27 @@ +package impl.com.calendarfx.view.resources; + +import com.calendarfx.view.DayViewBase; +import com.calendarfx.view.resources.Resource; +import com.calendarfx.view.resources.ResourcesView; +import javafx.scene.control.Skin; + +public class ResourcesViewContainer> extends DayViewBase { + + private final ResourcesView resourcesView; + + public ResourcesViewContainer(ResourcesView view) { + this.resourcesView = view; + getStyleClass().add("resources-view-container"); + setShowToday(false); + } + + @Override + protected Skin createDefaultSkin() { + return new ResourcesViewContainerSkin<>(this); + } + + public final ResourcesView getResourcesView() { + return resourcesView; + } +} + diff --git a/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesContainerSkin.java b/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewContainerSkin.java similarity index 78% rename from CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesContainerSkin.java rename to CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewContainerSkin.java index 02fcb76b..00e284d5 100644 --- a/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesContainerSkin.java +++ b/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewContainerSkin.java @@ -10,43 +10,43 @@ import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.beans.binding.Bindings; -import javafx.beans.property.ObjectProperty; -import javafx.beans.property.SimpleObjectProperty; import javafx.collections.ObservableList; import javafx.scene.layout.HBox; import javafx.scene.layout.Priority; import javafx.scene.layout.Region; import javafx.util.Callback; -import java.time.LocalDate; +public class ResourcesViewContainerSkin> extends DayViewBaseSkin> { -public class ResourcesContainerSkin> extends DayViewBaseSkin> { + private final HBox box = new HBox(); - private final HBox container = new HBox(); + private final ResourcesView resourcesView; - public ResourcesContainerSkin(ResourcesContainer view) { + public ResourcesViewContainerSkin(ResourcesViewContainer view) { super(view); - container.getStyleClass().add("container"); - container.setFillHeight(true); + box.getStyleClass().add("container"); + box.setFillHeight(true); + + resourcesView = view.getResourcesView(); InvalidationListener updateViewListener = (Observable it) -> updateView(); - view.getResources().addListener(updateViewListener); - view.numberOfDaysProperty().addListener(updateViewListener); - view.typeProperty().addListener(updateViewListener); + resourcesView.getResources().addListener(updateViewListener); + resourcesView.numberOfDaysProperty().addListener(updateViewListener); + resourcesView.typeProperty().addListener(updateViewListener); updateView(); - getChildren().add(container); + getChildren().add(box); } private void updateView() { - container.getChildren().clear(); + box.getChildren().clear(); - ResourcesContainer resourcesContainer = getSkinnable(); + ResourcesViewContainer resourcesContainer = getSkinnable(); resourcesContainer.unbindAll(); - if (resourcesContainer.getType().equals(Type.RESOURCES_OVER_DATE)) { + if (resourcesView.getType().equals(Type.RESOURCES_OVER_DATE)) { updateViewResourcesOverDates(); } else { updateViewDatesOverResources(); @@ -54,21 +54,22 @@ private void updateView() { } private void updateViewDatesOverResources() { - ResourcesContainer container = getSkinnable(); - ObservableList resources = container.getResources(); - int numberOfDays = container.getNumberOfDays(); + ResourcesViewContainer container = getSkinnable(); + ObservableList resources = resourcesView.getResources(); + int numberOfDays = resourcesView.getNumberOfDays(); for (int dayIndex = 0; dayIndex < numberOfDays; dayIndex++) { HBox resourcesBox = new HBox(); - ObjectProperty dateProperty = new SimpleObjectProperty<>(this, "date"); - final int additionalDays = dayIndex; - dateProperty.bind(Bindings.createObjectBinding(() -> container.getDate().plusDays(additionalDays), container.dateProperty())); for (int resourceIndex = 0; resourceIndex < resources.size(); resourceIndex++) { T resource = resources.get(resourceIndex); - DayView dayView = container.getDayViewFactory().call(resource); + DayView dayView = resourcesView.getDayViewFactory().call(resource); + + final int additionalDays = dayIndex; + + dayView.dateProperty().bind(Bindings.createObjectBinding(() -> container.getDate().plusDays(additionalDays), container.dateProperty())); dayView.getStyleClass().removeAll("only", "first", "middle", "last"); @@ -87,8 +88,6 @@ private void updateViewDatesOverResources() { // bind day view to container but remove bindings that interfere container.bind(dayView, false); - dayView.dateProperty().bind(dateProperty); - // unbind what is not needed Bindings.unbindBidirectional(container.defaultCalendarProviderProperty(), dayView.defaultCalendarProviderProperty()); Bindings.unbindBidirectional(container.draggedEntryProperty(), dayView.draggedEntryProperty()); @@ -122,14 +121,14 @@ private void updateViewDatesOverResources() { } } - this.container.getChildren().add(resourcesBox); + this.box.getChildren().add(resourcesBox); if (dayIndex < numberOfDays - 1) { - Callback, Region> separatorFactory = container.getLargeSeparatorFactory(); + Callback, Region> separatorFactory = resourcesView.getLargeSeparatorFactory(); if (separatorFactory != null) { Region separator = separatorFactory.call(container.getResourcesView()); if (separator != null) { - this.container.getChildren().add(separator); + this.box.getChildren().add(separator); HBox.setHgrow(separator, Priority.NEVER); } } @@ -139,12 +138,12 @@ private void updateViewDatesOverResources() { } private void updateViewResourcesOverDates() { - ResourcesContainer container = getSkinnable(); - ObservableList resources = container.getResources(); + ResourcesViewContainer container = getSkinnable(); + ObservableList resources = resourcesView.getResources(); for (int i = 0; i < resources.size(); i++) { T resource = resources.get(i); - WeekView weekView = container.getWeekViewFactory().call(resource); + WeekView weekView = resourcesView.getWeekViewFactory().call(resource); weekView.getStyleClass().removeAll("only", "first", "middle", "last"); @@ -166,7 +165,7 @@ private void updateViewResourcesOverDates() { container.bind(weekView, true); // bind additionally "adjust" - weekView.adjustToFirstDayOfWeekProperty().bind(container.adjustToFirstDayOfWeekProperty()); + weekView.adjustToFirstDayOfWeekProperty().bind(resourcesView.adjustToFirstDayOfWeekProperty()); // unbind what is not needed Bindings.unbindBidirectional(container.defaultCalendarProviderProperty(), weekView.defaultCalendarProviderProperty()); @@ -186,20 +185,20 @@ private void updateViewResourcesOverDates() { weekView.installDefaultLassoFinishedBehaviour(); - weekView.numberOfDaysProperty().bind(container.numberOfDaysProperty()); + weekView.numberOfDaysProperty().bind(resourcesView.numberOfDaysProperty()); CalendarSource calendarSource = createCalendarSource(resource); weekView.getCalendarSources().setAll(calendarSource); weekView.setDefaultCalendarProvider(control -> calendarSource.getCalendars().get(0)); - this.container.getChildren().add(weekView); + this.box.getChildren().add(weekView); if (i < resources.size() - 1) { - Callback, Region> separatorFactory = container.getLargeSeparatorFactory(); + Callback, Region> separatorFactory = resourcesView.getLargeSeparatorFactory(); if (separatorFactory != null) { Region separator = separatorFactory.call(container.getResourcesView()); if (separator != null) { - this.container.getChildren().add(separator); + this.box.getChildren().add(separator); HBox.setHgrow(separator, Priority.NEVER); } } diff --git a/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewSkin.java b/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewSkin.java index 45e08689..2afc96f8 100644 --- a/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewSkin.java +++ b/CalendarFXView/src/main/java/impl/com/calendarfx/view/resources/ResourcesViewSkin.java @@ -50,7 +50,7 @@ public class ResourcesViewSkin> extends DateControlSkin> { private final GridPane gridPane; - private final ResourcesContainer resourcesContainer; + private final ResourcesViewContainer resourcesContainer; private final DayViewScrollPane timeScaleScrollPane; private final DayViewScrollPane dayViewsScrollPane; private final ScrollBar scrollBar; @@ -93,7 +93,8 @@ public ResourcesViewSkin(ResourcesView view) { gridPane.getRowConstraints().setAll(row0, row1); gridPane.getStyleClass().add("container"); - resourcesContainer = new ResourcesContainer<>(view); + resourcesContainer = new ResourcesViewContainer<>(view); + view.bind(resourcesContainer, true); getChildren().add(gridPane); @@ -116,8 +117,6 @@ private void updateView() { gridPane.getColumnConstraints().clear(); ResourcesView view = getSkinnable(); - view.unbindAll(); - if (view.getType().equals(Type.RESOURCES_OVER_DATE)) { updateViewResourcesOverDates(); } else {