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 com.sun.javafx.scene.control.skin;
  27 
  28 import javafx.geometry.Orientation;
  29 import javafx.scene.control.ListCell;
  30 import javafx.scene.control.ListView;
  31 
  32 import com.sun.javafx.scene.control.behavior.ListCellBehavior;
  33 
  34 public class ListCellSkin<T> extends CellSkinBase<ListCell<T>, ListCellBehavior<T>> {
  35 
  36     private double fixedCellSize;
  37     private boolean fixedCellSizeEnabled;
  38 
  39     public ListCellSkin(ListCell<T> control) {
  40         super(control, new ListCellBehavior<T>(control));
  41 
  42         this.fixedCellSize = control.getListView().getFixedCellSize();
  43         this.fixedCellSizeEnabled = fixedCellSize > 0;
  44 
  45         registerChangeListener(control.getListView().fixedCellSizeProperty(), "FIXED_CELL_SIZE");
  46     }
  47 
  48     @Override protected void handleControlPropertyChanged(String p) {
  49         super.handleControlPropertyChanged(p);
  50         if ("FIXED_CELL_SIZE".equals(p)) {
  51             this.fixedCellSize = getSkinnable().getListView().getFixedCellSize();
  52             this.fixedCellSizeEnabled = fixedCellSize > 0;
  53         }
  54     }
  55     
  56     @Override protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
  57         double pref = super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset);
  58         ListView<T> listView = getSkinnable().getListView();
  59         return listView == null ? 0 :
  60             listView.getOrientation() == Orientation.VERTICAL ? pref : Math.max(pref, getCellSize());
  61     }
  62  
  63     @Override protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
  64         if (fixedCellSizeEnabled) {
  65             return fixedCellSize;
  66         }
  67 
  68         // Added the comparison between the default cell size and the requested
  69         // cell size to prevent the issue identified in RT-19873.
  70         final double cellSize = getCellSize();
  71         final double prefHeight = cellSize == DEFAULT_CELL_SIZE ? super.computePrefHeight(width, topInset, rightInset, bottomInset, leftInset) : cellSize;
  72         return prefHeight;
  73     }
  74 
  75     @Override protected double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
  76         if (fixedCellSizeEnabled) {
  77             return fixedCellSize;
  78         }
  79 
  80         return super.computeMinHeight(width, topInset, rightInset, bottomInset, leftInset);
  81     }
  82 
  83     @Override protected double computeMaxHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
  84         if (fixedCellSizeEnabled) {
  85             return fixedCellSize;
  86         }
  87 
  88         return super.computeMaxHeight(width, topInset, rightInset, bottomInset, leftInset);
  89     }
  90 }