AutoscrollDropTargetListener implements DropTargetListener, ActionListener { @Override public void actionPerformed(ActionEvent e) { Rectangle bounds = comp.getBounds(); Rectangle visible = comp.getVisibleRect(); Rectangle newVisible = new Rectangle(visible.x, visible.y, visible.width, visible.height); Scrollable s = (Scrollable) comp; if (mouse == null) { return; } if (mouse.x < visible.x + AUTOSCROLL_BORDER_SIZE) { newVisible.x = max(0, visible.x - 3*s.getScrollableUnitIncrement(visible, SwingConstants.HORIZONTAL, -1)); } else if (mouse.x > visible.x + visible.width - AUTOSCROLL_BORDER_SIZE) { newVisible.x = min(bounds.width - visible.width, visible.x + 3*s.getScrollableUnitIncrement(visible, SwingConstants.HORIZONTAL, 1)); } if (mouse.y < visible.y + AUTOSCROLL_BORDER_SIZE) { newVisible.y = max(0, visible.y - s.getScrollableUnitIncrement(visible, SwingConstants.VERTICAL, -1)); } else if (mouse.y > visible.y + visible.height - AUTOSCROLL_BORDER_SIZE) { newVisible.y = min(bounds.height - visible.height, visible.y + s.getScrollableUnitIncrement(visible, SwingConstants.VERTICAL, 1)); } if (!newVisible.equals(visible)) { comp.scrollRectToVisible(newVisible); } } AutoscrollDropTargetListener(JComponent comp);   AutoscrollDropTargetListener(JComponent comp, Timer timer); @Override synchronized void dragEnter(DropTargetDragEvent dtde); @Override void dragOver(DropTargetDragEvent dtde); @Override synchronized void dragExit(DropTargetEvent dte); @Override synchronized void drop(DropTargetDropEvent dtde); @Override void actionPerformed(ActionEvent e); @Override void dropActionChanged(DropTargetDragEvent dtde); static final int AUTOSCROLL_BORDER_SIZE; static final int AUTOSCROLL_INITIAL_DELAY; static final int AUTOSCROLL_INTERVAL; }
@Test(dataProvider="scrollTests") public void testScroll( int mouseX, int mouseY, int treeWidth, int treeHeight, int visibleX, int visibleY, int visibleWidth, int visibleHeight, int scrollXSign, int scrollYSign ) { Rectangle bounds = new Rectangle(-visibleX, -visibleY, treeWidth, treeHeight); Rectangle visible = new Rectangle(visibleX, visibleY, visibleWidth, visibleHeight); when(tree.getBounds()).thenReturn(bounds); when(tree.getVisibleRect()).thenReturn(visible); when(tree.getScrollableUnitIncrement(isA(Rectangle.class), eq(SwingConstants.HORIZONTAL), anyInt())).thenReturn(1); when(tree.getScrollableUnitIncrement(isA(Rectangle.class), eq(SwingConstants.VERTICAL), anyInt())).thenReturn(1); try { Field mouseField = listener.getClass().getDeclaredField("mouse"); mouseField.setAccessible(true); mouseField.set(listener, new Point(mouseX, mouseY)); } catch (Exception e) { Assert.fail(); } ActionEvent ae = mock(ActionEvent.class); listener.actionPerformed(ae); verifyZeroInteractions(ae); ArgumentCaptor<Rectangle> newVisibleArg = ArgumentCaptor.forClass(Rectangle.class); if (scrollXSign==0 && scrollYSign==0) { verify(tree, never()).scrollRectToVisible(isA(Rectangle.class)); } else { verify(tree).scrollRectToVisible(newVisibleArg.capture()); Rectangle newVisible = newVisibleArg.getValue(); if (scrollXSign < 0) { assertTrue(newVisible.x < visible.x); } else if (scrollXSign > 0) { assertTrue(newVisible.x > visible.x); } if (scrollYSign < 0) { assertTrue(newVisible.y < visible.y); } else if (scrollYSign > 0) { assertTrue(newVisible.y > visible.y); } } }
@Test(dataProvider="scrollTests") public void testScrollWhenMouseNull( int mouseX, int mouseY, int treeWidth, int treeHeight, int visibleX, int visibleY, int visibleWidth, int visibleHeight, int scrollXSign, int scrollYSign ) { Rectangle bounds = new Rectangle(-visibleX, -visibleY, treeWidth, treeHeight); Rectangle visible = new Rectangle(visibleX, visibleY, visibleWidth, visibleHeight); when(tree.getBounds()).thenReturn(bounds); when(tree.getVisibleRect()).thenReturn(visible); when(tree.getMousePosition()).thenReturn(null); when(tree.getScrollableUnitIncrement(isA(Rectangle.class), eq(SwingConstants.HORIZONTAL), anyInt())).thenReturn(1); when(tree.getScrollableUnitIncrement(isA(Rectangle.class), eq(SwingConstants.VERTICAL), anyInt())).thenReturn(1); ActionEvent ae = mock(ActionEvent.class); listener.actionPerformed(ae); verify(tree, never()).scrollRectToVisible(isA(Rectangle.class)); verifyZeroInteractions(ae); }