< prev index next >

modules/javafx.controls/src/main/java/javafx/scene/control/ControlUtils.java

Print this page
rev 10463 : 8089514: [TableView, TreeView, ListView, TreeTableView] Clicking outside of the edited cell, node, or entry should commit the value
   1 /*
   2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control;
  27 
  28 import com.sun.javafx.scene.control.skin.Utils;
  29 import javafx.collections.ListChangeListener;
  30 import javafx.collections.ObservableList;
  31 import javafx.event.Event;
  32 import javafx.scene.Node;
  33 import javafx.scene.Parent;
  34 import javafx.scene.Scene;
  35 
  36 import java.util.ArrayList;
  37 import java.util.List;


  38 import java.util.stream.Collectors;
  39 
  40 class ControlUtils {
  41     private ControlUtils() { }
  42 
  43     public static void scrollToIndex(final Control control, int index) {
  44         Utils.executeOnceWhenPropertyIsNonNull(control.skinProperty(), (Skin<?> skin) -> {
  45             Event.fireEvent(control, new ScrollToEvent<>(control, control, ScrollToEvent.scrollToTopIndex(), index));
  46         });
  47     }
  48 
  49     public static void scrollToColumn(final Control control, final TableColumnBase<?, ?> column) {
  50         Utils.executeOnceWhenPropertyIsNonNull(control.skinProperty(), (Skin<?> skin) -> {
  51             control.fireEvent(new ScrollToEvent<TableColumnBase<?, ?>>(control, control, ScrollToEvent.scrollToColumn(), column));
  52         });
  53     }
  54 
  55     static void requestFocusOnControlOnlyIfCurrentFocusOwnerIsChild(Control c) {
  56         Scene scene = c.getScene();
















  57         final Node focusOwner = scene == null ? null : scene.getFocusOwner();

  58         if (focusOwner == null) {
  59             c.requestFocus();
  60         } else if (! c.equals(focusOwner)) {


  61             Parent p = focusOwner.getParent();
  62             while (p != null) {
  63                 if (c.equals(p)) {
  64                     c.requestFocus();
  65                     break;
  66                 }
  67                 p = p.getParent();
  68             }
  69         }

  70     }
  71 
  72     static <T> ListChangeListener.Change<T> buildClearAndSelectChange(
  73             ObservableList<T> list, List<T> removed, int retainedRow) {
  74         return new ListChangeListener.Change<T>(list) {
  75             private final int[] EMPTY_PERM = new int[0];
  76 
  77             private final int removedSize = removed.size();
  78 
  79             private final List<T> firstRemovedRange;
  80             private final List<T> secondRemovedRange;
  81 
  82             private boolean invalid = true;
  83             private boolean atFirstRange = true;
  84 
  85             private int from = -1;
  86 
  87             {
  88                 int midIndex = retainedRow >= removedSize ? removedSize :
  89                                retainedRow < 0 ? 0 :


   1 /*
   2  * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.control;
  27 
  28 import com.sun.javafx.scene.control.skin.Utils;
  29 import javafx.collections.ListChangeListener;
  30 import javafx.collections.ObservableList;
  31 import javafx.event.Event;
  32 import javafx.scene.Node;
  33 import javafx.scene.Parent;
  34 import javafx.scene.Scene;
  35 

  36 import java.util.List;
  37 import java.util.Optional;
  38 import java.util.function.Function;
  39 import java.util.stream.Collectors;
  40 
  41 class ControlUtils {
  42     private ControlUtils() { }
  43 
  44     public static void scrollToIndex(final Control control, int index) {
  45         Utils.executeOnceWhenPropertyIsNonNull(control.skinProperty(), (Skin<?> skin) -> {
  46             Event.fireEvent(control, new ScrollToEvent<>(control, control, ScrollToEvent.scrollToTopIndex(), index));
  47         });
  48     }
  49 
  50     public static void scrollToColumn(final Control control, final TableColumnBase<?, ?> column) {
  51         Utils.executeOnceWhenPropertyIsNonNull(control.skinProperty(), (Skin<?> skin) -> {
  52             control.fireEvent(new ScrollToEvent<TableColumnBase<?, ?>>(control, control, ScrollToEvent.scrollToColumn(), column));
  53         });
  54     }
  55 
  56     static void requestFocusOnControlOnlyIfCurrentFocusOwnerIsChild(Control c) {
  57         runOnFocusedNodeIfChildOf(c, matchingNode -> {
  58             matchingNode.requestFocus();
  59             return null;
  60         });
  61     }
  62 
  63     static boolean isFocusOnNodeOrAnyChild(final Node n) {
  64         return runOnFocusedNodeIfChildOf(n, matchingNode -> true).orElse(false);
  65     }
  66 
  67     static <R> Optional<R> runOnFocusedNodeIfChildOf(final Node n, Function<Node, R> f) {
  68         if (n == null) return Optional.empty();
  69         if (n.isFocused()) {
  70             return Optional.ofNullable(f.apply(n));
  71         }
  72 
  73         Scene scene = n.getScene();
  74         final Node focusOwner = scene == null ? null : scene.getFocusOwner();
  75 
  76         if (focusOwner == null) {
  77             return Optional.empty();
  78         } else if (n.equals(focusOwner)) {
  79             return Optional.ofNullable(f.apply(n));
  80         } else {
  81             Parent p = focusOwner.getParent();
  82             while (p != null) {
  83                 if (n.equals(p)) {
  84                     return Optional.ofNullable(f.apply(p));

  85                 }
  86                 p = p.getParent();
  87             }
  88         }
  89         return Optional.empty();
  90     }
  91 
  92     static <T> ListChangeListener.Change<T> buildClearAndSelectChange(
  93             ObservableList<T> list, List<T> removed, int retainedRow) {
  94         return new ListChangeListener.Change<T>(list) {
  95             private final int[] EMPTY_PERM = new int[0];
  96 
  97             private final int removedSize = removed.size();
  98 
  99             private final List<T> firstRemovedRange;
 100             private final List<T> secondRemovedRange;
 101 
 102             private boolean invalid = true;
 103             private boolean atFirstRange = true;
 104 
 105             private int from = -1;
 106 
 107             {
 108                 int midIndex = retainedRow >= removedSize ? removedSize :
 109                                retainedRow < 0 ? 0 :


< prev index next >