mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-24 04:54:40 +02:00
7154048: [macosx] At least drag twice, the toolbar can be dragged to the left side
Reviewed-by: anthony, leonidr
This commit is contained in:
parent
3f596509e1
commit
4d0e923a60
10 changed files with 884 additions and 29 deletions
|
@ -669,6 +669,8 @@ public class LWWindowPeer
|
|||
}
|
||||
} else {
|
||||
if (targetPeer != lastMouseEventPeer) {
|
||||
|
||||
if (id != MouseEvent.MOUSE_DRAGGED || lastMouseEventPeer == null) {
|
||||
// lastMouseEventPeer may be null if mouse was out of Java windows
|
||||
if (lastMouseEventPeer != null && lastMouseEventPeer.isEnabled()) {
|
||||
// Sometimes, MOUSE_EXITED is not sent by delegate (or is sent a bit
|
||||
|
@ -693,7 +695,6 @@ public class LWWindowPeer
|
|||
clickCount, popupTrigger, button));
|
||||
}
|
||||
}
|
||||
lastMouseEventPeer = targetPeer;
|
||||
if (targetPeer != null && targetPeer.isEnabled() && id != MouseEvent.MOUSE_ENTERED) {
|
||||
Point newp = targetPeer.windowToLocal(x, y, curWindowPeer);
|
||||
postEvent(new MouseEvent(targetPeer.getTarget(),
|
||||
|
@ -703,6 +704,8 @@ public class LWWindowPeer
|
|||
clickCount, popupTrigger, button));
|
||||
}
|
||||
}
|
||||
lastMouseEventPeer = targetPeer;
|
||||
}
|
||||
// TODO: fill "bdata" member of AWTEvent
|
||||
|
||||
int eventButtonMask = (button > 0)? MouseEvent.getMaskForButton(button) : 0;
|
||||
|
|
|
@ -61,6 +61,7 @@ public class CPlatformWindow extends CFRetainedResource implements PlatformWindo
|
|||
private static native void nativeSetNSWindowMinimizedIcon(long nsWindowPtr, long nsImage);
|
||||
private static native void nativeSetNSWindowRepresentedFilename(long nsWindowPtr, String representedFilename);
|
||||
private static native void nativeSetNSWindowSecurityWarningPositioning(long nsWindowPtr, double x, double y, float biasX, float biasY);
|
||||
private static native void nativeSynthesizeMouseEnteredExitedEvents(long nsWindowPtr);
|
||||
|
||||
private static native int nativeGetScreenNSWindowIsOn_AppKitThread(long nsWindowPtr);
|
||||
|
||||
|
@ -582,6 +583,8 @@ public class CPlatformWindow extends CFRetainedResource implements PlatformWindo
|
|||
}
|
||||
}
|
||||
|
||||
nativeSynthesizeMouseEnteredExitedEvents(nsWindowPtr);
|
||||
|
||||
// 6. Configure stuff #2
|
||||
updateFocusabilityForAutoRequestFocus(true);
|
||||
|
||||
|
@ -791,6 +794,8 @@ public class CPlatformWindow extends CFRetainedResource implements PlatformWindo
|
|||
throw new RuntimeException("Unknown window state: " + windowState);
|
||||
}
|
||||
|
||||
nativeSynthesizeMouseEnteredExitedEvents(nsWindowPtr);
|
||||
|
||||
// NOTE: the SWP.windowState field gets updated to the newWindowState
|
||||
// value when the native notification comes to us
|
||||
}
|
||||
|
|
|
@ -52,9 +52,12 @@
|
|||
BOOL fPAHNeedsToSelect;
|
||||
|
||||
id cglLayer; // is a sublayer of view.layer
|
||||
|
||||
BOOL mouseIsOver;
|
||||
}
|
||||
|
||||
@property (nonatomic, retain) id cglLayer;
|
||||
@property (nonatomic) BOOL mouseIsOver;
|
||||
|
||||
- (id) initWithRect:(NSRect) rect platformView:(jobject)cPlatformView windowLayer:(CALayer*)windowLayer;
|
||||
- (void) deliverJavaMouseEvent: (NSEvent *) event;
|
||||
|
|
|
@ -61,6 +61,7 @@ static BOOL shouldUsePressAndHold() {
|
|||
@synthesize _dropTarget;
|
||||
@synthesize _dragSource;
|
||||
@synthesize cglLayer;
|
||||
@synthesize mouseIsOver;
|
||||
|
||||
// Note: Must be called on main (AppKit) thread only
|
||||
- (id) initWithRect: (NSRect) rect
|
||||
|
@ -81,6 +82,8 @@ AWT_ASSERT_APPKIT_THREAD;
|
|||
fInPressAndHold = NO;
|
||||
fPAHNeedsToSelect = NO;
|
||||
|
||||
mouseIsOver = NO;
|
||||
|
||||
if (windowLayer != nil) {
|
||||
self.cglLayer = windowLayer;
|
||||
[self setWantsLayer: YES];
|
||||
|
@ -299,6 +302,16 @@ AWT_ASSERT_APPKIT_THREAD;
|
|||
*/
|
||||
|
||||
-(void) deliverJavaMouseEvent: (NSEvent *) event {
|
||||
|
||||
NSEventType type = [event type];
|
||||
|
||||
// check synthesized mouse entered/exited events
|
||||
if ((type == NSMouseEntered && mouseIsOver) || (type == NSMouseExited && !mouseIsOver)) {
|
||||
return;
|
||||
}else if ((type == NSMouseEntered && !mouseIsOver) || (type == NSMouseExited && mouseIsOver)) {
|
||||
mouseIsOver = !mouseIsOver;
|
||||
}
|
||||
|
||||
[AWTToolkit eventCountPlusPlus];
|
||||
|
||||
JNIEnv *env = [ThreadUtilities getJNIEnv];
|
||||
|
@ -306,7 +319,6 @@ AWT_ASSERT_APPKIT_THREAD;
|
|||
NSPoint eventLocation = [event locationInWindow];
|
||||
NSPoint localPoint = [self convertPoint: eventLocation fromView: nil];
|
||||
NSPoint absP = [NSEvent mouseLocation];
|
||||
NSEventType type = [event type];
|
||||
|
||||
// Convert global numbers between Cocoa's coordinate system and Java.
|
||||
// TODO: need consitent way for doing that both with global as well as with local coordinates.
|
||||
|
|
|
@ -56,6 +56,7 @@
|
|||
contentView:(NSView *)contentView;
|
||||
|
||||
- (void) adjustGrowBoxWindow;
|
||||
- (BOOL) isTopmostWindowUnderMouse;
|
||||
@end
|
||||
|
||||
#endif _AWTWINDOW_H
|
||||
|
|
|
@ -170,6 +170,66 @@ AWT_ASSERT_APPKIT_THREAD;
|
|||
return self;
|
||||
}
|
||||
|
||||
// checks that this window is under the mouse cursor and this point is not overlapped by others windows
|
||||
- (BOOL) isTopmostWindowUnderMouse {
|
||||
|
||||
int currentWinID = [self windowNumber];
|
||||
|
||||
NSRect screenRect = [[NSScreen mainScreen] frame];
|
||||
NSPoint nsMouseLocation = [NSEvent mouseLocation];
|
||||
CGPoint cgMouseLocation = CGPointMake(nsMouseLocation.x, screenRect.size.height - nsMouseLocation.y);
|
||||
|
||||
NSMutableArray *windows = (NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
|
||||
|
||||
|
||||
for (NSDictionary *window in windows) {
|
||||
int layer = [[window objectForKey:(id)kCGWindowLayer] intValue];
|
||||
if (layer == 0) {
|
||||
int winID = [[window objectForKey:(id)kCGWindowNumber] intValue];
|
||||
CGRect rect;
|
||||
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[window objectForKey:(id)kCGWindowBounds], &rect);
|
||||
if (CGRectContainsPoint(rect, cgMouseLocation)) {
|
||||
return currentWinID == winID;
|
||||
} else if (currentWinID == winID) {
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void) synthesizeMouseEnteredExitedEvents {
|
||||
|
||||
int eventType = 0;
|
||||
BOOL isUnderMouse = [self isTopmostWindowUnderMouse];
|
||||
BOOL mouseIsOver = [[self contentView] mouseIsOver];
|
||||
|
||||
if (isUnderMouse && !mouseIsOver) {
|
||||
eventType = NSMouseEntered;
|
||||
} else if (!isUnderMouse && mouseIsOver) {
|
||||
eventType = NSMouseExited;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
NSPoint screenLocation = [NSEvent mouseLocation];
|
||||
NSPoint windowLocation = [self convertScreenToBase: screenLocation];
|
||||
int modifierFlags = (eventType == NSMouseEntered) ? NSMouseEnteredMask : NSMouseExitedMask;
|
||||
|
||||
NSEvent *mouseEvent = [NSEvent enterExitEventWithType: eventType
|
||||
location: windowLocation
|
||||
modifierFlags: modifierFlags
|
||||
timestamp: 0
|
||||
windowNumber: [self windowNumber]
|
||||
context: nil
|
||||
eventNumber: 0
|
||||
trackingNumber: 0
|
||||
userData: nil
|
||||
];
|
||||
|
||||
[[self contentView] deliverJavaMouseEvent: mouseEvent];
|
||||
}
|
||||
|
||||
- (void) dealloc {
|
||||
AWT_ASSERT_APPKIT_THREAD;
|
||||
|
||||
|
@ -669,6 +729,8 @@ AWT_ASSERT_NOT_APPKIT_THREAD;
|
|||
// ensure we repaint the whole window after the resize operation
|
||||
// (this will also re-enable screen updates, which were disabled above)
|
||||
// TODO: send PaintEvent
|
||||
|
||||
[window synthesizeMouseEnteredExitedEvents];
|
||||
}];
|
||||
|
||||
JNF_COCOA_EXIT(env);
|
||||
|
@ -897,6 +959,27 @@ AWT_ASSERT_NOT_APPKIT_THREAD;
|
|||
JNF_COCOA_EXIT(env);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: sun_lwawt_macosx_CPlatformWindow
|
||||
* Method: nativeSynthesizeMouseEnteredExitedEvents
|
||||
* Signature: (J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSynthesizeMouseEnteredExitedEvents
|
||||
(JNIEnv *env, jclass clazz, jlong windowPtr)
|
||||
{
|
||||
JNF_COCOA_ENTER(env);
|
||||
AWT_ASSERT_NOT_APPKIT_THREAD;
|
||||
|
||||
AWTWindow *window = OBJC(windowPtr);
|
||||
[JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
|
||||
AWT_ASSERT_APPKIT_THREAD;
|
||||
|
||||
[window synthesizeMouseEnteredExitedEvents];
|
||||
}];
|
||||
|
||||
JNF_COCOA_EXIT(env);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: sun_lwawt_macosx_CPlatformWindow
|
||||
* Method: nativeGetScreenNSWindowIsOn_AppKitThread
|
||||
|
|
|
@ -0,0 +1,267 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7154048
|
||||
* @summary Window created under a mouse does not receive mouse enter event.
|
||||
* Mouse Entered/Exited events should be generated during dragging the window
|
||||
* out of the frame and to the frame.
|
||||
* @library ../../regtesthelpers
|
||||
* @build Util
|
||||
* @author alexandr.scherbatiy area=awt.event
|
||||
* @run main DragWindowOutOfFrameTest
|
||||
*/
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import sun.awt.SunToolkit;
|
||||
|
||||
import test.java.awt.regtesthelpers.Util;
|
||||
|
||||
public class DragWindowOutOfFrameTest {
|
||||
|
||||
private static volatile int dragWindowMouseEnteredCount = 0;
|
||||
private static volatile int dragWindowMouseExitedCount = 0;
|
||||
private static volatile int dragWindowMouseReleasedCount = 0;
|
||||
private static volatile int buttonMouseEnteredCount = 0;
|
||||
private static volatile int buttonMouseExitedCount = 0;
|
||||
private static volatile int labelMouseEnteredCount = 0;
|
||||
private static volatile int labelMouseExitedCount = 0;
|
||||
private static volatile int labelMouseReleasedCount = 0;
|
||||
private static MyDragWindow dragWindow;
|
||||
private static JLabel label;
|
||||
private static JButton button;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
createAndShowGUI();
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
|
||||
Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {
|
||||
|
||||
@Override
|
||||
public Point call() throws Exception {
|
||||
return getCenterPoint(label);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
robot.mouseMove(pointToClick.x, pointToClick.y);
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
toolkit.realSync();
|
||||
|
||||
if (dragWindowMouseEnteredCount != 1 && dragWindowMouseExitedCount != 0) {
|
||||
throw new RuntimeException(
|
||||
"Wrong number mouse Entered/Exited events on Drag Window!");
|
||||
}
|
||||
|
||||
Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {
|
||||
|
||||
@Override
|
||||
public Point call() throws Exception {
|
||||
label.addMouseListener(new LabelMouseListener());
|
||||
button.addMouseListener(new ButtonMouseListener());
|
||||
return getCenterPoint(button);
|
||||
}
|
||||
});
|
||||
|
||||
robot.mouseMove(450, pointToClick.y);
|
||||
toolkit.realSync();
|
||||
|
||||
if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {
|
||||
throw new RuntimeException(
|
||||
"Wrong number Mouse Entered/Exited events on label!");
|
||||
}
|
||||
|
||||
robot.mouseMove(450, pointToDrag.y);
|
||||
toolkit.realSync();
|
||||
|
||||
if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {
|
||||
throw new RuntimeException(
|
||||
"Wrong number Mouse Entered/Exited events on label!");
|
||||
}
|
||||
|
||||
if (buttonMouseEnteredCount != 0 && buttonMouseExitedCount != 0) {
|
||||
throw new RuntimeException(
|
||||
"Wrong number Mouse Entered/Exited events on button!");
|
||||
}
|
||||
|
||||
robot.mouseMove(pointToDrag.y, pointToDrag.y);
|
||||
toolkit.realSync();
|
||||
|
||||
if (buttonMouseEnteredCount != 1 && buttonMouseExitedCount != 0) {
|
||||
throw new RuntimeException(
|
||||
"Wrong number Mouse Entered/Exited events on button!");
|
||||
}
|
||||
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
toolkit.realSync();
|
||||
|
||||
if (labelMouseReleasedCount != 1) {
|
||||
throw new RuntimeException("No MouseReleased event on label!");
|
||||
}
|
||||
}
|
||||
|
||||
private static Point getCenterPoint(Component comp) {
|
||||
Point p = comp.getLocationOnScreen();
|
||||
Rectangle rect = comp.getBounds();
|
||||
return new Point(p.x + rect.width / 2, p.y + rect.height / 2);
|
||||
}
|
||||
|
||||
private static void createAndShowGUI() {
|
||||
|
||||
JFrame frame = new JFrame("Main Frame");
|
||||
frame.setLocation(100, 100);
|
||||
frame.setSize(300, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
label = new JLabel("Label");
|
||||
|
||||
DragWindowCreationMouseListener listener = new DragWindowCreationMouseListener(frame);
|
||||
label.addMouseListener(listener);
|
||||
label.addMouseMotionListener(listener);
|
||||
|
||||
button = new JButton("Button");
|
||||
Panel panel = new Panel(new BorderLayout());
|
||||
|
||||
panel.add(label, BorderLayout.NORTH);
|
||||
panel.add(button, BorderLayout.CENTER);
|
||||
|
||||
frame.getContentPane().add(panel);
|
||||
frame.setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
private static Point getAbsoluteLocation(MouseEvent e) {
|
||||
return new Point(e.getXOnScreen(), e.getYOnScreen());
|
||||
}
|
||||
|
||||
static class MyDragWindow extends Window {
|
||||
|
||||
public MyDragWindow(Window parent, Point location) {
|
||||
super(parent);
|
||||
setSize(500, 300);
|
||||
setVisible(true);
|
||||
JPanel panel = new JPanel();
|
||||
add(panel);
|
||||
setLocation(location.x - 250, location.y - 150);
|
||||
addMouseListener(new DragWindowMouseListener());
|
||||
}
|
||||
|
||||
void dragTo(Point point) {
|
||||
setLocation(point.x - 250, point.y - 150);
|
||||
}
|
||||
}
|
||||
|
||||
static class DragWindowCreationMouseListener extends MouseAdapter {
|
||||
|
||||
Point origin;
|
||||
Window parent;
|
||||
|
||||
public DragWindowCreationMouseListener(Window parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
if (dragWindow == null) {
|
||||
dragWindow = new MyDragWindow(parent, getAbsoluteLocation(e));
|
||||
} else {
|
||||
dragWindow.setVisible(true);
|
||||
dragWindow.dragTo(getAbsoluteLocation(e));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
labelMouseReleasedCount++;
|
||||
if (dragWindow != null) {
|
||||
dragWindow.setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
if (dragWindow != null) {
|
||||
dragWindow.dragTo(getAbsoluteLocation(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class DragWindowMouseListener extends MouseAdapter {
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
dragWindowMouseEnteredCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
dragWindowMouseExitedCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
dragWindowMouseReleasedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
static class LabelMouseListener extends MouseAdapter {
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
labelMouseEnteredCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
labelMouseExitedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
static class ButtonMouseListener extends MouseAdapter {
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
buttonMouseEnteredCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
buttonMouseExitedCount++;
|
||||
}
|
||||
}
|
||||
}
|
219
jdk/test/java/awt/Mouse/EnterExitEvents/DragWindowTest.java
Normal file
219
jdk/test/java/awt/Mouse/EnterExitEvents/DragWindowTest.java
Normal file
|
@ -0,0 +1,219 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7154048
|
||||
* @summary Window created under a mouse does not receive mouse enter event.
|
||||
* Mouse Entered/Exited events are wrongly generated during dragging the window
|
||||
* from one component to another
|
||||
* @library ../../regtesthelpers
|
||||
* @build Util
|
||||
* @author alexandr.scherbatiy area=awt.event
|
||||
* @run main DragWindowTest
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import sun.awt.SunToolkit;
|
||||
|
||||
import test.java.awt.regtesthelpers.Util;
|
||||
|
||||
public class DragWindowTest {
|
||||
|
||||
private static volatile int dragWindowMouseEnteredCount = 0;
|
||||
private static volatile int dragWindowMouseReleasedCount = 0;
|
||||
private static volatile int buttonMouseEnteredCount = 0;
|
||||
private static volatile int labelMouseReleasedCount = 0;
|
||||
private static MyDragWindow dragWindow;
|
||||
private static JLabel label;
|
||||
private static JButton button;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
createAndShowGUI();
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
|
||||
Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {
|
||||
|
||||
@Override
|
||||
public Point call() throws Exception {
|
||||
return getCenterPoint(label);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
robot.mouseMove(pointToClick.x, pointToClick.y);
|
||||
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||
toolkit.realSync();
|
||||
|
||||
if (dragWindowMouseEnteredCount != 1) {
|
||||
throw new RuntimeException("No MouseEntered event on Drag Window!");
|
||||
}
|
||||
|
||||
Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {
|
||||
|
||||
@Override
|
||||
public Point call() throws Exception {
|
||||
button.addMouseListener(new ButtonMouseListener());
|
||||
return getCenterPoint(button);
|
||||
}
|
||||
});
|
||||
|
||||
robot.mouseMove(pointToDrag.x, pointToDrag.y);
|
||||
toolkit.realSync();
|
||||
|
||||
if (buttonMouseEnteredCount != 0) {
|
||||
throw new RuntimeException("Extra MouseEntered event on button!");
|
||||
}
|
||||
|
||||
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||
toolkit.realSync();
|
||||
|
||||
if (labelMouseReleasedCount != 1) {
|
||||
throw new RuntimeException("No MouseReleased event on label!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static Point getCenterPoint(Component comp) {
|
||||
Point p = comp.getLocationOnScreen();
|
||||
Rectangle rect = comp.getBounds();
|
||||
return new Point(p.x + rect.width / 2, p.y + rect.height / 2);
|
||||
}
|
||||
|
||||
private static void createAndShowGUI() {
|
||||
|
||||
JFrame frame = new JFrame("Main Frame");
|
||||
frame.setSize(300, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
label = new JLabel("Label");
|
||||
|
||||
LabelMouseListener listener = new LabelMouseListener(frame);
|
||||
label.addMouseListener(listener);
|
||||
label.addMouseMotionListener(listener);
|
||||
|
||||
button = new JButton("Button");
|
||||
Panel panel = new Panel(new BorderLayout());
|
||||
|
||||
panel.add(label, BorderLayout.NORTH);
|
||||
panel.add(button, BorderLayout.CENTER);
|
||||
|
||||
frame.getContentPane().add(panel);
|
||||
frame.setVisible(true);
|
||||
|
||||
}
|
||||
|
||||
private static Point getAbsoluteLocation(MouseEvent e) {
|
||||
return new Point(e.getXOnScreen(), e.getYOnScreen());
|
||||
}
|
||||
|
||||
static class MyDragWindow extends Window {
|
||||
|
||||
static int d = 30;
|
||||
|
||||
public MyDragWindow(Window parent, Point location) {
|
||||
super(parent);
|
||||
setSize(150, 150);
|
||||
setVisible(true);
|
||||
JPanel panel = new JPanel();
|
||||
add(panel);
|
||||
setLocation(location.x - d, location.y - d);
|
||||
addMouseListener(new DragWindowMouseListener());
|
||||
}
|
||||
|
||||
void dragTo(Point point) {
|
||||
setLocation(point.x - d, point.y - d);
|
||||
}
|
||||
}
|
||||
|
||||
static class LabelMouseListener extends MouseAdapter {
|
||||
|
||||
Point origin;
|
||||
Window parent;
|
||||
|
||||
public LabelMouseListener(Window parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
if (dragWindow == null) {
|
||||
dragWindow = new MyDragWindow(parent, getAbsoluteLocation(e));
|
||||
} else {
|
||||
dragWindow.setVisible(true);
|
||||
dragWindow.dragTo(getAbsoluteLocation(e));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
labelMouseReleasedCount++;
|
||||
if (dragWindow != null) {
|
||||
dragWindow.setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
if (dragWindow != null) {
|
||||
dragWindow.dragTo(getAbsoluteLocation(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class DragWindowMouseListener extends MouseAdapter {
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
dragWindowMouseEnteredCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
dragWindowMouseReleasedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
static class ButtonMouseListener extends MouseAdapter {
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
buttonMouseEnteredCount++;
|
||||
}
|
||||
}
|
||||
}
|
232
jdk/test/java/awt/Mouse/EnterExitEvents/ResizingFrameTest.java
Normal file
232
jdk/test/java/awt/Mouse/EnterExitEvents/ResizingFrameTest.java
Normal file
|
@ -0,0 +1,232 @@
|
|||
/*
|
||||
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7154048
|
||||
* @summary Programmatically resized window does not receive mouse entered/exited events
|
||||
* @author alexandr.scherbatiy area=awt.event
|
||||
* @run main ResizingFrameTest
|
||||
*/
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import sun.awt.SunToolkit;
|
||||
|
||||
public class ResizingFrameTest {
|
||||
|
||||
private static volatile int mouseEnteredCount = 0;
|
||||
private static volatile int mouseExitedCount = 0;
|
||||
private static JFrame frame;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
|
||||
Robot robot = new Robot();
|
||||
robot.setAutoDelay(50);
|
||||
robot.mouseMove(100, 100);
|
||||
|
||||
// create a frame under the mouse cursor
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
createAndShowGUI();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
toolkit.realSync();
|
||||
|
||||
if (mouseEnteredCount != 1 || mouseExitedCount != 0) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
|
||||
// iconify frame
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
frame.setExtendedState(Frame.ICONIFIED);
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
robot.delay(200);
|
||||
|
||||
if (mouseEnteredCount != 1 || mouseExitedCount != 1) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
|
||||
// deiconify frame
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
frame.setExtendedState(Frame.NORMAL);
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
robot.delay(200);
|
||||
|
||||
if (mouseEnteredCount != 2 || mouseExitedCount != 1) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
|
||||
// move the mouse out of the frame
|
||||
robot.mouseMove(500, 500);
|
||||
toolkit.realSync();
|
||||
robot.delay(200);
|
||||
|
||||
if (mouseEnteredCount != 2 || mouseExitedCount != 2) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
|
||||
// maximize the frame
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
robot.delay(200);
|
||||
|
||||
if (mouseEnteredCount != 3 || mouseExitedCount != 2) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
|
||||
|
||||
// demaximize the frame
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
frame.setExtendedState(Frame.NORMAL);
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
robot.delay(200);
|
||||
|
||||
if (mouseEnteredCount != 3 || mouseExitedCount != 3) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
|
||||
}
|
||||
|
||||
// move the frame under the mouse
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
frame.setLocation(400, 400);
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
robot.delay(200);
|
||||
|
||||
if (mouseEnteredCount != 4 || mouseExitedCount != 3) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
|
||||
// move the frame out of the mouse
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
frame.setLocation(100, 100);
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
robot.delay(400);
|
||||
|
||||
if (mouseEnteredCount != 4 || mouseExitedCount != 4) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
|
||||
// enlarge the frame bounds
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
frame.setBounds(100, 100, 800, 800);
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
robot.delay(200);
|
||||
|
||||
if (mouseEnteredCount != 5 || mouseExitedCount != 4) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
|
||||
// make the frame bounds smaller
|
||||
SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
frame.setBounds(100, 100, 200, 300);
|
||||
}
|
||||
});
|
||||
|
||||
toolkit.realSync();
|
||||
robot.delay(400);
|
||||
|
||||
|
||||
if (mouseEnteredCount != 5 || mouseExitedCount != 5) {
|
||||
throw new RuntimeException("No Mouse Entered/Exited events!");
|
||||
}
|
||||
}
|
||||
|
||||
private static void createAndShowGUI() {
|
||||
|
||||
frame = new JFrame("Main Frame");
|
||||
frame.setSize(300, 200);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
frame.addMouseListener(new MouseAdapter() {
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
mouseEnteredCount++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
mouseExitedCount++;
|
||||
}
|
||||
});
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
}
|
|
@ -600,4 +600,34 @@ public final class Util {
|
|||
time, printEvent);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Invokes the <code>task</code> on the EDT thread.
|
||||
*
|
||||
* @return result of the <code>task</code>
|
||||
*/
|
||||
public static <T> T invokeOnEDT(final java.util.concurrent.Callable<T> task) throws Exception {
|
||||
final java.util.List<T> result = new java.util.ArrayList<T>(1);
|
||||
final Exception[] exception = new Exception[1];
|
||||
|
||||
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
result.add(task.call());
|
||||
} catch (Exception e) {
|
||||
exception[0] = e;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (exception[0] != null) {
|
||||
throw exception[0];
|
||||
}
|
||||
|
||||
return result.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue