001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s): Alexandre Iline.
025: *
026: * The Original Software is the Jemmy library.
027: * The Initial Developer of the Original Software is Alexandre Iline.
028: * All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: *
042: *
043: * $Id$ $Revision$ $Date$
044: *
045: */
046:
047: package org.netbeans.jemmy.util;
048:
049: import org.netbeans.jemmy.JemmyProperties;
050: import org.netbeans.jemmy.TestOut;
051: import org.netbeans.jemmy.TimeoutExpiredException;
052: import org.netbeans.jemmy.Timeouts;
053:
054: import org.netbeans.jemmy.drivers.DriverManager;
055:
056: import org.netbeans.jemmy.drivers.input.MouseRobotDriver;
057:
058: import org.netbeans.jemmy.operators.ComponentOperator;
059: import org.netbeans.jemmy.operators.JInternalFrameOperator;
060: import org.netbeans.jemmy.operators.JScrollPaneOperator;
061: import org.netbeans.jemmy.operators.JTabbedPaneOperator;
062: import org.netbeans.jemmy.operators.Operator.ComponentVisualizer;
063: import org.netbeans.jemmy.operators.WindowOperator;
064:
065: import java.awt.Component;
066: import java.awt.Container;
067: import java.awt.Dialog;
068: import java.awt.Frame;
069: import java.awt.Point;
070:
071: import javax.swing.JInternalFrame;
072: import javax.swing.JScrollPane;
073: import javax.swing.JTabbedPane;
074:
075: /**
076: *
077: * Does <code>super.activate(org.netbeans.jemmy.operators.WindowOperator)</code>.
078: * Then, if java version is appropriate (1.3 or later) activates windows by robot mouse click on border.
079: *
080: * @see org.netbeans.jemmy.operators.Operator#setVisualizer(Operator.ComponentVisualizer)
081: * @see org.netbeans.jemmy.operators.Operator.ComponentVisualizer
082: *
083: * <BR><BR>Timeouts used: <BR>
084: * MouseVisualiser.BeforeClickTimeout - time to let a window manager to move a window as it wants<BR>
085: *
086: * @author Alexandre Iline (alexandre.iline@sun.com)
087: *
088: */
089: public class MouseVisualizer extends DefaultVisualizer {
090:
091: private static final long BEFORE_CLICK = 100;
092:
093: /**
094: * A constant used to inform that window activating click
095: * needs to performed on the <b>top</b> side of frame.
096: * @see #MouseVisualizer()
097: */
098: public static int TOP = 0;
099:
100: /**
101: * A constant used to inform that window activating click
102: * needs to performed on the <b>botton</b> side of frame.
103: * @see #MouseVisualizer()
104: */
105: public static int BOTTOM = 1;
106:
107: /**
108: * A constant used to inform that window activating click
109: * needs to performed on the <b>left</b> side of frame.
110: * @see #MouseVisualizer()
111: */
112: public static int LEFT = 2;
113:
114: /**
115: * A constant used to inform that window activating click
116: * needs to performed on the <b>right</b> side of frame.
117: * @see #MouseVisualizer()
118: */
119: public static int RIGHT = 3;
120:
121: private int place = 0;
122: private double pointLocation = 0;
123: private int depth = 0;
124: private boolean checkMouse = false;
125:
126: /**
127: * Creates a visualizer which clicks on (0, 0) window coords.
128: */
129: public MouseVisualizer() {
130: }
131:
132: /**
133: * Creates a visualizer which clicks on window boder.
134: * In case if <code>place == BOTTOM</code>, for example
135: * clicks on (width * pointLocation, height - depth) coordinates.
136: * @param place One of the predefined value: TOP, BOTTOM, LEFT, RIGHT
137: * @param pointLocation Proportial coordinates to click.
138: * @param depth Distance from the border.
139: * @param checkMouse Check if there is any java component under mouse
140: */
141: public MouseVisualizer(int place, double pointLocation, int depth,
142: boolean checkMouse) {
143: this .place = place;
144: this .pointLocation = pointLocation;
145: this .depth = depth;
146: this .checkMouse = checkMouse;
147: }
148:
149: static {
150: Timeouts.initDefault("MouseVisualiser.BeforeClickTimeout",
151: BEFORE_CLICK);
152: }
153:
154: protected boolean isWindowActive(WindowOperator winOper) {
155: return (super .isWindowActive(winOper) && (winOper.getSource() instanceof Frame || winOper
156: .getSource() instanceof Dialog));
157: }
158:
159: protected void makeWindowActive(WindowOperator winOper) {
160: JemmyProperties.getCurrentTimeouts().create(
161: "MouseVisualiser.BeforeClickTimeout").sleep();
162: super .makeWindowActive(winOper);
163: if (!System.getProperty("java.version").startsWith("1.2")) {
164: Point p = getClickPoint(winOper);
165: new MouseRobotDriver(winOper.getTimeouts().create(
166: "EventDispatcher.RobotAutoDelay")).clickMouse(
167: winOper, p.x, p.y, 1, winOper
168: .getDefaultMouseButton(), 0,
169: winOper.getTimeouts().create(
170: "ComponentOperator.MouseClickTimeout"));
171: }
172: }
173:
174: private Point getClickPoint(WindowOperator win) {
175: int x, y;
176: if (place == LEFT || place == RIGHT) {
177: y = ((int) (win.getHeight() * pointLocation - 1));
178: if (place == RIGHT) {
179: x = win.getWidth() - 1 - depth;
180: } else {
181: x = depth;
182: }
183: } else {
184: x = ((int) (win.getWidth() * pointLocation - 1));
185: if (place == BOTTOM) {
186: y = win.getHeight() - 1 - depth;
187: } else {
188: y = depth;
189: }
190: }
191: if (x < 0) {
192: x = 0;
193: }
194: if (x >= win.getWidth()) {
195: x = win.getWidth() - 1;
196: }
197: if (y < 0) {
198: y = 0;
199: }
200: if (y >= win.getHeight()) {
201: y = win.getHeight() - 1;
202: }
203: return (new Point(x, y));
204: }
205: }
|