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 javax.swing.JButton;
053: import javax.swing.JScrollBar;
054:
055: import org.netbeans.jemmy.ComponentSearcher;
056: import org.netbeans.jemmy.QueueTool;
057: import org.netbeans.jemmy.Timeout;
058:
059: import org.netbeans.jemmy.drivers.DriverManager;
060: import org.netbeans.jemmy.drivers.MouseDriver;
061:
062: import org.netbeans.jemmy.operators.ComponentOperator;
063: import org.netbeans.jemmy.operators.JButtonOperator;
064: import org.netbeans.jemmy.operators.JScrollBarOperator;
065: import org.netbeans.jemmy.operators.Operator;
066:
067: /**
068: * ScrollDriver for javax.swing.JScrollBar component type.
069: *
070: * @author Alexandre Iline(alexandre.iline@sun.com)
071: */
072: public class JScrollBarDriver extends AbstractScrollDriver {
073: private final static int SMALL_INCREMENT = 1;
074: private final static int MINIMAL_DRAGGER_SIZE = 5;
075: private final static int RELATIVE_DRAG_STEP_LENGTH = 20;
076:
077: private QueueTool queueTool;
078:
079: /**
080: * Constructs a JScrollBarDriver.
081: */
082: public JScrollBarDriver() {
083: super (
084: new String[] { "org.netbeans.jemmy.operators.JScrollBarOperator" });
085: queueTool = new QueueTool();
086: }
087:
088: public void scrollToMinimum(ComponentOperator oper, int orientation) {
089: startDragging(oper);
090: Point pnt = new Point(0, 0);
091: drag(oper, pnt);
092: Timeout sleepTime = oper.getTimeouts().create(
093: "Waiter.TimeDelta");
094: while (((JScrollBarOperator) oper).getValue() > ((JScrollBarOperator) oper)
095: .getMinimum()) {
096: sleepTime.sleep();
097: }
098: drop(oper, pnt);
099: }
100:
101: public void scrollToMaximum(ComponentOperator oper, int orientation) {
102: startDragging(oper);
103: Point pnt = new Point(oper.getWidth() - 1, oper.getHeight() - 1);
104: drag(oper, pnt);
105: Timeout sleepTime = oper.getTimeouts().create(
106: "Waiter.TimeDelta");
107: while (((JScrollBarOperator) oper).getValue() > (((JScrollBarOperator) oper)
108: .getMaximum() - ((JScrollBarOperator) oper)
109: .getVisibleAmount())) {
110: sleepTime.sleep();
111: }
112: drop(oper, pnt);
113: }
114:
115: protected void step(ComponentOperator oper, ScrollAdjuster adj) {
116: JButtonOperator boper = findAButton(oper, adj
117: .getScrollDirection());
118: DriverManager.getButtonDriver(boper).push(boper);
119: }
120:
121: protected void jump(final ComponentOperator oper,
122: final ScrollAdjuster adj) {
123: final JButtonOperator lessButton = findAButton(oper,
124: adj.DECREASE_SCROLL_DIRECTION);
125: final JButtonOperator moreButton = findAButton(oper,
126: adj.INCREASE_SCROLL_DIRECTION);
127: queueTool.invokeSmoothly(new QueueTool.QueueAction(
128: "Choise expanding") {
129: public Object launch() {
130: if (adj.getScrollDirection() != ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION) {
131: int x, y;
132: if (((JScrollBarOperator) oper).getOrientation() == JScrollBar.HORIZONTAL) {
133: if (adj.getScrollDirection() == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
134: x = moreButton.getX() - 1;
135: } else if (adj.getScrollDirection() == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
136: x = lessButton.getX()
137: + lessButton.getWidth();
138: } else {
139: return (null);
140: }
141: y = lessButton.getHeight() / 2;
142: } else if (((JScrollBarOperator) oper)
143: .getOrientation() == JScrollBar.VERTICAL) {
144: if (adj.getScrollDirection() == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
145: y = moreButton.getY() - 1;
146: } else if (adj.getScrollDirection() == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
147: y = lessButton.getY()
148: + lessButton.getHeight();
149: } else {
150: return (null);
151: }
152: x = lessButton.getWidth() / 2;
153: } else {
154: return (null);
155: }
156: DriverManager.getMouseDriver(oper).clickMouse(oper,
157: x, y, 1, oper.getDefaultMouseButton(), 0,
158: new Timeout("", 0));
159: }
160: return (null);
161: }
162: });
163: }
164:
165: protected void startPushAndWait(ComponentOperator oper,
166: int direction, int orientation) {
167: JButtonOperator boper = findAButton(oper, direction);
168: DriverManager.getButtonDriver(boper).press(boper);
169: }
170:
171: protected void stopPushAndWait(ComponentOperator oper,
172: int direction, int orientation) {
173: JButtonOperator boper = findAButton(oper, direction);
174: DriverManager.getButtonDriver(boper).release(boper);
175: }
176:
177: protected Point startDragging(ComponentOperator oper) {
178: JButtonOperator lessButton = findAButton(oper,
179: ScrollAdjuster.DECREASE_SCROLL_DIRECTION);
180: JButtonOperator moreButton = findAButton(oper,
181: ScrollAdjuster.INCREASE_SCROLL_DIRECTION);
182: Point pnt = getClickPoint((JScrollBarOperator) oper,
183: lessButton, moreButton, ((JScrollBarOperator) oper)
184: .getValue());
185: MouseDriver mdriver = DriverManager.getMouseDriver(oper);
186: mdriver.moveMouse(oper, pnt.x, pnt.y);
187: mdriver.pressMouse(oper, pnt.x, pnt.y, oper
188: .getDefaultMouseButton(), 0);
189: return (pnt);
190: }
191:
192: protected void drop(ComponentOperator oper, Point pnt) {
193: DriverManager.getMouseDriver(oper).releaseMouse(oper, pnt.x,
194: pnt.y, oper.getDefaultMouseButton(), 0);
195: }
196:
197: protected void drag(ComponentOperator oper, Point pnt) {
198: DriverManager.getMouseDriver(oper).dragMouse(oper, pnt.x,
199: pnt.y, oper.getDefaultMouseButton(), 0);
200: }
201:
202: protected Timeout getScrollDeltaTimeout(ComponentOperator oper) {
203: return (oper.getTimeouts()
204: .create("ScrollbarOperator.DragAndDropScrollingDelta"));
205: }
206:
207: protected boolean canDragAndDrop(ComponentOperator oper) {
208: if (!isSmallIncrement((JScrollBarOperator) oper)) {
209: return (false);
210: }
211: boolean result = false;
212: MouseDriver mdriver = DriverManager.getMouseDriver(oper);
213: JButtonOperator less = findAButton(oper,
214: ScrollAdjuster.DECREASE_SCROLL_DIRECTION);
215: JButtonOperator more = findAButton(oper,
216: ScrollAdjuster.INCREASE_SCROLL_DIRECTION);
217: Point pnt = getClickPoint((JScrollBarOperator) oper, less,
218: more, ((JScrollBarOperator) oper).getValue());
219: mdriver.moveMouse(oper, pnt.x, pnt.y);
220: mdriver.pressMouse(oper, pnt.x, pnt.y, oper
221: .getDefaultMouseButton(), 0);
222: result = ((JScrollBarOperator) oper).getValueIsAdjusting();
223: mdriver.releaseMouse(oper, pnt.x, pnt.y, oper
224: .getDefaultMouseButton(), 0);
225: return (result && isSmallIncrement((JScrollBarOperator) oper));
226: }
227:
228: protected boolean canJump(ComponentOperator oper) {
229: return (isSmallIncrement((JScrollBarOperator) oper));
230: }
231:
232: protected boolean canPushAndWait(ComponentOperator oper) {
233: return (isSmallIncrement((JScrollBarOperator) oper));
234: }
235:
236: protected int getDragAndDropStepLength(ComponentOperator oper) {
237: JButtonOperator less = findAButton(oper,
238: ScrollAdjuster.DECREASE_SCROLL_DIRECTION);
239: JButtonOperator more = findAButton(oper,
240: ScrollAdjuster.INCREASE_SCROLL_DIRECTION);
241: int width = oper.getWidth() - less.getWidth() - more.getWidth();
242: int height = oper.getHeight() - less.getHeight()
243: - more.getHeight();
244: int max = (width > height) ? width : height;
245: if (max >= RELATIVE_DRAG_STEP_LENGTH * 2) {
246: return ((int) (max / RELATIVE_DRAG_STEP_LENGTH));
247: } else {
248: return (1);
249: }
250: }
251:
252: private boolean isSmallIncrement(JScrollBarOperator oper) {
253: return (oper.getUnitIncrement(-1) <= SMALL_INCREMENT && oper
254: .getUnitIncrement(1) <= SMALL_INCREMENT);
255: }
256:
257: private Point getClickPoint(JScrollBarOperator oper,
258: JButtonOperator lessButton, JButtonOperator moreButton,
259: int value) {
260: int lenght = (oper.getOrientation() == JScrollBar.HORIZONTAL) ? oper
261: .getWidth()
262: - lessButton.getWidth() - moreButton.getWidth()
263: : oper.getHeight() - lessButton.getHeight()
264: - moreButton.getHeight();
265: int subpos = (int) (((float) lenght / (oper.getMaximum() - oper
266: .getMinimum())) * value);
267: if (oper.getOrientation() == JScrollBar.HORIZONTAL) {
268: subpos = subpos + lessButton.getWidth();
269: } else {
270: subpos = subpos + lessButton.getHeight();
271: }
272: subpos = subpos + MINIMAL_DRAGGER_SIZE / 2 + 1;
273: return ((oper.getOrientation() == JScrollBar.HORIZONTAL) ? new Point(
274: subpos, oper.getHeight() / 2)
275: : new Point(oper.getWidth() / 2, subpos));
276: }
277:
278: private JButtonOperator findAButton(ComponentOperator oper,
279: int direction) {
280: return ((direction == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) ? ((JScrollBarOperator) oper)
281: .getDecreaseButton()
282: : ((JScrollBarOperator) oper).getIncreaseButton());
283: }
284: }
|