1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 7171812
  26    @summary [macosx] Views keep scrolling back to the drag position after DnD
  27    @author Alexander Zuev
  28    @run main bug7171812
  29  */
  30 
  31 import sun.awt.SunToolkit;
  32 
  33 import java.awt.*;
  34 import java.awt.dnd.*;
  35 import java.awt.event.InputEvent;
  36 import javax.swing.*;
  37 
  38 public class bug7171812 {
  39     static JFrame mainFrame;
  40     static String listData[];
  41     static JListWithScroll<String> list;
  42     static JScrollPane scrollPane;
  43 
  44     /**
  45      * @param args the command line arguments
  46      */
  47     public static void main(String[] args) throws Exception{
  48         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  49 
  50         SwingUtilities.invokeAndWait(new Runnable() {
  51             @Override
  52             public void run() {
  53                 setupGUI();
  54             }
  55         });
  56         toolkit.realSync();
  57 
  58         Robot robot = new Robot();
  59         robot.setAutoDelay(10);
  60         robot.mouseMove(scrollPane.getLocationOnScreen().x + 5, scrollPane.getLocationOnScreen().y + 5);
  61         robot.mousePress(InputEvent.BUTTON1_MASK);
  62         for(int offset = 5; offset < scrollPane.getHeight()-20; offset++) {
  63             robot.mouseMove(scrollPane.getLocationOnScreen().x+5, scrollPane.getLocationOnScreen().y+offset);
  64         }
  65         for(int offset = 5; offset < 195; offset++) {
  66             robot.mouseMove(scrollPane.getLocationOnScreen().x+offset, scrollPane.getLocationOnScreen().y+scrollPane.getHeight()-20);
  67         }
  68         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  69         try {
  70             SwingUtilities.invokeAndWait(new Runnable() {
  71                 @Override
  72                 public void run() {
  73                     if(scrollPane.getViewport().getViewPosition().getY() < 30) {
  74                         throw new RuntimeException("Incorrect view position.");
  75                     };
  76                 }
  77             });
  78         } catch (java.lang.reflect.InvocationTargetException ite) {
  79             throw new RuntimeException("Test failed, scroll on drag doesn't work!");
  80         }
  81     }
  82 
  83     public static void setupGUI() {
  84         listData = new String[100];
  85         for (int i=0; i<100; i++) {
  86             listData[i] = "Long Line With Item "+i;
  87         }
  88         mainFrame = new JFrame("Rest frame");
  89         mainFrame.setSize(300, 500);
  90         mainFrame.setLayout(new BorderLayout());
  91         list = new JListWithScroll(listData);
  92         list.setDragEnabled(true);
  93         list.setAutoscrolls(true);
  94         final DropTarget dropTarget = new DropTarget(list, DnDConstants.ACTION_MOVE, new DropTargetListener() {
  95             @Override
  96             public void dragEnter(DropTargetDragEvent dtde) {
  97                 dragOver(dtde);
  98             }
  99 
 100             @Override
 101             public void dragOver(DropTargetDragEvent dtde) {
 102                 dtde.acceptDrag(DnDConstants.ACTION_MOVE);
 103             }
 104 
 105             @Override
 106             public void dropActionChanged(DropTargetDragEvent dtde) {
 107             }
 108 
 109             @Override
 110             public void dragExit(DropTargetEvent dte) {
 111             }
 112 
 113             @Override
 114             public void drop(DropTargetDropEvent dtde) {
 115             }
 116         }, true);
 117         scrollPane = new JScrollPane(list);
 118         mainFrame.add(scrollPane, BorderLayout.CENTER);
 119         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 120         mainFrame.setLocation(100, 100);
 121         mainFrame.setVisible(true);
 122     }
 123 }