Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrushforth committed Jun 24, 2022
2 parents e8b9ff1 + da5bd37 commit 929c1c7
Show file tree
Hide file tree
Showing 104 changed files with 19,435 additions and 17,279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,18 @@ public static <S> int getIndexOfChildWithDescendant(TreeItem<S> parent, TreeItem
}
return -1;
}

public static <S> boolean isTreeItemIncludingAncestorsExpanded(TreeItem<S> item) {
if (item == null || !item.isExpanded()) {
return false;
}
TreeItem<S> ancestor = item.getParent();
while (ancestor != null) {
if (!ancestor.isExpanded()) {
return false;
}
ancestor = ancestor.getParent();
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -106,7 +106,15 @@ public class DatePicker extends ComboBoxBase<LocalDate> {
*/
public DatePicker() {
this(null);
}

/**
* Creates a DatePicker instance and sets the
* {@link #valueProperty() value} to the given date.
*
* @param localDate to be set as the currently selected date in the DatePicker. Can be null.
*/
public DatePicker(LocalDate localDate) {
valueProperty().addListener(observable -> {
LocalDate date = getValue();
Chronology chrono = getChronology();
Expand All @@ -115,7 +123,7 @@ public DatePicker() {
lastValidDate = date;
} else {
System.err.println("Restoring value to " +
((lastValidDate == null) ? "null" : getConverter().toString(lastValidDate)));
((lastValidDate == null) ? "null" : getConverter().toString(lastValidDate)));
setValue(lastValidDate);
}
});
Expand All @@ -132,6 +140,17 @@ public DatePicker() {
setChronology(lastValidChronology);
}
});

setValue(localDate);
getStyleClass().add(DEFAULT_STYLE_CLASS);
setAccessibleRole(AccessibleRole.DATE_PICKER);
setEditable(true);

focusedProperty().addListener(o -> {
if (!isFocused()) {
commitValue();
}
});
}

private boolean validateDate(Chronology chrono, LocalDate date) {
Expand All @@ -146,25 +165,6 @@ private boolean validateDate(Chronology chrono, LocalDate date) {
}
}

/**
* Creates a DatePicker instance and sets the
* {@link #valueProperty() value} to the given date.
*
* @param localDate to be set as the currently selected date in the DatePicker. Can be null.
*/
public DatePicker(LocalDate localDate) {
setValue(localDate);
getStyleClass().add(DEFAULT_STYLE_CLASS);
setAccessibleRole(AccessibleRole.DATE_PICKER);
setEditable(true);

focusedProperty().addListener(o -> {
if (!isFocused()) {
commitValue();
}
});
}


/* *************************************************************************
* *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2344,7 +2344,7 @@ private void updateSelection(ListChangeListener.Change<? extends S> c) {
}

TablePosition<S,?> anchor = TableCellBehavior.getAnchor(tableView, null);
if (shift != 0 && startRow >= 0 && anchor != null && (c.wasRemoved() || c.wasAdded())) {
if (shift != 0 && startRow >= 0 && anchor != null && anchor.getRow() >= startRow && (c.wasRemoved() || c.wasAdded())) {
if (isSelected(anchor.getRow(), anchor.getTableColumn())) {
TablePosition<S,?> newAnchor = new TablePosition<>(tableView, anchor.getRow() + shift, anchor.getTableColumn());
TableCellBehavior.setAnchor(tableView, newAnchor, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2611,7 +2611,7 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {
}
} else if (e.wasAdded()) {
// shuffle selection by the number of added items
shift += treeItem.isExpanded() ? addedSize : 0;
shift += ControlUtils.isTreeItemIncludingAncestorsExpanded(treeItem) ? addedSize : 0;

// RT-32963: We were taking the startRow from the TreeItem
// in which the children were added, rather than from the
Expand All @@ -2622,7 +2622,7 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {
startRow = treeTableView.getRow(e.getChange().getAddedSubList().get(0));

TreeTablePosition<S, ?> anchor = TreeTableCellBehavior.getAnchor(treeTableView, null);
if (anchor != null) {
if (anchor != null && anchor.getRow() >= startRow) {
boolean isAnchorSelected = isSelected(anchor.getRow(), anchor.getTableColumn());
if (isAnchorSelected) {
TreeTablePosition<S, ?> newAnchor = new TreeTablePosition<>(treeTableView, anchor.getRow() + shift, anchor.getTableColumn());
Expand All @@ -2647,7 +2647,7 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {

// shuffle selection by the number of removed items
// only if removed items are before the current selection.
if (treeItem.isExpanded()) {
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(treeItem)) {
int lastSelectedSiblingIndex = selectedItems.stream()
.map(item -> ControlUtils.getIndexOfChildWithDescendant(treeItem, item))
.max(Comparator.naturalOrder())
Expand Down Expand Up @@ -3491,7 +3491,7 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {
// get the TreeItem the event occurred on - we only need to
// shift if the tree item is expanded
TreeItem<S> eventTreeItem = e.getTreeItem();
if (eventTreeItem.isExpanded()) {
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(eventTreeItem)) {
for (int i = 0; i < e.getAddedChildren().size(); i++) {
// get the added item and determine the row it is in
TreeItem<S> item = e.getAddedChildren().get(i);
Expand All @@ -3513,7 +3513,7 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {
}
}

if (e.getTreeItem().isExpanded()) {
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(e.getTreeItem())) {
int focusedSiblingRow = ControlUtils.getIndexOfChildWithDescendant(e.getTreeItem(), getFocusedItem());
if (e.getFrom() <= focusedSiblingRow) {
// shuffle selection by the number of removed items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ private void updateTreeEventListener(TreeItem<T> oldRoot, TreeItem<T> newRoot) {
// no-op
} else if (e.wasAdded()) {
// shuffle selection by the number of added items
shift += treeItem.isExpanded() ? addedSize : 0;
shift += ControlUtils.isTreeItemIncludingAncestorsExpanded(treeItem) ? addedSize : 0;

// RT-32963: We were taking the startRow from the TreeItem
// in which the children were added, rather than from the
Expand Down Expand Up @@ -1434,7 +1434,7 @@ private void updateTreeEventListener(TreeItem<T> oldRoot, TreeItem<T> newRoot) {

// shuffle selection by the number of removed items
// only if removed items are before the current selection.
if (treeItem.isExpanded()) {
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(treeItem)) {
int lastSelectedSiblingIndex = selectedItems.stream()
.map(item -> ControlUtils.getIndexOfChildWithDescendant(treeItem, item))
.max(Comparator.naturalOrder())
Expand Down Expand Up @@ -1683,7 +1683,7 @@ private void updateTreeEventListener(TreeItem<T> oldRoot, TreeItem<T> newRoot) {
// get the TreeItem the event occurred on - we only need to
// shift if the tree item is expanded
TreeItem<T> eventTreeItem = e.getTreeItem();
if (eventTreeItem.isExpanded()) {
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(eventTreeItem)) {
for (int i = 0; i < e.getAddedChildren().size(); i++) {
// get the added item and determine the row it is in
TreeItem<T> item = e.getAddedChildren().get(i);
Expand All @@ -1705,7 +1705,7 @@ private void updateTreeEventListener(TreeItem<T> oldRoot, TreeItem<T> newRoot) {
}
}

if (e.getTreeItem().isExpanded()) {
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(e.getTreeItem())) {
int focusedSiblingRow = ControlUtils.getIndexOfChildWithDescendant(e.getTreeItem(), getFocusedItem());
if (e.getFrom() <= focusedSiblingRow) {
// shuffle selection by the number of removed items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,9 @@ private <T,S> void resizeColumnToFitContent(TreeTableView<T> ttv, TreeTableColum
padding = r.snappedLeftInset() + r.snappedRightInset();
}

TreeTableRow<T> treeTableRow = new TreeTableRow<>();
treeTableRow.updateTreeTableView(ttv);
Callback<TreeTableView<T>, TreeTableRow<T>> rowFactory = ttv.getRowFactory();
TreeTableRow<T> treeTableRow = createMeasureRow(ttv, tableSkin, rowFactory);
((SkinBase<?>) treeTableRow.getSkin()).getChildren().add(cell);

int rows = maxRows == -1 ? items.size() : Math.min(items.size(), maxRows);
double maxWidth = 0;
Expand All @@ -752,8 +753,7 @@ private <T,S> void resizeColumnToFitContent(TreeTableView<T> ttv, TreeTableColum
cell.updateIndex(row);

if ((cell.getText() != null && !cell.getText().isEmpty()) || cell.getGraphic() != null) {
tableSkin.getChildren().add(cell);
cell.applyCss();
treeTableRow.applyCss();

double w = cell.prefWidth(-1);

Expand Down Expand Up @@ -797,6 +797,20 @@ private <T,S> void resizeColumnToFitContent(TreeTableView<T> ttv, TreeTableColum
}
}

private <T> TreeTableRow<T> createMeasureRow(TreeTableView<T> ttv, TableViewSkinBase tableSkin,
Callback<TreeTableView<T>, TreeTableRow<T>> rowFactory) {
TreeTableRow<T> treeTableRow = rowFactory != null ? rowFactory.call(ttv) : new TreeTableRow<>();
tableSkin.getChildren().add(treeTableRow);
treeTableRow.applyCss();
if (!(treeTableRow.getSkin() instanceof SkinBase<?>)) {
tableSkin.getChildren().remove(treeTableRow);
// recreate with null rowFactory will result in a standard TableRow that will
// have a SkinBase-derived skin
treeTableRow = createMeasureRow(ttv, tableSkin, null);
}
return treeTableRow;
}

private void updateSortPosition() {
this.sortPos = ! getTableColumn().isSortable() ? -1 : getSortPosition();
updateSortGrid();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,8 +25,12 @@

package test.javafx.scene.control;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.chrono.*;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.ValueRange;
import java.util.*;

import javafx.scene.control.Button;
Expand Down Expand Up @@ -620,6 +624,30 @@ public Node getDisplayNode() {
sl.dispose();
}

@Test
public void testInvalidChronologyIsRestored() {
datePicker = new DatePicker(LocalDate.of(1998, 1, 23));
datePicker.setChronology(IsoChronology.INSTANCE);

assertEquals(IsoChronology.INSTANCE, datePicker.getChronology());

// This should restore the old set chronology (Iso) as the chronology is invalid.
datePicker.setChronology(new InvalidChronology());
assertEquals(IsoChronology.INSTANCE, datePicker.getChronology());
}

@Test
public void testInvalidValueIsRestored() {
datePicker = new DatePicker(null);
assertNull(datePicker.getValue());

datePicker.setChronology(new InvalidChronology());
// This should restore the old set value (null) as the chronology is invalid.
datePicker.setValue(LocalDate.of(1998, 1, 23));

assertNull(datePicker.getValue());
}

@Test
public void testCommitValue() {
datePicker.setEditable(true);
Expand Down Expand Up @@ -709,4 +737,51 @@ public void testFocusLost() {
stageLoader.dispose();
}

private class InvalidChronology extends AbstractChronology {
@Override
public String getId() {
return null;
}
@Override
public String getCalendarType() {
return null;
}
@Override
public ChronoLocalDate date(int prolepticYear, int month, int dayOfMonth) {
return null;
}
@Override
public ChronoLocalDate dateYearDay(int prolepticYear, int dayOfYear) {
return null;
}
@Override
public ChronoLocalDate dateEpochDay(long epochDay) {
return null;
}
@Override
public ChronoLocalDate date(TemporalAccessor temporal) {
throw new DateTimeException("Invalid");
}
@Override
public boolean isLeapYear(long prolepticYear) {
return false;
}
@Override
public int prolepticYear(Era era, int yearOfEra) {
return 0;
}
@Override
public Era eraOf(int eraValue) {
return null;
}
@Override
public List<Era> eras() {
return null;
}
@Override
public ValueRange range(ChronoField field) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5752,4 +5752,47 @@ public void test_ChangeToStringMouseMultipleSelectionCellMode() {
// reset the exception handler
Thread.currentThread().setUncaughtExceptionHandler(exceptionHandler);
}

// see JDK-8284665
@Test
public void testAnchorRemainsWhenAddingMoreItemsBelow() {
TableView<String> stringTableView = new TableView<>();
stringTableView.getItems().addAll("a", "b", "c", "d");

TableColumn<String,String> column = new TableColumn<>("Column");
column.setCellValueFactory(cdf -> new ReadOnlyStringWrapper(cdf.getValue()));
stringTableView.getColumns().add(column);

TableSelectionModel<String> sm = stringTableView.getSelectionModel();
sm.setSelectionMode(SelectionMode.MULTIPLE);

// click on row 1
Cell startCell = VirtualFlowTestUtils.getCell(stringTableView, 1, 0);
new MouseEventFirer(startCell).fireMousePressAndRelease();

assertTrue(sm.isSelected(1));
assertEquals("b", sm.getSelectedItem());

TablePosition anchor = TableCellBehavior.getAnchor(stringTableView, null);
assertTrue(TableCellBehavior.hasNonDefaultAnchor(stringTableView));
assertEquals(1, anchor.getRow());
assertEquals(column, anchor.getTableColumn());

// now add a new item
stringTableView.getItems().add("e");

// select also row 2
Cell endCell = VirtualFlowTestUtils.getCell(stringTableView, 2, 0);
new MouseEventFirer(endCell).fireMousePressAndRelease(KeyModifier.SHIFT);

// row 1 should remain selected
assertTrue(sm.isSelected(1));
assertTrue(sm.isSelected(2));

// anchor should remain at 1
anchor = TableCellBehavior.getAnchor(stringTableView, null);
assertTrue(TableCellBehavior.hasNonDefaultAnchor(stringTableView));
assertEquals(1, anchor.getRow());
assertEquals(column, anchor.getTableColumn());
}
}
Loading

0 comments on commit 929c1c7

Please sign in to comment.