1 /*
   2  * Copyright (c) 2012, 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 com.sun.javafx.scene.control.behavior.TreeTableCellBehavior;
  29 import java.util.Map;
  30 import javafx.beans.property.BooleanProperty;
  31 import javafx.beans.property.ReadOnlyDoubleProperty;
  32 import javafx.scene.Node;
  33 import javafx.scene.control.Control;
  34 import javafx.scene.control.TreeItem;
  35 import javafx.scene.control.TreeTableCell;
  36 import javafx.scene.control.TreeTableColumn;
  37 import javafx.scene.control.TreeTableRow;
  38 import javafx.scene.control.TreeTableView;
  39 
  40 /**
  41  */
  42 public class TreeTableCellSkin<S,T> extends TableCellSkinBase<TreeTableCell<S,T>, TreeTableCellBehavior<S,T>> {
  43     
  44     private final TreeTableCell<S,T> treeTableCell;
  45     private final TreeTableColumn<S,T> tableColumn;
  46     
  47     public TreeTableCellSkin(TreeTableCell<S,T> treeTableCell) {
  48         super(treeTableCell, new TreeTableCellBehavior<S,T>(treeTableCell));
  49         
  50         this.treeTableCell = treeTableCell;
  51         this.tableColumn = treeTableCell.getTableColumn();
  52         
  53         super.init(treeTableCell);
  54     }
  55 
  56     @Override protected BooleanProperty columnVisibleProperty() {
  57         return tableColumn.visibleProperty();
  58     }
  59 
  60     @Override protected ReadOnlyDoubleProperty columnWidthProperty() {
  61         return tableColumn.widthProperty();
  62     }
  63 
  64     @Override protected double leftLabelPadding() {
  65         double leftPadding = super.leftLabelPadding();
  66         
  67         // RT-27167: we must take into account the disclosure node and the
  68         // indentation (which is not taken into account by the LabeledSkinBase.
  69         final double height = getCellSize();
  70 
  71         TreeTableCell<S,T> cell = getSkinnable();
  72 
  73         TreeTableColumn<S,T> tableColumn = cell.getTableColumn();
  74         if (tableColumn == null) return leftPadding;
  75 
  76         // check if this column is the TreeTableView treeColumn (i.e. the 
  77         // column showing the disclosure node and graphic).
  78         TreeTableView<S> treeTable = cell.getTreeTableView();
  79         if (treeTable == null) return leftPadding;
  80 
  81         int columnIndex = treeTable.getVisibleLeafIndex(tableColumn);
  82 
  83         TreeTableColumn<S,?> treeColumn = treeTable.getTreeColumn();
  84         if ((treeColumn == null && columnIndex != 0) || (treeColumn != null && ! tableColumn.equals(treeColumn))) {
  85             return leftPadding;
  86         }
  87 
  88         TreeTableRow<S> treeTableRow = cell.getTreeTableRow();
  89         if (treeTableRow == null) return leftPadding;
  90 
  91         TreeItem<S> treeItem = treeTableRow.getTreeItem();
  92         if (treeItem == null) return leftPadding;
  93         
  94         int nodeLevel = treeTable.getTreeItemLevel(treeItem);
  95         if (! treeTable.isShowRoot()) nodeLevel--;
  96 
  97         double indentPerLevel = 10;
  98         if (treeTableRow.getSkin() instanceof TreeTableRowSkin) {
  99             indentPerLevel = ((TreeTableRowSkin<?>)treeTableRow.getSkin()).getIndentationPerLevel();
 100         }
 101         leftPadding += nodeLevel * indentPerLevel;
 102 
 103         // add in the width of the disclosure node, if one exists
 104         Map<Control, Double> mdwp = TableRowSkinBase.maxDisclosureWidthMap;
 105         leftPadding += mdwp.containsKey(treeTable) ? mdwp.get(treeTable) : 0;
 106 
 107         // adding in the width of the graphic on the tree item
 108         Node graphic = treeItem.getGraphic();
 109         leftPadding += graphic == null ? 0 : graphic.prefWidth(height);
 110         
 111         return leftPadding;
 112     }
 113 
 114     @Override protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) {
 115         if (isDeferToParentForPrefWidth) {
 116             // RT-27167: we must take into account the disclosure node and the
 117             // indentation (which is not taken into account by the LabeledSkinBase.
 118             return super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset);
 119         }
 120         return columnWidthProperty().get();
 121     }
 122 }