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
33import java.awt.*;
34import java.awt.dnd.*;
35import java.awt.event.InputEvent;
36import javax.swing.*;
37
38public 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
49        SwingUtilities.invokeAndWait(new Runnable() {
50            @Override
51            public void run() {
52                setupGUI();
53            }
54        });
55
56        Robot robot = new Robot();
57        robot.setAutoDelay(10);
58        robot.waitForIdle();
59        robot.mouseMove(scrollPane.getLocationOnScreen().x + 5, scrollPane.getLocationOnScreen().y + 5);
60        robot.mousePress(InputEvent.BUTTON1_MASK);
61        for(int offset = 5; offset < scrollPane.getHeight()-20; offset++) {
62            robot.mouseMove(scrollPane.getLocationOnScreen().x+5, scrollPane.getLocationOnScreen().y+offset);
63        }
64        for(int offset = 5; offset < 195; offset++) {
65            robot.mouseMove(scrollPane.getLocationOnScreen().x+offset, scrollPane.getLocationOnScreen().y+scrollPane.getHeight()-20);
66        }
67        robot.mouseRelease(InputEvent.BUTTON1_MASK);
68        try {
69            SwingUtilities.invokeAndWait(new Runnable() {
70                @Override
71                public void run() {
72                    if(scrollPane.getViewport().getViewPosition().getY() < 30) {
73                        throw new RuntimeException("Incorrect view position.");
74                    };
75                }
76            });
77        } catch (java.lang.reflect.InvocationTargetException ite) {
78            throw new RuntimeException("Test failed, scroll on drag doesn't work!");
79        }
80    }
81
82    public static void setupGUI() {
83        listData = new String[100];
84        for (int i=0; i<100; i++) {
85            listData[i] = "Long Line With Item "+i;
86        }
87        mainFrame = new JFrame("Rest frame");
88        mainFrame.setSize(300, 500);
89        mainFrame.setLayout(new BorderLayout());
90        list = new JListWithScroll(listData);
91        list.setDragEnabled(true);
92        list.setAutoscrolls(true);
93        final DropTarget dropTarget = new DropTarget(list, DnDConstants.ACTION_MOVE, new DropTargetListener() {
94            @Override
95            public void dragEnter(DropTargetDragEvent dtde) {
96                dragOver(dtde);
97            }
98
99            @Override
100            public void dragOver(DropTargetDragEvent dtde) {
101                dtde.acceptDrag(DnDConstants.ACTION_MOVE);
102            }
103
104            @Override
105            public void dropActionChanged(DropTargetDragEvent dtde) {
106            }
107
108            @Override
109            public void dragExit(DropTargetEvent dte) {
110            }
111
112            @Override
113            public void drop(DropTargetDropEvent dtde) {
114            }
115        }, true);
116        scrollPane = new JScrollPane(list);
117        mainFrame.add(scrollPane, BorderLayout.CENTER);
118        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
119        mainFrame.setLocation(100, 100);
120        mainFrame.setVisible(true);
121    }
122}
123