< prev index next >

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

Print this page
rev 10463 : 8089514: [TableView, TreeView, ListView, TreeTableView] Clicking outside of the edited cell, node, or entry should commit the value


  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 javafx.css.converter.SizeConverter;
  29 import com.sun.javafx.scene.control.Properties;
  30 import com.sun.javafx.scene.control.behavior.TreeCellBehavior;
  31 import javafx.scene.control.skin.TreeViewSkin;
  32 
  33 import javafx.application.Platform;
  34 import javafx.beans.DefaultProperty;


  35 import javafx.beans.property.BooleanProperty;
  36 import javafx.beans.property.DoubleProperty;
  37 import javafx.beans.property.ObjectProperty;
  38 import javafx.beans.property.ObjectPropertyBase;
  39 import javafx.beans.property.ReadOnlyIntegerProperty;
  40 import javafx.beans.property.ReadOnlyIntegerWrapper;
  41 import javafx.beans.property.ReadOnlyObjectProperty;
  42 import javafx.beans.property.ReadOnlyObjectWrapper;
  43 import javafx.beans.property.SimpleBooleanProperty;
  44 import javafx.beans.property.SimpleObjectProperty;
  45 import javafx.beans.value.ChangeListener;
  46 import javafx.beans.value.WeakChangeListener;
  47 import javafx.beans.value.WritableValue;
  48 import javafx.collections.ListChangeListener;
  49 import javafx.css.CssMetaData;
  50 import javafx.css.Styleable;
  51 import javafx.css.StyleableDoubleProperty;
  52 import javafx.css.StyleableProperty;
  53 import javafx.event.Event;
  54 import javafx.event.EventHandler;


 319     /**
 320      * Creates a TreeView with the provided root node.
 321      *
 322      * <p>Refer to the {@link TreeView} class documentation for details on the
 323      * default state of other properties.
 324      *
 325      * @param root The node to be the root in this TreeView.
 326      */
 327     public TreeView(TreeItem<T> root) {
 328         getStyleClass().setAll(DEFAULT_STYLE_CLASS);
 329         setAccessibleRole(AccessibleRole.TREE_VIEW);
 330 
 331         setRoot(root);
 332         updateExpandedItemCount(root);
 333 
 334         // install default selection and focus models - it's unlikely this will be changed
 335         // by many users.
 336         MultipleSelectionModel<TreeItem<T>> sm = new TreeViewBitSetSelectionModel<T>(this);
 337         setSelectionModel(sm);
 338         setFocusModel(new TreeViewFocusModel<T>(this));









 339     }
 340 
 341 
 342 
 343     /***************************************************************************
 344      *                                                                         *
 345      * Instance Variables                                                      *
 346      *                                                                         *
 347      **************************************************************************/
 348 
 349     // used in the tree item modification event listener. Used by the
 350     // layoutChildren method to determine whether the tree item count should
 351     // be recalculated.
 352     private boolean expandedItemCountDirty = true;
 353 
 354     // Used in the getTreeItem(int row) method to act as a cache.
 355     // See RT-26716 for the justification and performance gains.
 356     private Map<Integer, SoftReference<TreeItem<T>>> treeItemCacheMap = new HashMap<>();
 357 
 358 


 367     // if necessary
 368     private final EventHandler<TreeModificationEvent<T>> rootEvent = e -> {
 369         // this forces layoutChildren at the next pulse, and therefore
 370         // updates the item count if necessary
 371         EventType<?> eventType = e.getEventType();
 372         boolean match = false;
 373         while (eventType != null) {
 374             if (eventType.equals(TreeItem.<T>expandedItemCountChangeEvent())) {
 375                 match = true;
 376                 break;
 377             }
 378             eventType = eventType.getSuperType();
 379         }
 380 
 381         if (match) {
 382             expandedItemCountDirty = true;
 383             requestLayout();
 384         }
 385     };
 386 







 387     private WeakEventHandler<TreeModificationEvent<T>> weakRootEventListener;
 388 
 389 
 390 
 391     /***************************************************************************
 392      *                                                                         *
 393      * Properties                                                              *
 394      *                                                                         *
 395      **************************************************************************/
 396 
 397 
 398     // --- Cell Factory
 399     private ObjectProperty<Callback<TreeView<T>, TreeCell<T>>> cellFactory;
 400 
 401     /**
 402      * Sets the cell factory that will be used for creating TreeCells,
 403      * which are used to represent items in the
 404      * TreeView. The factory works identically to the cellFactory in ListView
 405      * and other complex composite controls. It is called to create a new
 406      * TreeCell only when the system has determined that it doesn't have enough




  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 javafx.css.converter.SizeConverter;
  29 import com.sun.javafx.scene.control.Properties;
  30 import com.sun.javafx.scene.control.behavior.TreeCellBehavior;
  31 import javafx.scene.control.skin.TreeViewSkin;
  32 
  33 import javafx.application.Platform;
  34 import javafx.beans.DefaultProperty;
  35 import javafx.beans.InvalidationListener;
  36 import javafx.beans.WeakInvalidationListener;
  37 import javafx.beans.property.BooleanProperty;
  38 import javafx.beans.property.DoubleProperty;
  39 import javafx.beans.property.ObjectProperty;
  40 import javafx.beans.property.ObjectPropertyBase;
  41 import javafx.beans.property.ReadOnlyIntegerProperty;
  42 import javafx.beans.property.ReadOnlyIntegerWrapper;
  43 import javafx.beans.property.ReadOnlyObjectProperty;
  44 import javafx.beans.property.ReadOnlyObjectWrapper;
  45 import javafx.beans.property.SimpleBooleanProperty;
  46 import javafx.beans.property.SimpleObjectProperty;
  47 import javafx.beans.value.ChangeListener;
  48 import javafx.beans.value.WeakChangeListener;
  49 import javafx.beans.value.WritableValue;
  50 import javafx.collections.ListChangeListener;
  51 import javafx.css.CssMetaData;
  52 import javafx.css.Styleable;
  53 import javafx.css.StyleableDoubleProperty;
  54 import javafx.css.StyleableProperty;
  55 import javafx.event.Event;
  56 import javafx.event.EventHandler;


 321     /**
 322      * Creates a TreeView with the provided root node.
 323      *
 324      * <p>Refer to the {@link TreeView} class documentation for details on the
 325      * default state of other properties.
 326      *
 327      * @param root The node to be the root in this TreeView.
 328      */
 329     public TreeView(TreeItem<T> root) {
 330         getStyleClass().setAll(DEFAULT_STYLE_CLASS);
 331         setAccessibleRole(AccessibleRole.TREE_VIEW);
 332 
 333         setRoot(root);
 334         updateExpandedItemCount(root);
 335 
 336         // install default selection and focus models - it's unlikely this will be changed
 337         // by many users.
 338         MultipleSelectionModel<TreeItem<T>> sm = new TreeViewBitSetSelectionModel<T>(this);
 339         setSelectionModel(sm);
 340         setFocusModel(new TreeViewFocusModel<T>(this));
 341 
 342         sceneProperty().addListener((o, oldScene, newScene) -> {
 343             if (oldScene != null) {
 344                 oldScene.focusOwnerProperty().removeListener(weakFocusOwnerListener);
 345             }
 346             if (newScene != null) {
 347                 newScene.focusOwnerProperty().addListener(weakFocusOwnerListener);
 348             }
 349         });
 350     }
 351 
 352 
 353 
 354     /***************************************************************************
 355      *                                                                         *
 356      * Instance Variables                                                      *
 357      *                                                                         *
 358      **************************************************************************/
 359 
 360     // used in the tree item modification event listener. Used by the
 361     // layoutChildren method to determine whether the tree item count should
 362     // be recalculated.
 363     private boolean expandedItemCountDirty = true;
 364 
 365     // Used in the getTreeItem(int row) method to act as a cache.
 366     // See RT-26716 for the justification and performance gains.
 367     private Map<Integer, SoftReference<TreeItem<T>>> treeItemCacheMap = new HashMap<>();
 368 
 369 


 378     // if necessary
 379     private final EventHandler<TreeModificationEvent<T>> rootEvent = e -> {
 380         // this forces layoutChildren at the next pulse, and therefore
 381         // updates the item count if necessary
 382         EventType<?> eventType = e.getEventType();
 383         boolean match = false;
 384         while (eventType != null) {
 385             if (eventType.equals(TreeItem.<T>expandedItemCountChangeEvent())) {
 386                 match = true;
 387                 break;
 388             }
 389             eventType = eventType.getSuperType();
 390         }
 391 
 392         if (match) {
 393             expandedItemCountDirty = true;
 394             requestLayout();
 395         }
 396     };
 397 
 398     private InvalidationListener focusOwnerListener = o -> {
 399         if (!ControlUtils.isFocusOnNodeOrAnyChild(this)) {
 400             edit(null);
 401         }
 402     };
 403     private WeakInvalidationListener weakFocusOwnerListener = new WeakInvalidationListener(focusOwnerListener);
 404 
 405     private WeakEventHandler<TreeModificationEvent<T>> weakRootEventListener;
 406 
 407 
 408 
 409     /***************************************************************************
 410      *                                                                         *
 411      * Properties                                                              *
 412      *                                                                         *
 413      **************************************************************************/
 414 
 415 
 416     // --- Cell Factory
 417     private ObjectProperty<Callback<TreeView<T>, TreeCell<T>>> cellFactory;
 418 
 419     /**
 420      * Sets the cell factory that will be used for creating TreeCells,
 421      * which are used to represent items in the
 422      * TreeView. The factory works identically to the cellFactory in ListView
 423      * and other complex composite controls. It is called to create a new
 424      * TreeCell only when the system has determined that it doesn't have enough


< prev index next >