1 /*
2  * Copyright (c) 2013, 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  * @bug 8027628
27  * @author Oleg Pekhovskiy
28  * @summary JWindow jumps to (0, 0) after mouse clicked
29  * @run main TopLevelLocation
30  */
31 
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.EventQueue;
35 import java.awt.Point;
36 import java.awt.Robot;
37 import java.awt.event.MouseAdapter;
38 import java.awt.event.MouseEvent;
39 import javax.swing.JFrame;
40 import javax.swing.JWindow;
41 
42 public class TopLevelLocation {
43 
44     private static JFrame frame;
45     private static JWindow window;
46     private static boolean passed = true;
47 
main(String[] args)48     public static void main(String[] args) throws Exception {
49         EventQueue.invokeAndWait(() -> {
50             frame = new JFrame();
51             frame.getContentPane().setBackground(Color.PINK);
52             frame.setBounds(100, 100, 500, 400);
53             frame.setUndecorated(true);
54             frame.setVisible(true);
55             window = new JWindow(frame);
56             window.setBackground(Color.BLUE);
57             window.setAlwaysOnTop(true);
58             window.setBounds(200, 200, 200, 200);
59             window.addMouseListener(new MouseAdapter() {
60                 private Point dragOrigin = null;
61                 private Dimension origSize = null;
62                 private Point origLoc = null;
63                 private Point lastLoc = null;
64                 private boolean left = false;
65                 private boolean top = false;
66                 private boolean bottom = false;
67                 private boolean right = false;
68 
69                 @Override
70                 public void mousePressed(MouseEvent e) {
71                     System.out.println("mousePressed");
72                     dragOrigin = e.getLocationOnScreen();
73                     origSize = window.getSize();
74                     origLoc = window.getLocationOnScreen();
75                     if (lastLoc != null) {
76                         System.out.println("SET LOCATION: " + lastLoc);
77                         System.out.println("CURRENT LOCATION: " + origLoc);
78                         if (lastLoc.x != origLoc.x || lastLoc.y != origLoc.y) {
79                             passed = false;
80                         }
81                     }
82                     right = (origLoc.x + window.getWidth() - dragOrigin.x) < 5;
83                     left = !right && dragOrigin.x - origLoc.x < 5;
84                     bottom = (origLoc.y + window.getHeight() - dragOrigin.y) < 5;
85                     top = !bottom && dragOrigin.y - origLoc.y < 5;
86                 }
87 
88                 @Override
89                 public void mouseDragged(MouseEvent e) {
90                     System.out.println("mouseDragged");
91                     resize(e);
92                 }
93 
94                 @Override
95                 public void mouseReleased(MouseEvent e) {
96                     System.out.println("mouseReleased");
97                     resize(e);
98                 }
99 
100                 void resize(MouseEvent e) {
101                     Point dragDelta = e.getLocationOnScreen();
102                     dragDelta.translate(-dragOrigin.x, -dragOrigin.y);
103                     Point newLoc = new Point(origLoc);
104                     newLoc.translate(dragDelta.x, dragDelta.y);
105                     Dimension newSize = new Dimension(origSize);
106                     if (left || right) {
107                         newSize.width += right ? dragDelta.x : -dragDelta.x;
108                     }
109                     if (top || bottom) {
110                         newSize.height += bottom ? dragDelta.y : -dragDelta.y;
111                     }
112                     if (right || (top || bottom) && !left) {
113                         newLoc.x = origLoc.x;
114                     }
115                     if (bottom || (left || right) && !top) {
116                         newLoc.y = origLoc.y;
117                     }
118                     window.setBounds(newLoc.x, newLoc.y, newSize.width, newSize.height);
119                     lastLoc = newLoc;
120                 }
121             });
122             window.setVisible(true);
123         });
124         Thread.sleep(500);
125         Dimension size = window.getSize();
126         Point location = window.getLocation();
127         Robot robot = new Robot();
128         robot.setAutoDelay(200);
129         robot.setAutoWaitForIdle(true);
130         robot.waitForIdle();
131         robot.mouseMove(location.x + size.height - 2, location.y + size.width - 2);
132         robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
133         robot.mouseMove(location.x + size.height, location.y + size.width);
134         robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
135         robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
136         robot.mouseMove(location.x + size.height + 2, location.y + size.width + 2);
137         robot.mouseRelease(MouseEvent.BUTTON1_DOWN_MASK);
138         Thread.sleep(500);
139         frame.dispose();
140         if (!passed) {
141             throw new RuntimeException("TEST FAILED: Location doesn't match!");
142         }
143         System.out.println("TEST PASSED!");
144     }
145 }
146 
147