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.drivers.scrolling;
048:
049: import java.awt.Adjustable;
050: import java.awt.Point;
051:
052: import org.netbeans.jemmy.Timeout;
053:
054: import org.netbeans.jemmy.drivers.ScrollDriver;
055: import org.netbeans.jemmy.drivers.LightSupportiveDriver;
056:
057: import org.netbeans.jemmy.operators.ComponentOperator;
058:
059: /**
060: * Superclass for all scroll drivers.
061: * Contains all the logic of scrolling.
062: * Tryes allowed operations in this order:
063: * "jump", "drag'n'drop", "push'n'wait", "step".
064: * Repeats "step" scrolling while scroller value is not equal
065: * to the necessary value, but no more than <code>ADJUST_CLICK_COUNT</code>.
066: *
067: * @author Alexandre Iline(alexandre.iline@sun.com)
068: */
069: public abstract class AbstractScrollDriver extends
070: LightSupportiveDriver implements ScrollDriver {
071:
072: /**
073: * Maximal number of attemps to reach required position
074: * by minimal scrolling operation.
075: */
076: public static final int ADJUST_CLICK_COUNT = 10;
077:
078: /**
079: * Constructs an AbstractScrollDriver.
080: * @param supported an array of supported class names
081: */
082: public AbstractScrollDriver(String[] supported) {
083: super (supported);
084: }
085:
086: public void scroll(ComponentOperator oper, ScrollAdjuster adj) {
087: if (canJump(oper)) {
088: doJumps(oper, adj);
089: }
090: if (canDragAndDrop(oper)) {
091: doDragAndDrop(oper, adj);
092: }
093: if (canPushAndWait(oper)) {
094: doPushAndWait(oper, adj);
095: }
096: for (int i = 0; i < ADJUST_CLICK_COUNT; i++) {
097: doSteps(oper, adj);
098: }
099: }
100:
101: /**
102: * Performs minimal scrolling step.
103: * @param oper an operator.
104: * @param adj a scroll adjuster
105: */
106: protected abstract void step(ComponentOperator oper,
107: ScrollAdjuster adj);
108:
109: /**
110: * Performs maximal scroll step.
111: * @param oper an operator.
112: * @param adj a scroll adjuster
113: */
114: protected abstract void jump(ComponentOperator oper,
115: ScrollAdjuster adj);
116:
117: /**
118: * Presses something like a scroll button.
119: * @param oper an operator.
120: * @param direction - one of the ScrollAdjister.INCREASE_SCROLL_DIRECTION,
121: * ScrollAdjister.DECREASE_SCROLL_DIRECTION, ScrollAdjister.DO_NOT_TOUCH_SCROLL_DIRECTION values.
122: * @param orientation one of the Adjustable.HORIZONTAL or Adjustable.VERTICAL values.
123: */
124: protected abstract void startPushAndWait(ComponentOperator oper,
125: int direction, int orientation);
126:
127: /**
128: * Releases something like a scroll button.
129: * @param oper an operator.
130: * @param direction - one of the ScrollAdjister.INCREASE_SCROLL_DIRECTION,
131: * ScrollAdjister.DECREASE_SCROLL_DIRECTION, ScrollAdjister.DO_NOT_TOUCH_SCROLL_DIRECTION values.
132: * @param orientation one of the Adjustable.HORIZONTAL or Adjustable.VERTICAL values.
133: */
134: protected abstract void stopPushAndWait(ComponentOperator oper,
135: int direction, int orientation);
136:
137: /**
138: * Starts drag'n'drop scrolling.
139: * @param oper an operator.
140: * @return start drigging point.
141: */
142: protected abstract Point startDragging(ComponentOperator oper);
143:
144: /**
145: * Drop at a specified point.
146: * @param oper an operator.
147: * @param pnt the point to drop.
148: */
149: protected abstract void drop(ComponentOperator oper, Point pnt);
150:
151: /**
152: * Drag to a specified point.
153: * @param oper an operator.
154: * @param pnt the point to drag to.
155: */
156: protected abstract void drag(ComponentOperator oper, Point pnt);
157:
158: /**
159: * Returns a timeout for sleeping between verifications during
160: * "push and wait" scrolling.
161: * @param oper an operator.
162: * @return a timeout
163: */
164: protected abstract Timeout getScrollDeltaTimeout(
165: ComponentOperator oper);
166:
167: /**
168: * Tells if this driver allows to perform drag'n'drop scrolling.
169: * @param oper an operator.
170: * @return true if this driver allows to drag'n'drop.
171: */
172: protected abstract boolean canDragAndDrop(ComponentOperator oper);
173:
174: /**
175: * Tells if this driver allows to perform jumps.
176: * @param oper an operator.
177: * @return true if this driver allows to jump.
178: */
179: protected abstract boolean canJump(ComponentOperator oper);
180:
181: /**
182: * Tells if this driver allows to perform "push and wait" scrolling.
183: * @param oper an operator.
184: * @return true if this driver allows to "push and wait".
185: */
186: protected abstract boolean canPushAndWait(ComponentOperator oper);
187:
188: /**
189: * Returns a number of pixels in one drag and drop scrolling.
190: * @param oper an operator.
191: * @return drag'n'drop step length.
192: */
193: protected abstract int getDragAndDropStepLength(
194: ComponentOperator oper);
195:
196: /**
197: * Performs drag'n'drop scrolling till scroller's value
198: * does not cross required value.
199: * @param oper an operator.
200: * @param adj a scroll adjuster
201: */
202: protected void doDragAndDrop(ComponentOperator oper,
203: ScrollAdjuster adj) {
204: int direction = adj.getScrollDirection();
205: if (direction != adj.DO_NOT_TOUCH_SCROLL_DIRECTION) {
206: Point pnt = startDragging(oper);
207: while (adj.getScrollDirection() == direction) {
208: drag(oper, pnt = increasePoint(oper, pnt, adj,
209: direction));
210: }
211: drop(oper, pnt);
212: }
213: }
214:
215: /**
216: * Performs jump scrolling till scroller's value
217: * does not cross required value.
218: * @param oper an operator.
219: * @param adj a scroll adjuster
220: */
221: protected void doJumps(ComponentOperator oper, ScrollAdjuster adj) {
222: int direction = adj.getScrollDirection();
223: if (direction != adj.DO_NOT_TOUCH_SCROLL_DIRECTION) {
224: while (adj.getScrollDirection() == direction) {
225: jump(oper, adj);
226: }
227: }
228: }
229:
230: /**
231: * Performs "push and wait" scrolling till scroller's value
232: * does not cross required value.
233: * @param oper an operator.
234: * @param adj a scroll adjuster
235: */
236: protected void doPushAndWait(ComponentOperator oper,
237: ScrollAdjuster adj) {
238: int direction = adj.getScrollDirection();
239: int orientation = adj.getScrollOrientation();
240: if (direction != adj.DO_NOT_TOUCH_SCROLL_DIRECTION) {
241: Timeout delta = getScrollDeltaTimeout(oper);
242: startPushAndWait(oper, direction, orientation);
243: while (adj.getScrollDirection() == direction) {
244: delta.sleep();
245: }
246: stopPushAndWait(oper, direction, orientation);
247: }
248: }
249:
250: /**
251: * Performs minimal scrollings till scroller's value
252: * does not cross required value.
253: * @param oper an operator.
254: * @param adj a scroll adjuster
255: */
256: protected void doSteps(ComponentOperator oper, ScrollAdjuster adj) {
257: int direction = adj.getScrollDirection();
258: if (direction != adj.DO_NOT_TOUCH_SCROLL_DIRECTION) {
259: while (adj.getScrollDirection() == direction) {
260: step(oper, adj);
261: }
262: }
263: }
264:
265: private Point increasePoint(ComponentOperator oper, Point pnt,
266: ScrollAdjuster adj, int direction) {
267: return ((adj.getScrollOrientation() == Adjustable.HORIZONTAL) ? new Point(
268: pnt.x + ((direction == 1) ? 1 : -1)
269: * getDragAndDropStepLength(oper), pnt.y)
270: : new Point(pnt.x, pnt.y + ((direction == 1) ? 1 : -1)
271: * getDragAndDropStepLength(oper)));
272: }
273: }
|