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.Point;
050:
051: import javax.swing.JSlider;
052:
053: import org.netbeans.jemmy.QueueTool;
054: import org.netbeans.jemmy.Timeout;
055:
056: import org.netbeans.jemmy.drivers.DriverManager;
057: import org.netbeans.jemmy.drivers.MouseDriver;
058:
059: import org.netbeans.jemmy.operators.ComponentOperator;
060: import org.netbeans.jemmy.operators.JSliderOperator;
061: import org.netbeans.jemmy.operators.Operator;
062:
063: /**
064: * A scroll driver serving JSlider component.
065: *
066: * @author Alexandre Iline(alexandre.iline@sun.com)
067: */
068: public class JSliderDriver extends AbstractScrollDriver {
069: private QueueTool queueTool;
070:
071: /**
072: * Constructs a JSliderDriver object.
073: */
074: public JSliderDriver() {
075: super (
076: new String[] { "org.netbeans.jemmy.operators.JSliderOperator" });
077: queueTool = new QueueTool();
078: }
079:
080: public void scrollToMinimum(final ComponentOperator oper,
081: int orientation) {
082: checkSupported(oper);
083: scroll(oper, new ScrollAdjuster() {
084: public int getScrollDirection() {
085: return ((((JSliderOperator) oper).getMinimum() < ((JSliderOperator) oper)
086: .getValue()) ? DECREASE_SCROLL_DIRECTION
087: : DO_NOT_TOUCH_SCROLL_DIRECTION);
088: }
089:
090: public int getScrollOrientation() {
091: return (((JSliderOperator) oper).getOrientation());
092: }
093:
094: public String getDescription() {
095: return ("Scroll to minimum");
096: }
097: });
098: }
099:
100: public void scrollToMaximum(final ComponentOperator oper,
101: int orientation) {
102: checkSupported(oper);
103: scroll(oper, new ScrollAdjuster() {
104: public int getScrollDirection() {
105: return ((((JSliderOperator) oper).getMaximum() > ((JSliderOperator) oper)
106: .getValue()) ? INCREASE_SCROLL_DIRECTION
107: : DO_NOT_TOUCH_SCROLL_DIRECTION);
108: }
109:
110: public int getScrollOrientation() {
111: return (((JSliderOperator) oper).getOrientation());
112: }
113:
114: public String getDescription() {
115: return ("Scroll to maximum");
116: }
117: });
118: }
119:
120: protected void step(final ComponentOperator oper,
121: final ScrollAdjuster adj) {
122: if (adj.getScrollDirection() != ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION) {
123: queueTool.invokeSmoothly(new QueueTool.QueueAction(
124: "Choise expanding") {
125: public Object launch() {
126: Point clickPoint = getClickPoint(oper, adj
127: .getScrollDirection(), adj
128: .getScrollOrientation());
129: if (clickPoint != null) {
130: DriverManager
131: .getMouseDriver(oper)
132: .clickMouse(
133: oper,
134: clickPoint.x,
135: clickPoint.y,
136: 1,
137: Operator
138: .getDefaultMouseButton(),
139: 0,
140: oper
141: .getTimeouts()
142: .create(
143: "ComponentOperator.MouseClickTimeout"));
144: }
145: return (null);
146: }
147: });
148: }
149: }
150:
151: protected void jump(ComponentOperator oper, ScrollAdjuster adj) {
152: //cannot
153: }
154:
155: protected void startPushAndWait(final ComponentOperator oper,
156: final int direction, final int orientation) {
157: queueTool.invokeSmoothly(new QueueTool.QueueAction(
158: "Start scrolling") {
159: public Object launch() {
160: Point clickPoint = getClickPoint(oper, direction,
161: orientation);
162: if (clickPoint != null) {
163: MouseDriver mdriver = DriverManager
164: .getMouseDriver(oper);
165: mdriver.moveMouse(oper, clickPoint.x, clickPoint.y);
166: mdriver.pressMouse(oper, clickPoint.x,
167: clickPoint.y, Operator
168: .getDefaultMouseButton(), 0);
169: }
170: return (null);
171: }
172: });
173: }
174:
175: protected void stopPushAndWait(final ComponentOperator oper,
176: final int direction, final int orientation) {
177: queueTool.invokeSmoothly(new QueueTool.QueueAction(
178: "Stop scrolling") {
179: public Object launch() {
180: Point clickPoint = getClickPoint(oper, direction,
181: orientation);
182: if (clickPoint != null) {
183: MouseDriver mdriver = DriverManager
184: .getMouseDriver(oper);
185: mdriver.releaseMouse(oper, clickPoint.x,
186: clickPoint.y, Operator
187: .getDefaultMouseButton(), 0);
188: }
189: return (null);
190: }
191: });
192: }
193:
194: protected Point startDragging(ComponentOperator oper) {
195: //cannot
196: return (null);
197: }
198:
199: protected void drop(ComponentOperator oper, Point pnt) {
200: //cannot
201: }
202:
203: protected void drag(ComponentOperator oper, Point pnt) {
204: //cannot
205: }
206:
207: protected Timeout getScrollDeltaTimeout(ComponentOperator oper) {
208: return (oper.getTimeouts()
209: .create("JSliderOperator.ScrollingDelta"));
210: }
211:
212: protected boolean canDragAndDrop(ComponentOperator oper) {
213: return (false);
214: }
215:
216: protected boolean canJump(ComponentOperator oper) {
217: return (false);
218: }
219:
220: protected boolean canPushAndWait(ComponentOperator oper) {
221: return (true);
222: }
223:
224: protected int getDragAndDropStepLength(ComponentOperator oper) {
225: return (0);
226: }
227:
228: private Point getClickPoint(ComponentOperator oper, int direction,
229: int orientation) {
230: int x, y;
231: boolean inverted = ((JSliderOperator) oper).getInverted();
232: int realDirection = ScrollAdjuster.DO_NOT_TOUCH_SCROLL_DIRECTION;
233: if (inverted) {
234: if (direction == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
235: realDirection = ScrollAdjuster.DECREASE_SCROLL_DIRECTION;
236: } else if (direction == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
237: realDirection = ScrollAdjuster.INCREASE_SCROLL_DIRECTION;
238: } else {
239: return (null);
240: }
241: } else {
242: realDirection = direction;
243: }
244: if (orientation == JSlider.HORIZONTAL) {
245: if (realDirection == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
246: x = oper.getWidth() - 1;
247: } else if (realDirection == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
248: x = 0;
249: } else {
250: return (null);
251: }
252: y = oper.getHeight() / 2;
253: } else if (orientation == JSlider.VERTICAL) {
254: if (realDirection == ScrollAdjuster.INCREASE_SCROLL_DIRECTION) {
255: y = 0;
256: } else if (realDirection == ScrollAdjuster.DECREASE_SCROLL_DIRECTION) {
257: y = oper.getHeight() - 1;
258: } else {
259: return (null);
260: }
261: x = oper.getWidth() / 2;
262: } else {
263: return (null);
264: }
265: return (new Point(x, y));
266: }
267: }
|