changeset 4843:2fd809b11dfd

RT-30317 ChoiceBox failed to update the displayed text after calling setItems from Platfrom.runLater
author psomashe
date Wed, 28 Aug 2013 14:38:29 -0700
parents bf88b13087fd
children 9b8894841264
files modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ChoiceBoxSkin.java modules/controls/src/test/java/com/sun/javafx/scene/control/skin/ChoiceBoxSkinNodesRetriever.java modules/controls/src/test/java/javafx/scene/control/ChoiceBoxTest.java
diffstat 3 files changed, 71 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ChoiceBoxSkin.java	Thu Aug 29 09:34:24 2013 +1200
+++ b/modules/controls/src/main/java/com/sun/javafx/scene/control/skin/ChoiceBoxSkin.java	Wed Aug 28 14:38:29 2013 -0700
@@ -180,6 +180,11 @@
             choiceBoxItems.addListener(weakChoiceBoxItemsListener);
         }
     }
+    
+    // Test only purpose    
+    String getChoiceBoxSelectedText() {
+        return label.getText();
+    }
 
     @SuppressWarnings("rawtypes")
     @Override protected void handleControlPropertyChanged(String p) {
@@ -187,6 +192,11 @@
         if ("ITEMS".equals(p)) {
             updateChoiceBoxItems();
             updatePopupItems();
+            updateSelectionModel();
+            updateSelection();
+            if(selectionModel != null && selectionModel.getSelectedIndex() == -1) {
+                label.setText(""); // clear label text when selectedIndex is -1
+            }
         } else if (("SELECTION_MODEL").equals(p)) {
             updateSelectionModel();
         } else if ("SELECTION_CHANGED".equals(p)) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/controls/src/test/java/com/sun/javafx/scene/control/skin/ChoiceBoxSkinNodesRetriever.java	Wed Aug 28 14:38:29 2013 -0700
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2011, 2013, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.javafx.scene.control.skin;
+
+/**
+ *
+ * @author paru
+ */
+public class ChoiceBoxSkinNodesRetriever {
+    
+    // can only access the getChoiceBoxSelectedText method in ChoiceBoxSkin 
+    // from this package.
+    public static String getChoiceBoxSelectedText(ChoiceBoxSkin skin) {
+        return skin.getChoiceBoxSelectedText();
+    }
+}
--- a/modules/controls/src/test/java/javafx/scene/control/ChoiceBoxTest.java	Thu Aug 29 09:34:24 2013 +1200
+++ b/modules/controls/src/test/java/javafx/scene/control/ChoiceBoxTest.java	Wed Aug 28 14:38:29 2013 -0700
@@ -31,6 +31,9 @@
 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertPseudoClassDoesNotExist;
 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertPseudoClassExists;
 import static com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains;
+import com.sun.javafx.scene.control.skin.ChoiceBoxSkin;
+import com.sun.javafx.scene.control.skin.ChoiceBoxSkinNodesRetriever;
+import javafx.application.Platform;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -53,7 +56,7 @@
 import org.junit.Test;
 
 public class ChoiceBoxTest {
-    private ChoiceBox<String> box;
+    private final ChoiceBox<String> box = new ChoiceBox<String>();
     private Toolkit tk;
     private Scene scene;
     private Stage stage;
@@ -61,7 +64,6 @@
     @Before public void setup() {
         //This step is not needed (Just to make sure StubToolkit is loaded into VM)
         tk = (StubToolkit)Toolkit.getToolkit();
-        box = new ChoiceBox<String>();
     }
     
     protected void startApp(Parent root) {
@@ -439,4 +441,22 @@
         startApp(pane);
         assertEquals(1, box.getSelectionModel().getSelectedIndex());
     }
+     
+    @Test public void checkLabelAfterCallingSetItemsFromPlatformRunLater_RT30317() {
+        final String[] items = {"Apple", "Orange", "Banana"};
+        StackPane pane = new StackPane();
+        pane.getChildren().add(box);
+        Runnable runnable = new Runnable() {
+            public void run() {
+                box.setItems(FXCollections.observableArrayList(items));
+                box.getSelectionModel().setSelectedItem("Apple");
+            }
+        };
+        Platform.runLater(runnable); 
+        startApp(pane);
+        assertEquals(0, box.getSelectionModel().getSelectedIndex());
+        ChoiceBoxSkin skin = (ChoiceBoxSkin)box.getSkin();
+        assertEquals("Apple", ChoiceBoxSkinNodesRetriever.getChoiceBoxSelectedText(skin));
+        
+    }
 }