changeset 1386:feb33f9ce89b

RT-22925: When closing a Tab inside a TabPane, the selection model does not return the correct Tab.
author Kinsley Wong
date Thu, 28 Jun 2012 14:48:18 -0700
parents 2d7d2be9cdb7
children 90b96294241a
files javafx-ui-controls/src/com/sun/javafx/scene/control/behavior/TabPaneBehavior.java javafx-ui-controls/src/javafx/scene/control/TabPane.java javafx-ui-controls/test/javafx/scene/control/TabPaneTest.java
diffstat 3 files changed, 33 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/javafx-ui-controls/src/com/sun/javafx/scene/control/behavior/TabPaneBehavior.java	Thu Jun 28 14:45:48 2012 -0700
+++ b/javafx-ui-controls/src/com/sun/javafx/scene/control/behavior/TabPaneBehavior.java	Thu Jun 28 14:48:18 2012 -0700
@@ -144,8 +144,8 @@
         int index = tabPane.getTabs().indexOf(tab);
         if (tab.isSelected()) {
             if (index == 0) {
-                if (tabPane.getTabs().size() > 1) {
-                    tabPane.getSelectionModel().clearSelection();
+                if (tabPane.getTabs().size() > 0) {
+                    tabPane.getSelectionModel().selectFirst();
                 }
             } else {
                 tabPane.getSelectionModel().selectPrevious();
--- a/javafx-ui-controls/src/javafx/scene/control/TabPane.java	Thu Jun 28 14:45:48 2012 -0700
+++ b/javafx-ui-controls/src/javafx/scene/control/TabPane.java	Thu Jun 28 14:48:18 2012 -0700
@@ -629,8 +629,8 @@
                     while (c.next()) {
                         for (Tab tab : c.getRemoved()) {
                             if (tab != null && !tabPane.getTabs().contains(tab)) {
-                                if (getSelectedIndex() == 0 && tabPane.getTabs().size() > 0) {
-                                    clearSelection();
+                                if (getSelectedIndex() == 0 && tabPane.getTabs().size() > 0) {                                    
+                                    clearAndSelect(0);
                                     tab.setSelected(false);
                                 }
                                 if (tab.isSelected()) {
@@ -653,7 +653,7 @@
                             }
                         }
                     }
-                    if (getSelectedIndex() == -1 && getSelectedItem() == null && tabPane.getTabs().size() > 0) {
+                    if (getSelectedIndex() == -1 && getSelectedItem() == null && tabPane.getTabs().size() > 0) {                        
                         selectFirst();
                     } else if (tabPane.getTabs().isEmpty()) {
                         clearSelection();
@@ -667,10 +667,11 @@
 
         // API Implementation
         @Override public void select(int index) {
-            if (index < 0 || (getItemCount() > 0 && index >= getItemCount()) || getSelectedIndex() == index) {
+            if (index < 0 || (getItemCount() > 0 && index >= getItemCount()) ||
+                (index == getSelectedIndex() && getModelItem(index).isSelected())) {
                 return;
             }
-
+            
             // Unselect the old tab
             if (getSelectedIndex() >= 0 && getSelectedIndex() < tabPane.getTabs().size()) {
                 tabPane.getTabs().get(getSelectedIndex()).setSelected(false);
--- a/javafx-ui-controls/test/javafx/scene/control/TabPaneTest.java	Thu Jun 28 14:45:48 2012 -0700
+++ b/javafx-ui-controls/test/javafx/scene/control/TabPaneTest.java	Thu Jun 28 14:48:18 2012 -0700
@@ -15,6 +15,8 @@
 import javafx.beans.property.SimpleBooleanProperty;
 import javafx.beans.property.SimpleDoubleProperty;
 import javafx.beans.property.SimpleObjectProperty;
+import javafx.beans.value.ChangeListener;
+import javafx.beans.value.ObservableValue;
 import javafx.event.Event;
 import javafx.event.EventHandler;
 import javafx.geometry.Side;
@@ -742,4 +744,27 @@
         assertEquals(0, tabPane.getSelectionModel().getSelectedIndex());
         assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem());        
     }
+    
+    @Test public void selectionModelShouldNotBeNullWhenClosingFirstTab_RT22925() {
+        tabPane.getTabs().add(tab1);
+        tabPane.getTabs().add(tab2);
+        tabPane.getTabs().add(tab3);        
+
+        tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {
+            public void changed(ObservableValue<? extends Tab> ov, Tab t, Tab t1) {
+                assertEquals(t.getText(), "one");
+                assertEquals(t1.getText(), "two");
+            }            
+        });
+        
+        assertEquals("one", tabPane.getTabs().get(0).getText());
+        assertEquals("two", tabPane.getTabs().get(1).getText());
+        assertEquals("three", tabPane.getTabs().get(2).getText());
+        
+        tabPane.getTabs().remove(tab1);
+
+        assertEquals(2, tabPane.getTabs().size());
+        assertEquals(0, tabPane.getSelectionModel().getSelectedIndex());
+        assertEquals(tab2, tabPane.getSelectionModel().getSelectedItem());        
+    }    
 }