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 /*
  25  * @test
  26  * @key headful
  27  * @bug 7171812
  28  * @summary [macosx] Views keep scrolling back to the drag position after DnD
  29  * @author Alexander Zuev
  30  * @run main bug7171812
  31  */
  32 
  33 import sun.awt.SunToolkit;
  34 
  35 import java.awt.*;
  36 import java.awt.dnd.*;
  37 import java.awt.event.InputEvent;
  38 import javax.swing.*;
  39 
  40 public class bug7171812 {
  41     static JFrame mainFrame;
  42     static String listData[];
  43     static JListWithScroll<String> list;
  44     static JScrollPane scrollPane;
  45 
  46     /**
  47      * @param args the command line arguments
  48      */
  49     public static void main(String[] args) throws Exception{
  50         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  51 
  52         SwingUtilities.invokeAndWait(new Runnable() {
  53             @Override
  54             public void run() {
  55                 setupGUI();
  56             }
  57         });
  58         toolkit.realSync();
  59 
  60         Robot robot = new Robot();
  61         robot.setAutoDelay(10);
  62         robot.mouseMove(scrollPane.getLocationOnScreen().x + 5, scrollPane.getLocationOnScreen().y + 5);
  63         robot.mousePress(InputEvent.BUTTON1_MASK);
  64         for(int offset = 5; offset < scrollPane.getHeight()-20; offset++) {
  65             robot.mouseMove(scrollPane.getLocationOnScreen().x+5, scrollPane.getLocationOnScreen().y+offset);
  66         }
  67         for(int offset = 5; offset < 195; offset++) {
  68             robot.mouseMove(scrollPane.getLocationOnScreen().x+offset, scrollPane.getLocationOnScreen().y+scrollPane.getHeight()-20);
  69         }
  70         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  71         try {
  72             SwingUtilities.invokeAndWait(new Runnable() {
  73                 @Override
  74                 public void run() {
  75                     if(scrollPane.getViewport().getViewPosition().getY() < 30) {
  76                         throw new RuntimeException("Incorrect view position.");
  77                     };
  78                 }
  79             });
  80         } catch (java.lang.reflect.InvocationTargetException ite) {
  81             throw new RuntimeException("Test failed, scroll on drag doesn't work!");
  82         }
  83     }
  84 
  85     public static void setupGUI() {
  86         listData = new String[100];
  87         for (int i=0; i<100; i++) {
  88             listData[i] = "Long Line With Item "+i;
  89         }
  90         mainFrame = new JFrame("Rest frame");
  91         mainFrame.setSize(300, 500);
  92         mainFrame.setLayout(new BorderLayout());
  93         list = new JListWithScroll(listData);
  94         list.setDragEnabled(true);
  95         list.setAutoscrolls(true);
  96         final DropTarget dropTarget = new DropTarget(list, DnDConstants.ACTION_MOVE, new DropTargetListener() {
  97             @Override
  98             public void dragEnter(DropTargetDragEvent dtde) {
  99                 dragOver(dtde);
 100             }
 101 
 102             @Override
 103             public void dragOver(DropTargetDragEvent dtde) {
 104                 dtde.acceptDrag(DnDConstants.ACTION_MOVE);
 105             }
 106 
 107             @Override
 108             public void dropActionChanged(DropTargetDragEvent dtde) {
 109             }
 110 
 111             @Override
 112             public void dragExit(DropTargetEvent dte) {
 113             }
 114 
 115             @Override
 116             public void drop(DropTargetDropEvent dtde) {
 117             }
 118         }, true);
 119         scrollPane = new JScrollPane(list);
 120         mainFrame.add(scrollPane, BorderLayout.CENTER);
 121         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 122         mainFrame.setLocation(100, 100);
 123         mainFrame.setVisible(true);
 124     }
 125 }