Skip to content

Commit

Permalink
More refinement of event handling in DayViews.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Sep 29, 2022
1 parent 851f2aa commit 5690599
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void run() {
updateTimeThread.start();

Scene scene = new Scene(stackPane);
scene.focusOwnerProperty().addListener(it -> System.out.println("focus owner: " + scene.getFocusOwner()));
// scene.focusOwnerProperty().addListener(it -> System.out.println("focus owner: " + scene.getFocusOwner()));
CSSFX.start(scene);

primaryStage.setTitle("Calendar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,16 @@ public Type fromString(String string) {
CheckBox detailsBox = new CheckBox("Show details upon creation");
detailsBox.selectedProperty().bindBidirectional(resourcesView.showDetailsUponEntryCreationProperty());

CheckBox flipBox = new CheckBox("Enable start / end flip over");
flipBox.selectedProperty().bindBidirectional(resourcesView.enableStartAndEndTimesFlipProperty());

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

return new VBox(10, availabilityButton, new Label("View type"), typeBox, datePicker, adjustBox, new Label("Number of resources"), numberOfResourcesBox, new Label("Number of days"), daysBox, new Label("Clicks to create"), clicksBox,
new Label("Availability Behaviour"), behaviourBox, new Label("Availability Opacity"), slider, new Label("Grid Type"), gridTypeBox, scrollbarBox, timescaleBox, allDayBox, detailsBox);
new Label("Availability Behaviour"), behaviourBox, new Label("Availability Opacity"), slider, new Label("Grid Type"), gridTypeBox, scrollbarBox, timescaleBox, allDayBox, detailsBox, flipBox);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ public final void bind(DayViewBase otherControl, boolean bindDate) {
Bindings.bindBidirectional(otherControl.gridLinesProperty(), gridLinesProperty());
Bindings.bindBidirectional(otherControl.gridLineColorProperty(), gridLineColorProperty());
Bindings.bindBidirectional(otherControl.gridTypeProperty(), gridTypeProperty());
Bindings.bindBidirectional(otherControl.enableStartAndEndTimesFlipProperty(), enableStartAndEndTimesFlipProperty());
}

public final void unbind(DayViewBase otherControl) {
Expand All @@ -1139,6 +1140,7 @@ public final void unbind(DayViewBase otherControl) {
Bindings.unbindBidirectional(otherControl.gridLinesProperty(), gridLinesProperty());
Bindings.unbindBidirectional(otherControl.gridLineColorProperty(), gridLineColorProperty());
Bindings.unbindBidirectional(otherControl.gridTypeProperty(), gridTypeProperty());
Bindings.unbindBidirectional(otherControl.enableStartAndEndTimesFlipProperty(), enableStartAndEndTimesFlipProperty());
}

private static final String DAY_VIEW_BASE_CATEGORY = "Date View Base";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,32 @@ private void mouseClicked(MouseEvent evt) {
return;
}

if (evt.getClickCount() == 2 && view.getCreateEntryClickCount() == 2) {
createEntryAt(evt, false);
// mouse clicks only work on day views, not on entry views
if (!evt.getTarget().equals(view)) {
return;
}

if (evt.getClickCount() == view.getCreateEntryClickCount()) {
Entry<?> entry = createEntryAt(evt);
evt.consume();

if (view.isShowDetailsUponEntryCreation()) {
view.fireEvent(new RequestEvent(view, view, entry));
}
}
}

private Operation operation = Operation.NONE;

private MouseEvent mousePressedEvent;

private void mousePressed(MouseEvent evt) {
if (evt.isConsumed() || evt.getClickCount() > 1 || !evt.getButton().equals(MouseButton.PRIMARY)) {
if (evt.isConsumed() || !evt.getButton().equals(MouseButton.PRIMARY)) {
return;
}

mousePressedEvent = evt;

entry = null;

LOGGER.finer("mouse event source: " + evt.getSource());
Expand All @@ -212,10 +226,13 @@ private void mousePressed(MouseEvent evt) {
}
}

private boolean entryEditingAllowed;

private void mousePressedEditEntry(MouseEvent evt) {
System.out.println("event on an entry view");
operation = Operation.EDIT_ENTRY;

entryEditingAllowed = false;

dragMode = null;
handle = null;

Expand All @@ -224,8 +241,6 @@ private void mousePressedEditEntry(MouseEvent evt) {
LOGGER.finer("mouse event y-coordinate:" + evt.getY());
LOGGER.finer("time: " + view.getZonedDateTimeAt(evt.getX(), evt.getY(), view.getZoneId()));

boolean initiallyHideDraggedEntry = false;

boolean successfulInitialization = initDragModeAndHandle(evt);

if (successfulInitialization) {
Expand All @@ -242,35 +257,23 @@ private void mousePressedEditEntry(MouseEvent evt) {
if (entryEditPolicy.call(new EntryEditParameter(view, entry, EditOperation.MOVE))) {
operationAllowed = true;

if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged", true);
}

Instant time = view.getInstantAt(evt);
offsetDuration = Duration.between(entry.getStartAsZonedDateTime().toInstant(), time);
entryDuration = entry.getDuration();

LOGGER.finer("time at mouse pressed location: " + time);
LOGGER.finer("offset duration: " + offsetDuration);
LOGGER.finer("entry duration: " + entryDuration);

view.requestLayout();
}
break;
case END_TIME:
if (entryEditPolicy.call(new EntryEditParameter(view, entry, EditOperation.CHANGE_END))) {
operationAllowed = true;
if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged-end", true);
}
}
break;
case START_TIME:
if (entryEditPolicy.call(new EntryEditParameter(view, entry, EditOperation.CHANGE_START))) {
operationAllowed = true;
if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged-start", true);
}
}
break;
default:
Expand All @@ -281,70 +284,26 @@ private void mousePressedEditEntry(MouseEvent evt) {
return;
}

if (view != null) {
DraggedEntry draggedEntry = new DraggedEntry(entry, dragMode);
draggedEntry.setOffsetDuration(offsetDuration);
draggedEntry.setHidden(initiallyHideDraggedEntry);
view.setDraggedEntry(draggedEntry);
}
} else {
System.out.println("nope");
LOGGER.fine("unsuccessful initialization");
entryEditingAllowed = true;
}
}

private void mousePressedOnDayView(MouseEvent evt) {
if (view.isEditAvailability()) {
mousePressedEditAvailability(evt);
} else {
mousePressedCreateEntry(evt);
operation = Operation.CREATE_ENTRY;
}
}

private void mousePressedEditAvailability(MouseEvent evt) {
System.out.println("mouse pressed to edit availability");
operation = Operation.EDIT_AVAILABILITY;
VirtualGrid availabilityGrid = view.getAvailabilityGrid();
setLassoStart(snapToGrid(view.getInstantAt(evt), availabilityGrid, false));
setLassoEnd(snapToGrid(view.getInstantAt(evt), availabilityGrid, false).plus(availabilityGrid.getAmount(), availabilityGrid.getUnit()));
}

private void mousePressedCreateEntry(MouseEvent evt) {
if (view.getCreateEntryClickCount() == evt.getClickCount()) {
operation = Operation.CREATE_ENTRY;

dragMode = null;
handle = null;

entry = createEntryAt(evt, true);

DayView dayView = null;

if (view instanceof DayView) {
dayView = (DayView) view;
} else if (view instanceof WeekView) {
WeekView weekView = (WeekView) view;
dayView = weekView.getWeekDayViews().get(0);
}

if (dayView != null) {
dragMode = DragMode.END_TIME;
handle = Handle.BOTTOM;
}

if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged-end", true);
}

DraggedEntry draggedEntry = new DraggedEntry(entry, dragMode);
draggedEntry.setOffsetDuration(offsetDuration);
draggedEntry.setHidden(true);
view.setDraggedEntry(draggedEntry);
}
}

private Entry<?> createEntryAt(MouseEvent evt, boolean hidden) {
System.out.println("CREATING AN ENTRY");
private Entry<?> createEntryAt(MouseEvent evt) {
Optional<Calendar> calendar = view.getCalendarAt(evt.getX(), evt.getY());
Instant instantAt = view.getInstantAt(evt);

Expand All @@ -354,14 +313,26 @@ private Entry<?> createEntryAt(MouseEvent evt, boolean hidden) {
}

ZonedDateTime time = ZonedDateTime.ofInstant(instantAt, view.getZoneId());
Entry<?> newEntry = view.createEntryAt(time, calendar.orElse(null), hidden);
Entry<?> newEntry = view.createEntryAt(time, calendar.orElse(null), false);

Duration duration = newEntry.getMinimumDuration();

LOGGER.fine("minimum duration for the entry is " + duration);

if (virtualGrid != null) {
newEntry.setInterval(newEntry.getInterval().withEndTime(newEntry.getInterval().getStartTime().plus(virtualGrid.getAmount(), virtualGrid.getUnit())));
} else {
newEntry.setInterval(newEntry.getInterval().withEndTime(newEntry.getInterval().getStartTime().plus(newEntry.getMinimumDuration())));
LOGGER.fine("checking the virtual grid duration");
Duration gridAmount = Duration.of(virtualGrid.getAmount(), virtualGrid.getUnit());
if (gridAmount.toMillis() > duration.toMillis()) {
LOGGER.fine("using the grid amount as it is longer than the minimum duration of the entry");
duration = gridAmount;
}
}

newEntry.setInterval(newEntry.getInterval().withEndTime(newEntry.getInterval().getStartTime().plus(duration)));

view.getSelections().clear();
view.getSelections().add(newEntry);

return newEntry;
}

Expand Down Expand Up @@ -393,16 +364,65 @@ private void mouseDraggedEditAvailability(MouseEvent evt) {
}

private void mouseDraggedCreateEntry(MouseEvent evt) {
DraggedEntry draggedEntry = view.getDraggedEntry();
draggedEntry.getOriginalEntry().setHidden(false);
draggedEntry.setHidden(false);
if (entry == null) {
dragMode = null;
handle = null;

// Important, use the initial mouse event when the user pressed the button
entry = createEntryAt(mousePressedEvent);

DayView dayView = null;

if (view instanceof DayView) {
dayView = (DayView) view;
} else if (view instanceof WeekView) {
WeekView weekView = (WeekView) view;
dayView = weekView.getWeekDayViews().get(0);
}

if (dayView != null) {
dragMode = DragMode.END_TIME;
handle = Handle.BOTTOM;
}

if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged-end", true);
}

DraggedEntry draggedEntry = new DraggedEntry(entry, dragMode);
draggedEntry.setOffsetDuration(offsetDuration);
view.setDraggedEntry(draggedEntry);
}

changeEndTime(evt);
}

private void mouseDraggedEditEntry(MouseEvent evt) {
DraggedEntry draggedEntry = view.getDraggedEntry();
draggedEntry.getOriginalEntry().setHidden(false);
draggedEntry.setHidden(false);
if (entryEditingAllowed && view.getDraggedEntry() == null) {
DraggedEntry draggedEntry = new DraggedEntry(entry, dragMode);
draggedEntry.setOffsetDuration(offsetDuration);
view.setDraggedEntry(draggedEntry);

switch (dragMode) {
case START_AND_END_TIME:
if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged", true);
}
break;
case END_TIME:
if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged-end", true);
}
break;
case START_TIME:
if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged-start", true);
}
break;
default:
break;
}
}

switch (dragMode) {
case START_TIME:
Expand Down Expand Up @@ -479,30 +499,30 @@ private void mouseReleasedEditEntry() {
}

private void mouseReleasedCreateEntry() {
Calendar calendar = entry.getCalendar();
if (calendar.isReadOnly()) {
return;
}

if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged", false);
dayEntryView.getProperties().put("dragged-start", false);
dayEntryView.getProperties().put("dragged-end", false);
}
if (entry != null) {
Calendar calendar = entry.getCalendar();
if (calendar.isReadOnly()) {
return;
}

/*
* We might run in the sampler application. Then the entry view will not
* be inside a date control.
*/
DraggedEntry draggedEntry = view.getDraggedEntry();
if (dayEntryView != null) {
dayEntryView.getProperties().put("dragged", false);
dayEntryView.getProperties().put("dragged-start", false);
dayEntryView.getProperties().put("dragged-end", false);
}

if (draggedEntry != null) {
view.setDraggedEntry(null);
draggedEntry.setHidden(false);
draggedEntry.getOriginalEntry().setHidden(false);
entry.setInterval(draggedEntry.getInterval());
if (view.isShowDetailsUponEntryCreation() && operation.equals(Operation.CREATE_ENTRY)) {
view.fireEvent(new RequestEvent(view, view, entry));
/*
* We might run in the sampler application. Then the entry view will not
* be inside a date control.
*/
DraggedEntry draggedEntry = view.getDraggedEntry();

if (draggedEntry != null) {
view.setDraggedEntry(null);
entry.setInterval(draggedEntry.getInterval());
if (view.isShowDetailsUponEntryCreation() && operation.equals(Operation.CREATE_ENTRY)) {
view.fireEvent(new RequestEvent(view, view, entry));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ public boolean intersects(EntryViewBase<?> view) {
entryEndTime = entryEndTime.with(LocalTime.MAX);
}

return entryStartTime.isBefore(endTime)
&& entryEndTime.isAfter(startTime);

return entryStartTime.isBefore(endTime) && entryEndTime.isAfter(startTime);
}

public List<Placement> resolve() {
Expand Down

0 comments on commit 5690599

Please sign in to comment.