assertj / assertj/assertj-swing
Select + drag may fail in JTree in JScrollPane if the row is only partially visible
- Dominant language
- Java
- Stars
- 121
- Forks
- 52
- PR merge metrics
- No merged PRs in 30d
Description
_Issue by **[Per Rovegård](http://jira.codehaus.org/secure/ViewProfile.jspa?name=provegard)** from Thu, 20 Jan 2011 02:34:16 -0600_
_Originally opened as http://jira.codehaus.org/browse/FEST-422_
---
Description
I have created a small test program where a JTree is put in a JScrollPane. The tree has a root and a child with a really, really long name. The scroll pane is placed in a JSplitPane with the divider location set so that the row with the really long name is only partially visible. The other split pane component is a label.
The tree supports drag and drop with printouts to the console, and the label prints a message on the console if a mouse button is pressed.
When the test program runs, notice that selectRow fails, as does drag. When these calls are made, the mouse click occurs on the label instead, as can be seen on the console.
Test program
import static org.fest.swing.timing.Pause.pause;import java.awt.Component;
import java.awt.datatransfer.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.tree.*;import org.fest.swing.core.*;
import org.fest.swing.finder.WindowFinder;
import org.fest.swing.fixture.FrameFixture;public class SelectAndDnDTest
{
public static void main(String[] args)
{
Robot robot = BasicRobot.robotWithNewAwtHierarchy();SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JSplitPane pane = new JSplitPane();
pane.setDividerLocation(150);JLabel label = new JLabel("Test");
label.addMouseListener(new MouseAdapter()
{
@Override
public void mousePressed(MouseEvent e)
{
System.out.println("Mouse press on label");
}
});pane.setLeftComponent(new JScrollPane(createTree()));
pane.setRightComponent(label);JFrame frame = new JFrame("Test");
frame.setSize(640, 480);
frame.getContentPane().add(pane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
});FrameFixture window = WindowFinder.findFrame(JFrame.class).using(robot);
pause(1000);
window.tree().selectRow(1);
pause(1000);
window.tree().drag(1);
pause(1000);
window.tree().drop(0);robot.cleanUp();
}protected static Component createTree()
{
final JTree tree = new JTree();
tree.setDragEnabled(true);
tree.setDropMode(DropMode.ON);
tree.setTransferHandler(new TransferHandler()
{
@Override
public int getSourceActions(JComponent c)
{
return COPY;
}@Override
public boolean importData(TransferSupport support)
{
Transferable t = support.getTransferable();
try
{
String s = (String) t.getTransferData(DataFlavor.stringFlavor);
DropLocation loc = support.getDropLocation();
TreePath dropPath = tree.getPathForLocation(loc.getDropPoint().x, loc.getDropPoint().y);System.out.println("Dragged " + s + " and dropped on " + dropPath);
}
catch (Exception ex)
{
ex.printStackTrace(System.err);
return false;
}
return true;
}@Override
public boolean canImport(TransferSupport support)
{
return true;
}@Override
protected Transferable createTransferable(JComponent c)
{
TreePath path = tree.getSelectionPath();
return new StringSelection(path.toString());
}
});DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root", true);
TreeModel model = new DefaultTreeModel(root);root.add(new DefaultMutableTreeNode(
"Child with a really, really long name that makes the center point move to the right very much."));tree.setModel(model);
return tree;
}
}
Actual printout
Mouse press on label
Mouse press on label
Mouse press on label
Expected printout
Dragged [Root, Child with a really, really long name that makes the center point move to the right very much.] and dropped on [Root]
Fix
The problem seems to be in JTreeLocation.rowBoundsAndCoordinates. Although the JavaDoc comment says "Returns the bounds and visible coordinates of the given row", the method fails to take into account the visible rectangle of the tree. Suggested fix:
@RunsInCurrentThread
public Pair<Rectangle, Point> rowBoundsAndCoordinates(JTree tree, int row) {
Rectangle rowBounds = tree.getRowBounds(validIndex(tree, row));
// BEGIN FIX
Rectangle visibleRect = tree.getVisibleRect();
rowBounds = rowBounds.intersection(visibleRect);
// END FIX
if (rowBounds != null) return new Pair<Rectangle, Point>(rowBounds, pointAt(rowBounds));
throw new LocationUnavailableException(concat("The tree row ", row, " is not visible"));
}
---
votes (original issue): 0
watches (original issue): 0
Contributor guide
Assessment
This issue has not been assessed yet.