changeset 10831:da1e2198d2f5

8193495: TabPane does not update correctly tab positions in the header area after a quick remove and add operations Reviewed-by: kcr, aghaisas
author arapte
date Mon, 12 Feb 2018 15:52:01 +0530
parents 4f433399edbd
children 31c320bdc3a9
files modules/javafx.controls/src/main/java/javafx/scene/control/skin/TabPaneSkin.java modules/javafx.controls/src/test/java/test/javafx/scene/control/TabPaneTest.java
diffstat 2 files changed, 42 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.controls/src/main/java/javafx/scene/control/skin/TabPaneSkin.java	Mon Feb 12 11:54:06 2018 +0530
+++ b/modules/javafx.controls/src/main/java/javafx/scene/control/skin/TabPaneSkin.java	Mon Feb 12 15:52:01 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -826,36 +826,13 @@
                     if (tabsFit()) {
                         setScrollOffset(0.0);
                     } else {
-                        if (!removeTab.isEmpty()) {
-                            double offset = 0;
-                            double w = tabHeaderArea.getWidth() - snapSize(controlButtons.prefWidth(-1)) - firstTabIndent() - SPACER;
-                            Iterator<Node> i = getChildren().iterator();
-                            while (i.hasNext()) {
-                                TabHeaderSkin tabHeader = (TabHeaderSkin)i.next();
-                                double tabHeaderPrefWidth = snapSize(tabHeader.prefWidth(-1));
-                                if (removeTab.contains(tabHeader)) {
-                                    if (offset < w) {
-                                        isSelectingTab = true;
-                                    }
-                                    i.remove();
-                                    removeTab.remove(tabHeader);
-                                    if (removeTab.isEmpty()) {
-                                        break;
-                                    }
-                                }
-                                offset += tabHeaderPrefWidth;
-                            }
-//                        } else {
-//                            isSelectingTab = true;
+                        if (isSelectingTab) {
+                            ensureSelectedTabIsVisible();
+                        } else {
+                            validateScrollOffset();
                         }
                     }
-
-                    if (isSelectingTab) {
-                        ensureSelectedTabIsVisible();
-                        isSelectingTab = false;
-                    } else {
-                        validateScrollOffset();
-                    }
+                    isSelectingTab = false;
 
                     Side tabPosition = getSkinnable().getSide();
                     double tabBackgroundHeight = snapSize(prefHeight(-1));
@@ -990,18 +967,10 @@
             headersRegion.getChildren().add(addToIndex, tabHeaderSkin);
         }
 
-        private List<TabHeaderSkin> removeTab = new ArrayList<>();
         private void removeTab(Tab tab) {
             TabHeaderSkin tabHeaderSkin = getTabHeaderSkin(tab);
             if (tabHeaderSkin != null) {
-                if (tabsFit()) {
-                    headersRegion.getChildren().remove(tabHeaderSkin);
-                } else {
-                    // The tab will be removed during layout because
-                    // we need its width to compute the scroll offset.
-                    removeTab.add(tabHeaderSkin);
-                    tabHeaderSkin.removeListeners(tab);
-                }
+                headersRegion.getChildren().remove(tabHeaderSkin);
             }
         }
 
--- a/modules/javafx.controls/src/test/java/test/javafx/scene/control/TabPaneTest.java	Mon Feb 12 11:54:06 2018 +0530
+++ b/modules/javafx.controls/src/test/java/test/javafx/scene/control/TabPaneTest.java	Mon Feb 12 15:52:01 2018 +0530
@@ -1096,6 +1096,41 @@
         tabPane.getTabs().remove(t2);
     }
 
+    // Test for JDK-8193495
+    @Test public void testQuickRemoveAddTab() {
+        int tabHeaderMinWidth = 200;
+        int tabHeaderMinHeight = 50;
+        tabPane.setMaxSize(400, 200);
+        tabPane.setTabMinWidth(tabHeaderMinWidth);
+        tabPane.setTabMinHeight(tabHeaderMinHeight);
+        tabPane.getTabs().addAll(tab1, tab2, tab3);
+        tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
+
+        root.getChildren().add(tabPane);
+        show();
+        tabPane.requestFocus();
+        tk.firePulse();
+        assertTrue(tabPane.isFocused());
+
+        tabPane.getTabs().add(1, tabPane.getTabs().remove(0));
+        tk.firePulse();
+        tabPane.getSelectionModel().select(tab1);
+        tk.firePulse();
+
+        double xval = (tabPane.localToScene(tabPane.getLayoutBounds())).getMinX();
+        double yval = (tabPane.localToScene(tabPane.getLayoutBounds())).getMinY();
+
+        SceneHelper.processMouseEvent(scene,
+            MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval + 19, yval + 17));
+        tk.firePulse();
+        SceneHelper.processMouseEvent(scene,
+            MouseEventGenerator.generateMouseEvent(MouseEvent.MOUSE_PRESSED, xval + 19, yval + 17));
+        tk.firePulse();
+
+        assertEquals("Tabpane should have 3 tabs.", 3, tabPane.getTabs().size());
+        assertEquals("tab2 should be at index 0.", tab2, tabPane.getSelectionModel().getSelectedItem());
+    }
+
     // Test for JDK-8154039
     WeakReference<Tab> weakTab;
     @Test public void testSelectNonChildTab() {