diff --git a/jdk/src/share/classes/javax/swing/JTree.java b/jdk/src/share/classes/javax/swing/JTree.java index f4d89e00424..09b31da9f28 100644 --- a/jdk/src/share/classes/javax/swing/JTree.java +++ b/jdk/src/share/classes/javax/swing/JTree.java @@ -3751,7 +3751,7 @@ public class JTree extends JComponent implements Scrollable, Accessible * the nodes identified by in e. */ 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 @@ public class JTree extends JComponent implements Scrollable, Accessible // 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 @@ public class JTree extends JComponent implements Scrollable, Accessible if(e == null) return; - TreePath parent = e.getTreePath(); + TreePath parent = SwingUtilities2.getTreePath(e, getModel()); Object[] children = e.getChildren(); if(children == null) diff --git a/jdk/src/share/classes/javax/swing/event/TreeModelEvent.java b/jdk/src/share/classes/javax/swing/event/TreeModelEvent.java index 62389469839..3c92dc16377 100644 --- a/jdk/src/share/classes/javax/swing/event/TreeModelEvent.java +++ b/jdk/src/share/classes/javax/swing/event/TreeModelEvent.java @@ -128,7 +128,7 @@ public class TreeModelEvent extends EventObject { 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 class TreeModelEvent extends EventObject { */ public TreeModelEvent(Object source, Object[] path) { - this(source, new TreePath(path)); + this(source, (path == null) ? null : new TreePath(path)); } /** diff --git a/jdk/src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java b/jdk/src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java index 3d39cf6a876..0e7cd1768e7 100644 --- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java +++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicTreeUI.java @@ -3827,7 +3827,7 @@ public class BasicTreeUI extends TreeUI // 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 @@ public class BasicTreeUI extends TreeUI updateLeadSelectionRow(); - TreePath path = e.getTreePath(); + TreePath path = SwingUtilities2.getTreePath(e, getModel()); if(treeState.isExpanded(path)) { updateSize(); @@ -3907,7 +3907,7 @@ public class BasicTreeUI extends TreeUI updateLeadSelectionRow(); - TreePath path = e.getTreePath(); + TreePath path = SwingUtilities2.getTreePath(e, getModel()); if(treeState.isExpanded(path) || treeModel.getChildCount(path.getLastPathComponent()) == 0) @@ -3921,7 +3921,7 @@ public class BasicTreeUI extends TreeUI updateLeadSelectionRow(); - TreePath pPath = e.getTreePath(); + TreePath pPath = SwingUtilities2.getTreePath(e, getModel()); if (pPath != null) { pPath = pPath.getParentPath(); diff --git a/jdk/src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java b/jdk/src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java index fffea8b0b5b..4f61aff7304 100644 --- a/jdk/src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java +++ b/jdk/src/share/classes/javax/swing/tree/FixedHeightLayoutCache.java @@ -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. *

@@ -346,7 +347,7 @@ public class FixedHeightLayoutCache extends AbstractLayoutCache { 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 @@ public class FixedHeightLayoutCache extends AbstractLayoutCache { 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 @@ public class FixedHeightLayoutCache extends AbstractLayoutCache { 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 class FixedHeightLayoutCache extends AbstractLayoutCache { */ public void treeStructureChanged(TreeModelEvent e) { if(e != null) { - TreePath changedPath = e.getTreePath(); + TreePath changedPath = SwingUtilities2.getTreePath(e, getModel()); FHTreeStateNode changedNode = getNodeForPath (changedPath, false, false); diff --git a/jdk/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java b/jdk/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java index ad91d1c84c2..5185194ed3b 100644 --- a/jdk/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java +++ b/jdk/src/share/classes/javax/swing/tree/VariableHeightLayoutCache.java @@ -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.NoSuchElementException; import java.util.Stack; import java.util.Vector; +import sun.swing.SwingUtilities2; + /** * NOTE: This will become more open in a future release. *

@@ -413,7 +414,7 @@ public class VariableHeightLayoutCache extends AbstractLayoutCache { 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 @@ public class VariableHeightLayoutCache extends AbstractLayoutCache { 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 @@ public class VariableHeightLayoutCache extends AbstractLayoutCache { 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 class VariableHeightLayoutCache extends AbstractLayoutCache { 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); diff --git a/jdk/src/share/classes/sun/swing/SwingUtilities2.java b/jdk/src/share/classes/sun/swing/SwingUtilities2.java index c6134885adf..a1903f2bdd2 100644 --- a/jdk/src/share/classes/sun/swing/SwingUtilities2.java +++ b/jdk/src/share/classes/sun/swing/SwingUtilities2.java @@ -33,18 +33,19 @@ import java.awt.event.*; 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 @@ public class SwingUtilities2 { } 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; + } } diff --git a/jdk/test/javax/swing/JTree/8013571/Test8013571.java b/jdk/test/javax/swing/JTree/8013571/Test8013571.java new file mode 100644 index 00000000000..15ad391dcf2 --- /dev/null +++ b/jdk/test/javax/swing/JTree/8013571/Test8013571.java @@ -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); + } +}