8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies

Reviewed-by: alexsch
This commit is contained in:
Sergey Malenkov 2013-06-17 18:30:49 +04:00
parent 25b7910613
commit 153bde4dbb
7 changed files with 106 additions and 21 deletions

View File

@ -3751,7 +3751,7 @@ public class JTree extends JComponent implements Scrollable, Accessible
* 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 @@ 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)

View File

@ -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));
}
/**

View File

@ -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();

View File

@ -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 @@ 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);

View File

@ -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.
* <p>
@ -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);

View File

@ -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;
}
}

View File

@ -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);
}
}