MouseVisualizer.java revision 13978:1993af50385d
1/* 2 * Copyright (c) 1997, 2016, 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 */ 23package org.netbeans.jemmy.util; 24 25import java.awt.Dialog; 26import java.awt.Frame; 27import java.awt.Point; 28 29import org.netbeans.jemmy.JemmyProperties; 30import org.netbeans.jemmy.Timeouts; 31import org.netbeans.jemmy.drivers.input.MouseRobotDriver; 32import org.netbeans.jemmy.operators.Operator; 33import org.netbeans.jemmy.operators.WindowOperator; 34 35/** 36 * 37 * Does 38 * {@code super.activate(org.netbeans.jemmy.operators.WindowOperator)}. 39 * Then, if java version is appropriate (1.3 or later) activates windows by 40 * robot mouse click on border. 41 * 42 * @see 43 * org.netbeans.jemmy.operators.Operator#setVisualizer(Operator.ComponentVisualizer) 44 * @see org.netbeans.jemmy.operators.Operator.ComponentVisualizer 45 * 46 * <BR><BR>Timeouts used: <BR> 47 * MouseVisualiser.BeforeClickTimeout - time to let a window manager to move a 48 * window as it wants<BR> 49 * 50 * @author Alexandre Iline (alexandre.iline@oracle.com) 51 * 52 */ 53public class MouseVisualizer extends DefaultVisualizer { 54 55 private static final long BEFORE_CLICK = 100; 56 57 /** 58 * A constant used to inform that window activating click needs to performed 59 * on the <b>top</b> side of frame. 60 * 61 * @see #MouseVisualizer() 62 */ 63 public static final int TOP = 0; 64 65 /** 66 * A constant used to inform that window activating click needs to performed 67 * on the <b>botton</b> side of frame. 68 * 69 * @see #MouseVisualizer() 70 */ 71 public static final int BOTTOM = 1; 72 73 /** 74 * A constant used to inform that window activating click needs to performed 75 * on the <b>left</b> side of frame. 76 * 77 * @see #MouseVisualizer() 78 */ 79 public static final int LEFT = 2; 80 81 /** 82 * A constant used to inform that window activating click needs to performed 83 * on the <b>right</b> side of frame. 84 * 85 * @see #MouseVisualizer() 86 */ 87 public static final int RIGHT = 3; 88 89 private int place = 0; 90 private double pointLocation = 0; 91 private int depth = 0; 92 93 /** 94 * Creates a visualizer which clicks on (0, 0) window coords. 95 */ 96 public MouseVisualizer() { 97 } 98 99 /** 100 * Creates a visualizer which clicks on window border. In case if 101 * {@code place == BOTTOM}, for example clicks on (width * 102 * pointLocation, height - depth) coordinates. 103 * 104 * @param place One of the predefined value: TOP, BOTTOM, LEFT, RIGHT 105 * @param pointLocation Proportional coordinates to click. 106 * @param depth Distance from the border. 107 * @param checkMouse Check if there is any java component under mouse 108 * (currently ignored) 109 */ 110 public MouseVisualizer(int place, double pointLocation, int depth, boolean checkMouse) { 111 this.place = place; 112 this.pointLocation = pointLocation; 113 this.depth = depth; 114 } 115 116 static { 117 Timeouts.initDefault("MouseVisualiser.BeforeClickTimeout", BEFORE_CLICK); 118 } 119 120 @Override 121 protected boolean isWindowActive(WindowOperator winOper) { 122 return (super.isWindowActive(winOper) 123 && (winOper.getSource() instanceof Frame 124 || winOper.getSource() instanceof Dialog)); 125 } 126 127 @Override 128 protected void makeWindowActive(WindowOperator winOper) { 129 JemmyProperties.getCurrentTimeouts(). 130 create("MouseVisualiser.BeforeClickTimeout").sleep(); 131 super.makeWindowActive(winOper); 132 if (!System.getProperty("java.version").startsWith("1.2")) { 133 Point p = getClickPoint(winOper); 134 new MouseRobotDriver(winOper.getTimeouts().create("EventDispatcher.RobotAutoDelay")). 135 clickMouse(winOper, p.x, p.y, 136 1, Operator.getDefaultMouseButton(), 137 0, 138 winOper.getTimeouts().create("ComponentOperator.MouseClickTimeout")); 139 } 140 } 141 142 private Point getClickPoint(WindowOperator win) { 143 int x, y; 144 if (place == LEFT 145 || place == RIGHT) { 146 y = ((int) (win.getHeight() * pointLocation - 1)); 147 if (place == RIGHT) { 148 x = win.getWidth() - 1 - depth; 149 } else { 150 x = depth; 151 } 152 } else { 153 x = ((int) (win.getWidth() * pointLocation - 1)); 154 if (place == BOTTOM) { 155 y = win.getHeight() - 1 - depth; 156 } else { 157 y = depth; 158 } 159 } 160 if (x < 0) { 161 x = 0; 162 } 163 if (x >= win.getWidth()) { 164 x = win.getWidth() - 1; 165 } 166 if (y < 0) { 167 y = 0; 168 } 169 if (y >= win.getHeight()) { 170 y = win.getHeight() - 1; 171 } 172 return new Point(x, y); 173 } 174} 175