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 javax.swing.JButton;
050: import javax.swing.JSplitPane;
051:
052: import org.netbeans.jemmy.ComponentSearcher;
053:
054: import org.netbeans.jemmy.drivers.ButtonDriver;
055: import org.netbeans.jemmy.drivers.DriverManager;
056: import org.netbeans.jemmy.drivers.LightSupportiveDriver;
057: import org.netbeans.jemmy.drivers.ScrollDriver;
058:
059: import org.netbeans.jemmy.operators.ComponentOperator;
060: import org.netbeans.jemmy.operators.ContainerOperator;
061: import org.netbeans.jemmy.operators.JButtonOperator;
062: import org.netbeans.jemmy.operators.JSplitPaneOperator;
063:
064: /**
065: * ScrollDriver for javax.swing.JSplitPane component type.
066: *
067: * @author Alexandre Iline(alexandre.iline@sun.com)
068: */
069: public class JSplitPaneDriver extends LightSupportiveDriver implements
070: ScrollDriver {
071:
072: /**
073: * Constructs a JSplitPaneDriver.
074: */
075: public JSplitPaneDriver() {
076: super (
077: new String[] { "org.netbeans.jemmy.operators.JSplitPaneOperator" });
078: }
079:
080: public void scroll(ComponentOperator oper, ScrollAdjuster adj) {
081: moveDividerTo((JSplitPaneOperator) oper, adj);
082: }
083:
084: public void scrollToMinimum(ComponentOperator oper, int orientation) {
085: expandTo((JSplitPaneOperator) oper, 0);
086: }
087:
088: public void scrollToMaximum(ComponentOperator oper, int orientation) {
089: expandTo((JSplitPaneOperator) oper, 1);
090: }
091:
092: private void moveDividerTo(JSplitPaneOperator oper,
093: ScrollAdjuster adj) {
094: ContainerOperator divOper = oper.getDivider();
095: /* workaround */
096: if (oper.getDividerLocation() == -1) {
097: moveTo(oper, divOper, divOper.getCenterX() - 1, divOper
098: .getCenterY() - 1);
099: if (oper.getDividerLocation() == -1) {
100: moveTo(oper, divOper, divOper.getCenterX() + 1, divOper
101: .getCenterY() + 1);
102: }
103: }
104: if (oper.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
105: moveOnce(oper, divOper, adj, 0, oper.getWidth());
106: } else {
107: moveOnce(oper, divOper, adj, 0, oper.getHeight());
108: }
109: }
110:
111: private void moveOnce(JSplitPaneOperator oper,
112: ContainerOperator divOper, ScrollAdjuster adj,
113: int leftPosition, int rightPosition) {
114: int currentLocation = oper.getDividerLocation();
115: int currentPosition = 0;
116: if (oper.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
117: currentPosition = (int) (divOper.getLocationOnScreen()
118: .getX() - oper.getLocationOnScreen().getX());
119: } else {
120: currentPosition = (int) (divOper.getLocationOnScreen()
121: .getY() - oper.getLocationOnScreen().getY());
122: }
123: int nextPosition = 0;
124: if (adj.getScrollDirection() == adj.DECREASE_SCROLL_DIRECTION) {
125: nextPosition = (int) ((currentPosition + leftPosition) / 2);
126: moveToPosition(oper, divOper, nextPosition
127: - currentPosition);
128: if (currentPosition == (int) (divOper.getLocationOnScreen()
129: .getY() - oper.getLocationOnScreen().getY())) {
130: return;
131: }
132: moveOnce(oper, divOper, adj, leftPosition, currentPosition);
133: } else if (adj.getScrollDirection() == adj.INCREASE_SCROLL_DIRECTION) {
134: nextPosition = (int) ((currentPosition + rightPosition) / 2);
135: moveToPosition(oper, divOper, nextPosition
136: - currentPosition);
137: if (currentPosition == (int) (divOper.getLocationOnScreen()
138: .getY() - oper.getLocationOnScreen().getY())) {
139: return;
140: }
141: moveOnce(oper, divOper, adj, currentPosition, rightPosition);
142: } else { // (currentLocation == dividerLocation) - stop point
143: return;
144: }
145: }
146:
147: private void moveTo(JSplitPaneOperator oper,
148: ComponentOperator divOper, int x, int y) {
149: DriverManager.getMouseDriver(divOper).dragNDrop(
150: divOper,
151: divOper.getCenterX(),
152: divOper.getCenterY(),
153: x,
154: y,
155: oper.getDefaultMouseButton(),
156: 0,
157: oper.getTimeouts().create(
158: "ComponentOperator.BeforeDragTimeout"),
159: oper.getTimeouts().create(
160: "ComponentOperator.AfterDragTimeout"));
161: }
162:
163: private void moveToPosition(JSplitPaneOperator oper,
164: ComponentOperator divOper, int nextPosition) {
165: if (System.getProperty("java.version").startsWith("1.2")) {
166: oper.setDividerLocation(nextPosition);
167: }
168: if (oper.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
169: moveTo(oper, divOper, divOper.getCenterX() + nextPosition,
170: divOper.getCenterY());
171: } else {
172: moveTo(oper, divOper, divOper.getCenterX(), divOper
173: .getCenterY()
174: + nextPosition);
175: }
176: }
177:
178: private void expandTo(JSplitPaneOperator oper, int index) {
179: ContainerOperator divOper = oper.getDivider();
180: JButtonOperator bo = new JButtonOperator((JButton) divOper
181: .waitSubComponent(new JButtonOperator.JButtonFinder(
182: ComponentSearcher.getTrueChooser("JButton")),
183: index));
184: bo.copyEnvironment(divOper);
185: ButtonDriver bdriver = DriverManager.getButtonDriver(bo);
186: bdriver.push(bo);
187: bdriver.push(bo);
188: }
189: }
|