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>. * the nodes identified by in <code>e</code>.
*/ */
void removeDescendantSelectedPaths(TreeModelEvent e) { void removeDescendantSelectedPaths(TreeModelEvent e) {
TreePath pPath = e.getTreePath(); TreePath pPath = SwingUtilities2.getTreePath(e, getModel());
Object[] oldChildren = e.getChildren(); Object[] oldChildren = e.getChildren();
TreeSelectionModel sm = getSelectionModel(); TreeSelectionModel sm = getSelectionModel();
@ -3785,7 +3785,7 @@ public class JTree extends JComponent implements Scrollable, Accessible
// and update BasicTreeUIs treeStructureChanged method // and update BasicTreeUIs treeStructureChanged method
// to update descendants in response to a treeStructureChanged // to update descendants in response to a treeStructureChanged
// event, all the children of the event won't collapse! // event, all the children of the event won't collapse!
TreePath parent = e.getTreePath(); TreePath parent = SwingUtilities2.getTreePath(e, getModel());
if(parent == null) if(parent == null)
return; return;
@ -3822,7 +3822,7 @@ public class JTree extends JComponent implements Scrollable, Accessible
if(e == null) if(e == null)
return; return;
TreePath parent = e.getTreePath(); TreePath parent = SwingUtilities2.getTreePath(e, getModel());
Object[] children = e.getChildren(); Object[] children = e.getChildren();
if(children == null) if(children == null)

View File

@ -128,7 +128,7 @@ public class TreeModelEvent extends EventObject {
public TreeModelEvent(Object source, Object[] path, int[] childIndices, public TreeModelEvent(Object source, Object[] path, int[] childIndices,
Object[] children) 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) 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) { public void treeNodesChanged(TreeModelEvent e) {
if(treeState != null && e != null) { if(treeState != null && e != null) {
TreePath parentPath = e.getTreePath(); TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
int[] indices = e.getChildIndices(); int[] indices = e.getChildIndices();
if (indices == null || indices.length == 0) { if (indices == null || indices.length == 0) {
// The root has changed // The root has changed
@ -3882,7 +3882,7 @@ public class BasicTreeUI extends TreeUI
updateLeadSelectionRow(); updateLeadSelectionRow();
TreePath path = e.getTreePath(); TreePath path = SwingUtilities2.getTreePath(e, getModel());
if(treeState.isExpanded(path)) { if(treeState.isExpanded(path)) {
updateSize(); updateSize();
@ -3907,7 +3907,7 @@ public class BasicTreeUI extends TreeUI
updateLeadSelectionRow(); updateLeadSelectionRow();
TreePath path = e.getTreePath(); TreePath path = SwingUtilities2.getTreePath(e, getModel());
if(treeState.isExpanded(path) || if(treeState.isExpanded(path) ||
treeModel.getChildCount(path.getLastPathComponent()) == 0) treeModel.getChildCount(path.getLastPathComponent()) == 0)
@ -3921,7 +3921,7 @@ public class BasicTreeUI extends TreeUI
updateLeadSelectionRow(); updateLeadSelectionRow();
TreePath pPath = e.getTreePath(); TreePath pPath = SwingUtilities2.getTreePath(e, getModel());
if (pPath != null) { if (pPath != null) {
pPath = pPath.getParentPath(); pPath = pPath.getParentPath();

View File

@ -26,13 +26,14 @@
package javax.swing.tree; package javax.swing.tree;
import javax.swing.event.TreeModelEvent; import javax.swing.event.TreeModelEvent;
import java.awt.Dimension;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Stack; import java.util.Stack;
import sun.swing.SwingUtilities2;
/** /**
* NOTE: This will become more open in a future release. * NOTE: This will become more open in a future release.
* <p> * <p>
@ -346,7 +347,7 @@ public class FixedHeightLayoutCache extends AbstractLayoutCache {
if(e != null) { if(e != null) {
int changedIndexs[]; int changedIndexs[];
FHTreeStateNode changedParent = getNodeForPath FHTreeStateNode changedParent = getNodeForPath
(e.getTreePath(), false, false); (SwingUtilities2.getTreePath(e, getModel()), false, false);
int maxCounter; int maxCounter;
changedIndexs = e.getChildIndices(); changedIndexs = e.getChildIndices();
@ -390,7 +391,7 @@ public class FixedHeightLayoutCache extends AbstractLayoutCache {
if(e != null) { if(e != null) {
int changedIndexs[]; int changedIndexs[];
FHTreeStateNode changedParent = getNodeForPath FHTreeStateNode changedParent = getNodeForPath
(e.getTreePath(), false, false); (SwingUtilities2.getTreePath(e, getModel()), false, false);
int maxCounter; int maxCounter;
changedIndexs = e.getChildIndices(); changedIndexs = e.getChildIndices();
@ -429,7 +430,7 @@ public class FixedHeightLayoutCache extends AbstractLayoutCache {
if(e != null) { if(e != null) {
int changedIndexs[]; int changedIndexs[];
int maxCounter; int maxCounter;
TreePath parentPath = e.getTreePath(); TreePath parentPath = SwingUtilities2.getTreePath(e, getModel());
FHTreeStateNode changedParentNode = getNodeForPath FHTreeStateNode changedParentNode = getNodeForPath
(parentPath, false, false); (parentPath, false, false);
@ -475,7 +476,7 @@ public class FixedHeightLayoutCache extends AbstractLayoutCache {
*/ */
public void treeStructureChanged(TreeModelEvent e) { public void treeStructureChanged(TreeModelEvent e) {
if(e != null) { if(e != null) {
TreePath changedPath = e.getTreePath(); TreePath changedPath = SwingUtilities2.getTreePath(e, getModel());
FHTreeStateNode changedNode = getNodeForPath FHTreeStateNode changedNode = getNodeForPath
(changedPath, false, false); (changedPath, false, false);

View File

@ -26,7 +26,6 @@
package javax.swing.tree; package javax.swing.tree;
import javax.swing.event.TreeModelEvent; import javax.swing.event.TreeModelEvent;
import java.awt.Dimension;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Hashtable; import java.util.Hashtable;
@ -34,6 +33,8 @@ import java.util.NoSuchElementException;
import java.util.Stack; import java.util.Stack;
import java.util.Vector; import java.util.Vector;
import sun.swing.SwingUtilities2;
/** /**
* NOTE: This will become more open in a future release. * NOTE: This will become more open in a future release.
* <p> * <p>
@ -413,7 +414,7 @@ public class VariableHeightLayoutCache extends AbstractLayoutCache {
TreeStateNode changedNode; TreeStateNode changedNode;
changedIndexs = e.getChildIndices(); changedIndexs = e.getChildIndices();
changedNode = getNodeForPath(e.getTreePath(), false, false); changedNode = getNodeForPath(SwingUtilities2.getTreePath(e, getModel()), false, false);
if(changedNode != null) { if(changedNode != null) {
Object changedValue = changedNode.getValue(); Object changedValue = changedNode.getValue();
@ -466,7 +467,7 @@ public class VariableHeightLayoutCache extends AbstractLayoutCache {
TreeStateNode changedParentNode; TreeStateNode changedParentNode;
changedIndexs = e.getChildIndices(); 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 /* Only need to update the children if the node has been
expanded once. */ expanded once. */
// PENDING(scott): make sure childIndexs is sorted! // PENDING(scott): make sure childIndexs is sorted!
@ -540,7 +541,7 @@ public class VariableHeightLayoutCache extends AbstractLayoutCache {
TreeStateNode changedParentNode; TreeStateNode changedParentNode;
changedIndexs = e.getChildIndices(); 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 // PENDING(scott): make sure that changedIndexs are sorted in
// ascending order. // ascending order.
if(changedParentNode != null && changedIndexs != null && if(changedParentNode != null && changedIndexs != null &&
@ -628,7 +629,7 @@ public class VariableHeightLayoutCache extends AbstractLayoutCache {
public void treeStructureChanged(TreeModelEvent e) { public void treeStructureChanged(TreeModelEvent e) {
if(e != null) if(e != null)
{ {
TreePath changedPath = e.getTreePath(); TreePath changedPath = SwingUtilities2.getTreePath(e, getModel());
TreeStateNode changedNode; TreeStateNode changedNode;
changedNode = getNodeForPath(changedPath, false, false); changedNode = getNodeForPath(changedPath, false, false);

View File

@ -33,18 +33,19 @@ import java.awt.event.*;
import java.awt.font.*; import java.awt.font.*;
import java.awt.geom.*; import java.awt.geom.*;
import java.awt.print.PrinterGraphics; import java.awt.print.PrinterGraphics;
import java.text.Bidi;
import java.text.AttributedCharacterIterator; import java.text.AttributedCharacterIterator;
import java.text.AttributedString; import java.text.AttributedString;
import javax.swing.*; import javax.swing.*;
import javax.swing.plaf.*; import javax.swing.event.TreeModelEvent;
import javax.swing.text.Highlighter; import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
import javax.swing.text.DefaultHighlighter; import javax.swing.text.DefaultHighlighter;
import javax.swing.text.DefaultCaret; import javax.swing.text.DefaultCaret;
import javax.swing.table.TableCellRenderer; import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel; import javax.swing.table.TableColumnModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import sun.swing.PrintColorUIResource; import sun.swing.PrintColorUIResource;
import sun.swing.ImageIconUIResource; import sun.swing.ImageIconUIResource;
@ -1887,4 +1888,22 @@ public class SwingUtilities2 {
} }
return InputEvent.ALT_MASK; 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);
}
}