|
30 | 30 | import javafx.collections.ObservableList; |
31 | 31 | import javafx.event.Event; |
32 | 32 | import javafx.scene.Node; |
| 33 | +import javafx.scene.control.ContentDisplay; |
| 34 | +import javafx.scene.control.Label; |
33 | 35 | import javafx.scene.control.Skin; |
34 | 36 | import javafx.scene.control.TableColumn; |
| 37 | +import javafx.scene.control.TableColumnBase; |
35 | 38 | import javafx.scene.control.TableRow; |
36 | 39 | import javafx.scene.control.TableView; |
37 | 40 | import javafx.scene.control.cell.PropertyValueFactory; |
|
40 | 43 | import javafx.scene.input.MouseEvent; |
41 | 44 | import javafx.scene.layout.HBox; |
42 | 45 |
|
| 46 | +import javafx.scene.text.Text; |
43 | 47 | import org.junit.Before; |
44 | 48 | import org.junit.After; |
45 | 49 | import org.junit.Test; |
@@ -281,6 +285,90 @@ public void test_resizeColumnToFitContentCustomRowSkin() { |
281 | 285 | assertTrue(width > 0); |
282 | 286 | } |
283 | 287 |
|
| 288 | + /** |
| 289 | + * We expect that the css of the label is processed after the resizing took place, |
| 290 | + * since it is needed to correctly measure the size. |
| 291 | + */ |
| 292 | + @Test |
| 293 | + public void testResizeColumnToFitContentCssIsApplied() { |
| 294 | + Label label = TableColumnHeaderShim.getLabel(firstColumnHeader); |
| 295 | + label.setStyle("-fx-font-size: 24px;"); |
| 296 | + firstColumnHeader.getTableColumn().setText("longlonglonglong"); |
| 297 | + |
| 298 | + assertEquals(12, label.getFont().getSize(), 0); |
| 299 | + |
| 300 | + TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1); |
| 301 | + |
| 302 | + assertEquals(24, label.getFont().getSize(), 0); |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * A table column with a graphic {@link Text} should be bigger than without. |
| 307 | + */ |
| 308 | + @Test |
| 309 | + public void testResizeColumnToFitContentWithGraphicText() { |
| 310 | + TableColumnBase<?, ?> tableColumn = firstColumnHeader.getTableColumn(); |
| 311 | + |
| 312 | + tableColumn.setText("longlonglonglonglonglonglonglong"); |
| 313 | + tableColumn.setGraphic(new Text("longlonglonglong")); |
| 314 | + TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1); |
| 315 | + |
| 316 | + double widthWithGraphic = tableColumn.getWidth(); |
| 317 | + |
| 318 | + tableColumn.setGraphic(null); |
| 319 | + TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1); |
| 320 | + |
| 321 | + double width = tableColumn.getWidth(); |
| 322 | + |
| 323 | + assertTrue(widthWithGraphic > width); |
| 324 | + } |
| 325 | + |
| 326 | + /** |
| 327 | + * A table column with a graphic {@link Label} should be bigger than without. |
| 328 | + */ |
| 329 | + @Test |
| 330 | + public void testResizeColumnToFitContentWithGraphicLabel() { |
| 331 | + TableColumnBase<?, ?> tableColumn = firstColumnHeader.getTableColumn(); |
| 332 | + |
| 333 | + tableColumn.setText("longlonglonglonglonglonglonglong"); |
| 334 | + tableColumn.setGraphic(new Label("longlonglonglong")); |
| 335 | + TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1); |
| 336 | + |
| 337 | + double widthWithGraphic = tableColumn.getWidth(); |
| 338 | + |
| 339 | + tableColumn.setGraphic(null); |
| 340 | + TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1); |
| 341 | + |
| 342 | + double width = tableColumn.getWidth(); |
| 343 | + |
| 344 | + assertTrue(widthWithGraphic > width); |
| 345 | + } |
| 346 | + |
| 347 | + /** |
| 348 | + * The content display should also be taken into consideration when measuring the width. |
| 349 | + * See also: <a href="https://bugs.openjdk.org/browse/JDK-8186188">JDK-8186188</a> |
| 350 | + */ |
| 351 | + @Test |
| 352 | + public void testResizeColumnToFitContentWithGraphicAlignment() { |
| 353 | + TableColumnBase<?, ?> tableColumn = firstColumnHeader.getTableColumn(); |
| 354 | + |
| 355 | + tableColumn.setText("longlonglonglonglonglonglonglong"); |
| 356 | + tableColumn.setGraphic(new Text("longlonglonglong")); |
| 357 | + |
| 358 | + Label label = TableColumnHeaderShim.getLabel(firstColumnHeader); |
| 359 | + |
| 360 | + TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1); |
| 361 | + |
| 362 | + double widthWithGraphic = tableColumn.getWidth(); |
| 363 | + |
| 364 | + label.setContentDisplay(ContentDisplay.BOTTOM); |
| 365 | + TableColumnHeaderShim.resizeColumnToFitContent(firstColumnHeader, -1); |
| 366 | + |
| 367 | + double width = tableColumn.getWidth(); |
| 368 | + |
| 369 | + assertTrue(widthWithGraphic > width); |
| 370 | + } |
| 371 | + |
284 | 372 | private TableRow<Person> createCustomRow(TableView<Person> tableView) { |
285 | 373 | TableRow<Person> row = new TableRow<>() { |
286 | 374 | protected Skin<?> createDefaultSkin() { |
|
0 commit comments