Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrushforth committed Jul 12, 2022
2 parents ad02ce3 + d3c26c4 commit fcf2a46
Show file tree
Hide file tree
Showing 16 changed files with 1,295 additions and 61 deletions.
Expand Up @@ -467,8 +467,8 @@ static <X,Y> void makePaths(XYChart<X, Y> chart, Series<X, Y> series,
if (x < dataXMin || y < dataYMin) {
if (prevDataPoint == null) {
prevDataPoint = new LineTo(x, y);
} else if ((sortX && prevDataPoint.getX() < x) ||
(sortY && prevDataPoint.getY() < y))
} else if ((sortX && prevDataPoint.getX() <= x) ||
(sortY && prevDataPoint.getY() <= y))
{
prevDataPoint.setX(x);
prevDataPoint.setY(y);
Expand Down
Expand Up @@ -36,6 +36,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.IntPredicate;
import java.util.stream.Collectors;

class ControlUtils {
Expand Down Expand Up @@ -156,7 +157,7 @@ private void checkState() {
};
}

public static <S> void updateSelectedIndices(MultipleSelectionModelBase<S> sm, ListChangeListener.Change<? extends TablePositionBase<?>> c) {
public static <S> void updateSelectedIndices(MultipleSelectionModelBase<S> sm, boolean isCellSelectionEnabled, ListChangeListener.Change<? extends TablePositionBase<?>> c, IntPredicate removeRowFilter) {
sm.selectedIndices._beginChange();

while (c.next()) {
Expand All @@ -166,26 +167,35 @@ public static <S> void updateSelectedIndices(MultipleSelectionModelBase<S> sm, L

sm.startAtomic();
final List<Integer> removed = c.getRemoved().stream()
.map(TablePositionBase::getRow)
.mapToInt(TablePositionBase::getRow)
.distinct()
.filter(removeRowFilter)
.boxed()
.peek(sm.selectedIndices::clear)
.collect(Collectors.toList());

final int addedSize = (int)c.getAddedSubList().stream()
.map(TablePositionBase::getRow)
.mapToInt(TablePositionBase::getRow)
.distinct()
.peek(sm.selectedIndices::set)
.count();
sm.stopAtomic();

final int to = c.getFrom() + addedSize;
int from = c.getFrom();
if (isCellSelectionEnabled && 0 < from && from < c.getList().size()) {
// convert origin of change of list of tablePositions
// into origin of change of list of rows
int tpRow = c.getList().get(from).getRow();
from = sm.selectedIndices.indexOf(tpRow);
}
final int to = from + addedSize;

if (c.wasReplaced()) {
sm.selectedIndices._nextReplace(c.getFrom(), to, removed);
sm.selectedIndices._nextReplace(from, to, removed);
} else if (c.wasRemoved()) {
sm.selectedIndices._nextRemove(c.getFrom(), removed);
sm.selectedIndices._nextRemove(from, removed);
} else if (c.wasAdded()) {
sm.selectedIndices._nextAdd(c.getFrom(), to);
sm.selectedIndices._nextAdd(from, to);
}
}
c.reset();
Expand All @@ -207,4 +217,33 @@ public static <S> void updateSelectedIndices(MultipleSelectionModelBase<S> sm, L

sm.selectedIndices._endChange();
}

public static <S> int getIndexOfChildWithDescendant(TreeItem<S> parent, TreeItem<S> item) {
if (item == null || parent == null) {
return -1;
}
TreeItem<S> child = item, ancestor = item.getParent();
while (ancestor != null) {
if (ancestor == parent) {
return parent.getChildren().indexOf(child);
}
child = ancestor;
ancestor = child.getParent();
}
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;
}
}
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.IntPredicate;

import com.sun.javafx.logging.PlatformLogger.Level;
import com.sun.javafx.scene.control.Logging;
Expand Down Expand Up @@ -3023,7 +3024,11 @@ private void updateItemCount() {
}

private void fireCustomSelectedCellsListChangeEvent(ListChangeListener.Change<? extends TablePosition<S,?>> c) {
ControlUtils.updateSelectedIndices(this, c);
// Allow removing the row index if cell selection is not enabled or
// if such row doesn't have any selected cells
IntPredicate removeRowFilter = row -> !isCellSelectionEnabled() ||
getSelectedCells().stream().noneMatch(tp -> tp.getRow() == row);
ControlUtils.updateSelectedIndices(this, this.isCellSelectionEnabled(), c, removeRowFilter);

if (isAtomic()) {
return;
Expand Down
Expand Up @@ -62,6 +62,7 @@
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.IntPredicate;

import javafx.application.Platform;
import javafx.beans.DefaultProperty;
Expand Down Expand Up @@ -2609,7 +2610,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 @@ -2628,9 +2629,6 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {
}
}
} else if (e.wasRemoved()) {
// shuffle selection by the number of removed items
shift += treeItem.isExpanded() ? -removedSize : 0;

// the start row is incorrect - it is _not_ the index of the
// TreeItem in which the children were removed from (which is
// what it currently represents). We need to take the 'from'
Expand All @@ -2646,6 +2644,19 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {
final TreeItem<S> selectedItem = getSelectedItem();
final List<? extends TreeItem<S>> removedChildren = e.getChange().getRemoved();

// shuffle selection by the number of removed items
// only if removed items are before the current selection.
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(treeItem)) {
int lastSelectedSiblingIndex = selectedItems.stream()
.map(item -> ControlUtils.getIndexOfChildWithDescendant(treeItem, item))
.max(Comparator.naturalOrder())
.orElse(-1);
// shift only if the last selected sibling index is after the first removed child
if (e.getFrom() <= lastSelectedSiblingIndex || lastSelectedSiblingIndex == -1) {
shift -= removedSize;
}
}

for (int i = 0; i < selectedIndices.size() && !selectedItems.isEmpty(); i++) {
int index = selectedIndices.get(i);
if (index > selectedItems.size()) break;
Expand Down Expand Up @@ -3365,7 +3376,11 @@ private int getRowCount() {
}

private void fireCustomSelectedCellsListChangeEvent(ListChangeListener.Change<? extends TreeTablePosition<S,?>> c) {
ControlUtils.updateSelectedIndices(this, c);
// Allow removing the row index if cell selection is not enabled or
// if such row doesn't have any selected cells
IntPredicate removeRowFilter = row -> !isCellSelectionEnabled() ||
getSelectedCells().stream().noneMatch(tp -> tp.getRow() == row);
ControlUtils.updateSelectedIndices(this, this.isCellSelectionEnabled(), c, removeRowFilter);

if (isAtomic()) {
return;
Expand Down Expand Up @@ -3475,7 +3490,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 @@ -3497,9 +3512,12 @@ private void updateTreeEventListener(TreeItem<S> oldRoot, TreeItem<S> newRoot) {
}
}

if (row <= getFocusedIndex()) {
// shuffle selection by the number of removed items
shift += e.getTreeItem().isExpanded() ? -e.getRemovedSize() : 0;
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(e.getTreeItem())) {
int focusedSiblingRow = ControlUtils.getIndexOfChildWithDescendant(e.getTreeItem(), getFocusedItem());
if (e.getFrom() <= focusedSiblingRow) {
// shuffle selection by the number of removed items
shift -= e.getRemovedSize();
}
}
}
} while (e.getChange() != null && e.getChange().next());
Expand Down
Expand Up @@ -66,6 +66,7 @@
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1397,7 +1398,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 All @@ -1407,9 +1408,6 @@ private void updateTreeEventListener(TreeItem<T> oldRoot, TreeItem<T> newRoot) {
// subsequently commented out due to RT-33894.
startRow = treeView.getRow(e.getChange().getAddedSubList().get(0));
} else if (e.wasRemoved()) {
// shuffle selection by the number of removed items
shift += treeItem.isExpanded() ? -removedSize : 0;

// the start row is incorrect - it is _not_ the index of the
// TreeItem in which the children were removed from (which is
// what it currently represents). We need to take the 'from'
Expand All @@ -1426,6 +1424,19 @@ private void updateTreeEventListener(TreeItem<T> oldRoot, TreeItem<T> newRoot) {
final TreeItem<T> selectedItem = getSelectedItem();
final List<? extends TreeItem<T>> removedChildren = e.getChange().getRemoved();

// shuffle selection by the number of removed items
// only if removed items are before the current selection.
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(treeItem)) {
int lastSelectedSiblingIndex = selectedItems.stream()
.map(item -> ControlUtils.getIndexOfChildWithDescendant(treeItem, item))
.max(Comparator.naturalOrder())
.orElse(-1);
// shift only if the last selected sibling index is after the first removed child
if (e.getFrom() <= lastSelectedSiblingIndex || lastSelectedSiblingIndex == -1) {
shift -= removedSize;
}
}

for (int i = 0; i < selectedIndices1.size() && !selectedItems.isEmpty(); i++) {
int index = selectedIndices1.get(i);
if (index > selectedItems.size()) break;
Expand Down Expand Up @@ -1664,7 +1675,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 @@ -1686,9 +1697,12 @@ private void updateTreeEventListener(TreeItem<T> oldRoot, TreeItem<T> newRoot) {
}
}

if (row <= getFocusedIndex()) {
// shuffle selection by the number of removed items
shift += e.getTreeItem().isExpanded() ? -e.getRemovedSize() : 0;
if (ControlUtils.isTreeItemIncludingAncestorsExpanded(e.getTreeItem())) {
int focusedSiblingRow = ControlUtils.getIndexOfChildWithDescendant(e.getTreeItem(), getFocusedItem());
if (e.getFrom() <= focusedSiblingRow) {
// shuffle selection by the number of removed items
shift -= e.getRemovedSize();
}
}
}
} while (e.getChange() != null && e.getChange().next());
Expand Down
Expand Up @@ -148,12 +148,17 @@ public MenuButtonSkinBase(final C control) {
ControlAcceleratorSupport.addAcceleratorsIntoScene(getSkinnable().getItems(), getSkinnable());
}

List<Mnemonic> mnemonics = new ArrayList<>();
sceneChangeListener = (scene, oldValue, newValue) -> {
if (oldValue != null) {
ControlAcceleratorSupport.removeAcceleratorsFromScene(getSkinnable().getItems(), oldValue);

// We only need to remove the mnemonics from the old scene,
// they will be added to the new one as soon as the popup becomes visible again.
removeMnemonicsFromScene(mnemonics, oldValue);
}

// FIXME: null skinnable should not happen
// FIXME: null skinnable should not happen
if (getSkinnable() != null && getSkinnable().getScene() != null) {
ControlAcceleratorSupport.addAcceleratorsIntoScene(getSkinnable().getItems(), getSkinnable());
}
Expand Down Expand Up @@ -181,7 +186,6 @@ public MenuButtonSkinBase(final C control) {
label.setMnemonicParsing(getSkinnable().isMnemonicParsing());
getSkinnable().requestLayout();
});
List<Mnemonic> mnemonics = new ArrayList<>();
registerChangeListener(popup.showingProperty(), e -> {
if (!popup.isShowing() && getSkinnable().isShowing()) {
// Popup was dismissed. Maybe user clicked outside or typed ESCAPE.
Expand All @@ -201,9 +205,10 @@ public MenuButtonSkinBase(final C control) {
// consumed to prevent them being used elsewhere).
// See JBS-8090026 for more detail.
Scene scene = getSkinnable().getScene();
List<Mnemonic> mnemonicsToRemove = new ArrayList<>(mnemonics);
mnemonics.clear();
Platform.runLater(() -> mnemonicsToRemove.forEach(scene::removeMnemonic));
// JDK-8244234: MenuButton: NPE on removing from scene with open popup
if (scene != null) {
removeMnemonicsFromScene(mnemonics, scene);
}
}
});
}
Expand Down Expand Up @@ -311,6 +316,12 @@ private void hide() {
}
}

private void removeMnemonicsFromScene(List<Mnemonic> mnemonics, Scene scene) {
List<Mnemonic> mnemonicsToRemove = new ArrayList<>(mnemonics);
mnemonics.clear();
Platform.runLater(() -> mnemonicsToRemove.forEach(scene::removeMnemonic));
}

boolean requestFocusOnFirstMenuItem = false;
void requestFocusOnFirstMenuItem() {
this.requestFocusOnFirstMenuItem = true;
Expand Down

0 comments on commit fcf2a46

Please sign in to comment.