changeset 6886:c4d8457b7064

RT-36855: [TableView, TreeTableView] Auto sizing of TableColumn should take into account header text / graphic
author jgiles
date Tue, 29 Apr 2014 13:27:17 +1200
parents 143503528295
children d06e9b3e8351
files modules/controls/src/main/java/com/sun/javafx/scene/control/skin/NestedTableColumnHeader.java modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TableColumnHeader.java modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TableViewSkin.java modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TreeTableViewSkin.java
diffstat 4 files changed, 40 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/NestedTableColumnHeader.java	Tue Apr 29 12:13:17 2014 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/NestedTableColumnHeader.java	Tue Apr 29 13:27:17 2014 +1200
@@ -145,9 +145,7 @@
             if (me.getClickCount() == 2 && me.isPrimaryButtonDown()) {
                 // the user wants to resize the column such that its
                 // width is equal to the widest element in the column
-                if (column.isResizable()) {
-                    header.getTableViewSkin().resizeColumnToFitContent(column, -1);
-                }
+                header.getTableViewSkin().resizeColumnToFitContent(column, -1);
             } else {
                 // rather than refer to the rect variable, we just grab
                 // it from the source to prevent a small memory leak.
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TableColumnHeader.java	Tue Apr 29 12:13:17 2014 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TableColumnHeader.java	Tue Apr 29 13:27:17 2014 +1200
@@ -99,7 +99,7 @@
     private NestedTableColumnHeader parentHeader;
 
     // work out where this column currently is within its parent
-    private Label label;
+    Label label;
 
     // sort order
     int sortPos = -1;
@@ -509,6 +509,13 @@
         label.setGraphic(column.getGraphic());
         label.setVisible(column.isVisible());
 
+        label.fontProperty().addListener((o, old, newValue) -> {
+            // The font has changed (probably due to CSS being applied), so we
+            // need to re-run the column resizing algorithm to ensure columns
+            // fit nicely based on their content and their header
+            getTableViewSkin().resizeColumnToFitContent(column, 30);
+        });
+
         // ---- container for the sort arrow (which is not supported on embedded
         // platforms)
         if (isSortingEnabled()) {
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TableViewSkin.java	Tue Apr 29 12:13:17 2014 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TableViewSkin.java	Tue Apr 29 13:27:17 2014 +1200
@@ -48,6 +48,7 @@
 import javafx.scene.control.TableView.TableViewSelectionModel;
 import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.Region;
+import javafx.scene.text.Font;
 import javafx.util.Callback;
 
 import com.sun.javafx.scene.control.behavior.TableViewBehavior;
@@ -204,14 +205,16 @@
      * rows is large.
      */
     @Override protected void resizeColumnToFitContent(TableColumn<T, ?> tc, int maxRows) {
-        final TableColumn<T, ?> col = tc;
+        if (!tc.isResizable()) return;
+
+//        final TableColumn<T, ?> col = tc;
         List<?> items = itemsProperty().get();
         if (items == null || items.isEmpty()) return;
     
-        Callback/*<TableColumn<T, ?>, TableCell<T,?>>*/ cellFactory = col.getCellFactory();
+        Callback/*<TableColumn<T, ?>, TableCell<T,?>>*/ cellFactory = tc.getCellFactory();
         if (cellFactory == null) return;
     
-        TableCell<T,?> cell = (TableCell<T, ?>) cellFactory.call(col);
+        TableCell<T,?> cell = (TableCell<T, ?>) cellFactory.call(tc);
         if (cell == null) return;
         
         // set this property to tell the TableCell we want to know its actual
@@ -229,7 +232,7 @@
         int rows = maxRows == -1 ? items.size() : Math.min(items.size(), maxRows);
         double maxWidth = 0;
         for (int row = 0; row < rows; row++) {
-            cell.updateTableColumn(col);
+            cell.updateTableColumn(tc);
             cell.updateTableView(tableView);
             cell.updateIndex(row);
             
@@ -243,14 +246,23 @@
 
         // dispose of the cell to prevent it retaining listeners (see RT-31015)
         cell.updateIndex(-1);
+
+        // RT-36855 - take into account the column header text / graphic widths.
+        // Magic 10 is to allow for sort arrow to appear without text truncation.
+        TableColumnHeader header = getTableHeaderRow().getColumnHeaderFor(tc);
+        double headerTextWidth = Utils.computeTextWidth(header.label.getFont(), tc.getText(), -1);
+        Node graphic = header.label.getGraphic();
+        double headerGraphicWidth = graphic == null ? 0 : graphic.prefWidth(-1) + header.label.getGraphicTextGap();
+        double headerWidth = headerTextWidth + headerGraphicWidth + 10 + header.snappedLeftInset() + header.snappedRightInset();
+        maxWidth = Math.max(maxWidth, headerWidth);
         
         // RT-23486
-        double widthMax = maxWidth + padding;
+        maxWidth += padding;
         if(tableView.getColumnResizePolicy() == TableView.CONSTRAINED_RESIZE_POLICY) {
-             widthMax = Math.max(widthMax, col.getWidth());
+            maxWidth = Math.max(maxWidth, tc.getWidth());
         }
 
-        col.impl_setWidth(widthMax); 
+        tc.impl_setWidth(maxWidth);
     }
     
     /** {@inheritDoc} */
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TreeTableViewSkin.java	Tue Apr 29 12:13:17 2014 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/TreeTableViewSkin.java	Tue Apr 29 13:27:17 2014 +1200
@@ -327,14 +327,23 @@
 
         // dispose of the cell to prevent it retaining listeners (see RT-31015)
         cell.updateIndex(-1);
+
+        // RT-36855 - take into account the column header text / graphic widths.
+        // Magic 10 is to allow for sort arrow to appear without text truncation.
+        TableColumnHeader header = getTableHeaderRow().getColumnHeaderFor(tc);
+        double headerTextWidth = Utils.computeTextWidth(header.label.getFont(), tc.getText(), -1);
+        Node graphic = header.label.getGraphic();
+        double headerGraphicWidth = graphic == null ? 0 : graphic.prefWidth(-1) + header.label.getGraphicTextGap();
+        double headerWidth = headerTextWidth + headerGraphicWidth + 10 + header.snappedLeftInset() + header.snappedRightInset();
+        maxWidth = Math.max(maxWidth, headerWidth);
         
         // RT-23486
-        double widthMax = maxWidth + padding;
+        maxWidth += padding;
         if(treeTableView.getColumnResizePolicy() == TreeTableView.CONSTRAINED_RESIZE_POLICY) {
-             widthMax = Math.max(widthMax, col.getWidth());
+            maxWidth = Math.max(maxWidth, col.getWidth());
         }
 
-        col.impl_setWidth(widthMax); 
+        col.impl_setWidth(maxWidth);
     }
     
     /** {@inheritDoc} */