changeset 6936:822a99443a4e

RT-36908: [TabPane] If first tab is disabled, its content is still shown at startup
author jgiles
date Wed, 30 Apr 2014 11:01:10 +1200
parents 26306d581529
children 5c12dcee4f69
files modules/controls/src/main/java/javafx/scene/control/TabPane.java modules/controls/src/test/java/javafx/scene/control/TabPaneTest.java
diffstat 2 files changed, 61 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/javafx/scene/control/TabPane.java	Tue Apr 29 17:09:17 2014 -0700
+++ b/modules/controls/src/main/java/javafx/scene/control/TabPane.java	Wed Apr 30 11:01:10 2014 +1200
@@ -643,41 +643,10 @@
                             if (tab.isSelected()) {
                                 tab.setSelected(false);
                                 final int tabIndex = c.getFrom();
-                                final int tabCount = tabPane.getTabs().size();
 
                                 // we always try to select the nearest, non-disabled
                                 // tab from the position of the closed tab.
-                                int i = 1;
-                                while (true) {
-                                    // look leftwards
-                                    int downPos = tabIndex - i;
-                                    if (downPos >= 0) {
-                                        Tab _tab = getModelItem(downPos);
-                                        if (_tab != null && ! _tab.isDisable()) {
-                                            select(_tab);
-                                            break;
-                                        }
-                                    }
-
-                                    // look rightwards. We subtract one as we need
-                                    // to take into account that a tab has been removed
-                                    // and if we don't do this we'll miss the tab
-                                    // to the right of the tab (as it has moved into
-                                    // the removed tabs position).
-                                    int upPos = tabIndex + i - 1;
-                                    if (upPos < tabCount) {
-                                        Tab _tab = getModelItem(upPos);
-                                        if (_tab != null && ! _tab.isDisable()) {
-                                            select(_tab);
-                                            break;
-                                        }
-                                    }
-
-                                    if (downPos < 0 && upPos >= tabCount) {
-                                        break;
-                                    }
-                                    i++;
-                                }
+                                findNearestAvailableTab(tabIndex, true);
                             }
                         }
                     }
@@ -690,7 +659,9 @@
                     }
                 }
                 if (getSelectedIndex() == -1 && getSelectedItem() == null && tabPane.getTabs().size() > 0) {
-                    selectFirst();
+                    // we go looking for the first non-disabled tab, as opposed to
+                    // just selecting the first tab (fix for RT-36908)
+                    findNearestAvailableTab(0, true);
                 } else if (tabPane.getTabs().isEmpty()) {
                     clearSelection();
                 }
@@ -754,6 +725,50 @@
             final ObservableList<Tab> items = tabPane.getTabs();
             return items == null ? 0 : items.size();
         }
+
+        private Tab findNearestAvailableTab(int tabIndex, boolean doSelect) {
+            // we always try to select the nearest, non-disabled
+            // tab from the position of the closed tab.
+            final int tabCount = getItemCount();
+            int i = 1;
+            Tab bestTab = null;
+            while (true) {
+                // look leftwards
+                int downPos = tabIndex - i;
+                if (downPos >= 0) {
+                    Tab _tab = getModelItem(downPos);
+                    if (_tab != null && ! _tab.isDisable()) {
+                        bestTab = _tab;
+                        break;
+                    }
+                }
+
+                // look rightwards. We subtract one as we need
+                // to take into account that a tab has been removed
+                // and if we don't do this we'll miss the tab
+                // to the right of the tab (as it has moved into
+                // the removed tabs position).
+                int upPos = tabIndex + i - 1;
+                if (upPos < tabCount) {
+                    Tab _tab = getModelItem(upPos);
+                    if (_tab != null && ! _tab.isDisable()) {
+                        bestTab = _tab;
+                        break;
+                    }
+                }
+
+                if (downPos < 0 && upPos >= tabCount) {
+                    break;
+                }
+                i++;
+            }
+
+            if (doSelect && bestTab != null) {
+                select(bestTab);
+            }
+
+            return bestTab;
+        }
     }
 
     /**
--- a/modules/controls/src/test/java/javafx/scene/control/TabPaneTest.java	Tue Apr 29 17:09:17 2014 -0700
+++ b/modules/controls/src/test/java/javafx/scene/control/TabPaneTest.java	Wed Apr 30 11:01:10 2014 +1200
@@ -951,4 +951,17 @@
 
         sl.dispose();
     }
+
+    @Test public void test_rt_36908() {
+        TabPane pane = new TabPane();
+        final Tab disabled = new Tab("Disabled");
+        disabled.setDisable(true);
+
+        Tab tab1 = new Tab("Tab 1");
+        Tab tab2 = new Tab("Tab 2");
+        pane.getTabs().addAll(disabled, tab1, tab2);
+
+        assertEquals(1, pane.getSelectionModel().getSelectedIndex());
+        assertEquals(tab1, pane.getSelectionModel().getSelectedItem());
+    }
 }