1 /*
   2  * Copyright (c) 2010, 2014, 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.skin;
  27 
  28 import com.sun.javafx.scene.control.behavior.BehaviorBase;
  29 import javafx.geometry.Orientation;
  30 import javafx.scene.Node;
  31 import javafx.scene.control.Accordion;
  32 import javafx.scene.control.Button;
  33 import javafx.scene.control.Control;
  34 import javafx.scene.control.ListCell;
  35 import javafx.scene.control.ListView;
  36 
  37 import com.sun.javafx.scene.control.behavior.ListCellBehavior;
  38 
  39 /**
  40  * Default skin implementation for the {@link ListCell} control.
  41  *
  42  * @see ListCell
  43  * @since 9
  44  */
  45 public class ListCellSkin<T> extends CellSkinBase<ListCell<T>> {
  46 
  47     /***************************************************************************
  48      *                                                                         *
  49      * Private fields                                                          *
  50      *                                                                         *
  51      **************************************************************************/
  52 
  53     private double fixedCellSize;
  54     private boolean fixedCellSizeEnabled;
  55     private final BehaviorBase<ListCell<T>> behavior;
  56 
  57 
  58 
  59     /***************************************************************************
  60      *                                                                         *
  61      * Constructors                                                            *
  62      *                                                                         *
  63      **************************************************************************/
  64 
  65     /**
  66      * Creates a new ListCellSkin instance, installing the necessary child
  67      * nodes into the Control {@link Control#getChildren() children} list, as
  68      * well as the necessary input mappings for handling key, mouse, etc events.
  69      *
  70      * @param control The control that this skin should be installed onto.
  71      */
  72     public ListCellSkin(ListCell<T> control) {
  73         super(control);
  74 
  75         // install default input map for the ListCell control
  76         behavior = new ListCellBehavior<>(control);
  77 //        control.setInputMap(behavior.getInputMap());
  78 
  79         this.fixedCellSize = control.getListView().getFixedCellSize();
  80         this.fixedCellSizeEnabled = fixedCellSize > 0;
  81 
  82         registerChangeListener(control.getListView().fixedCellSizeProperty(), e -> {
  83             this.fixedCellSize = getSkinnable().getListView().getFixedCellSize();
  84             this.fixedCellSizeEnabled = fixedCellSize > 0;
  85         });
  86     }
  87 
  88 
  89 
  90     /***************************************************************************
  91      *                                                                         *
  92      * Public API                                                              *
  93      *                                                                         *
  94      **************************************************************************/
  95 
  96     /** {@inheritDoc} */
  97     @Override public void dispose() {
  98         super.dispose();
  99 
 100         if (behavior != null) {
 101             behavior.dispose();
 102         }
 103     }
 104 
 105     /** {@inheritDoc} */
 106     @Override protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
 107         double pref = super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset);
 108         ListView<T> listView = getSkinnable().getListView();
 109         return listView == null ? 0 :
 110             listView.getOrientation() == Orientation.VERTICAL ? pref : Math.max(pref, getCellSize());
 111     }
 112 
 113     /** {@inheritDoc} */
 114     @Override protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
 115         if (fixedCellSizeEnabled) {
 116             return fixedCellSize;
 117         }
 118 
 119         // Added the comparison between the default cell size and the requested
 120         // cell size to prevent the issue identified in RT-19873.
 121         final double cellSize = getCellSize();
 122         final double prefHeight = cellSize == DEFAULT_CELL_SIZE ? super.computePrefHeight(width, topInset, rightInset, bottomInset, leftInset) : cellSize;
 123         return prefHeight;
 124     }
 125 
 126     /** {@inheritDoc} */
 127     @Override protected double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
 128         if (fixedCellSizeEnabled) {
 129             return fixedCellSize;
 130         }
 131 
 132         return super.computeMinHeight(width, topInset, rightInset, bottomInset, leftInset);
 133     }
 134 
 135     /** {@inheritDoc} */
 136     @Override protected double computeMaxHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
 137         if (fixedCellSizeEnabled) {
 138             return fixedCellSize;
 139         }
 140 
 141         return super.computeMaxHeight(width, topInset, rightInset, bottomInset, leftInset);
 142     }
 143 }