8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
authormalenkov
Mon, 17 Jun 2013 18:30:49 +0400
changeset 18133 41b4b144eaff
parent 18132 83b6a2cc05a3
child 18134 af8df2260431
8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies. Reviewed-by: alexsch
jdk/src/share/classes/javax/swing/JTree.java
jdk/src/share/classes/javax/swing/event/TreeModelEvent.java
jdk/src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java
jdk/src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java
jdk/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java
jdk/src/share/classes/sun/swing/SwingUtilities2.java
jdk/test/javax/swing/JTree/8013571/Test8013571.java
--- a/jdk/src/share/classes/javax/swing/JTree.java	Fri Jun 14 10:41:45 2013 -0700
+++ b/jdk/src/share/classes/javax/swing/JTree.java	Mon Jun 17 18:30:49 2013 +0400
@@ -3751,7 +3751,7 @@
      * the nodes identified by in <code>e</code>.
      */
     void removeDescendantSelectedPaths(TreeModelEvent e) {
-        TreePath            pPath = e.getTreePath();
+        TreePath            pPath = SwingUtilities2.getTreePath(e, getModel());
         Object[]            oldChildren = e.getChildren();
         TreeSelectionModel  sm = getSelectionModel();
 
@@ -3785,7 +3785,7 @@
             // and update BasicTreeUIs treeStructureChanged method
             // to update descendants in response to a treeStructureChanged
             // event, all the children of the event won't collapse!
-            TreePath            parent = e.getTreePath();
+            TreePath            parent = SwingUtilities2.getTreePath(e, getModel());
 
             if(parent == null)
                 return;
@@ -3822,7 +3822,7 @@
             if(e == null)
                 return;
 
-            TreePath            parent = e.getTreePath();
+            TreePath            parent = SwingUtilities2.getTreePath(e, getModel());
             Object[]            children = e.getChildren();
 
             if(children == null)
--- a/jdk/src/share/classes/javax/swing/event/TreeModelEvent.java	Fri Jun 14 10:41:45 2013 -0700
+++ b/jdk/src/share/classes/javax/swing/event/TreeModelEvent.java	Mon Jun 17 18:30:49 2013 +0400
@@ -128,7 +128,7 @@
     public TreeModelEvent(Object source, Object[] path, int[] childIndices,
                           Object[] children)
     {
-        this(source, new TreePath(path), childIndices, children);
+        this(source, (path == null) ? null : new TreePath(path), childIndices, children);
     }
 
     /**
@@ -183,7 +183,7 @@
      */
     public TreeModelEvent(Object source, Object[] path)
     {
-        this(source, new TreePath(path));
+        this(source, (path == null) ? null : new TreePath(path));
     }
 
     /**
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java	Fri Jun 14 10:41:45 2013 -0700
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java	Mon Jun 17 18:30:49 2013 +0400
@@ -3827,7 +3827,7 @@
         //
         public void treeNodesChanged(TreeModelEvent e) {
             if(treeState != null && e != null) {
-                TreePath parentPath = e.getTreePath();
+                TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
                 int[] indices = e.getChildIndices();
                 if (indices == null || indices.length == 0) {
                     // The root has changed
@@ -3882,7 +3882,7 @@
 
                 updateLeadSelectionRow();
 
-                TreePath       path = e.getTreePath();
+                TreePath       path = SwingUtilities2.getTreePath(e, getModel());
 
                 if(treeState.isExpanded(path)) {
                     updateSize();
@@ -3907,7 +3907,7 @@
 
                 updateLeadSelectionRow();
 
-                TreePath       path = e.getTreePath();
+                TreePath       path = SwingUtilities2.getTreePath(e, getModel());
 
                 if(treeState.isExpanded(path) ||
                    treeModel.getChildCount(path.getLastPathComponent()) == 0)
@@ -3921,7 +3921,7 @@
 
                 updateLeadSelectionRow();
 
-                TreePath       pPath = e.getTreePath();
+                TreePath       pPath = SwingUtilities2.getTreePath(e, getModel());
 
                 if (pPath != null) {
                     pPath = pPath.getParentPath();
--- a/jdk/src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java	Fri Jun 14 10:41:45 2013 -0700
+++ b/jdk/src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java	Mon Jun 17 18:30:49 2013 +0400
@@ -26,13 +26,14 @@
 package javax.swing.tree;
 
 import javax.swing.event.TreeModelEvent;
-import java.awt.Dimension;
 import java.awt.Rectangle;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.NoSuchElementException;
 import java.util.Stack;
 
+import sun.swing.SwingUtilities2;
+
 /**
  * NOTE: This will become more open in a future release.
  * <p>
@@ -346,7 +347,7 @@
         if(e != null) {
             int                 changedIndexs[];
             FHTreeStateNode     changedParent = getNodeForPath
-                                  (e.getTreePath(), false, false);
+                                  (SwingUtilities2.getTreePath(e, getModel()), false, false);
             int                 maxCounter;
 
             changedIndexs = e.getChildIndices();
@@ -390,7 +391,7 @@
         if(e != null) {
             int                 changedIndexs[];
             FHTreeStateNode     changedParent = getNodeForPath
-                                  (e.getTreePath(), false, false);
+                                  (SwingUtilities2.getTreePath(e, getModel()), false, false);
             int                 maxCounter;
 
             changedIndexs = e.getChildIndices();
@@ -429,7 +430,7 @@
         if(e != null) {
             int                  changedIndexs[];
             int                  maxCounter;
-            TreePath             parentPath = e.getTreePath();
+            TreePath             parentPath = SwingUtilities2.getTreePath(e, getModel());
             FHTreeStateNode      changedParentNode = getNodeForPath
                                        (parentPath, false, false);
 
@@ -475,7 +476,7 @@
      */
     public void treeStructureChanged(TreeModelEvent e) {
         if(e != null) {
-            TreePath          changedPath = e.getTreePath();
+            TreePath          changedPath = SwingUtilities2.getTreePath(e, getModel());
             FHTreeStateNode   changedNode = getNodeForPath
                                                 (changedPath, false, false);
 
--- a/jdk/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java	Fri Jun 14 10:41:45 2013 -0700
+++ b/jdk/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java	Mon Jun 17 18:30:49 2013 +0400
@@ -26,7 +26,6 @@
 package javax.swing.tree;
 
 import javax.swing.event.TreeModelEvent;
-import java.awt.Dimension;
 import java.awt.Rectangle;
 import java.util.Enumeration;
 import java.util.Hashtable;
@@ -34,6 +33,8 @@
 import java.util.Stack;
 import java.util.Vector;
 
+import sun.swing.SwingUtilities2;
+
 /**
  * NOTE: This will become more open in a future release.
  * <p>
@@ -413,7 +414,7 @@
             TreeStateNode     changedNode;
 
             changedIndexs = e.getChildIndices();
-            changedNode = getNodeForPath(e.getTreePath(), false, false);
+            changedNode = getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
             if(changedNode != null) {
                 Object            changedValue = changedNode.getValue();
 
@@ -466,7 +467,7 @@
             TreeStateNode     changedParentNode;
 
             changedIndexs = e.getChildIndices();
-            changedParentNode = getNodeForPath(e.getTreePath(), false, false);
+            changedParentNode = getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
             /* Only need to update the children if the node has been
                expanded once. */
             // PENDING(scott): make sure childIndexs is sorted!
@@ -540,7 +541,7 @@
             TreeStateNode     changedParentNode;
 
             changedIndexs = e.getChildIndices();
-            changedParentNode = getNodeForPath(e.getTreePath(), false, false);
+            changedParentNode = getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
             // PENDING(scott): make sure that changedIndexs are sorted in
             // ascending order.
             if(changedParentNode != null && changedIndexs != null &&
@@ -628,7 +629,7 @@
     public void treeStructureChanged(TreeModelEvent e) {
         if(e != null)
         {
-            TreePath          changedPath = e.getTreePath();
+            TreePath          changedPath = SwingUtilities2.getTreePath(e, getModel());
             TreeStateNode     changedNode;
 
             changedNode = getNodeForPath(changedPath, false, false);
--- a/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Fri Jun 14 10:41:45 2013 -0700
+++ b/jdk/src/share/classes/sun/swing/SwingUtilities2.java	Mon Jun 17 18:30:49 2013 +0400
@@ -33,18 +33,19 @@
 import java.awt.font.*;
 import java.awt.geom.*;
 import java.awt.print.PrinterGraphics;
-import java.text.Bidi;
 import java.text.AttributedCharacterIterator;
 import java.text.AttributedString;
 
 import javax.swing.*;
-import javax.swing.plaf.*;
+import javax.swing.event.TreeModelEvent;
 import javax.swing.text.Highlighter;
 import javax.swing.text.JTextComponent;
 import javax.swing.text.DefaultHighlighter;
 import javax.swing.text.DefaultCaret;
 import javax.swing.table.TableCellRenderer;
 import javax.swing.table.TableColumnModel;
+import javax.swing.tree.TreeModel;
+import javax.swing.tree.TreePath;
 
 import sun.swing.PrintColorUIResource;
 import sun.swing.ImageIconUIResource;
@@ -1887,4 +1888,22 @@
         }
         return InputEvent.ALT_MASK;
     }
+
+    /**
+     * Returns the {@link TreePath} that identifies the changed nodes.
+     *
+     * @param event  changes in a tree model
+     * @param model  corresponing tree model
+     * @return  the path to the changed nodes
+     */
+    public static TreePath getTreePath(TreeModelEvent event, TreeModel model) {
+        TreePath path = event.getTreePath();
+        if ((path == null) && (model != null)) {
+            Object root = model.getRoot();
+            if (root != null) {
+                path = new TreePath(root);
+            }
+        }
+        return path;
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JTree/8013571/Test8013571.java	Mon Jun 17 18:30:49 2013 +0400
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 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.
+ *
+ * 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.
+ */
+
+import javax.swing.JTree;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.DefaultTreeModel;
+
+/*
+ * @test
+ * @bug 8016545
+ * @summary Tests beans with public fields
+ * @author Sergey Malenkov
+ */
+
+public class Test8013571 extends DefaultTreeModel {
+    public static void main(String[] args) {
+        DefaultMutableTreeNode root = create("root");
+        root.add(create("colors", "blue", "violet", "red", "yellow"));
+        root.add(create("sports", "basketball", "soccer", "football", "hockey"));
+        root.add(create("food", "hot dogs", "pizza", "ravioli", "bananas"));
+        Test8013571 model = new Test8013571(root);
+        JTree tree = new JTree(model);
+        model.fireTreeChanged(tree);
+    }
+
+    private static DefaultMutableTreeNode create(String name, String... values) {
+        DefaultMutableTreeNode node = new DefaultMutableTreeNode(name);
+        for (String value : values) {
+            node.add(create(value));
+        }
+        return node;
+    }
+
+    private Test8013571(DefaultMutableTreeNode root) {
+        super(root);
+    }
+
+    private void fireTreeChanged(Object source) {
+        fireTreeNodesInserted(source, null, null, null);
+        fireTreeNodesChanged(source, null, null, null);
+        fireTreeNodesRemoved(source, null, null, null);
+        fireTreeStructureChanged(source, null, null, null);
+    }
+}