1 /*
2  * Copyright (c) 2004, 2006, 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 4992908
27   @summary Need way to get location of MouseEvent in screen coordinates
28   @author Andrei.Dmitriev area=event
29   @run applet MouseWheelEventAbsoluteCoordsTest.html
30 */
31 
32 import java.applet.Applet;
33 import java.awt.*;
34 import java.awt.event.*;
35 import test.java.awt.regtesthelpers.Util;
36 
37 // The test consits of several parts:
38 // 1. create MouseWheelEvent with new Ctor and checking get(X|Y)OnScreen(),
39 // getLocationOnScreen(), get(X|Y), getPoint().
40 // 2. create MouseWheelEvent with old Ctor and checking get(X|Y)OnScreen(),
41 // getLocationOnScreen(),  get(X|Y), getPoint() .
42 
43 
44 public class MouseWheelEventAbsoluteCoordsTest extends Applet implements MouseWheelListener
45 {
46     Frame frame = new Frame("MouseWheelEvent Test Frame");
47 
48     Point mousePositionOnScreen = new Point(200, 200);
49     Point mousePosition = new Point(100, 100);
init()50     public void init()
51     {
52 
53         this.setLayout (new BorderLayout ());
54 
55         frame.addMouseWheelListener(this);
56     }//End  init()
57 
start()58     public void start ()
59     {
60         //Get things going.  Request focus, set size, et cetera
61         frame.setSize (200,200);
62         frame.setLocation(47, 47);
63 
64         frame.setVisible(true);
65         validate();
66         try {
67             Util.waitForIdle(new Robot());
68         }catch (Exception e){
69             throw new RuntimeException("Test failed.", e);
70         }
71 
72         // use new MouseEvent's Ctor with user-defined absolute
73         // coordinates
74         System.out.println("New Ctor Stage");
75         postMouseWheelEventNewCtor(MouseEvent.MOUSE_CLICKED);
76 
77         // now we gonna use old MouseEvent's Ctor thus absolute
78         // position calculates as frame's location + relative coords
79         // of the event.
80         mousePositionOnScreen = new Point(frame.getLocationOnScreen().x + mousePosition.x,
81                                           frame.getLocationOnScreen().y + mousePosition.y);
82         System.out.println("Old Ctor Stage");
83         postMouseWheelEventOldCtor(MouseEvent.MOUSE_CLICKED);
84 
85     }// start()
mouseWheelMoved(MouseWheelEvent e)86     public void mouseWheelMoved(MouseWheelEvent e){
87         checkEventAbsolutePosition(e, "MouseWheelMoved OK");
88     };
89 
postMouseWheelEventNewCtor(int MouseEventType)90     public void postMouseWheelEventNewCtor(int MouseEventType)    {
91         MouseWheelEvent me = new MouseWheelEvent(frame,
92                                                  MouseEventType,
93                                                  System.currentTimeMillis(),
94                                                  MouseEvent.BUTTON1_DOWN_MASK,
95                                                  mousePosition.x, mousePosition.y,
96                                                  mousePositionOnScreen.x,
97                                                  mousePositionOnScreen.y,
98                                                  1,
99                                                  false,  //popupTrigger
100                                                  MouseWheelEvent.WHEEL_UNIT_SCROLL,
101                                                  1,  //scrollAmount
102                                                  1  //wheelRotation
103                                                  );
104         frame.dispatchEvent( ( AWTEvent )me );
105     }
106 
postMouseWheelEventOldCtor(int MouseEventType)107     public void postMouseWheelEventOldCtor(int MouseEventType)    {
108         MouseWheelEvent meOld = new MouseWheelEvent(frame,
109                                                  MouseEventType,
110                                                  System.currentTimeMillis(),
111                                                  MouseEvent.BUTTON1_DOWN_MASK,
112                                                  mousePosition.x, mousePosition.y,
113                                                  1,
114                                                  false,  //popupTrigger
115                                                  MouseWheelEvent.WHEEL_UNIT_SCROLL,
116                                                  1,  //scrollAmount
117                                                  1  //wheelRotation
118                                                  );
119         frame.dispatchEvent( ( AWTEvent )meOld );
120     }
121 
checkEventAbsolutePosition(MouseEvent evt, String message)122     public void checkEventAbsolutePosition(MouseEvent evt, String message){
123         //        if (newCtorUsed){
124             if (evt.getXOnScreen() != mousePositionOnScreen.x ||
125                 evt.getYOnScreen() != mousePositionOnScreen.y ||
126                 !evt.getLocationOnScreen().equals( mousePositionOnScreen )  ){
127                 throw new RuntimeException("get(X|Y)OnScreen() or getPointOnScreen() work incorrectly");
128             }
129 
130             if (evt.getX() != mousePosition.x ||
131                 evt.getY() != mousePosition.y ||
132                 !evt.getPoint().equals( mousePosition )  ){
133                 throw new RuntimeException("get(X|Y)() or getPoint() work incorrectly");
134             }
135         System.out.println(message);
136     }
137 }// class
138